<?php 
    function callRequestpayment($amount, $currency, $externalId, $partyId, $accesstoken, $uuid, $message) {
        require_once '/opt/cpanel/ea-php72/root/usr/share/pear/HTTP/Request2.php';
        $request    = new Http_Request2('https://proxy.momoapi.mtn.com/collection/v1_0/requesttopay');
        $url        = $request->getUrl();
        $headers = array(
            'Authorization' => 'Bearer '.$accesstoken,
            'X-Reference-Id' => $uuid,
            'X-Target-Environment' => 'mtnbenin',
            'Content-Type' => 'application/json',
            'Ocp-Apim-Subscription-Key' => 'b4cfab4d10ac4416921694591c061a76',
        );
        
        $request->setHeader($headers);
        
        $parameters = array(
        );
        
        $url->setQueryVariables($parameters);
        
        $body = array(
          "externalId" => $externalId,
          "amount" => $amount,
          "currency" => $currency,
          "payer" => array(
            "partyIdType" => "MSISDN",
            "partyId" => $partyId
          ),
          "payerMessage" => $message,
          "payeeNote" => "GestStudentPay"
        );

        $request->setMethod(HTTP_Request2::METHOD_POST);
        // Request body
        $body = json_encode($body);
        $request->setBody($body);
        $response = '';
        try {
            $response = $request->send();
        } catch (HttpException $ex) {
            echo $ex;
        }
        return ($response);
    }
    
    function callRequestdisbursement($amount, $currency, $externalId, $partyId, $accesstoken, $uuid) {
        $request    = new Http_Request2('https://proxy.momoapi.mtn.com/disbursement/v1_0/transfer');
        $url        = $request->getUrl();
        $headers = array(
            'Authorization' => 'Bearer '.$accesstoken,
            //'X-Callback-Url' => 'e-services.geststudent.net',
            'X-Reference-Id' => $uuid,
            'X-Target-Environment' => 'mtnbenin',
            'Content-Type' => 'application/json',
            'Ocp-Apim-Subscription-Key' => 'b97a594500bc41a5b23315af0d52d06b'
        );
        
        $request->setHeader($headers);
        
        $parameters = array(
            //
        );
        
        $url->setQueryVariables($parameters);
        
        $body = array(
          "externalId" => $externalId,
          "amount" => $amount,
          "currency" => $currency,
          "payee" => array(
            "partyIdType" => "MSISDN",
            "partyId" => $partyId
          ),
          "payerMessage" => "Virement frais de scolarite pour le compte de votre universite...",
          "payeeNote" => "GestStudentPay : Virement frais de scolarite pour le compte de votre universite..."
        );

        $request->setMethod(HTTP_Request2::METHOD_POST);
        
        // Request body
        $body = json_encode($body);
        $request->setBody($body);
        $response = '';
        try {
            $response = $request->send();
            //die($response->getBody() );
        } catch (HttpException $ex) {
            echo $ex;
        }
        return ($response);
    }
    
    function getRequestpaymentstatus($accesstoken, $uuid) {
        $request = new Http_Request2('https://proxy.momoapi.mtn.com/collection/v1_0/requesttopay/'.$uuid.'');
        $url = $request->getUrl();
        
        $headers = array(
            // Request headers
            'Authorization' => 'Bearer '.$accesstoken,
            'X-Target-Environment' => 'mtnbenin',
            'Ocp-Apim-Subscription-Key' => 'b4cfab4d10ac4416921694591c061a76'
        );
        
        $request->setHeader($headers);
        
        $parameters = array(
            //'referenceId' => "$uuid"
        );
        
        $url->setQueryVariables($parameters);
        
        $request->setMethod(HTTP_Request2::METHOD_GET);
        
        // Request body
        $request->setBody("");
        $response = '';
        try {
            $response = $request->send();
            //print_r($response->getBody());
        } catch (HttpException $ex) {
            echo $ex;
        }
        return($response->getBody());
    }
    
    function getCollectionbalance($accesstoken) {
        $request = new Http_Request2('https://proxy.momoapi.mtn.com/collection/v1_0/account/balance');
        $url = $request->getUrl();
        
        $headers = array(
            // Request headers
            'Authorization' => 'Bearer '.$accesstoken,
            'X-Target-Environment' => 'mtnbenin',
            'Ocp-Apim-Subscription-Key' => 'b4cfab4d10ac4416921694591c061a76'
        );
        
        $request->setHeader($headers);
        
        $parameters = array(
            //'referenceId' => "$uuid"
        );
        
        $url->setQueryVariables($parameters);
        
        $request->setMethod(HTTP_Request2::METHOD_GET);
        
        // Request body
        $request->setBody("");
        $response = '';
        try {
            $response = $request->send();
            //print_r($response->getBody());
        } catch (HttpException $ex) {
            echo $ex;
        }
        return($response->getBody());
    }
    
    function getRequestdisbursementstatus($accesstoken, $uuid) {
        $request = new Http_Request2('https://proxy.momoapi.mtn.com/disbursement/v1_0/transfer/'.$uuid.'');
        $url = $request->getUrl();
        
        $headers = array(
            // Request headers
            'Authorization' =>  'Bearer '.$accesstoken,
            'X-Target-Environment' => 'mtnbenin',
            'Ocp-Apim-Subscription-Key' => 'b97a594500bc41a5b23315af0d52d06b',
        );
        
        $request->setHeader($headers);
        
        $parameters = array(
            // Request parameters
        );
        
        $url->setQueryVariables($parameters);
        
        $request->setMethod(HTTP_Request2::METHOD_GET);
        
        // Request body
        $request->setBody("{body}");
        
        $response = '';
        try {
            $response = $request->send();
        } catch (HttpException $ex) {
            echo $ex;
        }
        return($response->getBody());
    }
    
    function getDisbursementbalance($accesstoken) {
        $request = new Http_Request2('https://proxy.momoapi.mtn.com/disbursement/v1_0/account/balance');
        $url = $request->getUrl();
        
        $headers = array(
            // Request headers
            'Authorization' =>  'Bearer '.$accesstoken,
            'X-Target-Environment' => 'mtnbenin',
            'Ocp-Apim-Subscription-Key' => 'b97a594500bc41a5b23315af0d52d06b',
        );
        
        $request->setHeader($headers);
        
        $parameters = array(
            // Request parameters
        );
        
        $url->setQueryVariables($parameters);
        
        $request->setMethod(HTTP_Request2::METHOD_GET);
        
        // Request body
        $request->setBody("{body}");
        
        $response = '';
        try {
            $response = $request->send();
        } catch (HttpException $ex) {
            echo $ex;
        }
        return($response->getBody());
    }
?>