YetAnotherForum
Welcome Guest Search | Active Topics | Log In | Register

IPN and inserting records into MySQL database Options
haydiz
#1 Posted : Thursday, April 10, 2003 5:16:47 PM
Rank: Starting Member

Groups: Registered

Joined: 4/10/2003
Posts: 10
Location: ,
This is the error message I am receiving when I test my IPN page: Parse error: parse error, unexpected T_ELSE in /blah/blah/public_html/blah/shop/ipn.php on line 57 Here's my script: <?PHP // read the post from PayPal system and add 'cmd' $req = 'cmd=_notify-validate'; foreach ($HTTP_POST_VARS 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 ('www.paypal.com', 80, $errno, $errstr, 30); // assign posted variables to local variables // note: additional IPN variables also available -- see IPN documentation $item_name = $HTTP_POST_VARS['item_name']; $receiver_email = $HTTP_POST_VARS['receiver_email']; $item_number = $HTTP_POST_VARS['item_number']; $payment_status = $HTTP_POST_VARS['payment_status']; $payment_gross = $HTTP_POST_VARS['payment_gross']; $payer_email = $HTTP_POST_VARS['payer_email']; $quantity = $HTTP_POST_VARS['quantity']; $payment_date = $HTTP_POST_VARS['payment_date']; $first_name = $HTTP_POST_VARS['first_name']; $last_name = $HTTP_POST_VARS['last_name']; $address_street = $HTTP_POST_VARS['address_street']; $address_city = $HTTP_POST_VARS['address_city']; $address_state = $HTTP_POST_VARS['address_state']; $address_zip = $HTTP_POST_VARS['address_zip']; if (!$fp) { // ERROR echo '$errstr ($errno)'; } else { fputs ($fp, $header . $req); while (!feof($fp)) { $res = fgets ($fp, 1024); if (strcmp ($res, 'VERIFIED') == 0) { //Write the data to my own database $lien_mysql = mysql_connect('localhost','blah_blah','woofwoof'); mysql_select_db('blahblah'); $resultat_sql = mysql_query("insert into orders values('item_name', 'receiver_email', 'item_number', 'payment_status', 'payment_gross', 'payer_email', 'quantity', 'payment_date', 'first_name', 'last_name', 'address_street', 'address_city', 'address_state', 'address_zip')"); } } } //LINE 57 WHERE ERROR IS BELOW else if (strcmp ($res, 'INVALID') == 0) { // log for manual investigation fclose ($fp); } ?> Basically, the only reason I want to use IPN is so that orders will be stored in my MySQL Database. Thanks for any help you can give! haydiz
Sponsor  
 
haydiz
#2 Posted : Friday, April 11, 2003 1:42:52 AM
Rank: Starting Member

Groups: Registered

Joined: 4/10/2003
Posts: 10
Location: ,
Okay, forget about that first script. I'm now using the IPN Class from Trivia Shock. The IPN script is working great! However, I'm still having difficulties getting data into my database.

// If we made it down here, the order is verified and payment is complete.
// You could log the order to a MySQL database or do anything else at this point.
//Write the data to my own database
$item_name = $HTTP_POST_VARS['item_name'];
$receiver_email = $HTTP_POST_VARS['receiver_email'];
$item_number = $HTTP_POST_VARS['item_number'];
$payment_status = $HTTP_POST_VARS['payment_status'];
$payment_gross = $HTTP_POST_VARS['payment_gross'];
$payer_email = $HTTP_POST_VARS['payer_email'];
$quantity = $HTTP_POST_VARS['quantity'];
$payment_date = $HTTP_POST_VARS['payment_date'];
$first_name = $HTTP_POST_VARS['first_name'];
$last_name = $HTTP_POST_VARS['last_name'];
$address_street = $HTTP_POST_VARS['address_street'];
$address_city = $HTTP_POST_VARS['address_city'];
$address_state = $HTTP_POST_VARS['address_state'];
$address_zip = $HTTP_POST_VARS['address_zip'];

