forked from paypal-examples/googlepay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
paypal-api.js
114 lines (103 loc) · 3.06 KB
/
paypal-api.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import fetch from "node-fetch";
// set some important variables
const { PAYPAL_CLIENT_ID, PAYPAL_CLIENT_SECRET, PAYPAL_MERCHANT_ID , BASE_URL } = process.env;
// const sandbox = "https://api-m.sandbox.paypal.com";
// const production = "https://api-m.paypal.com";
const base = `${BASE_URL}`
// call the create order method
export async function createOrder() {
const purchaseAmount = "0.10"; // TODO: pull prices from a database
const accessToken = await generateAccessToken();
const url = `${base}/v2/checkout/orders`;
const response = await fetch(url, {
method: "post",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${accessToken}`,
},
body: JSON.stringify({
intent: "CAPTURE",
purchase_units: [
{
amount: {
currency_code: "USD",
value: purchaseAmount,
},
payee: {
merchant_id: PAYPAL_MERCHANT_ID,
}
},
],
payment_source: {
google_pay:{
attributes:{
verification: {
method: 'SCA_WHEN_REQUIRED',
},
}
}
}
}),
});
return handleResponse(response);
}
// capture payment for an order
export async function capturePayment(orderId) {
const accessToken = await generateAccessToken();
const url = `${base}/v2/checkout/orders/${orderId}/capture`;
const response = await fetch(url, {
method: "post",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${accessToken}`,
},
});
return handleResponse(response);
}
// generate access token
export async function generateAccessToken() {
const auth = Buffer.from(PAYPAL_CLIENT_ID + ":" + PAYPAL_CLIENT_SECRET).toString("base64");
const response = await fetch(`${base}/v1/oauth2/token`, {
method: "post",
body: "grant_type=client_credentials",
headers: {
Authorization: `Basic ${auth}`,
},
});
const jsonData = await handleResponse(response);
return jsonData.access_token;
}
// generate client token
export async function generateClientToken() {
const accessToken = await generateAccessToken();
const response = await fetch(`${base}/v1/identity/generate-token`, {
method: "post",
headers: {
Authorization: `Bearer ${accessToken}`,
"Accept-Language": "en_US",
"Content-Type": "application/json",
},
});
console.log('response', response.status)
const jsonData = await handleResponse(response);
return jsonData.client_token;
}
async function handleResponse(response) {
if (response.status === 200 || response.status === 201) {
return response.json();
}
const errorMessage = await response.text();
throw new Error(errorMessage);
}
export async function getOrder(orderId) {
const accessToken = await generateAccessToken();
const url = `${base}/v2/checkout/orders/${orderId}`;
const response = await fetch(url, {
method: "get",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${accessToken}`,
},
});
return handleResponse(response);
}