1 | |
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 payment token for a given payment source |
27 | * Creates a Payment 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 | token?: { id: string; type: "BILLING_AGREEMENT"; attributes?: {} }; |
108 | }; |
109 | metadata?: unknown; |
110 | }, |
111 | ) { |
112 | const token = await getToken(auth); |
113 | const url = new URL( |
114 | `https://api-m.paypal.com/v3/vault/payment-tokens`, |
115 | ); |
116 |
|
117 | const response = await fetch(url, { |
118 | method: "POST", |
119 | headers: { |
120 | "PayPal-Request-Id": PayPal_Request_Id, |
121 | "Content-Type": "application/json", |
122 | Authorization: "Bearer " + token, |
123 | }, |
124 | body: JSON.stringify(body), |
125 | }); |
126 | if (!response.ok) { |
127 | const text = await response.text(); |
128 | throw new Error(`${response.status} ${text}`); |
129 | } |
130 | return await response.json(); |
131 | } |
132 |
|