Integrate SaleSnip with Paddle.
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 });
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 });