<?php
/*
	--------------------
	--> DOCUMENTITION <--
	--------------------
	## Class :: ManageCheckInfo
	--------------------
	R :: required 
	O :: optional 
	--------------------
	## Valid fields ::   
	> UserName :: username || R 
	> Password :: password || R
	> TransType :: contains value ADD UPDATE DELETE || R
	> Vendor :: merchant id || R
	> CustomerKey :: customer id || R
	> CheckInfoKey :: unique key for indified record , required for update and delete record
	> CheckType ::  R || contains values PERSONAL BUSINESS
	> AccountType :: R || contains values CHECKING SAVINGS
	> CheckNum :: O  || max-length -256
	> MICR :: O  || max-length - 512
	> AccountNum :: O || valid account number
	> TransitNum :: O  || max-length -256
	> SS :: O || Social Security number. || max-length -50
	> DOB :: O || date of birth || Format - DDMMYYYY
	> BranchCity :: O || city where branch is located.
	> DL :: O || driving license number ||max-length -100
	> StateCode :: O || two digite state code 
	> NameOnCheck :: O || check owner name || max-length -256
	> Email :: O ||coustomer contact email
	> DayPhone :: O ||customer contact phone number 
	> Street1 :: O || Street address line 1 || max-length -512
	> Street2 :: O || Street address line 2 || max-length -512
	> Street3 :: O || Street address line 3 || max-length -512
	> City :: O || city name || max-length -256
	> StateID :: O || two digit state id
	> Province :: O || customer contact procvince || max-length -256
	> PostCode :: O ||customer contact zipcode || max-length -30
	> CountryID :: O ||three digit country id 
	> ExtDate :: O ||for feture use || format - DDMMYYYY
	
	--------------------
	## Response :: 
	success ::  Array([CheckInfoKey] =>  -- [Vendor] =>  -- [code] =>  OK [error] =>  OK [Username] =>  -- )
	error ::  Array ( [code] =>  Not_Enough_Privilege [error] =>  Not enough privilege to access this merchant )
	--------------------
	## Sample code :: 
	> creating 
	$return = \Encgateway\ManageCheckInfo::create(array('UserName'=>'test_emp','Password' => '123456','TransType' => 'ADD','Vendor'=>'107','CustomerKey'=>'45','CheckType' => 'PERSONAL','AccountType' => 'CHECKING','AccountNum' => '4111111111111111','TransitNum' => '234','CheckNum' => '012345','MICR' => 'Magnetic Ink Check Reader data line.','SS' => '2323','DOB' => '22041990','BranchCity' => 'perth','DL' => '234234234434','StateCode' => 'SL','NameOnCheck' => 'Testing name','Email' => 'testing@rmai.com','DayPhone' => '3242342342','Street1' => 'address 1','Street2' => 'sdfsdf', 'Street3' => 'three street','City' => 'city','StateID' => 'DE','Province'=> 'Province','PostCode' => '234234','CountryID' => 'CAN','ExtDate' => 12051990));

	> delete 
	//$return = \Encgateway\ManageCheckInfo::delete(array('UserName'=>'test_emp','Password' => '123456','TransType' => 'DELETE','Vendor'=>'107','CustomerKey'=>'45','CheckType' => 'PERSONAL','CheckInfoKey'=>'37','AccountType' => 'CHECKING','AccountNum' => '4111111111111111','TransitNum' => '234'));

	> update  
	//$return = \Encgateway\ManageCheckInfo::update(array('UserName'=>'test_emp','Password' => '123456','TransType' => 'UPDATE','Vendor'=>'107','CustomerKey'=>'45','CheckInfoKey'=>'36', 'CheckType' => 'PERSONAL','AccountType' => 'CHECKING','AccountNum' => '4111111111111111','TransitNum' => '234','CheckNum' => '012345','MICR' => 'update Magnetic Ink Check Reader data line.','SS' => '2323','DOB' => '22041990','BranchCity' => 'u perth','DL' => '234234234434','StateCode' => 'SL','NameOnCheck' => 'update Testing name','Email' => 'testing@rmai.com','DayPhone' => '3242342342','Street1' => 'address 1','Street2' => 'sdfsdf', 'Street3' => 'three street','City' => 'city','StateID' => 'DE','Province'=> 'Province','PostCode' => '234234','CountryID' => 'CAN','ExtDate' => 12051990));
	
	 
*/

namespace Encgateway;

class ManageCheckInfo extends ApiResource {
	
	public static $data = array(); 
	
