<?php
class pixelinnov
{
	private $error   = false; 
	private $Type   = 'send';
	private $Simulation = 0;
	private $Production = 0;
	
	private $Momo = 0;	

	private $Url = "https://apim.pixpayment.net";
	
		
	
	###########################################################
	function __construct()
	{
		$this->ClientID = 'qPP5cb5sIviTJ4_mPeMON1TbKSga';
		$this->ClientSecret = 'atoK30oV5cI2geTLxQd1XazSG_ca';
		$this->AccountNumber = '6e88b13d-c3a9-44b7-bc2c-ceced60f8e0a'; 
		$this->AccountCode = 'VIRALPAY';
		$this->InternalKey = 'eyJraWQiOiJnYXRld2F5X2NlcnRpZmljYXRlX2FsaWFzIiwiYWxnIjoiUlMyNTYifQ.eyJzdWIiOiJhZG1pbkBjYXJib24uc3VwZXIiLCJhdWQiOiI4OWY3OWFjMC1kYjRlLTRiODMtOGI5Ny1iNjJhZjlhYjhmMzYiLCJpc3MiOiJodHRwczovL2FwaW0ucGl4cGF5bWVudC5uZXQ6OTQ0My9vYXV0aDIvdG9rZW4iLCJrZXl0eXBlIjoiUFJPRFVDVElPTiIsInN1YnNjcmliZWRBUElzIjpbeyJuYW1lIjoiUGl4UGF5bWVudEFQSSIsImNvbnRleHQiOiIvYXBpLzEiLCJ2ZXJzaW9uIjoiMSIsInB1Ymxpc2hlciI6ImFkbWluIiwic3Vic2NyaXB0aW9uVGllciI6bnVsbCwic3Vic2NyaWJlclRlbmFudERvbWFpbiI6bnVsbH1dLCJleHAiOjE3MjIyNjE3NjMsInRva2VuX3R5cGUiOiJJbnRlcm5hbEtleSIsImlhdCI6MTcyMjIwMTc2MywianRpIjoiZmJkMTk0NmUtZDUxZC00N2RjLWJhYTEtMTFlODRlYzQ0ZDg3In0.TxvGeUWjz3Wt8hzCe2XjIt8aV9ECamIglCRou3Q2t5pHsnf1_wmsgrGoxqPH78scyKw2Jwq9qR6CJI94S5p8HrG2G5VT29CefqweSTw-itHHaT6_iDCjeGRAFvjjHFIiwEGrixam56rSa_Ft6cu_rcs9fms7Ntd6Z97_CfOXaLHyxJpD1vqPkEzPs1liHgcdEHTSgCCKtowcApLjuo8On53tAB4zWlPf_XT3zq39QnAaB5X5dBeQsZrBX6vIZ30ZIL5GeLJd2HPZSEHLvDj8O2uSM1Hrrv_QTTTLWPpLPXDnLeIH4wY1cHMK6uGYsq9ofsbByP7Eiy4XSbxlLDwCRw';
		
	}
	public function setData($data)
	{
		$this->receiveData = 
		$this->Data = $data;
	
	}
	function setProduction()
	{
		$this->PrivateKey = "live_private_Zj64PmPy2aGmFaAYhk8mpC5D8qq";
		$this->PublicKey = "live_public_JGbiw2EXM4M6EDl85cM52xFO6Pu";
		$this->Token = "qOLnSLr0ZOz9sK0O5Caz";
		$this->Url = 'https://app.paydunya.com/api/';
	}
	function setSimulation()
	{
		$this->Production = 0;
	}
	
	function getUID()
	{
		return getUID();
	}
	function getStatus($current)
	{
		global $sgbd;
		
		
		$info = loadTransaction($current);
		$type = substr($info['transaction_id'],0,3);
		/*$infos = json_decode($current['infos'],true);
		if(empty($infos['transaction_id'])) $infos['transaction_id']=$infos['transaction'];
		
		if(empty(['provider_id'])) return 'PENDING';*/
		
		uses_class('curl');
		$this->getToken();
		$curl = new curl;
		$curl->setUrl("https://api-gateway.pixpayment.net:8243/api/1/status/".$info['provider_id']);
        $curl->addHeader("Content-Type","application/json");

		$curl->addHeader("Authorization","Bearer ".$this->Token);
		$curl->addHeader("Content-Type","application/json");
		$curl->setMethode('GET');
		
		$curl->make();
		

		$status = strtoupper($curl->Reponse['data']['status']);
		
		switch($status)
		{
			case 'COMPLETED':
			case 'SUCCESSFUL':
			case 'SUCCESS':
				$status = 'SUCCESS';
			break;
			case 'FAILED':
				$status = 'FAILED';
			break;
			default: 
				$status = "PENDING";
		}
		
		if(!empty($status)) 
		{
			return $status;
		}
		elseif($curl->ErrorNumber>300)
		{
			return 'FAILED';
		}
		
		return "PENDING";

	}
	
	function getBalance()
	{
		$this->Solde = $this->getBalanceMTN();
	}
	
	
	function getAutorisation()//transfert de credit 
	{
		$this->Autorization = base64_encode($this->ClientID.":".$this->ClientSecret);//$curl->Response->apiKey;
		
	}

