/** * 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; } }