<?php

/**
*
* @Class 	CaptureReceipt
*
* @Author 	paycertify
* @Version  0.1 
* @Modified 29th Aug 2016
* 
* @R 		Required 
* @O 		Optional 
* @R* 		Required if username and password is not set
*	
* @Params
* @Type Array
* 
* @Var 	UserName  		username  R 
* @Var 	Password  		password  R
* @Var 	ApiToken 	 	apitoken  R*
* @Var 	PNRef    		password  R 	
* @Var 	Amount   				  R	
* @Var 	AuthCode 				  R
* @Var 	AuthTime 	
* @Var 	CardEaseReference
* @Var 	CardHash
* @Var 	CardReference
* @Var 	CardType
* @Var 	ExpireDate
* @Var 	MerchantName
* @Var 	PanMasked
* @Var 	PanSequence
* @Var 	Receipt
* @Var	Reference
* @Var 	Result
* @Var 	SignatureCaptured
* @Var 	SignatureRequired
* @Var 	TransSource
* @Var 	TransType
* @Var 	TransVerification
* @Var 	DeviceModel
* @Var 	DeviceId
* 
* @Example 
*
* @Create 
* @Param 	Array
* @Return 	Array
*
* $return = \Encgateway\CaptureReceipt::create(array('Amount'=>'23.00','AuthCode' => '2173710','AuthTime' => '20160823090325',
* 'CardEaseReference' => '0220e919-4b69-e611-8e66-0050569228c2','CardHash' => 'ExN/uBaDWTa6QkPRlU26rqfKuDU=',
* 'CardReference' => '98642dfd-6737-e411-a99d-001422187e37','CardType' => 'VISA DEBIT','ExpireDate' => '1712','MerchantName' => 'Standard Test Merchant',
* 'PanMasked'=>'476173******0211', 'PanSequence' => '01',
* 'Receipt' => '<?xml version="1.0" encoding="utf-8"?><ReceiptData version="1.0.0"><ReceiptEntries><ReceiptEntry>
* <ReceiptEntryId>MerchantName</ReceiptEntryId></ReceiptEntry></ReceiptEntries></ReceiptData>', 'Reference' => '96a343ab-cd83-4d9d-868f-2b931c3c6593',
* 'Result' => 'APPROVED', 'SignatureCaptured' => 'False', 'SignatureRequired' => 'False', 'TransSource' =>'ICC', 'TransType' => 'Sale',
*  'TransVerification' => 'PIN VERIFIED', 'DeviceModel' => 'Ingenico-iPP350-RBA', 'DeviceId' => '2214324PT012653' ));

* @Delete 
* @Param 	Array
* @Return 	Array
*
* $return = \Encgateway\CaptureReceipt::delete(array('LogKey' => '0000'));
*/

namespace Encgateway;

class CaptureReceipt extends ApiResource {
	
	/** 
	*
	* @Var 		Array	$data	
	*/
	public static $data = array(); 
	
	/** 
	*
	* @Var 		String	$processors 	
	*/
	public static $processors = "CaptureReceipt";
	
	/** 
	*
	* @Param 	Array	$param 
	* @Type  	Array
	* @Return 	boolean
	*/
	public static function validParams( $param ){
		$rule = array(
			'UserName' => 'required',
			'Password' => 'required',
			'Amount' => 'required',
			'PNRef' => 'required',
			'LogKey' => 'required',
			'AuthCode' => 'required|max_len,10',
			'AuthTime' => 'max_len,256',
			'CardEaseReference' => 'max_len,256',
			'CardHash' => 'max_len,256',
			'CardReference' => 'max_len,256',
			'CardType' => 'max_len,256',
			'ExpireDate' => 'max_len,4',
			'MerchantName' => 'max_len,256',
			'PanMasked' => 'max_len,256',
			'PanSequence' => 'max_len,256',
			'Reference' => 'max_len,256',
			'Result' => 'max_len,256',
			'SignatureCaptured' => 'contains,False True',
			'SignatureRequired' => 'contains,False True',
			'TransSource' => 'max_len,256',
			'TransType' => 'required|max_len,256',
			'DeviceModel' => 'max_len,256',
			'DeviceId' => 'max_len,256',
		);
		
		if(isset($param['ApiToken']) ) {
			unset($rule['UserName']);
			unset($rule['Password']);
		}
		
		if(isset($param['CaptureAction']) &&  $param['CaptureAction'] == "ADD"){
			unset($rule['LogKey']);
		}else{
			unset($rule['PNRef']);unset($rule['Amount']);unset($rule['AuthCode']);unset($rule['AuthTime']);unset($rule['CardEaseReference']);unset($rule['CardHash']);unset($rule['CardReference']);unset($rule['CardType']);unset($rule['ExpireDate']);unset($rule['MerchantName']);unset($rule['PanMasked']);unset($rule['PanMasked']);unset($rule['PanSequence']);unset($rule['Reference']);unset($rule['Result']);unset($rule['SignatureCaptured']);unset($rule['SignatureRequired']);unset($rule['TransSource']);unset($rule['TransType']);unset($rule['DeviceModel']);unset($rule['DeviceId']);
			
		}
		
		$is_valid = GUMP::is_valid($param,$rule);
		
		return $is_valid;
	}
	
	/** 
	* Craete the capture receipt
	* 
	* @Param 	Array	$params 
	* @Type  	Array
	* @Return 	Array
	*/
	public static function create( $params = null ) {	
		$params['CaptureAction'] = 'ADD';
		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 );
				self::$data['response'] = $response; 
				if(isset($response['LogKey'])) : 
					self::$data['success'] = 1;
				endif;
			}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;
    }
	
	/** 
	* Delete the capture receipt
	* 
	* @Param 	Array	$params 
	* @Type  	Array
	* @Return 	Array
	*/
	public static function delete( $params = null ) {	
		self::$data['params'] 	= $params;
		self::$data['success'] 	= 0;
		self::$data['error'] 	= '';
		$params['CaptureAction'] = 'DELETE';	
		if(self::_validateArray( $params )){
			if(Apib::getApiKey()) {
				$params['ApiToken'] = Apib::getApiKey();
			}
			$ret = self::validParams( $params );
			if($ret == 1){
				
				$response = self::_doTrans( $params ,self::$processors );
				self::$data['response'] = $response; 
				if(isset($response['LogKey'])) : 
					self::$data['success'] = 1;
				endif;
			}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;
	}
}