您无法使用 PaymentMethod 创建费用。请改用支付意图 API

Sta*_*soz 2 .net stripe-payments reactjs

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

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);
    }
  } 
Run Code Online (Sandbox Code Playgroud)

后端

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)
        {

        }
    }
Run Code Online (Sandbox Code Playgroud)

我知道您无法使用 PaymentMethod 创建费用。请改用付款意图 API。 我如何使用客户端的令牌发送到条纹付款?

hmu*_*noz 5

Stripe 上的费用 API (/v1/charges) 可与令牌、卡(附加令牌)和来源配合使用。

PaymentIntents API (/v1/ payment_intents) 可与 PaymentMethods 配合使用(技术上也可与遗留附加令牌(又名 Card 对象)和 Sources 配合使用,但后两者与您的用例无关)。

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

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