【问题标题】:Using Paypal Rest API with Node将 Paypal Rest API 与 Node 一起使用
【发布时间】:2014-02-26 08:24:36
【问题描述】:

我在调试我的贝宝与 Node 和其余 api https://github.com/paypal/rest-api-sdk-nodejs 的集成时遇到问题

  • 我创建了一个 Paypal 帐户
  • 我获得了该应用的信用证
  • 我创建了一个应用并为用户设置了密码
  • 我使用该用户登录沙盒
  • 我没有看到任何交易记录

sn-p代码如下:

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' } ] }

我想知道是否有一些额外的步骤来验证我缺少的东西?

【问题讨论】:

  • 更新:我可以在developer.paypal.com 上看到我的交易,但在我的沙盒帐户中看不到。贝宝是否改变了沙盒的工作方式?

标签: node.js paypal paypal-sandbox


【解决方案1】:

首先,您必须声明一个正确的 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);
    }  
});

我希望这可以帮助(尤其是对那些仍在寻找答案的人)

【讨论】:

  • 请帮帮我,我设法执行了交易,临时个人帐户显示该交易,但商家帐户没有显示任何批准它的信息。临时帐户显示“这是一项临时授权,以确保您的付款方式能够支付付款。当jawad 商家的测试商店完成您的订单时,您的付款方式将被收取。”
  • 这是我的问题和代码的链接:stackoverflow.com/questions/52297388/…
【解决方案2】:

您必须转到仪表板 -> 休息 API 事务。

见附件截图:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-09-13
    • 1970-01-01
    • 2016-07-16
    • 2012-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-22
    相关资源
    最近更新 更多