Skip to main content
Question

Need help in integrating the requesttopay


I am getting some response as string(0) ""

public function requestToPay()
    {

        $url = "https://sandbox.momodeveloper.mtn.com/collection/v1_0/requesttopay";
        $curl = curl_init($url);

        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

        # Request headers https://mysite.com
        $headers = array(
            'Authorization: '.$this->createAccessToken(),
            'X-Callback-Url: mysite.com',
            'X-Reference-Id: '.$this->getGUID(),
            'X-Target-Environment: sandbox',
            'Content-Type: application/json',
            'Cache-Control: no-cache',
            'Ocp-Apim-Subscription-Key: *******');
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);

        # Request body 0248888736
        $request_body = '{
            "amount": "5.0",
            "currency": "EUR",
            "externalId": "6353636",
            "payer": {
                "partyIdType": "MSISDN",
                "partyId": "46733123450"
            },
            "payerMessage": "Pay for product a",
            "payeeNote": "payer note"
        }';
        curl_setopt($curl, CURLOPT_POSTFIELDS, $request_body);

        $resp = curl_exec($curl);
        curl_close($curl);
        	$access_token = json_decode($resp,true);
        var_dump($resp);
        die();
}


 public function createAccessToken()
    {
        $auth=base64_encode($this->api_user.":".$this->api_key);
        // echo $auth; die();
    	$headers=array("Authorization: Basic ".$auth,
    	"Ocp-Apim-Subscription-Key:".$this->ocp_apim_subscription_key
    	);


        $url = 'https://sandbox.momodeveloper.mtn.com/collection/token/';
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_TIMEOUT, 50);
		curl_setopt($ch, CURLOPT_POST, true);
		curl_setopt($ch, CURLOPT_POSTFIELDS, array());
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
		$result = curl_exec($ch);
		$access_token = json_decode($result,true);
		curl_close($ch);

		return $access_token['access_token'];

    }

function getGUID(){
        if (function_exists('com_create_guid')){
            return com_create_guid();
        }else{
            mt_srand((double)microtime()*10000);//optional for php 4.2.0 and up.
            $charid = strtoupper(md5(uniqid(rand(), true)));
            $hyphen = chr(45);// "-"
            $uuid = substr($charid, 0, 8).$hyphen
                .substr($charid, 8, 4).$hyphen
                .substr($charid,12, 4).$hyphen
                .substr($charid,16, 4).$hyphen
                .substr($charid,20,12);

            return $uuid;
        }
    }

Can any one help me, where i am going wrong.

Thanks in Advance

Forum|alt.badge.img

Try to debug function at a time and share response 


I am getting some response as string(0) ""


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings