<?php /* * This script verifies and logs to a file all received IPN requests. * You can use it with PayPal IPN Simulator, just upload it to your server * and specify its path as IPN handler URL: * https://developer.paypal.com/developer/ipnSimulator/ * * Make sure that your web server has write access to the folder with this script, * otherwise you won't see any logs being created. */ include dirname(dirname(__FILE__)) . '/src/IpnListener.php'; use dezlov\PayPal\IpnListener; $listener = new IpnListener(); $listener->use_sandbox = true; $error = null; $verified = $listener->tryProcessIpn(null, $error); $report = $listener->getTextReport(); ipn_log($verified, $report, $error); function ipn_log($verified, $report, $error) { if ($verified) { $filename = 'ipn_verified.log'; $content = $report; } else { $filename = 'ipn_errors.log'; $content = 'ERROR: ' . $error . PHP_EOL . $report; } file_put_contents($filename, $content, FILE_APPEND | LOCK_EX); }
<?php /* * A basic script which will verify IPN via the sandbox server. * If an error occurs inside of processIpn() we will catch it * and output full text report. * * Note that 'echo' commands are just for demonstration purposes. * In reality, your script is going to be called by PayPal server and you * will not actually see the output unless you log it somewhere on your server. */ include dirname(dirname(__FILE__)) . '/src/IpnListener.php'; use dezlov\PayPal\IpnListener; $listener = new IpnListener(); $listener->use_sandbox = true; try { $verified = $listener->processIpn(); if ($verified) { echo 'IPN request was "VERIFIED".' . PHP_EOL; } else { echo 'IPN request was "INVALID".' . PHP_EOL; } } catch (Exception $e) { echo 'IPN error: ' . $e->getMessage() . PHP_EOL; echo $listener->getTextReport(); }
throw new Exception('Unable to process IPN due to issues with price/currency'); } // If we got this far, then its ok to fulfil the order $item_name = $listener->getData('item_name'); $item_number = $listener->getData('item_number'); $payer_name = trim($listener->getData('first_name') . ' ' . $listener->getData('last_name')); $payer_email = $listener->getData('payer_email'); process_order($transaction_id, $item_name, $item_number, $payer_name, $payer_email); // Tell PayPal that we have successfully processing IPN. header('HTTP/1.1 200 OK'); } catch (Exception $e) { // Tell PayPal that we had problems processing IPN. header('HTTP/1.1 500 Internal Server Error'); // Report error message. $message = 'IPN error: ' . (string) $e; $message .= PHP_EOL . PHP_EOL . $listener->getTextReport(); report_problem($message); } function process_order($transaction_id, $item_name, $item_number, $payer_name, $payer_email) { // For example: Fulfil the order, save details in the database. } function check_price($total, $currency) { // For example: Check that payment matches your advertised price. return true; } function check_receiver_email($email) { // For example: Check that email matches your PayPal account. return true;