Search code examples
phpapihttpcurlpayu

API call returns HTML instead of JSON


I'm working on payment gateway on my website. I have one more step and I'm stuck on it. I have to send order to payment provider API and it should return object with some data and redirectURI, which I must redirect client to.

Problem that I have is API response. It returns HTML instead of JSON. Below is my request:

    $curl = curl_init();
    
    $data = $data->get_params();
    
    $data['order']['customerIp'] = $_SERVER['REMOTE_ADDR'];
    $data['order']['extOrderId'] = generateRandomString();
    
    $postdata = json_encode($data['order']);

    curl_setopt_array($curl, array(
        CURLOPT_URL => 'https://secure.snd.payu.com/api/v2_1/orders',
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => '',
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 0,
        CURLOPT_HEADER => false,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => 'POST',
        CURLOPT_POSTFIELDS => $postdata,
        CURLOPT_HTTPHEADER => array(
                'Content-Type: application/json',
            'Authorization: Bearer '.$data['token']
        ),
    ));

    $response = curl_exec($curl);

    curl_close($curl);

    return rest_ensure_response( $response );

On documentation website I found this info message:

Note: The HTTP status code of the response is 302 and Location header is set to redirectUri, which - depending on the software used - may sometimes trigger an automatic redirect as well as receiving responses in HTML format.

I assume that HTML which is in response contains website that client should be redirrected to.

JSON response should look like this:

{  
   "status":{  
      "statusCode":"SUCCESS",
   },
   "redirectUri":"{payment_summary_redirection_url}",
   "orderId":"WZHF5FFDRJ140731GUEST000P01",
   "extOrderId":"{YOUR_EXT_ORDER_ID}",
}

Do you know how to fix this or if there is any possibility to do this? Or maybe to retrieve path that is in Location header and send it to client?

On my backend I'm using PHP and Angular on frontend.

Thanks for any ideas.

Thanks.


Solution

  • I have encountered a similar problem with PayU payment gateway in C#. The solution was setting AutoRedirect to false. In PHP it should look following:

    CURLOPT_FOLLOWLOCATION => false