@ultimabase/stripe 0.0.0-first.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.
Files changed (130) hide show
  1. package/README.md +7 -0
  2. package/dist/accounts.d.ts +9 -0
  3. package/dist/accounts.d.ts.map +1 -0
  4. package/dist/accounts.js +20 -0
  5. package/dist/apply.d.ts +26 -0
  6. package/dist/apply.d.ts.map +1 -0
  7. package/dist/apply.js +73 -0
  8. package/dist/audit.d.ts +3 -0
  9. package/dist/audit.d.ts.map +1 -0
  10. package/dist/audit.js +123 -0
  11. package/dist/billing-gateway.d.ts +158 -0
  12. package/dist/billing-gateway.d.ts.map +1 -0
  13. package/dist/billing-gateway.js +23 -0
  14. package/dist/billing.d.ts +130 -0
  15. package/dist/billing.d.ts.map +1 -0
  16. package/dist/billing.js +128 -0
  17. package/dist/calls.d.ts +61 -0
  18. package/dist/calls.d.ts.map +1 -0
  19. package/dist/calls.js +172 -0
  20. package/dist/catalog.d.ts +54 -0
  21. package/dist/catalog.d.ts.map +1 -0
  22. package/dist/catalog.js +182 -0
  23. package/dist/credit.d.ts +16 -0
  24. package/dist/credit.d.ts.map +1 -0
  25. package/dist/credit.js +18 -0
  26. package/dist/credits.d.ts +38 -0
  27. package/dist/credits.d.ts.map +1 -0
  28. package/dist/credits.js +138 -0
  29. package/dist/customer.d.ts +25 -0
  30. package/dist/customer.d.ts.map +1 -0
  31. package/dist/customer.js +88 -0
  32. package/dist/describe.d.ts +4 -0
  33. package/dist/describe.d.ts.map +1 -0
  34. package/dist/describe.js +56 -0
  35. package/dist/duplicates.d.ts +12 -0
  36. package/dist/duplicates.d.ts.map +1 -0
  37. package/dist/duplicates.js +24 -0
  38. package/dist/fake-billing.d.ts +72 -0
  39. package/dist/fake-billing.d.ts.map +1 -0
  40. package/dist/fake-billing.js +290 -0
  41. package/dist/gateway.d.ts +108 -0
  42. package/dist/gateway.d.ts.map +1 -0
  43. package/dist/gateway.js +196 -0
  44. package/dist/index.d.ts +15 -0
  45. package/dist/index.d.ts.map +1 -0
  46. package/dist/index.js +6 -0
  47. package/dist/job-input.d.ts +6 -0
  48. package/dist/job-input.d.ts.map +1 -0
  49. package/dist/job-input.js +20 -0
  50. package/dist/jobs.d.ts +16 -0
  51. package/dist/jobs.d.ts.map +1 -0
  52. package/dist/jobs.js +189 -0
  53. package/dist/keys.d.ts +10 -0
  54. package/dist/keys.d.ts.map +1 -0
  55. package/dist/keys.js +30 -0
  56. package/dist/kinds.d.ts +11 -0
  57. package/dist/kinds.d.ts.map +1 -0
  58. package/dist/kinds.js +4 -0
  59. package/dist/models.d.ts +71 -0
  60. package/dist/models.d.ts.map +1 -0
  61. package/dist/models.js +41 -0
  62. package/dist/plugin.d.ts +41 -0
  63. package/dist/plugin.d.ts.map +1 -0
  64. package/dist/plugin.js +229 -0
  65. package/dist/portal.d.ts +23 -0
  66. package/dist/portal.d.ts.map +1 -0
  67. package/dist/portal.js +66 -0
  68. package/dist/product.d.ts +57 -0
  69. package/dist/product.d.ts.map +1 -0
  70. package/dist/product.js +113 -0
  71. package/dist/running.d.ts +61 -0
  72. package/dist/running.d.ts.map +1 -0
  73. package/dist/running.js +35 -0
  74. package/dist/sdk-billing.d.ts +8 -0
  75. package/dist/sdk-billing.d.ts.map +1 -0
  76. package/dist/sdk-billing.js +200 -0
  77. package/dist/sdk-common.d.ts +13 -0
  78. package/dist/sdk-common.d.ts.map +1 -0
  79. package/dist/sdk-common.js +41 -0
  80. package/dist/sdk-customer.d.ts +9 -0
  81. package/dist/sdk-customer.d.ts.map +1 -0
  82. package/dist/sdk-customer.js +149 -0
  83. package/dist/sdk-invoices.d.ts +10 -0
  84. package/dist/sdk-invoices.d.ts.map +1 -0
  85. package/dist/sdk-invoices.js +53 -0
  86. package/dist/sdk-recovery.d.ts +5 -0
  87. package/dist/sdk-recovery.d.ts.map +1 -0
  88. package/dist/sdk-recovery.js +81 -0
  89. package/dist/sync.d.ts +8 -0
  90. package/dist/sync.d.ts.map +1 -0
  91. package/dist/sync.js +196 -0
  92. package/dist/testing.d.ts +24 -0
  93. package/dist/testing.d.ts.map +1 -0
  94. package/dist/testing.js +100 -0
  95. package/dist/webhook.d.ts +4 -0
  96. package/dist/webhook.d.ts.map +1 -0
  97. package/dist/webhook.js +73 -0
  98. package/package.json +51 -0
  99. package/src/accounts.ts +30 -0
  100. package/src/apply.ts +99 -0
  101. package/src/audit.ts +171 -0
  102. package/src/billing-gateway.ts +188 -0
  103. package/src/billing.ts +361 -0
  104. package/src/calls.ts +280 -0
  105. package/src/catalog.ts +327 -0
  106. package/src/credit.ts +36 -0
  107. package/src/credits.ts +206 -0
  108. package/src/customer.ts +113 -0
  109. package/src/describe.ts +76 -0
  110. package/src/duplicates.ts +45 -0
  111. package/src/fake-billing.ts +388 -0
  112. package/src/gateway.ts +333 -0
  113. package/src/index.ts +35 -0
  114. package/src/job-input.ts +24 -0
  115. package/src/jobs.ts +230 -0
  116. package/src/keys.ts +40 -0
  117. package/src/kinds.ts +15 -0
  118. package/src/models.ts +107 -0
  119. package/src/plugin.ts +307 -0
  120. package/src/portal.ts +95 -0
  121. package/src/product.ts +203 -0
  122. package/src/running.ts +104 -0
  123. package/src/sdk-billing.ts +220 -0
  124. package/src/sdk-common.ts +57 -0
  125. package/src/sdk-customer.ts +203 -0
  126. package/src/sdk-invoices.ts +81 -0
  127. package/src/sdk-recovery.ts +93 -0
  128. package/src/sync.ts +246 -0
  129. package/src/testing.ts +135 -0
  130. package/src/webhook.ts +100 -0
