部分客户端代码,用于从 strip 获取 token 并将其发送到后端
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。 如何使用客户端的 token 发送到 stripe payment?
最佳答案
Stripe 上的费用 API (/v1/charges) 可与 token 、卡(附加 token )和来源配合使用。
PaymentIntents API (/v1/payment_intents) 可与 PaymentMethods 配合使用(技术上也可与遗留附加 token (又名 Card 对象)和 Sources 配合使用,但后两者与您的用例无关)。
您的集成正在创建一个 PaymentMethod 对象。但是你的后端正在创建一个 Charge,它不能一起工作。
您需要创建一个 PaymentIntent,创建它并传递 confirm: true
参数以在其上创建付款。 https://stripe.com/docs/api/payment_intents/create
关于.net - 您无法使用 PaymentMethod 创建费用。请改用支付意图 API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66989191/