Exemple #1
0
 *  @copyright  Copyright (c) 2015, Wade Shuler
 *  @license    http://choosealicense.com/licenses/gpl-2.0/
 */
// include the IpnListener Class, unless it's in your autoload
require_once dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'IpnListener.php';
use wadeshuler\paypalipn\IpnListener;
$listener = new IpnListener();
$listener->use_sandbox = true;
// Only needed for testing (sandbox), else omit or set false
if ($verified = $listener->processIpn()) {
    // Valid IPN
    /*
        1. Check that $_POST['payment_status'] is "Completed"
        2. Check that $_POST['txn_id'] has not been previously processed
        3. Check that $_POST['receiver_email'] is your Primary PayPal email
        4. Check that $_POST['payment_amount'] and $_POST['payment_currency'] are correct
    */
    $transactionRawData = $listener->getRawPostData();
    // raw data from PHP input stream
    $transactionData = $listener->getPostData();
    // POST data array
    // Feel free to modify path and filename. Make SURE THE DIRECTORY IS WRITEABLE!
    // For security reasons, you should use a path above/outside of your webroot
    file_put_contents('ipn_success.log', print_r($transactionData, true) . PHP_EOL, LOCK_EX | FILE_APPEND);
} else {
    // Invalid IPN
    $errors = $listener->getErrors();
    // Feel free to modify path and filename. Make SURE THE DIRECTORY IS WRITEABLE!
    // For security reasons, you should use a path above/outside of your webroot
    file_put_contents('ipn_errors.log', print_r($errors, true) . PHP_EOL, LOCK_EX | FILE_APPEND);
}
 /**
  * Process Paypal IPN
  * @return [type] [description]
  */
 public function paypal()
 {
     $errors = array();
     $listener = new IpnListener();
     $logTime = date('Y-m-d H:i:s', time());
     if ($this->option('sandbox') == "on") {
         $listener->use_sandbox = true;
     }
     if (isset($_POST['payment_status']) && $_POST['payment_status'] !== "Completed") {
         $errors[$logTime][] = "Payment not completed.";
     }
     if (isset($_POST['receiver_email']) && $_POST['receiver_email'] !== $this->option('paypal_email')) {
         $errors[$logTime][] = "Source ({$_POST['receiver_email']}) is not " . $this->option('paypal_email');
     }
     // Valid IPN
     if (empty($errors) && ($verified = $listener->processIpn() && isset($_POST['txn_id']))) {
         $transactionRawData = $listener->getRawPostData();
         // raw data from PHP input stream
         $transactionData = $listener->getPostData();
         // POST data array
         $content = array();
         foreach ($transactionData as $data) {
             $item = explode("=", $data);
             $content[$item[0]] = $item[1];
         }
         $post = wp_insert_post(array('post_title' => $_POST['txn_id'] . '(' . $_POST['payer_email'] . ' to ' . $_POST['item_name'] . ')', 'post_content' => json_encode($content), 'post_type' => 'donations', 'post_status' => 'publish'));
         if ($post) {
             $donation = new Donation($post);
             $donation->setAmount($_POST['mc_gross']);
             $donation->setProject($_POST['item_number']);
             $mailchimp_list_id = get_post_meta($_POST['item_number'], 'wppd_project_mailchimp_list_id', TRUE);
             $this->addToMailchimp($mailchimp_list_id, $_POST['payer_email']);
             file_put_contents(plugin_dir_path(__FILE__) . 'logs/success.log', print_r(array($logTime, $transactionData), TRUE) . PHP_EOL, LOCK_EX | FILE_APPEND);
             return TRUE;
         } else {
             $errors[$logtime][] = "Failed to save donation (Transaction ID: {$_POST['txn_id']}).";
         }
     } else {
         // Invalid IPN
         $errors[$logTime][] = "Invalid IPN: " . $listener->getErrors();
     }
     if (count($errors) > 0) {
         file_put_contents(plugin_dir_path(__FILE__) . 'logs/errors.log', print_r($errors, TRUE) . PHP_EOL, LOCK_EX | FILE_APPEND);
         return $errors;
     }
 }