Value of empty coupon ID -STRIPE

Viewed 106

I am creating a checkout session for a subscription and I sometimes have a coupon ID and sometimes not. I was wondering about the value to which I should set the $coupon_id variable when there is no coupon. Should It be set to 'none' or empty string '' ?

$coupon_id = $ID;
} 
else {
$coupon_id = ''; //or 'none' ? 
}

$session = \Stripe\Checkout\Session::create([
  'payment_method_types' => ['card'],
  'line_items' => [[
    'price' => $plan_id,
    'quantity' => 1,
  ]],
  'mode' => 'subscription',
  'discounts' => [[
    'coupon' => $coupon_id,
  ]],
  'success_url' => 'https://example.com/success',
  'cancel_url' => 'https://example.com/cancel',
]);
1 Answers

Are you allowing your users to enter the promotion code in the Checkout page? If so, set the allow_promotion_codes parameter when creating the Session.

Otherwise, omit the discounts parameter entirely if there's no coupon to apply.

https://stripe.com/docs/api/checkout/sessions/create#create_checkout_session-allow_promotion_codes

$params = [
  'payment_method_types' => ['card'],
  'line_items' => [[
    'price' => $plan_id,
    'quantity' => 1,
  ]],
  'mode' => 'subscription',
  'success_url' => 'https://example.com/success',
  'cancel_url' => 'https://example.com/cancel',
];

if ($coupon_id) {
    $params['discounts'] = [[
        'coupon' => $coupon_id
    ]]
}

$session = \Stripe\Checkout\Session::create($params);
Related