Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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.

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?

like image 923
dobson Avatar asked Dec 10 '25 23:12

dobson


2 Answers

PaymentIntents do not track any sort of product that's connected to the purchase (just an amount). In order to link Products to a payment, you'd want to use Invoices which will generate a PaymentIntent as part of the process.

like image 183
taintedzodiac Avatar answered Dec 13 '25 11:12

taintedzodiac


I'm using 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)
  5. Retrieve the payment intent object by id (secret key must be used).

Step #5 response will contain the client_secret value that can be sent to the client for payment capture.

Nodes.js code

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 })
like image 32
srmark Avatar answered Dec 13 '25 13:12

srmark



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!