从 .NET 调用 JS 方法

Calling a JS method from .NET

我需要有关从 .NET c# 后端调用 Javascript 方法的帮助。

据我了解,我需要从后端获取 paymentIntent 并将其 post 发送到客户端,然后调用 stripe.confirmCardPayment

这是 Javascript 的样子:

    // Pass the failed PaymentIntent to your client from your server
stripe.confirmCardPayment(intent.client_secret, {
  payment_method: intent.last_payment_error.payment_method.id
}).then(function(result) {
  if (result.error) {
    // Show error to your customer
    console.log(result.error.message);
  } else {
    if (result.paymentIntent.status === 'succeeded') {
      // The payment is complete!
    }
  }
});

这是我的 .NET C# 代码的样子:

try
{
  var service = new PaymentIntentService();
  var options = new PaymentIntentCreateOptions
  {
    Amount = 1099,
    Currency = "usd",
    Customer = "{{CUSTOMER_ID}}",
    PaymentMethod = "{{PAYMENT_METHOD_ID}}",
    Confirm = true,
    OffSession = true,
  };
  service.Create(options);
}
catch (StripeException e)
{
  switch (e.StripeError.ErrorType)
  {
    case "card_error":
      // Error code will be authentication_required if authentication is needed
      Console.WriteLine("Error code: " + e.StripeError.Code);
      var paymentIntentId = e.StripeError.PaymentIntent.Id;
      var service = new PaymentIntentService();
      var paymentIntent = service.Get(paymentIntentId);

      Console.WriteLine(paymentIntent.Id);
      break;
    default:
      break;
  }
}

此流程包含在 guide to accepting a payment 中。在每个服务器端代码示例中,您可以 select“.NET”选项卡查看您首选服务器语言的代码。

您没有说明您最初是如何收集付款详细信息的,因此我假设您使用的是已知客户已关联的付款方式。您也没有显示如何将支付数据发送回客户端,但在您的 javascript 片段中,您似乎拥有整个 intent 对象。我不建议这样做,而是建议只发送您需要的东西。在这里,比如说,只是 client_secretpayment_method_id 用于 confirmCardPayment.