1 | <?php |
||
2 | |||
3 | /* |
||
4 | * This file is part of the GestPayWS library. |
||
5 | * |
||
6 | * (c) Manuel Dalla Lana <[email protected]> |
||
7 | * |
||
8 | * This source file is subject to the MIT license that is bundled |
||
9 | * with this source code in the file LICENSE. |
||
10 | */ |
||
11 | |||
12 | namespace EndelWar\GestPayWS\Parameter; |
||
13 | |||
14 | use InvalidArgumentException; |
||
15 | |||
16 | /** |
||
17 | * Class EncryptParameter |
||
18 | * |
||
19 | * @property string $shopLogin |
||
20 | * @property int $uicCode; |
||
21 | * @property float $amount; |
||
22 | * @property string $shopTransactionId; |
||
23 | * @property string $apikey; |
||
24 | * @property string $buyerName; |
||
25 | * @property string $buyerEmail; |
||
26 | * @property int $languageId; |
||
27 | * @property string $customInfo; |
||
28 | * @property string $requestToken; |
||
29 | * @property string $ppSellerProtection; |
||
30 | * @property string $shippingDetails; |
||
31 | * @property string $paymentTypes; |
||
32 | * @property string $paymentTypeDetail; |
||
33 | */ |
||
34 | class EncryptParameter extends Parameter |
||
35 | { |
||
36 | protected $parametersName = [ |
||
37 | // Mandatory parameters |
||
38 | 'shopLogin', |
||
39 | 'uicCode', |
||
40 | 'amount', |
||
41 | 'shopTransactionId', |
||
42 | // Optional parameters |
||
43 | 'apikey', |
||
44 | 'buyerName', |
||
45 | 'buyerEmail', |
||
46 | 'languageId', |
||
47 | 'customInfo', |
||
48 | 'requestToken', |
||
49 | //'cardNumber', //deprecated |
||
50 | //'expiryMonth', //deprecated |
||
51 | //'expiryYear', //deprecated |
||
52 | //'cvv', //deprecated |
||
53 | |||
54 | /* to be implemented |
||
55 | 'OrderDetails', |
||
56 | 'ppSellerProtection', |
||
57 | 'shippingDetails', |
||
58 | 'paymentTypes', |
||
59 | 'paymentTypeDetail', |
||
60 | 'redFraudPrevention', |
||
61 | 'Red_CustomerInfo', |
||
62 | 'Red_ShippingInfo', |
||
63 | 'Red_BillingInfo', |
||
64 | 'Red_CustomerData', |
||
65 | 'Red_CustomInfo', |
||
66 | 'Red_Items', |
||
67 | 'Consel_MerchantPro', |
||
68 | 'Consel_CustomerInfo', |
||
69 | 'payPalBillingAgreementDescription' |
||
70 | */ |
||
71 | ]; |
||
72 | protected $mandatoryParameters = [ |
||
73 | 'shopLogin', |
||
74 | 'uicCode', |
||
75 | 'amount', |
||
76 | 'shopTransactionId', |
||
77 | ]; |
||
78 | protected $separator = '*P1*'; |
||
79 | private $customInfoArray = []; |
||
80 | |||
81 | /** @see https://api.gestpay.it/#encrypt */ |
||
82 | private $invalidChars = [ |
||
83 | '&', |
||
84 | ' ', |
||
85 | '§', //need also to be added programmatically, because UTF-8 |
||
86 | '(', |
||
87 | ')', |
||
88 | '*', |
||
89 | '<', |
||
90 | '>', |
||
91 | ',', |
||
92 | ';', |
||
93 | ':', |
||
94 | '*P1*', |
||
95 | '/', |
||
96 | '[', |
||
97 | ']', |
||
98 | '?', |
||
99 | '=', |
||
100 | '--', |
||
101 | '/*', |
||
102 | '%', |
||
103 | '//', |
||
104 | '~', |
||
105 | ]; |
||
106 | private $invalidCharsFlattened = ''; |
||
107 | |||
108 | /** |
||
109 | * @param array $parameters |
||
110 | */ |
||
111 | public function __construct(array $parameters = []) |
||
112 | { |
||
113 | $this->invalidChars[] = chr(167); //§ ascii char |
||
114 | |||
115 | parent::__construct($parameters); |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * @param string $key |
||
120 | * @param mixed $value |
||
121 | */ |
||
122 | public function set($key, $value) |
||
123 | { |
||
124 | if (!in_array($key, $this->parametersName, true)) { |
||
125 | throw new InvalidArgumentException(sprintf('%s is not a valid parameter name.', $key)); |
||
126 | } |
||
127 | $this->verifyParameterValidity($value); |
||
128 | parent::set($key, $value); |
||
129 | } |
||
130 | |||
131 | /** |
||
132 | * @param mixed $customInfo string already encoded or array of key/value to be encoded |
||
133 | */ |
||
134 | public function setCustomInfo($customInfo) |
||
135 | { |
||
136 | if (!is_array($customInfo)) { |
||
137 | $this->data['customInfo'] = $customInfo; |
||
138 | } else { |
||
139 | //check string validity |
||
140 | |||
141 | foreach ($customInfo as $key => $value) { |
||
142 | $value = urlencode($value); |
||
143 | $this->verifyParameterValidity($key); |
||
144 | $this->verifyParameterValidity($value); |
||
145 | |||
146 | if (strlen($value) > 300) { |
||
147 | $value = substr($value, 0, 300); |
||
148 | } |
||
149 | $customInfo[$key] = $value; |
||
150 | } |
||
151 | $this->customInfoArray = $customInfo; |
||
152 | $this->data['customInfo'] = http_build_query($this->customInfoArray, '', $this->separator); |
||
153 | } |
||
154 | } |
||
155 | |||
156 | /** |
||
157 | * @return array |
||
158 | */ |
||
159 | public function getCustomInfoToArray() |
||
160 | { |
||
161 | $allinfo = explode($this->separator, $this->customInfo); |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
162 | $customInfoArray = []; |
||
163 | foreach ($allinfo as $singleInfo) { |
||
164 | $tagvalue = explode('=', $singleInfo); |
||
165 | $customInfoArray[$tagvalue[0]] = urldecode($tagvalue[1]); |
||
166 | } |
||
167 | |||
168 | return $customInfoArray; |
||
169 | } |
||
170 | |||
171 | /** |
||
172 | * @param $value |
||
173 | * @return bool |
||
174 | */ |
||
175 | public function verifyParameterValidity($value) |
||
176 | { |
||
177 | if ('' === $this->invalidCharsFlattened) { |
||
178 | $invalidCharsQuoted = array_map('preg_quote', $this->invalidChars); |
||
179 | $this->invalidCharsFlattened = implode('|', $invalidCharsQuoted); |
||
180 | } |
||
181 | |||
182 | if (preg_match_all('#' . $this->invalidCharsFlattened . '#', $value, $matches)) { |
||
183 | $invalidCharsMatched = '"' . implode('", "', $matches[0]) . '"'; |
||
184 | throw new InvalidArgumentException( |
||
185 | 'String ' . $value . ' contains invalid chars (i.e.: ' . $invalidCharsMatched . ').' |
||
186 | ); |
||
187 | } |
||
188 | |||
189 | return true; |
||
190 | } |
||
191 | } |
||
192 |