PluginProbe
imoje / 4.2.1
imoje v4.2.1
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 / Helper.php

Helper.php in imoje 4.2.1, at includes/Helper.php

506 lines 13.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 use Imoje\Payment\Api;
4 use Imoje\Payment\CartData;
5 use Imoje\Payment\Invoice;
6 use Imoje\Payment\LeaseNow;
7 use Imoje\Payment\Notification;
8 use Imoje\Payment\Util;
9
10 /**
11 * Class Helper
12 */
13 class Helper {
14
15 /**
16 * @return string
17 */
18 public static function get_version() {
19 $wc = WC();
20
21 return get_file_data( WOOCOMMERCE_IMOJE_PLUGIN_DIR, [ 'Version' ], 'plugin' )[0] . ';woocommerce_' . $wc->version;
22 }
23
24 /**
25 * @param string $name
26 * @param string $display_name
27 * @param string $default_description
28 *
29 * @return array
30 */
31 static function get_gateway_details( $name, $display_name, $default_description ) {
32 return [
33 'name' => $name,
34 'display_name' => $display_name,
35 'default_description' => $default_description,
36 ];
37 }
38
39 /**
40 * @param string $config_value
41 *
42 * @return bool
43 */
44 public static function check_is_config_value_selected( $config_value ) {
45 return $config_value === 'yes';
46 }
47
48 /**
49 * @param string $post_id
50 * @param string $taxonomy
51 * @param string $before
52 * @param string $sep
53 * @param string $after
54 *
55 * @return array|false|int|string|WP_Error|WP_Term|WP_Term[]|null
56 */
57 public static function leasenow_get_product_category( $post_id, $taxonomy, $before = '', $sep = '', $after = '' ) {
58 $terms = get_the_terms( $post_id, $taxonomy );
59
60 if ( is_wp_error( $terms ) ) {
61 return $terms;
62 }
63
64 if ( empty( $terms ) ) {
65 return false;
66 }
67
68 $links = [];
69
70 foreach ( $terms as $term ) {
71 $link = get_term_link( $term, $taxonomy );
72 if ( is_wp_error( $link ) ) {
73 return $link;
74 }
75 $links[] = $term->name;
76 }
77
78 $term_links = apply_filters( "term_links-$taxonomy", $links ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
79
80 return $before . implode( $sep, $term_links ) . $after;
81 }
82
83 /**
84 * @param WC_Order $order
85 * @param string $config_value
86 * @param bool $is_api
87 *
88 * @return string
89 */
90 public static function get_lease_now( $order, $config_value, $is_api ) {
91
92 if ( ! self::check_is_config_value_selected( $config_value ) ) {
93 return '';
94 }
95
96 $leasenow = new LeaseNow();
97
98 foreach ( $order->get_items() as $item ) {
99
100 $product = $item->get_product();
101 $product_id = $item->get_product_id();
102 $name = $product->get_title();
103
104 if ( method_exists( $product, 'get_attribute_summary' )
105 && $attribute_summary = $product->get_attribute_summary() ) {
106 $name .= ' - ' . $attribute_summary
107 ? ( ' - ' . $product->get_attribute_summary() )
108 : '';
109 }
110
111 $tax_rates = WC_Tax::get_rates( $item->get_tax_class() );
112 $tax_rate = 0;
113 if ( ! empty( $tax_rates ) ) {
114 $tax_rate = reset( $tax_rates );
115 $tax_rate = (int) $tax_rate['rate'];
116 }
117
118 $leasenow->addItem(
119 $item->get_product_id(),
120 self::leasenow_get_product_category(
121 ( $product->is_type( 'simple' )
122 ? $product_id
123 : $product->get_parent_id() ),
124 'product_cat',
125 '',
126 ', ' ),
127 $name,
128 Util::convertAmountToFractional( $order->get_item_total( $item ) ),
129 Util::convertAmountToFractional( $order->get_item_tax( $item ) ),
130 $tax_rate,
131 $item->get_quantity(),
132 $product->get_permalink()
133 );
134 }
135
136 return $leasenow->prepare( $is_api );
137 }
138
139 /**
140 * @param WC_Order $order
141 * @param bool $config_value
142 * @param bool $is_api
143 *
144 * @return array
145 */
146 public static function get_invoice( $order, $config_value, $is_api, $meta_tax_field_name ) {
147
148 if ( ! self::check_is_config_value_selected( $config_value ) ) {
149 return [];
150 }
151
152 $cart = self::get_cart( $order );
153
154 $invoice = new Invoice();
155
156 if ( empty( $cart['items'] ) ) {
157 return [];
158 }
159
160 foreach ( $cart['items'] as $item ) {
161
162 $tax = null;
163 try {
164 $tax = constant( '\Imoje\Payment\Invoice::TAX_' . $item['vat'] );
165 } catch( Exception $e ) {
166 }
167
168 if ( ! $tax ) {
169 return [];
170 }
171
172 $invoice->addItem( $item['name'],
173 $item['id'],
174 $item['quantity'],
175 constant( '\Imoje\Payment\Invoice::TAX_' . $item['vat'] ),
176 $item['amount']
177 );
178 }
179
180 if ( isset( $cart['shipping'] ) && $cart['shipping'] ) {
181 $invoice->addItem( $cart['shipping']['name'],
182 1,
183 1,
184 constant( '\Imoje\Payment\Invoice::TAX_' . $cart['shipping']['vat'] ),
185 $cart['shipping']['amount']
186 );
187 }
188
189 $invoice->setBuyer( Invoice::BUYER_PERSON,
190 $order->get_billing_email(),
191 $cart['address']['billing']['name'],
192 $cart['address']['billing']['street'],
193 $cart['address']['billing']['city'],
194 $cart['address']['billing']['postalCode'],
195 $cart['address']['billing']['country'] );
196
197 $vat_number = trim( $order->get_meta( $meta_tax_field_name ) );
198
199 if ( $meta_tax_field_name && $vat_number ) {
200
201 $vat_country_alpha2 = Invoice::VAT_COUNTRY_ALPHA2;
202
203 if ( strlen( $vat_number ) > 2 ) {
204 $first_two = substr( $vat_number, 0, 2 );
205
206 if ( ctype_alpha( $first_two ) ) {
207 $vat_country_alpha2 = $first_two;
208 }
209 }
210
211 $invoice->setCompanyBuyer( $vat_country_alpha2, $vat_number, $order->get_billing_company()
212 ?: '' );
213 }
214
215 return $invoice->prepare( $is_api );
216 }
217
218 /**
219 * @param WC_Order $order
220 *
221 * @return array
222 */
223 public static function get_cart( $order ) {
224
225 $cart_data = new CartData();
226
227 $cart_data->setCreatedAt( strtotime( $order->get_date_created() ) );
228 $cart_data->setAmount( Util::convertAmountToFractional( $order->get_total() ) );
229
230 foreach ( $order->get_items() as $item ) {
231
232 $product = $item->get_product();
233
234 $tax_rate_product = current( WC_Tax::get_rates( $product->get_tax_class() ) );
235
236 $rate = 0;
237 if ( $tax_rate_product && isset( $tax_rate_product['rate'] ) ) {
238 $rate = (float) $tax_rate_product['rate'];
239 }
240 // endregion
241
242 $cart_data->addItem(
243 $product->get_id(),
244 $rate,
245 $item->get_name(),
246 Util::convertAmountToFractional( $item->get_total() + $item->get_total_tax() ),
247 $item->get_quantity(),
248 false
249 );
250 }
251
252 $phone = $order->get_billing_phone();
253
254 $cart_data->setAddressBilling(
255 $order->get_billing_city(),
256 $order->get_billing_first_name() . ' ' . $order->get_billing_last_name(),
257 $phone
258 ?: '',
259 $order->get_billing_address_1() . ' ' . $order->get_billing_address_2(),
260 $order->get_billing_country(),
261 $order->get_billing_postcode()
262 );
263
264 if ( $order->get_total_discount( false ) > 0 ) {
265 $cart_data->setDiscount(
266 0,
267 'Zniżka',
268 Util::convertAmountToFractional( $order->get_total_discount( false ) )
269 );
270 }
271
272 $shipping_data = current( $order->get_items( 'shipping' ) );
273
274 if ( ! $shipping_data ) {
275 $cart_data->setAddressDelivery(
276 $order->get_billing_city(),
277 $order->get_billing_first_name() . ' ' . $order->get_billing_last_name(),
278 $phone
279 ?: '',
280 $order->get_billing_address_1() . ' ' . $order->get_billing_address_2(),
281 $order->get_billing_country(),
282 $order->get_billing_postcode()
283 );
284 $cart_data->setShipping(
285 0,
286 'Brak wysyłki',
287 0
288 );
289
290 return $cart_data->prepareCartDataArray();
291 }
292
293 $shipping_data = $shipping_data->get_data();
294 $id_shipping_tax = null;
295 foreach ( $order->get_items( 'shipping' ) as $item_id => $item_tax ) {
296 $shipping_data = json_decode( $item_tax, true );
297 $id_shipping_tax = current( array_keys( $shipping_data['taxes']['total'] ) );
298 }
299
300 $cart_data->setShipping( $id_shipping_tax
301 ? (float) WC_Tax::_get_tax_rate( $id_shipping_tax )['tax_rate']
302 : 0,
303 $shipping_data['name'],
304 Util::convertAmountToFractional( (float) $order->get_shipping_total() + (float) $order->get_shipping_tax() ) );
305
306 $cart_data->setAddressDelivery(
307 $order->get_shipping_city(),
308 $order->get_shipping_first_name() . ' ' . $order->get_shipping_last_name(),
309 $phone
310 ?: '',
311 $order->get_shipping_address_1() . ' ' . $order->get_shipping_address_2(),
312 $order->get_shipping_country(),
313 $order->get_shipping_postcode()
314 );
315
316 return $cart_data->prepareCartDataArray();
317 }
318
319 /**
320 * @param string $service_id
321 * @param string $service_key
322 * @param string $debug_mode
323 *
324 * @throws Exception
325 */
326 public static function check_notification( $service_id, $service_key, $debug_mode ) {
327 $notification = new Notification(
328 $service_id,
329 $service_key,
330 self::check_is_config_value_selected( $debug_mode )
331 );
332
333 // it can be order data or notification code - depends on verification notification
334 $result_check_request_notification = $notification->checkRequest();
335
336 if ( is_int( $result_check_request_notification ) ) {
337 echo $notification->formatResponse( Notification::NS_ERROR, $result_check_request_notification );
338 exit();
339 }
340
341 if ( ! ( $order = wc_get_order( $result_check_request_notification['transaction']['orderId'] ) ) ) {
342
343 echo $notification->formatResponse( Notification::NS_ERROR, Notification::NC_ORDER_NOT_FOUND );
344 exit();
345 }
346
347 $order_status = $order->get_status();
348
349 if ( $result_check_request_notification['transaction']['type'] === Notification::TRT_REFUND ) {
350
351 if ( $result_check_request_notification['transaction']['status'] !== Notification::TRS_SETTLED ) {
352 echo $notification->formatResponse( Notification::NS_OK, Notification::NC_IMOJE_REFUND_IS_NOT_SETTLED );
353
354 exit();
355 }
356
357 if ( $order_status === 'refunded' ) {
358 echo $notification->formatResponse( Notification::NS_ERROR, Notification::NC_ORDER_STATUS_IS_INVALID_FOR_REFUND );
359 exit();
360 }
361
362 $refund = wc_create_refund( [
363 'amount' => Util::convertAmountToMain( $result_check_request_notification['transaction']['amount'] ),
364 'reason' => 'imoje API',
365 'order_id' => $result_check_request_notification['transaction']['orderId'],
366 'refund_payment' => true,
367 ] );
368
369 if ( $refund->errors ) {
370
371 echo $notification->formatResponse( Notification::NS_ERROR );
372 exit();
373 }
374
375 $order->add_order_note(
376 sprintf(
377 __( 'Refund for amount %s with UUID %s has been correctly processed.', 'imoje' ),
378 $result_check_request_notification['transaction']['amount'],
379 $result_check_request_notification['transaction']['id']
380 )
381 );
382
383 echo $notification->formatResponse( Notification::NS_OK );
384 exit;
385 }
386
387 if ( $order_status === 'completed' || $order_status === 'processing' ) {
388
389 echo $notification->formatResponse( Notification::NS_ERROR, Notification::NC_INVALID_ORDER_STATUS );
390 exit();
391 }
392
393 if ( ! Notification::checkRequestAmount( $result_check_request_notification, Util::convertAmountToFractional( $order->data['total'] ), $order->data['currency'] ) ) {
394
395 echo $notification->formatResponse( Notification::NS_ERROR, Notification::NC_AMOUNT_NOT_MATCH );
396 exit();
397 }
398
399 $transactionStatuses = Util::getTransactionStatuses();
400
401 if ( ! isset( $transactionStatuses[ $result_check_request_notification['transaction']['status'] ] ) ) {
402 echo $notification->formatResponse( Notification::NS_ERROR, Notification::NC_UNHANDLED_STATUS );
403 exit;
404 }
405
406 switch ( $result_check_request_notification['transaction']['status'] ) {
407 case Notification::TRS_SETTLED:
408
409 $order->update_status( $order->needs_processing()
410 ? 'processing'
411 : 'completed',
412 __( 'Transaction reference', 'imoje' ) . ': ' . $result_check_request_notification['transaction']['id'] );
413
414 $order->update_meta_data( 'imoje_transaction_uuid', $result_check_request_notification['transaction']['id'] );
415 $order->save_meta_data();
416
417 echo $notification->formatResponse( Notification::NS_OK );
418 exit;
419 case Notification::TRS_REJECTED:
420 $order->update_status( 'failed' );
421 $order->add_order_note( __( 'Transaction reference', 'imoje' ) . ': ' . $result_check_request_notification['transaction']['id'] );
422 echo $notification->formatResponse( Notification::NS_OK );
423 exit;
424 default:
425 echo $notification->formatResponse( Notification::NS_OK, Notification::NC_UNHANDLED_STATUS );
426 exit;
427 }
428 }
429
430 /**
431 * @param int $order_id
432 * @param string $authorization_token
433 * @param string $merchant_id
434 * @param string $service_id
435 * @param float $amount
436 *
437 * @return bool
438 * @throws Exception
439 */
440 public static function process_refund( $order_id, $authorization_token, $merchant_id, $service_id, $amount, $environment = '' ) {
441
442 $order = wc_get_order( $order_id );
443
444 $api = new Api( $authorization_token, $merchant_id, $service_id, $environment );
445
446 $refund = $api->createRefund(
447 $api->prepareRefundData(
448 Util::convertAmountToFractional( $amount )
449 ),
450 $order->get_meta( 'imoje_transaction_uuid' )
451 );
452
453 $refund_error = self::get_refund_error( $refund );
454
455 if ( ! $refund['success'] ) {
456
457 $order->add_order_note(
458 $refund_error
459 );
460
461 throw new Exception( $refund_error );
462 }
463
464 if ( $refund['body']['transaction']['status'] === Notification::TRS_SETTLED ) {
465
466 $order->add_order_note(
467 sprintf(
468 __( 'Refund for amount %s with UUID %s has been correctly processed.', 'imoje' ),
469 $amount,
470 $refund['body']['transaction']['id']
471 )
472 );
473
474 return true;
475 }
476
477 if ( $refund['body']['transaction']['status'] !== Notification::TRS_NEW ) {
478
479 throw new Exception( $refund_error );
480 }
481
482 $order->add_order_note(
483 sprintf( __( 'Refund for amount %s with UUID %s has been requested', 'imoje' ),
484 $amount,
485 $refund['body']['transaction']['id']
486 )
487 );
488
489 throw new Exception( __( 'A refund has been requested and waiting for completion.', 'imoje' ) );
490 }
491
492 /**
493 * @param array $refund
494 *
495 * @return string|null
496 */
497 public static function get_refund_error( $refund ) {
498
499 if ( ! $refund['success'] && ( isset( $refund['data']['body'] ) && $refund['data']['body'] ) ) {
500 return __( 'Refund error: ', 'imoje' ) . ' ' . $refund['data']['body'];
501 }
502
503 return __( 'Refund error.', 'imoje' );
504 }
505 }
506