<?php 
namespace Encgateway;

/*
*	------------------------
*	-->   DOCUMENTITION   <--
*	------------------------
*	--> ProcessStoredCard <--
*	------------------------
*	R :: required 
*	O :: optional 
*	------------------------
*	-->   Valid fields   <-- 
*	------------------------
*	> UserName 		:: username || R 
*	> Password 		:: password || R
*	> TransType 	:: contains value Sale Auth Return Force Capture Void Reversal Refund Repeat || R
*	> TokenMode 	:: contains value DEFAULT CARDFORMAT  || R
*	> CardToken 	:: Numerical key value assigned to the payment method || R 
*	> Amount 		:: Amount || R
*	> InvNum 		:: InvNum || O
*	> PNRef 		:: PNRef || O
*	> ExtData 		:: Reserved for future use || O
*	------------------------
*	-->     Response     <--
*	------------------------ 
*	SUCCESS ::  Array ( [TransactionResult] => Array ( [Result] =>  0 [RespMSG] => Approved [Message] => Approved [PNRef] => 2166631 [PaymentType] *   => VISA [TransType] => Auth [Amount] => 20 [Account] => 411111******1111 [AuthCode] => 090579 [TransDate] => 0332016 [TransTime] => 025528 ) )
*	ERROR 	::  Array ( [code] =>  0 [error] => Invalid CardToken.)
*	------------------------ 
*	-->    Sample code   <--
*	------------------------ 
*	$return = \Encgateway\ProcessStoredCard::create(array('TransType' => 'Auth','TokenMode' => 'DEFAULT','CardToken' => '1234567890123456','Amount' *   => '20.00','InvNum' => time(),'ExtData' => ''));
*/

class ProcessStoredCard extends ApiResource {
	
	public static $data = array(); 
	
	public static $processors = "ProcessStoredCard";
	
	/* 
	* @validate parameter 
	* @return :: error ? array() : 1 
	*/
	public static function validParams($param) {
		$rule = array(
			'UserName' => 'required',
			'Password' => 'required',
			'TransType' => 'required|contains,Sale Auth Return Force Capture Void Reversal Refund Repeat',  
			'TokenMode' => 'required|contains,DEFAULT CARDFORMAT',
			'CardToken' => 'required|max_len,256',
			'Amount' => 'required|float',
			'InvNum' => 'max_len,20',
			'ExtData' => 'max_len,512',
		);
		if(isset($param['ApiToken']) ) {
			unset($rule['UserName']);
			unset($rule['Password']);
		}
		if(isset($param['TransType']) && ($param['TransType'] == 'Force' || $param['TransType'] == 'Void') ) {
			$rule['PNRef'] = 'required';
		}
		$is_valid = GUMP::is_valid($param,$rule);
		return $is_valid;
	}
	
	/* 
	* @function for process ProcessStoredCard
	*/
	public static function create($params = null) {	
		self::$data['params'] 	= $params;
		self::$data['success'] 	= 0;
		self::$data['error'] 	= '';
		
		if(self::_validateArray($params)){
			if(Apib::getApiKey()) {
				$params['ApiToken'] = Apib::getApiKey();
			}
			$ret = self::validParams($params);
			if($ret == 1){
				$response = self::_doTrans($params,self::$processors);
				
				if(isset($response['TransactionResult']['Result']) && $response['TransactionResult']['Result'] == 0) :
					self::$data['success'] = 1;
				endif;
				
				if(isset($response['ExtData'])) :
					$response['CardToken'] = strip_tags($response['ExtData']);
				endif;
					
				self::$data['response'] = $response; 
			}else{
				self::$data['error'] = $ret;
			}
		}else{
			self::$data['error']['invalid_argument'] = 'Array is not valid, You must pass array is argument.';
		}
		
		self::$data = self::responseFormat(self::$data,Apib::getOutputFormat());
		return self::$data;
    }
}