0

Post issuing authorizations authorization decline

by
Published Oct 30, 2023

[Deprecated] Declines a pending Issuing Authorization object. This request should be made within the timeout window of the real time authorization flow. This method is deprecated. Instead, respond directly to the webhook request to decline an authorization.

Script stripe Verified

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 399 days ago
1
type Stripe = {
2
  token: string;
3
};
4
/**
5
 * Post issuing authorizations authorization decline
6
 * [Deprecated] Declines a pending Issuing Authorization object. This request should be made within the timeout window of the real time authorization flow.
7
This method is deprecated. Instead, respond directly to the webhook request to decline an authorization.
8
 */
9
export async function main(
10
  auth: Stripe,
11
  authorization: string,
12
  body: { expand?: string[]; metadata?: { [k: string]: string } | "" }
13
) {
14
  const url = new URL(
15
    `https://api.stripe.com/v1/issuing/authorizations/${authorization}/decline`
16
  );
17

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

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