Created
March 11, 2015 10:00
-
-
Save GreatPotato/192211de6d067f614bbc to your computer and use it in GitHub Desktop.
LS PayPal IPN
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public function process_paypal_ipn($params) | |
{ | |
try | |
{ | |
$order = null; | |
/* | |
* Find order and load paypal settings | |
*/ | |
sleep(5); | |
$order_hash = array_key_exists(0, $params) ? $params[0] : null; | |
if (!$order_hash) | |
throw new Phpr_ApplicationException('Order not found'); | |
$order = Shop_Order::create()->find_by_order_hash($order_hash); | |
if (!$order) | |
throw new Phpr_ApplicationException('Order not found.'); | |
if (!$order->payment_method) | |
throw new Phpr_ApplicationException('Payment method not found.'); | |
$order->payment_method->define_form_fields(); | |
$payment_method_obj = $order->payment_method->get_paymenttype_object(); | |
if (!($payment_method_obj instanceof Shop_PayPal_Standard_Payment)) | |
throw new Phpr_ApplicationException('Invalid payment method.'); | |
$endpoint = $order->payment_method->test_mode ? | |
"www.sandbox.paypal.com/cgi-bin/webscr" : | |
"www.paypal.com/cgi-bin/webscr"; | |
$fields = $_POST; | |
foreach($fields as $key => $value) | |
{ | |
//replace every \n that isn't part of \r\n with \r\n to prevent an invalid response from PayPal | |
$fields[$key] = preg_replace("~(?<!\r)\n~","\r\n",$value); | |
} | |
$fields['cmd'] = '_notify-validate'; | |
if(array_key_exists('txn_id', $fields)) | |
$transaction_id = $fields['txn_id']; | |
$response = Core_Http::post_data($endpoint, $fields); | |
if (!$order->payment_processed(false)) | |
{ | |
if (post('mc_gross') != strval($this->get_paypal_total($order, $order->payment_method))) | |
$this->log_payment_attempt($order, 'Invalid order total received in IPN: '.format_currency(post('mc_gross')), 0, array(), $_POST, $response); | |
else | |
{ | |
if (strpos($response, 'VERIFIED') !== false) | |
{ | |
if ($order->set_payment_processed()) | |
{ | |
Shop_OrderStatusLog::create_record($order->payment_method->order_status, $order); | |
$this->log_payment_attempt($order, 'Successful payment', 1, array(), $_POST, $response); | |
if(isset($transaction_id) && strlen($transaction_id)) | |
$this->update_transaction_status($order->payment_method, $order, $transaction_id, 'Processed', 'processed'); | |
} | |
} else | |
$this->log_payment_attempt($order, 'Invalid payment notification', 0, array(), $_POST, $response); | |
} | |
} | |
} | |
catch (Exception $ex) | |
{ | |
if ($order) | |
$this->log_payment_attempt($order, $ex->getMessage(), 0, array(), $_POST, null); | |
throw new Phpr_ApplicationException($ex->getMessage()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment