Page 1 of 1
Paypal IPN
Posted: Fri Jun 23, 2006 11:21 pm
by Clukey
I'm trying to set up a site and use Paypal IPN to verify a payment then let the person download what they just purchased. I have activated IPN on my paypal account, but I don't know how to set up the php code on my end. Also, I tried using the sample code provided, but it doesn't work. Does anyone have any suggestions? Thanks.
Posted: Sat Jun 24, 2006 7:44 pm
by BZorch
I am trying to do the same thing this weekend for the first time.
Have you tried this site?
http://paypaltech.com/SG2/ Warning: this script asks for server/database names and password. Do not enter real information. Enter placeholder information and replace after you copy and paste the generated script.
Also, this has been helpful. But only because it showed me that I needed to change the following live from the live script to:
Code: Select all
$fp = @fsockopen( "www.sandbox.paypal.com", 80, &$errno, &$errstr, 120 );
http://www.19.5degs.com/element/369.php
I have been getting results with the script generator. One problem, I am having using the sandbox is that I am only getting a partial posting of my variables.
mc_fee
item_name
item_number
mc_gross
txn_id
payment_date
first_name
last_name
payment_type
payment_status
payer_email
These are the only variable that I can get to work. The rest return Undefined index: errors.
Has anyone else run into this? Unfortunately, Paypal's tech support for this is not open until Monday.
Posted: Sat Jun 24, 2006 9:32 pm
by BZorch
The fog is slowly lifting on how to do this, but there are still some unanswered questions. It seems that the reason the other variables were not posted is because they need to be called in the form. Can anyone confirm this? There is a standard set and then there are additional variable, but it is not clear how to access them. I know for instance that I need to include the custom field in the form dynamically in order to pass a users' id. I thought PayPal would do the rest. That does not seem to be the case. My IPN verification email shows that many of the variables that I was expecting were blank. For instance, a users address is not posted (only a shipping address). Obviously, this is not important if you are providing a on-line subscription service.
As I learn more, I will post. I am sure others are pulling their hair out as well. I know I am.
Posted: Sat Jun 24, 2006 9:44 pm
by Clukey
I found this code online, and it seems to work, I'm just having problems with my part now.
Code: Select all
// 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";
}
echo "test";
// 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
$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'];
// custom varible
$invoice_id = $_POST['invoice_id'];
//retrieve payment status
$payment_status = $_POST['payment_status'];
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
if ($payment_status == "Completed")
{
////////////////////////////////////////
//UPDATE DATABASE HERE //
//I also sent an email to myself here.//
////////////////////////////////////////
}
else if (strcmp ($res, "INVALID") == 0) {
// log for manual investigation
}
}
fclose ($fp);
}
Posted: Sat Jun 24, 2006 9:55 pm
by BZorch
Good to hear you are making progress. This looks similar to mine. Do you know how to pass more of the variable options in the IPN? The one I need is $txn_type, and subscr_id. Is it in the button or somewhere else?
Posted: Sat Jun 24, 2006 10:38 pm
by Clukey
BZorch wrote:Good to hear you are making progress. This looks similar to mine. Do you know how to pass more of the variable options in the IPN? The one I need is $txn_type, and subscr_id. Is it in the button or somewhere else?
I think those would go in the button.
Posted: Sat Jun 24, 2006 10:51 pm
by BZorch
You are right. I do not know why I missed it in the subscription manual. Though a closer study of my IPN verification email that I send during the IPN script does have the variables I need. There was just some confusion in my script that did not make the subscription post to trigger. I basically created the Paypal tables and kept all of the variables in from the script generator expecting the ones that were not passed, would just remain blank. Since the are not passed unless they exist, I had a ton of missing variable errors. It is getting clearer by the hour. Thanks for the information.
Posted: Sun Jun 25, 2006 3:31 pm
by BZorch
Just in case someone else does this bone head move, I thought I would save them some trouble. I used the Paypal table script to create the tables. I did not like the fact that it did not incude a primary key so I just added one and forgot to auto increment. I have not been able to add more than one record all day. I thought something was wrong with the script. Once the database included auto increment, it updates nicely.