贝宝与单页应用程序集成

我有完全相同的问题, 在单个页面的应用程序中的贝宝实施后所述不幸的是,在这个庞大的社区没有人似乎有一个答案:(有很多头撞我能够实施一个肮脏的黑客,如下所示,我知道这不是正确的解决scheme,任何一个大师善意地思考这个问题,并回答/给我们反馈,如果我的肮脏的黑客是正确的方式,或者它可以更好地实施在我的肮脏的黑客(非常感谢您的答案/注释。

我有一个button来说paypal与paypal和onClick我打开一个新窗口 – > window.open("/paypalCreate", width = "20px", height = "20px"); 我捕获这个获取请求“/ paypalCreate”在我的node.js服务器,并调用create方法,看起来像下面

 exports.create = function (req, res) { //Payment object var payment = { //fill details from DB }; //Passing the payment over to PayPal paypal.payment.create(payment, function (error, payment) { if (error) { console.log(error); } else { if (payment.payer.payment_method === 'paypal') { req.session.paymentId = payment.id; var redirectUrl; for (var i = 0; i < payment.links.length; i++) { var link = payment.links[i]; if (link.method === 'REDIRECT') { redirectUrl = link.href; } } res.redirect(redirectUrl); } } }); }; This redirects user to paypal and once user confirms or cancels payment, the redirect urls are called. And in the success redirect url I capture the payment details into the databse and render a html in this opened window with the confirmation. exports.execute = function (req, res) { var paymentId = req.session.paymentId; var payerId = req.param('PayerID'); // 1. if this is executed, then that means the payment was successful, now store the paymentId, payerId and token into the database // 2. At the close of the popup window open a confirmation for the reserved listing var details = {"payer_id": payerId}; paypal.payment.execute(paymentId, details, function (error, payment) { if (error) { console.log(error); } else { //res.send("Hell yeah!"); res.render('paypalSuccess', {payerId: payerId, paymentId: paymentId}); } }); }; 

一旦用户closures了正在处理PayPal的打开的窗口,原始SPA窗口将被刷新,从而从DB获得付款细节,在这里你可以以任何你想要的方式处理SPA。 我知道这是一个肮脏的黑客,但像你一样,我找不到更好的方法。 请让我知道这是否适合你,或者如果你find了一个更好的方法来做这件事。

欢呼声,奇丹

尝试这个。 这是我用我的应用程序。

 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 && error.response) }, "paypal payment response"); callback(error, response); }); } else { setImmediate(function () { callback(null, {"fakePaypal": "is fake"}); }); } } module.exports = pay; 

编辑:config3模块看起来像这样。 这个模块的文档可以在这里find

 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 }, 

至于redirect,你不需要发送你的用户贝宝。 成功后,只显示交易完成的信息/页面。 失败时显示错误,并让他们修复它。

Interesting Posts