Post disputes dispute

When you get a dispute, contacting your customer is always the best first step. If that doesn’t work, you can submit evidence to help us resolve the dispute in your favor. You can do this in your dashboard, but if you prefer, you can use the API to submit evidence programmatically. Depending on your dispute type, different evidence fields will give you a better chance of winning your dispute. To figure out which evidence fields to provide, see our guide to dispute types.

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 disputes dispute
6
 * When you get a dispute, contacting your customer is always the best first step. If that doesn’t work, you can submit evidence to help us resolve the dispute in your favor. You can do this in your dashboard, but if you prefer, you can use the API to submit evidence programmatically.
7

8
Depending on your dispute type, different evidence fields will give you a better chance of winning your dispute. To figure out which evidence fields to provide, see our guide to dispute types.
9
 */
10
export async function main(
11
  auth: Stripe,
12
  dispute: string,
13
  body: {
14
    evidence?: {
15
      access_activity_log?: string;
16
      billing_address?: string;
17
      cancellation_policy?: string;
18
      cancellation_policy_disclosure?: string;
19
      cancellation_rebuttal?: string;
20
      customer_communication?: string;
21
      customer_email_address?: string;
22
      customer_name?: string;
23
      customer_purchase_ip?: string;
24
      customer_signature?: string;
25
      duplicate_charge_documentation?: string;
26
      duplicate_charge_explanation?: string;
27
      duplicate_charge_id?: string;
28
      product_description?: string;
29
      receipt?: string;
30
      refund_policy?: string;
31
      refund_policy_disclosure?: string;
32
      refund_refusal_explanation?: string;
33
      service_date?: string;
34
      service_documentation?: string;
35
      shipping_address?: string;
36
      shipping_carrier?: string;
37
      shipping_date?: string;
38
      shipping_documentation?: string;
39
      shipping_tracking_number?: string;
40
      uncategorized_file?: string;
41
      uncategorized_text?: string;
42
      [k: string]: unknown;
43
    };
44
    expand?: string[];
45
    metadata?: { [k: string]: string } | "";
46
    submit?: boolean;
47
  }
48
) {
49
  const url = new URL(`https://api.stripe.com/v1/disputes/${dispute}`);
50

51
  const response = await fetch(url, {
52
    method: "POST",
53
    headers: {
54
      "Content-Type": "application/x-www-form-urlencoded",
55
      Authorization: "Bearer " + auth.token,
56
    },
57
    body: encodeParams(body),
58
  });
59
  if (!response.ok) {
60
    const text = await response.text();
61
    throw new Error(`${response.status} ${text}`);
62
  }
63
  return await response.json();
64
}
65

66
function encodeParams(o: any) {
67
  function iter(o: any, path: string) {
68
    if (Array.isArray(o)) {
69
      o.forEach(function (a) {
70
        iter(a, path + "[]");
71
      });
72
      return;
73
    }
74
    if (o !== null && typeof o === "object") {
75
      Object.keys(o).forEach(function (k) {
76
        iter(o[k], path + "[" + k + "]");
77
      });
78
      return;
79
    }
80
    data.push(path + "=" + o);
81
  }
82
  const data: string[] = [];
83
  Object.keys(o).forEach(function (k) {
84
    if (o[k] !== undefined) {
85
      iter(o[k], k);
86
    }
87
  });
88
  return new URLSearchParams(data.join("&"));
89
}
90