<?php

//NMI wrapper
class NMI {   
    
    private $url = 'https://secure.networkmerchants.com/api/transact.php';
    private $Username = "demo";  
    private $Password = "password";
    
    private $mode = 'TEST'; // TEST, LIVE
            
    function NMI() {        
        if($this->mode == 'LIVE'){
            $Username = 'demo';
            $Password = 'password';
        }                
    }

    function AuthorizeAndCapture($Amount = '', $CardNumber = '', $CardExpMonth = '', $CardExpYear = '', $CardCVV = '') {
        $type = "sale";
        if(strlen($CardExpYear) == 4) $CardExpYear = substr($CardExpYear, 2);
        $expire_date = $CardExpMonth . $CardExpYear;
        $postData = "username=" . $this->Username . "&password=" . $this->Passwordd . "&type=" . $type . " &ccnumber=" . $CardNumber . "&ccexp=" . $expire_date . "&amount=" . $Amount;
        if(!empty($CardCVV)) $postData .= "&cvv=" . $CardCVV;
        
        //test('request data', $postData);
        
        return $this->SendRequest($this->url, $postData);
    }        
    
    function Refund($TransactionID = '', $Amount = '') {        
        $type = "refund";
        $postData = "username=" . $this->Username . "&password=" . $this->Passwordd . "&type=" . $type . " &transactionid=" . $TransactionID . "&amount=" . $Amount;
        
        //test('request data', $postData);
        
        return $this->SendRequest($this->url, $postData);
    }
    
    function SendRequest($url, $data) {
        $process = curl_init($url);
        //curl_setopt($process, CURLOPT_HTTPHEADER, array("Content-Type: text/xml"));
        curl_setopt($process, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($process, CURLOPT_POSTFIELDS, $data);
        curl_setopt($process, CURLOPT_POST, 1);
        $return = curl_exec($process);
        curl_close($process);
        return $this->Response($return);
    }

    function Response($response) {
        $response = parse_str($response, $response_array);
        //test('response ', $response_array);                 
 
        return $response_array;
    }        

}

//global function
function NMI_authorize_capture($Amount = '', $CardNumber = '', $CardExpMonth = '', $CardExpYear = '', $CardCVV = ''){
    
    $result = array('success'=>false, 'message'=>'');
    
    $NMI = new NMI();
    $response = $NMI->AuthorizeAndCapture($Amount, $CardNumber, $CardExpMonth, $CardExpYear, $CardCVV);

    //test('response', $response);
    
    $result['message'] = $response['responsetext'];
    if($response['response_code'] == '100'){
        $result['success'] = true;
        $result['ResponseCode'] = $response['response_code'];        
        $result['TransactionID'] = $response['transactionid'];
    }
    
    return $result;
}

function NMI_refund($TransactionID = '', $Amount = ''){
    
    $result = array('success'=>false, 'message'=>'');
    
    $NMI = new NMI();
    $response = $NMI->Refund($TransactionID, $Amount);

    //test('response', $response);
    
    $result['message'] = $response['responsetext'];
    if($response['response_code'] == '100'){
        $result['success'] = true;
        $result['ResponseCode'] = $response['response_code'];        
        $result['TransactionID'] = $response['transactionid'];
    }
    
    return $result;
}

?>