0

I have cart that sends its information to paypal with the hidden html inputs. My problem is when the user finishes the payment in paypal my script the the paypal return sends its return url is not processing the information. When the user clicks pay in paypal it does redirect me back to my site and when I click the link in paypal to return to my site it goes to the return url but nothing shows up and the paypal variables arnt in the url. My cart code is below :

if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) {
    $cartOutput = "<h2 align='center'>Your shopping cart is empty</h2>";
} else {
        // Start PayPal Checkout Button
    $pp_checkout_btn .= '<form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post"  accept-charset="utf-8">
    <input type="hidden" name="cmd" value="_cart">
    <input type="hidden" name="upload" value="1">
    <input type="hidden" name="business" value="pwang@sofpower.com">';
        // Start the For Each loop
        $i = 0;
    foreach ($_SESSION["cart_array"] as $each_item) {
                $item_id = $each_item['item_id'];
                $query = "SELECT * FROM product WHERE id='$item_id' LIMIT 1";
                $result = $db_obj->query($query);
        if($db_obj->error) exit ($db_obj->error);
                //$sql = mysql_query("SELECT * FROM products WHERE id='$item_id' LIMIT 1");
                while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
                        $product_name = $row["product_name"];
                        $price = $row["price"];
                        $details = $row["details"];
                }
                $pricetotal = $price * $each_item['quantity'];
                $cartTotal = $pricetotal + $cartTotal;
                //$taxperc = .07;
                $tax = $cartTotal * .07;
                $_SESSION["cart"] = $cartTotal;
                setlocale(LC_MONETARY, "en_US");
        //$pricetotal = money_format("%10.2n", $pricetotal);
                // Dynamic Checkout Btn Assembly
                $x = $i + 1;
                $pp_checkout_btn .= '<input type="hidden" name="item_name_' . $x . '" value="' . $product_name . '">
        <input type="hidden" name="amount_'. $x . '" value="' . $price . '">
        <input type="hidden" name="quantity_' . $x . '" value="' . $each_item['quantity'] . '">
        <input type="hidden" name="shipping_'.$x.'" value="8.55">
        <input type="hidden" name="tax_'.$x.'" value="'.$tax.'">';
                // Create the product array variable
                $product_id_array .= "$item_id-".$each_item['quantity'].",";
                // Dynamic table row assembly
                $cartOutput .= "<tr>";
                $cartOutput .= '<td><a href="product.php?id=' . $item_id . '">' . $product_name . '</a><br /><img src="inventory_images/' . $item_id . '.jpg" alt="' . $product_name. '" width="40" height="52" border="1" /></td>';
                $cartOutput .= '<td>' . $details . '</td>';
                 $cartOutput .= '<td>$' . $price . '</td>';
                $cartOutput .= '<td><form action="cart.php" method="post">
                <input name="quantity" type="text" value="' . $each_item['quantity'] . '" size="1" maxlength="2" />
                <input name="adjustBtn' . $item_id . '" type="submit" value="change" />
                <input name="item_to_adjust" type="hidden" value="' . $item_id . '" />
                </form></td>';
                //$cartOutput .= '<td>' . $each_item['quantity'] . '</td>';
                $cartOutput .= '<td>' . $pricetotal . '</td>';
                $cartOutput .= '<td><form action="cart.php" method="post"><input name="deleteBtn' . $item_id . '" type="submit" value="X" /><input name="index_to_remove" type="hidden" value="' . $i . '" /></form></td>';
                $cartOutput .= '</tr>';
                $i++;

                 $query  = "INSERT INTO orders (order_num, customer_id, product_name, price, qty, date_added)
      VALUES('$ordernum','$userid','$product_name','$price','', now())";
        $result = $db_obj->query($query);

    }
        setlocale(LC_MONETARY, "en_US");
    //$cartTotal = money_format("%10.2n", $cartTotal);
        $cartTotal = "<div style='font-size:18px; margin-top:12px;' align='right'>Cart Total : ".$cartTotal." USD</div>";

  // Finish the Paypal Checkout Btn
        $pp_checkout_btn .= '
        <input type="hidden" name="currency_code" value="USD">
        <input type="hidden" name="return" value="http://webdev.cs.kent.edu/~jfunchio/wp2/HW2/pay.php">
        <input type="hidden" name="cancel_return" value="http://webdev.cs.kent.edu/~jfunchio/wp2/HW2/index.php">
        <input type="submit" value="PayPal">
        </form>';


}

Then here's the script for pdt that is similar to the one from the paypal website. It seems as though it only processes one item though. If it is can someone help me with how to do it with multiple items and if its not what is wrong with how it is. I've spent multiple days trying to figure out why it's not working. Can someone help please.

<?php
// read the post from PayPal system and add 'cmd'
$req = 'cmd=_notify-synch';

$tx_token = $_GET['tx'];

$auth_token = "dkt4rJVmyOMT_wdUC2Ej3flLKN3gIhzH5vjU0olABbPZioiqU7AHza-Ii1a";

$req .= "&tx=$tx_token&at=$auth_token";


// post back to PayPal system to validate
$header .= "POST /cgi-bin/webscr HTTP/1.0\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
$fp = fsockopen ('www.sandbox.paypal.com', 80, $errno, $errstr, 30);
// If possible, securely post back to paypal using HTTPS
// Your PHP server will need to be SSL enabled
// $fp = fsockopen ('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30);

if (!$fp) {
// HTTP ERROR
} else {
fputs ($fp, $header . $req);
// read the body data
$res = '';
$headerdone = false;
while (!feof($fp)) {
$line = fgets ($fp, 1024);
if (strcmp($line, "\r\n") == 0) {
// read the header
$headerdone = true;
}
else if ($headerdone)
{
// header has been read. now read the contents
$res .= $line;
}
}

// parse the data
$lines = explode("\n", $res);
$keyarray = array();
if (strcmp ($lines[0], "SUCCESS") == 0) {
for ($i=1; $i<count($lines);$i++){
list($key,$val) = explode("=", $lines[$i]);
$keyarray[urldecode($key)] = urldecode($val);
}
// check the payment_status is Completed
// check that txn_id has not been previously processed
// check that receiver_email is your Primary PayPal email
// check that payment_amount/payment_currency are correct
// process payment
$firstname = $keyarray['first_name'];
$lastname = $keyarray['last_name'];
$itemname = $keyarray['item_name'];
$amount = $keyarray['mc_gross'];

echo ("<p><h3>Thank you for your purchase!</h3></p>");

echo ("<b>Payment Details</b><br>\n");
echo ("<li>Name: $firstname $lastname</li>\n");
echo ("<li>Item: $itemname</li>\n");

echo ("<li>Amount: $amount</li>\n");
echo ("");
}
else if (strcmp ($lines[0], "FAIL") == 0) {
// log for manual investigation
}

}

fclose ($fp);

?>

Your transaction has been completed, and a receipt for your purchase has been emailed to you.<br>You may log into your account at <a href='https://www.paypal.com'>www.paypal.com</a> to view details of this transaction.<br>
2
Contributors
1
Reply
4
Views
5 Years
Discussion Span
Last Post by LastMitch
0

I click the link in paypal to return to my site it goes to the return url but nothing shows up and the paypal variables arnt in the url.

Either you integrate PayPal – Payment Data Transfer incorrectly or it has something to do with your variables not matching up with your database.

This is an tutorial on how to integrate PayPal – Payment Data Transfer

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.