@kerf-js/pay 0.0.0-stage → 0.1.0
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/LICENSE +16 -0
- package/README.md +756 -2
- package/docs/pay.md +171 -0
- package/package.json +58 -3
- package/plugin.d.mts +5 -0
- package/plugin.mjs +158 -0
- package/square-plugin.d.mts +9 -0
- package/square-plugin.mjs +69 -0
- package/src/checks/config.mjs +24 -0
- package/src/checks.mjs +379 -0
- package/src/drizzle.ts +57 -0
- package/src/index.ts +265 -0
- package/src/payer.ts +71 -0
- package/src/server/catalog.ts +80 -0
- package/src/server/ledger.ts +89 -0
- package/src/server/operations.ts +215 -0
- package/src/server/rails.ts +45 -0
- package/src/server.ts +283 -0
- package/src/setup.mjs +121 -0
- package/src/square.ts +309 -0
- package/src/stripe.ts +397 -0
- package/src/test.ts +323 -0
- package/stripe-plugin.d.mts +10 -0
- package/stripe-plugin.mjs +41 -0
package/docs/pay.md
ADDED
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
# Payments
|
|
2
|
+
|
|
3
|
+
This is a plugin, `@kerf-js/pay`, and nothing in kerf names it. `kerf docs plugins` walks through how it is built.
|
|
4
|
+
|
|
5
|
+
`kerf add pay` needs a database and auth. It adds the Stripe rail to `plugins` and writes `src/pay/catalog.ts`,
|
|
6
|
+
`src/pay.server.ts` and `src/server/pay.ts`, plus `src/db/pay.schema.ts` and placeholder keys. The keys go in `.env`
|
|
7
|
+
with real test values before you run it. Nothing is called until a user checks out.
|
|
8
|
+
|
|
9
|
+
App code names products, never a rail:
|
|
10
|
+
|
|
11
|
+
```ts
|
|
12
|
+
// src/pay/catalog.ts: reaches the client, so names and amounts only
|
|
13
|
+
import { product } from '@kerf-js/pay'
|
|
14
|
+
|
|
15
|
+
export const pro = product({
|
|
16
|
+
id: 'pro',
|
|
17
|
+
kind: 'subscription',
|
|
18
|
+
use: 'digital',
|
|
19
|
+
plans: { month: { usd: 999 }, year: { usd: 7999, trialDays: 7 } },
|
|
20
|
+
grants: ['pro'],
|
|
21
|
+
})
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
<!-- example: skip needs what `kerf add pay` writes -->
|
|
25
|
+
|
|
26
|
+
```tsx
|
|
27
|
+
// a page
|
|
28
|
+
import { price } from '@kerf-js/pay'
|
|
29
|
+
import { pro } from '#/pay/catalog'
|
|
30
|
+
import { checkout, entitlements, portal } from '#/server/pay'
|
|
31
|
+
|
|
32
|
+
export default function Pricing() {
|
|
33
|
+
const mine = entitlements.live()
|
|
34
|
+
return (
|
|
35
|
+
<section>
|
|
36
|
+
<button
|
|
37
|
+
type="button"
|
|
38
|
+
onClick={() => checkout({ product: 'pro', plan: 'year' })}
|
|
39
|
+
>
|
|
40
|
+
Go Pro · {price(pro, 'year').display}
|
|
41
|
+
</button>
|
|
42
|
+
<button type="button" onClick={() => portal()}>
|
|
43
|
+
Manage
|
|
44
|
+
</button>
|
|
45
|
+
{mine.value?.includes('pro') && <p>You have Pro.</p>}
|
|
46
|
+
</section>
|
|
47
|
+
)
|
|
48
|
+
}
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
`kind` is `subscription` (with `plans`), `one-time` or `consumable` (with `price`). Amounts are whole minor units with a
|
|
52
|
+
currency (PAY008). `price(p, plan)` formats by the currency's exponent, for the reader's locale.
|
|
53
|
+
|
|
54
|
+
**A grant comes only from a verified event.** `checkout` goes to the rail's hosted page and grants nothing (PAY004). The
|
|
55
|
+
rail then posts `/_kerf/x/pay/<rail>`, which is the URL to give it, and `/_kerf/x/pay/stripe` for Stripe. Each delivery is
|
|
56
|
+
verified on the exact bytes, applied to `purchases`, and pokes `entitlements:<user>`, so a `.live()` reader updates.
|
|
57
|
+
Delivery is at least once and in any order: a repeat is a no-op, and an old event changes nothing, by the rail's own clock.
|
|
58
|
+
A bad signature answers 400; a failure answers 500, so the rail delivers it again.
|
|
59
|
+
|
|
60
|
+
**A cart the server prices.** `pay.order(ctx, { ref, lines: [{ name, amount, quantity? }], currency?, email?, title? })`
|
|
61
|
+
opens one payment for lines your server priced from its own tables, for a guest or a signed-in buyer. It grants nothing.
|
|
62
|
+
`definePay({ onOrder({ ref, state, purchase }) })` hears each verified state, before the event is marked, so a throw
|
|
63
|
+
redelivers. Each order event pokes `orders` and `orderTag(ref)`. `urls.order` and `urls.orderCancel` may hold `{ref}`.
|
|
64
|
+
|
|
65
|
+
**Who paid.** A paid order's `onOrder` also gets `payer` when the rail says: `{ email?, name?, phone?, card?: { fingerprint,
|
|
66
|
+
brand, last4, expMonth, expYear } }`. The card's `fingerprint` is the rail's stable id for that card number, never the
|
|
67
|
+
number: the same card paying again gives the same one, so an app can know a returning customer (loyalty, a member's
|
|
68
|
+
card on file) without asking them for anything. Stripe reads it from the payment's charge (one API read per paid
|
|
69
|
+
checkout; if that read fails the order is still paid, without the card), Square from the payment. The ledger keeps none
|
|
70
|
+
of it. `fakePay(pay).complete(user, payer)` delivers one in tests.
|
|
71
|
+
|
|
72
|
+
**Enforce on the server.** What the client shows is decoration (PAY013):
|
|
73
|
+
|
|
74
|
+
<!-- example: skip fragments of a server module -->
|
|
75
|
+
|
|
76
|
+
```ts
|
|
77
|
+
export const report = query({
|
|
78
|
+
auth: 'user',
|
|
79
|
+
requires: ['pro'],
|
|
80
|
+
run: ({ ctx }) => buildReport(ctx),
|
|
81
|
+
}) // a typed 403: ENTITLEMENT_REQUIRED
|
|
82
|
+
// Await it (PAY015).
|
|
83
|
+
if (!(await ctx.entitlements.has('pro'))) redirect('/pricing')
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
`ctx.entitlements` has `has(id)` and `list()`, and works in any `query`, `mutation` or `load`. A query with `cache: { life }`
|
|
87
|
+
needs `scope: 'user'` to read it. A query's `tags` can take `ctx`, which is how one user's tag is written.
|
|
88
|
+
|
|
89
|
+
States are the same on every rail: `active`, `trial` and `grace` give access; `pending`, `billing-retry`, `paused`,
|
|
90
|
+
`expired`, `refunded` and `revoked` don't. Access also ends `leeway` (3 days) after a paid period, if no renewal came.
|
|
91
|
+
|
|
92
|
+
`definePay({ catalog, store, urls, user?, rail?, origin?, leeway? })`. `urls` are where a buyer returns, never from the
|
|
93
|
+
client. `pay.checkout(ctx, input)`, `pay.portal(ctx)`, `pay.entitlements(userId)`, `pay.purchases(userId)` and
|
|
94
|
+
`pay.refund(purchaseId)` are server calls. `store` is `drizzlePayStore(db, { purchases, payEvents })` from
|
|
95
|
+
`@kerf-js/pay/drizzle`, or five methods of your own (`PayStore`: `seen`, `mark`, `purchase`, `save`, `byUser`). `memoryPayStore()` is for tests: a production server
|
|
96
|
+
refuses it (PAY016).
|
|
97
|
+
|
|
98
|
+
**Another provider is one setting.** Replace `stripe()` in `plugins` with any plugin for the `pay.rail` slot; nothing else
|
|
99
|
+
changes. A rail implements `PayRail` from `@kerf-js/pay`: `checkout`, `portal`, `verify`, optionally `refund`, declared for the
|
|
100
|
+
`server` side, and passes `payRailSuite` from `@kerf-js/pay/test`. `verify` gets the exact body and throws
|
|
101
|
+
`PayError('PAY011', …)` for anything it can't prove. An app with no `src/pay.server.ts` has no route and no code from any
|
|
102
|
+
of this.
|
|
103
|
+
|
|
104
|
+
`square({ environment?, api?, apiVersion? })` from `@kerf-js/pay/square/plugin` is the Square rail: one-time payments
|
|
105
|
+
through payment links, so web orders land beside the shop's card readers. Env: `SQUARE_ACCESS_TOKEN`, `SQUARE_LOCATION_ID`,
|
|
106
|
+
`SQUARE_WEBHOOK_SIGNATURE_KEY`, `SQUARE_WEBHOOK_URL` (the exact URL given to Square, which it signs),
|
|
107
|
+
`SQUARE_ENVIRONMENT=sandbox` (or `SQUARE_ENV`) for the sandbox host, `SQUARE_APPLICATION_ID` (optional, checked).
|
|
108
|
+
Subscribe the webhook to `payment.created` and `payment.updated`. Subscriptions and `portal` are PAY022 on it.
|
|
109
|
+
|
|
110
|
+
`stripe({ prices?, api?, apiVersion?, tolerance?, shippingCountries? })` from `@kerf-js/pay/stripe/plugin`. It reads
|
|
111
|
+
`STRIPE_SECRET_KEY` and `STRIPE_WEBHOOK_SECRET` from the server env. Don't put a key in its options: they land in a
|
|
112
|
+
generated file (PAY003).
|
|
113
|
+
`prices` maps `'pro.month'` to a Stripe Price id; without it, amounts come from the catalog. Subscribe the webhook to
|
|
114
|
+
`customer.subscription.*`, `checkout.session.*`, `charge.refunded` and `charge.dispute.*`. No page loads Stripe.js.
|
|
115
|
+
|
|
116
|
+
## Sandbox and test mode
|
|
117
|
+
|
|
118
|
+
Build against each provider's sandbox; go live by changing env vars, not code. A key from the other mode is refused
|
|
119
|
+
before anything is sent (PAY032).
|
|
120
|
+
|
|
121
|
+
- Stripe: `STRIPE_MODE=test` with `sk_test_…` keys (`live` with `sk_live_…`). Locally, `stripe listen --forward-to
|
|
122
|
+
localhost:5173/_kerf/x/pay/stripe` and use the `whsec_` it prints. Test card `4242 4242 4242 4242`.
|
|
123
|
+
- Square: `SQUARE_ENVIRONMENT=sandbox` with the Developer Console's *Sandbox* tab token, a sandbox location, and
|
|
124
|
+
optionally `SQUARE_APPLICATION_ID=sandbox-sq0idb-…`. Webhooks need a public tunnel URL in `SQUARE_WEBHOOK_URL`. Test
|
|
125
|
+
card `4111 1111 1111 1111`, CVV `111`.
|
|
126
|
+
- `await pay.sandbox()` says whether the bound rail is in test mode, for a banner. `fakePay` reports `true`.
|
|
127
|
+
- `PAY_SANDBOX=1 pnpm --filter @kerf-js/pay test sandbox.live` runs checkout and refund against the real sandboxes when
|
|
128
|
+
their credentials are set, with `pm_card_visa` and `cnon:card-nonce-ok`.
|
|
129
|
+
|
|
130
|
+
## Testing
|
|
131
|
+
|
|
132
|
+
`fakePay` from `@kerf-js/pay/test` is a rail with no key and no network. Its events are signed and go through your own webhook and
|
|
133
|
+
ledger, so a test sees what a user would:
|
|
134
|
+
|
|
135
|
+
```tsx
|
|
136
|
+
const fake = fakePay(pay) // your `definePay`, or fakePay({ catalog })
|
|
137
|
+
net.context({ user: { id: 'u1' } })
|
|
138
|
+
render(Paywall)
|
|
139
|
+
await user.click(screen.getByRole('button', { name: /Go Pro/ }))
|
|
140
|
+
await fake.complete() // the buyer finishes the hosted page
|
|
141
|
+
expect(screen.getByLabelText('plan').textContent).toBe('Pro')
|
|
142
|
+
// renewal, recovered, grace-expired, cancel, pause, resume,
|
|
143
|
+
// refund, revoke
|
|
144
|
+
await fake.simulate('renewal-failed')
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
`fake.buy(user, product, plan)` skips the page. `renewal` and `renewal-failed` move `clock` to the period's end when it's
|
|
148
|
+
installed. `fake.redeliver()`, `fake.deliver(event, { forged: true })`, `fake.deliveries`, `fake.sessions`,
|
|
149
|
+
`fake.entitlements(user)`.
|
|
150
|
+
|
|
151
|
+
## Diagnostics
|
|
152
|
+
|
|
153
|
+
| Code | Catches |
|
|
154
|
+
| ------ | ----------------------------------------------------------- |
|
|
155
|
+
| PAY003 | A key in the source, or a server secret in client code |
|
|
156
|
+
| PAY004 | Paid state written on the client after `checkout()` |
|
|
157
|
+
| PAY005 | A price typed into a page that sells something (warning) |
|
|
158
|
+
| PAY008 | A float or a currency-less amount in `product()` |
|
|
159
|
+
| PAY010 | A checkout mutation without `auth: 'user'` |
|
|
160
|
+
| PAY011 | A webhook left unverified, or no `STRIPE_WEBHOOK_SECRET` |
|
|
161
|
+
| PAY012 | A rail's price or product id in client code |
|
|
162
|
+
| PAY013 | An entitlement checked only on the client |
|
|
163
|
+
| PAY014 | A rail bound with no `src/pay.server.ts` |
|
|
164
|
+
| PAY015 | `ctx.entitlements.has()` tested without `await` |
|
|
165
|
+
| PAY016 | `memoryPayStore()` in `src/pay.server.ts` (warning) |
|
|
166
|
+
| PAY017 | `@kerf-js/pay/server` or `/drizzle` imported in client code |
|
|
167
|
+
| PAY018 | `src/pay.server.ts` with no `pay.rail` provider |
|
|
168
|
+
| PAY031 | A live key in a test |
|
|
169
|
+
|
|
170
|
+
At run time: PAY021 (not in the catalog), PAY022 (no rail chosen), PAY023 (already subscribed, 409), PAY024 (nothing to
|
|
171
|
+
manage or refund), PAY030 (the rail refused), PAY032 (keys from the other mode: test vs live, sandbox vs production). Native rails, store policy and `kerf pay sync` aren't built.
|
package/package.json
CHANGED
|
@@ -1,6 +1,61 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kerf-js/pay",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "@kerf-js/pay: products, one ledger for every rail, and entitlements. kerf's reference plugin: read it to build your own",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"kerf"
|
|
7
|
+
],
|
|
8
|
+
"homepage": "https://github.com/Ravonus/kerf/tree/main/packages/pay#readme",
|
|
9
|
+
"bugs": {
|
|
10
|
+
"url": "https://github.com/Ravonus/kerf/issues"
|
|
11
|
+
},
|
|
12
|
+
"license": "MIT",
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "git+https://github.com/Ravonus/kerf.git",
|
|
16
|
+
"directory": "packages/pay"
|
|
17
|
+
},
|
|
18
|
+
"files": [
|
|
19
|
+
"src",
|
|
20
|
+
"docs",
|
|
21
|
+
"plugin.mjs",
|
|
22
|
+
"plugin.d.mts",
|
|
23
|
+
"stripe-plugin.mjs",
|
|
24
|
+
"stripe-plugin.d.mts",
|
|
25
|
+
"README.md",
|
|
26
|
+
"LICENSE",
|
|
27
|
+
"square-plugin.mjs",
|
|
28
|
+
"square-plugin.d.mts"
|
|
29
|
+
],
|
|
30
|
+
"type": "module",
|
|
31
|
+
"exports": {
|
|
32
|
+
".": "./src/index.ts",
|
|
33
|
+
"./server": "./src/server.ts",
|
|
34
|
+
"./drizzle": "./src/drizzle.ts",
|
|
35
|
+
"./test": "./src/test.ts",
|
|
36
|
+
"./plugin": "./plugin.mjs",
|
|
37
|
+
"./stripe": "./src/stripe.ts",
|
|
38
|
+
"./stripe/plugin": "./stripe-plugin.mjs",
|
|
39
|
+
"./square": "./src/square.ts",
|
|
40
|
+
"./square/plugin": "./square-plugin.mjs"
|
|
41
|
+
},
|
|
42
|
+
"publishConfig": {
|
|
43
|
+
"access": "public"
|
|
44
|
+
},
|
|
45
|
+
"dependencies": {
|
|
46
|
+
"typescript": "6.0.3"
|
|
47
|
+
},
|
|
48
|
+
"peerDependencies": {
|
|
49
|
+
"@kerf-js/core": "^0.1.0",
|
|
50
|
+
"@kerf-js/start": "^0.1.0",
|
|
51
|
+
"drizzle-orm": "^0.45.0"
|
|
52
|
+
},
|
|
53
|
+
"peerDependenciesMeta": {
|
|
54
|
+
"drizzle-orm": {
|
|
55
|
+
"optional": true
|
|
56
|
+
}
|
|
57
|
+
},
|
|
58
|
+
"engines": {
|
|
59
|
+
"node": ">=24"
|
|
60
|
+
}
|
|
6
61
|
}
|
package/plugin.d.mts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { PluginInstance } from '@kerf-js/core/plugin'
|
|
2
|
+
|
|
3
|
+
declare const plugin: (options?: Record<string, never>) => PluginInstance<unknown>
|
|
4
|
+
/** `pay()` for `kerf.config.ts`: the `pay.rail` slot, the PAY checks and the ledger's route. Rails are plugins of their own. */
|
|
5
|
+
export default plugin
|
package/plugin.mjs
ADDED
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
// pay() for kerf.config.ts: start here to see how a plugin is put together (`kerf docs plugins` walks through it). Everything
|
|
2
|
+
// a plugin gives kerf is in this one pure manifest: the server part as a slot provider, and its checks, docs, codes and
|
|
3
|
+
// `kerf add` steps as data. Heavy code stays behind `import()` and the slot's lazy `load()`, so a config load costs little
|
|
4
|
+
// and a page costs nothing. A payment provider is a separate plugin: this one defines the ledger, and `pay.rail` is the seam.
|
|
5
|
+
import { definePlugin, defineSlot, provide } from '@kerf-js/core/plugin'
|
|
6
|
+
import { setup } from './src/setup.mjs'
|
|
7
|
+
|
|
8
|
+
const extension = defineSlot({
|
|
9
|
+
id: 'server.extension',
|
|
10
|
+
version: 1,
|
|
11
|
+
cardinality: 'keyed',
|
|
12
|
+
sides: ['server'],
|
|
13
|
+
})
|
|
14
|
+
|
|
15
|
+
const diagnostics = [
|
|
16
|
+
[
|
|
17
|
+
'PAY003',
|
|
18
|
+
'error',
|
|
19
|
+
'A Stripe secret, restricted or webhook key in the source, a server secret read in client code, or a key in plugin options',
|
|
20
|
+
'Move it to the server env (`src/env.server.ts`), and roll any real key that was committed',
|
|
21
|
+
],
|
|
22
|
+
[
|
|
23
|
+
'PAY004',
|
|
24
|
+
'error',
|
|
25
|
+
"Paid state written on the client after `checkout()`, which only opens the rail's page",
|
|
26
|
+
'Read `entitlements` instead: a grant arrives from a verified event',
|
|
27
|
+
],
|
|
28
|
+
[
|
|
29
|
+
'PAY005',
|
|
30
|
+
'warning',
|
|
31
|
+
'A price typed into a page that sells something',
|
|
32
|
+
'`price(product, plan).display`',
|
|
33
|
+
],
|
|
34
|
+
[
|
|
35
|
+
'PAY008',
|
|
36
|
+
'error',
|
|
37
|
+
'Money as a float, or with no currency, in `product()`',
|
|
38
|
+
'Whole minor units with a currency: `{ usd: 999 }`',
|
|
39
|
+
],
|
|
40
|
+
[
|
|
41
|
+
'PAY010',
|
|
42
|
+
'error',
|
|
43
|
+
"A mutation that opens a checkout without `auth: 'user'`",
|
|
44
|
+
"Add `auth: 'user'`: a purchase has to belong to someone",
|
|
45
|
+
],
|
|
46
|
+
[
|
|
47
|
+
'PAY011',
|
|
48
|
+
'error',
|
|
49
|
+
'A webhook left unverified: a handler of payment events with no signature check, a `PayRail` whose `verify` ignores `headers`, or no `STRIPE_WEBHOOK_SECRET`',
|
|
50
|
+
'Delete the handler: `/_kerf/x/pay/<rail>` verifies. Give Stripe that URL, and set `STRIPE_WEBHOOK_SECRET`',
|
|
51
|
+
],
|
|
52
|
+
[
|
|
53
|
+
'PAY012',
|
|
54
|
+
'error',
|
|
55
|
+
"A rail's price or product id in client code",
|
|
56
|
+
"Name the catalog: `checkout({ product: 'pro', plan: 'month' })`. Map ids on the server: `stripe({ prices: { 'pro.month': … } })`",
|
|
57
|
+
],
|
|
58
|
+
[
|
|
59
|
+
'PAY013',
|
|
60
|
+
'error',
|
|
61
|
+
'An entitlement checked only on the client: nothing on the server enforces it',
|
|
62
|
+
"`requires: ['pro']` on the query or mutation, or `await ctx.entitlements.has('pro')` in a `load`",
|
|
63
|
+
],
|
|
64
|
+
[
|
|
65
|
+
'PAY014',
|
|
66
|
+
'error',
|
|
67
|
+
'The plugin with no `src/pay.server.ts`: no webhook is mounted, so payments grant nothing',
|
|
68
|
+
'Default-export `definePay({ … })` from `src/pay.server.ts`',
|
|
69
|
+
],
|
|
70
|
+
[
|
|
71
|
+
'PAY015',
|
|
72
|
+
'error',
|
|
73
|
+
'`ctx.entitlements.has()` tested without `await`: a promise is truthy, so everyone passes',
|
|
74
|
+
'Await it (`kerf fix` does, in an `async` function)',
|
|
75
|
+
],
|
|
76
|
+
[
|
|
77
|
+
'PAY016',
|
|
78
|
+
'warning',
|
|
79
|
+
'`memoryPayStore()` in `src/pay.server.ts`: a production server refuses to start with it',
|
|
80
|
+
'`drizzlePayStore(db, { purchases, payEvents })`, or your own `PayStore`',
|
|
81
|
+
],
|
|
82
|
+
[
|
|
83
|
+
'PAY017',
|
|
84
|
+
'error',
|
|
85
|
+
'`@kerf-js/pay/server` or `/drizzle` imported in client code',
|
|
86
|
+
'Import them in a server module; the client gets `@kerf-js/pay`',
|
|
87
|
+
],
|
|
88
|
+
[
|
|
89
|
+
'PAY018',
|
|
90
|
+
'error',
|
|
91
|
+
'`src/pay.server.ts` with no `pay.rail` provider',
|
|
92
|
+
'Add a rail to `plugins`: `kerf add pay-stripe`',
|
|
93
|
+
],
|
|
94
|
+
['PAY031', 'error', 'A live key in a test', '`fakePay` from `@kerf-js/pay/test`'],
|
|
95
|
+
].map(([code, severity, catches, fix]) => ({ code, severity, catches, fix }))
|
|
96
|
+
|
|
97
|
+
export const pay = definePlugin({
|
|
98
|
+
name: '@kerf-js/pay',
|
|
99
|
+
version: '0.1.0',
|
|
100
|
+
kerf: '*',
|
|
101
|
+
namespace: 'PAY',
|
|
102
|
+
permissions: {},
|
|
103
|
+
contribute: () => ({
|
|
104
|
+
provides: [
|
|
105
|
+
provide(extension, {
|
|
106
|
+
key: 'pay',
|
|
107
|
+
sides: { server: { module: '@kerf-js/pay/server', export: 'extension' } },
|
|
108
|
+
}),
|
|
109
|
+
],
|
|
110
|
+
diagnostics,
|
|
111
|
+
check: {
|
|
112
|
+
include: ['src/**/*.{ts,tsx,js,jsx,mts,mjs}', 'test/**/*.{ts,tsx,js,mjs}', 'kerf.config.*'],
|
|
113
|
+
run: (ctx) => import('./src/checks.mjs').then((c) => c.run(ctx)),
|
|
114
|
+
},
|
|
115
|
+
docs: {
|
|
116
|
+
topic: 'pay',
|
|
117
|
+
summary:
|
|
118
|
+
'Payments: products, checkout, entitlements, webhooks, fakePay, another provider; PAY-codes',
|
|
119
|
+
file: new URL('./docs/pay.md', import.meta.url).pathname,
|
|
120
|
+
},
|
|
121
|
+
setup,
|
|
122
|
+
agents: {
|
|
123
|
+
title: 'Payments',
|
|
124
|
+
text: 'Products are in `src/pay/catalog.ts`, in minor units. `checkout` and `entitlements` are in `src/server/pay.ts`. App code never names a rail or a price id, and `checkout` grants nothing: a verified webhook does. Enforce on the server, with `requires: [...]` or `await ctx.entitlements.has(...)`: a check only on the client protects nothing. Test with `fakePay` from `@kerf-js/pay/test`. `kerf docs pay`.',
|
|
125
|
+
},
|
|
126
|
+
catalog: [
|
|
127
|
+
{
|
|
128
|
+
name: 'product',
|
|
129
|
+
import: '@kerf-js/pay',
|
|
130
|
+
summary: 'One thing you sell, in integer minor units',
|
|
131
|
+
},
|
|
132
|
+
{
|
|
133
|
+
name: 'price',
|
|
134
|
+
import: '@kerf-js/pay',
|
|
135
|
+
summary: "A product's price, formatted for the reader",
|
|
136
|
+
},
|
|
137
|
+
{
|
|
138
|
+
name: 'definePay',
|
|
139
|
+
import: '@kerf-js/pay/server',
|
|
140
|
+
summary: 'The ledger: src/pay.server.ts default-exports it',
|
|
141
|
+
},
|
|
142
|
+
{
|
|
143
|
+
name: 'fakePay',
|
|
144
|
+
import: '@kerf-js/pay/test',
|
|
145
|
+
summary: 'A rail for tests: renewals, refunds and disputes on the clock',
|
|
146
|
+
},
|
|
147
|
+
],
|
|
148
|
+
map: [
|
|
149
|
+
{
|
|
150
|
+
want: 'Sell something',
|
|
151
|
+
call: 'add it to `src/pay/catalog.ts`, then `checkout({ product, plan })`',
|
|
152
|
+
have: '`kerf docs pay`',
|
|
153
|
+
},
|
|
154
|
+
],
|
|
155
|
+
}),
|
|
156
|
+
})
|
|
157
|
+
|
|
158
|
+
export default pay
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { PluginInstance } from '@kerf-js/core/plugin'
|
|
2
|
+
|
|
3
|
+
declare const plugin: (options?: {
|
|
4
|
+
environment?: 'sandbox' | 'production'
|
|
5
|
+
api?: string
|
|
6
|
+
apiVersion?: string
|
|
7
|
+
}) => PluginInstance<unknown>
|
|
8
|
+
/** `square()` for `kerf.config.ts`: Square as a `pay.rail` provider. Tokens stay in the environment. */
|
|
9
|
+
export default plugin
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
// square() for kerf.config.ts: Square as the `pay.rail` provider, so web orders land in the same account as the shop's
|
|
2
|
+
// card readers. Options are in the generated slot module, so they name env vars and never hold a token (PAY003).
|
|
3
|
+
import { definePlugin, defineSlot, provide } from '@kerf-js/core/plugin'
|
|
4
|
+
|
|
5
|
+
const slot = defineSlot({
|
|
6
|
+
id: 'pay.rail',
|
|
7
|
+
version: 1,
|
|
8
|
+
cardinality: 'keyed',
|
|
9
|
+
sides: ['server', 'client', 'native'],
|
|
10
|
+
})
|
|
11
|
+
|
|
12
|
+
/** What `kerf add pay-square` writes: the sandbox's variables, empty but for the switch, in .env and .env.example. */
|
|
13
|
+
const setup = {
|
|
14
|
+
run: () => ({
|
|
15
|
+
env: {
|
|
16
|
+
SQUARE_ENVIRONMENT: 'sandbox',
|
|
17
|
+
SQUARE_ACCESS_TOKEN: 'square_sandbox_placeholder',
|
|
18
|
+
SQUARE_LOCATION_ID: '',
|
|
19
|
+
SQUARE_APPLICATION_ID: '',
|
|
20
|
+
SQUARE_WEBHOOK_SIGNATURE_KEY: '',
|
|
21
|
+
SQUARE_WEBHOOK_URL: '',
|
|
22
|
+
},
|
|
23
|
+
next: [
|
|
24
|
+
"from the Developer Console's Sandbox tab, put the sandbox access token, a sandbox location and the `sandbox-sq0idb-…` app id in .env",
|
|
25
|
+
'set SQUARE_ENVIRONMENT=production with the production token and location to go live: a sandbox app id is then refused (PAY032)',
|
|
26
|
+
'give Square the webhook URL /_kerf/x/pay/square (a tunnel locally), the same string as SQUARE_WEBHOOK_URL',
|
|
27
|
+
],
|
|
28
|
+
}),
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export const square = definePlugin({
|
|
32
|
+
name: '@kerf-js/pay/square',
|
|
33
|
+
version: '0.0.0',
|
|
34
|
+
kerf: '*',
|
|
35
|
+
permissions: {
|
|
36
|
+
net: ['connect.squareup.com', 'connect.squareupsandbox.com'],
|
|
37
|
+
env: [
|
|
38
|
+
'SQUARE_ACCESS_TOKEN',
|
|
39
|
+
'SQUARE_LOCATION_ID',
|
|
40
|
+
'SQUARE_WEBHOOK_SIGNATURE_KEY',
|
|
41
|
+
'SQUARE_WEBHOOK_URL',
|
|
42
|
+
'SQUARE_ENVIRONMENT',
|
|
43
|
+
'SQUARE_ENV',
|
|
44
|
+
'SQUARE_APPLICATION_ID',
|
|
45
|
+
],
|
|
46
|
+
},
|
|
47
|
+
contribute: (options) => ({
|
|
48
|
+
setup,
|
|
49
|
+
provides: [
|
|
50
|
+
provide(slot, {
|
|
51
|
+
key: 'square',
|
|
52
|
+
sides: { server: { module: '@kerf-js/pay/square', export: 'squareRail', options } },
|
|
53
|
+
}),
|
|
54
|
+
],
|
|
55
|
+
agents: {
|
|
56
|
+
title: 'Pay rail: Square',
|
|
57
|
+
text: 'Square payment links, one-time payments only (`pay.order`, one-time products): `SQUARE_ACCESS_TOKEN`, `SQUARE_LOCATION_ID`, and for `/_kerf/x/pay/square` (subscribe it to `payment.created` and `payment.updated`) `SQUARE_WEBHOOK_SIGNATURE_KEY` plus `SQUARE_WEBHOOK_URL`, the exact URL given to Square. For the sandbox, `SQUARE_ENVIRONMENT=sandbox` with the token, location and app id (`sandbox-sq0idb-…`); a mismatch is PAY032. Pay on the hosted page with test card 4111 1111 1111 1111.',
|
|
58
|
+
},
|
|
59
|
+
map: [
|
|
60
|
+
{
|
|
61
|
+
want: 'Change the payment provider',
|
|
62
|
+
call: 'replace this plugin in `plugins`: app code stays',
|
|
63
|
+
have: 'Square payment links',
|
|
64
|
+
},
|
|
65
|
+
],
|
|
66
|
+
}),
|
|
67
|
+
})
|
|
68
|
+
|
|
69
|
+
export default square
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/** Check config and test files for exposed credentials. */
|
|
2
|
+
export function configChecks({ ctx, out, secrets, spanOf, isTest, SECRET_OPTION }) {
|
|
3
|
+
for (const rel of ctx.files.filter((r) => r.startsWith('kerf.config.'))) {
|
|
4
|
+
const text = ctx.read(rel)
|
|
5
|
+
secrets(rel, text, true, out)
|
|
6
|
+
const m = SECRET_OPTION.exec(text)
|
|
7
|
+
if (m)
|
|
8
|
+
out.push({
|
|
9
|
+
code: 'PAY003',
|
|
10
|
+
...spanOf(rel, m.index, m[0].length),
|
|
11
|
+
why: 'Plugin options are written into `.kerf/slots/`, so a key given here sits in a generated file.',
|
|
12
|
+
})
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
for (const rel of ctx.files.filter(isTest)) {
|
|
16
|
+
const text = ctx.read(rel)
|
|
17
|
+
for (const m of text.matchAll(/\b(?:sk|rk)_live_\w+/g))
|
|
18
|
+
out.push({
|
|
19
|
+
code: 'PAY031',
|
|
20
|
+
...spanOf(rel, m.index, m[0].length),
|
|
21
|
+
why: 'A live key in a test: it charges real cards, and it is now in git.',
|
|
22
|
+
})
|
|
23
|
+
}
|
|
24
|
+
}
|