PayPal onApprove not calling ajax function

Viewed 964

I'm trying to add a boolean to the database when the payment is successful.

  <script>
paypal.Buttons({
  createOrder: function(data, actions) {
    // This function sets up the details of the transaction, including the amount and line item details.
    return actions.order.create({
      purchase_units: [{
        amount: {
          value: '0.01'
        }
      }]
    });
  },
  onApprove: function(data, actions) {
    // This function captures the funds from the transaction.
    return actions.order.capture().then(function(details) {
      // This function shows a transaction success message to your buyer.
      alert('Transaction completed by ' + details.payer.name.given_name);
      $.ajax({
        url: "includes/payment_db.php",
        success: function(data) {
          alert('Successfully called');
        },
        error: function(jqxhr, status, exception) {
          alert('Exception:', exception);
        }
      });
    });
  }
}).render('#paypal-button-container');
The ajax function is not working. It doesn't show any error or success message.
1 Answers

This type of payment flow should not be used. Do not capture on the client side and then record information in your database.

Instead, use a proper server-side integration that captures directly from the server, so you have an immediate API response of success or failure, and are not dependent on the client to notify you of what happened.

To accomplish this, create two routes on your server, one for "Set Up Transaction" and one for "Capture Transaction", documented here: https://developer.paypal.com/docs/checkout/reference/server-integration/

For the customer's approval, pair the following JavaScript with the above two routes you created: https://developer.paypal.com/demo/checkout/#/pattern/server

Related