Jump to content

[SOLVED] Code Explanation


coop

Recommended Posts

Hi all,

 

Still new to PHP, but with a ActionScript background - I'm trying to run through this class which is for IPN with Paypal. I sort of get the idea but I'm trying to get a better understanding, could anyone explain this code. I know it's a big ask but any help would br greatly appreciated.

 

<?php
class paypal_ipn{
var $paypal_post_vars;
var $paypal_response;
var $timeout;
var $error_email;
function paypal_ipn($paypal_post_vars) {
	$this->paypal_post_vars = $paypal_post_vars;
	$this->timeout = 120;
}
function send_response(){
	$fp = @fsockopen( "www.paypal.com", 80, &$errno, &$errstr, 120 ); 
	if (!$fp) { 
		$this->error_out("PHP fsockopen() error: " . $errstr , "");
	} else {
		foreach($this->paypal_post_vars AS $key => $value) {
			if (@get_magic_quotes_gpc()) {
				$value = stripslashes($value);
			}
			$values[] = "$key" . "=" . urlencode($value);
		}
		$response = @implode("&", $values);
		$response .= "&cmd=_notify-validate";
		fputs( $fp, "POST /cgi-bin/webscr HTTP/1.0\r\n" ); 
		fputs( $fp, "Content-type: application/x-www-form-urlencoded\r\n" ); 
		fputs( $fp, "Content-length: " . strlen($response) . "\r\n\n" ); 
		fputs( $fp, "$response\n\r" ); 
		fputs( $fp, "\r\n" );
		$this->send_time = time();
		$this->paypal_response = ""; 
		// get response from paypal
		while (!feof($fp)) { 
			$this->paypal_response .= fgets( $fp, 1024 ); 
			if ($this->send_time < time() - $this->timeout) {
				$this->error_out("Timed out waiting for a response from PayPal. ($this->timeout seconds)" , "");
			}
		}
		fclose( $fp );
	}
}
function is_verified() {
	if( ereg("VERIFIED", $this->paypal_response) )
		return true;
	else
		return false;
} 
function get_payment_status() {
	return $this->paypal_post_vars['payment_status'];
}
function error_out($message, $em_headers){
	$date = date("D M j G:i:s T Y", time());
	$message .= "\n\nThe following data was received from PayPal:\n\n";
	@reset($this->paypal_post_vars);
	while( @list($key,$value) = @each($this->paypal_post_vars)) {
		$message .= $key . ':' . " \t$value\n";
	}
	mail($this->error_email, "[$date] paypay_ipn notification", $message, $em_headers);
}
} 
?>

Link to comment
Share on other sites

OK, I'll try to make this clear. I'll comment each section and/or function for you:

<?php
class paypal_ipn{
var $paypal_post_vars;
var $paypal_response;
var $timeout;
var $error_email;

// This is your constructor, when it runs, you pass in the values that you have
// received from paypal as an array.
function paypal_ipn($paypal_post_vars) {
	// Assign the array passed in to the member variable
	$this->paypal_post_vars = $paypal_post_vars;

	// Set the execution timeout
	$this->timeout = 120;
}

// Function that sends a response to alert paypal as to whether or not you
// received a successful data transmission
function send_response(){
	// Open a connection to paypal
	$fp = @fsockopen( "www.paypal.com", 80, &$errno, &$errstr, 120 ); 

	if (!$fp) { 
		// Connection to paypal failed
		$this->error_out("PHP fsockopen() error: " . $errstr , "");
	} else {
	  // Connection successful, loop through all your paypal variables and create
		// a query string to pass as a response
		foreach($this->paypal_post_vars AS $key => $value) {
			if (@get_magic_quotes_gpc()) {
				// Clean the values
				$value = stripslashes($value);
			}
			$values[] = "$key" . "=" . urlencode($value);
		}
		// Create a URL readable string
		$response = @implode("&", $values);

		// Add the command "_notify-validate" to pass to paypal
		$response .= "&cmd=_notify-validate";

		// POST the result string you have created to the paypal page
		fputs( $fp, "POST /cgi-bin/webscr HTTP/1.0\r\n" ); 
		fputs( $fp, "Content-type: application/x-www-form-urlencoded\r\n" ); 
		fputs( $fp, "Content-length: " . strlen($response) . "\r\n\n" ); 
		fputs( $fp, "$response\n\r" ); 
		fputs( $fp, "\r\n" );

		// Record the time of your transmission
		$this->send_time = time();

		// Clear the paypal response variable in preparation for receiving an answer
		$this->paypal_response = ""; 

		// get response from paypal
		while (!feof($fp)) { 
			$this->paypal_response .= fgets( $fp, 1024 ); 
			if ($this->send_time < time() - $this->timeout) {
				// If you have been waiting for more than your timeout time, throw an error
				$this->error_out("Timed out waiting for a response from PayPal. ($this->timeout seconds)" , "");
			}
		}

		// Close your file connection to paypal
		fclose( $fp );
	}
}

// Function simply returns a boolean value TRUE if the paypal response was
// successful, FALSE otherwise
function is_verified() {
	if( ereg("VERIFIED", $this->paypal_response) )
		return true;
	else
		return false;
} 

// Returns the payment status that was in the initial POST vars
function get_payment_status() {
	return $this->paypal_post_vars['payment_status'];
}

// This function handles all the errors for you
function error_out($message, $em_headers){
	// Current date
	$date = date("D M j G:i:s T Y", time());
	$message .= "\n\nThe following data was received from PayPal:\n\n";
	// Reset member variables since you had an error
	@reset($this->paypal_post_vars);
	// Display all the values that were in the POST vars
	while( @list($key,$value) = @each($this->paypal_post_vars)) {
		$message .= $key . ':' . " \t$value\n";
	}
	// Email the error to the provided error email
	mail($this->error_email, "[$date] paypay_ipn notification", $message, $em_headers);
}
} 
?>

 

Does this help?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.