PDA

View Full Version : Desperate IPN Help



JonnyH
June 20th, 2008, 05:11
I've never got the hang of this.. I'm a such Noob when it comes to paypal. I'm using Joe's paypal IPN which is great, but I don't know what to do it with it :knockedou

It shows the checkout, then uses ajax to confirm this and post these details to cP Creator for insertion and then calls paypal. The thing is, what details do I put in an array for a paypal? Why, when the function is called does just take a long time and do nothing?

Here's the checkout:

//Checkout
function checkout($time) {
//Split sessions up
foreach($_SESSION as $key => $content) {
${$key} = $content;
}
if(!isset($_SESSION['IRGENUINE']) /*|| isset($_SESSION['pdone'])*/) {
echo "Error: This isn't a genuine checkout call";
}
else {
mysql_query("INSERT INTO `{$this->sql['pre']}paiddetails`
(user ,fname, lname, address, address2, city, region, zip, country, num)
VALUES('{$user}','{$fname}','{$lname}','{$address}','{$addre ss2}','{$city}','{$region}','{$zip}','{$country}','{$num}')", $this->sql['con']);
$_SESSION['pdone'] = 1;

//Use the Paypal Script
$this->paypal($details);
}
}

Here's the Paypal function:

//Sends user to paypal - CREATED BY KRAKJOE @ http://freewebspace.net/forums/showthread.php?t=2199767
function paypal($details) {
define( "PAYPAL_WEBSCR", "www.paypal.com" ); # Server to use, should normally be www.paypal.com
define( "PAYPAL_ADDRESS", "paypal@kwix-host.com" ); # paypal address expected
define( "PAYPAL_CURRENCY", "USD"); # Currency of transaction
define( "PAYPAL_REQUIRED_STATUS", "completed" ); # All payments should be completed before we take automated action
define( "PAYPAL_ATTEND", $this->config("adminemail") ); # Address to send notifications generated by this code to

if( get_magic_quotes_gpc() ) $details = array_map( 'stripslashes', $_REQUEST );

if( count( $details ) )
{
if( ( $sock = fsockopen( PAYPAL_WEBSCR, 80, $errno, $errstr, 5 ) ) ) # Open a socket to paypal
{
$post[ ] = "cmd=_notify-validate"; # Append what we're doing here

foreach( $details as $k => $v ) $post[ ] = sprintf( "%s=%s", $k, urlencode( $v ) ); # Build postback

$post = implode( '&', $post ); # Finalize postback

$request[ ] = "POST /cgi-bin/webscr HTTP/1.0"; # POST request
$request[ ] = sprintf( "Content-length: %d", strlen( $post ) ); # Length of final postback
$request[ ] = "Content-type: application/x-www-form-urlencoded"; # Duh
$request[ ] = "" ; # HTTP Compliant post request
$request[ ] = $post ; # Final post string

fwrite( $sock, sprintf( "%s\r\n", implode("\r\n", $request ) ) ); # Write postback to paypal

while( !feof( $sock ) ) $response .= fgets( $sock ); # Get response from paypal server

fclose( $sock ); # Close socket
}

foreach( $details as $k => $v ) $email[ ] = sprintf( "%s:\t%s", $k, urldecode( $v ) ); # Build an email for later

$email[ ] = sprintf( "RAW RESPONSE:\n%s\n", $response ); # Append raw response to email

$email = implode("\n", $email ); # Finalize email

if( ereg( "VERIFIED", $response ) ) # Ensure the customer was verified
{
if( strtolower( $_REQUEST['payment_status'] ) == PAYPAL_REQUIRED_STATUS ) # Check for completed payment, echeck etc dont' complete instantly
{
if( $_REQUEST['receiver_email'] == PAYPAL_ADDRESS ) # Check for correct email address
{
if( $_REQUEST['mc_currency'] == PAYPAL_CURRENCY ) # Check for correct currency
{
// your site specific code here
}
else mail( PAYPAL_ATTEND, "Non standard currency used", $email );
}
else mail( PAYPAL_ATTEND, "Payment to wrong address", $email );
}
else mail( PAYPAL_ATTEND, "Non completed payment", $email );
}
else mail( PAYPAL_ATTEND, "Suspicious payment recieved", $email );

mail( PAYPAL_ATTEND, "TEST", $email );
}
}

I sooo need help with this. Im struggling.

krakjoe
June 20th, 2008, 05:48
Morning Johnny ...

I think I best explain the IPN thing a bit better ...

Your application never calls IPN handling code. All your application needs to do is collect the checkout information from the user insert into into the database and forward the request to Paypal with a button or link. The IPN handling code is called by Paypal's server when the payment goes through ( any time a payment status changes actually ), if you look at the IPN code closely, you'll see it opening a connection to paypal and writing everything that it recieved in the _REQUEST arrray back to paypals server - this is how authentication is done - if when the ipn code is executed by the paypal server you recieve VERIFIED back from Paypal you update your database and in this case I expect create the account and or email the user their login.

In short,

collect the checkout information into a database,
create a paypal button with the stuff in the database ( using a form field named "custom" for the id relative to this transaction probably from mysql_insert_id )
display the button to the user
when the user clicks on button, signs into paypal and pays, paypal will call your IPN script ( you can set this with notify_url in the paypal form you generated )
have your ipn script do the donkey work in the background, create the account etc ... ( this code should replace the comment // your site specific code here )

JonnyH
June 20th, 2008, 06:16
Mornin Joe.

That explains everything. Thanks for that explaination for my dumb self. It's appreciated.