Post test helpers treasury inbound transfers id fail

Transitions a test mode created InboundTransfer to the failed status. The InboundTransfer must already be in the processing state.

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 test helpers treasury inbound transfers id fail
6
 * Transitions a test mode created InboundTransfer to the failed status. The InboundTransfer must already be in the processing state.
7
 */
8
export async function main(
9
  auth: Stripe,
10
  id: string,
11
  body: {
12
    expand?: string[];
13
    failure_details?: {
14
      code?:
15
        | "account_closed"
16
        | "account_frozen"
17
        | "bank_account_restricted"
18
        | "bank_ownership_changed"
19
        | "debit_not_authorized"
20
        | "incorrect_account_holder_address"
21
        | "incorrect_account_holder_name"
22
        | "incorrect_account_holder_tax_id"
23
        | "insufficient_funds"
24
        | "invalid_account_number"
25
        | "invalid_currency"
26
        | "no_account"
27
        | "other";
28
      [k: string]: unknown;
29
    };
30
  }
31
) {
32
  const url = new URL(
33
    `https://api.stripe.com/v1/test_helpers/treasury/inbound_transfers/${id}/fail`
34
  );
35

36
  const response = await fetch(url, {
37
    method: "POST",
38
    headers: {
39
      "Content-Type": "application/x-www-form-urlencoded",
40
      Authorization: "Bearer " + auth.token,
41
    },
42
    body: encodeParams(body),
43
  });
44
  if (!response.ok) {
45
    const text = await response.text();
46
    throw new Error(`${response.status} ${text}`);
47
  }
48
  return await response.json();
49
}
50

51
function encodeParams(o: any) {
52
  function iter(o: any, path: string) {
53
    if (Array.isArray(o)) {
54
      o.forEach(function (a) {
55
        iter(a, path + "[]");
56
      });
57
      return;
58
    }
59
    if (o !== null && typeof o === "object") {
60
      Object.keys(o).forEach(function (k) {
61
        iter(o[k], path + "[" + k + "]");
62
      });
63
      return;
64
    }
65
    data.push(path + "=" + o);
66
  }
67
  const data: string[] = [];
68
  Object.keys(o).forEach(function (k) {
69
    if (o[k] !== undefined) {
70
      iter(o[k], k);
71
    }
72
  });
73
  return new URLSearchParams(data.join("&"));
74
}
75