2017-05-31 67 views
1

使用PayPal客户端REST:https://developer.paypal.com/demo/checkout/#/pattern/client贝宝客户端REST:如何通知服务器?

如何贝宝通知用户已支付的服务器,这样我就可以做一些后期处理,写入数据库等..有一个网络挂接我可以附加?

+0

可以使用** ** io.emit发出的插座,在你的控制器创建** ** socket.on事件来处理您的呼叫。 – user100693

+0

@ user100693是什么? –

+0

您可以使用套接字事件。 https://www.html5rocks.com/en/tutorials/frameworks/angular-websockets/ – user100693

回答

0

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

这样,您可以创建在客户端的支付,并使用服务器端的SDK(贝宝休息 - 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'); 
     } 
     } 
    });