6

I'm using the custom checkout flow from stripe to add online payment to my website which uses a server endpoint to create the PaymentIntent like so:

app.post("/create-payment-intent", async (req, res) => {
  const { items } = req.body;
  // Create a PaymentIntent with the order amount and currency
  const paymentIntent = await stripe.paymentIntents.create({
    amount: 499,
    currency: "usd"
  });
  res.send({
    clientSecret: paymentIntent.client_secret
  });
});

I was wondering, instead of setting the price and currency directly in the PaymentIntent, could that come from a Product created on Stripe? I understand that I can just fo it myself and fetch the associated Product and Price from the API and create my PaymentIntent from that but then Stripe will have no way to "track" that this payment was made for this product (useful for analytics).

I know that this is possible to do with Checkout where a product can be associated with it. But I couldn't find anything for a custom checkout flow.

Cheers!

Théo Champion
  • 1,701
  • 1
  • 21
  • 46
  • Does this answer your question? [Adding products to a payment with PaymentIntents API](https://stackoverflow.com/questions/59687006/adding-products-to-a-payment-with-paymentintents-api) – Nelu Jun 29 '22 at 13:34

2 Answers2

2

An option, until associating Prices and Products directly with PaymentIntents is supported, would be to leverage metadata on the PaymentIntent to track which Prices contribute to the final amount [1]. Then you would have the opportunity to record that in your DB or query that from the PaymentIntents API.

[1] https://stripe.com/docs/api/payment_intents/object#payment_intent_object-metadata

v3nkman
  • 1,011
  • 4
  • 3
0

Add this product to Metadata of PaymentIntentCreateParams as following

Product order = new Product();
Map<String, String> metadata = new HashMap<>();
metadata.put("productId", order.getProductId());
metadata.put("description", order.getDescription());

PaymentIntentCreateParams paymentIntentParams =  PaymentIntentCreateParams.builder().putAllMetadata(metadata)
    .setAmount(amountPaymentIntent).setCurrency(currencyPaymentIntent).setCustomer(customer.getId())
    .build();

 PaymentIntent paymentIntent = PaymentIntent.create(paymentIntentParams);
brianha289
  • 338
  • 1
  • 8