Magento NOCHEX extension faults and fixes

A customer of ours had opted to use Nochex as their payment gateway of choice for their most recent build - so we grabbed an existing extension to facilitate it. However, it seemed the Luxe Nochex extension was buggy (just like the Magento Paypoint gateway we tried). We didn't have time to wait for the authors of the software, so a quick fix was in order. The APC automatic payment confirmation (Nochex's equivalent to Paypal's IPN) was not posting the correct information to be validated. Looking at ./app/code/local/Luxe/Nochex/controllers/NochexController.php, we found a flaw in the CURL options. To fix it, around line 79 - we commented out their code (for clarity) and added in the following lines
 curl_setopt($ch, CURLOPT_URL, "https://www.nochex.com/nochex.dll/apc/apc");
# curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
# curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
# curl_setopt($ch, CURLOPT_POSTFIELDS, $request_vars);
 
 curl_setopt ($ch, CURLOPT_POST, true);
 curl_setopt ($ch, CURLOPT_POSTFIELDS, $request_vars);
 curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
 curl_setopt ($ch, CURLOPT_TIMEOUT, 60);
 curl_setopt ($ch, CURLOPT_SSLVERSION, 3);
 curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, false);
 curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
 
 $response = curl_exec($ch);
We actually cleaned up NochexController.php a little more so we could report declined transactions and log appropriately.
<?php
/**
 * Luxe
 *
 * @category Luxe
 * @package Luxe_Nochex
 * @copyright Copyright (c) 2009 Luxe Soft
 */


/**
 * Nochex controller
 *
 * @category Luxe
 * @package Luxe_Nochex
 */

class Luxe_Nochex_NochexController extends Mage_Core_Controller_Front_Action
{
 protected $_order;

 public function redirectAction()
 {
 $session = Mage::getSingleton('checkout/session');
 $payment = Mage::getModel('nochex/nochex');
 $order = Mage::getModel('sales/order');

 $order->loadByIncrementId($session->getLastRealOrderId());
 $order->addStatusToHistory($payment->getNewPaymentStatus(), 'Customer was redirected to Nochex.');
 $order->save();
 $this->getResponse()->setBody($this->getLayout()->createBlock('nochex/redirect')->toHtml());
 }

 public function statusAction()
 {
 Mage::log('NOCHEX:'.print_r($_REQUEST, true));

 $payment = Mage::getModel('nochex/nochex');
 if (!$payment->getConfigData('active')) {
 die('Nochex method disabled');
 }

 $request_vars = '';
 foreach ($_REQUEST as $key => $value) {
 $request_vars .= urlencode($key).'='.urlencode($value).'&';
 }
 $request_vars = substr($request_vars, 0, -1);
 $ch = curl_init();
 curl_setopt($ch, CURLOPT_URL, "https://www.nochex.com/nochex.dll/apc/apc");
# curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
# curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
# curl_setopt($ch, CURLOPT_POSTFIELDS, $request_vars);
 
 curl_setopt ($ch, CURLOPT_POST, true);
 curl_setopt ($ch, CURLOPT_POSTFIELDS, $request_vars);
 curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
 curl_setopt ($ch, CURLOPT_TIMEOUT, 60);
 curl_setopt ($ch, CURLOPT_SSLVERSION, 3);
 curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, false);
 curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
 
 $response = curl_exec($ch);
 curl_close($ch);
 
 #Mage::log('NOCHEX URL https://www.nochex.com/nochex.dll/apc/apc?'.$request_vars);
 Mage::log('NOCHEX Response '.$response);
 
 if ($response == 'AUTHORISED') {
 $order = Mage::getModel('sales/order');
 $order = $order->loadByIncrementId(intval($_REQUEST['order_id']));

 $order->getPayment()->setData('nochex_mode', $_REQUEST['status']);
 $order->getPayment()->setData('nochex_transaction_id', $_REQUEST['transaction_id']);
 $order->addStatusToHistory($payment->getApprovedPaymentStatus(), 'Customer returned from Nochex. Order approved and payment recieved.', true);
 $order->sendNewOrderEmail();
 $order->setEmailSent(true);
 $order->save();
 }
 else if ($response == 'DECLINED' && @$_REQUEST['order_id']) {
 $order = Mage::getModel('sales/order');
 $order = $order->loadByIncrementId(intval($_REQUEST['order_id']));
 $order->addStatusToHistory($payment->getNewPaymentStatus(), 'Order declined', true); 
 $order->save(); 
 }
 }
}
[syntaxhighlighter]