saq*_*aq7 34 node.js cors stripe-payments
我正在尝试使用此处给出的说明集成 Stripe Checkout https://stripe.com/docs/ payments/ accept-a- payment?integration=checkout 节点。
我已按照他们的说明进行了操作,并使用我帐户中的实际(测试)密钥更新了示例中的 API 密钥。
我在前端使用React,在后端使用express。我已经启用了cors。
React 向后端的请求成功,并向条带发起预检请求。来自 stripe 的预检响应是 403,实际请求因 CORS 错误而被阻止 -PreflightMissingAllowOriginHeader
const express = require("express");
const app = express();
const cors = require("cors");
const stripe = require("stripe")(
process.env.STRIPE_SECRET_KEY
);
app.use(
cors({
origin: ["http://localhost:8000", "https://checkout.stripe.com"],
})
);
app.post("/create-checkout-session", async (req, res) => {
console.log('getting here 1')
const session = await stripe.checkout.sessions.create({
payment_method_types: ["card"],
line_items: [
{
price_data: {
currency: "usd",
product_data: {
name: "T-shirt",
},
unit_amount: 2000,
},
quantity: 1,
},
],
mode: "payment",
success_url: "http://localhost:4242/success.html",
cancel_url: "http://localhost:4242/cancel.html",
});
console.log('getting here 2')
res.redirect(303, session.url);
});
app.listen(4242, () => console.log(`Listening on port ${4242}!`));
Run Code Online (Sandbox Code Playgroud)
handleClick = () => {
const res = fetch(`${process.env.BACKEND_API_URL}/create-checkout-session`, {
method: 'POST',
headers: {
"Content-Type": 'text/html'
}
})
}
Run Code Online (Sandbox Code Playgroud)
saq*_*aq7 60
以下是我在尝试调试时学到的一些东西。
text/plain。(是的,没错,我的服务器返回带 Stripe 的 url 的 303 后,Chrome 不会向 Stripe 发送 OPTIONS 请求)这是分别解决问题的更新后的后端和前端代码
app.post("/create-checkout-session", async (req, res) => {
const session = await stripe.checkout.sessions.create({
payment_method_types: ["card"],
line_items: [
{
price_data: {
currency: "usd",
product_data: {
name: "T-shirt",
},
unit_amount: 2000,
},
quantity: 1,
},
],
mode: "payment",
success_url: "http://localhost:8000/success",
cancel_url: "http://localhost:8000/cancel",
});
res.json({url: session.url}) // <-- this is the changed line
});
Run Code Online (Sandbox Code Playgroud)
handleClick = async () => {
const res = await fetch(`${process.env.BACKEND_API_URL}/create-checkout-session`, {
method: 'POST',
headers: {
"Content-Type": 'application/json'
}
})
const body = await res.json()
window.location.href = body.url
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
20568 次 |
| 最近记录: |