Samu funkciju sam negde "pokupio" i malo prilagodio. Klasa i test primer odakle je ovo izvučeno je prikačeno na poruku. Probao sam da vidim nešto gotovo pošto nisam imao vremena sam da se cimam (par sati proučavanja dokumentacije i eksperimentisanja), ali mi se čini da će na kraju ipak biti tako... Korisnik se redirektuje na Success stranicu, ali verifikacija ne prođe kako treba.
Pri kraju je dodato logovanje odgovora u fajl, a $this->finish_order() menja stanje porudžbine i čuva izmene u bazu podataka.
PHP kôd:
/**
* OK, verify payment
*
* @access public
* @param void
* @return null
*/
function paypal_notify() {
// Prepare
$url_parsed = parse_url('https://www.paypal.com/cgi-bin/webscr');
$ipn_data = array();
$ipn_response = '';
$post_string = '';
// Generate the post string from the $_POST vars aswell as load the
// $_POST vars into an arry so we can play with them from the calling
// script.
foreach ($_POST as $field => $value) {
$ipn_data[$field] = $value;
$post_string .= $field.'='.urlencode($value).'&';
} // foreach
// Add to post string...
$post_string .= 'cmd=_notify-validate'; // append ipn command
// Init..
$err_no = null;
$err_str = null;
// open the connection to paypal
$fp = fsockopen($url_parsed['host'], '80', $err_no, $err_str, 30);
// FSock?
if(!$fp) {
die();
} else {
// Post the data back to paypal
fputs($fp, "POST" . $url_parsed['path'] . "HTTP/1.1\r\n");
fputs($fp, "Host: " . $url_parsed['host'] . "\r\n");
fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n");
fputs($fp, "Content-length: " . strlen($post_string) . "\r\n");
fputs($fp, "Connection: close\r\n\r\n");
fputs($fp, $post_string . "\r\n\r\n");
// loop through the response from the server and append to variable
while(!feof($fp)) $ipn_response .= fgets($fp, 1024);
fclose($fp); // close connection
} // if
// Prepare for file and write it to the file...
$for_file = "Post string:\r\n\r\n$post_string\r\n\r\nResponse:\r\n\r\n$ipn_response";
@write_in_file( dirname(__FILE__) . '/lasttransaction.txt', $for_file );
// Vefied?
if (eregi("VERIFIED", $ipn_response)) {
$this->finish_order(false);
} // if
} // paypal_notify