$db = mysql_connect('localhost','username','password');
mysql_select_db('dbname');
$resultat_sql = mysql_query("INSERT INTO orders ('item_name', 'receiver_email', 'item_number', 'payment_status', 'payment_gross', 'payer_email', 'quantity', 'payment_date', 'first_name', 'last_name', 'address_street', 'address_city', 'address_state', 'address_zip') VALUES ('$item_name', '$receiver_email', '$item_number', '$payment_status', '$payment_gross', '$payer_email', '$quantity', '$payment_date', '$first_name', '$last_name', '$address_street', '$address_city', '$address_state', '$address_zip'");


Am I even close?



haydiz
coyote
#3 Posted : Friday, April 11, 2003 4:10:02 AM
Rank: Starting Member

Groups: Registered

Joined: 4/9/2003
Posts: 12
Location: ,
At the end of your query string,
Change
... '$address_zip'");

to
... '$address_zip')",$db);

haydiz
#4 Posted : Saturday, April 12, 2003 1:57:33 AM
Rank: Starting Member

Groups: Registered

Joined: 4/10/2003
Posts: 10
Location: ,
Thanks, I changed the code. However, I'm data isn't being inserted into my database. I'm having no problems with the IPN system itself. I've had several orders verified and completed.

Here's the entire code (from trivia shock and my added on MySQL statements):

&lt;?php
////////////////////////////////////////////////////////////////////////////////
// example.php
// PHP paypal_ipn class example
////////////////////////////////////////////////////////////////////////////////

require("paypal_ipn.php");

// PayPal will send the information through a POST
$paypal_info = $HTTP_POST_VARS;

$paypal_ipn = new paypal_ipn($paypal_info);

// where to contact us if something goes wrong
$paypal_ipn-&gt;error_email = "email@email.com";

// We send an identical response back to PayPal for verification
$paypal_ipn-&gt;send_response();

// PayPal will tell us whether or not this order is valid.
// This will prevent people from simply running your order script
// manually
if( !$paypal_ipn-&gt;is_verified() )
{
// bad order, someone must have tried to run this script manually
$paypal_ipn-&gt;error_out("Bad order (PayPal says it's invalid)");
}

// payment status
switch( $paypal_ipn-&gt;get_payment_status() )
{
case 'Completed':
// order is good
break;

case 'Pending':
// money isn't in yet, just quit.
// paypal will contact this script again when it's ready
$paypal_ipn-&gt;error_out("Pending Payment");
break;

case 'Failed':
// whoops, not enough money
$paypal_ipn-&gt;error_out("Failed Payment");
break;

case 'Denied':
// denied payment by us
// not sure what causes this one
$paypal_ipn-&gt;error_out("Denied Payment");
break;

default:
// order is no good
$paypal_ipn-&gt;error_out("Unknown Payment Status" . $paypal_ipn-&gt;get_payment_status());
break;

} // end switch



// If we made it down here, the order is verified and payment is complete.
// You could log the order to a MySQL database or do anything else at this point.

//Write the data to my own database
$item_name = $HTTP_POST_VARS['item_name'];
$receiver_email = $HTTP_POST_VARS['receiver_email'];
$item_number = $HTTP_POST_VARS['item_number'];
$payment_status = $HTTP_POST_VARS['payment_status'];
$payment_gross = $HTTP_POST_VARS['payment_gross'];
$payer_email = $HTTP_POST_VARS['payer_email'];
$quantity = $HTTP_POST_VARS['quantity'];
$payment_date = $HTTP_POST_VARS['payment_date'];
$first_name = $HTTP_POST_VARS['first_name'];
$last_name = $HTTP_POST_VARS['last_name'];
$address_street = $HTTP_POST_VARS['address_street'];
$address_city = $HTTP_POST_VARS['address_city'];
$address_state = $HTTP_POST_VARS['address_state'];
$address_zip = $HTTP_POST_VARS['address_zip'];

$db = mysql_connect('localhost','username','password');
mysql_select_db('db_name');
$resultat_sql = mysql_query("INSERT INTO orders ('item_name', 'receiver_email', 'item_number', 'payment_status', 'payment_gross', 'payer_email', 'quantity', 'payment_date', 'first_name', 'last_name', 'address_street', 'address_city', 'address_state', 'address_zip') VALUES ('$item_name', '$receiver_email', '$item_number', '$payment_status', '$payment_gross', '$payer_email', '$quantity', '$payment_date', '$first_name', '$last_name', '$address_street', '$address_city', '$address_state', '$address_zip')",$db);


