0

CreateDisputeEvidenceFile

by
Published Oct 17, 2025

Uploads a file to use as evidence in a dispute challenge. The endpoint accepts HTTP multipart/form-data file uploads in HEIC, HEIF, JPEG, PDF, PNG, and TIFF formats.

Script square Verified

The script

Submitted by hugo697 Bun
Verified 235 days ago
1
//native
2
type Square = {
3
  token: string;
4
};
5
type Base64 = string;
6
/**
7
 * CreateDisputeEvidenceFile
8
 * Uploads a file to use as evidence in a dispute challenge. The endpoint accepts HTTP
9
multipart/form-data file uploads in HEIC, HEIF, JPEG, PDF, PNG, and TIFF formats.
10
 */
11
export async function main(
12
  auth: Square,
13
  dispute_id: string,
14
  body: {
15
    request?: {
16
      idempotency_key: string;
17
      evidence_type?:
18
        | "GENERIC_EVIDENCE"
19
        | "ONLINE_OR_APP_ACCESS_LOG"
20
        | "AUTHORIZATION_DOCUMENTATION"
21
        | "CANCELLATION_OR_REFUND_DOCUMENTATION"
22
        | "CARDHOLDER_COMMUNICATION"
23
        | "CARDHOLDER_INFORMATION"
24
        | "PURCHASE_ACKNOWLEDGEMENT"
25
        | "DUPLICATE_CHARGE_DOCUMENTATION"
26
        | "PRODUCT_OR_SERVICE_DESCRIPTION"
27
        | "RECEIPT"
28
        | "SERVICE_RECEIVED_DOCUMENTATION"
29
        | "PROOF_OF_DELIVERY_DOCUMENTATION"
30
        | "RELATED_TRANSACTION_DOCUMENTATION"
31
        | "REBUTTAL_EXPLANATION"
32
        | "TRACKING_NUMBER";
33
      content_type?: string;
34
    };
35
    image_file?: {
36
      base64: Base64;
37
      type:
38
        | "image/png"
39
        | "image/jpeg"
40
        | "image/gif"
41
        | "application/pdf"
42
        | "appication/json"
43
        | "text/csv"
44
        | "text/plain"
45
        | "audio/mpeg"
46
        | "audio/wav"
47
        | "video/mp4";
48
      name: string;
49
    };
50
  },
51
) {
52
  const url = new URL(
53
    `https://connect.squareup.com/v2/disputes/${dispute_id}/evidence-files`,
54
  );
55

56
  const formData = new FormData();
57
  for (const [k, v] of Object.entries(body)) {
58
    if (v !== undefined) {
59
      if (["image_file"].includes(k)) {
60
        const { base64, type, name } = v as {
61
          base64: Base64;
62
          type: string;
63
          name: string;
64
        };
65
        formData.append(
66
          k,
67
          new Blob([Uint8Array.from(atob(base64), (m) => m.codePointAt(0)!)], {
68
            type,
69
          }),
70
          name,
71
        );
72
      } else {
73
        formData.append(k, String(v));
74
      }
75
    }
76
  }
77
  const response = await fetch(url, {
78
    method: "POST",
79
    headers: {
80
      Authorization: "Bearer " + auth.token,
81
    },
82
    body: formData,
83
  });
84
  if (!response.ok) {
85
    const text = await response.text();
86
    throw new Error(`${response.status} ${text}`);
87
  }
88
  return await response.json();
89
}
90