Retrieve which promotion code was used with Stripe

Viewed 382

After a payment was made with Stripe I want to know which promotion code was used.

After the redirect to my website, I can successfully retrieve information about that session with:

\Stripe\Checkout\Session::retrieve([SESSION_ID]);

And also information about the specific payment with:

\Stripe\paymentIntent::retrieve([PAYMENT_INTENT]);

But neither of those include information about which promotion code was used, just the amount that was paid.

Is it also possible to find out which promo code was used?

2 Answers

You can get this from the Checkout Sessions total_details by including breakdown (API ref) with expansion:

curl  https://api.stripe.com/v1/checkout/sessions/cs_test_a1b2c3 \
  -u sk_test_123: \
  --data 'expand[]=total_details.breakdown'

While you can't expand all the way down due to the depth limit of 4 layers, you cane get the promo code ID. If you want the corresponding code then you could retrieve that Promo code object and inspect that.

Elaborating on Nolan's answer, this was what I ended up with in PHP:

$checkout_session = \Stripe\Checkout\Session::retrieve([
    'id' => SESSION_ID,
    'expand' => ['total_details.breakdown'],
  ]);

Then I can retrieve the used promotion code with:

$total_details['breakdown']['discounts'][0]['discount']['coupon']['name']
Related