Paypal客户端REST:如何通知服务器?

时间:2017-05-31 05:02:13

标签: javascript node.js paypal paypal-sandbox paypal-rest-sdk

使用Paypal客户端REST:https://developer.paypal.com/demo/checkout/#/pattern/client

paypal如何通知服务器用户已付款,因此我可以进行一些后期处理,写入数据库等。是否有可以附加的webhook?

1 个答案:

答案 0 :(得分:0)

原来他们支持混合,客户端到服务器端和反之亦然集成:https://github.com/paypal/paypal-checkout/blob/master/docs/hybrid.md

使用它可以在客户端创建付款,并使用服务器端SDK(paypal-rest-sdk),在服务器端执行它。

客户:

paypal.Button.render({
  env: 'sandbox', // Or 'production',
  commit: true, // Show a 'Pay Now' button
  client: {
    sandbox: CLIENT_ID,
    production: CLIENT_ID
  },
  payment: function(data, actions) {        
    return actions.payment.create({
      ...
    });
  },
  onAuthorize: function(data, actions) {
    $.ajax({
      type: 'POST',
      url: 'http://potato-03.ea.com/paypal/payment/execute',
      dataType: 'json',
      contentType: "application/json",
      data: JSON.stringify({
        payment_id: data.paymentID,
        payer_id: data.payerID
      })     
    }).done(function(data) {
      console.log('Payment received!');
    });
  },
  onCancel: function(data) {
    console.log('The payment was cancelled!');
  }
}, '#paypal-button');

服务器:

const paypal = require('paypal-rest-sdk');

let paymentId = req.params.payment_id;
let payerId = { payer_id: req.params.payer_id };

paypal.payment.execute(paymentId, payerId, function(err, payment){
      if(err){
        console.error(JSON.stringify(err));
        return error.errorHandler(err, null, null, reject, null);
      } else {
        if (payment.state == 'approved'){
          console.log('payment completed successfully');
        } else {
          console.log('payment not successful');
        }
      }
    });