public function paypalIpn() { $this->autoRender = false; // https://developer.paypal.com/developer/ipnSimulator/ $listener = new IpnListener(); $listener->use_sandbox = true; $listener->use_curl = true; $listener->follow_location = false; $listener->timeout = 30; $listener->verify_ssl = true; if ($verified = $listener->processIpn()) { $transactionData = $listener->getPostData(); file_put_contents('../logs/ipn_success.log', print_r($transactionData, true) . PHP_EOL, LOCK_EX | FILE_APPEND); $paymentsTable = TableRegistry::get('Payments'); $payment = $paymentsTable->newEntity(); if ($this->request->data['payment_status'] == "Completed") { $payment->gross_amount = $this->request->data['payment_status']; } $payment->provider = 'PayPal'; $payment->transaction_id = $this->request->data['txn_id']; $payment->transaction_type = $this->request->data['payment_type']; $payment->gross_amount = $this->request->data['mc_gross']; $payment->tax_amount = $this->request->data['tax']; $payment->fee_amount = $this->request->data['mc_fee']; $payment->currency = $this->request->data['mc_currency']; $payment->received_amount = $payment->gross_amount - $payment->fee_amount; $payment->quantity = floor($payment->gross_amount / Configure::read('WebAudit.CreditPrice')); if ($this->request->data['payment_status'] == "Completed") { $payment->status = 1; } else { $payment->status = 0; } $duplicatePayments = $paymentsTable->find('all')->where(['transaction_id' => $payment->transaction_id])->andWhere(['provider' => 'PayPal']); if ($duplicatePayments->isEmpty()) { if (!empty($this->request->data['custom'])) { $usersTable = TableRegistry::get('Users'); $userID = $this->request->data['custom']; $user = $usersTable->get($userID); if (!empty($user)) { $payment->user_id = $user->id; } } if ($paymentsTable->save($payment)) { $id = $payment->id; if (!empty($payment->user_id)) { $user->credit_amount += $payment->quantity; $usersTable->save($user); } } } } else { $errors = $listener->getErrors(); file_put_contents('../logs/ipn_errors.log', print_r($errors, true) . PHP_EOL, LOCK_EX | FILE_APPEND); } }
* @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; } }