package/src/testing.ts ADDED
@@ -0,0 +1,135 @@
1
+ // @ultimabase/stripe/testing (D-371 point 8): a Stripe account in memory.
2
+ // Its secret key reaches it wherever a real key reaches Stripe, so a test
3
+ // runs the application's own calls, and no test reaches Stripe.
4
+ import { addFake } from './accounts.ts'
5
+ import type { FakeBilling } from './fake-billing.ts'
6
+ import { fakeBilling } from './fake-billing.ts'
7
+ import type {
8
+ PortalSettings,
9
+ StripeGateway,
10
+ StripePrice,
11
+ StripeProduct,
12
+ } from './gateway.ts'
13
+
14
+ export type {
15
+ CheckoutWrite,
16
+ CustomerProfile,
17
+ PortalFlow,
18
+ PortalWrite,
19
+ StripePayment,
20
+ StripeSubscription,
21
+ } from './billing-gateway.ts'
22
+ export type { FakeBilling, FakeCheckout, FakeCustomer } from './fake-billing.ts'
23
+ export { postEvent, thinEvent } from './fake-billing.ts'
24
+ export type { PortalSettings, StripePrice, StripeProduct } from './gateway.ts'
25
+
26
+ /**
27
+ * A Stripe account in memory: what the catalog wrote to it, its payers, and
28
+ * what a test does at Stripe. Hand `secret` and `webhookSecret` to
29
+ * `stripePlugin`'s keys.
30
+ */
31
+ export interface FakeStripe extends FakeBilling {
32
+ /** Its secret key: hand it to the code under test. */
33
+ readonly secret: string
34
+ /** The products the catalog manages, by id. */
35
+ readonly products: Map<string, StripeProduct>
36
+ /** The prices, by id. */
37
+ readonly prices: Map<string, StripePrice>
38
+ /** The customer portal configurations, by id. */
39
+ readonly portals: Map<string, PortalSettings>
40
+ }
41
+
42
+ /** The record of `map` at `id`, or Stripe's answer to an unknown id. */
43
+ const found = <T>(map: ReadonlyMap<string, T>, id: string): T => {
44
+ const record = map.get(id)
45
+ if (record === undefined) throw new Error(`No such object: '${id}'.`)
46
+ return record
47
+ }
48
+
49
+ /** A new Stripe account in memory; `secret` reaches it. */
50
+ export const fakeStripe = (): FakeStripe => {
51
+ const products = new Map<string, StripeProduct>()
52
+ const prices = new Map<string, StripePrice>()
53
+ const portals = new Map<string, PortalSettings>()
54
+ let created = 0
55
+ const newId = (prefix: string): string => {
56
+ created += 1
57
+ return `${prefix}_${String(created)}`
58
+ }
59
+ const billing = fakeBilling(prices, newId)
60
+ const fake: FakeStripe = {
61
+ ...billing.fake,
62
+ secret: `sk_test_fake_${crypto.randomUUID()}`,
63
+ products,
64
+ prices,
65
+ portals,
66
+ }
67
+ const gateway: StripeGateway = {
68
+ readCatalog: async () => {
69
+ const [portal] = portals
70
+ return {
71
+ products: [...products.values()],
72
+ prices: [...prices.values()],
73
+ portal:
74
+ portal === undefined ? null : { id: portal[0], settings: portal[1] },
75
+ }
76
+ },
77
+ createProduct: async ({ key, name, taxCode }) => {
78
+ const id = newId('prod')
79
+ products.set(id, { id, key, name, active: true, taxCode })
80
+ return id
81
+ },
82
+ updateProduct: async (id, { key, name, taxCode }) => {
83
+ products.set(id, {
84
+ ...found(products, id),
85
+ key,
86
+ name,
87
+ taxCode,
88
+ active: true,
89
+ })
90
+ },
91
+ archiveProduct: async (id) => {
92
+ products.set(id, { ...found(products, id), active: false })
93
+ },
94
+ createPrice: async (price) => {
95
+ found(products, price.productId)
96
+ // `transfer_lookup_key`: the key leaves the price that held it.
97
+ for (const held of prices.values()) {
98
+ if (held.lookupKey === price.lookupKey) {
99
+ prices.set(held.id, { ...held, lookupKey: null })
100
+ }
101
+ }
102
+ const id = newId('price')
103
+ prices.set(id, {
104
+ id,
105
+ productId: price.productId,
106
+ lookupKey: price.lookupKey,
107
+ amount: price.amount,
108
+ currency: price.currency,
109
+ recurring: price.recurring,
110
+ taxBehavior: price.taxBehavior,
111
+ nickname: price.nickname,
112
+ active: true,
113
+ metadata: { ...price.metadata },
114
+ })
115
+ return id
116
+ },
117
+ updatePrice: async (id, { nickname, metadata }) => {
118
+ const held = found(prices, id)
119
+ // Stripe merges the metadata it is sent.
120
+ const merged = { ...held.metadata, ...metadata }
121
+ prices.set(id, { ...held, nickname, metadata: merged })
122
+ },
123
+ archivePrice: async (id) => {
124
+ prices.set(id, { ...found(prices, id), active: false })
125
+ },
126
+ savePortal: async (id, settings) => {
127
+ if (id !== null) found(portals, id)
128
+ const saved = id ?? newId('bpc')
129
+ portals.set(saved, settings)
130
+ return saved
131
+ },
132
+ }
133
+ addFake(fake.secret, { ...gateway, ...billing.gateway })
134
+ return fake
135
+ }
package/src/webhook.ts ADDED
@@ -0,0 +1,100 @@
1
+ // The webhook (D-371 point 4): thin events, verified on the raw body,
2
+ // stored once with their job in one transaction, then answered 200. The
3
+ // event only says what changed: its job reads Stripe.
4
+ import type { Routes } from '@ultimabase/http'
5
+ import { routes } from '@ultimabase/http'
6
+ import type { Stripe } from 'stripe'
7
+ import { PublicError, transaction } from 'ultimabase'
8
+ import { eventJob } from './jobs.ts'
9
+ import { M, StripeEvent } from './models.ts'
10
+ import { runningOf } from './running.ts'
11
+
12
+ /** A thin event, as the plugin stores it. */
13
+ interface ThinEvent {
14
+ readonly id: string
15
+ readonly type: string
16
+ readonly livemode: boolean
17
+ readonly objectType: string
18
+ readonly objectId: string
19
+ }
20
+
21
+ const text = (from: unknown, key: string): string => {
22
+ const value: unknown =
23
+ typeof from === 'object' && from !== null
24
+ ? Reflect.get(from, key)
25
+ : undefined
26
+ return typeof value === 'string' ? value : ''
27
+ }
28
+
29
+ /** The verified event, or `undefined` for a wrong or stale signature. */
30
+ function verified(
31
+ client: Stripe,
32
+ payload: string,
33
+ signature: string,
34
+ secret: string,
35
+ ): ThinEvent | undefined {
36
+ let event: unknown
37
+ try {
38
+ event = client.parseEventNotification(payload, signature, secret)
39
+ } catch {
40
+ return undefined
41
+ }
42
+ const related: unknown = Reflect.get(Object(event), 'related_object')
43
+ return {
44
+ id: text(event, 'id'),
45
+ // `v1.customer.subscription.updated`: the v1 type it stands for.
46
+ type: text(event, 'type').replace(/^v1\./u, ''),
47
+ livemode: Reflect.get(Object(event), 'livemode') === true,
48
+ objectType: text(related, 'type'),
49
+ objectId: text(related, 'id'),
50
+ }
51
+ }
52
+
53
+ /** Stores the event and enqueues its job; one received twice is stored once. */
54
+ async function store(event: ThinEvent): Promise<void> {
55
+ try {
56
+ await transaction(async () => {
57
+ const { livemode: _, id: stripeId, ...data } = event
58
+ const { id } = await StripeEvent.create(
59
+ { data: { ...data, stripeId } },
60
+ M,
61
+ )
62
+ await eventJob.enqueue({ event: id })
63
+ })
64
+ } catch (error) {
65
+ if (error instanceof PublicError && error.code === 'UNIQUE_VIOLATION') {
66
+ return
67
+ }
68
+ throw error
69
+ }
70
+ }
71
+
72
+ /** The webhook's route, at `path`. */
73
+ export const webhookRoutes = (path: string): Routes =>
74
+ routes().post(path, async (c) => {
75
+ const { stripe, logger } = runningOf('The Stripe webhook')
76
+ if (stripe === undefined) {
77
+ const message = 'Payments are unavailable.'
78
+ return c.json({ code: 'PAYMENT_UNAVAILABLE', message }, 503)
79
+ }
80
+ const signature = c.req.header('stripe-signature')
81
+ // The raw body: the signature covers its exact bytes.
82
+ const payload = await c.req.text()
83
+ const event =
84
+ signature === undefined
85
+ ? undefined
86
+ : verified(stripe.client, payload, signature, stripe.webhook)
87
+ if (event === undefined) {
88
+ const message = 'The Stripe signature does not match.'
89
+ return c.json({ code: 'UNAUTHENTICATED', message }, 401)
90
+ }
91
+ // Another mode's event: acknowledged, so that Stripe stops sending
92
+ // it, and dropped.
93
+ if (event.livemode !== stripe.live) {
94
+ const details = { event: event.id, livemode: event.livemode }
95
+ logger.warn(details, 'A Stripe event of the other mode was dropped.')
96
+ return c.json({ received: false })
97
+ }
98
+ await store(event)
99
+ return c.json({ received: true })
100
+ })