Multiple Items PHP Shopping Cart to PayPal
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.View Cart Page
In viewcart.php I have added a "Pay with PayPal" button, once clicked it will redirect user to PayPal express checkout page. The express checkout page uses existing session variable from the shopping cart to determine the costs, taxes and additional fees. Then user will be redirected to PayPal's item summery view, where user can pay using their PayPal account.HTML
- 1
<a href="paypal-express-checkout" ><img src="images/btn_pay_with_paypal.png" width="179" height="36"></a>
Paypal Express Checkout
I've copied the Paypal Express Checkout folder into shopping cart and modified it to use shopping cart session variables, we just calculate the quantity and total amount and send it to PayPal. Have a look at the code below.PHP
- 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
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
if(isset($_SESSION["cart_products"])) //Post Data received from product list page.
{
//Other important variables like tax, shipping cost
//we need 4 variables from product page Item Name, Item Price, Item Number and Item Quantity.
//Please Note : People can manipulate hidden field amounts in form,
//In practical world you must fetch actual price from database using item id.
//eg : $ItemPrice = $mysqli->query("SELECT item_price FROM products WHERE id = Product_Number");
$paypal_data ='';
$ItemTotalPrice = 0;
$i = 0;
foreach ($_SESSION["cart_products"] as $cart_itm)
{
$product_code = filter_var($cart_itm["product_code"], FILTER_SANITIZE_STRING);
$results = $mysqli->query("SELECT product_name, product_desc, price FROM products WHERE product_code='$product_code' LIMIT 1");
$obj = $results->fetch_object();
$paypal_data .= '&L_PAYMENTREQUEST_0_NAME'.$i.'='.urlencode($obj->product_name);
$paypal_data .= '&L_PAYMENTREQUEST_0_NUMBER'.$i.'='.urlencode($cart_itm["product_code"]);
$paypal_data .= '&L_PAYMENTREQUEST_0_AMT'.$i.'='.urlencode($obj->price);
$paypal_data .= '&L_PAYMENTREQUEST_0_QTY'.$i.'='. urlencode($cart_itm["product_qty"]);
// item price X quantity
$subtotal = ($obj->price*$cart_itm["product_qty"]);
//total price
$ItemTotalPrice = $ItemTotalPrice + $subtotal;
//create items for session
$paypal_product['items'][] = array('itm_name'=>$obj->product_name,
'itm_price'=>$obj->price,
'itm_code'=>$cart_itm["product_code"],
'itm_qty'=>$cart_itm["product_qty"]
);
$i++;
}
$total_tax = 0;
foreach($taxes as $key => $value){ //list and calculate all taxes in array
$tax_amount = round($ItemTotalPrice * ($value / 100));
$tax_item[$key] = $tax_amount;
$total_tax = $total_tax + $tax_amount; //total tax amount
}
//Grand total including all tax, insurance, shipping cost and discount
$GrandTotal = ($ItemTotalPrice + $total_tax + $HandalingCost + $InsuranceCost + $shipping_cost + $ShippinDiscount);
$paypal_product['assets'] = array('tax_total'=>$total_tax,
'handaling_cost'=>$HandalingCost,
'insurance_cost'=>$InsuranceCost,
'shippin_discount'=>$ShippinDiscount,
'shippin_cost'=>$shipping_cost,
'grand_total'=>$GrandTotal);
//create session array for later use
$_SESSION["paypal_products"] = $paypal_product;
//Parameters for SetExpressCheckout, which will be sent to PayPal
$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, in-case products that does not require shipping
'&PAYMENTREQUEST_0_ITEMAMT='.urlencode($ItemTotalPrice).
'&PAYMENTREQUEST_0_TAXAMT='.urlencode($total_tax).
'&PAYMENTREQUEST_0_SHIPPINGAMT='.urlencode($shipping_cost).
'&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=https://www.sanwebe.com/wp-content/themes/sanwebe/img/logo.png'. //site logo
'&CARTBORDERCOLOR=FFFFFF'. //border color of cart
'&ALLOWNOTE=1';