3

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.

D. Hodge
  • 88
  • 1
  • 6
  • White page could mean possible syntax errors. Use error reporting http://php.net/manual/en/function.error-reporting.php – Funk Forty Niner Feb 26 '16 at 16:41
  • I have error reporting on, but I should have been clearer. The white page is on PayPal's end – D. Hodge Feb 26 '16 at 16:45
  • Possible duplicate of [PHP's white screen of death](http://stackoverflow.com/questions/1475297/phps-white-screen-of-death) – Maks3w Feb 26 '16 at 17:48
  • @D.Hodge are you trying setting enabled the sandbox in paypal? By this mode you enter in testing mode and you can find better the problem – Marcos Pérez Gude Feb 29 '16 at 10:20
  • 1
    I didn't really need to use sandbox since I don't need to process payments I just need to get to the cart overview and log in screen, which is just displaying as white. – D. Hodge Mar 01 '16 at 10:35
  • 1
    Were you ever able to solve this issue? I am also having similar problems, and need to support encrypted buttons. – Arbel Nov 11 '16 at 20:33

1 Answers1

0

Since this hasn't been answered yet I thought I may as well answer it for others who are in the same boat as I was.

Although this isn't really an answer to whether or not you can achieve what I was trying to achieve using the Paypal button generator, I ended up using the express checkout API instead (Which was what Paypal support suggested instead of the button generator after a few of their suggestions didn't work).

https://developer.paypal.com/docs/integration/direct/express-checkout/integration-jsv4/server-side-REST-integration/

This API appears to have changed slightly since I last used it and there may be a better way now.

D. Hodge
  • 88
  • 1
  • 6