Welcome to ANDROID Developer!

Friday, May 23, 2014

Multiple Items PHP Shopping Cart to PayPal


By on 6:52 AM

In the previous post, we had created a simple PHP session based shopping cart, now in this article we will integrating this shopping cart with our PayPal express checkout system. Main idea is to let users buy multiple items using PayPal, shopping cart collects the items users want to buy and then they’ll be able to pay for multiple items using their PayPal account.
Before we start, I suggest you go through both articles Creating Shopping cart and PayPal express checkout, to understand how they work.
I’ve just made few modification in view_cart.php (Creating Shopping cart) and process.php (PayPal express checkout) and that’s all we need to make it work. When the users click Pay Now button in view_cart.php, they are sent directly to process.php for further PayPal payment, similar to the picture below.
Shopping Cart to Paypal

View Cart Form

As discussed above, in previous article I’ve talked about items summary (view_cart.php), the final page where users review the items they are going to buy for the last time before buying. There we had created a form which is suppose to collect the items and post it to some payment gateway. We just need to modify the form part here, and point it to process.php, which is located in PayPal Express checkout folder. The final page (view_cart.php) generates a HTML form similar to code below:
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<form method="post" action="paypal-express-checkout/process.php">
    Android Phone FX1 (Code :PD1001)
    Qty : 1</div>
    <input type="hidden" name="item_name[0]" value="Android Phone FX1">
    <input type="hidden" name="item_code[0]" value="PD1001">
    <input type="hidden" name="item_price[0]" value="200.50">
    <input type="hidden" name="item_qty[0]" value="1">
   
    Television DXT (Code :PD1002)
    Qty : 1
    <input type="hidden" name="item_name[1]" value="Television DXT">
    <input type="hidden" name="item_code[1]" value="PD1002">
    <input type="hidden" name="item_price[1]" value="500.85">
    <input type="hidden" name="item_qty[1]" value="1">
    <input type="submit" value="Pay Now">
</form>

Paypal Express Checkout

So in the process.php we just need to collect the POST array variable named “item_name”, we then calculate the quantity and total amount and send it to PayPal. Have a look at the code below.
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
if($_POST) //Post Data received from product list page.
{
    //Other important variables like tax, shipping cost
    $TotalTaxAmount     = 2.58;  //Sum of tax for all items in this order.
    $HandalingCost      = 2.00;  //Handling cost for this order.
    $InsuranceCost      = 1.00;  //shipping insurance cost for this order.
    $ShippinDiscount    = -3.00; //Shipping discount for this order. Specify this as negative number.
    $ShippinCost        = 3.00; //Although you may change the value later, try to pass in a shipping amount that is reasonably accurate.

    //we need 4 variables from an item, Item Name, Item Price, Item Number and Item Quantity.
    $paypal_data = '';
    $ItemTotalPrice = 0;

    //loop through POST array
    foreach($_POST['item_name'] as $key=>$itmname)
    {
        //get actual product price from database using product code
       $product_code    = filter_var($_POST['item_code'][$key], FILTER_SANITIZE_STRING);
       
        $results = $mysqli->query("SELECT price FROM products WHERE product_code='$product_code' LIMIT 1");
        $obj = $results->fetch_object();

        $paypal_data .= '&L_PAYMENTREQUEST_0_NAME'.$key.'='.urlencode($_POST['item_name'][$key]);
        $paypal_data .= '&L_PAYMENTREQUEST_0_NUMBER'.$key.'='.urlencode($_POST['item_code'][$key]);
        $paypal_data .= '&L_PAYMENTREQUEST_0_AMT'.$key.'='.urlencode($obj->price);     
        $paypal_data .= '&L_PAYMENTREQUEST_0_QTY'.$key.'='. urlencode($_POST['item_qty'][$key]);
       
        // item price X quantity
        $subtotal = ($obj->price*$_POST['item_qty'][$key]);
       
        //total price
        $ItemTotalPrice = ($ItemTotalPrice + $subtotal);
    }

    //parameters for SetExpressCheckout
    $padata =   '&METHOD=SetExpressCheckout'.
                '&RETURNURL='.urlencode($PayPalReturnURL ).
                '&CANCELURL='.urlencode($PayPalCancelURL).
                '&PAYMENTREQUEST_0_PAYMENTACTION='.urlencode("SALE").
                $paypal_data.              
                '&NOSHIPPING=0'. //set 1 to hide buyer's shipping address
                '&PAYMENTREQUEST_0_ITEMAMT='.urlencode($ItemTotalPrice).
                '&PAYMENTREQUEST_0_TAXAMT='.urlencode($TotalTaxAmount).
                '&PAYMENTREQUEST_0_SHIPPINGAMT='.urlencode($ShippinCost).
                '&PAYMENTREQUEST_0_HANDLINGAMT='.urlencode($HandalingCost).
                '&PAYMENTREQUEST_0_SHIPDISCAMT='.urlencode($ShippinDiscount).
                '&PAYMENTREQUEST_0_INSURANCEAMT='.urlencode($InsuranceCost).
                '&PAYMENTREQUEST_0_AMT='.urlencode($GrandTotal).
                '&PAYMENTREQUEST_0_CURRENCYCODE='.urlencode($PayPalCurrencyCode).
                '&LOCALECODE=GB'. //PayPal pages to match the language on your website.
                '&LOGOIMG=http://www.sanwebe.com/wp-content/themes/sanwebe/img/logo.png'. //site logo
                '&CARTBORDERCOLOR=FFFFFF'. //border color of cart
                '&ALLOWNOTE=1';
   

}
That’s it, this is the main modification here, rest is explained in my previous post PayPal express checkout. Without making things further complicated, I want you to download and test the script yourself, but nothing is perfect, so if you want to leave some feedback or improvement suggestions please do so by commenting below. Good luck!

About Theavuth NHEL

Faizan is a 17 year old young guy who is blessed with the art of Blogging,He love to Blog day in and day out,He is a Website Designer and a Certified Graphics Designer.

0 comments:

Post a Comment