Skip to content

Commit 70903e5

Browse files
style fixes and made URLs generic
1 parent 65d7f56 commit 70903e5

File tree

1 file changed

+50
-52
lines changed

1 file changed

+50
-52
lines changed

paypal/functions/index.js

+50-52
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ const admin = require('firebase-admin');
2424
admin.initializeApp(functions.config().firebase);
2525
// Configure your environment
2626
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"
3030
});
3131
/**
3232
* Expected in the body the amount
@@ -38,26 +38,24 @@ exports.pay = functions.https.onRequest((req, res) => {
3838
const payReq = JSON.stringify({
3939
intent: 'sale',
4040
payer: {
41-
payment_method: 'paypal'
41+
payment_method: 'paypal'
4242
},
43-
// example url https://us-central1-<project-id>.cloudfunctions.net/process
44-
// replace return_url, cancel_url
4543
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`
4846
},
4947
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
6159
}]
6260
});
6361
// 2.Initialize the payment and redirect the user.
@@ -69,10 +67,10 @@ exports.pay = functions.https.onRequest((req, res) => {
6967
} else {
7068
// Capture HATEOAS links
7169
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+
};
7674
});
7775
// If redirect url present, redirect user
7876
if (links.hasOwnProperty('approval_url')) {
@@ -90,33 +88,33 @@ exports.pay = functions.https.onRequest((req, res) => {
9088

9189
// 3.Complete the payment. Use the payer and payment IDs provided in the query string following the redirect.
9290
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+
});
122120
});

0 commit comments

Comments
 (0)