|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Lexik\Bundle\PayboxBundle\Transport; |
|
4
|
|
|
|
|
5
|
|
|
use Lexik\Bundle\PayboxBundle\Paybox\RequestInterface; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class CurlTransport |
|
9
|
|
|
* |
|
10
|
|
|
* @package Lexik\Bundle\PayboxBundle\Transport |
|
11
|
|
|
* |
|
12
|
|
|
* @author Fabien Pomerol <[email protected]> |
|
13
|
|
|
*/ |
|
14
|
|
|
class CurlTransport extends AbstractTransport |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* Constructor |
|
18
|
|
|
* |
|
19
|
|
|
* @param string $url to paybox endpoint |
|
20
|
|
|
* |
|
21
|
|
|
* @throws \RuntimeException If cURL is not available |
|
22
|
|
|
*/ |
|
23
|
|
|
public function __construct($url = '') |
|
24
|
|
|
{ |
|
25
|
|
|
if (!function_exists('curl_init')) { |
|
26
|
|
|
throw new \RuntimeException('cURL is not available. Activate it first.'); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
parent::__construct($url); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* {@inheritDoc} |
|
34
|
|
|
* |
|
35
|
|
|
* @param RequestInterface $request Request instance |
|
36
|
|
|
* |
|
37
|
|
|
* @throws \RuntimeException On cURL error |
|
38
|
|
|
* |
|
39
|
|
|
* @return string $response The html of the temporary form |
|
40
|
|
|
*/ |
|
41
|
|
|
public function call(RequestInterface $request) |
|
42
|
|
|
{ |
|
43
|
|
|
$this->checkEndpoint(); |
|
44
|
|
|
|
|
45
|
|
|
$ch = curl_init(); |
|
46
|
|
|
|
|
47
|
|
|
// cURL options |
|
48
|
|
|
$options = array( |
|
49
|
|
|
CURLOPT_URL => $this->getEndpoint(), |
|
50
|
|
|
CURLOPT_HEADER => false, |
|
51
|
|
|
CURLOPT_RETURNTRANSFER => true, |
|
52
|
|
|
CURLOPT_POST => true, |
|
53
|
|
|
CURLOPT_POSTFIELDS => http_build_query($request->getParameters()), |
|
54
|
|
|
); |
|
55
|
|
|
curl_setopt_array($ch, $options); |
|
56
|
|
|
|
|
57
|
|
|
$response = curl_exec($ch); |
|
58
|
|
|
|
|
59
|
|
|
$curlErrorNumber = curl_errno($ch); |
|
60
|
|
|
$curlErrorMessage = curl_error($ch); |
|
61
|
|
|
$responseCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
|
62
|
|
|
|
|
63
|
|
|
if ($curlErrorNumber > 0 || !in_array($responseCode, array(0, 200, 201, 204))) { |
|
64
|
|
|
throw new \RuntimeException('cUrl returns some errors (cURL errno '.$curlErrorNumber.'): '.$curlErrorMessage.' (HTTP Code: '.$responseCode.')'); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
curl_close($ch); |
|
68
|
|
|
|
|
69
|
|
|
return $response; |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|