Jump to content
Larry Ullman's Book Forums

Paypal Ipn Trouble With Custom Variable


Recommended Posts

I made a site similar to the coffee store but I'm using paypal as the payment gateway. I followed the steps in the book making test sites, enabling IPN and so on. After a transaction is complete, I check the IPN history and it says "Sent". However the information is not inserting into the orders table and not deleting the shopping cart. I checked HTML Variables for paypal and it says custom variable " Pass-through variable for your own tracking purposes, which buyers do not see. Default – No variable is passed back to you." How can I get it to pass it back to me?

 

 

Here is the ipn.php script

 

<?php

 

// Require the configuration before any PHP code:

require ('includes/config.inc.php');

 

// read the post from PayPal system and add 'cmd'

$req = 'cmd=_notify-validate';

 

foreach ($_POST as $key => $value) {

$value = urlencode(stripslashes($value));

$req .= "&$key=$value";

}

 

// 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 ('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30);

 

// assign posted variables to local variables

$item_name = $_POST['item_name'];

$item_number = $_POST['item_number'];

$payment_status = $_POST['payment_status'];

$payment_amount = $_POST['mc_gross'];

$payment_currency = $_POST['mc_currency'];

$txn_id = $_POST['txn_id'];

$receiver_email = $_POST['receiver_email'];

$payer_email = $_POST['payer_email'];

$shipping = $_POST['mc_handling'];

 

if (!$fp) {

// HTTP ERROR

} else {

fputs ($fp, $header . $req);

while (!feof($fp)) {

$res = fgets ($fp, 1024);

if (strcmp ($res, "VERIFIED") == 0) {

// 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

//Check for transaction in the database

$receiver_email = $_POST['receiver_email'];

if($receiver_emial!='janice_1323380910_biz@live.com'){

$message = "receiver email is wrong Email=".$_POST['receiver_email'].'\n\n\n$req';

mail("janicepag23@yahoo.com","Receiver email is incoreect", $message,"From:janicepag23@yahoo.com");

exit();

}

 

if ($_POST['payment_status']!="Completed"){

$message = "Incomplete transaction";

mail("janicepag23@yahoo.com","Incomplete transaction", $message,"From:janicepag23@yahoo.com");

exit();

}

 

require_once('../mysqli_connect.php');

$txn_id = $_POST['txn_id'];

$q = "SELECT id FROM orders WHERE transaction_id='$txn_id'";

$r = mysqli_query($dbc,$q);

if(mysqli_num_rows($r) ==0){

 

//Add this transaction to the orders table:

$uid = $_POST['custom'];

$txn_id = $_POST['txn_id'];

$payment_status = $_POST['payment_status'];

$payment_amount = $_POST['mc_gross'];

$shipping = $_POST['mc_handling'];

$q = "INSERT INTO orders (user_id, transaction_id, payment_status,payment_amount,shipping) VALUES($uid,$txn_id,$payment_status,$amount,$shipping)";

$r = mysqli_query($dbc,$q);

if(mysqli_affected_rows($dbc)==1){

}

}

 

}else{//problem inserting the order!

trigger_error('could not insert order');

}

}// the order has already been stored

}//the right value don't exist in $_POST!

}else if (strcmp ($res, "INVALID") == 0) {

// log for manual investigation

}

}

fclose ($fp);

}

?>

 

here is the cart.html script:

 

 

<h1>Your Shopping Cart</h1><br />

<p>Please use this form to update your shopping cart. You may change the quantities or remove items.</p><br />

 

 

<script language="javascript" type="text/javascript">

function OnSubmitForm1()

{

document.formcart.action = "./cart.php"

document.formcart.submit();

return true;

}

 

function OnSubmitForm2()

{

document.formcart.action = "https://www.sandbox.paypal.com/cgi-bin/webscr"

document.formcart.submit();

return true;

}

 

</script>

 

<form class="formcart" name="formcart" method="post">

<div id="table1">

<table border="0" cellspacing="8" cellpadding="6">

<tr>

<th align="center">Item</th>

<th align="center">Quantity</th>

<th align="right">Price</th>

<th align="right">Subtotal</th>

<th align="center">Options</th>

</tr>

 

<?php

$min=14.98;

$total=0;

$i= 1;

echo'

<input type="hidden" name="business" value="'.$paypal_id.'">

<input type="hidden" name="cmd" value="_cart">

<input type="hidden" name="upload" value="1">

<input type="hidden" name="custom" value="'.$uid.'">

 

<input type="hidden" name="rm" value="2">

<input type="hidden" name="cbt" value="Return to Fantastic Fudge">

<input type="hidden" name="no_shipping" value="1">

<input type="hidden" name="currency_code" value="USD">

<input type="hidden" name="cancel_return" value="http://yoursite.com/cancel.php">

<input type="hidden" name="return" value="http://www.palepinkgraphics.com/storescripts/fudge/final.php">';

 

while ($row=mysqli_fetch_array($r,MYSQLI_ASSOC)){

 

 

$price=$row['price'];

$subtotal=$price*$row['quantity'];

$fudge_name=$row['name'];

$quantity=$row['quantity'];

$x = $i++ ;

echo'

 

<input type="hidden" name="item_name_'.$x.'" value="'.$fudge_name.'">

<input type="hidden" name="amount_'.$x.'" value="'.$price.'">

<input type="hidden" name="quantity_'.$x.'" value="'.$quantity.'">

<tr>

<td> '.$fudge_name.'</td>

 

<td align="center"> <p class="quantity"><input type="text" name="quantity['.$row['sku'].']"value="'.$quantity.'"size="2" /></p></td>

 

 

<td align="right"> $'.$price.'</td>

 

<td align="right">$'.number_format($subtotal,2).'</td>

<td align="right"><a href="./cart.php?sku='.$row['sku'].'&action=remove">Remove from Cart</a></td>

</tr>

';

 

if($row['stock']<$row['quantity']){

echo'<tr class="error"><td colspan="5" align="center">There are only'.$row['stock'].'left in stock of the '.$row['fudge_name'].'.Please update the quantity or remove the item entirely.</td></tr>';

}

 

//Add the subtotal to the total:

$total +=$subtotal;

 

}//End of while loop.

 

 

