1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nexy\PayboxDirect; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Annotations\AnnotationRegistry; |
6
|
|
|
use Nexy\PayboxDirect\Enum\Activity; |
7
|
|
|
use Nexy\PayboxDirect\Enum\Currency; |
8
|
|
|
use Nexy\PayboxDirect\Enum\Receiver; |
9
|
|
|
use Nexy\PayboxDirect\Enum\Version; |
10
|
|
|
use Nexy\PayboxDirect\Exception\InvalidRequestPropertiesException; |
11
|
|
|
use Nexy\PayboxDirect\HttpClient\AbstractHttpClient; |
12
|
|
|
use Nexy\PayboxDirect\HttpClient\GuzzleHttpClient; |
13
|
|
|
use Nexy\PayboxDirect\OptionsResolver\OptionsResolver; |
14
|
|
|
use Nexy\PayboxDirect\Request\InquiryRequest; |
15
|
|
|
use Nexy\PayboxDirect\Request\RequestInterface; |
16
|
|
|
use Nexy\PayboxDirect\Response\DirectPlusResponse; |
17
|
|
|
use Nexy\PayboxDirect\Response\DirectResponse; |
18
|
|
|
use Nexy\PayboxDirect\Response\InquiryResponse; |
19
|
|
|
use Nexy\PayboxDirect\Response\ResponseInterface; |
20
|
|
|
use Symfony\Component\Validator\Validation; |
21
|
|
|
use Symfony\Component\Validator\Validator\ValidatorInterface; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @author Sullivan Senechal <[email protected]> |
25
|
|
|
* |
26
|
|
|
* @see http://www1.paybox.com/espace-integrateur-documentation/les-solutions-paybox-direct-et-paybox-direct-plus/les-operations-de-caisse-direct-plus/ |
27
|
|
|
* @see http://www1.paybox.com/espace-integrateur-documentation/dictionnaire-des-donnees/paybox-direct-et-direct-plus/ |
28
|
|
|
*/ |
29
|
|
|
final class Paybox |
30
|
|
|
{ |
31
|
|
|
const API_URL_PRODUCTION = 'https://ppps.paybox.com/PPPS.php'; |
32
|
|
|
const API_URL_RESCUE = 'https://ppps1.paybox.com/PPPS.php'; |
33
|
|
|
const API_URL_TEST = 'https://preprod-ppps.paybox.com/PPPS.php'; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var ValidatorInterface |
37
|
|
|
*/ |
38
|
|
|
private $validator; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var AbstractHttpClient |
42
|
|
|
*/ |
43
|
|
|
private $httpClient; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var array |
47
|
|
|
*/ |
48
|
|
|
private $options; |
49
|
|
|
|
50
|
|
|
public function __construct(array $options = [], AbstractHttpClient $httpClient = null) |
51
|
|
|
{ |
52
|
|
|
$resolver = new OptionsResolver(); |
53
|
|
|
$this->configureOptions($resolver); |
54
|
|
|
|
55
|
|
|
$this->options = $resolver->resolve($options); |
56
|
|
|
|
57
|
|
|
AnnotationRegistry::registerLoader('class_exists'); |
58
|
|
|
$this->validator = Validation::createValidatorBuilder() |
59
|
|
|
->enableAnnotationMapping() |
60
|
|
|
->getValidator(); |
61
|
|
|
|
62
|
|
|
$this->httpClient = $httpClient ? $httpClient : new GuzzleHttpClient(); |
63
|
|
|
$this->httpClient->setOptions($this->options); |
64
|
|
|
$this->httpClient->init(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param RequestInterface $request |
69
|
|
|
* |
70
|
|
|
* @return DirectResponse |
71
|
|
|
*/ |
72
|
|
|
public function sendDirectRequest(RequestInterface $request) |
73
|
|
|
{ |
74
|
|
|
if ($request->getRequestType() >= RequestInterface::SUBSCRIBER_AUTHORIZE) { |
75
|
|
|
throw new \InvalidArgumentException( |
76
|
|
|
'Direct Plus requests must be passed onto '.__CLASS__.'::sendDirectPlusRequest method.' |
77
|
|
|
); |
78
|
|
|
} |
79
|
|
|
if ($request instanceof InquiryRequest) { |
80
|
|
|
throw new \InvalidArgumentException( |
81
|
|
|
'Inquiry requests must be passed onto '.__CLASS__.'::sendInquiryRequest method.' |
82
|
|
|
); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return $this->request($request); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param RequestInterface $request |
90
|
|
|
* |
91
|
|
|
* @return DirectPlusResponse |
92
|
|
|
*/ |
93
|
|
|
public function sendDirectPlusRequest(RequestInterface $request) |
94
|
|
|
{ |
95
|
|
|
if ($request->getRequestType() < RequestInterface::SUBSCRIBER_AUTHORIZE) { |
96
|
|
|
throw new \InvalidArgumentException( |
97
|
|
|
'Direct requests must be passed onto '.__CLASS__.'::sendDirectRequest method.' |
98
|
|
|
); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return $this->request($request, DirectPlusResponse::class); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param InquiryRequest $request |
106
|
|
|
* |
107
|
|
|
* @return InquiryResponse |
108
|
|
|
*/ |
109
|
|
|
public function sendInquiryRequest(InquiryRequest $request) |
110
|
|
|
{ |
111
|
|
|
return $this->request($request, InquiryResponse::class); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param RequestInterface $request |
116
|
|
|
* @param string $responseClass |
117
|
|
|
* |
118
|
|
|
* @return ResponseInterface |
119
|
|
|
* |
120
|
|
|
* @throws Exception\InvalidRequestPropertiesException |
121
|
|
|
* @throws Exception\PayboxException |
122
|
|
|
*/ |
123
|
|
|
private function request(RequestInterface $request, $responseClass = DirectResponse::class) |
124
|
|
|
{ |
125
|
|
|
$errors = $this->validator->validate($request); |
126
|
|
|
if ($errors->count() > 0) { |
127
|
|
|
throw new InvalidRequestPropertiesException($request, $errors); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
return $this->httpClient->call($request->getRequestType(), $request->getParameters(), $responseClass); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Paybox base options validation. |
135
|
|
|
* |
136
|
|
|
* @param OptionsResolver $resolver |
137
|
|
|
*/ |
138
|
|
|
private function configureOptions(OptionsResolver $resolver) |
139
|
|
|
{ |
140
|
|
|
$resolver->setDefaults([ |
141
|
|
|
'timeout' => 10, |
142
|
|
|
'production' => false, |
143
|
|
|
'paybox_default_currency' => Currency::EURO, |
144
|
|
|
]); |
145
|
|
|
$resolver->setRequired([ |
146
|
|
|
'paybox_version', // Paybox Direct Plus protocol |
147
|
|
|
'paybox_site', |
148
|
|
|
'paybox_rank', |
149
|
|
|
'paybox_identifier', |
150
|
|
|
'paybox_key', |
151
|
|
|
]); |
152
|
|
|
|
153
|
|
|
$resolver->setAllowedTypes('timeout', 'int'); |
154
|
|
|
$resolver->setAllowedTypes('production', 'bool'); |
155
|
|
|
$resolver->setAllowedTypes('paybox_version', 'string'); |
156
|
|
|
$resolver->setAllowedTypes('paybox_default_currency', 'int'); |
157
|
|
|
$resolver->setAllowedTypes('paybox_site', 'string'); |
158
|
|
|
$resolver->setAllowedTypes('paybox_rank', 'string'); |
159
|
|
|
$resolver->setAllowedTypes('paybox_identifier', 'string'); |
160
|
|
|
$resolver->setAllowedTypes('paybox_key', 'string'); |
161
|
|
|
|
162
|
|
|
$resolver->setAllowedValues('paybox_version', Version::getConstants()); |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|