× {{alert.msg}} Never ask again
Get notified about new tutorials RECEIVE NEW TUTORIALS

paypal integration with single page app

Ben Halverson
Jul 06, 2015
<p>Try this. It's what I use for my app. </p> <pre><code>var config = require("config3"); var paypal_api = require("paypal-rest-sdk"); paypal_api.configure(config.paypal); var log = require("app/log"); function pay(creditCard, amount, description, callback) { var paypalOptions = { intent: "sale", payer: { payment_method: "credit_card", funding_instruments: [{credit_card: creditCard}] }, transactions: [{ amount: { total: amount, currency: "USD" }, description: description }] }; if (config.paypal.enabled) { paypal_api.payment.create(paypalOptions, function (error, response) { log.debug({ err: error, response: response || (error &amp;&amp; error.response) }, "paypal payment response"); callback(error, response); }); } else { setImmediate(function () { callback(null, {"fakePaypal": "is fake"}); }); } } module.exports = pay; </code></pre> <p>Edit: The config3 module would look like this. The docs for this module can be found <a href="https://github.com/focusaurus/config3" rel="nofollow">here</a></p> <pre><code>module.exports = { paypal: { client_id: "Secret API key", client_secret: "Secret API key", host: "api.sandbox.paypal.com", enabled: true }, mysql: { host: 'localhost', user: 'root', password: '', database: '' }, redis: { host: "localhost", port: 6379 }, </code></pre> <p>As far as redirection you don't need to send your user to Paypal. On success just show a transaction completed message / page. On failure show the error and let them fix it.</p> <p>This tip was originally posted on <a href="http://stackoverflow.com/questions/27595796/paypal%20integration%20with%20single%20page%20app/27598911">Stack Overflow</a>.</p>

Get New Tutorials Delivered to Your Inbox

New tutorials will be sent to your Inbox once a week.

comments powered by Disqus