1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Nexylan packages. |
5
|
|
|
* |
6
|
|
|
* (c) Nexylan SAS <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Nexy\PayboxDirect\HttpClient; |
13
|
|
|
|
14
|
|
|
use Nexy\PayboxDirect\Exception\PayboxException; |
15
|
|
|
use Nexy\PayboxDirect\Paybox; |
16
|
|
|
use Nexy\PayboxDirect\Response\ResponseInterface; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @author Sullivan Senechal <[email protected]> |
20
|
|
|
* |
21
|
|
|
* @see http://www1.paybox.com/espace-integrateur-documentation/les-solutions-paybox-direct-et-paybox-direct-plus/ |
22
|
|
|
*/ |
23
|
|
|
abstract class AbstractHttpClient |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var int |
27
|
|
|
*/ |
28
|
|
|
protected $timeout; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var int |
32
|
|
|
*/ |
33
|
|
|
protected $baseUrl = Paybox::API_URL_TEST; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var string[] |
37
|
|
|
*/ |
38
|
|
|
private $baseParameters; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var int |
42
|
|
|
*/ |
43
|
|
|
private $defaultDevise; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var int|null |
47
|
|
|
*/ |
48
|
|
|
private $defaultActivity = null; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var int |
52
|
|
|
*/ |
53
|
|
|
private $questionNumber; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Constructor. |
57
|
|
|
*/ |
58
|
|
|
final public function __construct() |
59
|
|
|
{ |
60
|
|
|
$this->questionNumber = rand(0, time()); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param array $options |
65
|
|
|
*/ |
66
|
|
|
final public function setOptions($options) |
67
|
|
|
{ |
68
|
|
|
$this->timeout = $options['timeout']; |
69
|
|
|
$this->baseUrl = true === $options['production'] ? Paybox::API_URL_PRODUCTION : Paybox::API_URL_TEST; |
70
|
|
|
$this->baseParameters = [ |
|
|
|
|
71
|
|
|
'VERSION' => $options['paybox_version'], |
72
|
|
|
'SITE' => $options['paybox_site'], |
73
|
|
|
'RANG' => $options['paybox_rank'], |
74
|
|
|
'IDENTIFIANT' => $options['paybox_identifier'], |
75
|
|
|
'CLE' => $options['paybox_key'], |
76
|
|
|
]; |
77
|
|
|
$this->defaultDevise = $options['paybox_default_currency']; |
78
|
|
|
if (array_key_exists('paybox_default_activity', $options)) { |
79
|
|
|
$this->defaultActivity = $options['paybox_default_activity']; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Calls PayBox Direct platform with given operation type and parameters. |
85
|
|
|
* |
86
|
|
|
* @param int $type Request type |
87
|
|
|
* @param string[] $parameters Request parameters |
88
|
|
|
* @param string $responseClass |
89
|
|
|
* |
90
|
|
|
* @return ResponseInterface The response content |
91
|
|
|
* |
92
|
|
|
* @throws PayboxException |
93
|
|
|
*/ |
94
|
|
|
final public function call($type, array $parameters, $responseClass) |
95
|
|
|
{ |
96
|
|
|
if (!in_array(ResponseInterface::class, class_implements($responseClass))) { |
97
|
|
|
throw new \InvalidArgumentException('The response class must implement '.ResponseInterface::class.'.'); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
$bodyParams = array_merge($parameters, $this->baseParameters); |
101
|
|
|
$bodyParams['TYPE'] = $type; |
102
|
|
|
$bodyParams['NUMQUESTION'] = $this->questionNumber; |
103
|
|
|
$bodyParams['DATEQ'] = null !== $parameters['DATEQ'] ? $parameters['DATEQ'] : date('dmYHis'); |
104
|
|
|
// Restore default_currency from parameters if given |
105
|
|
|
if (array_key_exists('DEVISE', $parameters)) { |
106
|
|
|
$bodyParams['DEVISE'] = null !== $parameters['DEVISE'] ? $parameters['DEVISE'] : $this->defaultDevise; |
107
|
|
|
} |
108
|
|
|
if (!array_key_exists('ACTIVITE', $parameters) && $this->defaultActivity) { |
|
|
|
|
109
|
|
|
$bodyParams['ACTIVITE'] = $this->defaultActivity; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
$response = $this->request($bodyParams); |
113
|
|
|
|
114
|
|
|
// Generate results array |
115
|
|
|
$results = []; |
116
|
|
|
foreach (explode('&', $response) as $element) { |
117
|
|
|
list($key, $value) = explode('=', $element); |
118
|
|
|
$value = utf8_encode(trim($value)); |
119
|
|
|
$results[$key] = $value; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
$this->questionNumber = (int) $results['NUMQUESTION'] + 1; |
123
|
|
|
|
124
|
|
|
/** @var ResponseInterface $response */ |
125
|
|
|
$response = new $responseClass($results); |
126
|
|
|
|
127
|
|
|
if (!$response->isSuccessful()) { |
128
|
|
|
throw new PayboxException($response); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
return $response; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Init and setup http client with PayboxDirectPlus SDK options. |
136
|
|
|
*/ |
137
|
|
|
abstract public function init(); |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Sends a request to the server, receive a response and returns it as a string. |
141
|
|
|
* |
142
|
|
|
* @param string[] $parameters Request parameters |
143
|
|
|
* |
144
|
|
|
* @return string The response content |
145
|
|
|
*/ |
146
|
|
|
abstract protected function request($parameters); |
147
|
|
|
} |
148
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..