	function getToken()//transfert de credit 
	{
		$this->getAutorisation();
		uses_class('curl');
		

		$data['grant_type'] = 'client_credentials';
		//CREATION ET SAUVEGARDE DU ACCESS_TOCKEN
		$curl = new curl;
		$curl->setUrl($this->Url.":9443/oauth2/token");
		$curl->addHeader("Authorization","Basic ".$this->Autorization);
		$curl->addHeader("Ocp-Apim-Subscription-Key","".$this->Primary[$this->Module]."");

		$curl->setMethode('POST');
		$curl->setDataUrl($data);
		
		$curl->make();	
		
		
		
		$this->Token = $curl->Reponse['access_token'];
		//if(empty($this->Token)) setError("Service momentanément indisponible !","Le jéton de sécurité ".$this->Module." mtnpay n'est plus/pas valide");
	}
	
	function Depot()//transfert de credit 
	{
		$this->getToken();
		$data = '{
					"accountNumber": "'.$this->AccountNumber.'",
					"partnerCode": "'.$this->AccountCode.'",
					"beneficiary": {
						"firstName": "viralpay",
						"lastName": "International",
						"msisdn": "'.$this->Data['international_phone'].'", 
						"country": "CM", 
						"address": "Bonamoussadi Cameroun"
					},
					"country": "'.$this->Data['country'].'", 
					"currency": "'.$this->Data['currency'].'",
					"amount": "'.$this->Data['amount'].'",
					"transactionType": "TRANSFER", 
					"operator": "'.$this->Data['operator'].'"
				}';



				$curl = new curl;
				$curl->setUrl("https://api-gateway.pixpayment.net:8243/api/1/payment");
				
				$curl->addHeader("Authorization","Bearer ".$this->Token);
				$curl->addHeader("Content-Type","application/json ");		
				$curl->setMethode('GET');
				$curl->addHeader("accept","*/*");
				$curl->setData($data);
				$curl->setMethode('POST');
				$curl->make();
		
	
		if($curl->ErrorNumber<=201)
		{
			$this->Return['reponse']= array
            (
                "transaction_id"=> $this->IDTransaction,
				"order_id"=> $this->Data['order_id'],
				"phone_number"=> $this->Numero,
				"amount"=>$this->Data['amount'],
				"fees"=>$this->Data['fees'],
				"taxes"=>$this->Data['taxes'],
                "status"=>'PENDING',
                "reference_id"=> $this->Data['reference_id'],
                "provider_id"=>$curl->Reponse['data']['paymentId'],
				"provider_code"=>$this->Data['provider_code'],
               
            );
			
			
			if($this->Data['operator']=='wave') $this->Return['reponse']['provider_link']= $curl->Reponse['payment_url'];
		}
		else{
				$error = "transaction failed";

		 $this->Return['error']=array
		 (
			 "status"=> "FAILED",
			 "reason"=>$error,
			 "provider_error"=>json_encode($Reponse),
		 );
		 //local_json($this->Return);
		}
		
	}
	
	function Retrait()
	{
		$this->getToken();
		$data = '{
			"accountNumber": "'.$this->AccountNumber.'",
			"partnerCode": "'.$this->AccountCode.'",
			"operator": "'.$this->Data['operator'].'",
			"transactionType": "MERCHANT_PAYMENT",
			"msisdn": "'.$this->Data['international_phone'].'",
			"country": "'.$this->Data['country'].'",
			"currency": "'.$this->Data['currency'].'",
			"amount": "'.$this->Data['amount_total'].'"
		}';

		$curl = new curl;
		$curl->setUrl("https://api-gateway.pixpayment.net:8243/api/1/payment");
		
		$curl->addHeader("Authorization","Bearer ".$this->Token);
		$curl->addHeader("Content-Type","application/json ");		
		$curl->setMethode('GET');
		$curl->addHeader("accept","*/*");
		$curl->setData($data);
		$curl->setMethode('POST');
		$curl->make();

	
		
		if($curl->ErrorNumber<=201)
		{
			$this->Return['reponse']= array
            (
                "transaction_id"=> $this->IDTransaction,
				"order_id"=> $this->Data['order_id'],
				"phone_number"=> $this->Numero,
				"amount"=>$this->Data['amount'],
				"fees"=>$this->Data['fees'],
				"taxes"=>$this->Data['taxes'],
                "status"=>'PENDING',
                "reference_id"=> $this->Data['reference_id'],
                "provider_id"=>$curl->Reponse['data']['paymentId'],
				"provider_code"=>$this->Data['provider_code'],
               
            );
			
			
			if($this->Data['operator']=='wave') $this->Return['reponse']['provider_link']= $curl->Reponse['payment_url'];
		}
		else{
				$error = "transaction failed";

		 $this->Return['error']=array
		 (
			 "status"=> "FAILED",
			 "reason"=>$error,
			 "provider_error"=>json_encode($curl->Reponse)
		 );
		 //local_json($this->Return);
		}
	}
	
	
	
}
?>