繁体   English   中英

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

[英]Paypal client-side REST: how to notify server?

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

paypal如何通知服务器用户已支付,所以我可以做一些后处理,写入数据库等..是否有可以附加的webhook?

原来他们支持混合,客户端到服务器端和反之亦然集成: 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');
        }
      }
    });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM