繁体   English   中英

没有提供令牌(vue 条纹)

[英]No token Provided (vue stripe)

我正在尝试使用 stripe 进行付款,但onPurchase我点击购买时都没有收到任何令牌,有人可以帮我解决这个问题吗?

我的 Vue 模板调出 function

 <span class="a-button-inner">
 <span @click="onPurchase" class="a-button-text">Purchase</span>
 </span>

关于如何将令牌发送到条带以进行付款的脚本标签


<script>
import { mapGetters } from "vuex";
import axios from "axios";

export default {
  data() {
    return {
      error: "",
      stripe: null,
      card: null
    };
  },

  computed: {
    ...mapGetters([
      "getCart",
      "getCartTotalPriceWithShipping",
      "getEstimatedDelivery"
    ])
  },
  mounted() {
    this.stripe = window.Stripe(
      "pk_test_51KGqWkHCcyZvTrDrTmAbtZkngRWbP0FCvV3bgZnz8GXuleqD1fo1lRa5seDD3qKsk0irYLumaH3SeI5cILED3pwq00NR023dNZ"
    );
    let elements = this.stripe.elements();
    this.card = elements.create("card");
    this.card.mount(this.$refs.card);
  },
  methods: {
    
   async onPurchase() {
// this create a token to send to stripe
      let token = await this.stripe.createToken(this.card);
       return axios("http://localhost:5000/api/payment", {
        method: "POST",
        headers: {
          "Content-Type": "application/json"
        },
        body: JSON.stringify({
          token: token,
          totalPrice: this.getCartTotalPriceWithShipping,
          cart: this.getCart,
          estimatedDelivery: this.getEstimatedDelivery
        })
      })
       
        .then(data => {
          console.log(data);
        })
        .then(res => {
          console.log(res);
        });
    },
    formatPrice(value) {
      let val = (value / 1).toFixed(2).replace(".", ",");
      return val.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ".");
    }
  }
};
</script>

我在控制台中遇到的错误

{
    "success": false,
    "message": "No token Provided"
}

我的后端将付款发送到条带

const router = require("express").Router();
const moment = require("moment");
const stripe = require("stripe")(process.env.STRIPE_SECREY_KEY);
const verifyToken = require("../middelwares/verify-token");
const Order = require("../models/order");

const SHIPMENT = {
  normal: {
    price: 13.98,
    days: 7
  },
  fast: {
    price: 49.98,
    days: 3
  }
};

function shipmentPrice(shipmentOption) {
  let estimated = moment()
    .add(shipmentOption.days, "d")
    .format("dddd MMMM Do");

  return { estimated, price: shipmentOption.price };
}

router.post("/shipment", (req, res) => {
  let shipment;
  if (req.body.shipment === "normal") {
    shipment = shipmentPrice(SHIPMENT.normal);
  } else {
    shipment = shipmentPrice(SHIPMENT.fast);
  }

  res.json({ success: true, shipment: shipment });
});

router.post("/payment", verifyToken, (req, res) => {
  let totalPrice = Math.round(req.body.totalPrice * 100);
  stripe.customers
    .create({
      email: req.decoded.email
    })
    .then(customer => {
      return stripe.customers.createSource(customer.id, {
        source: "tok_visa"
      });
    })
    .then(source => {
      return stripe.charges.create({
        amount: totalPrice,
        currency: "usd",
        customer: source.customer
      });
    })
    .then(async charge => {
      let order = new Order();
      let cart = req.body.cart;

      cart.map(product => {
        order.products.push({
          productID: product._id,
          quantity: parseInt(product.quantity),
          price: product.price
        });
      });

      order.owner = req.decoded._id;
      order.estimatedDelivery = req.body.estimatedDelivery;
      await order.save();

      res.json({
        success: true,
        message: "Successfully made a payment"
      });
    })
    .catch(err => {
      res.status(500).json({
        success: false,
        message: err.message
      });
    });
});

module.exports = router;

在服务器上,您需要使用您的密钥初始化 Stripe,您在这里似乎没有这样做。

const stripe = require('stripe')('your-secret-key')

详细信息可以在 Stripe 文档中找到: https://stripe.com/docs/api/authentication?lang=node

--

编辑:在评论中解释后,OP 似乎没有使用 dotenv 文件,这意味着 process.env.STRIPE_SECRET_KEY 是 null,因此出现错误。

推荐阅读: https://www.npmjs.com/package/dotenv

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM