PHP shopping cart with Paypal payment gateway


PHP shopping cart with Paypal payment gateway

PHP shopping cart with Paypal payment gateway

 

In this post I would like to show you PHP shopping cart with Paypal payment gateway. The entire introduction will consist from four steps described below:

 

Step 1: Add to Cart

This step requires product list to be shown to the buyer who wants to purchase products. You can see two different ways to handle the cart: session-based and database-based cart. The database-based method is used to have a persistent shopping cart.

Add to cart
Add to cart

Step 2: Checkout Cart Item

In this step, we do cart checkout to proceed further. During checkout process, the buyer needs to give the shipping details to confirm placing the order of items.

Checkout cart item
Checkout cart item

Step 3: Proceed to Payment

On clicking the Proceed to Payment, the customer details in the form will call action to confirm placing the order with status pending until the payment is completed. On successfull order, the buyer is required to confirm payment by sending the payment information to the PayPal.

Further, I have used the PayPal sandbox URL for testing ( this is a special tool for developers who wants to test if Paypal transactions are working without using real money ). 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>

Step 4: Process Payment

The buyer will be redirected to the PayPal page to pay for his order. After successful payment, the response willupdate the tbl_payment table and the order status will be be shown as PAID. These database updates are done in the notify.php ( accessed via PayPal ) as we have specified in the above form. And finally, the order status will be acknowledged to the customer with the order id.

 

Hope you learned something new

 

If you learned something new from this tutorial or if you have any question let me know in comments section.

Download full script

Have any Question or Comment?

Leave a Reply

Your email address will not be published.

Subscribe for new posts!

Recent Posts

Recent Comments

    Blog Categories