// Email the information to us
$date = date("D M j G:i:s T Y", time());


I'm not an expert PHP programmer (kinda obvious) but if anybody can help, I sure would appreciate it!




haydiz
coyote
#5 Posted : Saturday, April 12, 2003 3:02:52 AM
Rank: Starting Member

Groups: Registered

Joined: 4/9/2003
Posts: 12
Location: ,
Does your orders table have an auto-increment field? In other words, is a record being written to the table but all the fields are empty?

Have you checked that you have all fields listed in the insert statement that are in the orders table?
haydiz
#6 Posted : Saturday, April 12, 2003 11:28:43 AM
Rank: Starting Member

Groups: Registered

Joined: 4/10/2003
Posts: 10
Location: ,
I do have a primary key field (auto_increment). Nothing is being inserted into it as well.

I've double checked my fields and they all match.



haydiz
coyote
#7 Posted : Saturday, April 12, 2003 12:40:20 PM
Rank: Starting Member

Groups: Registered

Joined: 4/9/2003
Posts: 12
Location: ,
Well, this problem could be a whole slew of things.

The line number in your original post corresponds to which line in your example script? That may give you a clue as to where the problem is.

Try including the auto-increment field in your insert statement, like so: insert into orders ('ordersid','item_name'...) values ('',$item_name', ...). ordersid is the column name of your auto-increment field in the orders table.

If you aren't getting any info written to the db, then it sounds like your db connection is erroring out...

Can you connect to your database through phpmyadmin, or the console, and execute your sql statement without errors?

You may want to remove any '@' character in front of your sql statements in your included php file so that you can see the errors, or add 'or die blah blah'. Check the docs at www.mysql.com for more info.

Make sure you have the database connection parameters properly set to identify the database and user account, and that they are in scope.

Hope this helps.
haydiz
#8 Posted : Sunday, April 13, 2003 10:51:01 AM
Rank: Starting Member

Groups: Registered

Joined: 4/10/2003
Posts: 10
Location: ,
Okay, I totally scrapped both of the scripts above. I copied the PHP code from PayPal and added a MySQL insert statement and Hallelujah it works!

However, there is still a problem. It inserts all of the data except the 'item_name', 'item_number', and 'quantity' fields. But atleast I'm getting something in my database!

Here's the code:

&lt;?php
// read the post from PayPal system and add 'cmd'
$req = 'cmd=_notify-validate';

foreach ($_POST as $key =&gt; $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 ('www.paypal.com', 80, $errno, $errstr, 30);

// assign variables
$item_name = $_POST['item_name'];
$receiver_email = $_POST['receiver_email'];
$item_number = $_POST['item_number'];
$payment_status = $_POST['payment_status'];
$payment_gross = $_POST['payment_gross'];
$payer_email = $_POST['payer_email'];
$quantity = $_POST['quantity'];
$payment_date = $_POST['payment_date'];
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$address_street = $_POST['address_street'];
$address_city = $_POST['address_city'];
$address_state = $_POST['address_state'];
$address_zip = $_POST['address_zip'];

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
// process payment
//Write the data to my own database

$db = mysql_connect('localhost','username','pass');
mysql_select_db('db_name');
$resultat_sql = mysql_query("INSERT INTO orders (item_name, receiver_email, item_number, payment_status, payment_gross, payer_email, quantity, payment_date, first_name, last_name, address_street, address_city, address_state, address_zip) VALUES ('$item_name', '$receiver_email', '$item_number', '$payment_status', '$payment_gross', '$payer_email', '$quantity', '$payment_date', '$first_name', '$last_name', '$address_street', '$address_city', '$address_state', '$address_zip')",$db);

}
else if (strcmp ($res, "INVALID") == 0) {
// log for manual investigation
}
}
fclose ($fp);
}
?&gt;


haydiz
haydiz
#9 Posted : Sunday, April 13, 2003 11:01:50 AM
Rank: Starting Member

Groups: Registered

Joined: 4/10/2003
Posts: 10
Location: ,
I just found this in another thread so this is probably the solution to my problem.