	public static $processors = "ManageCheckInfo";
	
	/* 
		validate parameter for add update and delete payment method  
	*/
	public static function validParams($param){
		$rule = array(
			'UserName' => 'required',
			'Password' => 'required',
			'TransType' => 'required|contains,ADD UPDATE DELETE',
			'Vendor' => 'required',
			'CustomerKey' => 'required',
			'CheckInfoKey' => 'integer',
			'CheckType' => 'required|contains,PERSONAL BUSINESS',
			'AccountType' => 'required|contains,CHECKING SAVINGS',
			'CheckNum' => 'max_len,256',
			'MICR' => 'max_len,512',
			'AccountNum' => 'valid_cc',
			'TransitNum' => 'max_len,256',
			'SS' => 'max_len,50',
			'DOB' => 'integer|exact_len,8',
			'BranchCity' => 'max_len,100',
			'DL' => 'max_len,100',
			'StateCode' => 'exact_len,2',
			'NameOnCheck' => 'max_len,256',
			'Email' => 'valid_email',
			'DayPhone' => 'phone_number',
			'Street1' => 'max_len,512',
			'Street2' => 'max_len,512',
			'Street3' => 'max_len,512',
			'City' => 'max_len,256',
			'StateID' => 'exact_len,2',
			'Province' => 'max_len,256',
			'PostCode' => 'max_len,30',
			'CountryID' => 'exact_len,3',
			'ExtDate' => 'integer|exact_len,8',			
		);
		if(isset($param['ApiToken']) ) {
			unset($rule['UserName']);
			unset($rule['Password']);
		}
		if(isset($param['TransType']) &&  $param['TransType'] == "DELETE"){
			$rule['CheckInfoKey'] = 'integer|required';
			unset($rule['CheckType']);unset($rule['AccountType']);unset($rule['CheckNum']);unset($rule['MICR']);unset($rule['AccountNum']);unset($rule['TransitNum']);
			unset($rule['SS']);unset($rule['DOB']);unset($rule['BranchCity']);unset($rule['DL']);unset($rule['StateCode']);unset($rule['NameOnCheck']);unset($rule['NameOnCheck']);
			unset($rule['Email']);unset($rule['DayPhone']);unset($rule['Street1']);unset($rule['Street2']);unset($rule['Street3']);unset($rule['City']);unset($rule['StateID']);
			unset($rule['Province']);unset($rule['PostCode']);unset($rule['CountryID']);unset($rule['ExtDate']);
		}
		
		$is_valid = GUMP::is_valid($param,$rule);
		
		return $is_valid;
	}
	
	
	/* 
		function for update payment method 
	*/
	public static function update($params = null) {
		self::$data['params'] 	= $params;
		self::$data['success'] 	= 0;
		self::$data['error'] 	= '';
		if(self::_validateArray($params)){
			if(Apib::getApiKey()) {
				$params['ApiToken'] = Apib::getApiKey();
			}
			if($params['TransType'] == 'UPDATE') {
				$ret = self::validParams($params);
				if($ret == 1){
					$response = self::_doTrans($params,self::$processors);
					self::$data['response'] = $response; 
					if(isset($response['CheckInfoKey'])) : 
						self::$data['success'] = 1;
					endif;
				}else{
					self::$data['error'] = $ret;
				}
			}else{
				self::$data['error']['invalid_transtype'] = 'Invalid transtype.';
			}
		}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;
	}
	
	/* 
		function for delete payment method 
	*/
	public static function delete($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($params['TransType'] == 'DELETE') {
				if($ret == 1){
					$response = self::_doTrans($params,self::$processors);
					self::$data['response'] = $response; 
					if(isset($response['CheckInfoKey'])) : 
						self::$data['success'] = 1;
					endif;
				}else{
					self::$data['error'] = $ret;
				}
			}else{
				self::$data['error']['invalid_transtype'] = 'Invalid transtype.';
			}
		}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;
	} 
	
	/* 
		function for creating payment method 
	*/
	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();
			}
			if($params['TransType'] == 'ADD') {
				$ret = self::validParams($params);
				if($ret == 1){
					$response = self::_doTrans($params,self::$processors);
					self::$data['response'] = $response; 
					if(isset($response['CheckInfoKey'])) : 
						self::$data['success'] = 1;
					endif;
				}else{
					self::$data['error'] = $ret;
				}
			}else{
				self::$data['error']['invalid_transtype'] = 'Invalid transtype.';
			}
		}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;
    }
}