Enrol a customer
Create the customer and put them straight onto a card. The response carries the Apple Wallet and Google Wallet links, so you can send them yourself.
const customer = await leal.customers.create({
account_id: storeId,
customer: { first_name: "Ada", email: "ada@example.com" },
card_id: cardId,
});
const card = customer.customer_cards[0];
console.log(card.apple_wallet_url, card.google_wallet_url); Add a stamp
The call most integrations need. Leal records the stamp, updates the wallet pass and sends the notification.
const card = await leal.customerCards.stamp({
account_id: storeId,
customer_id: customerId,
id: customerCardId,
stamps: 1,
});
console.log(`${card.stamps_remaining} stamps to the next reward`); Redeem a reward
Spend the stamps a customer has earned. Leal checks the reward is available before recording the redemption.
await leal.customerCards.redeem({
account_id: storeId,
customer_id: customerId,
id: customerCardId,
reward_id: rewardId,
}); Handle a failure
Every failure is a typed error carrying the status code, so you can tell a missing record from a rate limit without reading strings.
import { LealError } from "@lealhq/leal";
try {
await leal.customerCards.stamp({ account_id: storeId, customer_id: customerId, id: customerCardId, stamps: 1 });
} catch (error) {
if (error instanceof LealError && error.statusCode === 429) {
// Wait for the number of seconds in the Retry-After header, then try again.
}
throw error;
}