1 | |
2 | type Zoho = { |
3 | token: string; |
4 | }; |
5 | |
6 | * Create an coupon |
7 | * Create a new coupon. |
8 | */ |
9 | export async function main( |
10 | auth: Zoho, |
11 | X_com_zoho_subscriptions_organizationid: string, |
12 | body: { |
13 | coupon_code: string; |
14 | name: string; |
15 | description?: string; |
16 | type: string; |
17 | duration: number; |
18 | discount_by: string; |
19 | discount_value?: number; |
20 | product_id: string; |
21 | max_redemption?: number; |
22 | expiry_at?: string; |
23 | apply_to_plans?: string; |
24 | plans: { plan_code: string }[]; |
25 | apply_to_addons?: string; |
26 | addons: { addon_code: string }[]; |
27 | }, |
28 | ) { |
29 | const url = new URL(`https://www.zohoapis.com/billing/v1/coupons`); |
30 |
|
31 | const response = await fetch(url, { |
32 | method: "POST", |
33 | headers: { |
34 | "X-com-zoho-subscriptions-organizationid": |
35 | X_com_zoho_subscriptions_organizationid, |
36 | "Content-Type": "application/json", |
37 | Authorization: "Zoho-oauthtoken " + auth.token, |
38 | }, |
39 | body: JSON.stringify(body), |
40 | }); |
41 | if (!response.ok) { |
42 | const text = await response.text(); |
43 | throw new Error(`${response.status} ${text}`); |
44 | } |
45 | return await response.json(); |
46 | } |
47 |
|