node.js - 在 Node 中使用 Paypal Rest API

标签 node.js paypal paypal-sandbox

我在调试我的 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/

相关文章:

paypal - 如何在 VirtueMart 中设置 PayPal 支付方式以使用信用卡而不强制用户在 PayPal 注册?

php - 您没有进行此 API 调用的权限?使用 Ci-merchant 的 Paypal

android - 我不断收到验证错误 : client_id provided in the request does not match any of the registered clients

python - Paypal 在创建计费计划时给我一个 Required scope missing 错误

php - PayPal IPN 总是返回 "INVALID"

javascript - Node.js Readline 未写入输出文件

node.js - 如何保护 Mongoose/MongoDB 中的密码字段,以便在填充集合时它不会在查询中返回?

javascript - 如何理解对象中的第一个元素是否有效?

node.js - 发送 post 请求后 Express Router 重定向

drupal - 1 个固定产品的 CreditCart 付款(无 PayPal、无 Google Checkout、无 Ubercart)