munim-billing 0.2.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.
Files changed (60) hide show
  1. package/LICENSE +42 -201
  2. package/README.md +8 -27
  3. package/lib/cjs/index.js +2 -28
  4. package/lib/cjs/react-native.js +2 -118
  5. package/lib/cjs/web.js +2 -157
  6. package/lib/esm/chunks/57OHO4KT.js +2 -0
  7. package/lib/esm/client.d.ts +0 -1
  8. package/lib/esm/errors.d.ts +0 -1
  9. package/lib/esm/index.d.ts +0 -1
  10. package/lib/esm/index.js +2 -6
  11. package/lib/esm/react-native.d.ts +0 -1
  12. package/lib/esm/react-native.js +2 -114
  13. package/lib/esm/types.d.ts +0 -1
  14. package/lib/esm/version.d.ts +1 -2
  15. package/lib/esm/web.d.ts +0 -1
  16. package/lib/esm/web.js +2 -152
  17. package/lib/esm/webhooks.d.ts +0 -1
  18. package/package.json +5 -14
  19. package/lib/cjs/client.js +0 -233
  20. package/lib/cjs/client.js.map +0 -1
  21. package/lib/cjs/errors.js +0 -55
  22. package/lib/cjs/errors.js.map +0 -1
  23. package/lib/cjs/index.js.map +0 -1
  24. package/lib/cjs/react-native.js.map +0 -1
  25. package/lib/cjs/types.js +0 -21
  26. package/lib/cjs/types.js.map +0 -1
  27. package/lib/cjs/version.js +0 -6
  28. package/lib/cjs/version.js.map +0 -1
  29. package/lib/cjs/web.js.map +0 -1
  30. package/lib/cjs/webhooks.js +0 -137
  31. package/lib/cjs/webhooks.js.map +0 -1
  32. package/lib/esm/client.d.ts.map +0 -1
  33. package/lib/esm/client.js +0 -226
  34. package/lib/esm/client.js.map +0 -1
  35. package/lib/esm/errors.d.ts.map +0 -1
  36. package/lib/esm/errors.js +0 -50
  37. package/lib/esm/errors.js.map +0 -1
  38. package/lib/esm/index.d.ts.map +0 -1
  39. package/lib/esm/index.js.map +0 -1
  40. package/lib/esm/react-native.d.ts.map +0 -1
  41. package/lib/esm/react-native.js.map +0 -1
  42. package/lib/esm/types.d.ts.map +0 -1
  43. package/lib/esm/types.js +0 -18
  44. package/lib/esm/types.js.map +0 -1
  45. package/lib/esm/version.d.ts.map +0 -1
  46. package/lib/esm/version.js +0 -3
  47. package/lib/esm/version.js.map +0 -1
  48. package/lib/esm/web.d.ts.map +0 -1
  49. package/lib/esm/web.js.map +0 -1
  50. package/lib/esm/webhooks.d.ts.map +0 -1
  51. package/lib/esm/webhooks.js +0 -129
  52. package/lib/esm/webhooks.js.map +0 -1
  53. package/src/client.ts +0 -314
  54. package/src/errors.ts +0 -74
  55. package/src/index.ts +0 -26
  56. package/src/react-native.ts +0 -154
  57. package/src/types.ts +0 -366
  58. package/src/version.ts +0 -2
  59. package/src/web.ts +0 -182
  60. package/src/webhooks.ts +0 -169
