Post issuing disputes

Creates an Issuing Dispute object. Individual pieces of evidence within the evidence object are optional at this point. Stripe only validates that required evidence is present during submission. Refer to Dispute reasons and evidence for more details about evidence requirements.

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 issuing disputes
6
 * Creates an Issuing Dispute object. Individual pieces of evidence within the evidence object are optional at this point. Stripe only validates that required evidence is present during submission. Refer to Dispute reasons and evidence for more details about evidence requirements.
7
 */
8
export async function main(
9
  auth: Stripe,
10
  body: {
11
    amount?: number;
12
    evidence?: {
13
      canceled?:
14
        | {
15
            additional_documentation?: string | "";
16
            canceled_at?: number | "";
17
            cancellation_policy_provided?: boolean | "";
18
            cancellation_reason?: string | "";
19
            expected_at?: number | "";
20
            explanation?: string | "";
21
            product_description?: string | "";
22
            product_type?: "" | "merchandise" | "service";
23
            return_status?: "" | "merchant_rejected" | "successful";
24
            returned_at?: number | "";
25
            [k: string]: unknown;
26
          }
27
        | "";
28
      duplicate?:
29
        | {
30
            additional_documentation?: string | "";
31
            card_statement?: string | "";
32
            cash_receipt?: string | "";
33
            check_image?: string | "";
34
            explanation?: string | "";
35
            original_transaction?: string;
36
            [k: string]: unknown;
37
          }
38
        | "";
39
      fraudulent?:
40
        | {
41
            additional_documentation?: string | "";
42
            explanation?: string | "";
43
            [k: string]: unknown;
44
          }
45
        | "";
46
      merchandise_not_as_described?:
47
        | {
48
            additional_documentation?: string | "";
49
            explanation?: string | "";
50
            received_at?: number | "";
51
            return_description?: string | "";
52
            return_status?: "" | "merchant_rejected" | "successful";
53
            returned_at?: number | "";
54
            [k: string]: unknown;
55
          }
56
        | "";
57
      not_received?:
58
        | {
59
            additional_documentation?: string | "";
60
            expected_at?: number | "";
61
            explanation?: string | "";
62
            product_description?: string | "";
63
            product_type?: "" | "merchandise" | "service";
64
            [k: string]: unknown;
65
          }
66
        | "";
67
      other?:
68
        | {
69
            additional_documentation?: string | "";
70
            explanation?: string | "";
71
            product_description?: string | "";
72
            product_type?: "" | "merchandise" | "service";
73
            [k: string]: unknown;
74
          }
75
        | "";
76
      reason?:
77
        | "canceled"
78
        | "duplicate"
79
        | "fraudulent"
80
        | "merchandise_not_as_described"
81
        | "not_received"
82
        | "other"
83
        | "service_not_as_described";
84
      service_not_as_described?:
85
        | {
86
            additional_documentation?: string | "";
87
            canceled_at?: number | "";
88
            cancellation_reason?: string | "";
89
            explanation?: string | "";
90
            received_at?: number | "";
91
            [k: string]: unknown;
92
          }
93
        | "";
94
      [k: string]: unknown;
95
    };
96
    expand?: string[];
97
    metadata?: { [k: string]: string };
98
    transaction?: string;
99
    treasury?: { received_debit: string; [k: string]: unknown };
100
  }
101
) {
102
  const url = new URL(`https://api.stripe.com/v1/issuing/disputes`);
103

104
  const response = await fetch(url, {
105
    method: "POST",
106
    headers: {
107
      "Content-Type": "application/x-www-form-urlencoded",
108
      Authorization: "Bearer " + auth.token,
109
    },
110
    body: encodeParams(body),
111
  });
112
  if (!response.ok) {
113
    const text = await response.text();
114
    throw new Error(`${response.status} ${text}`);
115
  }
116
  return await response.json();
117
}
118

119
function encodeParams(o: any) {
120
  function iter(o: any, path: string) {
121
    if (Array.isArray(o)) {
122
      o.forEach(function (a) {
123
        iter(a, path + "[]");
124
      });
125
      return;
126
    }
127
    if (o !== null && typeof o === "object") {
128
      Object.keys(o).forEach(function (k) {
129
        iter(o[k], path + "[" + k + "]");
130
      });
131
      return;
132
    }
133
    data.push(path + "=" + o);
134
  }
135
  const data: string[] = [];
136
  Object.keys(o).forEach(function (k) {
137
    if (o[k] !== undefined) {
138
      iter(o[k], k);
139
    }
140
  });
141
  return new URLSearchParams(data.join("&"));
142
}
143