I'm currently trying to integrate a PayPal checkout button to a site but I'm not having much success.
Basically the user can add product numbers and quantities to their cart which is stored on the server. They can then go to a 'view cart' page which will show them the products in their cart. From there they should be able to click a button to transfer all the relevant information to PayPal so they can checkout with PayPal but the button I have generated just goes to a white page (on PayPal's end https://www.paypal.com/cgi-bin/webscr
).
Here is the code for generating the button:
$sendPayData = array(
"METHOD" => "BMCreateButton",
"USER" => "****",
"PWD" => "****",
"SIGNATURE" => "****",
"BUTTONCODE" => "CLEARTEXT", //Just while debugging
"BUTTONTYPE" => "BUYNOW",
"BUTTONCOUNTRY" => "GB",
"BUTTONIMAGE" => "reg",
"VERSION" => "65.1"
);
$productInfo = array();
$connection = new mysqli(constant("PERCH_DB_SERVER"),constant("PERCH_DB_USERNAME"),constant("PERCH_DB_PASSWORD"),constant("PERCH_DB_DATABASE"));
$sql = 'SELECT * FROM perch2_shop_products WHERE productCode IN ("'.implode('","',array_keys($_SESSION["cartProducts"])).'")';
if($connection->connect_error){
die("Connection Failed: ".$connection->connect_error);
}
$result = $connection->query($sql);
$total = 0;
if($result->num_rows>0){
$i = 0;
while($row = $result->fetch_assoc()){
$sendPayData["L_BUTTONVAR".((3*$i)+1)] = 'item_name_'.($i+1).'='.preg_replace("/[^A-Za-z0-9 ]/", '',$row["productTitle"]);
$sendPayData["L_BUTTONVAR".((3*$i)+2)] = 'quantity_'.($i+1).'='.$_SESSION["cartProducts"][$row["productCode"]];
$sendPayData["L_BUTTONVAR".((3*$i)+3)] = 'amount_'.($i+1).'='.$row["productPrice"];
$total += $row["productPrice"];
$i++;
}
}
$i--;
$sendPayData["L_BUTTONVAR".((3*$i)+4)] = "cmd=_cart";
$sendPayData["L_BUTTONVAR".((3*$i)+5)] = "upload=1";
$sendPayData["L_BUTTONVAR".((3*$i)+6)] = "currency_code=GBP";
$sendPayData["L_BUTTONVAR".((3*$i)+7)] = "business=****";
$sendPayData["L_BUTTONVAR".((3*$i)+8)] = "no_shipping=1";
$sendPayData["L_BUTTONVAR".((3*$i)+9)] = "no_note=1";
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_URL, 'https://api-3t.paypal.com/nvp?'.http_build_query($sendPayData));
$nvpPayReturn = curl_exec($curl);
curl_close($curl);
parse_str($nvpPayReturn, $returnArray);
echo $returnArray["WEBSITECODE"];
To make things simpler here are the variables that are being sent to paypal (Without the spacing):
https://api-3t.paypal.com/nvp?
METHOD=BMCreateButton&
USER=****&
PWD=****&
SIGNATURE=****&
BUTTONCODE=CLEARTEXT&
BUTTONTYPE=BUYNOW&
BUTTONCOUNTRY=GB&
BUTTONIMAGE=reg&
VERSION=65.1&
L_BUTTONVAR1=item_name_1%3DName1&
L_BUTTONVAR2=quantity_1%3D1&
L_BUTTONVAR3=amount_1%3D6.85&
L_BUTTONVAR4=item_name_2%3DName2&
L_BUTTONVAR5=quantity_2%3D1&
L_BUTTONVAR6=amount_2%3D5.99&
L_BUTTONVAR7=item_name_3%3DName3&
L_BUTTONVAR8=quantity_3%3D1&
L_BUTTONVAR9=amount_3%3D12.85&
L_BUTTONVAR10=cmd%3D_cart&
L_BUTTONVAR11=upload%3D1&
L_BUTTONVAR12=currency_code%3DGBP&
L_BUTTONVAR13=business%3D****&
L_BUTTONVAR14=no_shipping%3D1&
L_BUTTONVAR15=no_note%3D1
UPDATE 1
Turns out paypal wasn't applying the cmd variable and cmd was still:
<input type="hidden" name="cmd" value="_xclick">
Instead of:
<input type="hidden" name="cmd" value="_cart">
I'll update if I find out the cause.
As is have said previously this seems to be a problem with PayPal not my php as the white screen was when I transferred to the PayPal website.
UPDATE 2
It seems that if you set the button type to cart
instead of buynow
then it will set cmd
to _cart
but will only generate a button if you have item_name
, quantity
ect instead of item_name_1
, quantity_1
ect. If you do this, however, it will say your cart is empty when you click through to PayPal.
If I manually change the item variables to have _1
at the then the button will work as intended, displaying the PayPal login page with all the items listed in the order.
Is there something wrong with PayPals button generator?
UPDATE 3
The reason it goes to a white screen is because if you define the cmd
variable it will just add another one instead of overwriting the old one. Paypal then doesn't know what to do since there are 2 of them.
It doesn't seem there is a way to create an upload cart button unless you make it manually with html, which can be tampered with.
UPDATE 4
If you change buynow
to cart
and delete the <input type="hidden" name="cmd" value="_cart">
from the button then it works. For some reason using the upload=1
variable doesn't override the add=1
function of the cart
button and causes your basket to show as empty.
But even though this works in the html version I need to be able to use an encrypted button so I still don't know what to do other than group the order into one item then use the custom
variable to send the order details through to the IPN.