1 | type Stripe = { |
2 | token: string; |
3 | }; |
4 | |
5 | * Post quotes quote |
6 | * A quote models prices and services for a customer. |
7 | */ |
8 | export async function main( |
9 | auth: Stripe, |
10 | quote: string, |
11 | body: { |
12 | application_fee_amount?: number | ""; |
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 | collection_method?: "charge_automatically" | "send_invoice"; |
24 | customer?: string; |
25 | default_tax_rates?: string[] | ""; |
26 | description?: string | ""; |
27 | discounts?: |
28 | | { coupon?: string; discount?: string; [k: string]: unknown }[] |
29 | | ""; |
30 | expand?: string[]; |
31 | expires_at?: number; |
32 | footer?: string | ""; |
33 | header?: string | ""; |
34 | invoice_settings?: { |
35 | days_until_due?: number; |
36 | issuer?: { |
37 | account?: string; |
38 | type: "account" | "self"; |
39 | [k: string]: unknown; |
40 | }; |
41 | [k: string]: unknown; |
42 | }; |
43 | line_items?: { |
44 | id?: string; |
45 | price?: string; |
46 | price_data?: { |
47 | currency: string; |
48 | product: string; |
49 | recurring?: { |
50 | interval: "day" | "month" | "week" | "year"; |
51 | interval_count?: number; |
52 | [k: string]: unknown; |
53 | }; |
54 | tax_behavior?: "exclusive" | "inclusive" | "unspecified"; |
55 | unit_amount?: number; |
56 | unit_amount_decimal?: string; |
57 | [k: string]: unknown; |
58 | }; |
59 | quantity?: number; |
60 | tax_rates?: string[] | ""; |
61 | [k: string]: unknown; |
62 | }[]; |
63 | metadata?: { [k: string]: string }; |
64 | on_behalf_of?: string | ""; |
65 | subscription_data?: { |
66 | description?: string | ""; |
67 | effective_date?: "current_period_end" | number | ""; |
68 | metadata?: { [k: string]: string }; |
69 | trial_period_days?: number | ""; |
70 | [k: string]: unknown; |
71 | }; |
72 | transfer_data?: |
73 | | { |
74 | amount?: number; |
75 | amount_percent?: number; |
76 | destination: string; |
77 | [k: string]: unknown; |
78 | } |
79 | | ""; |
80 | } |
81 | ) { |
82 | const url = new URL(`https://api.stripe.com/v1/quotes/${quote}`); |
83 |
|
84 | const response = await fetch(url, { |
85 | method: "POST", |
86 | headers: { |
87 | "Content-Type": "application/x-www-form-urlencoded", |
88 | Authorization: "Bearer " + auth.token, |
89 | }, |
90 | body: encodeParams(body), |
91 | }); |
92 | if (!response.ok) { |
93 | const text = await response.text(); |
94 | throw new Error(`${response.status} ${text}`); |
95 | } |
96 | return await response.json(); |
97 | } |
98 |
|
99 | function encodeParams(o: any) { |
100 | function iter(o: any, path: string) { |
101 | if (Array.isArray(o)) { |
102 | o.forEach(function (a) { |
103 | iter(a, path + "[]"); |
104 | }); |
105 | return; |
106 | } |
107 | if (o !== null && typeof o === "object") { |
108 | Object.keys(o).forEach(function (k) { |
109 | iter(o[k], path + "[" + k + "]"); |
110 | }); |
111 | return; |
112 | } |
113 | data.push(path + "=" + o); |
114 | } |
115 | const data: string[] = []; |
116 | Object.keys(o).forEach(function (k) { |
117 | if (o[k] !== undefined) { |
118 | iter(o[k], k); |
119 | } |
120 | }); |
121 | return new URLSearchParams(data.join("&")); |
122 | } |
123 |
|