不幸的是,在这个庞大的社区中,似乎没有人有一个答案:(经过大量的努力,我能够实现一个肮脏的黑客攻击,如下所示,我知道这不是正确的解决方案。)我的问题与Implementation of Paypal in single page application的帖子中描述的完全相同。如果我的破解是正确的方式,或者可以更好地实现,请各位大师仔细思考这个问题,并回答/提供反馈。在我的脏话下面:(非常感谢你的回答/评论。
我有一个按钮说用paypal和onClick支付我打开一个新的窗口-> window.open("/paypalCreate", width = "20px", height = "20px");
,我在我的node.js服务器中捕获了这个get请求"/paypalCreate“,并调用下面看起来像liek的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窗口将被刷新,从而从DB获取付款详细信息,在这里您可以以任何您想要的方式处理SPA。我知道这是个卑劣的花招,但像你一样,我找不到更好的方法。请让我知道这是否对你有效,或者你是否找到了更好的方法来做这些事情。
干杯,奇丹
发布于 2014-12-22 16:17:52
尝尝这个。这是我在我的应用程序中使用的。
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模块将如下所示。此模块的文档可以在here中找到
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
},
至于重定向,你不需要发送你的用户到贝宝。如果成功,只需显示事务已完成消息/页面。在失败时显示错误并让他们修复它。
https://stackoverflow.com/questions/27595796
复制相似问题