Seamless E-commerce: PayPal Integration With JavaScript & PHP

In this blog post, I will provide you with a step-by-step guide on how to seamlessly integrate the PayPal payment gateway into your website using PHP. PayPal is a widely trusted and extensively used payment solution, making it an ideal choice for e-commerce websites and online businesses. By following these simple instructions, you can enable secure and convenient online transactions for your customers, thereby enhancing the user experience and boosting sales.


PayPal is a widely-used online payment processing platform that enables businesses and individuals to send and receive payments securely over the internet. Integrating PayPal with your PHP web application allows you to accept payments from customers using various methods such as credit/debit cards, PayPal accounts, and more.


To integrate PayPal into your JavaScript application, you'll typically use the PayPal REST API, which provides a set of HTTP endpoints for processing payments, managing orders, handling refunds, and more. PayPal also offers an official JavaScript  SDK that simplifies the integration process by providing pre-built methods and classes for interacting with the API.

If you don't already have one, sign up for a PayPal Business account on the PayPal Developer Dashboard. This account will allow you to access the necessary tools and credentials for integrating PayPal into your application.




Sign Up for a PayPal Business Account

You need to sign up for a PayPal Business account if you haven't already done so. This account will give you access to the necessary tools and APIs for accepting payments on your website. Simply visit the PayPal website and follow the instructions to create your account.

Implement 

<div id="paypal-button-container"></div>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script src="https://www.paypal.com/sdk/js?client-id={CLINTID}&currency=USD">
   // Required. Replace YOUR_CLIENT_ID with your sandbox client ID.
   </script>
<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({
   
               intent: "CAPTURE",
   
               application_context: {
   
                   brand_name: "Manna Web",
   
                   user_action: "PAY_NOW",
   
                   shipping_preference: "NO_SHIPPING",
   
                   //return_url:"http://localhost/return.php",
   
                   //cancel_url:"http://localhost/cancel.php",
   
                   payment_method: {
   
                       payer_selected: "PAYPAL",
   
                       payee_preferred: "IMMEDIATE_PAYMENT_REQUIRED"
   
                   }
   
               },
   
               payer: {
   
                   name: {
   
                       given_name: "Your firstname",
   
                       surname: "Your lastname"
   
                   },
   
                   email_address: "email adderess",
   
                   phone: {
   
                       phone_number: {
   
                           national_number: "002157788"
   
                       }
   
                   }
   
               },
   
               purchase_units: [{
   
                   invoice_id: "<?php echo rand(11111111,99999999); ?>",
   
                   amount: {
   
                       currency_code: 'USD',
   
                       value: '10',
   
                   },
   
                   item: [{
   
                       name: "Product Name",
   
                       description: "Product Description",
   
                       sku: "sku01",
   
                       unit_amount: {
   
                           currency_code: "USD",
   
                           value: "10",
   
                       },
   
                       quantity: "1",
   
                       category: "Product Category"
   
                   }]
   
               }],
   
               description: "transaction Description"
   
           });
   
       },
   
       onApprove: function(data, actions) {
   
           return actions.order.capture().then(function(details) {
   
               console.log(details);
   
               if(details.purchase_units[0].payments.captures[0].status) {
   
                   $.ajax({
   
                       type: "POST",
   
                       url: 'gettxn.php',
   
                       data: details,
   
                       dataType: "json",
   
                       complete: function(response, textStatus) {
   
                           //return window.location.href = "success.php";
   
                       }
   
                   });
   
               }
   
           }).then(function(res) {
   
               return res.json();
   
           }).then(function(captureData) {
   
               console.log(captureData);
   
               if(captureData.error === 'INSTRUMENT_DECLINED'); // Your server response structure and key names are what you choose
   
               return actions.restart();
   
           });
   
       }
   
   }).render('#paypal-button-container');
   
</script>


After a customer completes a payment on your website, PayPal will send a notification to a specified URL on your server. You need to set up a listener script on your server to handle these notifications and update your database or trigger any necessary actions, such as sending a confirmation email or updating the order status.


Response 

Create gettxn.php file and get response

<?php
  echo $_POST;
?>





Integrating PayPal payment gateway into your website using PHP is a straightforward process that can significantly enhance your website's functionality and user experience. By following the steps outlined in this guide and leveraging the power of PayPal's robust APIs and SDKs, you can securely accept payments online and streamline your e-commerce operations. Start integrating PayPal today and unlock the full potential of your online business.



Previous Post Next Post

Contact Form