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); } }
/** * 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; } }
/** * Process the callback data from the payment provider */ public function callback($request) { $this->extend('onBeforeCallback'); $data = $this->request->postVars(); $status = "error"; $order_id = 0; $payment_id = 0; $error_url = Controller::join_links(Director::absoluteBaseURL(), Payment_Controller::config()->url_segment, 'complete', 'error'); // Check if CallBack data exists and install id matches the saved ID if (isset($data) && isset($data['custom']) && isset($data['payment_status'])) { $order_id = $data['custom']; $paypal_request = 'cmd=_notify-validate'; $final_response = ""; // If the transaction ID is set, keep it if (array_key_exists("txn_id", $data)) { $payment_id = $data["txn_id"]; } $listener = new IpnListener(); if (Director::isDev()) { $listener->use_sandbox = true; } try { $verified = $listener->processIpn(); } catch (Exception $e) { error_log("Exception caught: " . $e->getMessage()); return $this->httpError(500); } if ($verified) { // IPN response was "VERIFIED" switch ($data['payment_status']) { case 'Canceled_Reversal': $status = "canceled"; break; case 'Completed': $status = "paid"; break; case 'Denied': $status = "failed"; break; case 'Expired': $status = "failed"; break; case 'Failed': $status = "failed"; break; case 'Pending': $status = "pending"; break; case 'Processed': $status = "pending"; break; case 'Refunded': $status = "refunded"; break; case 'Reversed': $status = "canceled"; break; case 'Voided': $status = "canceled"; break; } } else { error_log("Invalid payment status"); return $this->httpError(500); } } else { error_log("No payment details set"); return $this->httpError(500); } $payment_data = ArrayData::array_to_object(array("OrderID" => $order_id, "PaymentProvider" => "PayPal", "PaymentID" => $payment_id, "Status" => $status, "GatewayData" => $data)); $this->setPaymentData($payment_data); $this->extend('onAfterCallback'); return $this->httpError(200); }
* implement a PayPal Instant Payment Notification (IPN) listener script. * * This package is available at GitHub: * https://github.com/WadeShuler/PHP-PayPal-IPN/ * * @package PHP-PayPal-IPN * @link https://github.com/WadeShuler/PHP-PayPal-IPN * @forked https://github.com/Quixotix/PHP-PayPal-IPN * @author Wade Shuler * @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!