House of Jeans is a Swiss jeans retailer with physical stores and a Shopify store. Its gift cards are sold at the till, and they live in the till system, not in Shopify. Customers who had one in their wallet wanted to spend it online as well. I built the storefront side of that: a gift card field in the cart drawer and on the cart page, with the whole flow in German.

One balance, one place

The tempting shortcut is to copy the cards into Shopify. That creates two balances for the same card, and the day a customer spends half of it in a store, the online copy is wrong. So the till system stays the only source of truth. The theme never decides whether a card is valid; it asks a small backend service, which asks the till system and answers with three things: is the card valid, what is left on it, and if not, why.

The answers map to short messages a customer understands: the card does not exist, it has expired, it has already been added, or it could not be checked right now and they should try again. A network problem is never reported as "invalid card".

Several cards, one order

Gift cards are often small, so a customer may bring two or three. The cart applies them in the order they were added, and each card covers as much of what is still open as its balance allows:

let remaining = cartTotal;
for (const card of giftCards) {
  card.applied = Math.min(card.balance, remaining);
  remaining -= card.applied;
}

The idea in its simplest form. A card that is not needed, because the earlier ones already cover the order, is applied with zero and keeps its whole balance.

When the cart changes, for example a quantity goes up or an item is removed, the split is calculated again, so the amounts in the drawer always match the cart.

Checked again on every page load

The added cards are kept in sessionStorage, so they survive moving between pages in the same tab and disappear when the tab is closed. That alone is not enough, because the card can change while it sits in the online cart: it can be spent in a store in the meantime.

So every time a page loads, the cart asks again for each stored card. A card that is no longer valid, or has nothing left on it, is dropped and the totals are recalculated. If the check itself fails, for example because the connection drops for a moment, the card stays: a customer should not lose a gift card because of a network hiccup.

Checkout with a price the cart promised

Shopify's checkout knows nothing about cards from the till system. When a customer with gift cards presses the checkout button, the theme stops the normal submit and sends the cart contents and the amount taken from each card to the backend. The backend creates a draft order with the reduced total and sends back a checkout link for it, and the browser goes there. The price at checkout is the price the cart showed.

If that request fails, the theme falls back to the normal checkout. The gift card is then not applied, but the customer can still buy. A checkout button that does nothing is the worst outcome of all, so the code avoids it on purpose.

What I would keep from this

  • Leave the balance where it is managed, and ask for it each time instead of copying it.
  • Check stored state again when a page loads; a cart can sit open for hours.
  • Tell "this card is not valid" apart from "I could not check right now".
  • Every step that can fail at checkout needs a path that still lets the customer pay.

Further reading