\!/ KyuuKazami \!/

Path : /home/kohli/public_html/application/libraries/
Upload :
Current File : /home/kohli/public_html/application/libraries/payeezy.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
 
class Payeezy{
    
    public $ci;
    public $api_url;
    public $token;
    public $api_key;
    public $mode;
    public $payload;
    public $headers;
    
    function __construct(){
        $this->ci =& get_instance();        
    }
    
    function initialize($credential,$mode = "live"){         
        $this->api_key = $credential['api_key'];
        $this->token = $credential['token'];
        $this->mode = $mode;
        if($this->mode == "sandbox"){
            $this->api_url = "https://api-cert.payeezy.com/v1/transactions";    
        }else{
            $this->api_url = "https://api.payeezy.com/v1/transactions";
        }
    }
    
    function processInput($data) {
        $data = trim($data);
        $data = stripslashes($data);
        $data = htmlspecialchars($data);
        return strval($data);
    }
    
    function processPayment($args = array()){
        $this->getPayload($args);
        $this->get_headers();
        return $this->postTransaction();
    }
    
    function getPayload($args = array()){
        if($this->mode == "sandbox"){
            $baseURL = "https://api-cert.payeezy.com/v1/transactions";    
        }else{
            $baseURL = "https://api.payeezy.com/v1/transactions";
        }
        
        $args = array_merge(array(
            "amount"=> "",
            "card_number" => "", 
            "card_type" => "", 
            "card_holder_name" => "", 
            "card_cvv" => "", 
            "card_expiry" => "",
            "merchant_ref" => "",
            "currency_code" => "",
            "transaction_tag" => "",
            "split_shipment" => "",
            "transaction_id" => "",
        
        ), $args); 
        
        $transaction_type = strtolower(func_get_arg(1));                
        
        $data = "";
        if($transaction_type == "authorize" || $transaction_type == "purchase"){
            $data = array(
                  'merchant_ref'=> $this->processInput($args['merchant_ref']),
                  'transaction_type'=> $transaction_type,
                  'method'=> 'credit_card',
                  'amount'=> $this->processInput($args['amount']),
                  'currency_code'=> $this->processInput(strtoupper($args['currency_code'])),
                  'credit_card'=> array(
                          'type'=> $this->processInput($args['card_type']),
                          'cardholder_name'=> $this->processInput($args['card_holder_name']),
                          'card_number'=> $this->processInput($args['card_number']),
                          'exp_date'=> $this->processInput($args['card_expiry']),
                          'cvv'=> $this->processInput($args['card_cvv']),
                        )
            );        
            $url = $baseURL;
        }else{                
            $url = $baseURL . '/' . $args['transaction_id'];                        
            if($transaction_type == "split")
            {
                $data = array(
                    'merchant_ref'=> $this->processInput($args['merchant_ref']),
                    'transaction_type'=> $transaction_type,
                    'method'=> 'credit_card',
                    'amount'=> $this->processInput($args['amount']),
                    'currency_code'=> $this->processInput(strtoupper($args['currency_code'])),
                    'transaction_tag'=>$args['transaction_tag'],
                    'split_shipment'=>$args['split_shipment'],
                );            
            }else{
                $data = array(
                    'merchant_ref'=> $this->processInput($args['merchant_ref']),
                    'transaction_type'=> $transaction_type,
                    'method'=> 'credit_card',
                    'amount'=> $this->processInput($args['amount']),
                    'currency_code'=> $this->processInput(strtoupper($args['currency_code'])),
                    'transaction_tag'=>$args['transaction_tag'],
                );            
            }
        }
        $this->payload = json_encode($data, JSON_FORCE_OBJECT);
    }
    
    function postTransaction(){
        
        $request = curl_init();
        curl_setopt($request, CURLOPT_URL, $this->api_url);
        curl_setopt($request, CURLOPT_POST, true);
        curl_setopt($request, CURLOPT_POSTFIELDS, $this->payload);
        curl_setopt($request, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($request, CURLOPT_HEADER, false);
        curl_setopt($request, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($request, CURLOPT_HTTPHEADER, array(
          'Content-Type: application/json', 
          'apikey:'.strval($this->api_key), 
          'token:'.strval($this->token), 
          'Authorization:'.$this->headers['authorization'],
          'nonce:'.$this->headers['nonce'],
          'timestamp:'.$this->headers['timestamp'],
        ));
        
        $response = curl_exec($request);
        
        if (FALSE === $response)
            echo curl_error($request);
        	
        //$httpcode = curl_getinfo($request, CURLINFO_HTTP_CODE);
        curl_close($request);
        
        return $response;
    }
    
    function get_headers(){
        $nonce = strval(hexdec(bin2hex(openssl_random_pseudo_bytes(4, $cstrong))));
        $timestamp = strval(time()*1000); //time stamp in milli seconds
        $data = $this->api_key . $nonce . $timestamp . $this->token . $this->payload;
        $hashAlgorithm = "sha256";
        $hmac = hash_hmac ( $hashAlgorithm , $data , $api_secret, false );    // HMAC Hash in hex
        $authorization = base64_encode($hmac);
        $this->headers = array("authorization"=>$authorization, "nonce"=>$nonce, "timestamp"=>$timestamp);
    }

    
}
?>

@KyuuKazami