Post subscriptions subscription exposed id

Updates an existing subscription to match the specified parameters.

Script stripe Verified

by hugo697 ยท 10/30/2023

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 368 days ago
1
type Stripe = {
2
  token: string;
3
};
4
/**
5
 * Post subscriptions subscription exposed id
6
 * Updates an existing subscription to match the specified parameters.
7
 */
8
export async function main(
9
  auth: Stripe,
10
  subscription_exposed_id: string,
11
  body: {
12
    add_invoice_items?: {
13
      price?: string;
14
      price_data?: {
15
        currency: string;
16
        product: string;
17
        tax_behavior?: "exclusive" | "inclusive" | "unspecified";
18
        unit_amount?: number;
19
        unit_amount_decimal?: string;
20
        [k: string]: unknown;
21
      };
22
      quantity?: number;
23
      tax_rates?: string[] | "";
24
      [k: string]: unknown;
25
    }[];
26
    application_fee_percent?: number;
27
    automatic_tax?: {
28
      enabled: boolean;
29
      liability?: {
30
        account?: string;
31
        type: "account" | "self";
32
        [k: string]: unknown;
33
      };
34
      [k: string]: unknown;
35
    };
36
    billing_cycle_anchor?: "now" | "unchanged";
37
    billing_thresholds?:
38
      | {
39
          amount_gte?: number;
40
          reset_billing_cycle_anchor?: boolean;
41
          [k: string]: unknown;
42
        }
43
      | "";
44
    cancel_at?: number | "";
45
    cancel_at_period_end?: boolean;
46
    cancellation_details?: {
47
      comment?: string | "";
48
      feedback?:
49
        | ""
50
        | "customer_service"
51
        | "low_quality"
52
        | "missing_features"
53
        | "other"
54
        | "switched_service"
55
        | "too_complex"
56
        | "too_expensive"
57
        | "unused";
58
      [k: string]: unknown;
59
    };
60
    collection_method?: "charge_automatically" | "send_invoice";
61
    coupon?: string;
62
    days_until_due?: number;
63
    default_payment_method?: string;
64
    default_source?: string | "";
65
    default_tax_rates?: string[] | "";
66
    description?: string | "";
67
    expand?: string[];
68
    invoice_settings?: {
69
      account_tax_ids?: string[] | "";
70
      issuer?: {
71
        account?: string;
72
        type: "account" | "self";
73
        [k: string]: unknown;
74
      };
75
      [k: string]: unknown;
76
    };
77
    items?: {
78
      billing_thresholds?: { usage_gte: number; [k: string]: unknown } | "";
79
      clear_usage?: boolean;
80
      deleted?: boolean;
81
      id?: string;
82
      metadata?: { [k: string]: string } | "";
83
      price?: string;
84
      price_data?: {
85
        currency: string;
86
        product: string;
87
        recurring: {
88
          interval: "day" | "month" | "week" | "year";
89
          interval_count?: number;
90
          [k: string]: unknown;
91
        };
92
        tax_behavior?: "exclusive" | "inclusive" | "unspecified";
93
        unit_amount?: number;
94
        unit_amount_decimal?: string;
95
        [k: string]: unknown;
96
      };
97
      quantity?: number;
98
      tax_rates?: string[] | "";
99
      [k: string]: unknown;
100
    }[];
101
    metadata?: { [k: string]: string } | "";
102
    off_session?: boolean;
103
    on_behalf_of?: string | "";
104
    pause_collection?:
105
      | {
106
          behavior: "keep_as_draft" | "mark_uncollectible" | "void";
107
          resumes_at?: number;
108
          [k: string]: unknown;
109
        }
110
      | "";
111
    payment_behavior?:
112
      | "allow_incomplete"
113
      | "default_incomplete"
114
      | "error_if_incomplete"
115
      | "pending_if_incomplete";
116
    payment_settings?: {
117
      payment_method_options?: {
118
        acss_debit?:
119
          | {
120
              mandate_options?: {
121
                transaction_type?: "business" | "personal";
122
                [k: string]: unknown;
123
              };
124
              verification_method?: "automatic" | "instant" | "microdeposits";
125
              [k: string]: unknown;
126
            }
127
          | "";
128
        bancontact?:
129
          | {
130
              preferred_language?: "de" | "en" | "fr" | "nl";
131
              [k: string]: unknown;
132
            }
133
          | "";
134
        card?:
135
          | {
136
              mandate_options?: {
137
                amount?: number;
138
                amount_type?: "fixed" | "maximum";
139
                description?: string;
140
                [k: string]: unknown;
141
              };
142
              network?:
143
                | "amex"
144
                | "cartes_bancaires"
145
                | "diners"
146
                | "discover"
147
                | "eftpos_au"
148
                | "interac"
149
                | "jcb"
150
                | "mastercard"
151
                | "unionpay"
152
                | "unknown"
153
                | "visa";
154
              request_three_d_secure?: "any" | "automatic" | "challenge";
155
              [k: string]: unknown;
156
            }
157
          | "";
158
        customer_balance?:
159
          | {
160
              bank_transfer?: {
161
                eu_bank_transfer?: { country: string; [k: string]: unknown };
162
                type?: string;
163
                [k: string]: unknown;
164
              };
165
              funding_type?: string;
166
              [k: string]: unknown;
167
            }
168
          | "";
169
        konbini?: { [k: string]: unknown } | "";
170
        us_bank_account?:
171
          | {
172
              financial_connections?: {
173
                permissions?: (
174
                  | "balances"
175
                  | "ownership"
176
                  | "payment_method"
177
                  | "transactions"
178
                )[];
179
                prefetch?: ("balances" | "transactions")[];
180
                [k: string]: unknown;
181
              };
182
              verification_method?: "automatic" | "instant" | "microdeposits";
183
              [k: string]: unknown;
184
            }
185
          | "";
186
        [k: string]: unknown;
187
      };
188
      payment_method_types?:
189
        | (
190
            | "ach_credit_transfer"
191
            | "ach_debit"
192
            | "acss_debit"
193
            | "au_becs_debit"
194
            | "bacs_debit"
195
            | "bancontact"
196
            | "boleto"
197
            | "card"
198
            | "cashapp"
199
            | "customer_balance"
200
            | "eps"
201
            | "fpx"
202
            | "giropay"
203
            | "grabpay"
204
            | "ideal"
205
            | "konbini"
206
            | "link"
207
            | "p24"
208
            | "paynow"
209
            | "paypal"
210
            | "promptpay"
211
            | "sepa_debit"
212
            | "sofort"
213
            | "us_bank_account"
214
            | "wechat_pay"
215
          )[]
216
        | "";
217
      save_default_payment_method?: "off" | "on_subscription";
218
      [k: string]: unknown;
219
    };
220
    pending_invoice_item_interval?:
221
      | {
222
          interval: "day" | "month" | "week" | "year";
223
          interval_count?: number;
224
          [k: string]: unknown;
225
        }
226
      | "";
227
    promotion_code?: string;
228
    proration_behavior?: "always_invoice" | "create_prorations" | "none";
229
    proration_date?: number;
230
    transfer_data?:
231
      | { amount_percent?: number; destination: string; [k: string]: unknown }
232
      | "";
233
    trial_end?: "now" | number;
234
    trial_from_plan?: boolean;
235
    trial_settings?: {
236
      end_behavior: {
237
        missing_payment_method: "cancel" | "create_invoice" | "pause";
238
        [k: string]: unknown;
239
      };
240
      [k: string]: unknown;
241
    };
242
  }
243
) {
244
  const url = new URL(
245
    `https://api.stripe.com/v1/subscriptions/${subscription_exposed_id}`
246
  );
247

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

263
function encodeParams(o: any) {
264
  function iter(o: any, path: string) {
265
    if (Array.isArray(o)) {
266
      o.forEach(function (a) {
267
        iter(a, path + "[]");
268
      });
269
      return;
270
    }
271
    if (o !== null && typeof o === "object") {
272
      Object.keys(o).forEach(function (k) {
273
        iter(o[k], path + "[" + k + "]");
274
      });
275
      return;
276
    }
277
    data.push(path + "=" + o);
278
  }
279
  const data: string[] = [];
280
  Object.keys(o).forEach(function (k) {
281
    if (o[k] !== undefined) {
282
      iter(o[k], k);
283
    }
284
  });
285
  return new URLSearchParams(data.join("&"));
286
}
287