PluginProbe
imoje / 4.2.0
imoje v4.2.0
4.16.1 4.16.0 trunk 3.1.1 3.2.0 3.2.1 3.2.3 3.2.4 3.2.6 3.2.7 3.2.8 3.3.0 3.3.1 3.3.2 3.3.3 4.0.0 4.1.0 4.1.1 4.10.1 4.11.0 4.12.0 4.14.0 4.15.0 4.15.1 4.15.2 All 39 releases
imoje / includes / libs / payment-core / src / Util.php

Util.php in imoje 4.2.0, at includes/libs/payment-core/src/Util.php

421 lines 8.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Imoje\Payment;
4
5 /**
6 * Class Util
7 *
8 * @package Imoje\Payment
9 */
10 class Util {
11
12 /**
13 * @const string
14 */
15 const METHOD_REQUEST_POST = 'POST';
16
17 /**
18 * @const string
19 */
20 const METHOD_REQUEST_GET = 'GET';
21
22 /**
23 * @const string
24 */
25 const ENVIRONMENT_PRODUCTION = 'production';
26
27 /**
28 * @const string
29 */
30 const ENVIRONMENT_SANDBOX = 'sandbox';
31
32 /**
33 * @const string
34 */
35 const REGULATION = 'regulation';
36
37 /**
38 * @const string
39 */
40 const IODO = 'iodo';
41
42 /**
43 * @const string
44 */
45 const LANG_PL = 'pl';
46
47 /**
48 * @const string
49 */
50 const LANG_EN = 'en';
51
52 /**
53 * @var string
54 */
55 private static $cdnUrl = 'https://data.imoje.pl';
56
57 /**
58 * @var array[]
59 */
60 private static $docUrls = [
61 self::LANG_PL => [
62 self::REGULATION => 'https://data.imoje.pl/docs/imoje_regulamin_platnosci.pdf',
63 self::IODO => 'https://data.imoje.pl/docs/imoje_informacja_administratora_danych_osobowych.pdf',
64 ],
65 self::LANG_EN => [
66 self::REGULATION => 'https://data.imoje.pl/docs/en/imoje_general_terms_and_conditions.pdf',
67 self::IODO => 'https://data.imoje.pl/docs/en/imoje_information_of_the_personal_data_controller.pdf',
68 ],
69 ];
70
71 /**
72 * @var array
73 */
74 private static $supportedCurrencies = [
75 'pln' => 'PLN',
76 'eur' => 'EUR',
77 'czk' => 'CZK',
78 'gbp' => 'GBP',
79 'usd' => 'USD',
80 'uah' => 'UAH',
81 'hrk' => 'HRK',
82 'huf' => 'HUF',
83 'sek' => 'SEK',
84 'ron' => 'RON',
85 'chf' => 'CHF',
86 'bgn' => 'BGN',
87 ];
88
89 /**
90 * @var array
91 */
92 private static $paymentMethods = [
93 'blik' => 'blik',
94 'imoje_paylater' => 'imoje_paylater',
95 'pbl' => 'pbl',
96 'card' => 'card',
97 'ing' => 'ing',
98 ];
99
100 /**
101 * @var array
102 */
103 private static $paymentMethodCodeList = [
104 'blik' => 'blik',
105 'alior' => 'alior',
106 'bnpparibas' => 'bnpparibas',
107 'bos' => 'bos',
108 'bs' => 'bs',
109 'bspb' => 'bspb',
110 'bzwbk' => 'bzwbk',
111 'citi' => 'citi',
112 'creditagricole' => 'creditagricole',
113 'envelo' => 'envelo',
114 'getin' => 'getin',
115 'ideabank' => 'ideabank',
116 'ing' => 'ing',
117 'inteligo' => 'inteligo',
118 'ipko' => 'ipko',
119 'millennium' => 'millennium',
120 'mtransfer' => 'mtransfer',
121 'nest' => 'nest',
122 'noble' => 'noble',
123 'pbs' => 'pbs',
124 'pekao24' => 'pekao24',
125 'plusbank' => 'plusbank',
126 'pocztowy' => 'pocztowy',
127 'tmobile' => 'tmobile',
128 'imoje_twisto' => 'imoje_twisto',
129 'blik_oneclick' => 'blik_oneclick',
130 'paypo' => 'paypo',
131 'pragma_go' => 'pragma_go',
132 ];
133
134 /**
135 * @var array
136 */
137 private static $paymentMethodCodeLogoExt = [
138 'mtransfer' => 'mtransfer.png',
139 'bzwbk' => 'bzwbk.png',
140 'pekao24' => 'pekao24.svg',
141 'inteligo' => 'inteligo.png',
142 'ipko' => 'ipko.png',
143 'getin' => 'getin.svg',
144 'noble' => 'noble.png',
145 'creditagricole' => 'creditagricole.svg',
146 'alior' => 'alior.svg',
147 'pbs' => 'pbs.png',
148 'millennium' => 'millennium.svg',
149 'citi' => 'citi.png',
150 'bos' => 'bos.png',
151 'bnpparibas' => 'bnpparibas.png',
152 'pocztowy' => 'pocztowy.svg',
153 'plusbank' => 'plusbank.png',
154 'bs' => 'bs.png',
155 'bspb' => 'bspb.png',
156 'nest' => 'nest.svg',
157 'ing' => 'ing.png',
158 'paypo' => 'paypo.svg',
159 'pragma_go' => 'pragma_go.svg',
160 ];
161
162 /**
163 * @var array
164 */
165 private static $hashMethods = [
166 'sha224' => 'sha224',
167 'sha256' => 'sha256',
168 'sha384' => 'sha384',
169 'sha512' => 'sha512',
170 ];
171
172 /**
173 * @var array
174 */
175 private static $transactionStatuses = [
176 'new' => 'new',
177 'authorized' => 'authorized',
178 'pending' => 'pending',
179 'submitted_for_settlement'
180 => 'submitted_for_settlement',
181 'rejected' => 'rejected',
182 'settled' => 'settled',
183 'error' => 'error',
184 'cancelled' => 'cancelled',
185 ];
186
187 /**
188 * Functions that return true when passed currency is on supported currencies list.
189 *
190 * @param string $currencyCode ISO4217
191 *
192 * @return bool
193 */
194 public static function canUseForCurrency( $currencyCode ) {
195 return isset( self::$supportedCurrencies[ strtolower( $currencyCode ) ] );
196 }
197
198 /**
199 * @param string $hashMethod
200 *
201 * @return string
202 */
203 public static function getHashMethod( $hashMethod ) {
204
205 if ( isset( self::$hashMethods[ $hashMethod ] ) ) {
206 return self::$hashMethods[ $hashMethod ];
207 }
208
209 return '';
210 }
211
212 /**
213 * @param string $language
214 * @param string $name
215 *
216 * @return string
217 */
218 public static function getDocUrl( $language, $name ) {
219
220 if ( isset( self::$docUrls[ $language ][ $name ] ) ) {
221 return self::$docUrls[ $language ][ $name ];
222 }
223
224 return '';
225 }
226
227 /**
228 * @param array $order
229 * @param string $url
230 * @param string $method
231 * @param string $submitValue
232 * @param string $submitClass
233 * @param string $submitStyle
234 *
235 * @return string
236 */
237 public static function createOrderForm(
238 $order,
239 $url = '',
240 $method = '',
241 $submitValue = '',
242 $submitClass = '',
243 $submitStyle = ''
244 ) {
245 if ( ! $submitValue ) {
246 $submitValue = 'Kontynuuj';
247 }
248
249 if ( ! $url ) {
250 $url = '';
251 }
252
253 if ( ! $method ) {
254 $method = 'POST';
255 }
256
257 $form = '<form method="' . $method . '" action="' . $url . '">';
258
259 if ( is_array( $order ) ) {
260 foreach ( $order as $key => $value ) {
261 $form .= '<input type="hidden" value="' . htmlentities( $value ) . '" name="' . $key . '" id="imoje_' . $key . '">';
262 }
263 }
264
265 $form .= '<button' . ( $submitClass
266 ? ' class="' . $submitClass . '"'
267 : '' ) . ( $submitStyle
268 ? ' style="' . $submitStyle . '"'
269 : '' ) . ' type="submit" id="submit-payment-form">' . $submitValue . '</button>';
270 $form .= '</form>';
271
272 return $form;
273 }
274
275 /**
276 * @return array
277 */
278 public static function getSupportedCurrencies() {
279 return self::$supportedCurrencies;
280 }
281
282 /**
283 * @param string $paymentMethod
284 *
285 * @return string
286 */
287 public static function getPaymentMethod( $paymentMethod ) {
288
289 if ( isset( self::$paymentMethods[ $paymentMethod ] ) ) {
290 return self::$paymentMethods[ $paymentMethod ];
291 }
292
293 return '';
294 }
295
296 /**
297 * @param string $paymentMethodCode
298 *
299 * @return string
300 */
301 public static function getPaymentMethodCodeLogo( $paymentMethodCode ) {
302
303 if ( isset( self::$paymentMethodCodeLogoExt[ $paymentMethodCode ] ) ) {
304 return self::$cdnUrl . '/img/pay/' . self::$paymentMethodCodeLogoExt[ $paymentMethodCode ];
305 }
306
307 return '';
308 }
309
310 /**
311 * @param string $paymentMethodCode
312 *
313 * @return string
314 */
315 public static function getPaymentMethodCode( $paymentMethodCode ) {
316 if ( isset( self::$paymentMethodCodeList[ $paymentMethodCode ] ) ) {
317 return self::$paymentMethodCodeList[ $paymentMethodCode ];
318 }
319
320 return '';
321 }
322
323 /**
324 * @param string $variable
325 *
326 * @return bool
327 */
328 public static function isJson( $variable ) {
329 json_decode( $variable );
330
331 return ( json_last_error() === JSON_ERROR_NONE );
332 }
333
334 /**
335 * @return array
336 */
337 public static function getTransactionStatuses() {
338 return self::$transactionStatuses;
339 }
340
341 /**
342 * @param string|array $json
343 */
344 public static function doResponseJson( $json ) {
345
346 header( 'Content-Type: application/json' );
347
348 echo $json;
349 die();
350 }
351
352 /**
353 * @param string|int $data
354 *
355 * @return string
356 */
357 public static function getCid( $data ) {
358 return hash( 'md5', $data );
359 }
360
361 /**
362 * @param float $amount
363 *
364 * @return int
365 */
366 public static function convertAmountToFractional( $amount ) {
367 return (int) self::multiplyValues( round( $amount, 2 ), 100, 0 );
368 }
369
370 /**
371 * @param number $firstValue
372 * @param number $secondValue
373 * @param number $precision
374 *
375 * @return float
376 */
377 public static function multiplyValues( $firstValue, $secondValue, $precision ) {
378 return round( $firstValue * $secondValue, $precision );
379 }
380
381 /**
382 * @param int $amount
383 *
384 * @return float
385 */
386 public static function convertAmountToMain( $amount ) {
387 return round( $amount / 100, 2 );
388 }
389
390 /**
391 * @param string $hashMethod
392 * @param string $data
393 * @param string $serviceKey
394 *
395 * @return string
396 */
397 public static function hashSignature( $hashMethod, $data, $serviceKey ) {
398 return hash( $hashMethod, $data . $serviceKey );
399 }
400
401 /**
402 * @param array $transaction
403 *
404 * @return string|void
405 */
406 public static function calculateAmountToRefund( $transaction ) {
407 $refundAmount = 0;
408
409 if ( isset( $transaction['body']['transaction']['refunds'] ) && $transaction['body']['transaction']['refunds'] ) {
410
411 foreach ( $transaction['body']['transaction']['refunds'] as $refund ) {
412 if ( $refund['transaction']['status'] === 'settled' ) {
413 $refundAmount += $refund['transaction']['amount'];
414 }
415 }
416 }
417
418 return $transaction['body']['transaction']['amount'] - $refundAmount;
419 }
420 }
421