0

Post climate orders order

by
Published Mar 6, 2024

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

Script stripe Verified

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 398 days ago
1
type Stripe = {
2
  token: string;
3
};
4
/**
5
 * Post climate orders order
6
 * Updates the specified order by setting the values of the parameters passed.
7
 */
8
export async function main(
9
  auth: Stripe,
10
  order: string,
11
  body: {
12
    beneficiary?: { public_name: string | ""; [k: string]: unknown } | "";
13
    expand?: string[];
14
    metadata?: { [k: string]: string };
15
  }
16
) {
17
  const url = new URL(`https://api.stripe.com/v1/climate/orders/${order}`);
18

19
  const response = await fetch(url, {
20
    method: "POST",
21
    headers: {
22
      "Content-Type": "application/x-www-form-urlencoded",
23
      Authorization: "Bearer " + auth.token,
24
    },
25
    body: encodeParams(body),
26
  });
27
  if (!response.ok) {
28
    const text = await response.text();
29
    throw new Error(`${response.status} ${text}`);
30
  }
31
  return await response.json();
32
}
33

34
function encodeParams(o: any) {
35
  function iter(o: any, path: string) {
36
    if (Array.isArray(o)) {
37
      o.forEach(function (a) {
38
        iter(a, path + "[]");
39
      });
40
      return;
41
    }
42
    if (o !== null && typeof o === "object") {
43
      Object.keys(o).forEach(function (k) {
44
        iter(o[k], path + "[" + k + "]");
45
      });
46
      return;
47
    }
48
    data.push(path + "=" + o);
49
  }
50
  const data: string[] = [];
51
  Object.keys(o).forEach(function (k) {
52
    if (o[k] !== undefined) {
53
      iter(o[k], k);
54
    }
55
  });
56
  return new URLSearchParams(data.join("&"));
57
}
58