我在调试我的 paypal 与 Node 和其他 api 的集成时遇到问题 https://github.com/paypal/rest-api-sdk-nodejs
- 我创建了一个 Paypal 帐户
- 我得到了该应用程序的信用
- 我创建了一个应用程序并为用户设置了密码
- 我用那个用户登录沙箱
- 我没有看到任何交易历史
代码片段如下:
var config_opts = {
'host': 'api.sandbox.paypal.com',
'port': '',
'client_id': 'my id',
'client_secret': 'my secret'
};
var create_payment_json = {
"intent": "sale",
"payer": {
"payment_method": "paypal"
},
"redirect_urls": {
"return_url": "http://localhost:3000",
"cancel_url": "http://localhost:3000"
},
"transactions": [{
"amount": {
"currency": "USD",
"total": "10.00"
},
"description": "This is the payment description."
}]
};
paypal_sdk.payment.create(create_payment_json, config_opts, function (err, res) {
if (err) {
console.log( err );
}
if (res) {
console.log("Create Payment Response");
console.log(res);
}
});
我得到的响应如下:
{ id: 'PAY-55J119327J2030636KLXMVJI',
create_time: '2014-02-02T22:45:57Z',
update_time: '2014-02-02T22:45:57Z',
state: 'created',
intent: 'sale',
payer:
{ payment_method: 'paypal',
payer_info: { shipping_address: {} } },
transactions:
[ { amount: [Object],
description: 'This is the payment description.' } ],
links:
[ { href: 'https://api.sandbox.paypal.com/v1/payments/payment/PAY- 55J119327J2030636KLXMVJI',
rel: 'self',
method: 'GET' },
{ href: 'https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=EC-90D3081342725343U',
rel: 'approval_url',
method: 'REDIRECT' },
{ href: 'https://api.sandbox.paypal.com/v1/payments/payment/PAY-55J119327J2030636KLXMVJI/execute',
rel: 'execute',
method: 'POST' } ] }
我想知道是否有一些额外的步骤来验证我是否丢失了?
最佳答案
首先,您必须声明一个正确的 return_url(不仅仅是 localhost:3000)。我建议您将 return_url 更改为“http://localhost:3000/success”。然后,将此插入到您的付款 POST METHOD 上方:
app.get('/success', function(req, res) {
var paymentId = req.query.paymentId;
var payerId = { 'payer_id': req.query.PayerID };
paypal.payment.execute(paymentId, payerId, function(error, payment){
if(error){
console.error(error);
} else {
if (payment.state === 'approved'){
res.send('payment completed successfully');
console.log(payment);
} else {
res.send('payment not successful');
}
}
});
});
此 GET 方法将执行您的付款。但是,在执行之前,您必须首先将服务器的响应重定向到 paypal 沙箱以进行付款确认。
像这样编辑 paypal_sdk.payment.create() 函数的内容:
paypal_sdk.payment.create(create_payment_json, config_opts, function (err, res) {
if (err) {
console.log( err );
}
else {
console.log("Create Payment Response");
console.log(res);
//you forgot to redirect your response to paypal sandbox
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);
}
});
我希望这能有所帮助(尤其是对那些仍在寻找答案的人)
关于node.js - 在 Node 中使用 Paypal Rest API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21517386/