Post sources source

Updates the specified source by setting the values of the parameters passed. Any parameters not provided will be left unchanged. This request accepts the metadata and owner as arguments. It is also possible to update type specific information for selected payment methods. Please refer to our payment method guides for more detail.

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 sources source
6
 * Updates the specified source by setting the values of the parameters passed. Any parameters not provided will be left unchanged.
7

8
This request accepts the metadata and owner as arguments. It is also possible to update type specific information for selected payment methods. Please refer to our payment method guides for more detail.
9
 */
10
export async function main(
11
  auth: Stripe,
12
  source: string,
13
  body: {
14
    amount?: number;
15
    expand?: string[];
16
    mandate?: {
17
      acceptance?: {
18
        date?: number;
19
        ip?: string;
20
        offline?: { contact_email: string; [k: string]: unknown };
21
        online?: {
22
          date?: number;
23
          ip?: string;
24
          user_agent?: string;
25
          [k: string]: unknown;
26
        };
27
        status: "accepted" | "pending" | "refused" | "revoked";
28
        type?: "offline" | "online";
29
        user_agent?: string;
30
        [k: string]: unknown;
31
      };
32
      amount?: number | "";
33
      currency?: string;
34
      interval?: "one_time" | "scheduled" | "variable";
35
      notification_method?:
36
        | "deprecated_none"
37
        | "email"
38
        | "manual"
39
        | "none"
40
        | "stripe_email";
41
      [k: string]: unknown;
42
    };
43
    metadata?: { [k: string]: string } | "";
44
    owner?: {
45
      address?: {
46
        city?: string;
47
        country?: string;
48
        line1?: string;
49
        line2?: string;
50
        postal_code?: string;
51
        state?: string;
52
        [k: string]: unknown;
53
      };
54
      email?: string;
55
      name?: string;
56
      phone?: string;
57
      [k: string]: unknown;
58
    };
59
    source_order?: {
60
      items?: {
61
        amount?: number;
62
        currency?: string;
63
        description?: string;
64
        parent?: string;
65
        quantity?: number;
66
        type?: "discount" | "shipping" | "sku" | "tax";
67
        [k: string]: unknown;
68
      }[];
69
      shipping?: {
70
        address: {
71
          city?: string;
72
          country?: string;
73
          line1: string;
74
          line2?: string;
75
          postal_code?: string;
76
          state?: string;
77
          [k: string]: unknown;
78
        };
79
        carrier?: string;
80
        name?: string;
81
        phone?: string;
82
        tracking_number?: string;
83
        [k: string]: unknown;
84
      };
85
      [k: string]: unknown;
86
    };
87
  }
88
) {
89
  const url = new URL(`https://api.stripe.com/v1/sources/${source}`);
90

91
  const response = await fetch(url, {
92
    method: "POST",
93
    headers: {
94
      "Content-Type": "application/x-www-form-urlencoded",
95
      Authorization: "Bearer " + auth.token,
96
    },
97
    body: encodeParams(body),
98
  });
99
  if (!response.ok) {
100
    const text = await response.text();
101
    throw new Error(`${response.status} ${text}`);
102
  }
103
  return await response.json();
104
}
105

106
function encodeParams(o: any) {
107
  function iter(o: any, path: string) {
108
    if (Array.isArray(o)) {
109
      o.forEach(function (a) {
110
        iter(a, path + "[]");
111
      });
112
      return;
113
    }
114
    if (o !== null && typeof o === "object") {
115
      Object.keys(o).forEach(function (k) {
116
        iter(o[k], path + "[" + k + "]");
117
      });
118
      return;
119
    }
120
    data.push(path + "=" + o);
121
  }
122
  const data: string[] = [];
123
  Object.keys(o).forEach(function (k) {
124
    if (o[k] !== undefined) {
125
      iter(o[k], k);
126
    }
127
  });
128
  return new URLSearchParams(data.join("&"));
129
}
130