package/src/types.ts DELETED
@@ -1,366 +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
- /** sheet: mobile sheet (default); split: desktop two-column; compact: embeds and small sheets. */
48
- export type CheckoutTemplate = 'sheet' | 'split' | 'compact'
49
-
50
- export type Checkout = {
51
- id: string
52
- object: 'checkout'
53
- status: 'open' | 'completed' | 'expired'
54
- /** The hosted checkout page. Open it in a browser, an in-app sheet, or an overlay. */
55
- url: string
56
- price_id: string
57
- product_id: string
58
- quantity: number
59
- /** Before tax. */
60
- amount_subtotal: number
61
- /** Sales tax / VAT, once the customer picks a country. */
62
- amount_tax: number
63
- amount_total: number
64
- currency: Currency
65
- mode: 'payment' | 'subscription'
66
- template: CheckoutTemplate | null
67
- customer_id: string | null
68
- customer_email: string | null
69
- success_url: string | null
70
- cancel_url: string | null
71
- transaction_id: string | null
72
- subscription_id: string | null
73
- metadata: Metadata
74
- expires_at: number
75
- created_at: number
76
- }
77
-
78
- export type Customer = {
79
- id: string
80
- object: 'customer'
81
- email: string
82
- name: string | null
83
- metadata: Metadata
84
- created_at: number
85
- }
86
-
87
- export type Processor = 'stripe' | 'paypal'
88
-
89
- export type Transaction = {
90
- id: string
91
- object: 'transaction'
92
- status: 'completed' | 'partially_refunded' | 'refunded' | 'disputed'
93
- processor: Processor
94
- /** `'card'`, `'apple_pay'`, `'paypal'`, `'venmo'`, `'link'`, `'cashapp'`, … */
95
- payment_method: string | null
96
- customer_id: string | null
97
- checkout_id: string | null
98
- subscription_id: string | null
99
- product_id: string | null
100
- price_id: string | null
101
- amount: number
102
- currency: Currency
103
- /** Munim's fee. */
104
- /** Sales tax / VAT included in `amount` (Munim remits it; not part of your earnings). */
105
- tax: number
106
- fee: number
107
- /** What the seller earns: `amount - fee`. */
108
- net: number
109
- refunded_amount: number
110
- metadata: Metadata
111
- created_at: number
112
- }
113
-
114
- export type SubscriptionStatus =
115
- | 'incomplete'
116
- | 'trialing'
117
- | 'active'
118
- | 'past_due'
119
- | 'paused'
120
- | 'canceled'
121
-
122
- export type Subscription = {
123
- id: string
124
- object: 'subscription'
125
- status: SubscriptionStatus
126
- processor: Processor
127
- customer_id: string | null
128
- product_id: string
129
- price_id: string
130
- quantity: number
131
- current_period_start: number | null
132
- current_period_end: number | null
133
- cancel_at_period_end: boolean
134
- canceled_at: number | null
135
- checkout_id: string | null
136
- metadata: Metadata
137
- created_at: number
138
- }
139
-
140
- export type Balance = {
141
- object: 'balance'
142
- currency: Currency
143
- /** Can be paid out now. */
144
- available: number
145
- /** Still in the hold. */
146
- pending: number
147
- /** Lifetime payouts. */
148
- paid_out: number
149
- }
150
-
151
- export type Payout = {
152
- id: string
153
- object: 'payout'
154
- amount: number
155
- currency: Currency
156
- method: Processor
157
- status: 'pending' | 'paid' | 'failed'
158
- failure_reason: string | null
159
- created_at: number
160
- paid_at: number | null
161
- }
162
-
163
- export type List<T> = {
164
- object: 'list'
165
- data: T[]
166
- has_more: boolean
167
- next_cursor: string | null
168
- }
169
-
170
- export type ManageUrl = {
171
- url: string
172
- expires_at: number
173
- }
174
-
175
- // ---------------------------------------------------------------------------
176
- // Events
177
-
178
- type EventOf<Type extends string, Obj> = {
179
- id: string
180
- object: 'event'
181
- type: Type
182
- created_at: number
183
- data: { object: Obj }
184
- }
185
-
186
- export type CheckoutEvent = EventOf<'checkout.completed', Checkout>
187
- export type TransactionEvent = EventOf<
188
- 'transaction.completed' | 'transaction.refunded' | 'transaction.disputed',
189
- Transaction
190
- >
191
- export type SubscriptionEvent = EventOf<
192
- 'subscription.created' | 'subscription.updated' | 'subscription.canceled',
193
- Subscription
194
- >
195
- export type PayoutEvent = EventOf<'payout.paid' | 'payout.failed', Payout>
196
-
197
- /** A webhook event. Narrow it with `event.type`. */
198
- export type Event = CheckoutEvent | TransactionEvent | SubscriptionEvent | PayoutEvent
199
-
200
- export type EventType = Event['type']
201
-
202
- export const EVENT_TYPES: readonly EventType[] = [
203
- 'checkout.completed',
204
- 'transaction.completed',
205
- 'transaction.refunded',
206
- 'transaction.disputed',
207
- 'subscription.created',
208
- 'subscription.updated',
209
- 'subscription.canceled',
210
- 'payout.paid',
211
- 'payout.failed',
212
- ]
213
-
214
- // ---------------------------------------------------------------------------
215
- // Request params
216
-
217
- export type ListParams = {
218
- /** 1–100, default 25. */
219
- limit?: number
220
- cursor?: string
221
- }
222
-
223
- export type ProductCreateParams = {
224
- name: string
225
- description?: string
226
- image_url?: string
227
- tax_code?: string
228
- metadata?: Metadata
229
- }
230
-
231
- export type ProductUpdateParams = {
232
- name?: string
233
- description?: string | null
234
- image_url?: string | null
235
- tax_code?: string | null
236
- status?: 'active' | 'archived'
237
- metadata?: Metadata
238
- }
239
-
240
- export type PriceCreateParams = {
241
- product_id: string
242
- unit_amount: number
243
- currency: Currency
244
- type: 'one_time' | 'recurring'
245
- interval?: PriceInterval
246
- interval_count?: number
247
- trial_days?: number
248
- description?: string
249
- metadata?: Metadata
250
- }
251
-
252
- export type PriceUpdateParams = {
253
- description?: string | null
254
- status?: 'active' | 'archived'
255
- metadata?: Metadata
256
- }
257
-
258
- export type PriceListParams = ListParams & { product_id?: string }
259
-
260
- export type CheckoutCreateParams = {
261
- price_id: string
262
- quantity?: number
263
- customer_email?: string
264
- customer_id?: string
265
- /** `https://` URL or an app scheme (`myapp://billing/done`). `checkout_id` is appended. */
266
- success_url?: string
267
- cancel_url?: string
268
- /** Your own user id, so this purchase counts toward that user's entitlements (`subscribers.retrieve`). */
269
- app_user_id?: string
270
- /** Checkout look for this checkout; your dashboard default otherwise. */
271
- template?: CheckoutTemplate
272
- metadata?: Metadata
273
- }
274
-
275
- export type CustomerListParams = ListParams & { email?: string }
276
-
277
- export type TransactionListParams = ListParams & {
278
- customer_id?: string
279
- subscription_id?: string
280
- }
281
-
282
- export type RefundParams = {
283
- /** Cents. Defaults to everything not yet refunded. */
284
- amount?: number
285
- }
286
-
287
- export type SubscriptionListParams = ListParams & {
288
- customer_id?: string
289
- status?: SubscriptionStatus
290
- }
291
-
292
- export type SubscriptionCancelParams = {
293
- /** Default `true`: the subscription runs to the end of the paid period. */
294
- at_period_end?: boolean
295
- }
296
-
297
- // ---------------------------------------------------------------------------
298
- // App Store / Google Play subscriptions and entitlements
299
-
300
- export type StorePlatform = 'apple' | 'google'
301
-
302
- export type Entitlement = {
303
- id: string
304
- object: 'entitlement'
305
- identifier: string
306
- description: string | null
307
- /** App Store / Play product ids and/or Munim product ids (`pro_…`). */
308
- product_ids: string[]
309
- created_at: number
310
- }
311
-
312
- export type StoreSubscription = {
313
- id: string
314
- object: 'store_subscription'
315
- platform: StorePlatform
316
- app_user_id: string | null
317
- /** The store's product id. */
318
- product_id: string
319
- status: 'active' | 'trialing' | 'in_grace_period' | 'in_billing_retry' | 'paused' | 'expired' | 'revoked'
320
- will_renew: boolean
321
- /** null = lifetime (non-consumable / one-time product). */
322
- expires_at: number | null
323
- environment: 'sandbox' | 'production'
324
- price_amount: number | null
325
- currency: string | null
326
- created_at: number
327
- updated_at: number
328
- }
329
-
330
- export type EntitlementState = {
331
- active: boolean
332
- expires_at: number | null
333
- product_id: string
334
- source: 'web' | StorePlatform
335
- will_renew: boolean
336
- }
337
-
338
- export type Subscriber = {
339
- object: 'subscriber'
340
- app_user_id: string
341
- customer_id: string | null
342
- entitlements: Record<string, EntitlementState>
343
- store_subscriptions: StoreSubscription[]
344
- subscriptions: Subscription[]
345
- }
346
-
347
- export type AppleReceiptParams = {
348
- app_user_id: string
349
- /** StoreKit 2 transaction id… */
350
- transaction_id?: string
351
- /** …or its signed JWS. */
352
- signed_transaction?: string
353
- /** Bundle id, when you have more than one Apple app. */
354
- app_identifier?: string
355
- }
356
-
357
- export type GoogleReceiptParams = {
358
- app_user_id: string
359
- product_id: string
360
- purchase_token: string
361
- type?: 'subscription' | 'one_time'
362
- /** Package name, when you have more than one Android app. */
363
- app_identifier?: string
364
- }
365
-
366
- export type StoreSubscriptionListParams = ListParams & { app_user_id?: string }
package/src/version.ts DELETED
@@ -1,2 +0,0 @@
1
- /** Sent as `Munim-Billing-Client: munim-billing-js/<VERSION>`. */
2
- export const VERSION = '0.2.0'
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
- }