程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Stripe 从基于令牌的费用迁移到基于支付意图的费用大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决Stripe 从基于令牌的费用迁移到基于支付意图的费用?

开发过程中遇到Stripe 从基于令牌的费用迁移到基于支付意图的费用的问题如何解决?下面主要结合日常开发的经验,给出你关于Stripe 从基于令牌的费用迁移到基于支付意图的费用的解决方法建议,希望对你解决Stripe 从基于令牌的费用迁移到基于支付意图的费用有所启发或帮助;

以前我在另一个名为 Zuora 的第三方服务中使用 Stripe。我在前端生成了这样的令牌

const tokenoptions = {
  name: form.fullname,address_country: form.country,address_zip: form.zip
};

const cardElement = StripeElements.getElement(CardElement);

const { error,token } = await Stripe.createtoken(
  cardElement,tokenoptions
);

// use `token.ID` on the BE to make the charge

然后将 token.ID 传递到后端,后端到达 Zuora 的端点,该端点实际上使用此令牌 ID 进行收费。

但显然令牌方法不支持我们想要实现的 3D Secure。然而,使用支付意图的另一种方法确实支持 SD 安全。所以我用以下代码替换了上面的代码:

const getClIEntSecret = await fetch('http://localhost:3000/API/create-payment-intent',{
  method: 'POST',headers: {
    'Content-Type': 'application/Json'
  }
});

const { clIEnt_secret } = await getClIEntSecret.Json();

const paymenTintent = await Stripe.confirmCardPayment(clIEnt_secret,{
  payment_method: {
    card: cardElement,billing_details: {
      name: 'Foo bar',email: 'foo@bar.com'
    }
  }
});

我在后端的 create-payment-intent 端点如下所示:

router.post("/API/create-payment-intent",async (req,res,next) => {
  try {
    const paymenTintent = await Stripe.paymenTintents.create({
      amount: 100,currency: "usd",capture_method: "manual",});
    res.status(200).Json({
      clIEnt_secret: paymenTintent.clIEnt_secret,});
  } catch (err) {
    res.status(500).Json({ error: err.message });
  }
});

现在这个电话:

const paymenTintent = await Stripe.confirmCardPayment(clIEnt_secret,email: 'foo@bar.com'
    }
  }
});

像这样返回一个对象:

paymenTintent: {
  amount: 100,canceled_at: null,cancellation_reason: null,clIEnt_secret: "some-secret-ID-here",confirmation_method: "automatic",created: 1626287910,description: null,ID: "pi_1JDCyYCmZFHcp9eoF2MoVeyg",last_payment_error: null,livemode: false,next_action: null,object: "payment_intent",payment_method: "pm_1JDCyYCmZFHcp9eoZHAfkoCX",payment_method_types: ["card"],receipt_email: null,setup_future_usage: null,shipPing: null,source: null,status: "requires_capture"
}

我的问题是我可以使用返回对象中的值之一代替我之前使用的令牌 ID,以便稍后从 Zuora 进行收费吗?

我可以为此使用 paymenTintent.ID 吗?

解决方法

澄清一下,您的代码正在创建付款。当您调用 confirmCardPayment() 时,您的代码会在幕后创建 Charge。

具体来说,它已经“授权”了一项费用,但尚未捕获,这可以从您响应中 PaymenTintent 的 status: "requires_capture" 状态看出。

如果您想向 Zoura 发送一些东西以供稍后收费,那么您要寻找的是现代等效于 Token 的东西,即 PaymentMethod 对象。

在您的回复中,有问题的 PaymentMethod 是 payment_method: "pm_1JDCyYCmZFHcp9eoZHAfkoCX", 中回复的 confirmCardPayment 字段。

该 PaymentMethod 代表一张卡(类似于旧令牌所做的),这是您稍后创建费用所需的内容。

大佬总结

以上是大佬教程为你收集整理的Stripe 从基于令牌的费用迁移到基于支付意图的费用全部内容,希望文章能够帮你解决Stripe 从基于令牌的费用迁移到基于支付意图的费用所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。