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