1. Follow the Quickstart guide
Follow the Quickstart guide to create a SaleSnip session.
2. Create a checkout session
Create a Paddle coupon and checkout session from the SaleSnip negotiation session.
https://api.example.com/checkout
import Decimal from "decimal.js";
import axios from "axios";
import { Paddle } from 'paddle-node-sdk';
const paddle = new Paddle(process.env.PADDLE_API_KEY);
const session = await axios.get(`https://api.salesnip.com/v1/sessions/${req.body.session}`, {
headers: {
'X-Api-Key': process.env.SALESNIP_API_KEY,
},
});
const totalDiscount = session.products.reduce((acc, product) => {
const initial = new Decimal(product.price.initial);
const current = new Decimal(product.price.current);
return acc.plus(initial.minus(current));
}, new Decimal(0));
let discount = null;
if (totalDiscount.gt(0)) {
discount = await paddle.discounts.create({
description: `Negotiation Discount`,
type: 'flat',
currency_code: session.currency,
amount: totalDiscount.toFixed(0),
usage_limit: 1,
});
}
res.status(200).json({
products: session.products.map((product) => ({
priceId: product.id,
quantity: product.quantity.current,
})),
discount: discount.id
});
3. Open the Paddle checkout
Call your checkout API route to start the Paddle checkout process.
const res = await fetch("(YOUR_API_URL)/checkout", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
session: data.id,
}),
});
// TODO: Handle the error
if (!res.ok) throw new Error("Failed to start checkout");
const body = await res.json();
Paddle.Checkout.open({
items: body.products,
discountId: body.discount,
// Other options
});