【发布时间】:2015-02-20 03:29:27
【问题描述】:
我遇到了与Implementation of Paypal in single page application 帖子中描述的完全相同的问题,不幸的是,这个庞大的社区中似乎没有人有答案:( 通过大量的头部撞击,我能够实现如下肮脏的黑客攻击,我知道这不是正确的解决方案。如果我的肮脏黑客是正确的方法或者可以更好地实施,任何大师都可以好好思考这个问题并回答/提供反馈。在我肮脏的黑客下面:( 非常感谢您的回答/cmets。
我有一个按钮可以用 paypal 和 onClick 我打开一个新窗口 -> window.open("/paypalCreate", width = "20px", height = "20px");
我在我的 node.js 服务器中捕获了这个获取请求“/paypalCreate”并调用了如下所示的 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});
}
});
};
一旦用户关闭处理 paypal 的打开窗口,原始 SPA 窗口将被刷新,从而从数据库中获取付款详细信息,您可以在此处以任何您想要的方式处理 SPA。 我知道这是一个肮脏的黑客,但像你一样我找不到更好的方法。如果这对您有用,或者您是否找到了更好的方法,请告诉我。
干杯, 赤胆
【问题讨论】:
标签: angularjs node.js backbone.js paypal single-page-application