John Davidson

php - Retrieve which promotion code was used with Stripe

0 comments
Message:


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?



My Answer:
Unfortunately, Stripe does not store information about the promotion code used in the payment details or session information. If you want to track which promotion code was used, you will need to store this information yourself when the payment is made.

One way to do this is to include the promotion code as metadata when creating the payment intent or session. You can then retrieve this metadata when you retrieve the payment intent or session information.

For example, when creating the payment intent, you can include the promotion code as metadata like this:

php
$paymentIntent = \Stripe\PaymentIntent::create([
'amount' => 1000,
'currency' => 'usd',
'metadata' => [
'promotion_code' => 'PROMO123'
]
]);


Then, when retrieving the payment intent information, you can access the metadata like this:

php
$paymentIntent = \Stripe\PaymentIntent::retrieve([PAYMENT_INTENT]);
$promotionCode = $paymentIntent->metadata['promotion_code'];


By storing the promotion code as metadata when creating the payment intent or session, you can easily retrieve this information later to track which promotion code was used for the payment.

Rate this post

4 of 5 based on 6620 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us