PHP Shopping Cart with PayPal Payment Gateway Integration

by Vincy. Last modified on September 27th, 2022.

Shopping cart checkout with payment gateway integration (PayPal) is a most wanted article for the PHP shopping cart coders. In this article, we are going to integrate PayPal payment gateway with the shopping cart.

In a previous tutorial, we have seen how to add a product to the cart from a gallery. After adding products to the cart, it includes 3 further steps to purchase the cart items. Those are, checkout cart item, place order and process payment.

In this article, we are going to cover the remaining steps for implementing shopping cart with payment integration.

In this example, we are going to use the same product gallery view as we have used in the enriched shopping cart example. The shopping cart contains a Go to Checkout button to trigger the purchase flow to the payment via PayPal payment gateway.

Following is a screenshot of the example PHP script.

paynow-paypal

Step1: Add to Cart

This step requires product list to be shown to the buyer who wants to purchase products. In our shopping cart example, we have a gallery view to show each product using a card. Each product card contains the Add to Cart icon.

In a previous tutorial, we have seen examples of handling both session-based and database-based cart. The database-based method is used to have a persistent shopping cart. The following screenshot shows the product gallery and the cart view.

shopping-cart-item

Step2: Checkout Cart Item

In this step, we do cart checkout and proceed to the next level. While processing checkout, the buyer needs to give the shipping details as shown in the below screenshot. This is to confirm placing the order of items.

confirm-order

Step3: Place Order

On clicking the Proceed to Payment button shown on the above screen, the customer details form will call action to confirm placing the order with status pending until the payment is completed.

On successful placement of the order, the buyer required to confirm payment by sending the payment information to the PayPal.

In this example, I have used the PayPal sandbox URL for testing. In a previous tutorial, we have seen how to integrate Paypal payment gateway using PHP.

The following code shows the HTML form with parameters has to be sent to the Paypal like item_numer, amount, currency_code, notify_url, return_url and more.

<form name="frm_customer_detail" action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="POST">
    <input type='hidden' name='business' value='YOUR_BUSINESS_EMAIL'>
    <input type='hidden' name='item_name' value='Cart Item'> 
    <input type='hidden' name='item_number' value="<?php echo $order;?>">
    <input type='hidden' name='amount' value='<?php echo $item_price; ?>'> 
    <input type='hidden' name='currency_code' value='USD'> 
    <input type='hidden' name='notify_url' value='http://yourdomain.com/shopping-cart-check-out-flow-with-payment-integration/notify.php'>
    <input type='hidden' name='return' value='http://yourdomain.com/shopping-cart-check-out-flow-with-payment-integration/response.php'>
    <input type="hidden" name="cmd" value="_xclick"> 
    <input type="hidden" name="order" value="<?php echo $order;?>">
    <div> <input type="submit" class='btn-action' name="continue_payment" value="Continue Payment"> </div>
</form>

Step4: Process Payment

In this final step, the buyer will be redirected to the PayPal page and required. This page will ask the buyer to pay for his order.

After successful payment, the response will be added to the tbl_payment table and the order status will be updated as PAID. These database updates are done in the notify.php which will be accessed via PayPal as we have specified in the above form.

After that, the order status will be acknowledged to the buyer with the order id.

Download

Vincy
Written by Vincy, a web developer with 15+ years of experience and a Masters degree in Computer Science. She specializes in building modern, lightweight websites using PHP, JavaScript, React, and related technologies. Phppot helps you in mastering web development through over a decade of publishing quality tutorials.

Comments to “PHP Shopping Cart with PayPal Payment Gateway Integration”

Leave a Reply to Sam Cancel reply

Your email address will not be published. Required fields are marked *

↑ Back to Top

Share this page