Interactive commerce patterns built with the Merchant.js SDK that you can copy/paste into your projects.
const merchant = new Merchant({ apiKey: 'YOUR_KEY' });
merchant.checkout
.create({
items: [
{
product_id: 'SKU-12345',
price: 19900,
quantity: 1,
name: 'Custom Graphic T-Shirt',
variant: 'Unisex Medium'
}
],
successUrl: 'https://your-store.com/success',
cancelUrl: 'https://your-store.com/cancel'
})
.then(url => window.location = url);
const subscription = merchant.subscriptions.create({
plan: {
id: 'pro_monthly',
amount: 29900, // cents
interval: 'month',
},
trialDays: 7,
customer: {
email: 'user@example.com',
name: 'Jane Smith'
}
});
subscription.on('charge_succeeded', () => {
// Notify customer
merchant.notifications.email({
to: subscription.customer.email,
template: 'subscription_success',
data: {
nextCharge: formatDate(subscription.nextChargeAt),
plan: subscription.plan.name
}
});
});
async function updateInventory(product, quantity) {
const { stockAvailable } = await merchant.inventory.get(product.sku);
if (stockAvailable >= quantity) {
await merchant.inventory.decrement(product.sku, quantity);
return true;
} else {
merchant.tracking.log('inventory_low', {
sku: product.sku,
requested: quantity,
available: stockAvailable
});
return false;
}
}
// Usage
updateInventory(product, orderedQuantity)
.then(isAvailable => {
if (!isAvailable) {
merchant.notifications.error('Item temporarily unavailable');
}
});