Paypal Smart Payment throwing error but still processing payment

Viewed 750

I have a website for a client and they requested debit/credit payments on their site, I've set up the smart payment settings and it seems to work ok on the site, however, I just added a function to retrieve a variable (price) from the URL the smart buttons are throwing an error code and im assuming its because of the javascript portion to get the amount variable from the URL However the payments are processing as they should regardless of the error code.

Below is the sample of my code

function getQueryVariable(variable)
{
       var query = window.location.search.substring(1);
       var vars = query.split("&");
       for (var i=0;i<vars.length;i++) {
               var pair = vars[i].split("=");
               if(pair[0] == variable){return pair[1];}
       }
       return(false);
}
var sel = getQueryVariable("price");

  paypal.Buttons({
    createOrder: function(data, actions) {
      return actions.order.create({
        purchase_units: [{
          amount: {
            value: sel
          }
        }]
      });
    },
    onApprove: function(data, actions) {
      return actions.order.capture().then(function(details) {
        alert('Transaction completed by ' + details.payer.name.given_name);
      });
    }
  }).render('#paypal-button-container');

1 Answers

Try doing a window.location.href redirect on success instead of displaying the alert, and all should be well

Alternatively you can save a reference to the buttons before rendering, then close them on success and update the page with whatever success message.

Something like (untested) :

  window.myButtons = paypal.Buttons({
    createOrder: function(data, actions) {
      return actions.order.create({
        purchase_units: [{
          amount: {
            value: sel
          }
        }]
      });
    },
    onApprove: function(data, actions) {
      return actions.order.capture().then(function(details) {
        alert('Transaction completed by ' + details.payer.name.given_name);
        window.myButtons.close();
        document.getElementById('paypal-button-container').innerHTML = '<pre>' + JSON.stringify(details, null, 4) + '</pre>';
      });
    }
  });
  window.myButtons.render('#paypal-button-container');
Related