Paypal client-side REST: how to notify server?

With Paypal Client-Side REST: https://developer.paypal.com/demo/checkout/#/pattern/client

How does paypal notify server that a user has paid, so I can do some post-processing, write to database, etc.. is there a webhook i can attach?

Answers:

Answer

Turns out they support hybrid, client-side to server-side and vice versa integrations: https://github.com/paypal/paypal-checkout/blob/master/docs/hybrid.md

With that you can create a payment on the client side, and using the server side SDK (paypal-rest-sdk), execute it on the server side.

Client:

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');

Server:

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');
        }
      }
    });

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.