Post payouts payout reverse

Reverses a payout by debiting the destination bank account. At this time, you can only reverse payouts for connected accounts to US bank accounts. If the payout is manual and in the pending status, use /v1/payouts/:id/cancel instead. By requesting a reversal through /v1/payouts/:id/reverse, you confirm that the authorized signatory of the selected bank account authorizes the debit on the bank account and that no other authorization is required.

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 payouts payout reverse
6
 * Reverses a payout by debiting the destination bank account. At this time, you can only reverse payouts for connected accounts to US bank accounts. If the payout is manual and in the pending status, use /v1/payouts/:id/cancel instead.
7

8
By requesting a reversal through /v1/payouts/:id/reverse, you confirm that the authorized signatory of the selected bank account authorizes the debit on the bank account and that no other authorization is required.
9
 */
10
export async function main(
11
  auth: Stripe,
12
  payout: string,
13
  body: { expand?: string[]; metadata?: { [k: string]: string } }
14
) {
15
  const url = new URL(`https://api.stripe.com/v1/payouts/${payout}/reverse`);
16

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

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