"In the cart they are returned as:

num_cart_items=#
item_name1
item_number1
quantity1
item_name2
item_number2
quantity2
etc.

Patrick Breitenbach
PayPal, Inc.
Dev Net: https://www.paypal.com/pdn"

So, I suppose I'll have to assign my variables differently. I'm going to try a few things. When I get it right I'll post the correct code.



haydiz
haydiz
#10 Posted : Sunday, April 13, 2003 11:07:14 AM
Rank: Starting Member

Groups: Registered

Joined: 4/10/2003
Posts: 10
Location: ,
Here is the correct code for inserting records into your MySQL database.

&lt;?php
// read the post from PayPal system and add 'cmd'
$req = 'cmd=_notify-validate';

foreach ($_POST as $key =&gt; $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 ('www.paypal.com', 80, $errno, $errstr, 30);

// assign variables
$item_name = $_POST['item_name1'];
$receiver_email = $_POST['receiver_email'];
$item_number = $_POST['item_number1'];
$payment_status = $_POST['payment_status'];
$payment_gross = $_POST['payment_gross'];
$payer_email = $_POST['payer_email'];
$quantity = $_POST['quantity1'];
$payment_date = $_POST['payment_date'];
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$address_street = $_POST['address_street'];
$address_city = $_POST['address_city'];
$address_state = $_POST['address_state'];
$address_zip = $_POST['address_zip'];

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
// process payment
//Write the data to my own database

$db = mysql_connect('localhost','username','pass');
mysql_select_db('database_name');
$resultat_sql = mysql_query("INSERT INTO orders (item_name, receiver_email, item_number, payment_status, payment_gross, payer_email, quantity, payment_date, first_name, last_name, address_street, address_city, address_state, address_zip) VALUES ('$item_name1', '$receiver_email', '$item_number1', '$payment_status', '$payment_gross', '$payer_email', '$quantity1', '$payment_date', '$first_name', '$last_name', '$address_street', '$address_city', '$address_state', '$address_zip')",$db);

}
else if (strcmp ($res, "INVALID") == 0) {
// log for manual investigation
}
}
fclose ($fp);
}
?&gt;


haydiz
haydiz
#11 Posted : Sunday, April 13, 2003 11:16:30 AM
Rank: Starting Member

Groups: Registered

Joined: 4/10/2003
Posts: 10
Location: ,
The only problem with this is if more than one product is being purchased in your cart you'll only get the details from the first item in the cart although the total amount is correct. So I guess you'd have to add more fields like item_number2, item_number3, etc.

Uhm...

haydiz
mihark
#12 Posted : Sunday, August 03, 2003 4:16:30 PM
Rank: Starting Member

Groups: Registered

Joined: 8/3/2003
Posts: 5
Location: ,
I have tried out the last script you were using, and the script is posting back to paypal. I have included the script below. Please help. I have been trying to get the paypal IPN working a number of different ways with no luck and I am starting to pull out all my hair.

Thank you in advance,

&lt;?php
// read the post from PayPal system and add 'cmd'
$req = 'cmd=_notify-validate';

