<?php

 
class Vend{

   //API CREDENTIALS
   private $clientId       = "omqamkbq";
   private $clientSecret   = "rebpnwdb";
   private $token          = "a7d8ec91-82ed-489e-ba9c-0f6bc2360083"; //Vend api token separated from  curl POSTFIELDS array
   private $momo_call_back = 'http/localhost/response.php';

   const  CLIENT_ID        = "omqamkbq";
   const  CLIENT_SECRET    = "rebpnwdb";


/**
 * Send a GET request to a URL.
 * 
 * @param  string $service_type, 
 * @param  array $body 
 * @return json
 * @throws Exception If cURL request fails.
 */
public function pay_bills($service_type,$body){
    
           $body['token']     = $this->token; //Merge API token to body array

           $basic_auth_key = 'Basic ' . base64_encode($this->clientId . ':' . $this->clientSecret);
           $request_url    = 'https://api.hubtel.com/vend/'.$service_type;
           $body           = json_encode($body);
   
        $ch = curl_init($request_url);  
              curl_setopt( $ch, CURLOPT_POST, true );  
              curl_setopt( $ch, CURLOPT_POSTFIELDS, $body);  
              curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );  
              curl_setopt( $ch, CURLOPT_HTTPHEADER, array(
                  'Authorization: '.$basic_auth_key,
                  'Cache-Control: no-cache',
                  'Accept: application/json',
                  'Content-Type: application/json'
                ));
         
            
               $result = curl_exec($ch); 
          $err = curl_error($ch);
          curl_close($ch);

          $res = json_decode($result,true);
          return $res;
} 


public function  data_bundles($endpoint){
    
           $body['token']     = $this->token; //Merge API token to body array

           $basic_auth_key = 'Basic ' . base64_encode($this->clientId . ':' . $this->clientSecret);
           $request_url    = 'https://api.hubtel.com/vend/'.$endpoint;
           $body           = json_encode($body);

        $ch = curl_init($request_url);  
              curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );  
              curl_setopt( $ch, CURLOPT_HTTPHEADER, array(
                  'Authorization: '.$basic_auth_key,
                  'Cache-Control: no-cache',
                  'Accept: application/json',
                  'Content-Type: application/json'
                ));
            
          $result = curl_exec($ch); 
          $err = curl_error($ch);
          curl_close($ch);

          $res = json_decode($result,true);
          return $res;
} 



public function mobile_money($body){
    
           $body['token']       = $this->token; //Merge API token to body array
           $body['callbackUrl'] = $this->momo_call_back; //callback url

           $basic_auth_key = 'Basic ' . base64_encode($this->clientId . ':' . $this->clientSecret);
           $request_url    = 'https://api.hubtel.com/vend/mobilemoney';
           $body           = json_encode($body);

        $ch = curl_init($request_url);  
              curl_setopt( $ch, CURLOPT_POST, true );  
              curl_setopt( $ch, CURLOPT_POSTFIELDS, $body);  
              curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );  
              curl_setopt( $ch, CURLOPT_HTTPHEADER, array(
                  'Authorization: '.$basic_auth_key,
                  'Cache-Control: no-cache',
                  'Accept: application/json',
                  'Content-Type: application/json'
                ));
         
            
               $result = curl_exec($ch); 
          $err = curl_error($ch);
          curl_close($ch);

          if($err){
            echo $err;
          }else{
            echo $result;
          }  
} 



}