1 | type Stripe = { |
2 | token: string; |
3 | }; |
4 | |
5 | * Post billing portal sessions |
6 | * Creates a session of the customer portal. |
7 | */ |
8 | export async function main( |
9 | auth: Stripe, |
10 | body: { |
11 | configuration?: string; |
12 | customer: string; |
13 | expand?: string[]; |
14 | flow_data?: { |
15 | after_completion?: { |
16 | hosted_confirmation?: { custom_message?: string; [k: string]: unknown }; |
17 | redirect?: { return_url: string; [k: string]: unknown }; |
18 | type: "hosted_confirmation" | "portal_homepage" | "redirect"; |
19 | [k: string]: unknown; |
20 | }; |
21 | subscription_cancel?: { |
22 | retention?: { |
23 | coupon_offer: { coupon: string; [k: string]: unknown }; |
24 | type: "coupon_offer"; |
25 | [k: string]: unknown; |
26 | }; |
27 | subscription: string; |
28 | [k: string]: unknown; |
29 | }; |
30 | subscription_update?: { subscription: string; [k: string]: unknown }; |
31 | subscription_update_confirm?: { |
32 | discounts?: { |
33 | coupon?: string; |
34 | promotion_code?: string; |
35 | [k: string]: unknown; |
36 | }[]; |
37 | items: { |
38 | id: string; |
39 | price?: string; |
40 | quantity?: number; |
41 | [k: string]: unknown; |
42 | }[]; |
43 | subscription: string; |
44 | [k: string]: unknown; |
45 | }; |
46 | type: |
47 | | "payment_method_update" |
48 | | "subscription_cancel" |
49 | | "subscription_update" |
50 | | "subscription_update_confirm"; |
51 | [k: string]: unknown; |
52 | }; |
53 | locale?: |
54 | | "auto" |
55 | | "bg" |
56 | | "cs" |
57 | | "da" |
58 | | "de" |
59 | | "el" |
60 | | "en" |
61 | | "en-AU" |
62 | | "en-CA" |
63 | | "en-GB" |
64 | | "en-IE" |
65 | | "en-IN" |
66 | | "en-NZ" |
67 | | "en-SG" |
68 | | "es" |
69 | | "es-419" |
70 | | "et" |
71 | | "fi" |
72 | | "fil" |
73 | | "fr" |
74 | | "fr-CA" |
75 | | "hr" |
76 | | "hu" |
77 | | "id" |
78 | | "it" |
79 | | "ja" |
80 | | "ko" |
81 | | "lt" |
82 | | "lv" |
83 | | "ms" |
84 | | "mt" |
85 | | "nb" |
86 | | "nl" |
87 | | "pl" |
88 | | "pt" |
89 | | "pt-BR" |
90 | | "ro" |
91 | | "ru" |
92 | | "sk" |
93 | | "sl" |
94 | | "sv" |
95 | | "th" |
96 | | "tr" |
97 | | "vi" |
98 | | "zh" |
99 | | "zh-HK" |
100 | | "zh-TW"; |
101 | on_behalf_of?: string; |
102 | return_url?: string; |
103 | } |
104 | ) { |
105 | const url = new URL(`https://api.stripe.com/v1/billing_portal/sessions`); |
106 |
|
107 | const response = await fetch(url, { |
108 | method: "POST", |
109 | headers: { |
110 | "Content-Type": "application/x-www-form-urlencoded", |
111 | Authorization: "Bearer " + auth.token, |
112 | }, |
113 | body: encodeParams(body), |
114 | }); |
115 | if (!response.ok) { |
116 | const text = await response.text(); |
117 | throw new Error(`${response.status} ${text}`); |
118 | } |
119 | return await response.json(); |
120 | } |
121 |
|
122 | function encodeParams(o: any) { |
123 | function iter(o: any, path: string) { |
124 | if (Array.isArray(o)) { |
125 | o.forEach(function (a) { |
126 | iter(a, path + "[]"); |
127 | }); |
128 | return; |
129 | } |
130 | if (o !== null && typeof o === "object") { |
131 | Object.keys(o).forEach(function (k) { |
132 | iter(o[k], path + "[" + k + "]"); |
133 | }); |
134 | return; |
135 | } |
136 | data.push(path + "=" + o); |
137 | } |
138 | const data: string[] = []; |
139 | Object.keys(o).forEach(function (k) { |
140 | if (o[k] !== undefined) { |
141 | iter(o[k], k); |
142 | } |
143 | }); |
144 | return new URLSearchParams(data.join("&")); |
145 | } |
146 |
|