| 1 | type Stripe = {
 | 
 | 2 |   token: string;
 | 
 | 3 | };
 | 
 | 4 | 
 | 
 | 5 |  * Post charges charge dispute
 | 
 | 6 |  *
 | 
 | 7 |  */
 | 
 | 8 | export async function main(
 | 
 | 9 |   auth: Stripe,
 | 
 | 10 |   charge: string,
 | 
 | 11 |   body: {
 | 
 | 12 |     evidence?: {
 | 
 | 13 |       access_activity_log?: string;
 | 
 | 14 |       billing_address?: string;
 | 
 | 15 |       cancellation_policy?: string;
 | 
 | 16 |       cancellation_policy_disclosure?: string;
 | 
 | 17 |       cancellation_rebuttal?: string;
 | 
 | 18 |       customer_communication?: string;
 | 
 | 19 |       customer_email_address?: string;
 | 
 | 20 |       customer_name?: string;
 | 
 | 21 |       customer_purchase_ip?: string;
 | 
 | 22 |       customer_signature?: string;
 | 
 | 23 |       duplicate_charge_documentation?: string;
 | 
 | 24 |       duplicate_charge_explanation?: string;
 | 
 | 25 |       duplicate_charge_id?: string;
 | 
 | 26 |       product_description?: string;
 | 
 | 27 |       receipt?: string;
 | 
 | 28 |       refund_policy?: string;
 | 
 | 29 |       refund_policy_disclosure?: string;
 | 
 | 30 |       refund_refusal_explanation?: string;
 | 
 | 31 |       service_date?: string;
 | 
 | 32 |       service_documentation?: string;
 | 
 | 33 |       shipping_address?: string;
 | 
 | 34 |       shipping_carrier?: string;
 | 
 | 35 |       shipping_date?: string;
 | 
 | 36 |       shipping_documentation?: string;
 | 
 | 37 |       shipping_tracking_number?: string;
 | 
 | 38 |       uncategorized_file?: string;
 | 
 | 39 |       uncategorized_text?: string;
 | 
 | 40 |       [k: string]: unknown;
 | 
 | 41 |     };
 | 
 | 42 |     expand?: string[];
 | 
 | 43 |     metadata?: { [k: string]: string } | "";
 | 
 | 44 |     submit?: boolean;
 | 
 | 45 |   }
 | 
 | 46 | ) {
 | 
 | 47 |   const url = new URL(`https://api.stripe.com/v1/charges/${charge}/dispute`);
 | 
 | 48 | 
 | 
 | 49 |   const response = await fetch(url, {
 | 
 | 50 |     method: "POST",
 | 
 | 51 |     headers: {
 | 
 | 52 |       "Content-Type": "application/x-www-form-urlencoded",
 | 
 | 53 |       Authorization: "Bearer " + auth.token,
 | 
 | 54 |     },
 | 
 | 55 |     body: encodeParams(body),
 | 
 | 56 |   });
 | 
 | 57 |   if (!response.ok) {
 | 
 | 58 |     const text = await response.text();
 | 
 | 59 |     throw new Error(`${response.status} ${text}`);
 | 
 | 60 |   }
 | 
 | 61 |   return await response.json();
 | 
 | 62 | }
 | 
 | 63 | 
 | 
 | 64 | function encodeParams(o: any) {
 | 
 | 65 |   function iter(o: any, path: string) {
 | 
 | 66 |     if (Array.isArray(o)) {
 | 
 | 67 |       o.forEach(function (a) {
 | 
 | 68 |         iter(a, path + "[]");
 | 
 | 69 |       });
 | 
 | 70 |       return;
 | 
 | 71 |     }
 | 
 | 72 |     if (o !== null && typeof o === "object") {
 | 
 | 73 |       Object.keys(o).forEach(function (k) {
 | 
 | 74 |         iter(o[k], path + "[" + k + "]");
 | 
 | 75 |       });
 | 
 | 76 |       return;
 | 
 | 77 |     }
 | 
 | 78 |     data.push(path + "=" + o);
 | 
 | 79 |   }
 | 
 | 80 |   const data: string[] = [];
 | 
 | 81 |   Object.keys(o).forEach(function (k) {
 | 
 | 82 |     if (o[k] !== undefined) {
 | 
 | 83 |       iter(o[k], k);
 | 
 | 84 |     }
 | 
 | 85 |   });
 | 
 | 86 |   return new URLSearchParams(data.join("&"));
 | 
 | 87 | }
 | 
 | 88 | 
 |