munim-billing 0.1.0 → 0.3.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 +42 -201
- package/README.md +8 -27
- package/lib/cjs/index.js +2 -28
- package/lib/cjs/react-native.js +2 -118
- package/lib/cjs/web.js +2 -157
- package/lib/esm/chunks/57OHO4KT.js +2 -0
- package/lib/esm/client.d.ts +0 -1
- package/lib/esm/errors.d.ts +0 -1
- package/lib/esm/index.d.ts +0 -1
- package/lib/esm/index.js +2 -6
- package/lib/esm/react-native.d.ts +0 -1
- package/lib/esm/react-native.js +2 -114
- package/lib/esm/types.d.ts +5 -1
- package/lib/esm/version.d.ts +1 -2
- package/lib/esm/web.d.ts +0 -1
- package/lib/esm/web.js +2 -152
- package/lib/esm/webhooks.d.ts +0 -1
- package/package.json +11 -16
- package/lib/cjs/client.js +0 -233
- package/lib/cjs/client.js.map +0 -1
- package/lib/cjs/errors.js +0 -55
- package/lib/cjs/errors.js.map +0 -1
- package/lib/cjs/index.js.map +0 -1
- package/lib/cjs/react-native.js.map +0 -1
- package/lib/cjs/types.js +0 -21
- package/lib/cjs/types.js.map +0 -1
- package/lib/cjs/version.js +0 -6
- package/lib/cjs/version.js.map +0 -1
- package/lib/cjs/web.js.map +0 -1
- package/lib/cjs/webhooks.js +0 -137
- package/lib/cjs/webhooks.js.map +0 -1
- package/lib/esm/client.d.ts.map +0 -1
- package/lib/esm/client.js +0 -226
- package/lib/esm/client.js.map +0 -1
- package/lib/esm/errors.d.ts.map +0 -1
- package/lib/esm/errors.js +0 -50
- package/lib/esm/errors.js.map +0 -1
- package/lib/esm/index.d.ts.map +0 -1
- package/lib/esm/index.js.map +0 -1
- package/lib/esm/react-native.d.ts.map +0 -1
- package/lib/esm/react-native.js.map +0 -1
- package/lib/esm/types.d.ts.map +0 -1
- package/lib/esm/types.js +0 -18
- package/lib/esm/types.js.map +0 -1
- package/lib/esm/version.d.ts.map +0 -1
- package/lib/esm/version.js +0 -3
- package/lib/esm/version.js.map +0 -1
- package/lib/esm/web.d.ts.map +0 -1
- package/lib/esm/web.js.map +0 -1
- package/lib/esm/webhooks.d.ts.map +0 -1
- package/lib/esm/webhooks.js +0 -129
- package/lib/esm/webhooks.js.map +0 -1
- package/src/client.ts +0 -314
- package/src/errors.ts +0 -74
- package/src/index.ts +0 -26
- package/src/react-native.ts +0 -154
- package/src/types.ts +0 -360
- package/src/version.ts +0 -2
- package/src/web.ts +0 -182
- package/src/webhooks.ts +0 -169
package/src/types.ts
DELETED
|
@@ -1,360 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Object types returned by the Munim Billing API (docs/API.md).
|
|
3
|
-
*
|
|
4
|
-
* Amounts are integers in cents, currencies are lowercase ISO 4217 codes
|
|
5
|
-
* (only `usd` for now), and timestamps are Unix milliseconds.
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
export type Currency = 'usd'
|
|
9
|
-
|
|
10
|
-
export type Metadata = Record<string, string>
|
|
11
|
-
|
|
12
|
-
export type Mode = 'test' | 'live'
|
|
13
|
-
|
|
14
|
-
export type Product = {
|
|
15
|
-
id: string
|
|
16
|
-
object: 'product'
|
|
17
|
-
name: string
|
|
18
|
-
description: string | null
|
|
19
|
-
image_url: string | null
|
|
20
|
-
/** Stripe Tax product tax code (txcd_…). */
|
|
21
|
-
tax_code: string | null
|
|
22
|
-
status: 'active' | 'archived'
|
|
23
|
-
metadata: Metadata
|
|
24
|
-
created_at: number
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export type PriceInterval = 'day' | 'week' | 'month' | 'year'
|
|
28
|
-
|
|
29
|
-
export type Price = {
|
|
30
|
-
id: string
|
|
31
|
-
object: 'price'
|
|
32
|
-
product_id: string
|
|
33
|
-
description: string | null
|
|
34
|
-
unit_amount: number
|
|
35
|
-
currency: Currency
|
|
36
|
-
type: 'one_time' | 'recurring'
|
|
37
|
-
/** `null` for one-time prices. */
|
|
38
|
-
interval: PriceInterval | null
|
|
39
|
-
/** `null` for one-time prices. */
|
|
40
|
-
interval_count: number | null
|
|
41
|
-
trial_days: number | null
|
|
42
|
-
status: 'active' | 'archived'
|
|
43
|
-
metadata: Metadata
|
|
44
|
-
created_at: number
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
export type Checkout = {
|
|
48
|
-
id: string
|
|
49
|
-
object: 'checkout'
|
|
50
|
-
status: 'open' | 'completed' | 'expired'
|
|
51
|
-
/** The hosted checkout page. Open it in a browser, an in-app sheet, or an overlay. */
|
|
52
|
-
url: string
|
|
53
|
-
price_id: string
|
|
54
|
-
product_id: string
|
|
55
|
-
quantity: number
|
|
56
|
-
/** Before tax. */
|
|
57
|
-
amount_subtotal: number
|
|
58
|
-
/** Sales tax / VAT, once the customer picks a country. */
|
|
59
|
-
amount_tax: number
|
|
60
|
-
amount_total: number
|
|
61
|
-
currency: Currency
|
|
62
|
-
mode: 'payment' | 'subscription'
|
|
63
|
-
customer_id: string | null
|
|
64
|
-
customer_email: string | null
|
|
65
|
-
success_url: string | null
|
|
66
|
-
cancel_url: string | null
|
|
67
|
-
transaction_id: string | null
|
|
68
|
-
subscription_id: string | null
|
|
69
|
-
metadata: Metadata
|
|
70
|
-
expires_at: number
|
|
71
|
-
created_at: number
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
export type Customer = {
|
|
75
|
-
id: string
|
|
76
|
-
object: 'customer'
|
|
77
|
-
email: string
|
|
78
|
-
name: string | null
|
|
79
|
-
metadata: Metadata
|
|
80
|
-
created_at: number
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
export type Processor = 'stripe' | 'paypal'
|
|
84
|
-
|
|
85
|
-
export type Transaction = {
|
|
86
|
-
id: string
|
|
87
|
-
object: 'transaction'
|
|
88
|
-
status: 'completed' | 'partially_refunded' | 'refunded' | 'disputed'
|
|
89
|
-
processor: Processor
|
|
90
|
-
/** `'card'`, `'apple_pay'`, `'paypal'`, `'venmo'`, `'link'`, `'cashapp'`, … */
|
|
91
|
-
payment_method: string | null
|
|
92
|
-
customer_id: string | null
|
|
93
|
-
checkout_id: string | null
|
|
94
|
-
subscription_id: string | null
|
|
95
|
-
product_id: string | null
|
|
96
|
-
price_id: string | null
|
|
97
|
-
amount: number
|
|
98
|
-
currency: Currency
|
|
99
|
-
/** Munim's fee. */
|
|
100
|
-
/** Sales tax / VAT included in `amount` (Munim remits it; not part of your earnings). */
|
|
101
|
-
tax: number
|
|
102
|
-
fee: number
|
|
103
|
-
/** What the seller earns: `amount - fee`. */
|
|
104
|
-
net: number
|
|
105
|
-
refunded_amount: number
|
|
106
|
-
metadata: Metadata
|
|
107
|
-
created_at: number
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
export type SubscriptionStatus =
|
|
111
|
-
| 'incomplete'
|
|
112
|
-
| 'trialing'
|
|
113
|
-
| 'active'
|
|
114
|
-
| 'past_due'
|
|
115
|
-
| 'paused'
|
|
116
|
-
| 'canceled'
|
|
117
|
-
|
|
118
|
-
export type Subscription = {
|
|
119
|
-
id: string
|
|
120
|
-
object: 'subscription'
|
|
121
|
-
status: SubscriptionStatus
|
|
122
|
-
processor: Processor
|
|
123
|
-
customer_id: string | null
|
|
124
|
-
product_id: string
|
|
125
|
-
price_id: string
|
|
126
|
-
quantity: number
|
|
127
|
-
current_period_start: number | null
|
|
128
|
-
current_period_end: number | null
|
|
129
|
-
cancel_at_period_end: boolean
|
|
130
|
-
canceled_at: number | null
|
|
131
|
-
checkout_id: string | null
|
|
132
|
-
metadata: Metadata
|
|
133
|
-
created_at: number
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
export type Balance = {
|
|
137
|
-
object: 'balance'
|
|
138
|
-
currency: Currency
|
|
139
|
-
/** Can be paid out now. */
|
|
140
|
-
available: number
|
|
141
|
-
/** Still in the hold. */
|
|
142
|
-
pending: number
|
|
143
|
-
/** Lifetime payouts. */
|
|
144
|
-
paid_out: number
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
export type Payout = {
|
|
148
|
-
id: string
|
|
149
|
-
object: 'payout'
|
|
150
|
-
amount: number
|
|
151
|
-
currency: Currency
|
|
152
|
-
method: Processor
|
|
153
|
-
status: 'pending' | 'paid' | 'failed'
|
|
154
|
-
failure_reason: string | null
|
|
155
|
-
created_at: number
|
|
156
|
-
paid_at: number | null
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
export type List<T> = {
|
|
160
|
-
object: 'list'
|
|
161
|
-
data: T[]
|
|
162
|
-
has_more: boolean
|
|
163
|
-
next_cursor: string | null
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
export type ManageUrl = {
|
|
167
|
-
url: string
|
|
168
|
-
expires_at: number
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
// ---------------------------------------------------------------------------
|
|
172
|
-
// Events
|
|
173
|
-
|
|
174
|
-
type EventOf<Type extends string, Obj> = {
|
|
175
|
-
id: string
|
|
176
|
-
object: 'event'
|
|
177
|
-
type: Type
|
|
178
|
-
created_at: number
|
|
179
|
-
data: { object: Obj }
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
export type CheckoutEvent = EventOf<'checkout.completed', Checkout>
|
|
183
|
-
export type TransactionEvent = EventOf<
|
|
184
|
-
'transaction.completed' | 'transaction.refunded' | 'transaction.disputed',
|
|
185
|
-
Transaction
|
|
186
|
-
>
|
|
187
|
-
export type SubscriptionEvent = EventOf<
|
|
188
|
-
'subscription.created' | 'subscription.updated' | 'subscription.canceled',
|
|
189
|
-
Subscription
|
|
190
|
-
>
|
|
191
|
-
export type PayoutEvent = EventOf<'payout.paid' | 'payout.failed', Payout>
|
|
192
|
-
|
|
193
|
-
/** A webhook event. Narrow it with `event.type`. */
|
|
194
|
-
export type Event = CheckoutEvent | TransactionEvent | SubscriptionEvent | PayoutEvent
|
|
195
|
-
|
|
196
|
-
export type EventType = Event['type']
|
|
197
|
-
|
|
198
|
-
export const EVENT_TYPES: readonly EventType[] = [
|
|
199
|
-
'checkout.completed',
|
|
200
|
-
'transaction.completed',
|
|
201
|
-
'transaction.refunded',
|
|
202
|
-
'transaction.disputed',
|
|
203
|
-
'subscription.created',
|
|
204
|
-
'subscription.updated',
|
|
205
|
-
'subscription.canceled',
|
|
206
|
-
'payout.paid',
|
|
207
|
-
'payout.failed',
|
|
208
|
-
]
|
|
209
|
-
|
|
210
|
-
// ---------------------------------------------------------------------------
|
|
211
|
-
// Request params
|
|
212
|
-
|
|
213
|
-
export type ListParams = {
|
|
214
|
-
/** 1–100, default 25. */
|
|
215
|
-
limit?: number
|
|
216
|
-
cursor?: string
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
export type ProductCreateParams = {
|
|
220
|
-
name: string
|
|
221
|
-
description?: string
|
|
222
|
-
image_url?: string
|
|
223
|
-
tax_code?: string
|
|
224
|
-
metadata?: Metadata
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
export type ProductUpdateParams = {
|
|
228
|
-
name?: string
|
|
229
|
-
description?: string | null
|
|
230
|
-
image_url?: string | null
|
|
231
|
-
tax_code?: string | null
|
|
232
|
-
status?: 'active' | 'archived'
|
|
233
|
-
metadata?: Metadata
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
export type PriceCreateParams = {
|
|
237
|
-
product_id: string
|
|
238
|
-
unit_amount: number
|
|
239
|
-
currency: Currency
|
|
240
|
-
type: 'one_time' | 'recurring'
|
|
241
|
-
interval?: PriceInterval
|
|
242
|
-
interval_count?: number
|
|
243
|
-
trial_days?: number
|
|
244
|
-
description?: string
|
|
245
|
-
metadata?: Metadata
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
export type PriceUpdateParams = {
|
|
249
|
-
description?: string | null
|
|
250
|
-
status?: 'active' | 'archived'
|
|
251
|
-
metadata?: Metadata
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
export type PriceListParams = ListParams & { product_id?: string }
|
|
255
|
-
|
|
256
|
-
export type CheckoutCreateParams = {
|
|
257
|
-
price_id: string
|
|
258
|
-
quantity?: number
|
|
259
|
-
customer_email?: string
|
|
260
|
-
customer_id?: string
|
|
261
|
-
/** `https://` URL or an app scheme (`myapp://billing/done`). `checkout_id` is appended. */
|
|
262
|
-
success_url?: string
|
|
263
|
-
cancel_url?: string
|
|
264
|
-
/** Your own user id, so this purchase counts toward that user's entitlements (`subscribers.retrieve`). */
|
|
265
|
-
app_user_id?: string
|
|
266
|
-
metadata?: Metadata
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
export type CustomerListParams = ListParams & { email?: string }
|
|
270
|
-
|
|
271
|
-
export type TransactionListParams = ListParams & {
|
|
272
|
-
customer_id?: string
|
|
273
|
-
subscription_id?: string
|
|
274
|
-
}
|
|
275
|
-
|
|
276
|
-
export type RefundParams = {
|
|
277
|
-
/** Cents. Defaults to everything not yet refunded. */
|
|
278
|
-
amount?: number
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
export type SubscriptionListParams = ListParams & {
|
|
282
|
-
customer_id?: string
|
|
283
|
-
status?: SubscriptionStatus
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
export type SubscriptionCancelParams = {
|
|
287
|
-
/** Default `true`: the subscription runs to the end of the paid period. */
|
|
288
|
-
at_period_end?: boolean
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
// ---------------------------------------------------------------------------
|
|
292
|
-
// App Store / Google Play subscriptions and entitlements
|
|
293
|
-
|
|
294
|
-
export type StorePlatform = 'apple' | 'google'
|
|
295
|
-
|
|
296
|
-
export type Entitlement = {
|
|
297
|
-
id: string
|
|
298
|
-
object: 'entitlement'
|
|
299
|
-
identifier: string
|
|
300
|
-
description: string | null
|
|
301
|
-
/** App Store / Play product ids and/or Munim product ids (`pro_…`). */
|
|
302
|
-
product_ids: string[]
|
|
303
|
-
created_at: number
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
export type StoreSubscription = {
|
|
307
|
-
id: string
|
|
308
|
-
object: 'store_subscription'
|
|
309
|
-
platform: StorePlatform
|
|
310
|
-
app_user_id: string | null
|
|
311
|
-
/** The store's product id. */
|
|
312
|
-
product_id: string
|
|
313
|
-
status: 'active' | 'trialing' | 'in_grace_period' | 'in_billing_retry' | 'paused' | 'expired' | 'revoked'
|
|
314
|
-
will_renew: boolean
|
|
315
|
-
/** null = lifetime (non-consumable / one-time product). */
|
|
316
|
-
expires_at: number | null
|
|
317
|
-
environment: 'sandbox' | 'production'
|
|
318
|
-
price_amount: number | null
|
|
319
|
-
currency: string | null
|
|
320
|
-
created_at: number
|
|
321
|
-
updated_at: number
|
|
322
|
-
}
|
|
323
|
-
|
|
324
|
-
export type EntitlementState = {
|
|
325
|
-
active: boolean
|
|
326
|
-
expires_at: number | null
|
|
327
|
-
product_id: string
|
|
328
|
-
source: 'web' | StorePlatform
|
|
329
|
-
will_renew: boolean
|
|
330
|
-
}
|
|
331
|
-
|
|
332
|
-
export type Subscriber = {
|
|
333
|
-
object: 'subscriber'
|
|
334
|
-
app_user_id: string
|
|
335
|
-
customer_id: string | null
|
|
336
|
-
entitlements: Record<string, EntitlementState>
|
|
337
|
-
store_subscriptions: StoreSubscription[]
|
|
338
|
-
subscriptions: Subscription[]
|
|
339
|
-
}
|
|
340
|
-
|
|
341
|
-
export type AppleReceiptParams = {
|
|
342
|
-
app_user_id: string
|
|
343
|
-
/** StoreKit 2 transaction id… */
|
|
344
|
-
transaction_id?: string
|
|
345
|
-
/** …or its signed JWS. */
|
|
346
|
-
signed_transaction?: string
|
|
347
|
-
/** Bundle id, when you have more than one Apple app. */
|
|
348
|
-
app_identifier?: string
|
|
349
|
-
}
|
|
350
|
-
|
|
351
|
-
export type GoogleReceiptParams = {
|
|
352
|
-
app_user_id: string
|
|
353
|
-
product_id: string
|
|
354
|
-
purchase_token: string
|
|
355
|
-
type?: 'subscription' | 'one_time'
|
|
356
|
-
/** Package name, when you have more than one Android app. */
|
|
357
|
-
app_identifier?: string
|
|
358
|
-
}
|
|
359
|
-
|
|
360
|
-
export type StoreSubscriptionListParams = ListParams & { app_user_id?: string }
|
package/src/version.ts
DELETED
package/src/web.ts
DELETED
|
@@ -1,182 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* munim-billing/web: open a Munim checkout from a web page.
|
|
3
|
-
*
|
|
4
|
-
* Create the checkout on your server (`munim.checkouts.create`) and pass its
|
|
5
|
-
* `url` here. Never put your secret key in browser code.
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
/** Messages the checkout page posts to the page that opened it. */
|
|
9
|
-
export type CheckoutMessage = {
|
|
10
|
-
source: 'munim-billing'
|
|
11
|
-
type: 'checkout.completed' | 'checkout.closed'
|
|
12
|
-
checkout_id: string
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export type OpenCheckoutOptions = {
|
|
16
|
-
/** The customer paid. Called once; the overlay closes itself. */
|
|
17
|
-
onComplete?: (result: { checkoutId: string }) => void
|
|
18
|
-
/** The overlay closed without a payment (close button, Escape, or the page asked). */
|
|
19
|
-
onClose?: () => void
|
|
20
|
-
/** Overlay z-index. Default 2147483000. */
|
|
21
|
-
zIndex?: number
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export type CheckoutHandle = {
|
|
25
|
-
/** Closes the overlay. Calls `onClose` if the checkout didn't complete. */
|
|
26
|
-
close: () => void
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
/** Sends the whole page to the checkout. */
|
|
30
|
-
export function redirectToCheckout(url: string): void {
|
|
31
|
-
assertCheckoutUrl(url)
|
|
32
|
-
window.location.assign(url)
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
function assertCheckoutUrl(url: string): URL {
|
|
36
|
-
let parsed: URL
|
|
37
|
-
try {
|
|
38
|
-
parsed = new URL(url)
|
|
39
|
-
} catch {
|
|
40
|
-
throw new Error(`munim-billing: not a checkout URL: ${url}`)
|
|
41
|
-
}
|
|
42
|
-
if (parsed.protocol !== 'https:' && parsed.hostname !== 'localhost' && parsed.hostname !== '127.0.0.1') {
|
|
43
|
-
throw new Error('munim-billing: checkout URLs must be https.')
|
|
44
|
-
}
|
|
45
|
-
return parsed
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
/** Whether a `postMessage` payload is one of the checkout page's messages. */
|
|
49
|
-
export function isCheckoutMessage(data: unknown): data is CheckoutMessage {
|
|
50
|
-
if (!data || typeof data !== 'object') return false
|
|
51
|
-
const message = data as Partial<CheckoutMessage>
|
|
52
|
-
return (
|
|
53
|
-
message.source === 'munim-billing' &&
|
|
54
|
-
(message.type === 'checkout.completed' || message.type === 'checkout.closed') &&
|
|
55
|
-
typeof message.checkout_id === 'string'
|
|
56
|
-
)
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* Shows the checkout in a full-screen overlay on top of your page.
|
|
61
|
-
*
|
|
62
|
-
* The checkout page tells the overlay when the customer has paid (or wants
|
|
63
|
-
* out) with `postMessage`; only messages from the checkout's own origin
|
|
64
|
-
* count. Card wallets that need the top window (some bank redirects) still
|
|
65
|
-
* work: the checkout page handles those itself.
|
|
66
|
-
*/
|
|
67
|
-
export function openCheckout(url: string, options: OpenCheckoutOptions = {}): CheckoutHandle {
|
|
68
|
-
const parsed = assertCheckoutUrl(url)
|
|
69
|
-
const origin = parsed.origin
|
|
70
|
-
// Tell the page it's embedded, so it posts messages instead of redirecting.
|
|
71
|
-
parsed.searchParams.set('embed', '1')
|
|
72
|
-
|
|
73
|
-
const doc = window.document
|
|
74
|
-
const overlay = doc.createElement('div')
|
|
75
|
-
overlay.setAttribute('role', 'dialog')
|
|
76
|
-
overlay.setAttribute('aria-modal', 'true')
|
|
77
|
-
overlay.setAttribute('aria-label', 'Checkout')
|
|
78
|
-
overlay.setAttribute('data-munim-billing-overlay', '')
|
|
79
|
-
Object.assign(overlay.style, {
|
|
80
|
-
position: 'fixed',
|
|
81
|
-
inset: '0',
|
|
82
|
-
zIndex: String(options.zIndex ?? 2147483000),
|
|
83
|
-
background: 'rgba(15, 15, 20, 0.55)',
|
|
84
|
-
display: 'flex',
|
|
85
|
-
alignItems: 'center',
|
|
86
|
-
justifyContent: 'center',
|
|
87
|
-
} satisfies Partial<CSSStyleDeclaration>)
|
|
88
|
-
|
|
89
|
-
const frame = doc.createElement('iframe')
|
|
90
|
-
frame.src = parsed.toString()
|
|
91
|
-
frame.title = 'Checkout'
|
|
92
|
-
// Payment Request (Apple Pay, Google Pay) needs this inside an iframe.
|
|
93
|
-
frame.setAttribute('allow', 'payment *')
|
|
94
|
-
Object.assign(frame.style, {
|
|
95
|
-
width: '100%',
|
|
96
|
-
height: '100%',
|
|
97
|
-
maxWidth: '480px',
|
|
98
|
-
maxHeight: '760px',
|
|
99
|
-
border: '0',
|
|
100
|
-
borderRadius: '14px',
|
|
101
|
-
background: '#ffffff',
|
|
102
|
-
boxShadow: '0 20px 60px rgba(0,0,0,0.35)',
|
|
103
|
-
} satisfies Partial<CSSStyleDeclaration>)
|
|
104
|
-
if (window.matchMedia?.('(max-width: 520px)').matches) {
|
|
105
|
-
frame.style.maxWidth = 'none'
|
|
106
|
-
frame.style.maxHeight = 'none'
|
|
107
|
-
frame.style.borderRadius = '0'
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
const closeButton = doc.createElement('button')
|
|
111
|
-
closeButton.type = 'button'
|
|
112
|
-
closeButton.setAttribute('aria-label', 'Close checkout')
|
|
113
|
-
closeButton.textContent = '×'
|
|
114
|
-
Object.assign(closeButton.style, {
|
|
115
|
-
position: 'absolute',
|
|
116
|
-
top: '12px',
|
|
117
|
-
right: '12px',
|
|
118
|
-
width: '36px',
|
|
119
|
-
height: '36px',
|
|
120
|
-
borderRadius: '18px',
|
|
121
|
-
border: '0',
|
|
122
|
-
background: 'rgba(255,255,255,0.92)',
|
|
123
|
-
color: '#111',
|
|
124
|
-
fontSize: '24px',
|
|
125
|
-
lineHeight: '36px',
|
|
126
|
-
cursor: 'pointer',
|
|
127
|
-
boxShadow: '0 2px 8px rgba(0,0,0,0.25)',
|
|
128
|
-
} satisfies Partial<CSSStyleDeclaration>)
|
|
129
|
-
|
|
130
|
-
overlay.appendChild(frame)
|
|
131
|
-
overlay.appendChild(closeButton)
|
|
132
|
-
|
|
133
|
-
const previousOverflow = doc.body.style.overflow
|
|
134
|
-
doc.body.style.overflow = 'hidden'
|
|
135
|
-
doc.body.appendChild(overlay)
|
|
136
|
-
|
|
137
|
-
let done = false
|
|
138
|
-
|
|
139
|
-
const teardown = () => {
|
|
140
|
-
window.removeEventListener('message', onMessage)
|
|
141
|
-
doc.removeEventListener('keydown', onKeyDown)
|
|
142
|
-
overlay.remove()
|
|
143
|
-
doc.body.style.overflow = previousOverflow
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
const close = () => {
|
|
147
|
-
if (done) return
|
|
148
|
-
done = true
|
|
149
|
-
teardown()
|
|
150
|
-
options.onClose?.()
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
const complete = (checkoutId: string) => {
|
|
154
|
-
if (done) return
|
|
155
|
-
done = true
|
|
156
|
-
teardown()
|
|
157
|
-
options.onComplete?.({ checkoutId })
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
function onMessage(event: MessageEvent) {
|
|
161
|
-
if (event.origin !== origin || event.source !== frame.contentWindow) return
|
|
162
|
-
if (!isCheckoutMessage(event.data)) return
|
|
163
|
-
if (event.data.type === 'checkout.completed') {
|
|
164
|
-
complete(event.data.checkout_id)
|
|
165
|
-
} else {
|
|
166
|
-
close()
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
function onKeyDown(event: KeyboardEvent) {
|
|
171
|
-
if (event.key === 'Escape') close()
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
window.addEventListener('message', onMessage)
|
|
175
|
-
doc.addEventListener('keydown', onKeyDown)
|
|
176
|
-
closeButton.addEventListener('click', close)
|
|
177
|
-
overlay.addEventListener('click', (event) => {
|
|
178
|
-
if (event.target === overlay) close()
|
|
179
|
-
})
|
|
180
|
-
|
|
181
|
-
return { close }
|
|
182
|
-
}
|
package/src/webhooks.ts
DELETED
|
@@ -1,169 +0,0 @@
|
|
|
1
|
-
import { MunimBillingError } from './errors.js'
|
|
2
|
-
import type { Event } from './types.js'
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Webhook signatures.
|
|
6
|
-
*
|
|
7
|
-
* Munim signs each delivery with the endpoint's secret (`whsec_…`):
|
|
8
|
-
*
|
|
9
|
-
* Munim-Signature: t=<unix seconds>,v1=<hex HMAC-SHA256(secret, `${t}.${rawBody}`)>
|
|
10
|
-
*
|
|
11
|
-
* The HMAC key is the whole secret string, `whsec_` prefix included, as
|
|
12
|
-
* UTF-8. A header may carry several `v1=` values (while a secret rotates);
|
|
13
|
-
* any one matching is enough.
|
|
14
|
-
*
|
|
15
|
-
* Uses WebCrypto (`crypto.subtle`), so it runs in Node 20+, Deno, Bun,
|
|
16
|
-
* edge runtimes, Convex and browsers. Node 18 needs
|
|
17
|
-
* `--experimental-global-webcrypto`.
|
|
18
|
-
*/
|
|
19
|
-
|
|
20
|
-
export const SIGNATURE_HEADER = 'Munim-Signature'
|
|
21
|
-
export const EVENT_ID_HEADER = 'Munim-Event-Id'
|
|
22
|
-
export const DEFAULT_TOLERANCE_SECONDS = 300
|
|
23
|
-
|
|
24
|
-
const encoder = new TextEncoder()
|
|
25
|
-
|
|
26
|
-
function subtle(): SubtleCrypto {
|
|
27
|
-
const value = (globalThis as { crypto?: Crypto }).crypto?.subtle
|
|
28
|
-
if (!value) {
|
|
29
|
-
throw new MunimBillingError(
|
|
30
|
-
'Web Crypto (crypto.subtle) is not available in this runtime. Use Node 20+ or another runtime with WebCrypto.',
|
|
31
|
-
{ type: 'internal' }
|
|
32
|
-
)
|
|
33
|
-
}
|
|
34
|
-
return value
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
function toHex(buffer: ArrayBuffer): string {
|
|
38
|
-
return Array.from(new Uint8Array(buffer), (byte) => byte.toString(16).padStart(2, '0')).join('')
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
function asString(body: string | Uint8Array): string {
|
|
42
|
-
return typeof body === 'string' ? body : new TextDecoder().decode(body)
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
/** Hex HMAC-SHA256 of `${timestamp}.${rawBody}` with `secret` as the key. */
|
|
46
|
-
export async function computeSignature(
|
|
47
|
-
secret: string,
|
|
48
|
-
rawBody: string | Uint8Array,
|
|
49
|
-
timestamp: number
|
|
50
|
-
): Promise<string> {
|
|
51
|
-
const key = await subtle().importKey(
|
|
52
|
-
'raw',
|
|
53
|
-
encoder.encode(secret),
|
|
54
|
-
{ name: 'HMAC', hash: 'SHA-256' },
|
|
55
|
-
false,
|
|
56
|
-
['sign']
|
|
57
|
-
)
|
|
58
|
-
const mac = await subtle().sign('HMAC', key, encoder.encode(`${timestamp}.${asString(rawBody)}`))
|
|
59
|
-
return toHex(mac)
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* The `Munim-Signature` header value for a payload: `t=<timestamp>,v1=<hex>`.
|
|
64
|
-
*
|
|
65
|
-
* @param timestamp Unix **seconds**. Defaults to now.
|
|
66
|
-
*/
|
|
67
|
-
export async function signWebhookPayload(
|
|
68
|
-
secret: string,
|
|
69
|
-
rawBody: string | Uint8Array,
|
|
70
|
-
timestamp: number = Math.floor(Date.now() / 1000)
|
|
71
|
-
): Promise<string> {
|
|
72
|
-
const t = Math.floor(timestamp)
|
|
73
|
-
return `t=${t},v1=${await computeSignature(secret, rawBody, t)}`
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
/** Splits `t=…,v1=…,v1=…` into its timestamp and signatures. */
|
|
77
|
-
export function parseSignatureHeader(header: string): { timestamp: number | null; signatures: string[] } {
|
|
78
|
-
let timestamp: number | null = null
|
|
79
|
-
const signatures: string[] = []
|
|
80
|
-
for (const part of header.split(',')) {
|
|
81
|
-
const index = part.indexOf('=')
|
|
82
|
-
if (index < 0) continue
|
|
83
|
-
const key = part.slice(0, index).trim()
|
|
84
|
-
const value = part.slice(index + 1).trim()
|
|
85
|
-
if (key === 't' && /^\d+$/.test(value)) {
|
|
86
|
-
timestamp = Number(value)
|
|
87
|
-
} else if (key === 'v1' && value) {
|
|
88
|
-
signatures.push(value.toLowerCase())
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
return { timestamp, signatures }
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
/** Compares two strings in time that depends only on their length. */
|
|
95
|
-
export function timingSafeEqual(a: string, b: string): boolean {
|
|
96
|
-
if (a.length !== b.length) return false
|
|
97
|
-
let diff = 0
|
|
98
|
-
for (let i = 0; i < a.length; i++) {
|
|
99
|
-
diff |= a.charCodeAt(i) ^ b.charCodeAt(i)
|
|
100
|
-
}
|
|
101
|
-
return diff === 0
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
export type VerifyWebhookOptions = {
|
|
105
|
-
/** How far the signature's timestamp may be from now. Default 300. `0` turns the check off. */
|
|
106
|
-
toleranceSeconds?: number
|
|
107
|
-
/** "Now" in Unix milliseconds, for tests. Defaults to `Date.now()`. */
|
|
108
|
-
now?: number
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
function fail(message: string): never {
|
|
112
|
-
throw new MunimBillingError(message, { type: 'signature_verification', status: 400 })
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
/**
|
|
116
|
-
* Checks a webhook delivery and returns its event.
|
|
117
|
-
*
|
|
118
|
-
* Pass the request body exactly as received (before any JSON parsing), the
|
|
119
|
-
* `Munim-Signature` header, and the endpoint's `whsec_…` secret. Throws a
|
|
120
|
-
* `MunimBillingError` with `type: 'signature_verification'` when the
|
|
121
|
-
* signature doesn't match or the timestamp is outside the tolerance.
|
|
122
|
-
*/
|
|
123
|
-
export async function verifyWebhook(
|
|
124
|
-
rawBody: string | Uint8Array,
|
|
125
|
-
signatureHeader: string | null | undefined,
|
|
126
|
-
secret: string,
|
|
127
|
-
options: VerifyWebhookOptions = {}
|
|
128
|
-
): Promise<Event> {
|
|
129
|
-
if (!secret) fail('No webhook secret was given.')
|
|
130
|
-
if (!signatureHeader) fail(`Missing ${SIGNATURE_HEADER} header.`)
|
|
131
|
-
|
|
132
|
-
const { timestamp, signatures } = parseSignatureHeader(signatureHeader)
|
|
133
|
-
if (timestamp === null || signatures.length === 0) {
|
|
134
|
-
fail(`Malformed ${SIGNATURE_HEADER} header.`)
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
const expected = await computeSignature(secret, rawBody, timestamp)
|
|
138
|
-
if (!signatures.some((signature) => timingSafeEqual(signature, expected))) {
|
|
139
|
-
fail('Webhook signature does not match. Check the secret and that the body is the raw, unparsed request body.')
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
const tolerance = options.toleranceSeconds ?? DEFAULT_TOLERANCE_SECONDS
|
|
143
|
-
if (tolerance > 0) {
|
|
144
|
-
const nowSeconds = Math.floor((options.now ?? Date.now()) / 1000)
|
|
145
|
-
if (Math.abs(nowSeconds - timestamp) > tolerance) {
|
|
146
|
-
fail('Webhook timestamp is outside the tolerance.')
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
try {
|
|
151
|
-
return JSON.parse(asString(rawBody)) as Event
|
|
152
|
-
} catch (cause) {
|
|
153
|
-
throw new MunimBillingError('Webhook body is not valid JSON.', {
|
|
154
|
-
type: 'signature_verification',
|
|
155
|
-
status: 400,
|
|
156
|
-
cause,
|
|
157
|
-
})
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
/** `munim.webhooks` / `MunimBilling.webhooks`. */
|
|
162
|
-
export const webhooks = {
|
|
163
|
-
verify: verifyWebhook,
|
|
164
|
-
sign: signWebhookPayload,
|
|
165
|
-
computeSignature,
|
|
166
|
-
parseHeader: parseSignatureHeader,
|
|
167
|
-
SIGNATURE_HEADER,
|
|
168
|
-
EVENT_ID_HEADER,
|
|
169
|
-
} as const
|