【问题标题】:Payfort REST POST request using JSON使用 JSON 的 Payfort REST POST 请求
【发布时间】:2016-12-03 19:42:37
【问题描述】:

使用 payfort 支付 API https://testfort.payfort.com/api/?#merchant-page 执行标记化后,我遇到了使用 JSON 的 REST POST 请求的问题。我的代码是

$requestParams=json_encode($requestParams);
$service_url = 'https://sbpaymentservices.payfort.com/FortAPI/paymentApi';
$curl = curl_init($service_url);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_POSTFIELDS, $requestParams);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($requestParams))); 
$curl_response = curl_exec($curl);

$curl_response 总是假的

【问题讨论】:

  • 不使用原始 API,而是检查 Payfort 是否没有包装器 :)
  • 搜索了很多,如果你找到了,请分享链接:)
  • 在github上查看payfort/start-php
  • 我之前也查过,但是感觉不一样。他们确实需要 api 密钥才能使用它。我正在尝试另一种方式,我希望我的方式应该成功。谢谢

标签: php json rest payfort


【解决方案1】:

我也遇到了同样的问题。 您可以使用以下代码。 它工作正常。

    $merchant_reference = str_random(30);
    $redirectUrl = 'https://sbpaymentservices.payfort.com/FortAPI/paymentApi';
    $return_url = 'enter_your_return_url_here';

    $requestParams = array(
        'command' => 'PURCHASE',
        'access_code' => 'enter_your_acccess_code_here',
        'merchant_identifier' => 'enter_your_merchant_identifier_here',
        'merchant_reference' => enter_your_merchant_reference_here,
        'amount' => enter_your_amount_here,
        'currency' => 'AED',
        'language' => enter_your_language_here,
        'customer_email' => 'customer@gmail.com',
        'token_name' => enter_your_token_name_here,
        'return_url' => return_url,
        'card_security_code' => enter_your_cvv_here,
    );

    // calculating signature
    $shaString = '';
    ksort($arrData);
    $SHARequestPhrase   = 'GLAM';
    $SHAResponsePhrase   = 'GLAM';
    $SHAType       = 'sha256';
    foreach ($arrData as $k => $v) {
        $shaString .= "$k=$v";
    }

    if ($signType == 'request') 
        $shaString = $SHARequestPhrase . $shaString . $SHARequestPhrase;
    else 
        $shaString = $SHAResponsePhrase . $shaString . $SHAResponsePhrase;

    $signature = hash($SHAType, $shaString);

    $requestParams['signature'] = hash($SHAType, $shaString);

    // calling payfort api using curl
    //open connection
    $ch = curl_init();

    //set the url, number of POST vars, POST data
    $useragent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:20.0) Gecko/20100101 Firefox/20.0";
    curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Content-Type: application/json;charset=UTF-8',
            //'Accept: application/json, application/*+json',
            //'Connection:keep-alive'
    ));
    curl_setopt($ch, CURLOPT_URL, $redirectUrl);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_FAILONERROR, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // allow redirects     
    //curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // return into a variable
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0); // The number of seconds to wait while trying to connect
    //curl_setopt($ch, CURLOPT_TIMEOUT, Yii::app()->params['apiCallTimeout']); // timeout in seconds
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($requestParams));

    $response = curl_exec($ch);

    curl_close($ch);

    return $response;

这是向您返回完整的付款响应。

【讨论】:

  • 有没有类似的java?
【解决方案2】:

这个给出结果

        $requestParams['signature'] = $signature;
        $requestParams=json_encode($requestParams);

        $result = file_get_contents('https://sbpaymentservices.payfort.com/FortAPI/paymentApi', null, stream_context_create(array(
                'http' => array(
                'method' => 'POST',
                'header' => 'Content-Type: application/json' . "\r\n"
                . 'Content-Length: ' . strlen($requestParams) . "\r\n",
                'content' => $requestParams,
                ),
            )
        ));

        $result=json_decode($result);

【讨论】:

    【解决方案3】:

    我没有关于 payfort 的具体信息,但是您以错误的方式发送 JSON 数据(可能)。

    $requestParams = json_encode($requestParams);
    /*other codes*/
    curl_setopt($ch, CURLOPT_POSTFIELDS, 'json='.urlencode($requestParams));
    

    您应该尝试添加“json=”前缀并使用 urlencode 过滤/编码一些特殊字符。

    【讨论】:

    • 还是假的,在你这边测试一下,如果 curl 成功它会给出响应(即使是错误的)
    • 你能在代码末尾添加if (curl_error($ch)) echo curl_error($ch); 行并分享结果吗?
    • 在 $info = curl_getinfo($curl); 之后分享了图片var_export($info)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-16
    • 2018-12-20
    • 2019-03-31
    • 2020-02-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多