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" ;
55
66const { 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" ;
88const 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" ) ) ;
1111
1212// parse post params sent in body in json format
1313app . use ( express . json ( ) ) ;
@@ -19,14 +19,14 @@ app.use(express.json());
1919const generateAccessToken = async ( ) => {
2020 try {
2121 if ( ! PAYPAL_CLIENT_ID || ! PAYPAL_CLIENT_SECRET ) {
22- throw new Error ( ' MISSING_API_CREDENTIALS' ) ;
22+ throw new Error ( " MISSING_API_CREDENTIALS" ) ;
2323 }
2424 const auth = Buffer . from (
25- PAYPAL_CLIENT_ID + ':' + PAYPAL_CLIENT_SECRET ,
26- ) . toString ( ' base64' ) ;
25+ PAYPAL_CLIENT_ID + ":" + PAYPAL_CLIENT_SECRET ,
26+ ) . toString ( " base64" ) ;
2727 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" ,
3030 headers : {
3131 Authorization : `Basic ${ auth } ` ,
3232 } ,
@@ -35,7 +35,7 @@ const generateAccessToken = async () => {
3535 const data = await response . json ( ) ;
3636 return data . access_token ;
3737 } catch ( error ) {
38- console . error ( ' Failed to generate Access Token:' , error ) ;
38+ console . error ( " Failed to generate Access Token:" , error ) ;
3939 }
4040} ;
4141
@@ -47,11 +47,11 @@ const generateClientToken = async () => {
4747 const accessToken = await generateAccessToken ( ) ;
4848 const url = `${ base } /v1/identity/generate-token` ;
4949 const response = await fetch ( url , {
50- method : ' POST' ,
50+ method : " POST" ,
5151 headers : {
5252 Authorization : `Bearer ${ accessToken } ` ,
53- ' Accept-Language' : ' en_US' ,
54- ' Content-Type' : ' application/json' ,
53+ " Accept-Language" : " en_US" ,
54+ " Content-Type" : " application/json" ,
5555 } ,
5656 } ) ;
5757
@@ -65,35 +65,35 @@ const generateClientToken = async () => {
6565const createOrder = async ( cart ) => {
6666 // use the cart information passed from the front-end to calculate the purchase unit details
6767 console . log (
68- ' shopping cart information passed from the frontend createOrder() callback:' ,
68+ " shopping cart information passed from the frontend createOrder() callback:" ,
6969 cart ,
7070 ) ;
7171
7272 const accessToken = await generateAccessToken ( ) ;
7373 const url = `${ base } /v2/checkout/orders` ;
7474 const payload = {
75- intent : ' CAPTURE' ,
75+ intent : " CAPTURE" ,
7676 purchase_units : [
7777 {
7878 amount : {
79- currency_code : ' USD' ,
80- value : ' 0.02' ,
79+ currency_code : " USD" ,
80+ value : " 0.02" ,
8181 } ,
8282 } ,
8383 ] ,
8484 } ;
8585
8686 const response = await fetch ( url , {
8787 headers : {
88- ' Content-Type' : ' application/json' ,
88+ " Content-Type" : " application/json" ,
8989 Authorization : `Bearer ${ accessToken } ` ,
9090 // Uncomment one of these to force an error for negative testing (in sandbox mode only). Documentation:
9191 // https://developer.paypal.com/tools/sandbox/negative-testing/request-headers/
9292 // "PayPal-Mock-Response": '{"mock_application_codes": "MISSING_REQUIRED_PARAMETER"}'
9393 // "PayPal-Mock-Response": '{"mock_application_codes": "PERMISSION_DENIED"}'
9494 // "PayPal-Mock-Response": '{"mock_application_codes": "INTERNAL_SERVER_ERROR"}'
9595 } ,
96- method : ' POST' ,
96+ method : " POST" ,
9797 body : JSON . stringify ( payload ) ,
9898 } ) ;
9999
@@ -109,9 +109,9 @@ const captureOrder = async (orderID) => {
109109 const url = `${ base } /v2/checkout/orders/${ orderID } /capture` ;
110110
111111 const response = await fetch ( url , {
112- method : ' POST' ,
112+ method : " POST" ,
113113 headers : {
114- ' Content-Type' : ' application/json' ,
114+ " Content-Type" : " application/json" ,
115115 Authorization : `Bearer ${ accessToken } ` ,
116116 // Uncomment one of these to force an error for negative testing (in sandbox mode only). Documentation:
117117 // https://developer.paypal.com/tools/sandbox/negative-testing/request-headers/
@@ -138,10 +138,10 @@ async function handleResponse(response) {
138138}
139139
140140// render checkout page with client id & unique client token
141- app . get ( '/' , async ( req , res ) => {
141+ app . get ( "/" , async ( req , res ) => {
142142 try {
143143 const { jsonResponse } = await generateClientToken ( ) ;
144- res . render ( ' checkout' , {
144+ res . render ( " checkout" , {
145145 clientId : PAYPAL_CLIENT_ID ,
146146 clientToken : jsonResponse . client_token ,
147147 } ) ;
@@ -150,26 +150,26 @@ app.get('/', async (req, res) => {
150150 }
151151} ) ;
152152
153- app . post ( ' /api/orders' , async ( req , res ) => {
153+ app . post ( " /api/orders" , async ( req , res ) => {
154154 try {
155155 // use the cart information passed from the front-end to calculate the order amount detals
156156 const { cart } = req . body ;
157157 const { jsonResponse, httpStatusCode } = await createOrder ( cart ) ;
158158 res . status ( httpStatusCode ) . json ( jsonResponse ) ;
159159 } 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." } ) ;
162162 }
163163} ) ;
164164
165- app . post ( ' /api/orders/:orderID/capture' , async ( req , res ) => {
165+ app . post ( " /api/orders/:orderID/capture" , async ( req , res ) => {
166166 try {
167167 const { orderID } = req . params ;
168168 const { jsonResponse, httpStatusCode } = await captureOrder ( orderID ) ;
169169 res . status ( httpStatusCode ) . json ( jsonResponse ) ;
170170 } 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." } ) ;
173173 }
174174} ) ;
175175
0 commit comments