1 | |
2 | type Zoho = { |
3 | token: string; |
4 | }; |
5 | |
6 | * Create a subscription |
7 | * Create 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 | add_to_unbilled_charges?: false | true; |
14 | customer: { |
15 | display_name: string; |
16 | salutation?: string; |
17 | first_name?: string; |
18 | last_name?: string; |
19 | email: string; |
20 | company_name?: string; |
21 | billing_address?: { |
22 | attention?: string; |
23 | street?: string; |
24 | city?: string; |
25 | state?: string; |
26 | country?: string; |
27 | zip?: string; |
28 | fax?: string; |
29 | }; |
30 | shipping_address?: { |
31 | attention?: string; |
32 | street?: string; |
33 | city?: string; |
34 | state?: string; |
35 | zip?: string; |
36 | country?: string; |
37 | fax?: string; |
38 | }; |
39 | payment_terms?: number; |
40 | payment_terms_label?: string; |
41 | custom_fields?: { label: string; value: string }[]; |
42 | place_of_contact?: string; |
43 | gst_no?: string; |
44 | gst_treatment?: 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 | }; |
55 | customer_id: {}; |
56 | payment_terms?: number; |
57 | payment_terms_label?: string; |
58 | custom_fields?: { label?: string; value?: string }[]; |
59 | contactpersons?: { contactperson_id: string }[]; |
60 | card_id: {}; |
61 | starts_at?: string; |
62 | exchange_rate?: never; |
63 | place_of_supply?: string; |
64 | plan: { |
65 | plan_code: string; |
66 | plan_description?: string; |
67 | price?: number; |
68 | setup_fee?: never; |
69 | setup_fee_tax_id?: string; |
70 | tags?: { tag_id?: number; tag_option_id?: number }[]; |
71 | item_custom_fields?: { label?: string; value?: string }[]; |
72 | quantity?: number; |
73 | tax_id: {}; |
74 | tax_exemption_id?: string; |
75 | tax_exemption_code?: string; |
76 | tds_tax_id?: string; |
77 | sat_item_key_code?: string; |
78 | unitkey_code?: string; |
79 | setup_fee_tax_exemption_id?: string; |
80 | setup_fee_tax_exemption_code?: string; |
81 | exclude_trial?: false | true; |
82 | exclude_setup_fee?: false | true; |
83 | billing_cycles?: number; |
84 | trial_days?: never; |
85 | }; |
86 | addons?: { |
87 | addon_code: string; |
88 | addon_description?: string; |
89 | price?: number; |
90 | quantity?: {}; |
91 | tags?: { tag_id?: number; tag_option_id?: number }[]; |
92 | item_custom_fields?: { label?: string; value?: string }[]; |
93 | tax_id: {}; |
94 | tax_exemption_id?: string; |
95 | tax_exemption_code?: string; |
96 | tds_tax_id?: string; |
97 | sat_item_key_code?: string; |
98 | unitkey_code?: string; |
99 | }[]; |
100 | coupon_code?: string; |
101 | auto_collect?: false | true; |
102 | reference_id?: string; |
103 | salesperson_name?: string; |
104 | payment_gateways?: { payment_gateway?: {} }[]; |
105 | create_backdated_invoice?: false | true; |
106 | can_charge_setup_fee_immediately?: false | true; |
107 | template_id?: string; |
108 | cfdi_usage?: string; |
109 | allow_partial_payments?: false | true; |
110 | account_id: string; |
111 | }, |
112 | ) { |
113 | const url = new URL(`https://www.zohoapis.com/billing/v1/subscriptions`); |
114 |
|
115 | const response = await fetch(url, { |
116 | method: "POST", |
117 | headers: { |
118 | "X-com-zoho-subscriptions-organizationid": |
119 | X_com_zoho_subscriptions_organizationid, |
120 | "Content-Type": "application/json", |
121 | Authorization: "Zoho-oauthtoken " + auth.token, |
122 | }, |
123 | body: JSON.stringify(body), |
124 | }); |
125 | if (!response.ok) { |
126 | const text = await response.text(); |
127 | throw new Error(`${response.status} ${text}`); |
128 | } |
129 | return await response.json(); |
130 | } |
131 |
|