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

Adding data to my own database after a sale (IPN) Options
alfol
#1 Posted : Monday, March 24, 2003 11:49:54 PM
Rank: Starting Member

Groups: Registered

Joined: 3/24/2003
Posts: 3
Location: ,
'Hi, I am trying to use IPN to store info in my database when someone sends me a payment from the paypal shopping cart I am running on my site. I took the sample code on PayPal's site and added a few lines. My script does not seem to work ( the line that contains the sql statement is not reached ) and I know my sql statement is ok as I tested it. The script does not seem to go pass this line: if (strcmp ($res, 'VERIFIED') == 0) { I also noticed that the script is executed lots of times... Is this normal? There is an error so the server trys to execute it many times? I also noticed that when I access the script ( when I type the adress in my browser ) it does not load... It seems it is timed out or something like that... Anyway here is my code: <?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']; $address_country = $HTTP_POST_VARS['address_country']; 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','groupe1','********'); mysql_select_db('groupe1_db'); $resultat_sql = mysql_query("insert into Commande_Alex values('test-reussi', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ')"); } else if (strcmp ($res, 'INVALID') == 0) { // log for manual investigation } } fclose ($fp); } ?> Could you please tell me what's wrong? Thanks' Alexandre Alexandre
Sponsor  
 
Andrew Chang
#2 Posted : Sunday, March 30, 2003 10:18:24 PM
Rank: Starting Member

Groups: Registered

Joined: 1/10/2003
Posts: 738
Location: ,
try changing this block


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','groupe1','jiyohM8k');
mysql_select_db('groupe1_db');
$resultat_sql = mysql_query("insert into Commande_Alex values('test-reussi', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ')");


}
}
}
else if (strcmp ($res, 'INVALID') == 0) {
// log for manual investigation


fclose ($fp);
}
?&gt;

Andrew Chang
ScorpionSystems.net
Home Shopping Web Design Advertising And Much More.........
For web Design or advertising Contact Sales@scorpionsystems.net
Web Site http://scorpionsystems.net
haydiz
#3 Posted : Sunday, April 13, 2003 10:48:12 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
#4 Posted : Sunday, April 13, 2003 10:49:50 AM
Rank: Starting Member

Groups: Registered

Joined: 4/10/2003
Posts: 10
Location: ,
Oops I just added this reply to the wrong thread. I've been working on this too long :-)

haydiz
paypal_pb
#5 Posted : Sunday, April 13, 2003 5:57:59 PM
Rank: Starting Member

Groups: Registered

Joined: 9/16/2002
Posts: 2,960
Location: ,
If it's a shopping cart txn, you will receive item_name1, item_name2, etc.

Patrick Breitenbach
PayPal, Inc.
Dev Net: https://www.paypal.com/pdn
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.301 seconds.