PHP Curl请求不适用于easypaisa付款网关发布请求

时间:2020-08-23 19:38:05

标签: php curl

我正在将支付网关集成到我的网站中,并且通过包含隐藏字段以发布数据的表单来完成支付网关的初始化。

Form的HTML代码为: (仅粘贴HTML代码无法共享详细信息,因为API支持不好,并且由于5-10次频繁的请求,他们还删除了我的Sandbox商店ID。)

<!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>

当我提交表单时,它会将我重定向到付款门户,并且在成功交易后,它会将我带回到回发URL。

通过表单的支付网关可以正常工作,但是由于表单将包含敏感网关API的详细信息,因此我试图通过PHP Curl Request将表单数据发布到后端以进行安全交易。但是当我尝试使用PHP curl请求时,它显示了一个无效的请求:

以下是我的PHP卷曲请求代码:

<?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;
?>

我将以下python请求(他们在其文档中提供了此请求)更改为PHP Curl Request:

(数据是虚拟的,因此请求将不会成功,请不要尝试运行此代码,仅用于python,我已将其转换为PHP CURL请求)

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()

创建 PHP curl request 时我做错什么了吗?

1 个答案:

答案 0 :(得分:0)

如果要将CURLOPT_POSTFIELDS设置为字符串,则需要对值进行url编码,那么使用数组会更容易更干净 您还需要使用选项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
],