<?php
//URL AND REQUEST DETAILS
$url ='https://secure.tigo.com/v1/oauth/generate/accesstoken?grant_type=client_credentials';
$method='POST';
$headers='Content-Type:application/x-www-form-urlencoded';
$body='client_id=TbfA7jggK2rxnYiDYz5hcyh7APADejpy&client_secret=Cqg0dCKWGh9xKqZA';
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_CUSTOMREQUEST,$method);
curl_setopt($ch,CURLOPT_HTTPHEADER,[$headers]);
curl_setopt($ch,CURLOPT_POSTFIELDS,$body);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
$response=curl_exec($ch);
$data=json_decode($response,true);
$token=$data['accessToken'];
if (curl_errno($ch))
{
	echo 'Error'.curl_error($ch);
} //else
//{
//	echo 'Response: '.$response;
//}
curl_close($ch);
$rid=md5(Date("Ymdhs"));
//$head=array( 'accessToken:BGHJKGGdffsbhkkkkj',
               //'Content-Type:application/json'
              //);
                                  
$cch=curl_init();
curl_setopt($cch,CURLOPT_URL,'https://secure.tigo.com/v1/tigo/payment-auth/authorize');
curl_setopt($cch,CURLOPT_POST,1);

curl_setopt($cch,CURLOPT_HTTPHEADER,array(
                'accessToken:"'.$token.'"',
                'Content-Type:application/json'));
                
//curl_setopt($cch,RETURNTRANSFER,1);

curl_setopt($cch,CURLOPT_POSTFIELDS,json_encode(array
(
  'MasterMerchant'=>array(
                           'account'=>'25565404242',
                           'pin'=>'5978',
                           'id'=>'AIDERS TECH CO.LTD'
                          ),
  'Merchant'=>array(
  
                      'reference'=>$rid,
                      'fee'=>'0',
                       'currencCode'=>'TZS'
                    ),
  'Subscriber'=>array(
                      'account'=>'',
                      'countryCode'=>255,
                      'country'=>'TZA',
                      'firsName'=>'',
                      'lastName'=>'',
                      'emailId'=>''
                    ),
                    
     'redirectUri' =>'https://www.aiderstech.co.tz/myPhone/payFees.php',
    'callbackUri'=>'',
  //'redirectUri'=>header("Location:https://secure.tigo.com/v1/tigo/payment-auth/transactions?auth_code=hghgjgh&transaction_ref_id=".$rid."&lang=eng", true, 301),
 //'callbackUri'=>header("Location:https://secure.tigo.com/v1/tigo/payment-auth/transactions?auth_code=hghgjgjj&transaction_ref_id=".$rid."&lang=eng", true, 301),

  'language'=>'eng',
  'terminalId'=>'',
  'originPayment'=>array(
                       'amount'=>'10000',
                       'currencyCode'=>'TZS',
                       'tax'=>'0',
                       'fee'=>'0'
                                      
                      ),
  'exchangeRate'=>'1',
  'LocalPayment'=>array( 
                         'amount'=>'10000',
                         'currencyCode'=>'TZS'
                       ),
  'transactionRefId'=>$rid
)));
curl_setopt($cch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($cch,CURLOPT_HTTPAUTH,CURLAUTH_BASIC);
$response=curl_exec($cch);
$result=json_decode($response,true);
//print_r($result);
$ur=$result['redirectUrl'];
//print_r($ur);
//echo $token;
//print_r($result);
//echo "<script>window.location.href='. $ur.'</script>";
//echo $ur;
header("Location:$ur", true, 301);
//$jsonData=json_encode($data);
//$url='';
/*$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,'https://secure.tigo.com/v1/tigo/payment-auth/authorize');
curl_setopt($ch,CURLOPT_CUSTOMREQUEST,$method);
curl_setopt($ch,CURLOPT_HTTPHEADER, array(
                                          'Content-Type:application/json',
                                          'accessToken:'.$token
                                         )
                                  
                                  );
curl_setopt($ch,CURLOPT_POSTFIELDS,$jsonData);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
echo $token;
//$response=curl_exec($ch);
*/
///curl_close($ch);/////////
?>