@proveanything/smartlinks 2.1.34 → 2.1.37

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/docs/shop.md ADDED
@@ -0,0 +1,176 @@
1
+ # Shop (basic commerce)
2
+
3
+ Sell products from any site: physical goods that ship, and digital ones (a file, a proof of ownership, access to
4
+ a members' group, or something your app delivers). Buyers pay on Stripe's own checkout page into the **seller's**
5
+ Stripe account. SmartLinks prices the basket, holds the stock, records the order, sends the emails your app sets up
6
+ and tells integrations.
7
+
8
+ - **Money is in minor units** (pence, cents): `1299` is £12.99. Prices include VAT.
9
+ - **The browser never decides an amount.** The server prices the basket from the shop's prices, and only Stripe's
10
+ webhook marks an order paid.
11
+ - **What's for sale:** a product (or variant) with an active price. One price per product per currency.
12
+ - **Stock:** with `trackStock`, the shop sells only what stock says is available and holds it while the buyer pays
13
+ (see `docs/operations.md`). An unpaid checkout lets go of its stock when it runs out (30 minutes by default).
14
+
15
+ ## A shop front
16
+
17
+ ```typescript
18
+ import { shop } from '@proveanything/smartlinks'
19
+
20
+ const config = await shop.config(collectionId) // seller details, shipping rates, policies, ready
21
+ const { items } = await shop.products(collectionId) // what's for sale, with stock and prices
22
+
23
+ for (const p of items) {
24
+ console.log(p.name, shop.formatMoney(p.amount, p.currency), p.inStock ? '' : 'sold out')
25
+ }
26
+
27
+ // Pay
28
+ const { checkoutUrl } = await shop.checkout(collectionId, {
29
+ lines: [{ productId: 'soap', quantity: 2 }],
30
+ shippingRateId: 'std', // needed for physical items when there are several rates
31
+ successUrl: `${location.origin}/thanks`, // Stripe adds ?session_id=…
32
+ cancelUrl: `${location.origin}/basket`,
33
+ })
34
+ location.href = checkoutUrl
35
+ ```
36
+
37
+ The basket is yours to keep: in the page's state or local storage. `shop.checkout` only needs the lines.
38
+
39
+ **Before paying, show** (UK consumer law, and it's what `config` is for):
40
+ - the seller's name, address and email
41
+ - the returns policy (`config.policies.returns`)
42
+ - delivery costs and times
43
+
44
+ Leave the buy button off while `config.ready` is false.
45
+
46
+ **Digital items** end the buyer's 14-day right to cancel once delivered, so ask for agreement: a checkbox that sends
47
+ `acceptances: { digitalImmediate: true }`. Without it, checkout refuses with `DIGITAL_CONSENT_REQUIRED`.
48
+
49
+ **Signing in:** guests can buy physical goods and downloads. A **proof** or **access** goes to an account, so those
50
+ items have `needsSignIn: true`. Ask the person to sign in first, or checkout refuses with `SIGN_IN_REQUIRED`.
51
+
52
+ ### The thank-you page
53
+
54
+ ```typescript
55
+ const sessionId = new URLSearchParams(location.search).get('session_id')
56
+ let order = await shop.orderBySession(collectionId, sessionId!)
57
+ // Stripe's message can take a moment: 'pending-payment' means "check again in a second or two"
58
+ if (order.status === 'pending-payment') { /* wait and ask again */ }
59
+ order.downloads // [{ name, url }] for bought files, once paid
60
+ ```
61
+
62
+ ### My orders
63
+
64
+ ```typescript
65
+ const { items } = await shop.myOrders(collectionId) // signed in: their orders, newest first, with downloads
66
+ ```
67
+
68
+ ### Errors
69
+
70
+ `checkout` fails plainly, with nothing charged:
71
+
72
+ | Code | Meaning |
73
+ |---|---|
74
+ | `SHOP_NOT_READY` | The seller hasn't finished setting up (details, returns policy, Stripe) |
75
+ | `NOT_FOR_SALE` | No active price for an item |
76
+ | `OUT_OF_STOCK` | Not enough available; `details` says which |
77
+ | `SHIPPING_REQUIRED` | Choose a shipping rate |
78
+ | `SIGN_IN_REQUIRED` | A proof or access item needs a signed-in buyer |
79
+ | `DIGITAL_CONSENT_REQUIRED` | Digital items need `acceptances.digitalImmediate` |
80
+ | `INVALID_INPUT` | For example, a return page on another site |
81
+ | `PAYMENT_UNAVAILABLE` | Stripe couldn't be reached; try again |
82
+
83
+ ## The seller's side (`shop.admin`)
84
+
85
+ For the Shop app and Studio (collection admins).
86
+
87
+ ```typescript
88
+ // Set up
89
+ const s = await shop.admin.settings(collectionId) // s.readiness.missing: what's still needed; s.fee: SmartLinks' rate
90
+ await shop.admin.updateSettings(collectionId, {
91
+ seller: { name: 'Hill Farm Soaps', address: '1 High St, Bath', email: 'hello@hillfarm.test' },
92
+ policies: { returns: 'Return unused items within 30 days.' },
93
+ shipping: { rates: [{ id: 'std', label: 'Standard', amount: 395, days: '2-3 working days' }], freeOver: 5000 },
94
+ checkout: { successUrl: 'https://hillfarm.smartlinks.host/thanks', cancelUrl: 'https://hillfarm.smartlinks.host/basket' },
95
+ })
96
+ const { url } = await shop.admin.connect(collectionId, { returnUrl, refreshUrl }) // the seller finishes on Stripe
97
+ await shop.admin.connectStatus(collectionId) // { connected, chargesEnabled, payoutsEnabled }
98
+
99
+ // Put things on sale
100
+ await shop.admin.setPrice(collectionId, { productId: 'soap', amount: 1200, trackStock: true })
101
+ await shop.admin.setPrice(collectionId, {
102
+ productId: 'soap-book', amount: 800, delivery: 'digital',
103
+ deliveryConfig: { kind: 'download', assetIds: ['asset_1'], maxDownloads: 5 },
104
+ })
105
+ await shop.admin.setPrice(collectionId, { productId: 'founder-cert', amount: 2500, delivery: 'digital', deliveryConfig: { kind: 'proof' } })
106
+
107
+ // Orders
108
+ const { items } = await shop.admin.listOrders(collectionId, { status: 'paid' })
109
+ await shop.admin.ship(collectionId, orderId, { carrier: 'Royal Mail', trackingNumber: 'AB123456789GB' })
110
+ await shop.admin.refund(collectionId, orderId, { lines: [{ itemId, quantity: 1 }], reason: 'Arrived damaged' })
111
+ ```
112
+
113
+ **An order's life:**
114
+
115
+ | Status | Meaning |
116
+ |---|---|
117
+ | `pending-payment` | At Stripe's checkout. Stock is held. |
118
+ | `expired` | Not paid in time, or cancelled. Stock released. |
119
+ | `paid` | Paid. Digital lines are delivered at once; physical ones wait to be shipped. |
120
+ | `fulfilled` | Everything sent or delivered. |
121
+ | `partly-refunded` / `refunded` | Money given back through Stripe. |
122
+
123
+ Anything that needs a look is in `order.attention`, for example a payment that didn't match (`AMOUNT_MISMATCH`)
124
+ or stock that sold out while the buyer was paying late (`STOCK_SHORT`).
125
+
126
+ **Delivery kinds** (`deliveryConfig.kind` on a digital price):
127
+
128
+ | Kind | What the buyer gets |
129
+ |---|---|
130
+ | `download` | Links to the files, by email and on the thank-you page (signed, 7 days; `maxDownloads` per file) |
131
+ | `proof` | A proof of ownership per unit, minted for their account |
132
+ | `access` | Membership of a user group (`userGroupId`), for members-only content |
133
+ | `app` | Your app delivers it on `order.paid`, then calls `shop.admin.markDelivered` |
134
+
135
+ ## Emails, flows and integrations
136
+
137
+ Nothing is emailed by default. The selling app (the shop's `appId`, `shop` unless set) decides what goes out, in
138
+ flows on order events, with templates the business can reword in Studio. They're set up the same way as booking
139
+ emails (`docs/bookings.md`, "Confirmations and messages"): a site's `cloud/flows.json`, or
140
+ `POST /admin/collection/:cId/blueprint/apply` with `{ appId, blueprint: { templates, flows } }`.
141
+
142
+ ```json
143
+ {
144
+ "templates": {
145
+ "order-confirmed": { "name": "Order confirmation", "email": { "subject": "Order {{ order.reference }}: thank you", "body": "<p>Thanks {{ contact.firstName }}. Total {{ order.total }}.</p>{% for f in order.downloads %}<p><a href=\"{{ f.url }}\">{{ f.name }}</a></p>{% endfor %}" } },
146
+ "order-shipped": { "name": "On its way", "email": { "subject": "Your order is on its way", "body": "<p>{{ order.tracking.carrier }} {{ order.tracking.number }}</p>" } },
147
+ "staff-new-order": { "name": "New order", "email": { "subject": "New order {{ order.reference }}: {{ order.total }}", "body": "<p>{{ order.shippingMethod }}</p>" } }
148
+ },
149
+ "flows": [
150
+ { "when": { "event": "order.paid" }, "do": [{ "send": "order-confirmed", "to": "contact" }, { "send": "staff-new-order", "to": "team" }] },
151
+ { "when": { "event": "order.shipped" }, "do": [{ "send": "order-shipped", "to": "contact" }] }
152
+ ]
153
+ }
154
+ ```
155
+
156
+ - **Events:** `order.paid`, `order.shipped`, `order.fulfilled`, `order.refunded`, `order.expired`,
157
+ `order.needs_attention`. `when.status` narrows to the order's status; `when.delivery` (`physical` /
158
+ `digital`) to orders with a line delivered that way.
159
+ - **`{{ order }}`:** `reference`, `status`, `placedAt`, `subtotal`, `shipping`, `shippingMethod`, `vat`, `total`,
160
+ `refunded` (all formatted, such as "£12.99"), `lines[]` (`name`, `quantity`, `unitPrice`, `total`, `delivery`),
161
+ `shippingAddress`, `tracking` (`carrier`, `number`, `url`), `seller`, `hasDigital`, `hasPhysical`, and
162
+ `downloads[]` (`name`, `url`). Download links go only to the buyer.
163
+ - **Integrations:** `order` is a flow source, so an order can go to a courier, a finance system or a fulfilment
164
+ partner. The delivery address leaves only under the `raw` identity policy; otherwise just the country.
165
+ - **Interactions:** set `interaction.interactionId` in the settings to record a purchase interaction when an order
166
+ is paid.
167
+
168
+ ## SmartLinks' fee
169
+
170
+ SmartLinks may take a small fee per sale, as Stripe's application fee. SmartLinks sets it per app, and sometimes per
171
+ business; sellers can't change it. Each price records the app that put it on sale (`appId`, `shop` by default),
172
+ and the fee follows that app. It's never charged on shipping. The seller sees their rate in
173
+ `shop.admin.settings().fee`.
174
+
175
+ Another app can sell through the same calls by passing its own `appId` on `setPrice`. It must be installed in the
176
+ collection. Its order emails come from its own flows when it's the shop's `appId`.
@@ -56,6 +56,8 @@ await ctx.sl.wallet.update(passId, { points: 145 })
56
56
 
57
57
  await ctx.sl.wallet.void(passId, 'refunded')
58
58
  await ctx.sl.wallet.message(passId, { header: 'Doors open', body: 'Doors open at 7pm. See you soon!' })
59
+ // Everyone holding a ticket for this event
60
+ await ctx.sl.wallet.messageAll({ type: 'event', id: eventId }, { header: 'Running late', body: 'We start at 7:30 tonight.' })
59
61
  ```
60
62
 
61
63
  - **Contact:** `contactId` defaults to the caller's contact.
@@ -64,6 +66,24 @@ await ctx.sl.wallet.message(passId, { header: 'Doors open', body: 'Doors open at
64
66
  person's passes about the message's `about`, otherwise to your app's own passes for them. It's worded from the
65
67
  template's push version.
66
68
 
69
+ ## Knowing who added it
70
+
71
+ Every pass belongs to a known person, so you can tell who has actually put it in their wallet:
72
+
73
+ - **Apple:** the phone registers the pass with SmartLinks when it's added, and unregisters it when it's removed.
74
+ - **Google:** Google sends a signed "saved" or "deleted" notice.
75
+
76
+ The pass's `inWallet` shows `{ apple: { saved, savedAt }, google: { saved, savedAt } }`. Each change raises an
77
+ event for the pass holder, so a flow in your app's blueprint can react, for example by sending a "see you there"
78
+ email when someone adds their ticket:
79
+
80
+ | Event | When |
81
+ |---|---|
82
+ | `wallet.pass_added` | the person added the pass to Apple or Google Wallet |
83
+ | `wallet.pass_removed` | they removed it |
84
+
85
+ Google's notices are best-effort, so treat them as a good signal rather than a guarantee.
86
+
67
87
  ## Checking a pass at the door
68
88
 
69
89
  The QR holds the pass's own code, never an id. Scan it, then: