PHP Paypal IPN Class
May 31st 2010
I recently was asked to integrate a website with PayPal's IPN system. I have worked with PayPal in the past on a few other shopping cart projects however they where with the PayPal website payment pro accounts. So I needed to work with a Standard PayPal Account that securely sent payment information to my script when a payment was processed.
I ended up finding out about PayPal's IPN Integration. So I started messing around with it and realized how easy it is to work with. Here is the simple class I made based of PayPal's sample code.
<?php
/*
** VARIUX INC.
** PAYPAL IPN LISTENER
** 05-27-2010
*/
class paypal_ipn_listener{
private $postdata;
private $logger;
private $opensocket;
public $valid;
public $results = array();
/*
* GET POSTED DATA / SET POSTED RESULTS / REQUEST VALIDATION
*/
public function construct($log = false){
$this->postdata = $_POST;
$this->logger = $log;
$this->set_results();
$this->request();
}
/*
* SET RESULTS
*/
private function set_results(){
$this->results['first_name'] = $this->postdata['first_name'];
$this->results['last_name'] = $this->postdata['last_name'];
$this->results['contact_phone'] = $this->postdata['contact_phone'];
$this->results['address_zip'] = $this->postdata['address_zip'];
$this->results['address_street'] = $this->postdata['address_street'];
$this->results['item_name'] = $this->postdata['item_name'];
$this->results['custom'] = $this->postdata['custom'];
$this->results['item_number'] = $this->postdata['item_number'];
$this->results['payment_status'] = $this->postdata['payment_status'];
$this->results['payment_fee'] = $this->postdata['payment_fee'];
$this->results['payment_gross'] = $this->postdata['payment_gross'];
$this->results['payment_currency'] = $this->postdata['mc_currency'];
$this->results['txn_id'] = $this->postdata['txn_id'];
$this->results['receiver_email'] = $this->postdata['receiver_email'];
$this->results['payer_email'] = $this->postdata['payer_email'];
}
/*
* REQUEST VALIDATION
*/
private function request(){
$req = 'cmd=_notify-validate';
foreach ($this->postdata as $key => $value) {
$value = urlencode(stripslashes($value));
$req .= "&$key=$value";
}
$this->opensock = fsockopen ('ssl://www.paypal.com', 443, $errno, $errstr, 30);
if($this->opensock){
$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";
fputs ($this->opensock, $header . $req);
while (!feof($this->opensock)) {
$res = fgets ($this->opensock, 1024);
if (strcmp ($res, "VERIFIED") == 0) {
$this->valid = true;
}elseif (strcmp ($res, "INVALID") == 0) {
$this->valid = false;
}
}
}else{
$this->valid = false;
}
fclose($this->opensock);
}
/*
* LOG EVENT
*/
private function logger(){
if(($this->logger) && (is_writable($this->logger))){
$logdata = "////// START RESPONSE " . date("Y-m-d G:h:i") . " //////\n";
foreach($this->results as $f => $value){
$logdata .= $f . ' - ' . $value . "\n";
}
$logdata .= "\nSTATUS: " . $this->valid . "\n\n";
$logdata .= "////// END RESPONSE //////\n\n\n";
$fopen = fopen($this->logger,'a+');
$fwrite = fwrite($fopen,$logdata);
fclose($fopen);
}
}
}
?>
