簡體   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