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