PayPal客户端休息:如何通知服务器



with paypal客户端rest:https://developer.paypal.com/demo/checkout/patern/pattern/pattern/client

PayPal如何将用户付款的服务器通知服务器,因此我可以进行一些后处理,写入数据库等。

结果,他们支持混合端,客户端对服务器端和VICE VERSA Integrations:https://github.com/paypal/paypal/paypal/paypal-checkout/blob/master/master/master/docs/docs/hhybrid.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');
        }
      }
    });

最新更新