1

I have a website where the user fills in a form, and when they submit, my site inserts the info into a mySql database, then the user makes the payment using a PayPal button. After the sale is complete, Paypal returns the user to example.com/thank-you?id=XXX and the details are extracted from the mySql database and sent to the email.

The problem is that the database does not have any information about whether they actually completed the payment. The customer can go directly to the Thankyou page by entering the URL (along with the order number). I want the thankyou page to load only when that order is marked as completed in the database.

Is there anything I can insert in the Paypal button code to update the order in my database with a "paid" flag?

This is my PayPal smart button code:

  <script
    src="https://www.paypal.com/sdk/js?client-id=sb"> // Required. Replace SB_CLIENT_ID with your sandbox client ID.
  </script> 
      <div id="paypal-button-container" id="contine_btn"></div>
    <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: [{
          invoice_id: '<?php echo $order_id; ?>',
          description: '<?php echo $details['service']; ?>',
          amount: {
            value: '<?php echo $details['cost']; ?>'
          }
        }]
      });
    },
    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.
        actions.redirect('<?php echo 'https://www.example.com/thank-you/?id='.$order_id; ?>');
      });
    }
  }).render('#paypal-button-container');
</script>
Shadow
  • 33,525
  • 10
  • 51
  • 64
  • `onApprove` means the customer has approved the transaction. After that, it's up to you to capture the payment. – aynber May 16 '23 at 17:53
  • What you need is webhooks https://developer.paypal.com/api/rest/webhooks/. After sucessfull payment PayPal will send a POST request to configured URL on your site, than you need to validate it and do something with your business logic – lezhni May 16 '23 at 18:17
  • @RiggsFolly Thats a link on official PayPal docs, I see no problem with it – lezhni May 16 '23 at 18:17
  • if you need to show in the browser when paypal has actually processed the payment, you need to go into a loop (either with a meta http-equiv="refresh" tag or javascript) waiting for paypal to send you the webhook/ipn message. if you just need to do something when they have actually paid (update the order, send an email, etc), you can just do that in response to the message from paypal. – ysth May 16 '23 at 20:08
  • This question is essentially a duplicate of https://stackoverflow.com/questions/73532030/paypal-checkout-write-to-database-onapprove (among others, that's the one I found) – Preston PHX May 16 '23 at 22:23

0 Answers0