@yaks/host 0.0.0-stage → 0.2.36

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.
Files changed (69) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +306 -2
  3. package/_dist/anatomy.d.ts +35 -0
  4. package/_dist/anatomy.d.ts.map +1 -0
  5. package/_dist/assembly.d.ts +94 -0
  6. package/_dist/assembly.d.ts.map +1 -0
  7. package/_dist/config.d.ts +150 -0
  8. package/_dist/config.d.ts.map +1 -0
  9. package/_dist/documents.d.ts +9 -0
  10. package/_dist/documents.d.ts.map +1 -0
  11. package/_dist/facets.d.ts +164 -0
  12. package/_dist/facets.d.ts.map +1 -0
  13. package/_dist/keepers.d.ts +15 -0
  14. package/_dist/keepers.d.ts.map +1 -0
  15. package/_dist/keywords.d.ts +3 -0
  16. package/_dist/keywords.d.ts.map +1 -0
  17. package/_dist/lending.d.ts +34 -0
  18. package/_dist/lending.d.ts.map +1 -0
  19. package/_dist/mod.d.ts +260 -0
  20. package/_dist/mod.d.ts.map +1 -0
  21. package/_dist/payments.d.ts +501 -0
  22. package/_dist/payments.d.ts.map +1 -0
  23. package/_dist/roles.d.ts +10 -0
  24. package/_dist/roles.d.ts.map +1 -0
  25. package/_dist/senders.d.ts +180 -0
  26. package/_dist/senders.d.ts.map +1 -0
  27. package/_dist/words.d.ts +27 -0
  28. package/_dist/words.d.ts.map +1 -0
  29. package/anatomy.js +432 -0
  30. package/anatomy.js.map +1 -0
  31. package/anatomy.ts +439 -0
  32. package/assembly.js +737 -0
  33. package/assembly.js.map +1 -0
  34. package/assembly.ts +930 -0
  35. package/config.js +149 -0
  36. package/config.js.map +1 -0
  37. package/config.ts +287 -0
  38. package/deno.json +1 -0
  39. package/documents.js +80 -0
  40. package/documents.js.map +1 -0
  41. package/documents.ts +105 -0
  42. package/facets.js +2 -0
  43. package/facets.js.map +1 -0
  44. package/facets.ts +236 -0
  45. package/keepers.js +7 -0
  46. package/keepers.js.map +1 -0
  47. package/keepers.ts +17 -0
  48. package/keywords.js +24 -0
  49. package/keywords.js.map +1 -0
  50. package/keywords.ts +25 -0
  51. package/lending.js +46 -0
  52. package/lending.js.map +1 -0
  53. package/lending.ts +77 -0
  54. package/mod.js +5 -0
  55. package/mod.js.map +1 -0
  56. package/mod.ts +356 -0
  57. package/package.json +79 -4
  58. package/payments.js +66 -0
  59. package/payments.js.map +1 -0
  60. package/payments.ts +665 -0
  61. package/roles.js +22 -0
  62. package/roles.js.map +1 -0
  63. package/roles.ts +18 -0
  64. package/senders.js +58 -0
  65. package/senders.js.map +1 -0
  66. package/senders.ts +272 -0
  67. package/words.js +134 -0
  68. package/words.js.map +1 -0
  69. package/words.ts +210 -0
