1. Follow the Quickstart guide

Follow the Quickstart guide to create a SaleSnip session.

2. Create a checkout session

Create a Shopify discount code and checkout session from the SaleSnip negotiation session.

import axios from "axios";
import Shopify from "shopify-api-node";

const shopify = new Shopify({
  shopName: process.env.SHOP_NAME,
  apiKey: process.env.SHOPIFY_API_KEY,
  password: process.env.SHOPIFY_API_SECRET,
});

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 = parseFloat(product.price.initial);
  const current = parseFloat(product.price.current);

  return acc + (initial - current);
}, 0);

let discount = null;

if (totalDiscount > 0) {
  discount = await shopify.discountCode.create({
    code: `NEGOTIATION_DISCOUNT_${Date.now()}`,
    price_rule: {
      title: "Negotiation Discount",
      target_type: "line_item",
      target_selection: "all",
      allocation_method: "across",
      value_type: "fixed_amount",
      value: -totalDiscount.toFixed(2),
      customer_selection: "all",
      starts_at: new Date().toISOString(),
      once_per_customer: true,
    },
  });
}

const lineItems = session.products.map((product) => ({
  variant_id: product.id,
  quantity: product.quantity.current,
  price: product.price.initial,
}));

const checkout = await shopify.checkout.create({
  email: req.body.customer_email,
  line_items: lineItems,
  discount_codes: discount ? [{ code: discount.code }] : [],
  currency: session.currency,
});

res.status(200).json({ checkout: checkout.web_url });

3. Redirect the User

Call your checkout API route from the previous step and redirect the user to the checkout URL.