【发布时间】:2018-05-21 16:37:13
【问题描述】:
我今天的问题是我有一个要执行快速结帐的 Web 应用程序。 我使用客户端 - 服务器架构来避免安全问题。 所以我的客户端代码如下:
paypal.Button.render({
env : 'sandbox',
commit: true,
style: {
size: 'medium',
color: 'gold',
shape: 'pill',
label: 'checkout'
},
locale: 'en_US',
payment: function() {
nickname = $("#nickname").val();
amount = $("#amount").val();
description = $("#description").val();
data = {
"nickname" : nickname,
"amount" : amount,
"description" : description
};
return new paypal.Promise(function(resolve, reject) {
// Call your server side to get the Payment ID from step 3, then pass it to the resolve callback
jQuery.post("/newPayment", data).done(function(data){
console.log("HEI I GOT THE PAYMENT JSON = " + data);
parsed_data = JSON.parse(data);
resolve(parsed_data['id']);
});
});
},
onAuthorize: function(data, actions) {
console.log("JSON = " + JSON.stringify(actions.payment.getTransactions()[0]));
console.log("TRANSACTION = " + actions.payment.getTransactions()[0])
console.log("PAYMENT SUCCESSFUL");
return actions.payment.execute();
},
onError: function(data, actions){
console.log("ERRORRRRRR");
$("#warning").show();
}
}, '#paypal-button');
我的服务器端代码:
app.post("/newPayment",function(req,res){
console.log("RECEIVING POST WITH AMOUNT = " + req.body.amount);
//CONFIGURE PAYMENY - PAYPAL PHASE 1
var first_config = {
'mode': 'sandbox',
'client_id': '<MY CLIENT ID>',
'client_secret': '<MY SECRET>'
};
paypal.configure(first_config);
console.log("AMOUNT AUTHORIZED = " + req.body.amount);
//CREATING PAYMENT
PAYMENT = {
"intent": "sale",
"payer": {
"payment_method": "paypal"
},
"redirect_urls": {
"return_url": "http://return.url",
"cancel_url": "http://cancel.url"
},
"transactions": [{
"amount": {
"currency": "EUR",
"total": req.body.amount
},
"description": "This is the payment description."
}]
};
//CREATING PAYMENT AND SENDING IT TO THE CLIENT
paypal.payment.create(PAYMENT, function (error, payment) {
if (error) {
throw error;
} else {
console.log("Create Payment Response");
res.send(JSON.stringify(payment));
}
});
现在,paypal 窗口打开,我可以插入所有数据来执行测试付款。 但是,当我确认付款并且 Paypal 服务应该调用我的“onAuthorize”方法时,会触发“onError”...
可能是什么问题? 重定向网址? 我应该在它之后执行 paypal.configure() 和 .then() 以确保操作流程的事实?
我真的很想使用 Paypal 服务
编辑: 将我的代码推送到 cloudno.de 支付正确执行,但在本地运行它不起作用...我不知道该说什么啊啊啊
【问题讨论】:
-
附带说明,您有防止篡改的方法吗?例如,是什么阻止了用户在控制台中更改付款金额然后提交表单?
-
感谢您的关注!实际上这不是完整的代码,只有在用户被授权的情况下,服务器端才会执行一些检查;)
标签: javascript node.js rest paypal express-checkout