2

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.

Michał B
  • 339
  • 1
  • 3
  • 12
  • 1
    No sure why they would respond with JSON and a redirect at the same time, can't see much sense in that. Have you tried setting CURLOPT_FOLLOWLOCATION to false? – CBroe Nov 26 '21 at 08:51
  • Good advice! Change CURLOPT_FOLLOWLOCATION to false solved the problem. – Michał B Nov 26 '21 at 09:31

2 Answers2

0

try this:

<?php

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => 'https://secure.payu.com/api/v2_1/orders',
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'POST',
  CURLOPT_POSTFIELDS =>'{
        "notifyUrl": "https://your.eshop.com/notify",
        "customerIp": "127.0.0.1",
        "merchantPosId": "145227",
        "description": "RTV market",
        "currencyCode": "PLN",
        "totalAmount": "21000",
        "buyer": {
            "email": "john.doe@example.com",
            "phone": "654111654",
            "firstName": "John",
            "lastName": "Doe",
            "language": "pl"
        },
        "products": [
            {
                "name": "Wireless Mouse for Laptop",
                "unitPrice": "15000",
                "quantity": "1"
            },
            {
                "name": "HDMI cable",
                "unitPrice": "6000",
                "quantity": "1"
            }
        ]
    }',
  CURLOPT_HTTPHEADER => array(
    'Content-Type: application/json',
    'Authorization: Bearer 3e5cac39-7e38-4139-8fd6-30adc06a61bd'
  ),
));

$response = curl_exec($curl);

curl_close($curl);
echo $response;

Change fields accordingly. I strongly recommend using Postman for debugging requests. It also has a feature of importing requests from curl (provided here) and generating code snippets

gettinggud
  • 13
  • 4
  • Unfortunately this gives me same result. Changing CURLOPT_FOLLOWLOCATION to false as @CBroe suggested, helped to solve this. – Michał B Nov 26 '21 at 09:33
0

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