Post customers customer

Updates the specified customer by setting the values of the parameters passed.

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 customers customer
6
 * Updates the specified customer by setting the values of the parameters passed.
7
 */
8
export async function main(
9
  auth: Stripe,
10
  customer: string,
11
  body: {
12
    address?:
13
      | {
14
          city?: string;
15
          country?: string;
16
          line1?: string;
17
          line2?: string;
18
          postal_code?: string;
19
          state?: string;
20
          [k: string]: unknown;
21
        }
22
      | "";
23
    balance?: number;
24
    bank_account?:
25
      | {
26
          account_holder_name?: string;
27
          account_holder_type?: "company" | "individual";
28
          account_number: string;
29
          country: string;
30
          currency?: string;
31
          object?: "bank_account";
32
          routing_number?: string;
33
          [k: string]: unknown;
34
        }
35
      | string;
36
    card?:
37
      | {
38
          address_city?: string;
39
          address_country?: string;
40
          address_line1?: string;
41
          address_line2?: string;
42
          address_state?: string;
43
          address_zip?: string;
44
          cvc?: string;
45
          exp_month: number;
46
          exp_year: number;
47
          metadata?: { [k: string]: string };
48
          name?: string;
49
          number: string;
50
          object?: "card";
51
          [k: string]: unknown;
52
        }
53
      | string;
54
    cash_balance?: {
55
      settings?: {
56
        reconciliation_mode?: "automatic" | "manual" | "merchant_default";
57
        [k: string]: unknown;
58
      };
59
      [k: string]: unknown;
60
    };
61
    coupon?: string;
62
    default_alipay_account?: string;
63
    default_bank_account?: string;
64
    default_card?: string;
65
    default_source?: string;
66
    description?: string;
67
    email?: string;
68
    expand?: string[];
69
    invoice_prefix?: string;
70
    invoice_settings?: {
71
      custom_fields?:
72
        | { name: string; value: string; [k: string]: unknown }[]
73
        | "";
74
      default_payment_method?: string;
75
      footer?: string;
76
      rendering_options?:
77
        | {
78
            amount_tax_display?: "" | "exclude_tax" | "include_inclusive_tax";
79
            [k: string]: unknown;
80
          }
81
        | "";
82
      [k: string]: unknown;
83
    };
84
    metadata?: { [k: string]: string } | "";
85
    name?: string;
86
    next_invoice_sequence?: number;
87
    phone?: string;
88
    preferred_locales?: string[];
89
    promotion_code?: string;
90
    shipping?:
91
      | {
92
          address: {
93
            city?: string;
94
            country?: string;
95
            line1?: string;
96
            line2?: string;
97
            postal_code?: string;
98
            state?: string;
99
            [k: string]: unknown;
100
          };
101
          name: string;
102
          phone?: string;
103
          [k: string]: unknown;
104
        }
105
      | "";
106
    source?: string;
107
    tax?: {
108
      ip_address?: string | "";
109
      validate_location?: "deferred" | "immediately";
110
      [k: string]: unknown;
111
    };
112
    tax_exempt?: "" | "exempt" | "none" | "reverse";
113
  }
114
) {
115
  const url = new URL(`https://api.stripe.com/v1/customers/${customer}`);
116

117
  const response = await fetch(url, {
118
    method: "POST",
119
    headers: {
120
      "Content-Type": "application/x-www-form-urlencoded",
121
      Authorization: "Bearer " + auth.token,
122
    },
123
    body: encodeParams(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

132
function encodeParams(o: any) {
133
  function iter(o: any, path: string) {
134
    if (Array.isArray(o)) {
135
      o.forEach(function (a) {
136
        iter(a, path + "[]");
137
      });
138
      return;
139
    }
140
    if (o !== null && typeof o === "object") {
141
      Object.keys(o).forEach(function (k) {
142
        iter(o[k], path + "[" + k + "]");
143
      });
144
      return;
145
    }
146
    data.push(path + "=" + o);
147
  }
148
  const data: string[] = [];
149
  Object.keys(o).forEach(function (k) {
150
    if (o[k] !== undefined) {
151
      iter(o[k], k);
152
    }
153
  });
154
  return new URLSearchParams(data.join("&"));
155
}
156