Skip to content

Commit

Permalink
Merge pull request #16 from AntonShevel/add-timeout-setting
Browse files Browse the repository at this point in the history
Add request timeout setting
  • Loading branch information
viplifes authored Oct 26, 2017
2 parents 8566527 + 7f155af commit 43d8e69
Showing 1 changed file with 30 additions and 16 deletions.
46 changes: 30 additions & 16 deletions LiqPay.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ class LiqPay
private $_private_key;
private $_server_response_code = null;


/**
* Constructor.
*
Expand All @@ -70,25 +69,25 @@ public function __construct($public_key, $private_key)
$this->_private_key = $private_key;
}


/**
* Call API
*
* @param string $path
* @param array $params
* @param int $timeout
*
* @return string
*/
public function api($path, $params = array())
public function api($path, $params = array(), $timeout = 5)
{
if (!isset($params['version'])) {
throw new InvalidArgumentException('version is null');
}
$url = $this->_api_url . $path;
$public_key = $this->_public_key;
$private_key = $this->_private_key;
$data = base64_encode(json_encode(array_merge(compact('public_key'), $params)));
$signature = base64_encode(sha1($private_key.$data.$private_key, 1));
$data = $this->encode_params(array_merge(compact('public_key'), $params));
$signature = $this->str_to_sign($private_key.$data.$private_key);
$postfields = http_build_query(array(
'data' => $data,
'signature' => $signature
Expand All @@ -98,6 +97,8 @@ public function api($path, $params = array())
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); // Avoid MITM vulnerability http://phpsecurity.readthedocs.io/en/latest/Input-Validation.html#validation-of-input-sources
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); // Check the existence of a common name and also verify that it matches the hostname provided
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT ,$timeout); // The number of seconds to wait while trying to connect
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); // The maximum number of seconds to allow cURL functions to execute
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
Expand All @@ -109,6 +110,7 @@ public function api($path, $params = array())

/**
* Return last api response http code
*
* @return string|null
*/
public function get_response_code()
Expand All @@ -133,7 +135,7 @@ public function cnb_form($params)
}

$params = $this->cnb_params($params);
$data = base64_encode(json_encode($params));
$data = $this->encode_params($params);
$signature = $this->cnb_signature($params);

return sprintf('
Expand All @@ -150,12 +152,6 @@ public function cnb_form($params)
);
}







/**
* cnb_signature
*
Expand All @@ -168,15 +164,12 @@ public function cnb_signature($params)
$params = $this->cnb_params($params);
$private_key = $this->_private_key;

$json = base64_encode(json_encode($params));
$json = $this->encode_params($params);
$signature = $this->str_to_sign($private_key . $json . $private_key);

return $signature;
}




/**
* cnb_params
*
Expand Down Expand Up @@ -210,6 +203,27 @@ private function cnb_params($params)
return $params;
}

/**
* encode_params
*
* @param array $params
* @return string
*/
private function encode_params($params)
{
return base64_encode(json_encode($params));
}

/**
* decode_params
*
* @param string $params
* @return array
*/
public function decode_params($params)
{
return json_decode(base64_decode($params), true);
}

/**
* str_to_sign
Expand Down

0 comments on commit 43d8e69

Please sign in to comment.