package/payments.ts ADDED
@@ -0,0 +1,665 @@
1
+ /** What a payment provider lends a host that takes money through it (the
2
+ * lending kind `payments`, ./lending.ts): its name, for the sentences people
3
+ * read, the plans it sells a party, the shops a party sells through on an
4
+ * account of its own, and the webhook doors it hears both at. Each is
5
+ * functions over the provider's own wire, answering in words that name no
6
+ * provider, so a host's billing keeps its rules and refusals and a second
7
+ * provider has one contract to meet. */
8
+
9
+ /** One line of a cart: which product, how many, and its variant. */
10
+ export type Item = { product: string; qty: number; options?: string }
11
+
12
+ /** The items as they ride the checkout and come back on the event: the product,
13
+ * how many, and the variant, in the shortest form that survives a round
14
+ * trip. They ride one tag, so its length is what decides how many lines fit
15
+ * in an order. Dashes come off the uuid to buy back four
16
+ * characters a line: a uuid without them is 32 characters and a line about
17
+ * 45. */
18
+ export let packed = (items: Item[]): string =>
19
+ JSON.stringify(
20
+ items.map((i) => ({
21
+ p: i.product.replace(/-/g, ''),
22
+ q: i.qty,
23
+ ...(i.options ? { o: i.options } : {}),
24
+ })),
25
+ )
26
+
27
+ /** The cart read back out of a checkout's tags — {@link packed}, undone.
28
+ * Junk answers an empty list rather than throwing: an order whose items cannot
29
+ * be read is still an order somebody paid for, and losing the money over the
30
+ * label would be the worse mistake. */
31
+ export let unpacked = (items: unknown): Item[] => {
32
+ try {
33
+ let out = JSON.parse(String(items ?? '[]')) as {
34
+ p?: string
35
+ q?: number
36
+ o?: string
37
+ }[]
38
+ return (Array.isArray(out) ? out : []).map((i) => ({
39
+ product: String(i.p ?? '').replace(
40
+ /^(.{8})(.{4})(.{4})(.{4})(.{12})$/,
41
+ '$1-$2-$3-$4-$5',
42
+ ),
43
+ qty: Number(i.q ?? 1),
44
+ ...(i.o ? { options: String(i.o) } : {}),
45
+ }))
46
+ } catch {
47
+ return []
48
+ }
49
+ }
50
+
51
+ /** A party's details and tags as a payer is minted with them, and what every
52
+ * object the provider makes for that party carries back: the host's own
53
+ * words, such as whose it is and where it was bought. */
54
+ export type Tags = Record<string, string>
55
+
56
+ /** How often a plan is paid for. */
57
+ export type Per = 'month' | 'year'
58
+
59
+ /** Where a subscription stands, as its provider reads its status: `paying`
60
+ * and billed as it should be, `owing` still held while a failed charge is
61
+ * retried, `awaiting` made but not yet paid for the first time, `lapsed` no
62
+ * longer paid for though it may be again, `ended` over for good, which no
63
+ * later word of the provider undoes. Only `paying` and `owing` hold a plan. */
64
+ export type Standing = 'paying' | 'owing' | 'awaiting' | 'lapsed' | 'ended'
65
+
66
+ /** A subscription as its provider reads it. */
67
+ export type Subscription = {
68
+ id: string
69
+ /** the payer it bills */
70
+ payer: string
71
+ /** the tier its price buys, one of those the provider was configured to
72
+ * sell, whatever it stands at */
73
+ tier: string
74
+ /** How often the subscription bills, where the provider names it. */
75
+ per?: Per
76
+ /** the provider's own word for where it stands ({@link Plans.standing}) */
77
+ status: string
78
+ /** when the period paid for runs out */
79
+ until: string | null
80
+ /** set only when it will not renew: when it stops, or stopped */
81
+ ending: string | null
82
+ /** what it was bought with ({@link Checkout.tags}) */
83
+ tags: Tags
84
+ }
85
+
86
+ /** A hosted checkout for a plan: where to send the person, and what it
87
+ * subscribes them to. */
88
+ export type Checkout = {
89
+ payer: string
90
+ tier: string
91
+ per: Per
92
+ /** where the provider hands them back when they paid, and when they did
93
+ * not */
94
+ done: string
95
+ cancel: string
96
+ /** the host's own name for what is bought, carried back on the checkout */
97
+ reference: string
98
+ /** carried on the checkout and the subscription it makes */
99
+ tags: Tags
100
+ /** the day time already given free runs through, which the subscription
101
+ * starts with as a trial so its first charge falls after it */
102
+ trial?: string | null
103
+ }
104
+
105
+ /** A subscription to a plan paid by invoice rather than at a checkout: the
106
+ * provider sends `to` an invoice for each period, payable `due` days after
107
+ * it is sent, by bank transfer or by a check the seller records as paid. */
108
+ export type Invoice = {
109
+ payer: string
110
+ tier: string
111
+ per: Per
112
+ /** where the invoices go, and the name they are made out to */
113
+ to: { email: string; name: string }
114
+ /** days from an invoice being sent to its falling due */
115
+ due: number
116
+ /** A durable request identity: the same key asked twice subscribes
117
+ * once. */
118
+ key: string
119
+ /** carried on the subscription, as {@link Checkout.tags} */
120
+ tags: Tags
121
+ /** as {@link Checkout.trial}: the first invoice is sent when it ends */
122
+ trial?: string | null
123
+ addOn?: RecurringAddOn
124
+ }
125
+
126
+ export type RecurringAddOn = {
127
+ key: string
128
+ name: string
129
+ cents: number
130
+ start: string
131
+ renewal: string
132
+ }
133
+
134
+ export type OneTimeInvoice = {
135
+ payer: string
136
+ key: string
137
+ name: string
138
+ cents: number
139
+ due: number
140
+ tags: Tags
141
+ }
142
+
143
+ /** A subscription as one call made it, and when the provider made it, which
144
+ * orders it among the events about it that follow. */
145
+ export type Subscribed = { subscription: Subscription; at: string }
146
+
147
+ /** A hosted checkout for one thing paid for once, such as a block of
148
+ * credit: its price whole, in the currency's smallest unit, tax included,
149
+ * and what the buyer reads it as. Paid before anything is given for it. */
150
+ export type Once = {
151
+ payer: string
152
+ name: string
153
+ cents: number
154
+ currency: string
155
+ done: string
156
+ cancel: string
157
+ /** the host's own name for what is bought, carried back on the checkout */
158
+ reference: string
159
+ /** carried on the checkout and every event about it */
160
+ tags: Tags
161
+ }
162
+
163
+ /** A one-off checkout paid, as one verified event the provider posted says:
164
+ * the checkout, which names the purchase however often it is told, whether
165
+ * the money is there (a checkout can complete with its payment still on its
166
+ * way, and a later event says it arrived), and what it carried. */
167
+ export type Bought = {
168
+ about: 'purchase'
169
+ at: string
170
+ live: boolean
171
+ checkout: string
172
+ paid: boolean
173
+ tags: Tags
174
+ reference?: string
175
+ payerName?: PayerName
176
+ }
177
+
178
+ /** The name and address a completed checkout reported for its payer. */
179
+ export type PayerName = { name: string; email?: string }
180
+
181
+ /** A subscription changed, as one verified event the provider posted says:
182
+ * when and how it happened, what the object it is about carries, and the
183
+ * subscription it is about. */
184
+ export type Changed = {
185
+ about: 'subscription'
186
+ /** when it happened */
187
+ at: string
188
+ /** whether it moved money, rather than happening in a sandbox */
189
+ live: boolean
190
+ /** what the object it is about was tagged with, its own or its
191
+ * subscription's */
192
+ tags: Tags
193
+ /** the reference a checkout it is about carried */
194
+ reference?: string
195
+ /** where the payer said they are billed, from a checkout it is about that
196
+ * asked: never the street or postal code, only the town, its state or
197
+ * province, and the two-letter country */
198
+ billed?: { city?: string; state?: string; country?: string }
199
+ payerName?: PayerName
200
+ /** the subscription it is about, read off it or asked for: undefined
201
+ * where it is about none, null where the provider holds none by the name
202
+ * it gives */
203
+ subscription: () => Promise<Subscription | null | undefined>
204
+ }
205
+
206
+ /** What a provider sells a party: a payer minted once, a subscription to a
207
+ * tier bought at its hosted checkout and managed at its hosted portal, and
208
+ * the charges of one moved later. */
209
+ export type Plans = {
210
+ /** whether it can be asked anything here: its credentials are set */
211
+ on: () => boolean
212
+ /** whether a tier is on sale here, paid for so often */
213
+ offers: (tier: string, per: Per) => boolean
214
+ /** where a subscription with this status stands */
215
+ standing: (status: string) => Standing
216
+ /** a new payer, its id */
217
+ payer: (
218
+ who: { email: string; name: string; tags: Tags; key?: string },
219
+ ) => Promise<string>
220
+ /** whom a payer is billed to; null where the provider holds none by that
221
+ * id */
222
+ billed: (payer: string) => Promise<{ email: string; name: string } | null>
223
+ /** One payable checkout per payer and reference: reuse the same choice,
224
+ * expire an earlier choice before opening another. */
225
+ checkout: (asked: Checkout) => Promise<string>
226
+ /** a subscription billed by invoice, made now: its first invoice is sent
227
+ * within the hour, and it awaits that invoice's payment before it holds
228
+ * its plan */
229
+ invoice: (asked: Invoice) => Promise<Subscribed>
230
+ /** Whether the provider is configured to set up support billing. */
231
+ invoicingOn?: () => boolean
232
+ /** Attach or revise a yearly item on an existing invoiced yearly plan. */
233
+ addOn?: (subscription: string, item: RecurringAddOn) => Promise<string | null>
234
+ /** Send a stand-alone invoice, once per payer and key. */
235
+ invoiceOnce?: (asked: OneTimeInvoice) => Promise<string>
236
+ /** The smallest standalone payment accepted in this currency. */
237
+ minimum?: (currency: string) => number
238
+ /** the address of a hosted checkout for something paid for once */
239
+ once: (asked: Once) => Promise<string>
240
+ /** A paid {@link once} checkout's payment refunded whole, for what was
241
+ * bought and could not be given, or by the given number of cents. `key`
242
+ * names the request: asked again, it refunds nothing twice. */
243
+ refunded: (
244
+ asked: { key: string; checkout: string; tags: Tags; cents?: number },
245
+ ) => Promise<Refunded>
246
+ /** the address of the hosted portal where a payer manages what it pays,
247
+ * handing them back to `back` */
248
+ portal: (payer: string, back: string) => Promise<string>
249
+ /** a subscription, or null where the provider holds none by that id */
250
+ subscription: (id: string) => Promise<Subscription | null>
251
+ /** Every subscription this payer currently holds. */
252
+ subscriptions: (payer: string) => Promise<Subscription[]>
253
+ /** when a subscription next charges; null where nothing says */
254
+ charges: (id: string) => Promise<string | null>
255
+ /** a subscription's next charge moved to `until`, prorating nothing,
256
+ * unless it falls then or later: asked again, it moves nothing twice */
257
+ postpone: (id: string, until: string) => Promise<void>
258
+ }
259
+
260
+ /** A seller's account as its provider reads it: its id, whether it may take
261
+ * payments now, and whether its owner finished the provider's form, which
262
+ * alone does not make it ready.
263
+ *
264
+ * The house is a seller that sells on the host's own account rather than one
265
+ * of its own: the host is the merchant, the provider's fees are the host's,
266
+ * and no fee is taken, since the host would pay it to itself. Its account is
267
+ * '' wherever {@link Shops} names one, and every event about its sales names
268
+ * none ({@link Shopped}). */
269
+ export type Seller = {
270
+ account: string
271
+ ready: boolean
272
+ submitted: boolean
273
+ house?: boolean
274
+ }
275
+
276
+ /** One line of a sale: what the buyer reads it as, its price in the
277
+ * currency's smallest unit, and how many. */
278
+ export type Line = {
279
+ name: string
280
+ description?: string
281
+ cents: number
282
+ qty: number
283
+ }
284
+
285
+ /** The delivery address verified when a physical service was quoted. */
286
+ export type Shipping = {
287
+ name: string
288
+ phone?: string
289
+ address: {
290
+ line1: string
291
+ line2?: string
292
+ city: string
293
+ state?: string
294
+ postal_code: string
295
+ country: string
296
+ }
297
+ }
298
+
299
+ /** An immutable seller quote. All amounts are in the currency's smallest unit;
300
+ * subtotal covers the complete quantity, and total includes shipping and tax. */
301
+ export type Quote = {
302
+ id: string
303
+ buyer: string
304
+ source: string
305
+ seller: string
306
+ document: string
307
+ revision: string
308
+ specification: string
309
+ name: string
310
+ quantity: number
311
+ currency: string
312
+ destination: Shipping
313
+ shipping: { id: string; name: string; cents: number }
314
+ subtotal: number
315
+ tax: number
316
+ total: number
317
+ expires: string
318
+ }
319
+
320
+ /** A hosted checkout on a seller's account: paid once, or, where it says
321
+ * how often, a subscription that charges its lines again each period until
322
+ * it is cancelled. */
323
+ export type Sale = {
324
+ /** A durable request identity: retries address the same checkout. */
325
+ key?: string
326
+ /** A fixed physical quote; the provider does not offer address edits. */
327
+ destination?: Shipping
328
+ shipping?: { name: string; cents: number }
329
+ expires?: string
330
+ lines: Line[]
331
+ currency: string
332
+ /** how often every line is charged again; none for a sale paid once */
333
+ per?: Per
334
+ /** the host's share of each payment, in basis points (250 is 2.5%), which
335
+ * the provider takes for it, rounded down; none when 0 */
336
+ bps: number
337
+ /** the buyer's address to start the page with, if one is known */
338
+ email?: string
339
+ /** Offer cards and card wallets in the sale currency. */
340
+ cardOnly?: boolean
341
+ /** Offer instantly verified US bank debits alongside cards and wallets. */
342
+ bankDebit?: boolean
343
+ /** the seller's name, which the buyer's card statement reads after the
344
+ * host's on a sale on the host's own account; a seller's own account
345
+ * already names them there */
346
+ descriptor?: string
347
+ /** A card statement suffix requested for a one-time payment. */
348
+ statementSuffix?: string
349
+ /** The description in the provider's payment or subscription records. */
350
+ description?: string
351
+ /** where the provider hands the buyer back when they paid, and when they
352
+ * did not: addresses the host checked as `URL` writes them, so a
353
+ * placeholder the provider fills in on the way back arrives
354
+ * percent-encoded, and the provider writes it back as it reads it */
355
+ done: string
356
+ cancel: string
357
+ /** carried on the checkout and the payment it makes, so every event about
358
+ * either says whose sale it is, each no longer than {@link Shops.tag} */
359
+ tags: Tags
360
+ }
361
+
362
+ /** A checkout a buyer can complete with the provider's Elements on the app page. */
363
+ export type PaymentSession = {
364
+ id?: string
365
+ clientSecret: string
366
+ publishableKey: string
367
+ account?: string
368
+ }
369
+
370
+ /** A completed checkout, as its provider reads it. */
371
+ export type Paid = {
372
+ currency?: string
373
+ /** whether the money is there: a checkout can complete with its payment
374
+ * still processing */
375
+ paid: boolean
376
+ expired?: boolean
377
+ /** Delayed payment failed after checkout. */
378
+ failed?: boolean
379
+ /** The buyer submitted checkout and the payment is still settling. */
380
+ processing?: boolean
381
+ /** the provider's own word for where the payment stands, for a sentence */
382
+ status: string
383
+ /** the checkout's id, and the payment it made, which every later word
384
+ * about the sale names: '' for a subscription's, whose payments are its
385
+ * own */
386
+ checkout: string
387
+ payment: string
388
+ /** the subscription it started, where it sold one ({@link Term}) */
389
+ subscription?: string
390
+ /** customer on the seller’s account */
391
+ payer?: string
392
+ /** what was paid, in the currency's smallest unit */
393
+ total: number
394
+ /** the address the buyer gave, else the one the checkout started with */
395
+ email: string
396
+ /** the name the buyer gave, where the checkout asked for one */
397
+ name?: string
398
+ /** Time and mode of the provider event that confirmed a gift. */
399
+ gift?: { at: string; live: boolean }
400
+ /** what the checkout carried ({@link Sale.tags}) */
401
+ tags: Tags
402
+ }
403
+
404
+ /** A merchant's durable refund request. The key survives retries, including
405
+ * a response lost after the provider accepted the refund. */
406
+ export type Refund = {
407
+ key: string
408
+ payment: string
409
+ amount: number
410
+ fee?: boolean
411
+ tags: Tags
412
+ }
413
+
414
+ /** What the provider says about one refund, including asynchronous changes. */
415
+ export type Refunded = {
416
+ id: string
417
+ payment: string
418
+ amount: number
419
+ status: 'pending' | 'succeeded' | 'failed' | 'canceled'
420
+ tags: Tags
421
+ }
422
+
423
+ /** A payment read from a charge or dispute event. Refund amounts are cumulative
424
+ * provider facts; older events may not carry them. */
425
+ export type Payment = {
426
+ id: string
427
+ tags: Tags
428
+ total?: number
429
+ refunded?: number
430
+ fee?: string
431
+ }
432
+
433
+ /** Where an order's payment moved: `refunded` in whole or `partially_refunded`
434
+ * in part, `disputed` by the buyer, then `lost` to them or `restored` to the
435
+ * seller. */
436
+ export type Settled =
437
+ | 'refunded'
438
+ | 'partially_refunded'
439
+ | 'disputed'
440
+ | 'lost'
441
+ | 'restored'
442
+
443
+ /** A subscription a buyer took out at a seller's checkout, as the term it
444
+ * is paid for. Whether it is paid for now is `until` against the clock, so
445
+ * nothing about it is a status to keep. */
446
+ export type Term = {
447
+ id: string
448
+ /** when the term paid for ends; null where nothing was paid */
449
+ until: string | null
450
+ /** set only when it will not renew: when it stops, or stopped */
451
+ ending: string | null
452
+ /** what the checkout that started it carried ({@link Sale.tags}) */
453
+ tags: Tags
454
+ }
455
+
456
+ /** One period of a subscription on a seller's account, paid: the invoice,
457
+ * the subscription and the payer it billed, what was paid in the currency's
458
+ * smallest unit, when, the address and name it was billed to, and the tags
459
+ * the checkout that started the subscription carried ({@link Sale.tags}). */
460
+ export type Billed = {
461
+ id: string
462
+ subscription: string
463
+ payer: string
464
+ total: number
465
+ at: string
466
+ email: string
467
+ name?: string
468
+ live?: boolean
469
+ tags: Tags
470
+ }
471
+
472
+ /** One verified event a seller's account posted, read, with the account it
473
+ * happened on ('' where it names none, as an event about a house sale on the
474
+ * host's own account does, whose tags say whose sale it was): the seller's account changed, or it
475
+ * revoked the host's permission to act on it, or a checkout completed, or a
476
+ * payment moved, or a subscription's term moved (it renewed, was cancelled or
477
+ * lapsed), or something the host has no word for. */
478
+ export type Shopped =
479
+ & { account: string }
480
+ & (
481
+ | { about: 'seller'; seller: Seller }
482
+ | { about: 'revoked' }
483
+ | { about: 'sale'; sale: Paid }
484
+ | { about: 'refund'; refund: Refunded }
485
+ | {
486
+ about: 'payment'
487
+ to: Settled
488
+ /** the payment it moved and what it carried, read on asking; null
489
+ * where the event names none */
490
+ payment: () => Promise<Payment | null>
491
+ }
492
+ | {
493
+ about: 'term'
494
+ subscription: string
495
+ tags: Tags
496
+ /** the subscription as it stands now, read on asking, so events
497
+ * delivered out of order still leave the latest word; null where the
498
+ * event names none, or the provider holds none by that name */
499
+ term: () => Promise<Term | null>
500
+ /** the period's invoice, where the event is one paid */
501
+ invoice?: Billed
502
+ }
503
+ | { about: 'other' }
504
+ )
505
+
506
+ /** What a provider charges a seller, as its published pricing says, for
507
+ * the pages that tell a seller what selling costs before they start. */
508
+ export type Costs = {
509
+ /** a payment by a card from the seller's own country: a rate in basis
510
+ * points of the payment, plus a fixed amount in cents */
511
+ card: { bps: number; cents: number }
512
+ /** US bank debit rate in basis points and maximum fee in cents. */
513
+ bankDebit?: { bps: number; cap: number }
514
+ /** Additional estimated billing costs for recurring payments. */
515
+ recurring?: number
516
+ /** what a card from another country adds to the rate, in basis points */
517
+ abroad: number
518
+ /** the fee for a payment a buyer disputes with their bank, in cents,
519
+ * returned when the seller wins */
520
+ dispute: number
521
+ /** whether the processing fee comes back when a payment is refunded */
522
+ refunded: boolean
523
+ /** days from a sale to its payout, and from a new account's first sale
524
+ * to its first payout */
525
+ payout: { days: number; first: number }
526
+ /** the country those numbers are for, and where the provider publishes
527
+ * them for every other */
528
+ country: string
529
+ pricing: string
530
+ }
531
+
532
+ /** What a provider keeps of a card payment of `cents`: its rate, rounded to
533
+ * the nearest cent, plus its fixed amount, never more than the payment.
534
+ *
535
+ * ```ts
536
+ * import { charged } from '@yaks/host/payments'
537
+ * import { equal } from '@yaks/testing'
538
+ * let card = { bps: 290, cents: 30 }
539
+ * equal(charged(card, 2000), 88)
540
+ * equal(charged(card, 1999), 88)
541
+ * equal(charged(card, 10), 10)
542
+ * ```
543
+ */
544
+ export let charged = (
545
+ card: Costs['card'],
546
+ cents: number,
547
+ ): number => Math.min(cents, Math.round(cents * card.bps / 10_000) + card.cents)
548
+
549
+ /** What a payer adds to `cents` so that what the provider keeps of the whole
550
+ * ({@link charged}) leaves the seller `cents`: the least that does.
551
+ *
552
+ * ```ts
553
+ * import { charged, covering } from '@yaks/host/payments'
554
+ * import { equal } from '@yaks/testing'
555
+ * let card = { bps: 290, cents: 30 }
556
+ * equal(covering(card, 10000), 330)
557
+ * equal(10330 - charged(card, 10330) >= 10000, true)
558
+ * equal(10329 - charged(card, 10329) < 10000, true)
559
+ * equal(covering(card, 2500), 106)
560
+ * ```
561
+ */
562
+ export let covering = (card: Costs['card'], cents: number): number => {
563
+ let whole = Math.ceil((cents + card.cents) / (1 - card.bps / 10_000)) - 1
564
+ while (whole - charged(card, whole) < cents) whole++
565
+ while (whole > cents && whole - 1 - charged(card, whole - 1) >= cents) whole--
566
+ return whole - cents
567
+ }
568
+
569
+ /** What a provider lends a party selling on an account of its own: the
570
+ * account opened once and finished through the provider's onboarding, or one
571
+ * the seller already had and connected at the provider's own page, and
572
+ * hosted checkouts on it, paid once or by subscription, which the host takes
573
+ * a fee from. An account named '' is the host's own, the house's
574
+ * ({@link Seller}): its checkouts take no fee and are paid once. */
575
+ export type Shops = {
576
+ /** whether it can be asked anything here: its credentials are set */
577
+ on: () => boolean
578
+ /** what selling through it costs a seller */
579
+ costs: Costs
580
+ /** the least one payment may be in a currency, in its smallest unit */
581
+ minimum: (currency: string) => number
582
+ /** Which mode this connection can charge in. */
583
+ mode: () => 'test' | 'live' | undefined
584
+ /** the most characters one tag's value carries ({@link Sale.tags}): a
585
+ * longer one the provider refuses or cuts short */
586
+ tag: number
587
+ /** a new account for a seller, started with their address if one is known
588
+ * and carrying `tags` */
589
+ open: (who: { email?: string; tags: Tags }) => Promise<Seller>
590
+ /** the address of the provider's onboarding for an account, handing them
591
+ * back to `back` when they finish or the address goes stale: the provider's
592
+ * own page for an account the seller brought, which only they may change */
593
+ onboarding: (account: string, back: string) => Promise<string>
594
+ /** the address of the provider's page where a seller connects an account
595
+ * they already have, handing them back to `back` with `state` and a code
596
+ * {@link authorized} reads; null where the provider cannot connect one
597
+ * here */
598
+ authorize: (state: string, back: string) => string | null
599
+ /** the account a seller connected at {@link authorize}, from the code they
600
+ * were handed back with, which is good once */
601
+ authorized: (code: string) => Promise<Seller>
602
+ /** the address of a hosted checkout on a seller's account */
603
+ checkout: (account: string, sale: Sale) => Promise<string>
604
+ /** Checkout using provider-owned payment fields on the app's page. */
605
+ embedded?: {
606
+ on: () => boolean
607
+ checkout: (account: string, sale: Sale) => Promise<PaymentSession>
608
+ resume?: (
609
+ account: string,
610
+ checkout: string,
611
+ ) => Promise<PaymentSession | null>
612
+ walletDomain: (account: string, domain: string) => Promise<void>
613
+ }
614
+ /** a checkout on a seller's account as it stands, read back by its id,
615
+ * completed or not; null where the provider holds none by that id */
616
+ paid: (account: string, checkout: string) => Promise<Paid | null>
617
+ /** a subscription on a seller's account as it stands; null where the
618
+ * provider holds none by that id */
619
+ term: (account: string, subscription: string) => Promise<Term | null>
620
+ /** a subscription on a seller's account set to stop when the period paid
621
+ * for ends, and as it then stands: asked again, it changes nothing */
622
+ cancel: (account: string, subscription: string) => Promise<Term>
623
+ /** Refund on the merchant's account, returning the application fee in
624
+ * proportion. An identical key and request must never refund twice. */
625
+ refund: (account: string, request: Refund) => Promise<Refunded>
626
+ /** the address of the provider's hosted portal on a seller's account,
627
+ * where its payer manages and cancels what it pays, handing them back to
628
+ * `back` */
629
+ portal: (
630
+ account: string,
631
+ payer: string,
632
+ subscription: string,
633
+ back: string,
634
+ purpose?: 'subscription',
635
+ ) => Promise<string>
636
+ /** Reconcile a dashboard refund's cumulative application-fee reversal. */
637
+ refundFee: (fee: string, refunded: number, total: number) => Promise<void>
638
+ }
639
+
640
+ /** One verified event the provider posted, read into the host's words: a
641
+ * subscription changed, a one-off checkout was paid, or something happened
642
+ * on a seller's account. */
643
+ export type Heard = Changed | Bought | Shopped
644
+
645
+ /** What a door said of one request posted to it: the event it heard, or why
646
+ * it refused it, `ours` where the refusal can mean the host's own secret is
647
+ * wrong, which is worth reporting. */
648
+ export type Said = { heard: Heard } | { refused: string; ours: boolean }
649
+
650
+ /** A webhook door: one request the provider posted, verified and read.
651
+ * Undefined where it holds no secret to verify with, so it hears nothing. */
652
+ export type Door = (request: Request) => Promise<Said | undefined>
653
+
654
+ /** A payment provider as it is lent. */
655
+ export type Payments = {
656
+ /** its name, as people read it: the account they open, the page they pay
657
+ * on */
658
+ name: string
659
+ plans: Plans
660
+ shops: Shops
661
+ /** its webhook doors by name, which a host serves at `/<via>/<name>`, `via`
662
+ * the name it is lent under, so the provider is told one address per door
663
+ * and every door's events reach the host read */
664
+ doors: Record<string, Door>
665
+ }
package/roles.js ADDED
@@ -0,0 +1,22 @@
1
+ // The facets selected by each common role; a plugin's service is its own role.
2
+ import { facetsOf, used } from "./config.js";
3
+ export let ROLES = {
4
+ graph: [
5
+ 'vocab',
6
+ 'graph',
7
+ 'tools'
8
+ ],
9
+ web: [
10
+ 'routes',
11
+ 'ui'
12
+ ],
13
+ effects: [
14
+ 'effects'
15
+ ]
16
+ };
17
+ /** Common roles, followed by each configured plugin's service role. */ export let every = (config)=>[
18
+ ...Object.keys(ROLES),
19
+ ...(config.plugins ?? []).filter((p)=>facetsOf(p)?.includes('service') ?? true).map(used)
20
+ ];
21
+ export let common = (role)=>Object.hasOwn(ROLES, role);
22
+ //# sourceMappingURL=roles.js.map
package/roles.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"roles.js","sources":["./roles.ts"],"names":[],"mappings":"AAAA,+EAA+E;AAE/E,SAAS,QAAQ,EAAE,IAAI,sBAAqB;AAE5C,OAAO,IAAI,QAAQ;EACjB,OAAO;IAAC;IAAS;IAAS;GAAQ;EAClC,KAAK;IAAC;IAAU;GAAK;EACrB,SAAS;IAAC;GAAU;AACtB,EAAU;AACV,qEAAqE,GACrE,OAAO,IAAI,QAAQ,CAAC,SAA2B;OAC1C,OAAO,IAAI,CAAC;OACZ,CAAC,OAAO,OAAO,IAAI,EAAE,EAAE,MAAM,CAAC,CAAC,IAChC,SAAS,IAAI,SAAS,cAAc,MACpC,GAAG,CAAC;GACP,CAAA;AACD,OAAO,IAAI,SAAS,CAAC,OACnB,OAAO,MAAM,CAAC,OAAO,MAAK"}