Integrate SaleSnip with Stripe.
import Decimal from "decimal.js"; import Stripe from "stripe"; import axios from "axios"; const stripe = new Stripe(process.env.STRIPE_SECRET_KEY, { apiVersion: "[YOUR_API_VERSION]", }); 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 coupon = null; if (totalDiscount.gt(0)) { coupon = await stripe.coupons.create({ name: `Negotiation Discount`, amount_off: totalDiscount.times(100).toNumber(), currency: session.currency.toLowerCase(), max_redemptions: 1, }); } const checkout = await stripe.checkout.sessions.create({ line_items: session.products.map((product) => ({ price_data: { currency: session.currency.toLowerCase(), product_data: { name: product.name, images: [product.image], }, unit_amount: product.price.current * 100, recurring: { interval: product.recurring.unit.toLowerCase(), interval_count: product.recurring.count, }, // TODO: If you're using trial periods, you need to create the // subscriptions with stripe.subscriptions.create first and then // add the subscription id to the line items. }, quantity: product.quantity.current, })), discounts: coupon ? [{ coupon: coupon.id }] : [], // Other options }); res.status(200).json({ checkout: checkout.url });