Post billing portal configurations

Creates a configuration that describes the functionality and behavior of a PortalSession

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

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

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