<?php

class IOBPAY_UTILITY
{

	// This function used to generate IOB Payment gateway token, which is  prerequisite for initialte txn
	public static function generateToken($encryption_key,$encryption_iv,$sign_key,$merchant_id,$merchant_sub_id,$token_generation_url,$totalamt,$feetype,$merchantreplyurl)
	{

		   //log_message('ERROR','### Gen Token Called...');die();
			$paymentObject = new stdClass;
			$paymentObject->merchantid = $merchant_id;
			$paymentObject->merchantsubid = $merchant_sub_id;
			$paymentObject->feetype = $feetype;
			$paymentObject->totalamt = $totalamt;
			$paymentObject->replyurl = $merchantreplyurl;
			
			$paymentObject->action = 'GENTOK';
			$data_string = json_encode($paymentObject);
			$data_string = SELF::pkcs5_pad($data_string,16);
			
       		$log_message = "###gentok data string before encryption : "  . $data_string;
       		log_message('ERROR',$log_message);

      		$paymentData = base64_encode(openssl_encrypt ( $data_string , 'AES-256-CBC' , $encryption_key , OPENSSL_RAW_DATA | OPENSSL_NO_PADDING, $encryption_iv));//php 7.1 and above
			$paymentTokenObj = new stdClass;
			$paymentTokenObj->merchantid = $merchant_id;
			$paymentTokenObj->merchantsubid = $merchant_sub_id;
			$paymentTokenObj->action = 'GENTOK';
			$paymentTokenObj->data = $paymentData;
			$hmac = strtoupper(hash_hmac('sha256', $paymentData, $sign_key));
			$paymentTokenObj->hmac = $hmac;

			$paymentTokenString = json_encode($paymentTokenObj);

			$log_message = "### Invoking token generation service with json : ".$paymentTokenString;
       		log_message('ERROR',$log_message);
      		
			$ch = curl_init($token_generation_url);
			curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
			curl_setopt($ch, CURLOPT_POSTFIELDS, $paymentTokenString);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
			curl_setopt($ch, CURLOPT_HTTPHEADER, array(
					'Content-Type: application/json',
					'Content-Length: ' . strlen($paymentTokenString))
			);
			curl_setopt($ch,CURLOPT_FAILONERROR,true);
			curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60); 
			curl_setopt($ch, CURLOPT_TIMEOUT, 60); //timeout in seconds		
			curl_setopt ($ch, CURLOPT_SSLVERSION, 6); //TLS 1.2
			$result = curl_exec($ch);
			if(curl_error($ch))
			{
				$curl_errno= curl_errno($ch);
				$log_message = "### Token generate curl error : ".curl_error($ch)." errorno :".$curl_errno;
       		    log_message('ERROR',$log_message);
				
			}
			else
			{
				$log_message = "### Response from token generation service : ".$result;
       		    log_message('ERROR',$log_message);
				
				$decodedVal = json_decode($result);
				$retData = $decodedVal->data;
        		$res = SELF::pkcs5_unpad(openssl_decrypt ( base64_decode($retData) , 'AES-256-CBC' , $encryption_key ,OPENSSL_RAW_DATA | OPENSSL_NO_PADDING, $encryption_iv)); //php 7.1 and above
        		
        		$log_message = "### Token generation service response after decryption: ".$res;
       		    log_message('ERROR',$log_message);
			

				$decodedData = json_decode($res);
				return $decodedData;
			}
	}




	public static function initiateTxn($encryption_key,$encryption_iv,$sign_key,$merchant_id,$merchant_sub_id,$txn_initiation_url,$tokenId,$merchanttxnid,$udf1,$udf2,$udf3,$totalamt,$feetype)
	{
					$txninitObject = new stdClass;
					$txninitObject->merchantid = $merchant_id;
					$txninitObject->merchantsubid = $merchant_sub_id;
					$txninitObject->feetype = $feetype;
					$txninitObject->totalamt = $totalamt;
					$txninitObject->action = 'TXNINIT';
					$txninitObject->tokenid = $tokenId;
					$txninitObject->merchanttxnid = $merchanttxnid; 
					$txninitObject->udf1 = $udf1; 
					$txninitObject->udf2 = $udf2; 
					$txninitObject->udf3 = $udf3; 

					$txninitdata_string = json_encode($txninitObject);
					$txninitdata_string = SELF::pkcs5_pad($txninitdata_string,16);

					$log_message = "### txninit data String before encryption : ".$txninitdata_string;
       		    	log_message('ERROR',$log_message);
					
	        		$txninitData = base64_encode(openssl_encrypt ( $txninitdata_string , 'AES-256-CBC' , $encryption_key , OPENSSL_RAW_DATA | OPENSSL_NO_PADDING, $encryption_iv));//php 7.1 and above
					$txninitfinalobj = new stdClass;
					$txninitfinalobj->merchantid = $merchant_id;
					$txninitfinalobj->merchantsubid = $merchant_sub_id;
					$txninitfinalobj->action = 'TXNINIT';
					$txninitfinalobj->data = $txninitData;
					$hmac = strtoupper(hash_hmac('sha256', $txninitData, $sign_key));
					$txninitfinalobj->hmac = $hmac;

					$txninitfinalString = json_encode($txninitfinalobj);
					
					$log_message = "### txninit final json  : ".$txninitfinalString;
       		    	log_message('ERROR',$log_message);
				
					
					$txninitfinalString = str_replace("\"", "&quot;", $txninitfinalString); //to convery double quotes to &quot; before form submission
					return $txninitfinalString;
					//pass this txninitfinalString as reqjson parameter from browser using POST method form submission to IOBPAY txninit url

	}


	static function pkcs5_pad ($text, $blocksize) 
	{ 
	    $pad = $blocksize - (strlen($text) % $blocksize); 
	    return $text . str_repeat(chr($pad), $pad); 
	} 

	static function pkcs5_unpad($text) 
	{ 
	    $pad = ord($text{strlen($text)-1}); 
	    if ($pad > strlen($text)) return false; 
	    if (strspn($text, chr($pad), strlen($text) - $pad) != $pad) return false; 
	    return substr($text, 0, -1 * $pad); 
	}

	public static function checkTxnStatus($url,$merchant_id,$merchant_sub_id,$action,$feetype,$totalamt,$tokenid,$trackid,$sign_key,$encryption_key,$encryption_iv)
	{
			$log_message = "### Invoking checkTxnStatus : ";
       		log_message('ERROR',$log_message);
      		
			$paymentObject = new stdClass;
			$paymentObject->merchantid = $merchant_id;
			$paymentObject->merchantsubid = $merchant_sub_id;
			$paymentObject->feetype = $feetype;
			$paymentObject->totalamt = $totalamt;
			//$paymentObject->replyurl = $merchantreplyurl;
			$paymentObject->action = $action;
			$paymentObject->tokenid = $tokenid;
			//$paymentObject->trackid = $trackid;

			$data_string = json_encode($paymentObject);
			$data_string = SELF::pkcs5_pad($data_string,16);
			
       		$log_message = "###txtstatus data string before encryption : "  . $data_string;
       		log_message('ERROR',$log_message);

      		$paymentData = base64_encode(openssl_encrypt ( $data_string , 'AES-256-CBC' , $encryption_key , OPENSSL_RAW_DATA | OPENSSL_NO_PADDING, $encryption_iv));//php 7.1 and above
			$paymentTokenObj = new stdClass;
			$paymentTokenObj->merchantid = $merchant_id;
			$paymentTokenObj->merchantsubid = $merchant_sub_id;
			$paymentTokenObj->action = $action;
			$paymentTokenObj->data = $paymentData;
			$hmac = strtoupper(hash_hmac('sha256', $paymentData, $sign_key));
			$paymentTokenObj->hmac = $hmac;

			$txnStatusCheckString = json_encode($paymentTokenObj);

			$log_message = "### Invoking txn check  service with json : ".$txnStatusCheckString;
       		log_message('ERROR',$log_message);
      		
			$ch = curl_init($url);
			curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
			curl_setopt($ch, CURLOPT_POSTFIELDS, $txnStatusCheckString);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
			curl_setopt($ch, CURLOPT_HTTPHEADER, array(
					'Content-Type: application/json',
					'Content-Length: ' . strlen($txnStatusCheckString))
			);
			curl_setopt($ch,CURLOPT_FAILONERROR,true);
			curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60); 
			curl_setopt($ch, CURLOPT_TIMEOUT, 60); //timeout in seconds		
			curl_setopt ($ch, CURLOPT_SSLVERSION, 6); //TLS 1.2
			$result = curl_exec($ch);
			if(curl_error($ch))
			{
				$curl_errno= curl_errno($ch);
				$log_message = "### txn check curl error : ".curl_error($ch)." errorno :".$curl_errno;
       		    log_message('ERROR',$log_message);
				
			}
			else
			{
				$log_message = "### Response from txn check service : ".$result;
       		    log_message('ERROR',$log_message);
				
				$decodedVal = json_decode($result);
				$retData = $decodedVal->data;
        		$res = SELF::pkcs5_unpad(openssl_decrypt ( base64_decode($retData) , 'AES-256-CBC' , $encryption_key ,OPENSSL_RAW_DATA | OPENSSL_NO_PADDING, $encryption_iv)); //php 7.1 and above
        		
        		$log_message = "### Txn check service response after decryption: ".$res;
       		    log_message('ERROR',$log_message);
			

				$decodedData = json_decode($res);
				return $decodedData;
			}
	}
	
	
	

}

?>