Saturday, December 08 2018
In this article we have explained about How to integrate PayPal express checkout in PHP. PayPal Express Checkout payment gateway is the easiest way to integrate in PHP. There are two ways to integrate PayPal checkout payment gateway, Client side and server side integrations.
In server side integration means we have to use PayPal API to create and execute payment via API calls. In client side integration means PayPal provides javascript codes we have to update our credentials key, amount and product details. Once form submitted our data is processing to paypal server and returns the data to our success page.
Copy the below code and paste in your file as “checkout.php”
<div id="paypal-button"></div> <script src="https://www.paypalobjects.com/api/checkout.js"></script> <script> paypal.Button.render({ // Configure environment env: 'sandbox', client: { sandbox: 'demo_sandbox_client_id', production: 'demo_production_client_id' }, // Customize button (optional) locale: 'en_US', style: { size: 'small', color: 'gold', shape: 'pill', }, // Enable Pay Now checkout flow (optional) commit: true, // Set up a payment payment: function(data, actions) { return actions.payment.create({ transactions: [{ amount: { total: '0.01', currency: 'USD' } }] }); }, // Execute the payment onAuthorize: function(data, actions) { return actions.payment.execute().then(function() { // Show a confirmation message to the buyer //alert('Thank you for your purchase!'); $("#hid_payment_id").val(data.paymentID); $("#hid_token").val(data.paymentToken); $("#hid_payer_id").val(data.payerID); $("#success_form").submit(); }); } }, '#paypal-button'); </script>
HTML code
<form name="sucess_form" id="sucess_form" method="POST" action="success.php"> <input type="hidden" name="hid_payment_id" id="hid_payment_id"> <input type="hidden" name="hid_token" id="hid_token"> <input type="hidden" name="hid_payer_id" id="hid_payer_id"> </form>
Once payment has been successfully done and response will sent to success.php file. In this file we have to store the payment id and transaction id in the transaction history table.
Thanks for reading this article. If you have any queries and comment in the below.