1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nexy\PayboxDirect\HttpClient; |
4
|
|
|
|
5
|
|
|
use Nexy\PayboxDirect\Exception\PayboxException; |
6
|
|
|
use Nexy\PayboxDirect\Paybox; |
7
|
|
|
use Nexy\PayboxDirect\Response\DirectPlusResponse; |
8
|
|
|
use Nexy\PayboxDirect\Response\DirectResponse; |
9
|
|
|
use Nexy\PayboxDirect\Response\PayboxResponse; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @author Sullivan Senechal <[email protected]> |
13
|
|
|
* |
14
|
|
|
* @see http://www1.paybox.com/espace-integrateur-documentation/les-solutions-paybox-direct-et-paybox-direct-plus/ |
15
|
|
|
*/ |
16
|
|
|
abstract class AbstractHttpClient |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var int |
20
|
|
|
*/ |
21
|
|
|
protected $timeout; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var int |
25
|
|
|
*/ |
26
|
|
|
protected $baseUrl = Paybox::API_URL_TEST; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string[] |
30
|
|
|
*/ |
31
|
|
|
private $baseParameters; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var int |
35
|
|
|
*/ |
36
|
|
|
private $defaultDevise; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var int |
40
|
|
|
*/ |
41
|
|
|
private $questionNumber; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Constructor. |
45
|
|
|
*/ |
46
|
|
|
final public function __construct() |
47
|
|
|
{ |
48
|
|
|
$this->questionNumber = rand(0, time()); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param array $options |
53
|
|
|
*/ |
54
|
|
|
final public function setOptions($options) |
55
|
|
|
{ |
56
|
|
|
$this->timeout = $options['timeout']; |
57
|
|
|
$this->baseUrl = true === $options['production'] ? Paybox::API_URL_PRODUCTION : Paybox::API_URL_TEST; |
58
|
|
|
$this->baseParameters = [ |
|
|
|
|
59
|
|
|
'VERSION' => $options['paybox_version'], |
60
|
|
|
'SITE' => $options['paybox_site'], |
61
|
|
|
'RANG' => $options['paybox_rank'], |
62
|
|
|
'IDENTIFIANT' => $options['paybox_identifier'], |
63
|
|
|
'CLE' => $options['paybox_key'], |
64
|
|
|
]; |
65
|
|
|
$this->defaultDevise = $options['paybox_default_currency']; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Calls PayBox Direct platform with given operation type and parameters. |
70
|
|
|
* |
71
|
|
|
* @param string $type Request type |
72
|
|
|
* @param string[] $parameters Request parameters |
73
|
|
|
* @param bool $directPlus |
74
|
|
|
* |
75
|
|
|
* @return DirectResponse|DirectPlusResponse The response content |
76
|
|
|
* |
77
|
|
|
* @throws PayboxException |
78
|
|
|
*/ |
79
|
|
|
public function call($type, array $parameters, $directPlus = false) |
80
|
|
|
{ |
81
|
|
|
$bodyParams = array_merge($parameters, $this->baseParameters); |
82
|
|
|
$bodyParams['TYPE'] = $type; |
83
|
|
|
$bodyParams['NUMQUESTION'] = $this->questionNumber; |
84
|
|
|
$bodyParams['DATEQ'] = null !== $parameters['DATEQ'] ? $parameters['DATEQ'] : date('dmYHis'); |
85
|
|
|
// Restore default_currency from parameters if given |
86
|
|
|
if (array_key_exists('DEVISE', $parameters)) { |
87
|
|
|
$bodyParams['DEVISE'] = null !== $parameters['DEVISE'] ? $parameters['DEVISE'] : $this->defaultDevise; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$response = $this->request($bodyParams); |
91
|
|
|
|
92
|
|
|
// Generate results array |
93
|
|
|
$results = []; |
94
|
|
|
foreach (explode('&', $response) as $element) { |
95
|
|
|
list($key, $value) = explode('=', $element); |
96
|
|
|
$value = utf8_encode(trim($value)); |
97
|
|
|
$results[$key] = $value; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
$this->questionNumber = (int) $results['NUMQUESTION'] + 1; |
101
|
|
|
|
102
|
|
|
if ('00000' !== $results['CODEREPONSE']) { |
103
|
|
|
throw new PayboxException($results['COMMENTAIRE'], $results['CODEREPONSE']); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
if (true === $directPlus) { |
107
|
|
|
return new DirectPlusResponse($results); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return new DirectResponse($results); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Init and setup http client with PayboxDirectPlus SDK options. |
115
|
|
|
*/ |
116
|
|
|
abstract public function init(); |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Sends a request to the server, receive a response and returns it as a string. |
120
|
|
|
* |
121
|
|
|
* @param string[] $parameters Request parameters |
122
|
|
|
* |
123
|
|
|
* @return string The response content |
124
|
|
|
*/ |
125
|
|
|
abstract protected function request($parameters); |
126
|
|
|
} |
127
|
|
|
|
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..