【问题标题】:Curl and SagepayCurl 和 Sagepay
【发布时间】:2014-02-20 13:23:30
【问题描述】:

我在将 Sagepay InFrame Server 与 PHP 集成时遇到了很多麻烦,因为没有可用于新协议 (v3) 的集成工具包。我有 v2.23 的旧工具包,但其中的大部分代码已被弃用。

目前,我成功从 Sagepay 服务器服务器检索 OK 状态的唯一方法是拥有一个表单,其中包含 Sagepay 所需的隐藏值集合,包括加密字段,并使用服务器 URL 作为形成动作。这在浏览器选项卡中为我提供了“OK”状态和 SecurityKey 等,但它在浏览器选项卡中没有多大用处,因为我需要在我的服务器上返回 POST 响应,而不是在他们的服务器上。

为此,我选择了 curl。我将 curl_exec 的返回值保存在一个名为 $rawresponse 的变量中,并在每次尝试后转储响应,就目前而言,$rawresponse 以虚假布尔值的形式返回:

$curlSession = curl_init();

curl_setopt ($curlSession, CURLOPT_URL, $url);
curl_setopt ($curlSession, CURLOPT_HEADER, 0);
curl_setopt ($curlSession, CURLOPT_POST, 1);
$data['Crypt'] = new CurlFile('filename.png', 'image/png', 'filename.png');
curl_setopt ($curlSession, CURLOPT_POSTFIELDS, $data);
curl_setopt($curlSession, CURLOPT_RETURNTRANSFER,1); 
curl_setopt($curlSession, CURLOPT_TIMEOUT,30); 
curl_setopt($curlSession, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curlSession, CURLOPT_SSL_VERIFYHOST, 0);

$rawresponse = curl_exec($curlSession);

现在你可以在这里看到,我必须强制 Crypt 值是 CurlFile 类型,我认为这会破坏请求,但是如果我不这样做,我会收到以下错误:

“不推荐使用@filename API 进行文件上传。请改用CURLFile 类”

我只能从中确定加密被误认为是文件(可能是因为字符串以 @ 开头),并且为了应对这种情况,我试图将其强制为图像。

所以我的问题是 - 有没有一种特殊的方式来使用 CurlFile 以便可以理解加密字符串?有没有更好的方法将此功能与任何人都知道的 Sagepay 集成?这确实是一个非常令人困惑的系统,而且由于完全缺乏示例而使良好的文档失望。

【问题讨论】:

    标签: php curl opayo


    【解决方案1】:

    为此道歉,我以错误的方式构建字符串。以防万一将来有人遇到类似问题,我将粘贴适合我的代码:

    public function registerTransaction()
    {
        $VPSProtocol = urlencode($_POST['VPSProtocol']);
        $TxType = urlencode($_POST['TxType']);
        $Vendor = urlencode($_POST['Vendor']);
        $VendorTxCode = urlencode($_POST['VendorTxCode']);
        $Currency = urlencode($_POST['Currency']);
        $Amount = urlencode($_POST['Amount']);
        $NotificationURL = urlencode($_POST['NotificationURL']);
        $Description = urlencode($_POST['Description']);
        $BillingSurname = urlencode($_POST['BillingSurname']);
        $BillingFirstnames = urlencode($_POST['BillingFirstnames']);
        $BillingAddress1 = urlencode($_POST['BillingAddress1']);
        $BillingCity = urlencode($_POST['BillingCity']);
        $BillingPostCode = urlencode($_POST['BillingPostCode']);
        $BillingCountry = urlencode($_POST['BillingCountry']);
        $DeliverySurname = urlencode($_POST['DeliverySurname']);
        $DeliveryFirstnames = urlencode($_POST['DeliveryFirstnames']);
        $DeliveryAddress1 = urlencode($_POST['DeliveryAddress1']);
        $DeliveryCity = urlencode($_POST['DeliveryCity']);
        $DeliveryPostCode = urlencode($_POST['DeliveryPostCode']);
        $DeliveryCountry = urlencode($_POST['DeliveryCountry']);
    
        $url = "?VPSProtocol=" . $VPSProtocol;
        $url .= "&TxType=" . $TxType;
        $url .= "&Vendor=" . $Vendor;
        $url .= "&VendorTxCode=" . $VendorTxCode;
        $url .= "&Currency=" . $Currency;
        $url .= "&Amount=" . $Amount;
        $url .= "&NotificationURL=" . $NotificationURL;
        $url .= "&Description=" . $Description;
        $url .= "&BillingSurname=" . $BillingSurname;
        $url .= "&BillingFirstnames=" . $BillingFirstnames;
        $url .= "&BillingAddress1=" . $BillingAddress1;
        $url .= "&BillingCity=" . $BillingCity;
        $url .= "&BillingPostCode=" . $BillingPostCode;
        $url .= "&BillingCountry=" . $BillingCountry;
        $url .= "&DeliverySurname=" . $DeliverySurname;
        $url .= "&DeliveryFirstnames=" . $DeliveryFirstnames;
        $url .= "&DeliveryAddress1=" . $DeliveryAddress1;
        $url .= "&DeliveryCity=" . $DeliveryCity;
        $url .= "&DeliveryPostCode=" . $DeliveryPostCode;
        $url .= "&DeliveryCountry=" . $DeliveryCountry;
    
        $strPurchaseURL = "https://test.sagepay.com/gateway/service/vspserver-register.vsp";
        $arrResponse = $this->requestPost($strPurchaseURL, $url);
        dd($arrResponse);
    }
    
    
    public function requestPost($url, $data){
        // Set a one-minute timeout for this script
        set_time_limit(60);
    
        // Initialise output variable
        $output = array();
    
        // Open the cURL session
        $curlSession = curl_init();
    
        curl_setopt ($curlSession, CURLOPT_URL, $url);
        curl_setopt ($curlSession, CURLOPT_HEADER, 0);
        curl_setopt ($curlSession, CURLOPT_POST, 1);
        curl_setopt ($curlSession, CURLOPT_POSTFIELDS, $data);
        curl_setopt($curlSession, CURLOPT_RETURNTRANSFER,1); 
        curl_setopt($curlSession, CURLOPT_TIMEOUT,30); 
        curl_setopt($curlSession, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curlSession, CURLOPT_SSL_VERIFYHOST, 2);
    
        $rawresponse = curl_exec($curlSession);
        dd($rawresponse);
        //Store the raw response for later as it's useful to see for integration and understanding 
        $_SESSION["rawresponse"]=$rawresponse;
        //Split response into name=value pairs
        $response = preg_split(chr(10), $rawresponse);
        // Check that a connection was made
        if (curl_error($curlSession)){
            // If it wasn't...
            $output['Status'] = "FAIL";
            $output['StatusDetail'] = curl_error($curlSession);
        }
    
        // Close the cURL session
        curl_close ($curlSession);
    
        // Tokenise the response
        for ($i=0; $i<count($response); $i++){
            // Find position of first "=" character
            $splitAt = strpos($response[$i], "=");
            // Create an associative (hash) array with key/value pairs ('trim' strips excess whitespace)
            $output[trim(substr($response[$i], 0, $splitAt))] = trim(substr($response[$i], ($splitAt+1)));
        } // END for ($i=0; $i<count($response); $i++)
    
        // Return the output
        return $output;
    } // END function requestPost()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-21
      • 1970-01-01
      • 2016-11-15
      • 2012-05-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多