0

Create a setup token

by
Published Apr 8, 2025

Creates a Setup Token from the given payment source and adds it to the Vault of the associated customer.

Script paypal Verified

The script

Submitted by hugo697 Bun
Verified 428 days ago
1
//native
2
type Paypal = {
3
  clientId: string;
4
  clientSecret: string;
5
};
6

7
async function getToken(auth: Paypal): Promise<string> {
8
  const url = new URL(`https://api-m.paypal.com/v1/oauth2/token`);
9
  const response = await fetch(url, {
10
    method: "POST",
11
    headers: {
12
      Authorization: `Basic ${btoa(`${auth.clientId}:${auth.clientSecret}`)}`,
13
    },
14
    body: new URLSearchParams({
15
      grant_type: "client_credentials",
16
    }),
17
  });
18
  if (!response.ok) {
19
    const text = await response.text();
20
    throw new Error(`Could not get token: ${response.status} ${text}`);
21
  }
22
  const json = await response.json();
23
  return json.access_token;
24
}
25
/**
26
 * Create a setup token
27
 * Creates a Setup Token from the given payment source and adds it to the Vault of the associated customer.
28
 */
29
export async function main(
30
  auth: Paypal,
31
  PayPal_Request_Id: string,
32
  body: {
33
    customer?: { id?: string };
34
    payment_source: {
35
      card?: {
36
        id?: string;
37
        name?: string;
38
        number?: string;
39
        expiry?: string;
40
        security_code?: string;
41
        last_digits?: string;
42
        card_type?:
43
          | "VISA"
44
          | "MASTERCARD"
45
          | "DISCOVER"
46
          | "AMEX"
47
          | "SOLO"
48
          | "JCB"
49
          | "STAR"
50
          | "DELTA"
51
          | "SWITCH"
52
          | "MAESTRO"
53
          | "CB_NATIONALE"
54
          | "CONFIGOGA"
55
          | "CONFIDIS"
56
          | "ELECTRON"
57
          | "CETELEM"
58
          | "CHINA_UNION_PAY";
59
        type?: "CREDIT" | "DEBIT" | "PREPAID" | "STORE" | "UNKNOWN";
60
        brand?:
61
          | "VISA"
62
          | "MASTERCARD"
63
          | "DISCOVER"
64
          | "AMEX"
65
          | "SOLO"
66
          | "JCB"
67
          | "STAR"
68
          | "DELTA"
69
          | "SWITCH"
70
          | "MAESTRO"
71
          | "CB_NATIONALE"
72
          | "CONFIGOGA"
73
          | "CONFIDIS"
74
          | "ELECTRON"
75
          | "CETELEM"
76
          | "CHINA_UNION_PAY";
77
        billing_address?: {
78
          address_line_1?: string;
79
          address_line_2?: string;
80
          address_line_3?: string;
81
          admin_area_4?: string;
82
          admin_area_3?: string;
83
          admin_area_2?: string;
84
          admin_area_1?: string;
85
          postal_code?: string;
86
          country_code: string;
87
          address_details?: {
88
            street_number?: string;
89
            street_name?: string;
90
            street_type?: string;
91
            delivery_service?: string;
92
            building_name?: string;
93
            sub_building?: string;
94
          };
95
        };
96
      } & {
97
        verification_method?: string;
98
        experience_context?: {
99
          brand_name?: string;
100
          locale?: string;
101
          return_url?: string;
102
          cancel_url?: string;
103
          shipping_preference?: string;
104
          vault_instruction?: string;
105
        };
106
      };
107
      paypal?: {
108
        description?: string;
109
        shipping?: {
110
          name?: {
111
            prefix?: string;
112
            given_name?: string;
113
            surname?: string;
114
            middle_name?: string;
115
            suffix?: string;
116
            alternate_full_name?: string;
117
            full_name?: string;
118
          };
119
          type?: "SHIPPING" | "PICKUP_IN_PERSON";
120
          address?: {
121
            address_line_1?: string;
122
            address_line_2?: string;
123
            address_line_3?: string;
124
            admin_area_4?: string;
125
            admin_area_3?: string;
126
            admin_area_2?: string;
127
            admin_area_1?: string;
128
            postal_code?: string;
129
            country_code: string;
130
            address_details?: {
131
              street_number?: string;
132
              street_name?: string;
133
              street_type?: string;
134
              delivery_service?: string;
135
              building_name?: string;
136
              sub_building?: string;
137
            };
138
          };
139
        };
140
        permit_multiple_payment_tokens?: false | true;
141
        usage_type?: string;
142
        customer_type?: string;
143
      } & {
144
        experience_context?: {
145
          brand_name?: string;
146
          locale?: string;
147
          return_url?: string;
148
          cancel_url?: string;
149
          shipping_preference?: string;
150
          vault_instruction?: string;
151
        };
152
      };
153
      venmo?: {
154
        description?: string;
155
        shipping?: {
156
          name?: {
157
            prefix?: string;
158
            given_name?: string;
159
            surname?: string;
160
            middle_name?: string;
161
            suffix?: string;
162
            alternate_full_name?: string;
163
            full_name?: string;
164
          };
165
          type?: "SHIPPING" | "PICKUP_IN_PERSON";
166
          address?: {
167
            address_line_1?: string;
168
            address_line_2?: string;
169
            address_line_3?: string;
170
            admin_area_4?: string;
171
            admin_area_3?: string;
172
            admin_area_2?: string;
173
            admin_area_1?: string;
174
            postal_code?: string;
175
            country_code: string;
176
            address_details?: {
177
              street_number?: string;
178
              street_name?: string;
179
              street_type?: string;
180
              delivery_service?: string;
181
              building_name?: string;
182
              sub_building?: string;
183
            };
184
          };
185
        };
186
        permit_multiple_payment_tokens?: false | true;
187
        usage_type?: string;
188
        customer_type?: string;
189
      } & {
190
        experience_context?: {
191
          brand_name?: string;
192
          locale?: string;
193
          return_url?: string;
194
          cancel_url?: string;
195
          shipping_preference?: string;
196
          vault_instruction?: string;
197
        };
198
      };
199
      token?: { id: string; type: "BILLING_AGREEMENT"; attributes?: {} };
200
    };
201
    metadata?: unknown;
202
  },
203
) {
204
  const token = await getToken(auth);
205
  const url = new URL(`https://api-m.paypal.com/v3/vault/setup-tokens`);
206

207
  const response = await fetch(url, {
208
    method: "POST",
209
    headers: {
210
      "PayPal-Request-Id": PayPal_Request_Id,
211
      "Content-Type": "application/json",
212
      Authorization: "Bearer " + token,
213
    },
214
    body: JSON.stringify(body),
215
  });
216
  if (!response.ok) {
217
    const text = await response.text();
218
    throw new Error(`${response.status} ${text}`);
219
  }
220
  return await response.json();
221
}
222