mageDev0688
mageDev0688

Reputation: 566

How to get IPN status after success full payment in paypal express checkout using checkout.js

I have implemented the new paypal express checkout. I want to pass my success_ipn.php URL and cancelled.php URL to checkout.js.

I have done lots of google and as well as official doc Paypal DOC.

Below is my file code:

<!DOCTYPE html>

<head>
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <script src="https://www.paypalobjects.com/api/checkout.js"></script>
</head>

<body>
    <div id="paypal-button-container"></div>

    <script>
        var EXECUTE_PAYMENT_URL = 'http://example.com/success_ipn.php';
        paypal.Button.render({

            env: 'sandbox', // sandbox | production

            // PayPal Client IDs - replace with your own
            // Create a PayPal app: https://developer.paypal.com/developer/applications/create
            client: {
                sandbox:    'My Sandbox Client ID here.',
                //production: '<insert production client id>'
            },

            // Show the buyer a 'Pay Now' button in the checkout flow
            commit: true,

            // payment() is called when the button is clicked
            payment: function(data, actions) {

                // Make a call to the REST api to create the payment
                return actions.payment.create({
                    payment: {
                        transactions: [
                            {
                                amount: { total: '0.01', currency: 'USD' }
                            }
                        ]
                    }
                });
            },

            // onAuthorize() is called when the buyer approves the payment
            onAuthorize: function(data, actions) {
                return paypal.request.post(EXECUTE_PAYMENT_URL, {
                    paymentID: data.paymentID,
                    payerID:   data.payerID
                }).then(function() {
                        window.alert('Payment Complete!');
                });
            }

        }, '#paypal-button-container');

    </script>
</body>
</html>

It's working fine, but how I can pass my IPN file URL and Cancel URL.

After do some more google on this I can get only below variables not transactions ID etc.:

paymentID: data.paymentID,
payerID:   data.payerID

Please help me out this guys.

Upvotes: 0

Views: 784

Answers (1)

mageDev0688
mageDev0688

Reputation: 566

After the tried too and do google on this I have found the solution.

The paypal has deprecated the Express Checkout - NVP/SOAP after the checckout.js newest version. So, GetExpressCheckoutDetails and DoExpressCheckoutPayment is deprecated as well.

Now, all work will be done on the Rest API.

Below is update code how we can get the Payer information and transaction details in json format. After get the json response we can update our database record as well using aJax.

<script src="https://www.paypalobjects.com/api/checkout.js"></script>
  <div id="paypal-button-container"></div>

<script>
  var EXECUTE_PAYMENT_URL = 'http://example.com/success_checkout.php';

  paypal.Button.render({

      env: 'sandbox', // sandbox | production

      // PayPal Client IDs - replace with your own
      // Create a PayPal app: https://developer.paypal.com/developer/applications/create
      client: {
          sandbox:    '',
          //production: '<insert production client id>'
      },

      // Show the buyer a 'Pay Now' button in the checkout flow
      commit: true,

      // payment() is called when the button is clicked
      payment: function(data, actions) {

          // Make a call to the REST api to create the payment
          return actions.payment.create({
              payment: {
                  transactions: [
                      {
                          amount: { total: '10', currency: 'USD' },
                          custom: 'custom value here'
                      }
                  ]
              }
          });
      },
      onAuthorize: function(data, actions) {
        return actions.payment.get().then(function(payment) {
            //debugger;
            console.log(payment);
            var txn_id = payment.cart;
            var bookID = payment.transactions[0].custom;
            var currency = payment.transactions[0].amount["currency"];
            var amount = payment.transactions[0].amount["total"];
            var payerID = payment.payer.payer_info["payer_id"];
            var pstatus = payment.payer.status;

            var successUrl = EXECUTE_PAYMENT_URL+'?txn_id='+txn_id+'&bookID='+bookID+'&currency='+currency+'&amount='+amount+'&payerID='+payerID+'&pstatus='+pstatus;
            //console.log(newUrl);
           window.location.replace(successUrl);
        });
      },
      onCancel: function(data, actions) {
        var cancelUrl = "http://example.com/cancelled.php";
        //console.log(newUrl);
        window.location.replace(cancelUrl);
      }    

  }, '#paypal-button-container');

</script>

And you will get response in the success_checkout.php like below:

<?php
echo '<pre>' print_r($_REQUEST); 

// do your stuff here

header('Location: thanks.php');
?>

Hope this will help to other developers.

Upvotes: 1

Related Questions