PHP Curl request is not working for easypaisa payment gateway post request

Viewed 694

I am integrating a payment gateway in my website and the payment gateway initiation is done via form which includes hidden fields to post the data.

Form's HTML code is: (Pasting only HTML code cannot share details, as API support is not good, and they have also removed my Sandbox store ID due to 5-10 frequent requests.)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Easypay</title>
</head>
<body>
    <form action="https://easypaystg.easypaisa.com.pk/easypay/Index.jsf" method="POST" id="easyPayStartForm" target="_blank">
        <input name="storeId" value="<?php echo $storeId; ?>" hidden = "true"/>
        <input name="amount" value="<?php echo $amount; ?>" hidden = "true"/>
        <input name="postBackURL" value="<?php echo $postBackURL; ?>" hidden = "true"/>
        <input name="orderRefNum" value="<?php echo $orderRefNum; ?>" hidden = "true"/>
        <input type ="text" name="expiryDate" value="<?php echo $expiryDate; ?>">
        <input type="hidden" name="autoRedirect" value="<?php echo $autoRedirect; ?>" >
        <input type ="hidden" name="paymentMethod" value="<?php echo $paymentMethod; ?>">
        <input type ="hidden" name="emailAddr" value="<?php echo $emailAddr; ?>">
        <input type ="hidden" name="mobileNum" value="<?php echo $mobileNum; ?>">
        <input type ="hidden" name="merchantHashedReq" value="<?php echo $hashRequest; ?>">
        <button type="submit">Submit</button>
    </form>
</body>
</html>

When I submit the form it redirects me to the payment portal and after successful transactions, it throws me back to the postback URL.

Payment gateway via the form is working perfectly fine, but as the form will include sensitive gateway API's details, so I am trying to post the form data via PHP Curl Request on the backend for secure transactions. But when I try using PHP curl request, it shows me an invalid request:

Following is my PHP Curl Request code:

<?php
    $curl = curl_init();
    
    curl_setopt_array($curl, array(
      CURLOPT_URL => "https://easypay.easypaisa.com.pk/easypay/Index.jsf",
      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 => "storeId=$storeId&amount=$amount&postBackURL=$postBackURL&orderRefNum=$orderRefNum&expiryDate=$expiryDate&autoRedirect=$autoRedirect&paymentMethod=$paymentMethod&emailAddr=$emailAddr&mobileNum=$mobileNum&merchantHashedReq=$merchantHashedReq",
      CURLOPT_HTTPHEADER => array(
        "Content-Type: application/x-www-form-urlencoded"
      ),
    ));
    
    $response = curl_exec($curl);
    
    curl_close($curl);
    echo $response;
?>

I changed the following python request (they have given it in their doc) to PHP Curl Request:

(Data is a dummy so the request will not be successful, kindly don't try to run this code, it's just for python I have converted it to PHP CURL request)

import httplib,urllib
host='https://easypay.easypaisa.com.pk'
url ='/easypay/Index.jsf'
values ={'storeId':'43','amount':'10','postBackURL':'http://www.my.online-store.com/transaction/MessageHandler','orderRefNum':'1101','expiryDate':'20140606201521',‘merchantHashedReq’=> ‘aasldfjlaksdjf;laksjdf;asdf--=====’,‘autoRedirect’=> ‘0’,‘paymentMethod’=> ‘OTC_PAYMENT_METHOD’,‘emailAddr’=> ‘test.abcd@domain.com’,‘mobileNum’=> ‘03321041021’}
headers={'User-Agent': 'python','Content-Type':'application/x-www-form-urlencoded',}
values =urllib.urlencode(values)
conn = httplib.HTTPSConnection(host)
conn.request("POST",url,values,headers)
response =conn.getresponse()data=response.read()

Am I doing something wrong when creatinf the PHP curl request?

1 Answers

if you want to set CURLOPT_POSTFIELDS as a string you need to url encode the values, it is easier and cleaner to use an array also you need to use the option CURLOPT_POST => true

CURLOPT_POST => true,
CURLOPT_POSTFIELDS => [
    'storeId' => $storeId, 
    'amount' => $amount, 
    'postBackURL' => $postBackURL, 
    'orderRefNum' => $orderRefNum, 
    'expiryDate' => $expiryDate, 
    'autoRedirect' => $autoRedirect, 
    'paymentMethod' => $paymentMethod, 
    'emailAddr' => $emailAddr, 
    'mobileNum' => $mobileNum, 
    'merchantHashedReq' => $merchantHashedReq
],
Related