PluginProbe
MakeCommerce for WooCommerce / 4.1.0
MakeCommerce for WooCommerce v4.1.0
4.1.0 4.0.8 trunk 1.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 2.0.0 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.2.0 2.2.1 2.2.2 All 96 releases
makecommerce / payment / gateway / woocommerce / methods.php

methods.php in MakeCommerce for WooCommerce 4.1.0, at payment/gateway/woocommerce/methods.php

573 lines 19.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace MakeCommerce\Payment\Gateway\WooCommerce;
4
5 class Methods {
6
7 public $banklinks = array();
8 public $banklinks_grouped = array();
9 public $cards = array();
10 public $paylater = array();
11 public $paylater_grouped = array();
12
13 private $id;
14 private $init;
15 private $settings;
16
17 /**
18 * Construct payment methods
19 *
20 * @since 3.0.0
21 */
22 public function __construct( $id, $init, $settings ) {
23
24 $this->id = $id;
25 $this->init = $init;
26 $this->settings = $settings;
27
28 $this->load_methods();
29 }
30
31 /**
32 * Loads all payment methods from the database
33 *
34 * @since 3.0.0
35 */
36 public function load_methods() {
37
38 global $wpdb;
39
40 $internationalMethods = [
41 'revolut' => 'revolut',
42 'n26' => 'n26'
43 ];
44
45 $manual_renewals = false;
46 $has_subscriptions = false;
47 if ( class_exists( '\WC_Subscriptions_Cart' ) ) {
48 // On change payment method, check for subscription
49 $subscription_order = false;
50 if ( isset( $_GET['change_payment_method'] ) ) {
51 $post_type = get_post_type( $_GET['change_payment_method'] );
52 if ( $post_type == 'shop_subscription' ) {
53 $subscription_order = true;
54 }
55 }
56 if ( \WC_Subscriptions_Cart::cart_contains_subscription() || $subscription_order ) {
57 $has_subscriptions = true;
58
59 if ( class_exists( '\WC_Subscriptions_Admin' )
60 && get_option( \WC_Subscriptions_Admin::$option_prefix . '_accept_manual_renewals', 'no' ) === 'yes'
61 && get_option( \WC_Subscriptions_Admin::$option_prefix . '_turn_off_automatic_payments', 'no' ) === 'yes'
62 ) {
63 $manual_renewals = true;
64 }
65 }
66 }
67
68 $methods = $wpdb->get_results( "SELECT * FROM " . $wpdb->prefix . MAKECOMMERCE_TABLENAME );
69
70 if ( count( $methods ) ) {
71
72 if ( is_admin() && $this->init == true && get_option( 'mc_banklinks_api_type' ) != get_option( 'mk_api_type', false ) ) {
73 add_action( 'admin_notices', array( $this, 'banklinks_list_type_notice' ) );
74 }
75
76 foreach ( $methods as $method ) {
77 if ( $method->type == 'banklink'|| $method->type == 'other' ) {
78 # If manual renewals are enabled, allow banklinks
79 if ( !$has_subscriptions || $manual_renewals ) {
80 $banklinks[] = $banklinks_grouped[$method->country][] = $method;
81 if ( in_array( $method->name, $internationalMethods ) ) {
82 $intMethod = clone( $method );
83 $intMethod->country = 'other';
84 $banklinks[] = $banklinks_grouped['other'][] = $intMethod;
85 unset( $internationalMethods[$method->name] );
86 }
87 }
88 } elseif ( $method->type == 'card' ) {
89 if ( $has_subscriptions && !$manual_renewals ) {
90 if ( in_array($method->name, ['visa', 'mastercard'] ) ) {
91 $cards[] = $method;
92 }
93 } else {
94 $cards[] = $method;
95 }
96 } elseif( $method->type == 'payLater' ) {
97 if ( !$has_subscriptions ) {
98 $paylater[] = $paylater_grouped[$method->country][] = $method;
99 }
100 }
101 }
102
103 if ( isset( $this->settings['ui_chorder'] ) ) {
104
105 if ( (!$has_subscriptions || $manual_renewals) && isset( $banklinks ) && isset( $banklinks_grouped ) ) {
106 usort( $banklinks, array( $this, 'sort_banklinks' ) );
107
108 foreach( $banklinks_grouped as &$country ) {
109 usort( $country, array( $this, 'sort_banklinks' ) );
110 }
111 }
112 }
113
114 if ( !$has_subscriptions ) {
115
116 if ( isset( $paylater ) ) {
117 $this->paylater = $paylater;
118 }
119
120 if ( isset( $paylater_grouped ) ) {
121 $this->paylater_grouped = $paylater_grouped;
122 }
123 }
124
125 # Only when the order does not contain subscriptions or has manual renewals enabled
126 if ( !$has_subscriptions || $manual_renewals) {
127 if ( isset( $banklinks ) ) {
128 $this->banklinks = $banklinks;
129 }
130
131 if ( isset( $banklinks_grouped ) ) {
132 $this->banklinks_grouped = $banklinks_grouped;
133 }
134 }
135
136 if ( isset( $cards ) ) {
137 $this->cards = $cards;
138 }
139
140 if ( is_admin() ) {
141 remove_action( 'admin_notices', array( $this, 'empty_banklinks_notice' ), 30 );
142 }
143
144 } elseif ( is_admin() && $this->init == true ) {
145
146 add_action( 'admin_notices', array( $this, 'empty_banklinks_notice' ), 30 );
147 }
148 }
149
150 /**
151 * Banklink sorting function
152 *
153 * @since 3.0.0
154 */
155 public function sort_banklinks( $a, $b ) {
156
157 $order = array_map( 'trim', explode( ",", $this->settings['ui_chorder'] ) );
158
159 $posA = array_search( $a->name, $order );
160 $posB = array_search( $b->name, $order );
161
162 if ( $posA === $posB ) {
163 return $a->id > $b->id ? 1 : -1;
164 }
165
166 if ( $posA === FALSE ) {
167 return 1;
168 }
169
170 if ( $posB === FALSE ) {
171 return -1;
172 }
173
174 return $posA > $posB ? 1 : -1;
175 }
176
177 /**
178 * Notice for admin that banklinks are empty
179 *
180 * @since 3.0.0
181 */
182 public function empty_banklinks_notice() {
183 ?>
184 <div class="notice notice-error is-dismissible">
185 <p>
186 <?php echo __('The payment methods list for MakeCommerce payment module is empty.', 'wc_makecommerce_domain'); ?>
187 <a href="<?php echo admin_url('admin.php?page=wc-settings&tab=checkout&section=makecommerce'); ?>"><?php echo __('Go to the settings to update them', 'wc_makecommerce_domain'); ?></a>
188 </p>
189 </div>
190 <?php
191 }
192
193 /**
194 * Notice for admin that payment methods have been loaded for a different environment
195 *
196 * @since 3.0.0
197 */
198 public function banklinks_list_type_notice() {
199 ?>
200 <div class="notice notice-error is-dismissible">
201 <p>
202 <?php echo __('You have changed the environment for MakeCommerce payment module. The payment methods list has been loaded for a different environment.', 'wc_makecommerce_domain'); ?>
203 <a href="<?php echo admin_url('admin.php?page=wc-settings&tab=checkout&section=makecommerce'); ?>"><?php echo __('Go to the settings to update them', 'wc_makecommerce_domain'); ?></a>
204 </p>
205 </div>
206 <?php
207 }
208
209 /**
210 * Returns customers default country if they are logged in
211 * if not, returns default country by selected language
212 *
213 * @since 3.0.0
214 */
215 private function get_default_country() {
216
217 global $woocommerce;
218
219 if ( $woocommerce->customer ) {
220
221 $customerCountry = strtolower( $woocommerce->customer->get_shipping_country() );
222 if ( array_key_exists( $customerCountry, $this->banklinks_grouped ) ) {
223 return $customerCountry;
224 } else {
225 return 'other';
226 }
227 }
228
229 $localeToCountry = array(
230 'et' => 'ee',
231 'lv' => 'lv',
232 'lt' => 'lt',
233 'fi' => 'fi',
234 );
235
236 $locale = \MakeCommerce\i18n::get_two_char_locale();
237 if ( array_key_exists( $locale, $localeToCountry ) ) {
238 return $localeToCountry[$locale];
239 }
240
241 return key( $this->banklinks_grouped );
242 }
243
244 /**
245 * Show available payment methods in checkout
246 *
247 * @since 3.0.0
248 */
249 public function show_methods() {
250
251 if ( !($this->settings['ui_mode'] == 'inline') ) {
252 $this->hidden_select_box_payment_methods();
253 }
254
255 $this->method_list();
256 }
257
258 /**
259 * Display country selector for payment methods
260 *
261 * @since 3.0.1
262 */
263 private function method_list_countries() {
264
265 //sort countries
266 $this->sort_countries_list();
267
268 if ( ( empty( $this->settings['ui_widget_groupcountries'] ) || $this->settings['ui_widget_groupcountries'] == 'no' ) &&
269 ( !isset( $this->settings['ui_widget_countries_hidden'] ) || $this->settings['ui_widget_countries_hidden'] == 'no' ) ) {
270 ?>
271 <div class="makecommerce_country_picker_countries">
272 <?php foreach ( array_keys( $this->banklinks_grouped ) as $country ): ?>
273 <input style="display: none;" type="radio" id="makecommerce_country_picker_<?php echo $country; ?>" name="makecommerce_country_picker" value="<?php echo $country; ?>" <?php if ( $this->get_default_country() == $country ) echo 'checked="checked" '; ?>/><?php if ( $this->settings['ui_widget_countryselector'] == 'flag' ) { ?><label for="makecommerce_country_picker_<?php echo $country; ?>" class="makecommerce_country_picker_label country_picker_image_<?php echo $country; ?>" style="background-image: url(<?php echo plugins_url( '/images/'.$country.'32.png', __FILE__ ); ?>);"></label><?php } ?>
274 <?php endforeach; ?>
275 <?php if ( $this->settings['ui_widget_countryselector'] == 'dropdown' ) : ?>
276 <select name="makecommerce_country_picker_select" style="width: 100%;">
277 <?php foreach ( array_keys( $this->banklinks_grouped ) as $country ): ?>
278 <option value="<?php echo $country; ?>" <?php if ( $this->get_default_country() == $country ) echo 'selected="selected" '; ?>><?php echo \MakeCommerce\i18n::get_country_name( $country ); ?></option>
279 <?php endforeach; ?>
280 <option value="card" style="display:none;"></option>
281 </select>
282 <?php endif; ?>
283 </div>
284 <?php
285 }
286 }
287
288 /**
289 * Sort countries
290 *
291 * @since 3.0.6
292 */
293 public function sort_countries_list() {
294
295 if ( isset( $this->settings['ui_payment_country_order'] ) ) {
296
297 $order = explode( ',', $this->settings['ui_payment_country_order'] );
298 foreach ( $order as $key=>&$country ) {
299 if ( !isset( $this->banklinks_grouped[$country] ) ) {
300 unset( $order[$key] );
301 continue;
302 }
303
304 $country = strtolower( trim( $country ) );
305 }
306
307 if ( !empty( $order ) ) {
308 $this->banklinks_grouped = array_merge( array_flip( $order ), $this->banklinks_grouped );
309 }
310 }
311 }
312
313 /**
314 * Hidden selectbox for all payment methods
315 *
316 * @since 3.0.1
317 */
318 private function hidden_select_box_payment_methods() {
319
320 ?>
321 <select id="<?php echo $this->id; ?>" name="PRESELECTED_METHOD_<?php echo $this->id; ?>">
322 <option value=""></option>
323 <?php foreach( $this->banklinks as $method ): ?>
324 <option value="<?php echo $method->country.'_'.$method->name; ?>"><?php echo strtoupper( $method->country ).' - '.ucfirst( $method->display_name ); ?></option>
325 <?php endforeach; ?>
326
327 <?php foreach( $this->cards as $method ): ?>
328 <option value="card_<?php echo $method->name; ?>"><?php echo ucfirst( $method->display_name ); ?></option>
329 <?php endforeach; ?>
330
331 <?php foreach( $this->paylater as $method ): ?>
332 <option value="<?php echo $method->country.'_'.$method->name; ?>"><?php echo strtoupper( $method->country ).' - '.ucfirst( $method->display_name ); ?></option>
333 <?php endforeach; ?>
334 </select>
335 <?php
336 }
337
338 /**
339 * Country picker
340 *
341 * @since 3.0.1
342 */
343 private function country_picker( $country ) {
344
345 if ( $this->settings['ui_widget_groupcountries'] == 'yes' ) {
346
347 $checked = "";
348 if ( $this->get_default_country() == $country ) {
349 $checked = 'checked="checked"';
350 }
351
352 if ( empty( $this->settings['ui_widget_groupcountries'] ) || $this->settings['ui_widget_groupcountries'] == 'no' ) {
353 echo '<input type="radio" id="makecommerce_country_picker_'. $country .'" name="makecommerce_country_picker" value="'. $country .'" '. $checked . '/>';
354 } else {
355
356 }
357
358 echo '
359 <label for="makecommerce_country_picker_'. $country .'">
360 <img src="'.plugins_url( '/images/'.$country.'32.png', __FILE__ ) .'" />
361 </label>
362 ';
363 }
364 }
365
366 /**
367 * Display payment methods as a widget
368 *
369 * @since 3.0.0
370 */
371 private function method_list() {
372
373 echo '<input type="hidden" id="makecommerce_customer_country" value="'.$this->get_default_country().'"/>
374 <ul class="makecommerce-picker">';
375
376 global $woocommerce;
377
378 $cartTotal = $woocommerce->cart->total;
379
380 if ( $this->banklinks || $this->cards ) {
381
382 //dont show other countries if there are no card payments available.
383 if ( $this->cards && empty( $this->banklinks_grouped['other'] ) ) {
384 $this->banklinks_grouped['other'] = array();
385 }
386
387 $this->method_list_countries();
388
389 foreach ( $this->banklinks_grouped as $country => $methods ) {
390
391 echo '<li class="makecommerce-picker-country">';
392
393 $this->country_picker( $country );
394
395 echo '<div class="makecommerce_country_picker_methods logosize-' . $this->settings["ui_widget_logosize"] . '" id="makecommerce_country_picker_methods_'. $country .'">';
396
397 $this->banklink_list( $cartTotal, $methods );
398
399 $this->creditcard_list( $cartTotal, $country );
400
401 $this->paylater_list( $cartTotal, $country );
402
403 echo '
404 </div>
405 </li>
406 ';
407 }
408 }
409
410 echo '</ul>
411
412 <div class="mc-clear-both"></div
413 ';
414 }
415
416 /**
417 * List version of a method item
418 *
419 * @since 3.0.1
420 */
421 private function list_version_item( $method, $card = false, $country = '' ) {
422
423 $banklink_value = $banklink_id = $method->country.'_'.$method->name;
424 if ( $card ) {
425 $banklink_value = 'card_' . $method->name;
426 $banklink_id = 'card_' . $method->name . '_' . $country;
427 }
428
429 $methodName = "";
430 if ( in_array( $this->settings['ui_inline_uselogo'], array( 'text', 'text_logo' ) ) ) {
431 $methodName = $method->display_name;
432 }
433
434 $logo = "";
435 if ( in_array( $this->settings['ui_inline_uselogo'], array( 'logo', 'text_logo' ) ) ) {
436 if ( $methodName == "" ) {
437 $logo = '<img style="display: inline-block; float: none; margin-bottom: -7px;" src="'. $method->logo_url .'" title="'. ucfirst( $method->display_name ) .'" />';
438 } else {
439 $logo = '<img src="'. $method->logo_url .'" title="'. ucfirst( $method->display_name ) .'" />';
440 }
441 }
442
443 echo '
444 <div class="payment_method_list_item_container">
445 <input type="radio" id="makecommerce_method_picker_'. $banklink_id .'" name="PRESELECTED_METHOD_'. $this->id .'" value="'. $banklink_value .'"/>
446
447 <label for="makecommerce_method_picker_' . $banklink_id . '">
448 <span class="makecommerce-method-title">' . trim($methodName) . $logo.'</span>
449 </label>
450 </div>
451 ';
452 }
453
454 /**
455 * Widget version of a method item
456 *
457 * @since 3.0.1
458 */
459 private function widget_version_item( $method, $card = false ) {
460
461 $banklink_id = $method->country.'_'.$method->name;
462 if ( $card ) {
463 $banklink_id = "card_" . $method->name;
464 }
465
466 echo '
467 <div class="makecommerce-banklink-picker" banklink_id="'. $banklink_id .'" id="' . $method->name . '">
468 <img src="'. $method->logo_url .'" title="'. ucfirst( $method->display_name ) .'" />
469 </div>
470 ';
471 }
472
473 /**
474 * Show list of banklinks
475 *
476 * @since 3.0.1
477 */
478 private function banklink_list( $cartTotal, $methods ) {
479
480 foreach ( $methods as $method ) {
481 // Check min max values
482 if ( $this->is_allowed_method( $method, $cartTotal ) ) {
483 if ( $this->settings['ui_mode'] == 'inline' ) {
484 $this->list_version_item( $method );
485 } else {
486 $this->widget_version_item( $method );
487 }
488 }
489 }
490 }
491
492 /**
493 * Show list of paylater methodss
494 *
495 * @since 3.0.1
496 */
497 private function paylater_list( $cartTotal, $selected_country ) {
498
499 if ( $this->paylater_grouped ) {
500 echo '<div class="breaker"></div>';
501 }
502
503 foreach ( $this->paylater_grouped as $country => $methods ) {
504
505 if ( $country == $selected_country ) {
506 foreach ( $methods as $method ) {
507 // Check min max values
508 if ( $this->is_allowed_method( $method, $cartTotal ) ) {
509 if ( $this->settings['ui_mode'] == 'inline' ) {
510 $this->list_version_item( $method );
511 } else {
512 $this->widget_version_item( $method );
513 }
514 }
515 }
516
517 if ( $country == 'other' ) {
518 echo '<p class="no-methods">'. _e( 'No payment methods for selected country' ,'wc_makecommerce_domain' ) .'</p>';
519 }
520 }
521 }
522 }
523
524 /**
525 * Show list of credit cards
526 *
527 * @since 3.0.1
528 */
529 private function creditcard_list( $cartTotal, $country ) {
530
531 if ( $this->cards ) {
532
533 echo '<div class="breaker"></div>';
534
535 foreach ( $this->cards as $method ) {
536 // Check min max values
537 if ( $this->is_allowed_method( $method, $cartTotal ) ) {
538 if ( $this->settings['ui_mode'] == 'inline' ) {
539 $this->list_version_item( $method, true, $country );
540 } else {
541 $this->widget_version_item( $method, true );
542 }
543 }
544 }
545 } else if ( $country == 'other' && empty( $this->banklinks_grouped['other'] ) ) {
546 echo '<p class="no-methods">'. _e( 'No payment methods for selected country' ,'wc_makecommerce_domain' ) .'</p>';
547 }
548 }
549
550 /**
551 * Return list of all payment methods
552 *
553 * @since 3.0.12
554 */
555 static function get_payment_methods() {
556 global $wpdb;
557
558 return $wpdb->get_results( "SELECT * FROM " . $wpdb->prefix . MAKECOMMERCE_TABLENAME );
559 }
560
561 /**
562 * Return bool whether the payment method can be used based on amount constraints
563 *
564 * @since 3.3.0
565 */
566 private function is_allowed_method( $method, $cartTotal ) {
567 $min = $method->min_amount ?? 0;
568 $max = $method->max_amount ?? INF;
569
570 return $min <= $cartTotal && $max >= $cartTotal;
571 }
572 }
573