//Add the shipping:

$shipping=get_shipping($total);

$total += $shipping;

echo'<tr>

<td colspan="3" align="right"><strong>Shipping & Handling</strong></td>

<td align="right">$'.$shipping.'</td>

<td> </td>

</tr>

';

 

//Display the total:

echo '<tr>

<td colspan="3" align="right"><strong>Total</strong></td>

<td align="right">$'.number_format($total,2).'</td>

<td> </td>

</tr><br />

<input type="hidden" name="handling_cart" value="'.$shipping.'">

 

';

 

?>

 

 

</table>

<p align="center"><input type="submit" Value="Update Quantities" onClick="return OnSubmitForm1()" />

<input type="submit" Value="PayPal" onClick="return OnSubmitForm2()" /><br />

 

</form></p></div><br />

<p class="hometitle">Shipping is $5.00 for orders 1 Ib or under. Add $2.00 for every Ib over. Most orders shipped within 3 business days. Don't forget if you order 2 Ibs or more to include your FREE 1/2 Ib!!! A min order of 1/2lb required.</p><br />

<p>* Flavors are subject to change.</p>

 

 

I have $uid = $_COOKIE['SESSION'];

in the top of cart.php script.

 

Thanks

Link to comment
Share on other sites

If you pass a custom variable to paypal it should be included when it sends information to your ipn script.

 

I would start by dumping all the information being sent to your ipn script into a text file to see exactly what you're getting from paypal - I'm guessing you checked your logs already and nothing is showing?

 

Could be an issue with how you're setting and sending the value to paypal. If it is being sent and is correct, then it's an issue with your ipn script - I'd start with mysql query debugging.

 

You don't mention how you're storing the shopping cart information - I don't have the book to hand and cannot remember how that example in the book does it. If it's in a database, there's nothing in your script to remove it.

Link to comment
Share on other sites

I did the log and it was showing invalid. After making a few changes, it finally comes back valid. It was able to INSERT the information into the orders table but now it stopped doing that. As for the remove from the shopping cart, I followed the instructions in the final.php script in the coffee example but now i'm guessing I should but that mysql query in the ipn script. Here are the changes I made to the IPN script:

 

<?php

 

// Require the configuration before any PHP code:

require ('includes/config.inc.php');

 

 

// Open the text file:

// Change this path to make it accurate.

// The text file must be writable by PHP!

$file = fopen('ipn.txt', 'a');

 

// Write the POST data to the file:

fwrite($file, "Received:\n");

fwrite($file, print_r($_POST, true));

fwrite($file, "\n");

 

// read the post from PayPal system and add 'cmd'

$req = 'cmd=_notify-validate';

 

foreach ($_POST as $key => $value) {

$value = urlencode(stripslashes($value));

$req .= "&$key=$value";

fwrite($file, $key.":".$value."\n");

}

 

$fp = fsockopen ('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30);

 

if (!$fp) { // If we couldn't connect, send an email:

 

trigger_error('Could not connect for the IPN!');

 

} else { // Send the request to PayPal:

 

// 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";

fputs ($fp, $header . $req);

 

// Write the PayPal request to the text file:

fwrite($file, "Sent:\n");

fwrite($file, "$header\n");

fwrite($file, "$req\n");

 

 

while (!feof($fp)) {

 

$res = fgets ($fp, 1024);

 

// Write the PayPal response to the text file:

fwrite($file, "Received:\n");

fwrite($file, "$res\n");

 

if (strcmp ($res, "VERIFIED") == 0) {

// assign posted variables to local variables

if ( ($_POST['payment_status'] == 'Completed')

&& ($_POST['receiver_email'] == 'janice_1323380910_biz@live.com')

&& ($_POST['mc_currency'] == 'USD')

&& (!empty($_POST['txn_id']))

) {

 

 

 

 

 

require_once('../mysqli_connect.php');

$txn_id = $_POST['txn_id'];

$q = "SELECT id FROM orders WHERE transaction_id='$txn_id'";

$r = mysqli_query($dbc,$q);

if(mysqli_num_rows($r) ==0){//Add this transaction to the orders table:

 

$txn_id = $_POST['txn_id'];

$uid = $_POST['payer_email'];

$status = $_POST['payment_status'];

$amount = $_POST['mc_gross'];

$shipping = $_POST['mc_handling'];

$q = "INSERT INTO orders (user_id, transaction_id, payment_status,payment_amount,shipping) VALUES ('$uid','$txn_id','$status','$amount','$shipping')";

$r = mysqli_query($dbc,$q);

if (mysqli_affected_rows($dbc) == 1) {

}

 

 

}else{//problem inserting the order!

trigger_error('could not insert order');

 

// the order has already been stored

 

}//the right value don't exist in $_POST!

 

}elseif (strcmp ($res, "INVALID") == 0) {

// log for manual investigation

 

}

}

}

// Inidicate the end of this transaction in the text file:

fwrite($file, "--------------\n");

fclose ($file);

fclose ($fp);

}

?>

Link to comment
Share on other sites

  • 2 weeks later...

I'm so sorry for not replying earlier. I put this on my "content to follow" list because I couldn't get to it immediately, and then I forgot to check my "content to follow"! Sincere apologies. Are you still having problems with this?

Link to comment
Share on other sites

 Share

×
×
  • Create New...