简体   繁体   English

使用 PaymentIntents API 将产品添加到付款

[英]Adding products to a payment with PaymentIntents API

I just finished setting up payments through stripe on PHP using their payment intents API and forgot about having to add the products/items the user has just purchased.我刚刚使用他们的付款意图 API 通过 PHP 上的条带完成了付款设置,并且忘记了必须添加用户刚刚购买的产品/项目。

Looking through the API Reference I didn't see anything where I could add items to the payment intent so I could retreive what was purchased later on in the webhook.查看 API 参考,我没有看到任何可以将项目添加到付款意图的地方,因此我可以检索稍后在 webhook 中购买的内容。

Do I need to do my own magic and somehow add the items to the metadata array or is there something that i'm missing here?我需要做我自己的魔法并以某种方式将项目添加到元数据数组中还是我在这里遗漏了什么?

PaymentIntents do not track any sort of product that's connected to the purchase (just an amount). PaymentIntents 不跟踪与购买相关的任何类型的产品(只是一个金额)。 In order to link Products to a payment, you'd want to use Invoices which will generate a PaymentIntent as part of the process.为了将产品链接到付款,您需要使用 Invoices ,它将生成 PaymentIntent 作为流程的一部分。

I'm using node.js.我正在使用 node.js。 Here's what worked for me:以下是对我有用的内容:

Steps脚步

  1. Create customer object创建客户对象
  2. Create invoice item创建发票项目
  3. Create invoice创建发票
  4. Finalize invoice (returns a payment intent id but not a secret)完成发票(返回付款意图 ID 但不是秘密)
  5. Retrieve the payment intent object by id (secret key must be used).通过 id 检索支付意图对象(必须使用密钥)。

Step #5 response will contain the client_secret value that can be sent to the client for payment capture.第 5 步响应将包含client_secret值,该值可以发送到客户端进行支付捕获。

Nodes.js code Node.js 代码

const customer = ...
const invoiceItem = await stripe.invoiceItems.create({
  customer: ...
  price_data: { ... }
})
const invoice = await stripe.invoices.create({
  customer: ...
})
const finalInvoice = await stripe.invoices.finalizeInvoice(
  invoice.id
)
const paymentIntent = await stripe.paymentIntents.retrieve(
  finalInvoice.payment_intent
)

res.send({ secret: paymentIntent.client_secret })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM