Atari's avatar
Level 9

Coupons for one-time payments and subscriptions using Stripe / Cashier

I am using a custom checkout flow, not Stripe Checkout, so using payment element.

I need to create coupons and promo codes like 15% off coupon with promo code 15OFF.

Using stripe coupons with subscriptions is fine, thats all working, its one-time payments thats the issue. I cant use Stripe coupons with one-time payments. So i would need to manually discount the paymentIntent amount based on coupon, that's fine, that works.

But how do i handle coupon redemption limits, if in one-time payments stripe doesnt know the coupon was used as i manually discounted the paymentIntent amount how can i track coupon redemptions?

0 likes
2 replies
LaryAI's avatar
Level 58

To handle coupon redemption limits for one-time payments, you can implement a custom solution using Stripe's metadata feature. Here's a step-by-step guide on how to achieve this:

  1. Create a coupon in Stripe with the desired discount and redemption limit. Note down the coupon ID.

  2. When creating the PaymentIntent for the one-time payment, include the coupon ID in the metadata of the PaymentIntent. You can do this by adding a metadata key to the options parameter when creating the PaymentIntent.

$paymentIntent = \Stripe\PaymentIntent::create([
    // other parameters
    'amount' => 1000, // example amount
    'currency' => 'usd', // example currency
    'metadata' => [
        'coupon_id' => 'your_coupon_id',
    ],
]);
  1. After the payment is successful, retrieve the PaymentIntent from Stripe and check the metadata to determine if a coupon was used.
$paymentIntent = \Stripe\PaymentIntent::retrieve('pi_123456789'); // replace with your PaymentIntent ID

if ($paymentIntent->metadata->coupon_id) {
    // Coupon was used, handle accordingly
} else {
    // No coupon was used
}
  1. To track coupon redemptions, you can maintain a separate database table or collection in your application. Whenever a payment is successful and a coupon was used, increment the redemption count for that coupon.

  2. Before applying the coupon discount manually to the paymentIntent amount, check if the redemption count has reached the limit. If it has, you can choose to reject the payment or apply a different discount.

Remember to handle any race conditions or concurrency issues that may arise when multiple payments are being processed simultaneously.

Note: This solution assumes you are using the Stripe PHP library. Adjust the code accordingly if you are using a different language or library.

Please or to participate in this conversation.