This is automatically synced with vercel
The following directories and their contents are Copyright Yannick Schutz. You may not reuse anything therein without my permission:
- content/
All other directories and files are MIT Licensed.
The shop uses Stripe Checkout
It is a set of 2 serverless functions running on Vercel, a data file and a template.
api/checkout.js: Responsible for creating a session on stripe checkout. It uses data/shop.json file to map the SKU invented code to the Stripeprice_id. Right now a lot is hardcoded in the function.api/success.js: Used to retrieve customer information on the callback after a sale. Goal is to show a banner thanking the user.
The template has a form handled by JS in main.js where we first post to the checkout serverless function. Then with the session_id, we redirect to stripe for the payment. We are not handling any money data down here.
The frontmatter of a shop item requires price_id which is the price code of a stripe product
A shop page points at a Stripe product with product_id. Its price is the product's default
price, its stock is the stock / stock_total metadata of the product.
Before each build, bin/sync-prices.js reads every product_id (and
older price_id) in content/shop/, fetches it from Stripe and writes data/prices.json
(not committed). The shop/offer.html partial
uses it for the price tag, sold out state, structured data, Open Graph and the feed. Without a
live key (local dev, previews) it falls back to price, pricetag and soldout in the front matter.
Checkout and the stock check resolve the product's default price live, so a new price applies to purchases right away. The redeploy only refreshes what the pages display.
To change a price: in Stripe, add a new price on the product and set it as default. Redeploy.
Pages that only have a price_id still work: the product's default price is used once that
price is archived, and the stock is read from the price when the product has none.
bin/migrate-to-products.js adds product_id to those pages and
moves their stock to the product (dry run by default, --write to apply, needs a key that can
write products).
In page text, write {{</* price */>}} instead of the amount.
Run it locally with yarn sync-prices. A test STRIPE_SECRET_KEY (for local checkout) is skipped by the sync: set STRIPE_PRICES_KEY to a live restricted read-only key to get live prices locally. The bin/ scripts load .env (see .env.example) with dotenv; variables already set in the environment win. A restricted key with read
access to prices and products is enough.
Shipping rates live in data/shipping.json, as named sets (zine, print,
cyanotype), each with its live rates and optional testing ones (used with a test Stripe key).
A page gets the default set, or picks one with shipping: print in its front matter.
The page sends the set's name, the checkout function sends its rates to Stripe (the testing ones
with a test key) and the buyer picks one on the Checkout page. Stripe preselects the first rate, so
keep the usual paid one first.
Add new Stripe rates to a set in the data file: checkout only uses what is there.
The Configuration keys needed by the functions are:
STRIPE_SECRET_KEY: Key used to interact with the API from the functionSTRIPE_PUBLISHABLE_KEY: Key used by the frontend client to Stripe
The keys can be found at https://dashboard.stripe.com/apikeys
The flow for a payment is:
shop/single.htmlpage form gets a session_id fromapi/checkout.js- user gets redirected to stripe
- After payment, user is redirected back to the right slug
- page gets the payment information via
api/success.jsand thanks the user.