【问题标题】:You cannot create a charge with a PaymentMethod. Use the Payment Intents API instead您不能使用 PaymentMethod 创建费用。改用支付意图 API
【发布时间】:2021-07-03 10:48:02
【问题描述】:

用于从条带获取令牌并将其发送到后端的部分客户端代码

const cardElement = elements.getElement(CardElement);
const {error, paymentMethod} = await stripe.createPaymentMethod({
   type: 'card',
   card: cardElement,
});

    try {
      const { id } = paymentMethod;
      console.log(id)
      const response = await axios.post(
        "https://localhost:44325/api/Payment",
        {
          token: id,
        }
      );

  
    } catch (error) {
      console.log("CheckoutForm.js 28 | ", error);
    }
  } 

后台

public async Task Test(string token)
        {
            StripeConfiguration.ApiKey = "SecretKey";
        var options = new ChargeCreateOptions()
        {
            Amount = 12345,
            Currency = "usd",

            Description = "Donation from me",
            Source = token
        };
        var service = new ChargeService();

        try
        {
            var charge = service.Create(options);
        }
        catch (StripeException e)
        {

        }
    }

我明白了 您无法使用 PaymentMethod 创建费用。请改用 Payment Intents API。 如何使用来自客户端的令牌发送条带付款?

【问题讨论】:

    标签: .net reactjs stripe-payments


    【解决方案1】:

    Stripe 上的 Charges API (/v1/charges) 可与令牌、卡(附加令牌)和来源一起使用。

    PaymentIntents API (/v1/payment_intents) 与 PaymentMethods 一起使用(技术上也与旧的附加令牌(也称为 Card 对象)和 Sources 一起使用,但后两者与您的用例无关)。

    您的集成正在创建一个 PaymentMethod 对象。但是你的后端正在创建一个 Charge,它不能一起工作。

    您需要创建一个 PaymentIntent,创建它并传递confirm: true 参数以在其上创建付款。 https://stripe.com/docs/api/payment_intents/create

    【讨论】:

      【解决方案2】:

      以防万一您仍想在服务器端使用收费 API 而不是 PaymentIntents,因为它是异步的(基于 webhook)。

      然后在客户端的这种情况下,您可以使用条带元素以及 stripe.createToken 方法。只需将任何条带卡元素传递给此方法,它就会为您生成一个令牌,将此令牌发送到服务器端以创建费用。

      stripe.createToken

      【讨论】:

      • stripe.createToken 现在被视为旧版。来自 Stripe:“代币被认为是遗留的,使用 [PaymentMethod] 和 [PaymentIntent]”
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-06-26
      • 2020-12-01
      • 2018-07-31
      • 1970-01-01
      • 2013-08-20
      • 1970-01-01
      • 2020-06-01
      相关资源
      最近更新 更多