1 | <?php |
||
2 | /** |
||
3 | * Gateway |
||
4 | * |
||
5 | * @author Pronamic <[email protected]> |
||
6 | * @copyright 2005-2021 Pronamic |
||
7 | * @license GPL-3.0-or-later |
||
8 | * @package Pronamic\WordPress\Pay\Payments |
||
9 | */ |
||
10 | |||
11 | namespace Pronamic\WordPress\Pay\Gateways\Sisow; |
||
12 | |||
13 | use Pronamic\WordPress\Pay\Core\Gateway as Core_Gateway; |
||
14 | use Pronamic\WordPress\Pay\Core\PaymentMethods; |
||
15 | use Pronamic\WordPress\Pay\Core\Util as Core_Util; |
||
16 | use Pronamic\WordPress\Pay\Banks\BankAccountDetails; |
||
17 | use Pronamic\WordPress\Pay\Payments\PaymentStatus as Core_Statuses; |
||
18 | use Pronamic\WordPress\Pay\Payments\Payment; |
||
19 | use Pronamic\WordPress\Pay\Payments\PaymentLineType; |
||
20 | |||
21 | /** |
||
22 | * Title: Sisow gateway |
||
23 | * Description: |
||
24 | * Copyright: 2005-2021 Pronamic |
||
25 | * Company: Pronamic |
||
26 | * |
||
27 | * @author Remco Tolsma |
||
28 | * @version 2.0.4 |
||
29 | * @since 1.0.0 |
||
30 | */ |
||
31 | class Gateway extends Core_Gateway { |
||
32 | /** |
||
33 | * Client. |
||
34 | * |
||
35 | * @var Client |
||
36 | */ |
||
37 | protected $client; |
||
38 | |||
39 | /** |
||
40 | * Constructs and initialize an Sisow gateway |
||
41 | * |
||
42 | * @param Config $config Config. |
||
43 | */ |
||
44 | public function __construct( Config $config ) { |
||
45 | parent::__construct( $config ); |
||
46 | |||
47 | $this->set_method( self::METHOD_HTTP_REDIRECT ); |
||
48 | |||
49 | // Supported features. |
||
50 | $this->supports = array( |
||
51 | 'payment_status_request', |
||
52 | 'reservation_payments', |
||
53 | ); |
||
54 | |||
55 | // Client. |
||
56 | $this->client = new Client( $config->merchant_id, $config->merchant_key ); |
||
57 | $this->client->set_test_mode( self::MODE_TEST === $config->mode ); |
||
58 | } |
||
59 | |||
60 | /** |
||
61 | * Get issuers |
||
62 | * |
||
63 | * @see Core_Gateway::get_issuers() |
||
64 | * @return array<int, array<string, array<int|string, string>>> |
||
65 | */ |
||
66 | public function get_issuers() { |
||
67 | $groups = array(); |
||
68 | |||
69 | $result = $this->client->get_directory(); |
||
70 | |||
71 | if ( false !== $result ) { |
||
72 | $groups[] = array( |
||
73 | 'options' => $result, |
||
74 | ); |
||
75 | } |
||
76 | |||
77 | return $groups; |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * Get available payment methods. |
||
82 | * |
||
83 | * @see Core_Gateway::get_available_payment_methods() |
||
84 | * @return array<int,string>|null |
||
85 | */ |
||
86 | public function get_available_payment_methods() { |
||
87 | if ( self::MODE_TEST === $this->config->mode ) { |
||
88 | return null; |
||
89 | } |
||
90 | |||
91 | $payment_methods = array(); |
||
92 | |||
93 | // Merchant request. |
||
94 | $request = new MerchantRequest( $this->config->merchant_id ); |
||
95 | |||
96 | // Get merchant. |
||
97 | try { |
||
98 | $result = $this->client->get_merchant( $request ); |
||
99 | } catch ( \Exception $e ) { |
||
100 | $this->error = new \WP_Error( 'sisow_error', $e->getMessage() ); |
||
101 | |||
102 | return $payment_methods; |
||
103 | } |
||
104 | |||
105 | if ( false !== $result ) { |
||
106 | foreach ( $result->payments as $method ) { |
||
107 | // Transform to WordPress payment methods. |
||
108 | $payment_method = Methods::transform_gateway_method( $method ); |
||
109 | |||
110 | if ( $payment_method ) { |
||
111 | $payment_methods[] = $payment_method; |
||
112 | } |
||
113 | } |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * Add active payment methods which are not returned by Sisow in merchant response. |
||
118 | * |
||
119 | * @link https://github.com/wp-pay-gateways/sisow/issues/1 |
||
120 | */ |
||
121 | if ( false !== \array_search( PaymentMethods::IDEAL, $payment_methods, true ) ) { |
||
122 | $payment_methods[] = PaymentMethods::BANCONTACT; |
||
123 | $payment_methods[] = PaymentMethods::BANK_TRANSFER; |
||
124 | $payment_methods[] = PaymentMethods::BELFIUS; |
||
125 | $payment_methods[] = PaymentMethods::BUNQ; |
||
126 | $payment_methods[] = PaymentMethods::EPS; |
||
127 | $payment_methods[] = PaymentMethods::GIROPAY; |
||
128 | $payment_methods[] = PaymentMethods::KBC; |
||
129 | $payment_methods[] = PaymentMethods::SOFORT; |
||
130 | |||
131 | $payment_methods = \array_unique( $payment_methods ); |
||
132 | |||
133 | // Renumber keys. |
||
134 | $payment_methods = \array_values( $payment_methods ); |
||
135 | } |
||
136 | |||
137 | return $payment_methods; |
||
138 | } |
||
139 | |||
140 | /** |
||
141 | * Get supported payment methods |
||
142 | * |
||
143 | * @see Core_Gateway::get_supported_payment_methods() |
||
144 | * @return array<int,string> |
||
145 | */ |
||
146 | public function get_supported_payment_methods() { |
||
147 | return array( |
||
148 | PaymentMethods::AFTERPAY, |
||
149 | PaymentMethods::BANK_TRANSFER, |
||
150 | PaymentMethods::BANCONTACT, |
||
151 | PaymentMethods::BELFIUS, |
||
152 | PaymentMethods::BILLINK, |
||
153 | PaymentMethods::BUNQ, |
||
154 | PaymentMethods::CAPAYABLE, |
||
155 | PaymentMethods::IN3, |
||
156 | PaymentMethods::CREDIT_CARD, |
||
157 | PaymentMethods::FOCUM, |
||
158 | PaymentMethods::GIROPAY, |
||
159 | PaymentMethods::IDEAL, |
||
160 | PaymentMethods::IDEALQR, |
||
161 | PaymentMethods::KLARNA_PAY_LATER, |
||
162 | PaymentMethods::PAYPAL, |
||
163 | PaymentMethods::SOFORT, |
||
164 | ); |
||
165 | } |
||
166 | |||
167 | /** |
||
168 | * Is payment method required to start transaction? |
||
169 | * |
||
170 | * @see Core_Gateway::payment_method_is_required() |
||
171 | */ |
||
172 | public function payment_method_is_required() { |
||
173 | return true; |
||
174 | } |
||
175 | |||
176 | /** |
||
177 | * Format amount. |
||
178 | * |
||
179 | * @param Money $amount Money. |
||
180 | * @return string |
||
181 | */ |
||
182 | private function format_amount( Money $amount ) { |
||
183 | return $amount->get_minor_units()->format( 0, '', '' ); |
||
184 | } |
||
185 | |||
186 | /** |
||
187 | * Start |
||
188 | * |
||
189 | * @param Payment $payment Payment. |
||
190 | * |
||
191 | * @throws \Exception Throws exception on transaction error. |
||
192 | * @see Core_Gateway::start() |
||
193 | */ |
||
194 | public function start( Payment $payment ) { |
||
195 | // Order and purchase ID. |
||
196 | $order_id = $payment->get_order_id(); |
||
197 | $purchase_id = strval( empty( $order_id ) ? (string) $payment->get_id() : $order_id ); |
||
198 | |||
199 | // Maximum length for purchase ID is 16 characters, otherwise an error will occur: |
||
200 | // ideal_sisow_error - purchaseid too long (16). |
||
201 | $purchase_id = substr( $purchase_id, 0, 16 ); |
||
202 | |||
203 | // New transaction request. |
||
204 | $request = new TransactionRequest( |
||
205 | $this->config->merchant_id, |
||
206 | $this->config->shop_id |
||
207 | ); |
||
208 | |||
209 | $request->merge_parameters( |
||
210 | array( |
||
211 | 'payment' => Methods::transform( $payment->get_method(), $payment->get_method() ), |
||
212 | 'purchaseid' => substr( $purchase_id, 0, 16 ), |
||
213 | 'entrancecode' => $payment->get_entrance_code(), |
||
214 | 'amount' => $this->format_amount( $payment->get_total_amount() ), |
||
215 | 'description' => substr( (string) $payment->get_description(), 0, 32 ), |
||
216 | 'testmode' => ( self::MODE_TEST === $this->config->mode ) ? 'true' : 'false', |
||
217 | 'returnurl' => $payment->get_return_url(), |
||
218 | 'cancelurl' => $payment->get_return_url(), |
||
219 | 'notifyurl' => $payment->get_return_url(), |
||
220 | 'callbackurl' => $payment->get_return_url(), |
||
221 | // Other parameters. |
||
222 | 'issuerid' => $payment->get_issuer(), |
||
223 | 'billing_mail' => $payment->get_email(), |
||
224 | ) |
||
225 | ); |
||
226 | |||
227 | // Payment method. |
||
228 | $this->set_payment_method( null === $payment->get_method() ? PaymentMethods::IDEAL : $payment->get_method() ); |
||
229 | |||
230 | // Additional parameters for payment method. |
||
231 | if ( PaymentMethods::IDEALQR === $payment->get_method() ) { |
||
232 | $request->set_parameter( 'qrcode', 'true' ); |
||
233 | } |
||
234 | |||
235 | // Customer. |
||
236 | $customer = $payment->get_customer(); |
||
237 | |||
238 | if ( null !== $customer ) { |
||
239 | $request->merge_parameters( |
||
240 | array( |
||
241 | 'ipaddress' => $customer->get_ip_address(), |
||
242 | 'gender' => $customer->get_gender(), |
||
243 | ) |
||
244 | ); |
||
245 | |||
246 | $locale = $customer->get_locale(); |
||
247 | |||
248 | if ( null !== $locale ) { |
||
249 | /* |
||
250 | * @link https://github.com/wp-pay-gateways/sisow/tree/feature/post-pay/documentation#parameter-locale |
||
251 | */ |
||
252 | $sisow_locale = strtoupper( substr( $locale, -2 ) ); |
||
253 | |||
254 | $request->set_parameter( 'locale', $sisow_locale ); |
||
255 | } |
||
256 | |||
257 | $birth_date = $customer->get_birth_date(); |
||
258 | |||
259 | if ( null !== $birth_date ) { |
||
260 | $request->set_parameter( 'birthdate', $birth_date->format( 'dmY' ) ); |
||
261 | } |
||
262 | } |
||
263 | |||
264 | // Billing address. |
||
265 | $address = $payment->get_billing_address(); |
||
266 | |||
267 | if ( null !== $address ) { |
||
268 | $name = $address->get_name(); |
||
269 | |||
270 | if ( null !== $name ) { |
||
271 | $request->merge_parameters( |
||
272 | array( |
||
273 | 'billing_firstname' => $name->get_first_name(), |
||
274 | 'billing_lastname' => $name->get_last_name(), |
||
275 | ) |
||
276 | ); |
||
277 | |||
278 | // Remove accents from first name for AfterPay. |
||
279 | if ( PaymentMethods::AFTERPAY === $payment->get_method() ) { |
||
280 | $request->set_parameter( 'billing_firstname', remove_accents( (string) $name->get_first_name() ) ); |
||
281 | } |
||
282 | } |
||
283 | |||
284 | $request->merge_parameters( |
||
285 | array( |
||
286 | 'billing_mail' => $address->get_email(), |
||
287 | 'billing_company' => $address->get_company_name(), |
||
288 | 'billing_coc' => $address->get_coc_number(), |
||
289 | 'billing_address1' => $address->get_line_1(), |
||
290 | 'billing_address2' => $address->get_line_2(), |
||
291 | 'billing_zip' => $address->get_postal_code(), |
||
292 | 'billing_city' => $address->get_city(), |
||
293 | 'billing_country' => $address->get_country_name(), |
||
294 | 'billing_countrycode' => $address->get_country_code(), |
||
295 | 'billing_phone' => $address->get_phone(), |
||
296 | ) |
||
297 | ); |
||
298 | } |
||
299 | |||
300 | // Shipping address. |
||
301 | $address = $payment->get_shipping_address(); |
||
302 | |||
303 | if ( null !== $address ) { |
||
304 | $name = $address->get_name(); |
||
305 | |||
306 | if ( null !== $name ) { |
||
307 | $request->merge_parameters( |
||
308 | array( |
||
309 | 'shipping_firstname' => $name->get_first_name(), |
||
310 | 'shipping_lastname' => $name->get_last_name(), |
||
311 | ) |
||
312 | ); |
||
313 | } |
||
314 | |||
315 | $request->merge_parameters( |
||
316 | array( |
||
317 | 'shipping_mail' => $address->get_email(), |
||
318 | 'shipping_company' => $address->get_company_name(), |
||
319 | 'shipping_address1' => $address->get_line_1(), |
||
320 | 'shipping_address2' => $address->get_line_2(), |
||
321 | 'shipping_zip' => $address->get_postal_code(), |
||
322 | 'shipping_city' => $address->get_city(), |
||
323 | 'shipping_country' => $address->get_country_name(), |
||
324 | 'shipping_countrycode' => $address->get_country_code(), |
||
325 | 'shipping_phone' => $address->get_phone(), |
||
326 | ) |
||
327 | ); |
||
328 | } |
||
329 | |||
330 | // Lines. |
||
331 | $lines = $payment->get_lines(); |
||
332 | |||
333 | if ( null !== $lines ) { |
||
334 | $x = 1; |
||
335 | |||
336 | foreach ( $lines as $line ) { |
||
337 | // Product ID. |
||
338 | $product_id = $line->get_id(); |
||
339 | |||
340 | switch ( $line->get_type() ) { |
||
341 | case PaymentLineType::SHIPPING: |
||
342 | $product_id = 'shipping'; |
||
343 | |||
344 | break; |
||
345 | case PaymentLineType::FEE: |
||
346 | $product_id = 'paymentfee'; |
||
347 | |||
348 | break; |
||
349 | } |
||
350 | |||
351 | // Price. |
||
352 | $net_price = null; |
||
353 | |||
354 | $unit_price = $line->get_unit_price(); |
||
355 | |||
356 | if ( null !== $unit_price ) { |
||
357 | $net_price = $this->format_amount( $unit_price->get_excluding_tax() ); |
||
358 | } |
||
359 | |||
360 | // Request parameters. |
||
361 | $request->merge_parameters( |
||
362 | array( |
||
363 | 'product_id_' . $x => $product_id, |
||
364 | 'product_description_' . $x => $line->get_name(), |
||
365 | 'product_quantity_' . $x => $line->get_quantity(), |
||
366 | 'product_netprice_' . $x => $net_price, |
||
367 | 'product_total_' . $x => $this->format_amount( $line->get_total_amount()->get_including_tax() ), |
||
368 | 'product_nettotal_' . $x => $this->format_amount( $line->get_total_amount()->get_excluding_tax() ), |
||
369 | ) |
||
370 | ); |
||
371 | |||
372 | // Tax request parameters. |
||
373 | $tax_amount = $line->get_tax_amount(); |
||
374 | |||
375 | if ( null !== $tax_amount ) { |
||
376 | $request->set_parameter( 'product_tax_' . $x, $this->format_amount( $tax_amount ); |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
377 | } |
||
378 | |||
379 | $tax_percentage = $line->get_total_amount()->get_tax_percentage(); |
||
380 | |||
381 | if ( null !== $tax_percentage ) { |
||
382 | $request->set_parameter( 'product_taxrate_' . $x, strval( $tax_percentage * 100 ) ); |
||
383 | } |
||
384 | |||
385 | $x++; |
||
386 | } |
||
387 | } |
||
388 | |||
389 | // Create transaction. |
||
390 | $result = $this->client->create_transaction( $request ); |
||
391 | |||
392 | if ( false !== $result ) { |
||
393 | $payment->set_transaction_id( $result->id ); |
||
394 | $payment->set_action_url( $result->issuer_url ); |
||
395 | } |
||
396 | } |
||
397 | |||
398 | /** |
||
399 | * Update status of the specified payment |
||
400 | * |
||
401 | * @param Payment $payment Payment. |
||
402 | */ |
||
403 | public function update_status( Payment $payment ) { |
||
404 | $transaction_id = (string) $payment->get_transaction_id(); |
||
405 | $merchant_id = $this->config->merchant_id; |
||
406 | |||
407 | // Process notify and callback requests for payments without transaction ID. |
||
408 | if ( empty( $transaction_id ) && Core_Util::input_has_vars( \INPUT_GET, array( 'trxid', 'ec', 'status', 'sha1' ) ) ) { |
||
409 | $transaction_id = \filter_input( \INPUT_GET, 'trxid' ); |
||
410 | $entrance_code = \filter_input( \INPUT_GET, 'ec' ); |
||
411 | $status = \filter_input( \INPUT_GET, 'status' ); |
||
412 | $signature = \filter_input( \INPUT_GET, 'sha1' ); |
||
413 | |||
414 | $notify = new NotifyRequest( $transaction_id, $entrance_code, $status, $merchant_id ); |
||
415 | |||
416 | // Set status if signature validates. |
||
417 | if ( $notify->get_signature( $this->config->merchant_key ) === $signature ) { |
||
418 | $payment->set_status( Statuses::transform( $status ) ); |
||
419 | } |
||
420 | |||
421 | return; |
||
422 | } |
||
423 | |||
424 | // Status request. |
||
425 | $request = new StatusRequest( |
||
426 | $transaction_id, |
||
427 | $merchant_id, |
||
428 | $this->config->shop_id |
||
429 | ); |
||
430 | |||
431 | try { |
||
432 | $result = $this->client->get_status( $request ); |
||
433 | |||
434 | if ( false === $result ) { |
||
435 | return; |
||
436 | } |
||
437 | } catch ( \Exception $e ) { |
||
438 | $this->error = new \WP_Error( 'sisow_error', $e->getMessage() ); |
||
439 | |||
440 | return; |
||
441 | } |
||
442 | |||
443 | // Set status. |
||
444 | $payment->set_status( Statuses::transform( $result->status ) ); |
||
445 | |||
446 | // Set consumer details. |
||
447 | $consumer_details = $payment->get_consumer_bank_details(); |
||
448 | |||
449 | if ( null === $consumer_details ) { |
||
450 | $consumer_details = new BankAccountDetails(); |
||
451 | |||
452 | $payment->set_consumer_bank_details( $consumer_details ); |
||
453 | } |
||
454 | |||
455 | $consumer_details->set_name( $result->consumer_name ); |
||
456 | $consumer_details->set_account_number( $result->consumer_account ); |
||
457 | $consumer_details->set_city( $result->consumer_city ); |
||
458 | $consumer_details->set_iban( $result->consumer_iban ); |
||
459 | $consumer_details->set_bic( $result->consumer_bic ); |
||
460 | } |
||
461 | |||
462 | /** |
||
463 | * Create invoice. |
||
464 | * |
||
465 | * @param Payment $payment Payment. |
||
466 | * |
||
467 | * @return bool |
||
468 | */ |
||
469 | public function create_invoice( $payment ) { |
||
470 | $transaction_id = $payment->get_transaction_id(); |
||
471 | |||
472 | if ( empty( $transaction_id ) ) { |
||
473 | return false; |
||
474 | } |
||
475 | |||
476 | // Invoice request. |
||
477 | $request = new InvoiceRequest( |
||
478 | $this->config->merchant_id, |
||
479 | $this->config->shop_id |
||
480 | ); |
||
481 | |||
482 | $request->set_parameter( 'trxid', $transaction_id ); |
||
483 | |||
484 | // Create invoice. |
||
485 | try { |
||
486 | $result = $this->client->create_invoice( $request ); |
||
487 | } catch ( \Exception $e ) { |
||
488 | $this->error = new \WP_Error( 'sisow_error', $e->getMessage() ); |
||
489 | |||
490 | return false; |
||
491 | } |
||
492 | |||
493 | if ( $result instanceof \Pronamic\WordPress\Pay\Gateways\Sisow\Invoice ) { |
||
494 | $payment->set_status( Core_Statuses::SUCCESS ); |
||
495 | |||
496 | $payment->save(); |
||
497 | |||
498 | return true; |
||
499 | } |
||
500 | |||
501 | return false; |
||
502 | } |
||
503 | |||
504 | /** |
||
505 | * Cancel reservation. |
||
506 | * |
||
507 | * @param Payment $payment Payment. |
||
508 | * |
||
509 | * @return bool |
||
510 | */ |
||
511 | public function cancel_reservation( $payment ) { |
||
512 | $transaction_id = $payment->get_transaction_id(); |
||
513 | |||
514 | if ( empty( $transaction_id ) ) { |
||
515 | return false; |
||
516 | } |
||
517 | |||
518 | // Cancel reservation request. |
||
519 | $request = new CancelReservationRequest( |
||
520 | $this->config->merchant_id, |
||
521 | $this->config->shop_id |
||
522 | ); |
||
523 | |||
524 | $request->set_parameter( 'trxid', $transaction_id ); |
||
525 | |||
526 | // Cancel reservation. |
||
527 | try { |
||
528 | $result = $this->client->cancel_reservation( $request ); |
||
529 | } catch ( \Exception $e ) { |
||
530 | $this->error = new \WP_Error( 'sisow_error', $e->getMessage() ); |
||
531 | |||
532 | return false; |
||
533 | } |
||
534 | |||
535 | if ( false === $result ) { |
||
536 | return false; |
||
537 | } |
||
538 | |||
539 | if ( isset( $result->status ) ) { |
||
540 | $payment->set_status( Statuses::transform( $result->status ) ); |
||
541 | |||
542 | $payment->save(); |
||
543 | |||
544 | return true; |
||
545 | } |
||
546 | |||
547 | return false; |
||
548 | } |
||
549 | } |
||
550 |