Automatically apply a promotion code to a checkout session
A promotion code can be automatically applied to a checkout session, so the shopper doesn't need to type in the promotion code.
When Smartpay checkout is launched the promotion code is automatically verified. If the code is valid the specified discount is applied to the order and displayed to the shopper. If the code is not valid (for example it expired) the discount is not applied. If you have other valid promotion codes available, the promotion code input field is shown to the shopper.
Applying the promotion code automatically
To apply the promotion code automatically to a checkout session, you need to add the promotion-code={your-promotion-code}
argument to the redirect url.
If you are using one of our SDKs, they all support the functionality.
$api = new \Smartpay\Api(getenv('SECRET_KEY'), getenv('PUBLIC_KEY'));
$checkoutSession = $api->checkoutSession([...])
return $response->withRedirect($checkoutSession->redirectUrl(['promotionCode' => 'SUMMERSALE']), 302);
require 'smartpay'
Smartpay.configure do |config|
config.public_key = ENV['PUBLIC_KEY']
config.secret_key = ENV['SECRET_KEY']
end
session = Smartpay::Api.create_checkout_session({...})
redirect session.redirect_url({promotionCode: "SUMMERSALE"})
from smartpay import Smartpay
# Replace the keys with yours
SECRET_KEY = os.environ.get('SECRET_KEY', '<YOUR_SECRET_KEY>')
PUBLIC_KEY = os.environ.get('PUBLIC_KEY', '<YOUR_PUBLIC_KEY>')
smartpay = Smartpay(SECRET_KEY, public_key=PUBLIC_KEY)
session = smartpay.create_checkout_session({...})
redirectUrl = smartpay.get_session_url(session, options={'promotionCode': 'SUMMERSALE'})
return redirect(redirectUrl, 303)
const Smartpay = require('@smartpay/sdk-node').default;
const SECRET_KEY = process.env.SECRET_KEY || '<YOUR_SECRET_KEY>';
const PUBLIC_KEY = process.env.PUBLIC_KEY || '<YOUR_PUBLIC_KEY>';
const smartpay = new Smartpay(SECRET_KEY, {
publicKey: PUBLIC_KEY,
});
(async () => {
const payload = {...};
const session = await smartpay.createCheckoutSession(payload);
})();
const sessionURL = Smartpay.getSessionURL(session, {
promotionCode: 'SUMMERSALE',
});
res.redirect(303, sessionURL);
Updated 3 months ago