PDA

View Full Version : Paypal IPN, anyone did this before??


ElvansX
11-22-05, 10:06 AM
Hi,

I did my searched but couldnt find any post/thread that similar with my problem, so i start this.
Senario:
I want to make a donation for my site and a member who donate will get a access for download. So i login to my paypal account, and enable IPN and auto return. i created a paypal button, and put it somewhere on my website. It look like this :


<form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_xclick">
<input type="hidden" name="business" value="email@domain.com">
<input type=hidden name=item_name value="Subscribe">
<input type=hidden name=amount value="1">
<input type=hidden name=item_number value="$itemnumber"11282>
<input type="hidden" name="no_note" value="1">
<input type="hidden" name="currency_code" value="USD">
<input type="hidden" name="lc" value="US">
<input type="image" src="https://www.paypal.com/en_US/i/btn/x-click-but23.gif" border="0" name="submit" alt="Make payments with PayPal - it's fast, free and secure!">
</form>

* no problem with this button.

then the code i copied from paypal and modified a bit to check whick part is wrong. Notice
echo "CHK #<br>";

the ipn code:

<?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 ('www.sandbox.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'];

if (!$fp) {
echo "CHK 1<br>";
} else {

echo "CHK 2<br>";

fputs ($fp, $header . $req);
echo "CHK 3<br>";

$anime_donate = 5;
$userid = 2;

global $db;


while (!feof($fp)) {
//echo "CHK 4<br>";
$res = fgets ($fp, 1024);
if (strcmp ($res, "VERIFIED") == 0) {

echo "CHK 5<br>";

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

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

echo "CHK 6<br>";

// log for manual investigation
}
}
echo "CHK 7<br>";
fclose ($fp);
echo "CHK 8<br>";
}
?>


The process:
user will click the paypal button and redirrect to paypal page. They will login with paypal account and after finish, paypal will redirrect them back to my page(use auto-return) which is the ipn page(code above).

The result for the page should be blank, but i insert the
echo "CHK #<br>";
to see which part is wrong.
the result is here:

CHK 2
CHK 3
CHK 4
CHK 4
CHK 4
CHK 4
CHK 4
CHK 4
CHK 4
CHK 4
CHK 4
CHK 6
CHK 7
CHK 8


Problem:
As you can see >> CHK 5 is missing, and want to put my code inside to auto validate my member so that they can start download after donate.

Am i missing something or powweb didnt support/enable fsockopen or anything else?
im still newbie with PHP, kindly please explain to me step by step.

Thank you.

ElvansX
11-22-05, 10:51 AM
I do a testing for my code with this web:


https://www.eliteweaver.co.uk/testing/ipntest.php


I change the button to redirrect to this address and
$fp = fsockopen ('www.sandbox.paypal.com', 80, $errno, $errstr, 30);
to
$fp = fsockopen ('www.eliteweaver.co.uk', 80, $errno, $errstr, 30);

i need to post the ipn value to my ipn page and see it reply or not.
And the result is like this:

Your simulated IPN has been sent successfully IPN Sent!

Your status report appears below:

IPN Creation Date: PDT
IPN Expiration Date: PDT
Send Count:
Target IPN Script: http://www.domain.com/ipn.php

IPN Sent: PDT
HTTP Status: HTTP/1.1 200 OK
IPN Received: Your script did not reply!
Post Back: N/A

Variables Sent:
Variables Expected: 1
Variables Received: 0

String Length Sent:
String Length Expected: 21
String Length Received: 0

Desired Response:
Actual Response: None
Intentional Response: No!


Any comment what when wrong??

ElvansX
11-24-05, 09:03 PM
No one reply?
Yesterday i find out that actually nothing wrong with the code(might be) or fsockopen.

The script suppost to return VERIFIED, but i only got INVALID all the times.
Any ideas?

MadSage
11-27-05, 10:33 AM
I do a testing for my code with this web:


https://www.eliteweaver.co.uk/testing/ipntest.php


I change the button to redirrect to this address and
$fp = fsockopen ('www.sandbox.paypal.com', 80, $errno, $errstr, 30);
to
$fp = fsockopen ('www.eliteweaver.co.uk', 80, $errno, $errstr, 30);

i need to post the ipn value to my ipn page and see it reply or not.
And the result is like this:



I've been having trouble getting PayPal IPN working for more than a month, and I'm extremely puzzled. I had never tried the eliteweaver test before, but I tried it today and got exactly the same result as you. My script did not respond. It does look as if fsockopen isn't working... :confused:

I will do some more testing. The script I am currently using is the simple example from PayPal's website. I decided to go back to basics because I'm so confused.

MadSage
11-27-05, 11:37 AM
I've looked around and discovered that many people have had this same problem, at least since 2004. I'm guessing the eliteweaver test site is not kept up to date. I gave up testing with eliteweaver and went back to using the PayPal Sandbox for testing. Now I'm getting somewhere!

If you want to log output while using the sandbox, do something like this:

$logfp = fopen("log.txt", "at");
fputs($logfp, "VERIFIED");
fclose($logfp);

linnetwoods
11-27-05, 02:05 PM
Did you create an autoreturn page with the content specified by PayPal? They are very specific about what the contents must include.

ElvansX
11-29-05, 01:55 AM
i figured out that the "autoreturn url" and "IPN url" cannot be same url.
If not it will return error.

Not im able to get the variable but the request return is INVALID.
I also try $_SERVER['HTTP_REFERER'] is not working. maybe bcause HTTPS

MadSage, i'll the your code. Thanks

ElvansX
12-1-05, 08:15 AM
MadSage,

i tried ur code. I put it like this:

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

$logfp = fopen("log.txt", "at");
fputs($logfp, "VERIFIEDSS");
fclose($logfp);


and the output is always like this:
VERIFIEDVERIFIEDVERIFIED

What does it mean? Why VERIFIEDx3???

stevel
12-4-05, 08:02 PM
Probably because this code is called three times.