<?php
class intouch
{
	private $error   = true; 
	private $Cle   = 'send';
	private $Simulation = 1;
	private $production = 0;
	private $ApiUser = "403770793";
	private $ApiKey = "ZR3TdAKCtX";
	private $ApiID = "CM0843";
	private $Domaine = "https://api.gutouch.com/";
	private $SecretKey = "FLWSECK_TEST94859ae0db2d";
	
	###########################################################
	function intouch()
	{
		
	}
	public function setData($data)
	{
		$this->receiveData = 
		$this->Data = $data;
	
	}
	function getUID()
	{
		return getUID();
	}
	function setProduction()
	{
		//return $this->setSimulation();
		$this->Production = 1;
		$this->Simulation = 0;
		$this->Domaine = "https://api.gutouch.com/";
		$this->AgenceCode = "DANOL2798";
		$this->ApiID ="CM8774";
	}
	
	function setService()
	{
		$service = strtolower($this->Service['IDService']);
		$this->Pays = '237';
		//Le format du numéro 
		
		$country = strtoupper($this->Data['country']);
		$operator = strtoupper($this->Data['operator']);
		
		//CAMEROUN
		$this->APIDATAS['CM']['AgenceCode'] = "DANOL2798";
		$this->APIDATAS['CM']['ApiID']= "CM8774";
		$this->APIDATAS['CM']['ApiLogin'] = "697947494";
		$this->APIDATAS['CM']['ApiPassword'] = "0000";		
		$this->APIDATAS['CM']['AgentLogin'] = "";
		$this->APIDATAS['CM']['AgentPassword'] = "";

		//Côte d'ivoire
		$this->APIDATAS['CI']['AgenceCode'] = "DNLCI3762";
		$this->APIDATAS['CI']['ApiID']= "CI7658";
		$this->APIDATAS['CI']['ApiLogin'] = "69794749";
		$this->APIDATAS['CI']['ApiPassword'] = "0000";
        $this->APIDATAS['CI']['AgentLogin'] = "";
		$this->APIDATAS['CI']['AgentPassword'] = "";

		//SENEGAL
		$this->APIDATAS['SN']['AgenceCode'] = "TEMBI20256";
		$this->APIDATAS['SN']['ApiID']= "TEMBI20256";
		$this->APIDATAS['SN']['ApiLogin'] = "403770793";
		$this->APIDATAS['SN']['ApiPassword'] = "ZR3TdAKCtX";
		$this->APIDATAS['SN']['AgentLogin'] = "1bbd12343b8438f93214d7b6b504f6ee2373ccf34edff031014a705df6ad5b94";
		$this->APIDATAS['SN']['AgentPassword'] = "b3a50651897fb27437e5077bf55a3ca1e1bdf96ce8b1c0375354e15d2a5e997f";
		
		$this->getService($operator,$country);
		$this->Country = $country;
		
		
		
	}
	function getBalance()
	{
		$data = array(
						'partner_id' => $this->ApiID,
						'login_api' => $this->ApiLogin,
						'password_api' => $this->ApiPassword
					 );

		
		$data = json_encode($data);

		//CREATION ET SAUVEGARDE DU ACCESS_TOCKEN
		$Autorization = base64_encode($this->ApiUser.":".$this->ApiKey);
		uses_class('curl');
		$curl = new curl;
		//https://dev-api.gutouch.com/dist/api/touchpayapi/v1/ASUR0845/utils?loginAgent=9856&passwordAgent=0000
		$curl->setUrl($this->Domaine."v1/".$this->APIDATAS[$this->Country]."/get_balance");
		$curl->addHeader("Authorization","Basic ".$Autorization);
		$curl->addHeader("Content-Type","application/json ");
 
		$curl->setMethode('POST');
		
		$curl->setData($data);
		$curl->make();
		
		
		if($curl->Response->error==1) return $return['balance'] = "INDISPONIBLE";
		$return['Montant']=numbertoprix($curl->Response->amount);
		return $return;
	}
	function getService($operator,$country)
	{
		$operator = strtoupper($operator);
		switch($operator)
		{
			case 'MTN':
				//$this->Domaine = "https://dev-api.gutouch.com/";
				
				switch($country)
				{
					case 'CM':

						if($this->Cash=='OUT')
						{
							$this->ServiceID ="CASHOUTMTNCM";
							$this->Request   ="cashout_request";
						}
						elseif($this->Cash=='IN')
						{
							$this->ServiceID ="CASHINMTNCM";
							$this->Request   ="cashin";
						}
						
					break;
					case 'CI':
							if($this->Cash=='OUT')
							{
								$this->ServiceID ="CASHOUTMTN";
								$this->Request   ="cashout_request";
							}
							elseif($this->Cash=='IN')
							{
								$this->ServiceID ="CASHINMTN";
								$this->Request   ="cashin";
							}
						
					break;
					default:
					$this->SetError("The operator is not available in this country");
				}
				
				
			break;

			case 'ORANGE':
				switch($country)
				{
					case 'CM':

						if($this->Cash=='OUT')
						{
							$this->ServiceID ="CASHOUTOMCM";
							$this->Request   ="cashout_request";
						}
						elseif($this->Cash=='IN')
						{
							$this->ServiceID ="CASHINOMCM";
							$this->Request   ="cashin";
						}
						
					break;
					case 'CI':
						if($this->Cash=='OUT')
						{
							$this->ServiceID ="CASHOUTOMCI";
							$this->Request   ="cashout_request";
						}
						elseif($this->Cash=='IN')
						{
							$this->ServiceID ="CASHINOMCI";
							$this->Request   ="cashin";
						}
								
					break;
					case 'SN':
						if($this->Cash=='OUT')
						{
							$this->ServiceID ="CASHOUTOM";
							$this->Request   ="cashout_request";
						}
						elseif($this->Cash=='IN')
						{
							$this->ServiceID ="CASHINOM";
							$this->Request   ="cashin";
						}
					break;
					default:
					$this->SetError("The operator is not available in this country");
				}
				
				
			break;
			
			default:
				$this->SetError("Mobile money operator not taken into account.");
		}
	}

