I am currently implementing a Paypal Express Checkout and I have multiple items going into an order. These items are successfully going into Paypal and being shown individually on the invoice. Once the customer returns to my website the client is returned through a processing page which puts the items into a database, but instead of the individual items with their individual prices being listed I am only getting one item and the total price.
if($_POST) //Post Data received from product list page.
{
//Other important variables like tax, shipping cost
$TotalTaxAmount = 0.00; //Sum of tax for all items in this order.
$HandalingCost = 0.00; //Handling cost for this order.
$InsuranceCost = 0.00; //shipping insurance cost for this order.
$ShippinDiscount = 0.00; //Shipping discount for this order. Specify this as negative number.
$ShippinCost = 0.00; //Although you may change the value later, try to pass in a shipping amount that is reasonably accurate.
//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;
foreach($_POST['item_name'] as $key=>$itmname)
{
$product_code = filter_var($_POST['item_code'][$key], FILTER_SANITIZE_STRING);
$results = $db->prepare("SELECT product_name, product_desc, price FROM ".$company."_quotes WHERE product_code='$product_code'");
$results->execute();
$obj = $results->fetch(PDO::FETCH_ASSOC);
$paypal_data .= '&L_PAYMENTREQUEST_0_NAME'.$key.'='.urlencode($obj['product_name']);
$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;
$TotalTaxAmount = $ItemTotalPrice * 0.2;
//create items for session
$paypal_product['items'][] = array('itm_name'=>$obj['product_name'],
'itm_price'=>$obj['price'],
'itm_code'=>$_POST['item_code'][$key],
'itm_qty'=>$_POST['item_qty'][$key]
);
}
//Grand total including all tax, insurance, shipping cost and discount
$GrandTotal = ($ItemTotalPrice + $TotalTaxAmount + $HandalingCost + $InsuranceCost + $ShippinCost + $ShippinDiscount);
$paypal_product['assets'] = array('tax_total'=>$TotalTaxAmount,
'handaling_cost'=>$HandalingCost,
'insurance_cost'=>$InsuranceCost,
'shippin_discount'=>$ShippinDiscount,
'shippin_cost'=>$ShippinCost,
'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=1'. //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($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://leads.uks1.com/images/logo.png'. //site logo
'&CARTBORDERCOLOR=FFFFFF'. //border color of cart
'&ALLOWNOTE=1';
//We need to execute the "SetExpressCheckOut" method to obtain paypal token
$paypal= new MyPayPal();
$httpParsedResponseAr = $paypal->PPHttpPost('SetExpressCheckout', $padata, $PayPalApiUsername, $PayPalApiPassword, $PayPalApiSignature, $PayPalMode);
//Respond according to message we receive from Paypal
if("SUCCESS" == strtoupper($httpParsedResponseAr["ACK"]) || "SUCCESSWITHWARNING" == strtoupper($httpParsedResponseAr["ACK"]))
{
//Redirect user to PayPal store with Token received.
$paypalurl ='https://www'.$paypalmode.'.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token='.$httpParsedResponseAr["TOKEN"].'';
header('Location: '.$paypalurl);
}
else
{
//Show error message
echo '<div style="color:red"><b>Error : </b>'.urldecode($httpParsedResponseAr["L_LONGMESSAGE0"]).'</div>';
echo '<pre>';
print_r($httpParsedResponseAr);
echo '</pre>';
}
}
//Paypal redirects back to this page using ReturnURL, We should receive TOKEN and Payer ID
if(isset($_GET["token"]) && isset($_GET["PayerID"]))
{
//we will be using these two variables to execute the "DoExpressCheckoutPayment"
//Note: we haven't received any payment yet.
$token = $_GET["token"];
$payer_id = $_GET["PayerID"];
//get session variables
$paypal_product = $_SESSION["paypal_products"];
$paypal_data = '';
$ItemTotalPrice = 0;
foreach($paypal_product['items'] as $key=>$p_item)
{
$paypal_data .= '&L_PAYMENTREQUEST_0_QTY'.$key.'='. urlencode($p_item['itm_qty']);
$paypal_data .= '&L_PAYMENTREQUEST_0_AMT'.$key.'='.urlencode($p_item['itm_price']);
$paypal_data .= '&L_PAYMENTREQUEST_0_NAME'.$key.'='.urlencode($p_item['itm_name']);
$paypal_data .= '&L_PAYMENTREQUEST_0_NUMBER'.$key.'='.urlencode($p_item['itm_code']);
// item price X quantity
$subtotal = ($p_item['itm_price']*$p_item['itm_qty']);
//total price
$ItemTotalPrice = ($ItemTotalPrice + $subtotal);
}
$padata = '&TOKEN='.urlencode($token).
'&PAYERID='.urlencode($payer_id).
'&PAYMENTREQUEST_0_PAYMENTACTION='.urlencode("SALE").
$paypal_data.
'&PAYMENTREQUEST_0_ITEMAMT='.urlencode($ItemTotalPrice).
'&PAYMENTREQUEST_0_TAXAMT='.urlencode($paypal_product['assets']['tax_total']).
'&PAYMENTREQUEST_0_SHIPPINGAMT='.urlencode($paypal_product['assets']['shippin_cost']).
'&PAYMENTREQUEST_0_HANDLINGAMT='.urlencode($paypal_product['assets']['handaling_cost']).
'&PAYMENTREQUEST_0_SHIPDISCAMT='.urlencode($paypal_product['assets']['shippin_discount']).
'&PAYMENTREQUEST_0_INSURANCEAMT='.urlencode($paypal_product['assets']['insurance_cost']).
'&PAYMENTREQUEST_0_AMT='.urlencode($paypal_product['assets']['grand_total']).
'&PAYMENTREQUEST_0_CURRENCYCODE='.urlencode($PayPalCurrencyCode);
//We need to execute the "DoExpressCheckoutPayment" at this point to Receive payment from user.
$paypal= new MyPayPal();
$httpParsedResponseAr = $paypal->PPHttpPost('DoExpressCheckoutPayment', $padata, $PayPalApiUsername, $PayPalApiPassword, $PayPalApiSignature, $PayPalMode);
//Check if everything went ok..
if("SUCCESS" == strtoupper($httpParsedResponseAr["ACK"]) || "SUCCESSWITHWARNING" == strtoupper($httpParsedResponseAr["ACK"]))
{
echo '<h2>Success</h2>';
echo 'Your Transaction ID : '.urldecode($httpParsedResponseAr["PAYMENTINFO_0_TRANSACTIONID"]);
/*
//Sometimes Payment are kept pending even when transaction is complete.
//hence we need to notify user about it and ask him manually approve the transiction
*/
if('Completed' == $httpParsedResponseAr["PAYMENTINFO_0_PAYMENTSTATUS"])
{
echo '<div style="color:green">Payment Received! Your product will be sent to you very soon!</div>';
}
elseif('Pending' == $httpParsedResponseAr["PAYMENTINFO_0_PAYMENTSTATUS"])
{
echo '<div style="color:red">Transaction Complete, but payment is still pending! '.
'You need to manually authorize this payment in your <a target="_new" href="http://www.paypal.com">Paypal Account</a></div>';
}
// we can retrive transection details using either GetTransactionDetails or GetExpressCheckoutDetails
// GetTransactionDetails requires a Transaction ID, and GetExpressCheckoutDetails requires Token returned by SetExpressCheckOut
$padata = '&TOKEN='.urlencode($token);
$paypal= new MyPayPal();
$httpParsedResponseAr = $paypal->PPHttpPost('GetExpressCheckoutDetails', $padata, $PayPalApiUsername, $PayPalApiPassword, $PayPalApiSignature, $PayPalMode);
if("SUCCESS" == strtoupper($httpParsedResponseAr["ACK"]) || "SUCCESSWITHWARNING" == strtoupper($httpParsedResponseAr["ACK"]))
{
echo '<br /><b>Stuff to store in database :</b><br />';
echo '<pre>';
$buyerName = urldecode($httpParsedResponseAr["FIRSTNAME"]).' '.urldecode($httpParsedResponseAr["LASTNAME"]);
$buyerEmail = urldecode($httpParsedResponseAr["EMAIL"]);
$ItemNumber = urldecode($httpParsedResponseAr["L_NUMBER0"]);
$insert_row = $db->query("INSERT INTO BuyerTable
(BuyerName,BuyerEmail,TransactionID,ItemName,ItemNumber,ItemAmount,ItemQTY)
VALUES ('$buyerName','$buyerEmail','$transactionID','$ItemName','$ItemNumber','$ItemTotalPrice','$ItemQTY')");
$insert_row->execute();
$update_row = $db->query("UPDATE ".$company."_quotes SET slot_1 = '".$webdevid."' WHERE product_code = '".$ItemNumber."' AND slot_1 = ''");
$update_row->execute();
unset($_SESSION['products']);
echo '<pre>';
print_r($httpParsedResponseAr);
echo '</pre>';
?>
<!--
<script>
window.location.href = "/products/";
</script>
-->
<?php
} else {
echo '<div style="color:red"><b>GetTransactionDetails failed:</b>'.urldecode($httpParsedResponseAr["L_LONGMESSAGE0"]).'</div>';
echo '<pre>';
print_r($httpParsedResponseAr);
echo '</pre>';
}
}else{
echo '<div style="color:red"><b>Error : </b>'.urldecode($httpParsedResponseAr["L_LONGMESSAGE0"]).'</div>';
echo '<pre>';
print_r($httpParsedResponseAr);
echo '</pre>';
}
}
This is the array of information that Paypal is sending.
[L_ITEMWEIGHTVALUE0] => %20%20%200%2e00000
[L_ITEMWEIGHTVALUE1] => %20%20%200%2e00000
[L_ITEMWEIGHTVALUE2] => %20%20%200%2e00000
[L_ITEMLENGTHVALUE0] => %20%20%200%2e00000
[L_ITEMLENGTHVALUE1] => %20%20%200%2e00000
[L_ITEMLENGTHVALUE2] => %20%20%200%2e00000
[L_ITEMWIDTHVALUE0] => %20%20%200%2e00000
[L_ITEMWIDTHVALUE1] => %20%20%200%2e00000
[L_ITEMWIDTHVALUE2] => %20%20%200%2e00000
[L_ITEMHEIGHTVALUE0] => %20%20%200%2e00000
[L_ITEMHEIGHTVALUE1] => %20%20%200%2e00000
[L_ITEMHEIGHTVALUE2] => %20%20%200%2e00000
[PAYMENTREQUEST_0_CURRENCYCODE] => GBP
[PAYMENTREQUEST_0_AMT] => 90%2e00
[PAYMENTREQUEST_0_ITEMAMT] => 75%2e00
[PAYMENTREQUEST_0_SHIPPINGAMT] => 0%2e00
[PAYMENTREQUEST_0_HANDLINGAMT] => 0%2e00
[PAYMENTREQUEST_0_TAXAMT] => 15%2e00
[PAYMENTREQUEST_0_INSURANCEAMT] => 0%2e00
[PAYMENTREQUEST_0_SHIPDISCAMT] => 0%2e00
[PAYMENTREQUEST_0_TRANSACTIONID] => 11E938880U168782F
[PAYMENTREQUEST_0_INSURANCEOPTIONOFFERED] => false
[PAYMENTREQUEST_0_ADDRESSNORMALIZATIONSTATUS] => None
[L_PAYMENTREQUEST_0_NAME0] => Web%20Lead
[L_PAYMENTREQUEST_0_NAME1] => Web%20Lead
[L_PAYMENTREQUEST_0_NAME2] => Web%20Lead
[L_PAYMENTREQUEST_0_NUMBER0] => weblead_7
[L_PAYMENTREQUEST_0_NUMBER1] => weblead_5
[L_PAYMENTREQUEST_0_NUMBER2] => weblead_6
[L_PAYMENTREQUEST_0_QTY0] => 1
[L_PAYMENTREQUEST_0_QTY1] => 1
[L_PAYMENTREQUEST_0_QTY2] => 1
[L_PAYMENTREQUEST_0_TAXAMT0] => 0%2e00
[L_PAYMENTREQUEST_0_TAXAMT1] => 0%2e00
[L_PAYMENTREQUEST_0_TAXAMT2] => 0%2e00
[L_PAYMENTREQUEST_0_AMT0] => 25%2e00
[L_PAYMENTREQUEST_0_AMT1] => 25%2e00
[L_PAYMENTREQUEST_0_AMT2] => 25%2e00
Any help on this would be fantastic and big kudos to the person who can!