2 min read

Stripe discount codes

Stripe discount codes
Photo by Markus Winkler / Unsplash

The following PHP code will dynamically create a Stripe checkout link. These are my working notes on this - will update as I go!

public function getStripeLink() {   
    $session_data = [
      'payment_method_types' => ['card'],
      'line_items' => [[
        'price_data' => [
          'currency' => $this->getCurrency(),
          'product_data' => [
            'name' => $this->eventName,
            'description' => $this->eventName . $description,
          ],
          'unit_amount' => $this->getPrice() * 100,
          'tax_behavior' => 'exclusive',
        ],
        'quantity' => 1,
      ]],
      'mode' => 'payment',
      'success_url' => $success_url,
      'cancel_url' => $cancel_url,
      'client_reference_id' => $sid,
      'automatic_tax' => [
        'enabled' => true,
      ],
    ];

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

    return $session->url;
}

I need to allow discount codes in the Stripe checkout. There are two options I looked at:

  1. Customer add the discount code
  2. Everyone gets the discount

Add coupon in Stripe

This is found under Product catalogue and Coupons. I’ve added the Coupon and the promotion code with the same name. The promotion code is what the customer enters.

Options for discounts

There are a few options to allow discount codes.

Option 1: user adds the code

This is the simplest option. The customer simply adds the promotion code during the checkout (obviously you need to send it to them first).

Add the following:

'allow_promotion_codes' => true,

So it becomes:

public function getStripeLink() {   
  $session_data = [
      'payment_method_types' => ['card'],
      'line_items' => [[
        'price_data' => [
          'currency' => $this->getCurrency(),
          'product_data' => [
            'name' => $this->eventName,
            'description' => $this->eventName . $description,
          ],
          'unit_amount' => $this->getPrice() * 100,
          'tax_behavior' => 'exclusive',
        ],
        'quantity' => 1,
      ]],
      'mode' => 'payment',
      'allow_promotion_codes' => true,
      'success_url' => $success_url,
      'cancel_url' => $cancel_url,
      'client_reference_id' => $sid,
      'automatic_tax' => [
        'enabled' => true,
      ],
    ];

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

    return $session->url;
}

Option 2: Everyone gets the discount

For everyone to get the discount, you can pass in the discount code to the checkout. If you have already adding the coupon and discount code like I have (see screen shot at the top), you can add it like so:

$discount_code = 'promo_1RbjsfJ3X6AKFUXKo0xhLFOD';
if (!empty($discount_code)) {
   $session_data['discounts'] = [[
      'promotion_code' => $discount_code,
   ]];
}

Note, the discount code is the API ID of the promotion code, not the promotion code itself. It will appear as the promotion code to the customer though.

The full code:

public function getStripeLink() {  
    $discount_code = 'promo_1RbjsfJ3X6AKFUXKo0xhLFOD'; 
    $session_data = [
      'payment_method_types' => ['card'],
      'line_items' => [[
        'price_data' => [
          'currency' => $this->getCurrency(),
          'product_data' => [
            'name' => $this->eventName,
            'description' => $this->eventName . $description,
          ],
          'unit_amount' => $this->getPrice() * 100,
          'tax_behavior' => 'exclusive',
        ],
        'quantity' => 1,
      ]],
      'mode' => 'payment',
	  'allow_promotion_codes' => true,
      'success_url' => $success_url,
      'cancel_url' => $cancel_url,
      'client_reference_id' => $sid,
      'automatic_tax' => [
        'enabled' => true,
      ],
    ];

    if (!empty($discount_code)) 
       $session_data['discounts'] = [[
       'promotion_code' => $discount_code,
      ]];
    }

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

    return $session->url;
}