0

UpdateSubscription

by
Published Oct 17, 2025

Updates a subscription by modifying or clearing `subscription` field values. To clear a field, set its value to `null`.

Script square Verified

The script

Submitted by hugo697 Bun
Verified 235 days ago
1
//native
2
type Square = {
3
  token: string;
4
};
5
/**
6
 * UpdateSubscription
7
 * Updates a subscription by modifying or clearing `subscription` field values.
8
To clear a field, set its value to `null`.
9
 */
10
export async function main(
11
  auth: Square,
12
  subscription_id: string,
13
  body: {
14
    subscription?: {
15
      id?: string;
16
      location_id?: string;
17
      plan_variation_id?: string;
18
      customer_id?: string;
19
      start_date?: string;
20
      canceled_date?: string;
21
      charged_through_date?: string;
22
      status?: "PENDING" | "ACTIVE" | "CANCELED" | "DEACTIVATED" | "PAUSED";
23
      tax_percentage?: string;
24
      invoice_ids?: string[];
25
      price_override_money?: {
26
        amount?: number;
27
        currency?:
28
          | "UNKNOWN_CURRENCY"
29
          | "AED"
30
          | "AFN"
31
          | "ALL"
32
          | "AMD"
33
          | "ANG"
34
          | "AOA"
35
          | "ARS"
36
          | "AUD"
37
          | "AWG"
38
          | "AZN"
39
          | "BAM"
40
          | "BBD"
41
          | "BDT"
42
          | "BGN"
43
          | "BHD"
44
          | "BIF"
45
          | "BMD"
46
          | "BND"
47
          | "BOB"
48
          | "BOV"
49
          | "BRL"
50
          | "BSD"
51
          | "BTN"
52
          | "BWP"
53
          | "BYR"
54
          | "BZD"
55
          | "CAD"
56
          | "CDF"
57
          | "CHE"
58
          | "CHF"
59
          | "CHW"
60
          | "CLF"
61
          | "CLP"
62
          | "CNY"
63
          | "COP"
64
          | "COU"
65
          | "CRC"
66
          | "CUC"
67
          | "CUP"
68
          | "CVE"
69
          | "CZK"
70
          | "DJF"
71
          | "DKK"
72
          | "DOP"
73
          | "DZD"
74
          | "EGP"
75
          | "ERN"
76
          | "ETB"
77
          | "EUR"
78
          | "FJD"
79
          | "FKP"
80
          | "GBP"
81
          | "GEL"
82
          | "GHS"
83
          | "GIP"
84
          | "GMD"
85
          | "GNF"
86
          | "GTQ"
87
          | "GYD"
88
          | "HKD"
89
          | "HNL"
90
          | "HRK"
91
          | "HTG"
92
          | "HUF"
93
          | "IDR"
94
          | "ILS"
95
          | "INR"
96
          | "IQD"
97
          | "IRR"
98
          | "ISK"
99
          | "JMD"
100
          | "JOD"
101
          | "JPY"
102
          | "KES"
103
          | "KGS"
104
          | "KHR"
105
          | "KMF"
106
          | "KPW"
107
          | "KRW"
108
          | "KWD"
109
          | "KYD"
110
          | "KZT"
111
          | "LAK"
112
          | "LBP"
113
          | "LKR"
114
          | "LRD"
115
          | "LSL"
116
          | "LTL"
117
          | "LVL"
118
          | "LYD"
119
          | "MAD"
120
          | "MDL"
121
          | "MGA"
122
          | "MKD"
123
          | "MMK"
124
          | "MNT"
125
          | "MOP"
126
          | "MRO"
127
          | "MUR"
128
          | "MVR"
129
          | "MWK"
130
          | "MXN"
131
          | "MXV"
132
          | "MYR"
133
          | "MZN"
134
          | "NAD"
135
          | "NGN"
136
          | "NIO"
137
          | "NOK"
138
          | "NPR"
139
          | "NZD"
140
          | "OMR"
141
          | "PAB"
142
          | "PEN"
143
          | "PGK"
144
          | "PHP"
145
          | "PKR"
146
          | "PLN"
147
          | "PYG"
148
          | "QAR"
149
          | "RON"
150
          | "RSD"
151
          | "RUB"
152
          | "RWF"
153
          | "SAR"
154
          | "SBD"
155
          | "SCR"
156
          | "SDG"
157
          | "SEK"
158
          | "SGD"
159
          | "SHP"
160
          | "SLL"
161
          | "SLE"
162
          | "SOS"
163
          | "SRD"
164
          | "SSP"
165
          | "STD"
166
          | "SVC"
167
          | "SYP"
168
          | "SZL"
169
          | "THB"
170
          | "TJS"
171
          | "TMT"
172
          | "TND"
173
          | "TOP"
174
          | "TRY"
175
          | "TTD"
176
          | "TWD"
177
          | "TZS"
178
          | "UAH"
179
          | "UGX"
180
          | "USD"
181
          | "USN"
182
          | "USS"
183
          | "UYI"
184
          | "UYU"
185
          | "UZS"
186
          | "VEF"
187
          | "VND"
188
          | "VUV"
189
          | "WST"
190
          | "XAF"
191
          | "XAG"
192
          | "XAU"
193
          | "XBA"
194
          | "XBB"
195
          | "XBC"
196
          | "XBD"
197
          | "XCD"
198
          | "XDR"
199
          | "XOF"
200
          | "XPD"
201
          | "XPF"
202
          | "XPT"
203
          | "XTS"
204
          | "XXX"
205
          | "YER"
206
          | "ZAR"
207
          | "ZMK"
208
          | "ZMW"
209
          | "BTC"
210
          | "XUS";
211
      };
212
      version?: number;
213
      created_at?: string;
214
      card_id?: string;
215
      timezone?: string;
216
      source?: { name?: string };
217
      actions?: {
218
        id?: string;
219
        type?:
220
          | "CANCEL"
221
          | "PAUSE"
222
          | "RESUME"
223
          | "SWAP_PLAN"
224
          | "CHANGE_BILLING_ANCHOR_DATE";
225
        effective_date?: string;
226
        monthly_billing_anchor_date?: number;
227
        phases?: {
228
          uid?: string;
229
          ordinal?: number;
230
          order_template_id?: string;
231
          plan_phase_uid?: string;
232
        }[];
233
        new_plan_variation_id?: string;
234
      }[];
235
      monthly_billing_anchor_date?: number;
236
      phases?: {
237
        uid?: string;
238
        ordinal?: number;
239
        order_template_id?: string;
240
        plan_phase_uid?: string;
241
      }[];
242
    };
243
  },
244
) {
245
  const url = new URL(
246
    `https://connect.squareup.com/v2/subscriptions/${subscription_id}`,
247
  );
248

249
  const response = await fetch(url, {
250
    method: "PUT",
251
    headers: {
252
      "Content-Type": "application/json",
253
      Authorization: "Bearer " + auth.token,
254
    },
255
    body: JSON.stringify(body),
256
  });
257
  if (!response.ok) {
258
    const text = await response.text();
259
    throw new Error(`${response.status} ${text}`);
260
  }
261
  return await response.json();
262
}
263