【发布时间】:2020-09-22 11:35:03
【问题描述】:
所以我在我的前端进行条带结帐:
const StripeCheckoutButton = ({ price }) => {
const priceForStripe = price * 100;
const publishableKey = "pk_test_QxG1LDucNFRDvVPJcgoQDsph00A2YvKyS4";
const onToken = (token) => {
axios({
url: "payment",
method: "post",
data: {
amount: priceForStripe,
token: token,
},
})
.then((response) => {
alert("succesful payment");
})
.catch((error) => {
console.log("Payment Error: ", error);
alert(
"There was an issue with your payment! Please make sure you use the provided credit card."
);
});
};
我为我的可发布密钥制作变量,并制作组件:
<StripeCheckout
label="Pay now"
name=
billingAddress
shippingAddress
description={`Your total is $${price}`}
amount={priceForStripe}
panelLabel="Pay now"
token={onToken}
stripeKey={publishableKey}
/>
显然我将刚刚删除的名称字符串粘贴到此处...
之后我在后端制作 server.js 来处理付款:
const express = require("express");
const cors = require("cors");
const bodyParser = require("body-parser");
const path = require("path");
const enforce = require("express-sslify");
if (process.env.NODE_ENV !== "production") require("dotenv").config();
const stripe = require("stripe")(process.env.STRIPE_SECRET_KEY);
const app = express();
const port = process.env.PORT || 5000;
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(enforce.HTTPS({ trustProtoHeader: true }));
app.use(cors());
if (process.env.NODE_ENV === "production") {
app.use(express.static(path.join(__dirname, "client/build")));
app.get("*", function (req, res) {
res.sendFile(path.join(__dirname, "client/build", "index.html"));
});
}
app.listen(port, (error) => {
if (error) throw error;
console.log("Server running on port " + port);
});
app.get("/service-worker.js", (req, res) => {
res.sendFile(path.resolve(__dirname, "..", "build", "service-worker.js"));
});
app.post("/payment", (req, res) => {
const body = {
source: req.body.token.id,
amount: req.body.amount,
currency: "usd",
};
stripe.charges.create(body, (stripeErr, stripeRes) => {
if (stripeErr) {
res.status(500).send({ error: stripeErr });
} else {
res.status(200).send({ success: stripeRes });
}
});
});
我在我的根目录中创建了 .env 变量,以存储我的密钥,STRIPE_SECRET_KEY=mykey 像这样,当我想付款时,我得到了 403 错误,我检查了所有内容,我的密钥,我的包,我的 .env文件,我不知道问题所在。
也许我使用的是旧版本,这就是问题所在。 有谁知道问题出在哪里?
【问题讨论】:
标签: reactjs express stripe-payments