<?php 
namespace Encgateway;

/*
*	------------------------
*	-->   DOCUMENTITION   <--
*	------------------------
*	-->    StoreCard     <--
*	------------------------
*	R :: required 
*	O :: optional 
*	------------------------
*	-->   Valid fields   <-- 
*	------------------------
*	> UserName 		:: username || R 
*	> Password 		:: password || R 
*	> ApiToken 		:: ApiToken || O
*	> TokenMode 	:: contains value DEFAULT CARDFORMAT  || R
*	> CardNum 		:: Credit card number || R
*	> ExpDate 		:: MMYY format. || R
*	> CustomerKey	:: Unique identifier assigned to the customer record || R
*	> NameOnCard 	:: Card owner’s name || O
*	> Street 		:: Card owner’s billing address street name and number || O
*	> Zip 			:: Card owner’s billing address postal/zip code || O
*	> ExtData 		:: Reserved for future use || O
*	------------------------
*	-->     Response     <--
*	------------------------ 
*	SUCCESS ::  Array ( [Result] => 0 [RespMSG] => Token generated successfully [ExtData] => <cardsafetoken>1121865482724989</cardsafetoken> [code]*   => OK [error] => OK [Username] => Array ( [0] => ) [CardToken] => 1121865482724989 )
*	ERROR 	::  Array ( [code] => Not_Enough_Privilege [error] =>  Not enough privilege to access this customer )
*	------------------------ 
*	-->    Sample code   <--
*	------------------------ 
*   Encgateway\Apib::setApiKey("7E35FC46-C951-2D2F-FB42-7795F3D24C60");
*	$return = \Encgateway\StoreCard::create(array('TokenMode' => 'DEFAULT','CardNum' => '4111111111111111','ExpDate' => '1233','CustomerKey' =>  
*   '83','NameOnCard' => 'John Doe','Street' => '123 Miami Street','Zip' => '123456','ExtData' => ''));
*/
class StoreCard extends ApiResource {
	
	public static $data = array(); 
	
	public static $processors = "StoreCard";
	
	/* 
	* @validate parameter 
	* @return :: error ? array() : 1 
	*/
	public static function validParams($param) {
		$rule = array(
			'UserName' => 'required',
			'Password' => 'required',
			'TokenMode' => 'required|contains,DEFAULT CARDFORMAT',
			'CardNum' => 'required|valid_cc|max_len,16',
			'ExpDate' => 'required|exact_len,4',
			'CustomerKey' => 'required',
			'NameOnCard' => 'max_len,256',
			'Street' => 'min_len,5|max_len,512',
			'Zip' => 'integer|max_len,10',
			'ExtData' => 'max_len,512',
		);
		if(isset($param['ApiToken']) ) {
			unset($rule['UserName']);
			unset($rule['Password']);
		}
		
		$is_valid = GUMP::is_valid($param,$rule);
		
		return $is_valid;
	}
	
	/* 
	* @function for process storecard
	*/
	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['Result']) && $response['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;
    }
}