Post billing portal configurations configuration

Updates a configuration that describes the functionality of the customer portal.

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 billing portal configurations configuration
6
 * Updates a configuration that describes the functionality of the customer portal.
7
 */
8
export async function main(
9
  auth: Stripe,
10
  configuration: string,
11
  body: {
12
    active?: boolean;
13
    business_profile?: {
14
      headline?: string | "";
15
      privacy_policy_url?: string | "";
16
      terms_of_service_url?: string | "";
17
      [k: string]: unknown;
18
    };
19
    default_return_url?: string | "";
20
    expand?: string[];
21
    features?: {
22
      customer_update?: {
23
        allowed_updates?:
24
          | ("address" | "email" | "name" | "phone" | "shipping" | "tax_id")[]
25
          | "";
26
        enabled?: boolean;
27
        [k: string]: unknown;
28
      };
29
      invoice_history?: { enabled: boolean; [k: string]: unknown };
30
      payment_method_update?: { enabled: boolean; [k: string]: unknown };
31
      subscription_cancel?: {
32
        cancellation_reason?: {
33
          enabled: boolean;
34
          options?:
35
            | (
36
                | "customer_service"
37
                | "low_quality"
38
                | "missing_features"
39
                | "other"
40
                | "switched_service"
41
                | "too_complex"
42
                | "too_expensive"
43
                | "unused"
44
              )[]
45
            | "";
46
          [k: string]: unknown;
47
        };
48
        enabled?: boolean;
49
        mode?: "at_period_end" | "immediately";
50
        proration_behavior?: "always_invoice" | "create_prorations" | "none";
51
        [k: string]: unknown;
52
      };
53
      subscription_pause?: { enabled?: boolean; [k: string]: unknown };
54
      subscription_update?: {
55
        default_allowed_updates?:
56
          | ("price" | "promotion_code" | "quantity")[]
57
          | "";
58
        enabled?: boolean;
59
        products?:
60
          | { prices: string[]; product: string; [k: string]: unknown }[]
61
          | "";
62
        proration_behavior?: "always_invoice" | "create_prorations" | "none";
63
        [k: string]: unknown;
64
      };
65
      [k: string]: unknown;
66
    };
67
    login_page?: { enabled: boolean; [k: string]: unknown };
68
    metadata?: { [k: string]: string } | "";
69
  }
70
) {
71
  const url = new URL(
72
    `https://api.stripe.com/v1/billing_portal/configurations/${configuration}`
73
  );
74

75
  const response = await fetch(url, {
76
    method: "POST",
77
    headers: {
78
      "Content-Type": "application/x-www-form-urlencoded",
79
      Authorization: "Bearer " + auth.token,
80
    },
81
    body: encodeParams(body),
82
  });
83
  if (!response.ok) {
84
    const text = await response.text();
85
    throw new Error(`${response.status} ${text}`);
86
  }
87
  return await response.json();
88
}
89

90
function encodeParams(o: any) {
91
  function iter(o: any, path: string) {
92
    if (Array.isArray(o)) {
93
      o.forEach(function (a) {
94
        iter(a, path + "[]");
95
      });
96
      return;
97
    }
98
    if (o !== null && typeof o === "object") {
99
      Object.keys(o).forEach(function (k) {
100
        iter(o[k], path + "[" + k + "]");
101
      });
102
      return;
103
    }
104
    data.push(path + "=" + o);
105
  }
106
  const data: string[] = [];
107
  Object.keys(o).forEach(function (k) {
108
    if (o[k] !== undefined) {
109
      iter(o[k], k);
110
    }
111
  });
112
  return new URLSearchParams(data.join("&"));
113
}
114