我不明白 Stripe 快速结帐示例代码:https://docs.stripe.com/elements/express-checkout-element/accept-a- payment。
金额和货币是硬编码的。如果有人购买了多种产品 - 或者以另一种货币付款怎么办?
客户端
expressCheckoutElement.on('confirm', async (event) => {
const {error: submitError} = await elements.submit();
if (submitError) {
handleError(submitError);
return;
}
// Create the PaymentIntent and obtain clientSecret
const res = await fetch('/create-intent', {
method: 'POST',
});
const {client_secret: clientSecret} = await res.json();
const {error} = await stripe.confirmPayment({
// `elements` instance used to create the Express Checkout Element
elements,
// `clientSecret` from the created PaymentIntent
clientSecret,
confirmParams: {
return_url: 'https://example.com/order/123/complete',
},
});
if (error) {
// This point is only reached if there's an immediate error when
// confirming the payment. Show the error to your customer (for example, payment details incomplete)
handleError(error);
} else {
// The payment UI automatically closes with a success animation.
// Your customer is redirected to your `return_url`.
}
});
服务器端
const stripe = require("stripe")("my precious");
const express = require('express');
const app = express();
app.use(express.static("."));
app.post('/create-intent', async (req, res) => {
const intent = await stripe.paymentIntents.create({
amount: 1099,
currency: 'usd',
// In the latest version of the API, specifying the `automatic_payment_methods` parameter is optional because Stripe enables its functionality by default.
automatic_payment_methods: {enabled: true},
});
res.json({client_secret: intent.client_secret});
});
app.listen(3000, () => {
console.log('Running on port 3000');
});
请帮助新手。
我看到了一些在帖子请求正文中传递此信息的建议:
客户端
const res = await fetch('/create-intent', {
method: 'POST',
body: JSON.stringify({
amount: myamount,
currency: mycurrency
)}
});
服务器端:
const intent = await stripe.paymentIntents.create({
amount: req.body.amount,
currency: req.body.currency,
// In the latest version of the API, specifying the `automatic_payment_methods` parameter is optional because Stripe enables its functionality by default.
automatic_payment_methods: {enabled: true},
});
但目前还不清楚如何:
请 - 帮助新手。
您可以考虑使用Web会话来对收费金额进行评分,并确保您在前端设置的金额
options
与后端PaymentIntent一致。否则,如果您的客户支付的金额与 Google Pay/Apple Pay 付款表上看到的金额不同,他们可能会提出争议。