foreach ($_POST as $key =&gt; $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 ('www.paypal.com', 80, $errno, $errstr, 30);

// assign variables
$receiver_email = $_POST['receiver_email'];
$item_name = $_POST['item_name'];
$item_number = $_POST['item_number'];
$quantity = $_POST['quantity'];
$invoice = $_POST['invoice'];
$custom = $_POST['custom'];
$payment_status = $_POST['payment_status'];
$pending_reason = $_POST['pending_reason'];
$payment_date = $_POST['payment_date'];
$exchange_rate = $_POST['exchange_rate'];
$payment_gross = $_POST['payment_gross'];
$payment_fee = $_POST['payment_fee'];
$txn_id = $_POST['txn_id'];
$txn_type = $_POST['txn_type'];
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$address_street = $_POST['address_street'];
$address_city = $_POST['address_city'];
$address_state = $_POST['address_state'];
$address_zip = $_POST['address_zip'];
$address_country = $_POST['address_country'];
$address_status = $_POST['address_status'];
$payer_email = $_POST['payer_email'];
$payer_id = $_POST['payer_id'];
$payer_status = $_POST['payer_status'];
$payment_type = $_POST['payment_type'];
$verify_sign = $_POST['verify_sign'];
$subscr_date = $_POST['subscr_date'];
$subscr_effective = $_POST['subscr_effective'];
$period1 = $_POST['period1'];
$period2 = $_POST['period2'];
$period3 = $_POST['period3'];
$amount1 = $_POST['amount1'];
$amount2 = $_POST['amount2'];
$amount3 = $_POST['amount3'];
$recurring = $_POST['recurring'];
$reattempt = $_POST['reattempt'];
$retry_at = $_POST['retry_at'];
$recur_times = $_POST['recur_times'];
$username = $_POST['username'];
$password = $_POST['password'];
$subscr_id = $_POST['subscr_id'];

if (!$fp) {
// HTTP ERROR
echo "http error";
} else {
fputs ($fp, $header . $req);
while (!feof($fp)) {
$res = fgets ($fp, 1024);
if (strcmp ($res, "VERIFIED") == 0) {
if (strcmp ($payment_status, "Completed") == 0) {
$qry = "SELECT password FROM paypal WHERE item_number = \"$item_number\" ";
$result = mysql_query($qry,$db);
while ($myrow = mysql_fetch_row($result)) { $passwd = $myrow[0]; }
$message .= "Dear Customer,\n Thankyou for your order.\n\nThe password for the item you ordered is: $row[0]\n\nIf you have any problems, please contact us: \n\nholla\@hiphopg.com";
mail($payer_email, "Your Book Password...", $message, "From: holla@hiphopg.com\nReply-To: holla@hiphopg.com");
echo "It worked.";
// check the payment_status is Completed
// check that txn_id has not been previously processed
// check that receiver_email is your Primary PayPal email
// process payment
//Write the data to my own database

$db = mysql_connect('localhost','user','pass');
mysql_select_db('db');
$resultat_sql = mysql_query("INSERT INTO paypal(receiver_email,item_name,item_number,quantity,invoice,custom,payment_status,pending_reason,payment_date,payment_gross,payment_fee,txn_id,txn_type,first_name,last_name,address_street,address_city,address_state,address_zip,address_country,address_status,payer_email,payer_status,payment_type,verify_sign,subscr_date,period1,period2,period3,amount1,amount2,amount3,recurring,reattempt,retry_at,recur_times,username,password,subscr_id )
VALUES ('$invoice', '$receiver_email', '$item_name', '$item_number', '$quantity', '$payment_status', '$pending_reason', '$payment_date', '$payment_gross', '$payment_fee', '$txn_id', '$txn_type', '$first_name', '$last_name', '$address_street', '$address_city', '$address_state', '$address_zip', '$address_country', '$address_status', '$payer_email', '$payer_status', '$payment_type', '$custom', '$verify_sign', '$subscr_date','$period1', '$period2','$period3','$amount1','$amount2','$amount3','$recurring','$reattempt','$retry_at','$recure_times','$username','$password','$subscr_id' ) ",$db);

}
else if (strcmp ($res, "INVALID") == 0) {
echo "invalid";
// log for manual investigation
}
}
fclose ($fp);
}
?&gt;
BriM146
#13 Posted : Tuesday, August 05, 2003 12:59:25 AM
Rank: Starting Member

Groups: Registered

Joined: 8/5/2003
Posts: 2
Location: ,
Did anyone ever figure out how to enter purchase data into a database if more than one item was purchased through the paypal shopping cart? It seems that everything here will only take care of the first item.
pepe
#14 Posted : Thursday, August 07, 2003 11:16:27 PM
Rank: Starting Member

Groups: Registered

Joined: 4/30/2003
Posts: 105
Location: ,
Hi,

Yes we have a solution built exactly for this. Send us an email if you have specific questions or if you are looking for a system tailored to your needs.

Cheers,

Sharad Goel
Well Drafted Consultants Inc.
http://www.welldrafted.net
Users browsing this topic
Guest
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

YAFVision Theme by Jaben Cargman (Tiny Gecko)
Powered by YAF | YAF © 2003-2009, Yet Another Forum.NET
This page was generated in 0.520 seconds.