<?php

/*
	--------------------
	--> DOCUMATITION <--
	--------------------
	## Class :: ManageCreditCardInfo
	--------------------
	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
	> CardInfoKey :: unique key for update and delele record || R => for update and delete recored optional for add  
	> CcAccountNum :: valid account number || O
	> CcExpDate :: valid expiry date || O
	> CcStreet :: street address || O
	> CCZip :: zip code || O
	> ExtData :: optional for additional  data || O
	
	--------------------
	## Response :: 
	success ::  Array([CcInfoKey] =>  -- [Vendor] =>  -- [code] =>  OK [error] =>  OK [Username] =>  -- )
	error ::  Array ( [code] =>  Not_Enough_Privilege [error] =>  Not enough privilege to access this merchant )
	--------------------
	## Sample code :: 
	> creating payment method 
	$return = \Encgateway\ManageCreditCardInfo::create(array('UserName'=>'test_emp','Password' => '123456','TransType' => 'ADD','Vendor'=>'107','CustomerKey'=>'45','CcAccountNum' => '4111111111111111','CcExpDate' => '0424','CcNameOnCard' => 'testing card','CcStreet' => 'my cc stret','CCZip' => '12032','ExtData' => '<data>F</data>'));

	> delete payment method 
	$return = \Encgateway\ManageCreditCardInfo::delete(array('UserName'=>'test_emp','Password' => '123456','TransType' => 'DELETE','Vendor'=>'107','CustomerKey'=>'45','CardInfoKey'=> '32'));

	> update payment method 
	$return = \Encgateway\ManageCreditCardInfo::update(array('UserName'=>'test_emp','Password' => '123456','TransType' => 'UPDATE','Vendor'=>'107','CustomerKey'=>'45','CardInfoKey'=> '32','CcAccountNum' => '4111111111111111','CcExpDate' => '0424','CcNameOnCard' => 'update testing card','CcStreet' => 'my cc stret','CCZip' => '12032','ExtData' => '<data>F</data>'));
	
	 
*/

namespace Encgateway;

class ManageCreditCardInfo extends ApiResource {
	
	public static $data = array(); 
	
	public static $processors = "ManageCreditCardInfo";
	
	/* 
		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',
			'CardInfoKey' => 'integer',
			'CcAccountNum' => 'valid_cc',
			'CcExpDate' => 'exact_len,4',
			'CcStreet' => 'min_len,5|max_len,512',
			'CCZip' => 'integer|max_len,10',
			'ExtData' => 'max_len,512'
		);
		if(isset($param['ApiToken']) ) {
			unset($rule['UserName']);
			unset($rule['Password']);
		}
		if(isset($param['TransType']) &&  $param['TransType'] == "DELETE"){
			unset($rule['CcAccountNum']);unset($rule['CcExpDate']);unset($rule['CcStreet']);unset($rule['CCZip']);unset($rule['ExtData']);
		}
		
		$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['CcInfoKey'])) : 
						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['CcInfoKey'])) : 
						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['CcInfoKey'])) : 
						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;
    }
}