@@ -24,9 +24,9 @@ const admin = require('firebase-admin');
24
24
admin . initializeApp ( functions . config ( ) . firebase ) ;
25
25
// Configure your environment
26
26
paypal . configure ( {
27
- ' mode' : 'sandbox' , // sandbox or live
28
- ' client_id' : functions . config ( ) . paypal . client_id , // run: firebase functions:config:set paypal.client_id="yourPaypalClientID"
29
- ' client_secret' : functions . config ( ) . paypal . client_secret // run: firebase functions:config:set paypal.client_secret="yourPaypalClientSecret"
27
+ mode : 'sandbox' , // sandbox or live
28
+ client_id : functions . config ( ) . paypal . client_id , // run: firebase functions:config:set paypal.client_id="yourPaypalClientID"
29
+ client_secret : functions . config ( ) . paypal . client_secret // run: firebase functions:config:set paypal.client_secret="yourPaypalClientSecret"
30
30
} ) ;
31
31
/**
32
32
* Expected in the body the amount
@@ -38,26 +38,24 @@ exports.pay = functions.https.onRequest((req, res) => {
38
38
const payReq = JSON . stringify ( {
39
39
intent : 'sale' ,
40
40
payer : {
41
- payment_method : 'paypal'
41
+ payment_method : 'paypal'
42
42
} ,
43
- // example url https://us-central1-<project-id>.cloudfunctions.net/process
44
- // replace return_url, cancel_url
45
43
redirect_urls : {
46
- return_url : `https ://us-central1- ${ process . env . GCLOUD_PROJECT } .cloudfunctions.net /process` , //
47
- cancel_url : 'http ://localhost:5000/cancel' // replace with your app url
44
+ return_url : `${ req . protocol } ://${ req . get ( 'host' ) } /process` ,
45
+ cancel_url : ` ${ req . protocol } ://${ req . get ( 'host' ) } /cancel`
48
46
} ,
49
47
transactions : [ {
50
- amount : {
51
- total : req . body . price ,
52
- currency : 'USD'
53
- } ,
54
- // This is the payment transaction description. Maximum length: 127
55
- description : req . body . uid , // req.body.id
56
- // reference_id string .Optional. The merchant-provided ID for the purchase unit. Maximum length: 256.
57
- // reference_id: req.body.uid,
58
- custom : req . body . uid ,
59
- // soft_descriptor: req.body.uid
60
- // "invoice_number": req.body.uid,A
48
+ amount : {
49
+ total : req . body . price ,
50
+ currency : 'USD'
51
+ } ,
52
+ // This is the payment transaction description. Maximum length: 127
53
+ description : req . body . uid , // req.body.id
54
+ // reference_id string .Optional. The merchant-provided ID for the purchase unit. Maximum length: 256.
55
+ // reference_id: req.body.uid,
56
+ custom : req . body . uid ,
57
+ // soft_descriptor: req.body.uid
58
+ // "invoice_number": req.body.uid,A
61
59
} ]
62
60
} ) ;
63
61
// 2.Initialize the payment and redirect the user.
@@ -69,10 +67,10 @@ exports.pay = functions.https.onRequest((req, res) => {
69
67
} else {
70
68
// Capture HATEOAS links
71
69
payment . links . forEach ( ( linkObj ) => {
72
- links [ linkObj . rel ] = {
73
- href : linkObj . href ,
74
- method : linkObj . method
75
- } ;
70
+ links [ linkObj . rel ] = {
71
+ href : linkObj . href ,
72
+ method : linkObj . method
73
+ } ;
76
74
} ) ;
77
75
// If redirect url present, redirect user
78
76
if ( links . hasOwnProperty ( 'approval_url' ) ) {
@@ -90,33 +88,33 @@ exports.pay = functions.https.onRequest((req, res) => {
90
88
91
89
// 3.Complete the payment. Use the payer and payment IDs provided in the query string following the redirect.
92
90
exports . process = functions . https . onRequest ( ( req , res ) => {
93
- const paymentId = req . query . paymentId ;
94
- const payerId = {
95
- payer_id : req . query . PayerID
96
- } ;
97
- paypal . payment . execute ( paymentId , payerId , function ( error , payment ) {
98
- if ( error ) {
99
- console . error ( error ) ;
100
- res . redirect ( 'http ://localhost:5000 /error' ) ; // replace with your url page error
101
- } else {
102
- if ( payment . state === 'approved' ) {
103
- console . info ( 'payment completed successfully, description: ' , payment . transactions [ 0 ] . description ) ;
104
- // console.info('req.custom: : ', payment.transactions[0].custom);
105
- // set paid status to True in RealTime Database
106
- const date = Date . now ( ) ;
107
- const uid = payment . transactions [ 0 ] . description ;
108
- const ref = admin . database ( ) . ref ( 'users/' + uid + '/' ) ;
109
- ref . push ( {
110
- 'paid' : true ,
111
- // 'description': description,
112
- 'date' : date
113
- } ) . then ( r => console . info ( 'promise: ' , r ) ) ;
114
- res . redirect ( 'http ://localhost:5000 /success' ) ; // replace with your url, page success
115
- } else {
116
- console . warn ( 'payment.state: not approved ?' ) ;
117
- // replace debug url
118
- res . redirect ( `https://console.firebase.google.com/project/${ process . env . GCLOUD_PROJECT } /functions/logs?search=&severity=DEBUG` ) ;
119
- }
120
- }
121
- } ) ;
91
+ const paymentId = req . query . paymentId ;
92
+ const payerId = {
93
+ payer_id : req . query . PayerID
94
+ } ;
95
+ paypal . payment . execute ( paymentId , payerId , ( error , payment ) => {
96
+ if ( error ) {
97
+ console . error ( error ) ;
98
+ res . redirect ( ` ${ req . protocol } ://${ req . get ( 'host' ) } /error` ) ; // replace with your url page error
99
+ } else {
100
+ if ( payment . state === 'approved' ) {
101
+ console . info ( 'payment completed successfully, description: ' , payment . transactions [ 0 ] . description ) ;
102
+ // console.info('req.custom: : ', payment.transactions[0].custom);
103
+ // set paid status to True in RealTime Database
104
+ const date = Date . now ( ) ;
105
+ const uid = payment . transactions [ 0 ] . description ;
106
+ const ref = admin . database ( ) . ref ( 'users/' + uid + '/' ) ;
107
+ ref . push ( {
108
+ 'paid' : true ,
109
+ // 'description': description,
110
+ 'date' : date
111
+ } ) . then ( r => console . info ( 'promise: ' , r ) ) ;
112
+ res . redirect ( ` ${ req . protocol } ://${ req . get ( 'host' ) } /success` ) ; // replace with your url, page success
113
+ } else {
114
+ console . warn ( 'payment.state: not approved ?' ) ;
115
+ // replace debug url
116
+ res . redirect ( `https://console.firebase.google.com/project/${ process . env . GCLOUD_PROJECT } /functions/logs?search=&severity=DEBUG` ) ;
117
+ }
118
+ }
119
+ } ) ;
122
120
} ) ;
0 commit comments