1 | |
2 | type Zoho = { |
3 | token: string; |
4 | }; |
5 | |
6 | * Create a subscription |
7 | * Create a hosted page for a new subscription. To create a subscription for a new customer, you have to pass the customer object. To create a subscription for a existing customer pass the customer_id of that customer. |
8 | */ |
9 | export async function main( |
10 | auth: Zoho, |
11 | X_com_zoho_subscriptions_organizationid: string, |
12 | body: { |
13 | customer_id: {}; |
14 | pricebook_id?: string; |
15 | customer: { |
16 | display_name: string; |
17 | salutation?: string; |
18 | first_name?: string; |
19 | last_name?: string; |
20 | email: string; |
21 | company_name?: string; |
22 | billing_address?: { |
23 | attention?: string; |
24 | street?: string; |
25 | city?: string; |
26 | state?: string; |
27 | country?: string; |
28 | zip?: string; |
29 | fax?: string; |
30 | }; |
31 | shipping_address?: { |
32 | attention?: string; |
33 | street?: string; |
34 | city?: string; |
35 | state?: string; |
36 | country?: string; |
37 | zip?: string; |
38 | fax?: string; |
39 | }; |
40 | pricebook_id?: string; |
41 | payment_terms?: number; |
42 | payment_terms_label?: string; |
43 | exchange_rate?: never; |
44 | currency_code?: string; |
45 | vat_treatment?: string; |
46 | vat_reg_no?: string; |
47 | country_code?: string; |
48 | is_taxable?: string; |
49 | tax_id?: string; |
50 | tax_authority_id: string; |
51 | tax_authority_name: string; |
52 | tax_exemption_id?: string; |
53 | tax_exemption_code?: string; |
54 | default_templates?: { |
55 | invoice_template_id?: string; |
56 | creditnote_template_id?: string; |
57 | }; |
58 | place_of_contact?: string; |
59 | gst_no?: string; |
60 | gst_treatment?: string; |
61 | }; |
62 | contactpersons?: { contactperson_id: string }[]; |
63 | plan: { |
64 | plan_code: string; |
65 | plan_description?: string; |
66 | price?: number; |
67 | setup_fee?: never; |
68 | setup_fee_tax_id?: string; |
69 | quantity?: number; |
70 | tags?: { tag_id?: number; tag_option_id?: number }[]; |
71 | item_custom_fields?: { label?: string; value?: string }[]; |
72 | tax_id: {}; |
73 | tax_exemption_id?: string; |
74 | tax_exemption_code?: string; |
75 | setup_fee_tax_exemption_id?: string; |
76 | setup_fee_tax_exemption_code?: string; |
77 | exclude_trial?: false | true; |
78 | exclude_setup_fee?: false | true; |
79 | billing_cycles?: number; |
80 | trial_days?: never; |
81 | }; |
82 | addons?: { |
83 | addon_code: string; |
84 | addon_description?: string; |
85 | price?: number; |
86 | quantity?: {}; |
87 | tags?: { tag_id?: number; tag_option_id?: number }[]; |
88 | item_custom_fields?: { label?: string; value?: string }[]; |
89 | tax_id?: {}; |
90 | tax_exemption_id?: string; |
91 | tax_exemption_code?: string; |
92 | }[]; |
93 | reference_id?: string; |
94 | starts_at?: string; |
95 | custom_fields?: { label?: string; value?: string }[]; |
96 | coupon_code?: string; |
97 | redirect_url?: string; |
98 | salesperson_name?: string; |
99 | template_id?: number; |
100 | can_charge_setup_fee_immediately?: false | true; |
101 | exchange_rate?: never; |
102 | place_of_supply?: string; |
103 | gst_treatment?: string; |
104 | gst_no?: string; |
105 | cfdi_usage?: string; |
106 | payment_gateways?: { payment_gateway?: string }[]; |
107 | billing_address_id?: string; |
108 | shipping_address_id?: string; |
109 | branch_id?: string; |
110 | }, |
111 | ) { |
112 | const url = new URL( |
113 | `https://www.zohoapis.com/billing/v1/hostedpages/newsubscription`, |
114 | ); |
115 |
|
116 | const response = await fetch(url, { |
117 | method: "POST", |
118 | headers: { |
119 | "X-com-zoho-subscriptions-organizationid": |
120 | X_com_zoho_subscriptions_organizationid, |
121 | "Content-Type": "application/json", |
122 | Authorization: "Zoho-oauthtoken " + auth.token, |
123 | }, |
124 | body: JSON.stringify(body), |
125 | }); |
126 | if (!response.ok) { |
127 | const text = await response.text(); |
128 | throw new Error(`${response.status} ${text}`); |
129 | } |
130 | return await response.json(); |
131 | } |
132 |
|