- Notifications
You must be signed in to change notification settings - Fork 3.9k
Add paypal-rest-sdk sample #151
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add paypal-rest-sdk sample #151
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks so much for this sample @Kevded , it's a nice one! I have added a few comments, would be nice if you could resolve them!
Cheers!
paypal/functions/index.js Outdated
// Configure your environment | ||
paypal.configure({ | ||
'mode': 'sandbox', // sandbox or live | ||
'client_id': 'your_client_id', // replace your_client_id |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you replace 'your_cliend_id'
by functions.config().paypal.client_id
and add instrucitons to set this environment variable in the README (i.e. users have to run: firebase functions:config:set paypal.client_id="yourPaypalClientID" paypal.client_secret="yourPaypalClientSecret"
)?
paypal/functions/index.js Outdated
paypal.configure({ | ||
'mode': 'sandbox', // sandbox or live | ||
'client_id': 'your_client_id', // replace your_client_id | ||
'client_secret': 'your_client_secret' // replace your_client_secret |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you replace 'your_cliend_id'
by functions.config().paypal.client_secret
?
paypal/functions/index.js Outdated
res.setHeader('Content-Type', 'application/json'); | ||
res.setHeader('Access-Control-Allow-Origin', '*'); | ||
res.setHeader('Access-Control-Allow-Headers', 'application/json, Content-Type'); | ||
res.setHeader('Access-Control-Allow-Methods', 'POST'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you use the cors
ExpressJS middleware here instead of setting all the headers manually? See an example here: https://github.com/firebase/functions-samples/blob/master/quickstarts/time-server/functions/index.js#L57
'client_secret': 'your_client_secret' // replace your_client_secret | ||
}); | ||
| ||
exports.pay = functions.https.onRequest((req, res) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add some comment above explaining what the Functions does?
paypal/functions/index.js Outdated
// example url https://us-central1-<project-id>.cloudfunctions.net/process | ||
// replace return_url, cancel_url | ||
redirect_urls: { | ||
return_url: 'https://us-central1-<project-id>.cloudfunctions.net/process', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can get the project ID from: process.env.GCLOUD_PROJECT
so please replace with:
return_url: `https://us-central1-${process.env.GCLOUD_PROJECT}.cloudfunctions.net/process`,
paypal/functions/index.js Outdated
payer_id: req.query.PayerID | ||
}; | ||
| ||
paypal.payment.execute(paymentId, payerId, function (error, payment) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use an arrow function here :)
paypal/functions/index.js Outdated
| ||
paypal.payment.execute(paymentId, payerId, function (error, payment) { | ||
if (error) { | ||
console.error(JSON.stringify(error)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just console.error(error);
paypal/functions/index.js Outdated
paypal.payment.execute(paymentId, payerId, function (error, payment) { | ||
if (error) { | ||
console.error(JSON.stringify(error)); | ||
res.redirect('http://localhost:4200/error'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Change that URL to a production URL ?
paypal/functions/index.js Outdated
} else { | ||
console.warn('payment.state: not approved ?'); | ||
// replace debug url | ||
res.redirect('https://console.firebase.google.com/project/<project-id>/functions/logs?search=&severity=DEBUG'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can replace the project ID automatically here as shown above.
}); | ||
| ||
// Complete the payment | ||
exports.process = functions.https.onRequest((req, res) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add a little bit more details in the comment here to explain a bit more what this function does?
ping :) |
I work on it. |
Thanks @Kevded I've updated the README with more detailed step-by-step instructions. Do you think it would be possible for you to also commit a small web UI like the one you showed in the animated GIF? this would make the sample so much more complete! PS if you do don't forget about this feature: https://firebase.google.com/docs/hosting/functions this will allow you to have the Paypal Cloud Function on the same domain as the web UI. |
I tried to pass the uid value via the optional "custom" field in the "transaction" object as in paypal https://developer.paypal.com/docs/api/payments/#definition-transaction, but this does not work, so I have passed through the "description" field. |
paypal/functions/.eslintrc.js Outdated
@@ -0,0 +1,3 @@ | |||
module.exports = { | |||
"extends": "google" | |||
}; No newline at end of file |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you remove that file fro the PR?
paypal/functions/.eslintrc.json Outdated
"constructor-super": "warn", | ||
"valid-typeof": "warn" | ||
} | ||
} No newline at end of file |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you remove that file from the PR ?
paypal/functions/.jshintrc Outdated
| ||
// Custom Globals | ||
"globals" : {} // additional predefined global variables | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you remove that file from the PR?
paypal/functions/=3.8.1 Outdated
paypal-functions@ /home/dsv/WebstormProjects/functions-samples/paypal/functions | ||
├── eslint@4.1.1 | ||
└── eslint-config-google@0.8.0 | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you remove that file from the PR?
@@ -0,0 +1,102 @@ | |||
'use strict'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add a LICNCE header here please? Example: https://github.com/firebase/functions-samples/blob/master/assistant-say-number/functions/index.js#L1-L16
'client_secret': functions.config().paypal.client_secret // run: firebase functions:config:set paypal.client_secret="yourPaypalClientSecret" | ||
}); | ||
| ||
exports.pay = functions.https.onRequest((req, res) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add a commenbt describing what the function does, what is expected as body in the request etc...
paypal/functions/index.js Outdated
| ||
exports.pay = functions.https.onRequest((req, res) => { | ||
// Dev | ||
cors(req, res, () => {}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove?
paypal/functions/index.js Outdated
} | ||
} | ||
}); | ||
}); No newline at end of file |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add line at the end of the file.
paypal/functions/package.json Outdated
"description": "Paypal Firebase Functions", | ||
"dependencies": { | ||
"cors": "^2.8.3", | ||
"eslint": "^4.1.1", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you remove eslint stuff from dependency and devDependency?
paypal/public/success.html Outdated
<a disabled class="mdl-button mdl-js-button mdl-button--raised" href="/">Home</a> | ||
</body> | ||
| ||
</html> No newline at end of file |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you please add a trailing line at the end of the file ? :)
paypal/public/index.html Outdated
</div> | ||
</body> | ||
| ||
</html> No newline at end of file |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a line at the end of the file.
I added a few style fixes and we should be all set :) |
Use Paypal-rest-sdk on firebase functions
paypal-rest-sdks