	function Depot()
	{
		$this->Cash = 'IN';
		
	  $this->setService();
		
		$data = array(
						'service_id' => $this->ServiceID,
						'recipient_phone_number' => $this->Numero,
						'amount' => $this->Montant,
						'partner_id' => $this->APIDATAS[$this->Country]['ApiID'],
						'partner_transaction_id' => getUID(),
						'login_api' => $this->APIDATAS[$this->Country]['ApiLogin'],
						'password_api' => $this->APIDATAS[$this->Country]['ApiPassword'],
						'call_back_url' => API_URL."callbacks"
					 );
		
		$data = json_encode($data);
		
		

		//CREATION ET SAUVEGARDE DU ACCESS_TOCKEN
		$Autorization = base64_encode($this->APIDATAS[$this->Country]['AgentLogin'].":".$this->APIDATAS[$this->Country]['AgentPassword']);
		uses_class('curl');
		$curl = new curl;
		
		$curl->setUrl($this->Domaine."v1/".$this->APIDATAS[$this->Country]['AgenceCode']."/".$this->Request);
		$curl->addHeader("Authorization","Basic ".$Autorization);
		$curl->addHeader("Content-Type","application/json ");
 
		$curl->setMethode('POST');
		$curl->setData($data);
		
		$curl->make();

		if(($curl->ErrorNumber)<400)
		{
			$statut = $this->realStatut($curl->Reponse['data']['status']);
			
			$this->Return['reponse']= array
			(
				"amount"=>$this->Montant,
				"status"=>$statut,
				"transaction_id"=> $this->IDTransaction,
				"reference"=> $this->Reference,
				"provider_number"=> $this->Numeros['disbursement'],
				"provider_name"=> $this->Names['disbursement'],
				"uid"=> $this->ReferenceID,
				"phone"=> $this->Numero
			);
		 
		}
		elseif(($curl->ErrorNumber)==500)
		{
			if(!empty($curl->Reponse['message'])) $message = $curl->Reponse['message'];
			else $message = "The server did not respond correctly. ";
			$this->Return['error']=array
			(
				"status"=> "FAILED",
				"reason"=>$message,
				"code"=> $curl->ErrorNumber
			);
			//local_json($this->Return);
		}
		else{
		 $this->Return['error']=array
		 (
			 "status"=> "FAILED",
			 "reason"=>$curl->Reponse['errorMessage'],
			 "code"=> $curl->ErrorNumber
		 );
		 //local_json($this->Return);
		}
		
	}
	function Retrait()
	{
		$this->Confirm = 1;
		$this->Cash = 'OUT';

		$this->setService();
		
		
		
		$data = array(
						'service_id' => $this->ServiceID,
						'recipient_phone_number' => $this->Numero,
						'amount' => $this->Data['amount_total'],
						'partner_id' => $this->ApiID,
						'partner_transaction_id' => getUID(),
						'login_api' => $this->ApiLogin,
						'password_api' => $this->ApiPassword,
						'call_back_url' => API_URL."callbacks"
					 );

		$data = array(
		'service_id' => $this->ServiceID,
		'recipient_phone_number' => $this->Numero,
		'amount' => $this->Montant,
		'partner_id' => $this->APIDATAS[$this->Country]['ApiID'],
		'partner_transaction_id' => getUID(),
		'login_api' => $this->APIDATAS[$this->Country]['ApiLogin'],
		'password_api' => $this->APIDATAS[$this->Country]['ApiPassword'],
		'call_back_url' => API_URL."callbacks"
		);
	
		$data = json_encode($data);
		
		
		
		//CREATION ET SAUVEGARDE DU ACCESS_TOCKEN
		$Autorization = base64_encode($this->APIDATAS[$this->Country]['AgentLogin'].":".$this->APIDATAS[$this->Country]['AgentPassword']);
		uses_class('curl');
		$curl = new curl;
		
		$curl->setUrl($this->Domaine."v1/".$this->Identifiant."/".$this->Request);
		$curl->setUrl($this->Domaine."v1/".$this->APIDATAS[$this->Country]['AgenceCode']."/".$this->Request);
		$curl->addHeader("Authorization","Basic ".$Autorization);
		$curl->addHeader("Content-Type","application/json ");
 
		$curl->setMethode('POST');
		$curl->setData($data);
		
		$curl->make();
		
		if(($curl->ErrorNumber)<400)
		{
			$statut = $this->realStatut($curl->Reponse['data']['status']);
			
			$this->Return['reponse']= array
			(
				"amount"=>$this->receiveData['amount'],
				"status"=>$statut,
				"transaction_id"=> $this->IDTransaction,
				"reference_id"=> $this->receiveData['reference_id'],
				"order_id"=> $this->receiveData['order_id'],
				"provider_number"=> $this->Numeros['disbursement'],
				"provider_name"=> $this->Names['disbursement'],
				"uid"=> $this->ReferenceID,
				"amount_total"=>$this->receiveData['amount_total'],
				"phone_number"=> $this->Numero
			);
		 
		}
		elseif(($curl->ErrorNumber)==500)
		{
			if(!empty($curl->Reponse['message'])) $message = $curl->Reponse['message'];
			else $message = "The server did not respond correctly. ";
			$this->Return['error']=array
			(
				"status"=> "FAILED",
				"reason"=>$message,
				"code"=> $curl->ErrorNumber
			);
			//local_json($this->Return);
		}
		else{
		 $this->Return['error']=array
		 (
			 "status"=> "FAILED",
			 "reason"=>$curl->Reponse['errorMessage'],
			 "code"=> $curl->ErrorNumber
		 );
		 //local_json($this->Return);
		}
	}

	function getStatus($ptn)
	{
		global $sgbd;
		if(!is_array($current))
		{
			//$current = $sgbd->query("select * from transactions where MaviancePTN ='$current'")->row_array();
		}
	    $info = loadTransaction($current);
		
		//Cas des fonctionnalités PAYEMENT marchand
		if($callback = $sgbd->ligne("select * from callbacks where UID ='".$info->idFromClient."' and service ='".$info->serviceCode."'"))
		{
			$call = json_decode($callback['Infos']);
			return $call->status;
		}
		//if($this->Production) return 'PENDING';
		return 'PENDING';		
		
	}
	
	function setError($err)
	{
		$this->Return['error']   = 1;
		$this->Return['message'] = mb_encode($err);
		traceError($err,$this->Service);
		$this->Return = iso_encode($this->Return);
		echo (json_encode($this->Return,JSON_FORCE_OBJECT|JSON_UNESCAPED_UNICODE));
		exit();
	}
	private function realStatut($statut)
	{
		$teststatut = strtoupper($statut);
		switch($teststatut)
		{
			case  'SUCCESSFULL':
			case  'SUCCESS':
				$statut = 'SUCCESS';
			break;
			default: $statut = 'PENDING';
		}
		return $statut;
	}
}



?>