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 web experience profile |
27 | * Creates a web experience profile. In the JSON request body, specify the profile name and details. |
28 | */ |
29 | export async function main( |
30 | auth: Paypal, |
31 | PayPal_Request_Id: string, |
32 | body: { |
33 | id?: string; |
34 | name: string; |
35 | temporary?: false | true; |
36 | flow_config?: { |
37 | landing_page_type?: "login" | "billing"; |
38 | bank_txn_pending_url?: string; |
39 | user_action?: "COMMIT"; |
40 | return_uri_http_method?: "GET" | "POST"; |
41 | }; |
42 | input_fields?: { no_shipping?: number; address_override?: number }; |
43 | presentation?: { |
44 | brand_name?: string; |
45 | logo_image?: string; |
46 | locale_code?: string; |
47 | }; |
48 | }, |
49 | ) { |
50 | const token = await getToken(auth); |
51 | const url = new URL( |
52 | `https://api-m.paypal.com/v1/payment-experience/web-profiles`, |
53 | ); |
54 |
|
55 | const response = await fetch(url, { |
56 | method: "POST", |
57 | headers: { |
58 | "PayPal-Request-Id": PayPal_Request_Id, |
59 | "Content-Type": "application/json", |
60 | Authorization: "Bearer " + token, |
61 | }, |
62 | body: JSON.stringify(body), |
63 | }); |
64 | if (!response.ok) { |
65 | const text = await response.text(); |
66 | throw new Error(`${response.status} ${text}`); |
67 | } |
68 | return await response.json(); |
69 | } |
70 |
|