PluginProbe
MakeCommerce for WooCommerce / 3.0.11
MakeCommerce for WooCommerce v3.0.11
4.1.1 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 All 97 releases
makecommerce / payment / gateway / woocommerce / methods.php

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

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