1 | type Stripe = { |
2 | token: string; |
3 | }; |
4 | |
5 | * Post subscription schedules schedule |
6 | * Updates an existing subscription schedule. |
7 | */ |
8 | export async function main( |
9 | auth: Stripe, |
10 | schedule: string, |
11 | body: { |
12 | default_settings?: { |
13 | application_fee_percent?: number; |
14 | automatic_tax?: { |
15 | enabled: boolean; |
16 | liability?: { |
17 | account?: string; |
18 | type: "account" | "self"; |
19 | [k: string]: unknown; |
20 | }; |
21 | [k: string]: unknown; |
22 | }; |
23 | billing_cycle_anchor?: "automatic" | "phase_start"; |
24 | billing_thresholds?: |
25 | | { |
26 | amount_gte?: number; |
27 | reset_billing_cycle_anchor?: boolean; |
28 | [k: string]: unknown; |
29 | } |
30 | | ""; |
31 | collection_method?: "charge_automatically" | "send_invoice"; |
32 | default_payment_method?: string; |
33 | description?: string | ""; |
34 | invoice_settings?: { |
35 | account_tax_ids?: string[] | ""; |
36 | days_until_due?: number; |
37 | issuer?: { |
38 | account?: string; |
39 | type: "account" | "self"; |
40 | [k: string]: unknown; |
41 | }; |
42 | [k: string]: unknown; |
43 | }; |
44 | on_behalf_of?: string | ""; |
45 | transfer_data?: |
46 | | { amount_percent?: number; destination: string; [k: string]: unknown } |
47 | | ""; |
48 | [k: string]: unknown; |
49 | }; |
50 | end_behavior?: "cancel" | "none" | "release" | "renew"; |
51 | expand?: string[]; |
52 | metadata?: { [k: string]: string } | ""; |
53 | phases?: { |
54 | add_invoice_items?: { |
55 | price?: string; |
56 | price_data?: { |
57 | currency: string; |
58 | product: string; |
59 | tax_behavior?: "exclusive" | "inclusive" | "unspecified"; |
60 | unit_amount?: number; |
61 | unit_amount_decimal?: string; |
62 | [k: string]: unknown; |
63 | }; |
64 | quantity?: number; |
65 | tax_rates?: string[] | ""; |
66 | [k: string]: unknown; |
67 | }[]; |
68 | application_fee_percent?: number; |
69 | automatic_tax?: { |
70 | enabled: boolean; |
71 | liability?: { |
72 | account?: string; |
73 | type: "account" | "self"; |
74 | [k: string]: unknown; |
75 | }; |
76 | [k: string]: unknown; |
77 | }; |
78 | billing_cycle_anchor?: "automatic" | "phase_start"; |
79 | billing_thresholds?: |
80 | | { |
81 | amount_gte?: number; |
82 | reset_billing_cycle_anchor?: boolean; |
83 | [k: string]: unknown; |
84 | } |
85 | | ""; |
86 | collection_method?: "charge_automatically" | "send_invoice"; |
87 | coupon?: string; |
88 | default_payment_method?: string; |
89 | default_tax_rates?: string[] | ""; |
90 | description?: string | ""; |
91 | end_date?: number | "now"; |
92 | invoice_settings?: { |
93 | account_tax_ids?: string[] | ""; |
94 | days_until_due?: number; |
95 | issuer?: { |
96 | account?: string; |
97 | type: "account" | "self"; |
98 | [k: string]: unknown; |
99 | }; |
100 | [k: string]: unknown; |
101 | }; |
102 | items: { |
103 | billing_thresholds?: { usage_gte: number; [k: string]: unknown } | ""; |
104 | metadata?: { [k: string]: string }; |
105 | price?: string; |
106 | price_data?: { |
107 | currency: string; |
108 | product: string; |
109 | recurring: { |
110 | interval: "day" | "month" | "week" | "year"; |
111 | interval_count?: number; |
112 | [k: string]: unknown; |
113 | }; |
114 | tax_behavior?: "exclusive" | "inclusive" | "unspecified"; |
115 | unit_amount?: number; |
116 | unit_amount_decimal?: string; |
117 | [k: string]: unknown; |
118 | }; |
119 | quantity?: number; |
120 | tax_rates?: string[] | ""; |
121 | [k: string]: unknown; |
122 | }[]; |
123 | iterations?: number; |
124 | metadata?: { [k: string]: string }; |
125 | on_behalf_of?: string; |
126 | proration_behavior?: "always_invoice" | "create_prorations" | "none"; |
127 | start_date?: number | "now"; |
128 | transfer_data?: { |
129 | amount_percent?: number; |
130 | destination: string; |
131 | [k: string]: unknown; |
132 | }; |
133 | trial?: boolean; |
134 | trial_end?: number | "now"; |
135 | [k: string]: unknown; |
136 | }[]; |
137 | proration_behavior?: "always_invoice" | "create_prorations" | "none"; |
138 | } |
139 | ) { |
140 | const url = new URL( |
141 | `https://api.stripe.com/v1/subscription_schedules/${schedule}` |
142 | ); |
143 |
|
144 | const response = await fetch(url, { |
145 | method: "POST", |
146 | headers: { |
147 | "Content-Type": "application/x-www-form-urlencoded", |
148 | Authorization: "Bearer " + auth.token, |
149 | }, |
150 | body: encodeParams(body), |
151 | }); |
152 | if (!response.ok) { |
153 | const text = await response.text(); |
154 | throw new Error(`${response.status} ${text}`); |
155 | } |
156 | return await response.json(); |
157 | } |
158 |
|
159 | function encodeParams(o: any) { |
160 | function iter(o: any, path: string) { |
161 | if (Array.isArray(o)) { |
162 | o.forEach(function (a) { |
163 | iter(a, path + "[]"); |
164 | }); |
165 | return; |
166 | } |
167 | if (o !== null && typeof o === "object") { |
168 | Object.keys(o).forEach(function (k) { |
169 | iter(o[k], path + "[" + k + "]"); |
170 | }); |
171 | return; |
172 | } |
173 | data.push(path + "=" + o); |
174 | } |
175 | const data: string[] = []; |
176 | Object.keys(o).forEach(function (k) { |
177 | if (o[k] !== undefined) { |
178 | iter(o[k], k); |
179 | } |
180 | }); |
181 | return new URLSearchParams(data.join("&")); |
182 | } |
183 |
|