1
- import express from ' express' ;
2
- import fetch from ' node-fetch' ;
3
- import ' dotenv/config' ;
4
- import path from ' path' ;
1
+ import express from " express" ;
2
+ import fetch from " node-fetch" ;
3
+ import " dotenv/config" ;
4
+ import path from " path" ;
5
5
6
6
const { PAYPAL_CLIENT_ID , PAYPAL_CLIENT_SECRET , PORT = 8888 } = process . env ;
7
- const base = ' https://api-m.sandbox.paypal.com' ;
7
+ const base = " https://api-m.sandbox.paypal.com" ;
8
8
const app = express ( ) ;
9
- app . set ( ' view engine' , ' ejs' ) ;
10
- app . use ( express . static ( ' public' ) ) ;
9
+ app . set ( " view engine" , " ejs" ) ;
10
+ app . use ( express . static ( " public" ) ) ;
11
11
12
12
// parse post params sent in body in json format
13
13
app . use ( express . json ( ) ) ;
@@ -19,14 +19,14 @@ app.use(express.json());
19
19
const generateAccessToken = async ( ) => {
20
20
try {
21
21
if ( ! PAYPAL_CLIENT_ID || ! PAYPAL_CLIENT_SECRET ) {
22
- throw new Error ( ' MISSING_API_CREDENTIALS' ) ;
22
+ throw new Error ( " MISSING_API_CREDENTIALS" ) ;
23
23
}
24
24
const auth = Buffer . from (
25
- PAYPAL_CLIENT_ID + ':' + PAYPAL_CLIENT_SECRET ,
26
- ) . toString ( ' base64' ) ;
25
+ PAYPAL_CLIENT_ID + ":" + PAYPAL_CLIENT_SECRET ,
26
+ ) . toString ( " base64" ) ;
27
27
const response = await fetch ( `${ base } /v1/oauth2/token` , {
28
- method : ' POST' ,
29
- body : ' grant_type=client_credentials' ,
28
+ method : " POST" ,
29
+ body : " grant_type=client_credentials" ,
30
30
headers : {
31
31
Authorization : `Basic ${ auth } ` ,
32
32
} ,
@@ -35,7 +35,7 @@ const generateAccessToken = async () => {
35
35
const data = await response . json ( ) ;
36
36
return data . access_token ;
37
37
} catch ( error ) {
38
- console . error ( ' Failed to generate Access Token:' , error ) ;
38
+ console . error ( " Failed to generate Access Token:" , error ) ;
39
39
}
40
40
} ;
41
41
@@ -47,11 +47,11 @@ const generateClientToken = async () => {
47
47
const accessToken = await generateAccessToken ( ) ;
48
48
const url = `${ base } /v1/identity/generate-token` ;
49
49
const response = await fetch ( url , {
50
- method : ' POST' ,
50
+ method : " POST" ,
51
51
headers : {
52
52
Authorization : `Bearer ${ accessToken } ` ,
53
- ' Accept-Language' : ' en_US' ,
54
- ' Content-Type' : ' application/json' ,
53
+ " Accept-Language" : " en_US" ,
54
+ " Content-Type" : " application/json" ,
55
55
} ,
56
56
} ) ;
57
57
@@ -65,35 +65,35 @@ const generateClientToken = async () => {
65
65
const createOrder = async ( cart ) => {
66
66
// use the cart information passed from the front-end to calculate the purchase unit details
67
67
console . log (
68
- ' shopping cart information passed from the frontend createOrder() callback:' ,
68
+ " shopping cart information passed from the frontend createOrder() callback:" ,
69
69
cart ,
70
70
) ;
71
71
72
72
const accessToken = await generateAccessToken ( ) ;
73
73
const url = `${ base } /v2/checkout/orders` ;
74
74
const payload = {
75
- intent : ' CAPTURE' ,
75
+ intent : " CAPTURE" ,
76
76
purchase_units : [
77
77
{
78
78
amount : {
79
- currency_code : ' USD' ,
80
- value : ' 0.02' ,
79
+ currency_code : " USD" ,
80
+ value : " 0.02" ,
81
81
} ,
82
82
} ,
83
83
] ,
84
84
} ;
85
85
86
86
const response = await fetch ( url , {
87
87
headers : {
88
- ' Content-Type' : ' application/json' ,
88
+ " Content-Type" : " application/json" ,
89
89
Authorization : `Bearer ${ accessToken } ` ,
90
90
// Uncomment one of these to force an error for negative testing (in sandbox mode only). Documentation:
91
91
// https://developer.paypal.com/tools/sandbox/negative-testing/request-headers/
92
92
// "PayPal-Mock-Response": '{"mock_application_codes": "MISSING_REQUIRED_PARAMETER"}'
93
93
// "PayPal-Mock-Response": '{"mock_application_codes": "PERMISSION_DENIED"}'
94
94
// "PayPal-Mock-Response": '{"mock_application_codes": "INTERNAL_SERVER_ERROR"}'
95
95
} ,
96
- method : ' POST' ,
96
+ method : " POST" ,
97
97
body : JSON . stringify ( payload ) ,
98
98
} ) ;
99
99
@@ -109,9 +109,9 @@ const captureOrder = async (orderID) => {
109
109
const url = `${ base } /v2/checkout/orders/${ orderID } /capture` ;
110
110
111
111
const response = await fetch ( url , {
112
- method : ' POST' ,
112
+ method : " POST" ,
113
113
headers : {
114
- ' Content-Type' : ' application/json' ,
114
+ " Content-Type" : " application/json" ,
115
115
Authorization : `Bearer ${ accessToken } ` ,
116
116
// Uncomment one of these to force an error for negative testing (in sandbox mode only). Documentation:
117
117
// https://developer.paypal.com/tools/sandbox/negative-testing/request-headers/
@@ -138,10 +138,10 @@ async function handleResponse(response) {
138
138
}
139
139
140
140
// render checkout page with client id & unique client token
141
- app . get ( '/' , async ( req , res ) => {
141
+ app . get ( "/" , async ( req , res ) => {
142
142
try {
143
143
const { jsonResponse } = await generateClientToken ( ) ;
144
- res . render ( ' checkout' , {
144
+ res . render ( " checkout" , {
145
145
clientId : PAYPAL_CLIENT_ID ,
146
146
clientToken : jsonResponse . client_token ,
147
147
} ) ;
@@ -150,26 +150,26 @@ app.get('/', async (req, res) => {
150
150
}
151
151
} ) ;
152
152
153
- app . post ( ' /api/orders' , async ( req , res ) => {
153
+ app . post ( " /api/orders" , async ( req , res ) => {
154
154
try {
155
155
// use the cart information passed from the front-end to calculate the order amount detals
156
156
const { cart } = req . body ;
157
157
const { jsonResponse, httpStatusCode } = await createOrder ( cart ) ;
158
158
res . status ( httpStatusCode ) . json ( jsonResponse ) ;
159
159
} catch ( error ) {
160
- console . error ( ' Failed to create order:' , error ) ;
161
- res . status ( 500 ) . json ( { error : ' Failed to create order.' } ) ;
160
+ console . error ( " Failed to create order:" , error ) ;
161
+ res . status ( 500 ) . json ( { error : " Failed to create order." } ) ;
162
162
}
163
163
} ) ;
164
164
165
- app . post ( ' /api/orders/:orderID/capture' , async ( req , res ) => {
165
+ app . post ( " /api/orders/:orderID/capture" , async ( req , res ) => {
166
166
try {
167
167
const { orderID } = req . params ;
168
168
const { jsonResponse, httpStatusCode } = await captureOrder ( orderID ) ;
169
169
res . status ( httpStatusCode ) . json ( jsonResponse ) ;
170
170
} catch ( error ) {
171
- console . error ( ' Failed to create order:' , error ) ;
172
- res . status ( 500 ) . json ( { error : ' Failed to capture order.' } ) ;
171
+ console . error ( " Failed to create order:" , error ) ;
172
+ res . status ( 500 ) . json ( { error : " Failed to capture order." } ) ;
173
173
}
174
174
} ) ;
175
175
0 commit comments