为什么 Stripe Checkout 样本金额是硬编码的? (如何获取篮子具体单价、数量和币种?)

问题描述 投票:0回答:1

我不明白 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},
  });

但目前还不清楚如何:

  1. expressCheckoutElement.on('confirm', async (event) => {}) 获取这些变量 - 它们只是在其范围之外设置的全局变量吗? (例如,我们安装快速结账元素的那些)
  2. 如果我们只是将前者传递给后者,服务器端代码会验证客户端订单详细信息

请 - 帮助新手。

javascript node.js firebase google-cloud-functions stripe-payments
1个回答
0
投票

您可以考虑使用Web会话来对收费金额进行评分,并确保您在前端设置的金额

options
与后端PaymentIntent一致。否则,如果您的客户支付的金额与 Google Pay/Apple Pay 付款表上看到的金额不同,他们可能会提出争议。

© www.soinside.com 2019 - 2024. All rights reserved.