PluginProbe
MakeCommerce for WooCommerce / 1.1.4
MakeCommerce for WooCommerce v1.1.4
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 / makecommerce.php

makecommerce.php in MakeCommerce for WooCommerce 1.1.4, at makecommerce.php

2,137 lines 88.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 Plugin Name: MakeCommerce for WooCommerce
4 Description: Adds MakeCommerce payment gateway and Itella/Omniva parcel machine shipping methods to Woocommerce checkout
5 Version: 1.1.4
6 Author: Maksekeskus AS
7 Author URI: https://MakeCommerce.net/
8 Text Domain: wc_makecommerce_domain
9 */
10
11 if ( ! defined( 'ABSPATH' ) ) {
12 exit; // Exit if accessed directly.
13 }
14
15 if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
16
17 global $mkDbTable;
18 $mkDbTable = 'mc_banklinks';
19
20 function mc_install() {
21 global $wpdb;
22 global $mkDbTable;
23
24 $tableName = $wpdb->prefix . $mkDbTable;
25
26 $charset = $wpdb->get_charset_collate();
27
28 $sql = "CREATE TABLE $tableName (
29 id mediumint(9) NOT NULL AUTO_INCREMENT,
30 type varchar(10) NOT NULL,
31 country char(2) NOT NULL,
32 name varchar(25) NOT NULL,
33 url varchar(250) NOT NULL,
34 UNIQUE KEY id (id)
35 ) $charset;";
36
37 require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
38 dbDelta( $sql );
39 }
40
41 register_activation_hook( __FILE__, 'mc_install' );
42
43 if (!class_exists('wc_makecommerce_domain')) {
44 require_once('includes/Api.php');
45 }
46
47 function woocommerce_payment_makecommerce_init() {
48 load_plugin_textdomain('wc_makecommerce_domain', false, dirname(plugin_basename(__FILE__)) . '/');
49
50
51 class woocommerce_makecommerce extends WC_Payment_Gateway {
52
53 const MC_CANCELLED = 'CANCELLED';
54 const MC_COMPLETED = 'COMPLETED';
55 const MC_DEPOSITED = 'DEPOSITED';
56
57 const MC_PART_REFUNDED = 'PART_REFUNDED';
58 const MC_REFUNDED = 'REFUNDED';
59
60 const module_name = 'MakeCommerce';
61 const api_url = 'https://api.maksekeskus.ee/v1/';
62 const gateway_url = 'https://payment.maksekeskus.ee/pay/1/signed.html';
63 const gateway_url_static = 'https://payment.maksekeskus.ee/checkout/dist/';
64 const fo_url = 'https://merchant.maksekeskus.ee/';
65 const test_api_url = 'https://api-test.maksekeskus.ee/v1/';
66 const test_gateway_url = 'https://payment-test.maksekeskus.ee/pay/1/signed.html';
67 const test_gateway_url_static = 'https://payment-test.maksekeskus.ee/checkout/dist/';
68 const test_fo_url = 'https://merchant-test.maksekeskus.ee/';
69 const billing_descriptor_dba = '';
70 const currencies_allowed = 'EUR';
71 const module_homepage_url = 'https://maksekeskus.ee/en/integration-modules/makecommerce-woocommerce-payment-plugin/';
72 const testenv_homepage_url = 'http://maksekeskus.ee/en/for-developers/test-environment/';
73
74 public $id = 'makecommerce';
75 public $version = '1.1.2';
76
77 public $return_url;
78 public $return_url_cancel;
79
80 public $return_url_cc;
81
82 protected $_shop_id;
83 protected $_api_key_secret;
84 protected $_api_key_public;
85
86 protected $_gateway_url_static;
87
88 protected $_banklinks = array();
89 protected $_banklinks_grouped;
90 protected $_cards = array();
91
92 protected $_api;
93
94 protected $_init;
95
96 public function __construct($init = false) {
97
98 $this->_init = $init;
99
100 $this->return_url = site_url('/?makecommerce_return=1');
101 $this->return_url_cancel = site_url('/?makecommerce_return=1');
102
103 $this->return_url_cc = site_url('/?makecommerce_return=2');
104
105 // Load the form fields.
106 $this->init_form_fields();
107
108 // Load the settings.
109 $this->init_settings();
110
111 $this->initBanklinks();
112
113 $this->title = $this->settings['ui_widget_title'];
114 if (defined('ICL_LANGUAGE_CODE') && !empty($this->settings['ui_widget_title_'.ICL_LANGUAGE_CODE])) {
115 $this->title = $this->settings['ui_widget_title_'.ICL_LANGUAGE_CODE];
116 }
117 $this->method_title = 'MakeCommerce';
118 $this->description = true;
119
120 $this->_api = mk_get_api();
121 if (!$this->_api && $this->_init) {
122 add_action( 'admin_notices', array(&$this, 'makecommerce_api_info_missing') );
123 }
124
125 if(get_option('mk_api_type', false) == 'live') {
126 $this->_gateway_url_static = self::gateway_url_static;
127 } elseif (get_option('mk_api_type', false) == 'test') {
128 $this->_gateway_url_static = self::test_gateway_url_static;
129 }
130
131 $this->supports = array(
132 'products',
133 'refunds',
134 );
135
136 add_filter('query_vars', array(&$this, 'makecommerce_return_trigger'));
137 add_action('template_redirect', array(&$this, 'makecommerce_return_trigger_check'));
138
139 add_action( 'wp_ajax_makecommerce_pay_token', array(&$this, 'makecommerce_pay_token') );
140 add_action( 'wp_ajax_nopriv_makecommerce_pay_token', array(&$this, 'makecommerce_pay_token') );
141
142 if($this->_init == false) {
143 add_action('woocommerce_update_options_payment_gateways', array(&$this, 'process_admin_options'));
144 add_action('woocommerce_update_options_payment_gateways_' . $this->id, array(&$this, 'process_admin_options'));
145
146 add_action('woocommerce_receipt_' . $this->id, array(&$this, 'receipt_page'));
147 add_action('woocommerce_admin_order_data_after_order_details', array(&$this, 'admin_order_page'), 10, 1);
148
149 wp_enqueue_script('jquery');
150 wp_enqueue_style('makecommerce', plugins_url('/css/makecommerce.css', __FILE__), array(), $this->version);
151 }
152
153 if(is_admin()) {
154 add_action( 'wp_ajax_mc_banklinks_reload', array(&$this, 'mc_banklinks_reload') );
155 }
156 }
157
158 function makecommerce_api_info_missing() {
159 ?>
160 <div class="notice notice-error is-dismissible">
161 <p>
162 <?php echo __('You have not entered the Shop ID and keys for the MakeCommerce payment module. The module will not work without them.', 'wc_makecommerce_domain'); ?>
163 <a href="<?php echo admin_url('admin.php?page=wc-settings&tab=api&section=mk_api'); ?>"><?php echo __('Click here to enter them', 'wc_makecommerce_domain'); ?></a>
164 </p>
165 </div>
166 <?php
167 }
168
169 function makecommerce_banklinks_list_empty() {
170 ?>
171 <div class="notice notice-error is-dismissible">
172 <p>
173 <?php echo __('The payment methods list for MakeCommerce payment module is empty.', 'wc_makecommerce_domain'); ?>
174 <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>
175 </p>
176 </div>
177 <?php
178 }
179
180 function makecommerce_banklinks_list_type_notice() {
181 ?>
182 <div class="notice notice-error is-dismissible">
183 <p>
184 <?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'); ?>
185 <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>
186 </p>
187 </div>
188 <?php
189 }
190
191 /**
192 * Initialise Gateway Settings Form Fields
193 */
194 function init_form_fields() {
195
196 $languages = apply_filters('wpml_active_languages', NULL, 'skip_missing=0');
197
198 $this->form_fields = array();
199 $this->form_fields['header'] = array(
200 'type' => 'mc_header',
201 );
202 $this->form_fields['active'] = array(
203 'title' => __('Enable/Disable', 'wc_makecommerce_domain'),
204 'type' => 'checkbox',
205 'label' => __('Enable MakeCommerce payments', 'wc_makecommerce_domain'),
206 'default' => 'no'
207 );
208 $this->form_fields['api_title'] = array(
209 'title' => __('MakeCommerce API', 'wc_makecommerce_domain'),
210 'description' => sprintf(__('Go to <a href="%s">API settings</a> to fill in the credentials', 'wc_makecommerce_domain'), admin_url('admin.php?page=wc-settings&tab=api&section=mk_api')),
211 'type' => 'title',
212 );
213 $this->form_fields['ui_title'] = array(
214 'title' => '<br>'.__('User Interface', 'wc_makecommerce_domain'),
215 'type' => 'title',
216 'class' => 'ui-identifier',
217 );
218 $this->form_fields['ui_open_by_default'] = array(
219 'title' => __('Set as default selection', 'wc_makecommerce_domain'),
220 'label' => __('MakeCommerce payments widget will be selected by default', 'wc_makecommerce_domain'),
221 'type' => 'checkbox',
222 'default' => 'yes',
223 'class' => 'ui-identifier',
224 );
225 $this->form_fields['ui_mode'] = array(
226 'title' => __('Display MC payment channels as', 'wc_makecommerce_domain'),
227 'type' => 'mc_hidden',
228 'default' => 'widget',
229 'options' => array(
230 'inline' => __('List', 'wc_makecommerce_domain'),
231 'widget' => __('Grouped to widget', 'wc_makecommerce_domain'),
232 ),
233 'class' => 'ui-identifier',
234 );
235
236
237 if (empty($languages)) {
238 $this->form_fields['ui_widget_title'] = array(
239 'title' => __('Payments widget title', 'wc_makecommerce_domain'),
240 'type' => 'text',
241 'desc_tip' => __("Appropriate title may depend on the configuration you have made, i.e. 'pay with bank-link or credit card', 'pay with bank-links' or 'payment methods'", 'wc_makecommerce_domain'),
242 'default' => __('Pay with bank-links or credit card', 'wc_makecommerce_domain'),
243 'class' => 'ui-identifier',
244 );
245 } else {
246 foreach ($languages as $language_code => $language) {
247 $language_name = !empty($language['translated_name']) ? $language['translated_name'] : $language_code;
248 $this->form_fields['ui_widget_title_'.$language_code] = array(
249 'title' => __('Payments widget title', 'wc_makecommerce_domain').sprintf(' (%s)', $language_code),
250 'type' => 'text',
251 'desc_tip' => __("Appropriate title may depend on the configuration you have made, i.e. 'pay with bank-link or credit card', 'pay with bank-links' or 'payment methods'", 'wc_makecommerce_domain'),
252 'default' => __('Pay with bank-links or credit card', 'wc_makecommerce_domain'),
253 'class' => 'ui-identifier',
254 );
255 }
256 }
257 $this->form_fields['ui_inline_uselogo'] = array(
258 'title' => __('MC payment channels display style', 'wc_makecommerce_domain'),
259 'type' => 'select',
260 'default' => 'logo',
261 'options' => array(
262 'logo' => __('Logo', 'wc_makecommerce_domain'),
263 'text_logo' => __('Text & logo', 'wc_makecommerce_domain'),
264 'text' => __('Text', 'wc_makecommerce_domain'),
265 ),
266 'class' => 'ui-identifier',
267 );
268 $this->form_fields['ui_widget_logosize'] = array(
269 'title' => __('Size of payment channel logos', 'wc_makecommerce_domain'),
270 'type' => 'select',
271 'default' => 'medium',
272 'options' => array(
273 'small' => __('Small', 'wc_makecommerce_domain'),
274 'medium' => __('Medium', 'wc_makecommerce_domain'),
275 'large' => __('Large', 'wc_makecommerce_domain')
276 ),
277 'class' => 'ui-identifier',
278 );
279 $this->form_fields['ui_widget_groupcountries'] = array(
280 'title' => __('Group bank-links by countries', 'wc_makecommerce_domain'),
281 'type' => 'mc_hidden',
282 'default' => 'no',
283 'class' => 'ui-identifier',
284 );
285 $this->form_fields['ui_widget_countryselector'] = array(
286 'title' => __('Country selector style', 'wc_makecommerce_domain'),
287 'type' => 'mc_hidden',
288 'default' => 'flag',
289 'options' => array(
290 'flag' => __('Flag', 'wc_makecommerce_domain'),
291 'dropdown' => __('Dropdown', 'wc_makecommerce_domain'),
292 ),
293 'class' => 'ui-identifier',
294 );
295 $this->form_fields['ui_widget_groupcc'] = array(
296 'title' => __('Group credit card into separate widget', 'wc_makecommerce_domain'),
297 'type' => 'mc_hidden',
298 'default' => 'no',
299 'class' => 'ui-identifier',
300 );
301 $this->form_fields['ui_chorder'] = array(
302 'title' => __('Define custom order of payment channels', 'wc_makecommerce_domain'),
303 'type' => 'text',
304 'desc_tip' => __('If you want to change default order, put here comma separated list of channels. i,e, - seb,lhv,swedbank. see more on the module home page (link above)', 'wc_makecommerce_domain'),
305 'class' => 'ui-identifier',
306 );
307 $this->form_fields['ui_javascript'] = array(
308 'type' => 'ui_javascript',
309 );
310 $this->form_fields['cc_title'] = array(
311 'title' => '<br>'.__('Credit Card Settings', 'wc_makecommerce_domain'),
312 'type' => 'title',
313 );
314 $this->form_fields['cc_pass_cust_data'] = array(
315 'title' => __('Prefill Credit Card form with customer data', 'wc_makecommerce_domain'),
316 'type' => 'checkbox',
317 'default' => 'yes',
318 'desc_tip' => __('It will pass user Name and e-mail address to the Credit Card dialog to make the form filling easier', 'wc_makecommerce_domain'),
319 );
320 $this->form_fields['cc_shop_name'] = array(
321 'title' => __('Shop name on credit card payment', 'wc_makecommerce_domain'),
322 'type' => 'text',
323 'desc_tip' => __('This will appear on buyer\'s credit card transaction', 'wc_makecommerce_domain'),
324 );
325 $this->form_fields['cc_cart_reference_string'] = array(
326 'title' => __('Order reference on credit card payment', 'wc_makecommerce_domain'),
327 'type' => 'text',
328 'default' => 'Order %s',
329 'desc_tip' => __('This will appear on buyer\'s credit card transaction where "%s" is the order number', 'wc_makecommerce_domain'),
330 );
331 $this->form_fields['adv_title'] = array(
332 'title' => '<br>'.__('Advanced Settings', 'wc_makecommerce_domain'),
333 'type' => 'title',
334 );
335 $this->form_fields['reload_links'] = array(
336 'type' => 'mc_banklinks_reload',
337 'title' => __('Update payment methods', 'wc_makecommerce_domain'),
338 'description' => __('Update', 'wc_makecommerce_domain'),
339 'desc_tip' => __('This will update shop configuration from MakeCommerce servers.', 'wc_makecommerce_domain'),
340 );
341 }
342
343 public function generate_mc_hidden_html( $key, $data ) {
344 $field_key = $this->get_field_key($key);
345 ob_start();
346 ?>
347 <tr style="display: none;">
348 <td colspan="2">
349 <input type="hidden" name="<?php echo $field_key; ?>" value="<?php echo $data['default']; ?>" />
350 </td>
351 </tr>
352 <?php
353 return ob_get_clean();
354 }
355
356 public function generate_mc_banklinks_reload_html( $key, $data ) {
357
358 $field = $this->get_field_key( $key );
359 $defaults = array(
360 'title' => '',
361 'disabled' => false,
362 'class' => '',
363 'css' => '',
364 'placeholder' => '',
365 'type' => 'text',
366 'desc_tip' => false,
367 'description' => '',
368 'custom_attributes' => array()
369 );
370
371 $data = wp_parse_args( $data, $defaults );
372
373 ob_start();
374 ?>
375 <tr valign="top">
376 <th scope="row" class="titledesc">
377 <label for="<?php echo esc_attr( $field ); ?>"><?php echo wp_kses_post( $data['title'] ); ?></label>
378 <?php echo $this->get_tooltip_html( $data ); ?>
379 </th>
380 <td class="forminp">
381 <fieldset>
382 <legend class="screen-reader-text"><span><?php echo wp_kses_post( $data['title'] ); ?></span></legend>
383 <input id="mc_banklinks_reload" class="button <?php echo esc_attr( $data['class'] ); ?>" type="button" name="<?php echo esc_attr( $field ); ?>" id="<?php echo esc_attr( $field ); ?>" style="<?php echo esc_attr( $data['css'] ); ?>" value="<?php echo esc_attr( $data['description'] ); ?>" placeholder="<?php echo esc_attr( $data['placeholder'] ); ?>" <?php disabled( $data['disabled'], true ); ?> <?php echo $this->get_custom_attribute_html( $data ); ?> />
384 <script type="text/javascript">
385 jQuery('input#mc_banklinks_reload').on('click', function() {
386 init_mc_loading();
387 jQuery.ajax({
388 url: '<?php echo get_site_url(); ?>/wp-admin/admin-ajax.php',
389 type: 'POST',
390 data: 'action=mc_banklinks_reload',
391 success: function (output) {
392 if(output.data) {
393 alert(output.data);
394 } else {
395 alert('<?php echo __('There was an error with your update. Please try again.', 'wc_makecommerce_domain'); ?>');
396 }
397 },
398 complete: function() { stop_mc_loading(); }
399 });
400 });
401
402 function init_mc_loading() {
403 jQuery('input#mc_banklinks_reload').attr('disabled', 'disabled');
404 }
405
406 function stop_mc_loading() {
407 jQuery('input#mc_banklinks_reload').removeAttr('disabled');
408 }
409 </script>
410 </fieldset>
411 </td>
412 </tr>
413 <?php
414
415 return ob_get_clean();
416 }
417
418 public function generate_ui_javascript_html( $key, $data ) {
419 ?>
420 <script type="text/javascript">
421 jQuery(document).ready(function($) {
422
423 var api_type = $('#woocommerce_<?php echo $this->id; ?>_api_type');
424
425 var ui_inline_uselogo_row = $('#woocommerce_<?php echo $this->id; ?>_ui_inline_uselogo').closest('tr');
426 var ui_widget_title_row = $('#woocommerce_<?php echo $this->id; ?>_ui_widget_title').closest('tr');
427 var ui_widget_logosize_row = $('#woocommerce_<?php echo $this->id; ?>_ui_widget_logosize').closest('tr');
428 var ui_widget_countryselector_row = $('#woocommerce_<?php echo $this->id; ?>_ui_widget_countryselector').closest('tr');
429 var ui_widget_groupcountries_row = $('#woocommerce_<?php echo $this->id; ?>_ui_widget_groupcountries').closest('tr');
430 var ui_widget_groupcc_row = $('#woocommerce_<?php echo $this->id; ?>_ui_widget_groupcc').closest('tr');
431 var ui_widget_groupcc_title_row = $('#woocommerce_<?php echo $this->id; ?>_ui_widget_groupcc_title').closest('tr');
432
433 var ui_mode = $('#woocommerce_<?php echo $this->id; ?>_ui_mode');
434 var ui_widget_groupcountries = $('#woocommerce_<?php echo $this->id; ?>_ui_widget_groupcountries');
435 var ui_widget_groupcc = $('#woocommerce_<?php echo $this->id; ?>_ui_widget_groupcc');
436
437 parseVisibility();
438 function parseVisibility() {
439 $('.ui-identifier').closest('tr').show();
440
441 if(api_type.val() == 'live') {
442 $('.mc-test-link').hide();
443 $('.api-test').closest('tr').hide();
444 } else {
445 $('.mc-test-link').show();
446 $('.api-live').closest('tr').hide();
447 }
448
449 if(ui_mode.val() == 'inline') {
450 ui_widget_title_row.hide();
451 ui_widget_logosize_row.hide();
452 ui_widget_countryselector_row.hide();
453 ui_widget_groupcountries_row.hide();
454 ui_widget_groupcc_row.hide();
455 ui_widget_groupcc_title_row.hide();
456 } else {
457 ui_inline_uselogo_row.hide();
458
459 if(ui_widget_groupcountries.prop('checked')) {
460 ui_widget_countryselector_row.hide();
461 }
462 if(!ui_widget_groupcc.prop('checked')) {
463 ui_widget_groupcc_title_row.hide();
464 }
465 }
466 }
467
468 api_type.on('change', parseVisibility);
469 ui_mode.on('change', parseVisibility);
470 ui_widget_groupcountries.on('change', parseVisibility);
471 ui_widget_groupcc.on('change', parseVisibility);
472
473 });
474 </script>
475 <?php
476 }
477
478 public function generate_mc_header_html( $key, $data ) {
479 ?>
480 <div class="makecommerce-info">
481 <div class="makecommerce-logo">
482 <a target="_blank" href="http://makecommerce.net"><img src="<?php echo plugins_url('/images/makecommerce_logo_en.svg', __FILE__); ?>" class="makecommerce-logo"></a>
483 </div>
484 <div class="makecommerce-links">
485 <div class="makecommerce-link"><a target="_blank" href="https://merchant.maksekeskus.ee">Merchant Portal</a></div>
486 <div class="makecommerce-link"><a target="_blank" href="https://makecommerce.net/">makecommerce.net</a></div>
487 <div class="makecommerce-link"><a target="_blank" href="http://maksekeskus.ee">maksekeskus.ee</a></div>
488 </div>
489 </div>
490 <?php
491 }
492
493 public function mc_banklinks_reload($force = false) {
494 if ($force || (defined('DOING_AJAX') && DOING_AJAX)) {
495
496 global $wpdb;
497 global $mkDbTable;
498 global $wp_version;
499
500 $tableName = $wpdb->prefix . $mkDbTable;
501 $wpdb->query('TRUNCATE TABLE '.$tableName);
502
503 $request_params = array(
504 'environment' => json_encode(array(
505 'platform' => 'wordpress '.$wp_version,
506 'module' => $this->id.' '.$this->version,
507 )),
508 );
509
510 if (!$this->_api) {
511 return false;
512 }
513
514 try {
515 $methods = $this->_api->getShopConfig($request_params)->payment_methods;
516 } catch (Exception $e) {
517 error_log(print_r($e, 1));
518 return false;
519 }
520 if(isset($methods->banklinks)) {
521 foreach($methods->banklinks as $method) {
522 $wpdb->insert($tableName, array('type' => 'banklink', 'country' => $method->country, 'name' => $method->name, 'url' => $method->url));
523 }
524 $updated = true;
525 }
526
527 if(isset($methods->cards)) {
528 foreach($methods->cards as $method) {
529 $wpdb->insert($tableName, array('type' => 'card', 'name' => $method->name));
530 }
531 $updated = true;
532 }
533 if ($updated) {
534 update_option( 'mc_banklinks_api_type', get_option('mk_api_type', false) );
535 }
536 if ($force) {
537 $this->initBankLinks();
538 return $updated;
539 }
540
541 if($updated) {
542 wp_send_json(array('success' => 1, 'data' => __('Update successfully completed!', 'wc_makecommerce_domain')));
543 exit;
544 }
545
546 wp_send_json(array('success' => 0, 'data' => __('There was an error with your update. Please try again.', 'wc_makecommerce_domain')));
547 exit;
548 }
549 die();
550 }
551
552 protected function _getWooCommerce() {
553 global $woocommerce;
554 return $woocommerce;
555 }
556
557 function is_valid_for_use() {
558 return true;
559 }
560
561 public function is_available() {
562 if ($this->settings['active'] == "yes") {
563 return true;
564 }
565 }
566
567 function payment_fields() {
568
569 if($this->settings['ui_mode'] == 'inline') {
570
571 ?>
572 <ul class="makecommerce-picker">
573 <?php foreach($this->_banklinks as $method): ?>
574 <li class="makecommerce-picker-method">
575 <input type="radio" id="makecommerce_method_picker_<?php echo $method->country.'_'.$method->name; ?>" name="PRESELECTED_METHOD_<?php echo $this->id; ?>" value="<?php echo $method->country.'_'.$method->name; ?>"/>
576 <label for="makecommerce_method_picker_<?php echo $method->country.'_'.$method->name; ?>">
577 <span class="makecommerce-method-title"><?php if(in_array($this->settings['ui_inline_uselogo'], array('text', 'text_logo'))) { echo ucfirst($method->name); if(count($this->_banklinks_grouped) > 1) { echo ' ('.$this->getCountryName($method->country).')'; } } ?></span>
578 <?php if(in_array($this->settings['ui_inline_uselogo'], array('logo', 'text_logo'))) : ?>
579 <div><img src="<?php echo $this->getImageUrl($method->name); ?>" title="<?php echo ucfirst($method->name); ?>" /></div>
580 <?php endif; ?>
581 </label>
582 </li>
583 <?php endforeach; ?>
584 <?php foreach($this->_cards as $method): ?>
585 <li class="makecommerce-picker-method">
586 <input type="radio" id="makecommerce_method_picker_<?php echo 'card_'.$method->name; ?>" name="PRESELECTED_METHOD_<?php echo $this->id; ?>" value="<?php echo 'card_'.$method->name; ?>"/>
587 <label for="makecommerce_method_picker_<?php echo 'card_'.$method->name; ?>">
588 <span class="makecommerce-method-title"><?php if(in_array($this->settings['ui_inline_uselogo'], array('text', 'text_logo'))) { echo ucfirst($method->name); } ?></span>
589 <?php if(in_array($this->settings['ui_inline_uselogo'], array('logo', 'text_logo'))) : ?>
590 <div><img src="<?php echo $this->getImageUrl($method->name); ?>" title="<?php echo ucfirst($method->name); ?>" /></div>
591 <?php endif; ?>
592 </label>
593 </li>
594 <?php endforeach; ?>
595 </ul>
596 <?php
597
598 } else {
599 ?>
600 <select id="<?php echo $this->id; ?>" name="PRESELECTED_METHOD_<?php echo $this->id; ?>">
601 <option value=""></option>
602 <?php foreach($this->_banklinks as $method): ?>
603 <option value="<?php echo $method->country.'_'.$method->name; ?>"><?php echo strtoupper($method->country).' - '.ucfirst($method->name); ?></option>
604 <?php endforeach; ?>
605 <?php foreach($this->_cards as $method): ?>
606 <option value="card_<?php echo $method->name; ?>"><?php echo ucfirst($method->name); ?></option>
607 <?php endforeach; ?>
608 </select>
609 <ul class="makecommerce-picker">
610 <?php
611
612 if($this->_banklinks) {
613 $defaultCountry = $this->getDefaultCountry();
614 ?>
615 <?php if(empty($this->settings['ui_widget_groupcountries']) || $this->settings['ui_widget_groupcountries'] == 'no') : ?>
616 <div class="makecommerce_country_picker_countries">
617 <?php foreach(array_keys($this->_banklinks_grouped) as $country): ?>
618 <input style="display: none;" type="radio" id="makecommerce_country_picker_<?php echo $country; ?>" name="makecommerce_country_picker" value="<?php echo $country; ?>" <?php if($defaultCountry == $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" style="background-image: url(<?php echo plugins_url('/images/'.$country.'32.png', __FILE__); ?>);"></label><?php } ?>
619 <?php endforeach; ?>
620 <?php if($this->settings['ui_widget_countryselector'] == 'dropdown') : ?>
621 <select name="makecommerce_country_picker_select">
622 <?php foreach(array_keys($this->_banklinks_grouped) as $country): ?>
623 <option value="<?php echo $country; ?>" <?php if($defaultCountry == $country) echo 'selected="selected" '; ?>><?php echo $this->getCountryName($country); ?></option>
624 <?php endforeach; ?>
625 <option value="card" style="display:none;"></option>
626 </select>
627 <?php endif; ?>
628 </div>
629 <?php endif; ?>
630 <?php foreach($this->_banklinks_grouped as $country => $methods): ?>
631 <li class="makecommerce-picker-country">
632 <?php if($this->settings['ui_widget_groupcountries'] == 'yes') : ?>
633 <input type="radio" id="makecommerce_country_picker_<?php echo $country; ?>" name="makecommerce_country_picker" value="<?php echo $country; ?>" <?php if($defaultCountry == $country) echo 'checked="checked" '; ?>/><label for="makecommerce_country_picker_<?php echo $country; ?>"><img src="<?php echo plugins_url('/images/'.$country.'32.png', __FILE__); ?>" /></label>
634 <?php endif; ?>
635 <div class="makecommerce_country_picker_methods" id="makecommerce_country_picker_methods_<?php echo $country; ?>">
636 <?php foreach($methods as $method): ?>
637 <div class="makecommerce-banklink-picker" banklink_id="<?php echo $method->country.'_'.$method->name; ?>">
638 <img src="<?php echo $this->getImageUrl($method->name); ?>" title="<?php echo ucfirst($method->name); ?>" />
639 </div>
640 <?php endforeach; ?>
641 <?php if($this->_cards && $this->settings['ui_widget_groupcc'] == 'no') : ?>
642 <div class="breaker"></div>
643 <?php foreach($this->_cards as $method): ?>
644 <div class="makecommerce-banklink-picker" banklink_id="card_<?php echo $method->name; ?>">
645 <img src="<?php echo $this->getImageUrl($method->name); ?>" title="<?php echo ucfirst($method->name); ?>" />
646 </div>
647 <?php endforeach; ?>
648 <?php endif; ?>
649 </div>
650 </li>
651 <?php endforeach; ?>
652 <?php if($this->_cards && $this->settings['ui_widget_groupcc'] == 'yes') : ?>
653 <li class="makecommerce-picker-country">
654 <input type="radio" id="makecommerce_country_picker_card" name="makecommerce_country_picker" value="card"/><label for="makecommerce_country_picker_card"><?php echo $this->settings['ui_widget_groupcc_title']; ?></label>
655 <div class="makecommerce_country_picker_methods" id="makecommerce_country_picker_methods_card">
656 <?php foreach($this->_cards as $method): ?>
657 <div class="makecommerce-banklink-picker" banklink_id="card_<?php echo $method->name; ?>">
658 <img src="<?php echo $this->getImageUrl($method->name); ?>" title="<?php echo ucfirst($method->name); ?>" />
659 </div>
660 <?php endforeach; ?>
661 </div>
662 </li>
663 <?php endif; ?>
664 <?php
665 }
666 ?>
667 </ul>
668 <script type="text/javascript">
669 var makecommerceId = '<?php echo $this->id; ?>';
670 var selectedCountry = jQuery('input[name=makecommerce_country_picker]:checked').val();
671
672 var logosize = '<?php echo $this->settings['ui_widget_logosize']; ?>';
673 jQuery('div.makecommerce_country_picker_methods').addClass('logosize-'+logosize);
674
675 <?php if(count($this->_banklinks_grouped) == 1): ?>
676 jQuery('div.makecommerce_country_picker_methods').show();
677 jQuery('div.makecommerce_country_picker_countries').hide();
678 jQuery('li.makecommerce-picker-country > input, li.makecommerce-picker-country > label').hide();
679 <?php else: ?>
680 makecommercePick();
681
682 jQuery('body').on('change', 'select[name=makecommerce_country_picker_select]', function() {
683 selectedCountry = jQuery(this).val();
684 jQuery('input[name=makecommerce_country_picker]').removeAttr('checked');
685 makecommercePick();
686 });
687 jQuery('body').on('change', 'input[name=makecommerce_country_picker]', function() {
688 selectedCountry = jQuery(this).val();
689 jQuery('select[name=makecommerce_country_picker_select]').val(selectedCountry);
690 makecommercePick();
691 });
692
693 function makecommercePick() {
694 jQuery('select#'+makecommerceId).val('');
695 jQuery('div.makecommerce-banklink-picker').removeClass('selected');
696 jQuery('label.makecommerce_country_picker_label').removeClass('selected');
697
698 jQuery('div.makecommerce_country_picker_methods').hide();
699 jQuery('div#makecommerce_country_picker_methods_' + selectedCountry).show();
700 jQuery('label[for=makecommerce_country_picker_' + selectedCountry + ']').addClass('selected');
701 }
702 <?php endif; ?>
703
704 jQuery('div.makecommerce-banklink-picker').on('click', function() {
705 var banklink_id = jQuery(this).attr('banklink_id');
706 jQuery('select#'+makecommerceId).val(banklink_id);
707
708 jQuery('div.makecommerce-banklink-picker').removeClass('selected');
709 jQuery(this).addClass('selected');
710 });
711 </script>
712 <?php
713 }
714
715 if($this->settings['ui_open_by_default'] == 'yes') {
716 ?>
717 <script type="text/javascript">
718 jQuery('input#payment_method_<?php echo $this->id; ?>').trigger('click');
719 </script>
720 <?php
721 }
722 }
723
724 private function getDefaultCountry() {
725 if ($this->_getWooCommerce()->customer) {
726 $customerCountry = strtolower($this->_getWooCommerce()->customer->get_shipping_country());
727 if(array_key_exists($customerCountry, $this->_banklinks_grouped)) {
728 return $customerCountry;
729 }
730 }
731
732 $localeToCountry = array(
733 'et' => 'ee',
734 'lv' => 'lv',
735 'lt' => 'lt',
736 'fi' => 'fi',
737 );
738 if(array_key_exists(get_locale(), $localeToCountry)) {
739 return $localeToCountry[get_locale()];
740 }
741
742 return key($this->_banklinks_grouped);
743 }
744
745 private function initBanklinks() {
746 global $wpdb;
747 global $mkDbTable;
748 $this->_banklinks = array();
749
750 $tableName = $wpdb->prefix . $mkDbTable;
751 $methods = $wpdb->get_results('SELECT * FROM '.$tableName);
752
753 if(count($methods)) {
754 if(is_admin() && $this->_init == true && get_option( 'mc_banklinks_api_type' ) != get_option('mk_api_type', false)) {
755 //add_action( 'admin_notices', array(&$this, 'makecommerce_banklinks_list_type_notice') );
756 }
757 $banklinks = array();
758 $banklinks_grouped = array();
759 $cards = array();
760 foreach($methods as $method) {
761 if($method->type == 'banklink') {
762 $banklinks[] = $banklinks_grouped[$method->country][] = $method;
763 } elseif($method->type == 'card') {
764 $cards[] = $method;
765 }
766 }
767
768 usort($banklinks, array($this, 'orderBanklinks'));
769 foreach($banklinks_grouped as &$country) {
770 usort($country, array($this, 'orderBanklinks'));
771 }
772
773 $this->_banklinks = $banklinks;
774 $this->_banklinks_grouped = $banklinks_grouped;
775 $this->_cards = $cards;
776 remove_action('admin_notices', array(&$this, 'makecommerce_banklinks_list_empty'), 30);
777 } elseif(is_admin() && $this->_init == true) {
778 add_action('admin_notices', array(&$this, 'makecommerce_banklinks_list_empty'), 30);
779 }
780 }
781
782 private function orderBanklinks($a, $b) {
783 $order = array_map('trim', explode(",", $this->settings['ui_chorder']));
784
785 $posA = array_search($a->name, $order);
786 $posB = array_search($b->name, $order);
787
788 if($posA === $posB) return $a->id > $b->id ? 1 : -1;
789 if($posA === FALSE) return 1;
790 if($posB === FALSE) return -1;
791
792 return $posA > $posB ? 1 : -1;
793 }
794
795 protected function getImageUrl($methodName) {
796 $imageUrlPath = 'https://static.maksekeskus.ee/img/channel/lnd/';
797
798 return $imageUrlPath.$methodName.'.png';
799 }
800
801 public function validate_fields() {
802 $selected = isset($_POST['PRESELECTED_METHOD_' . $this->id]) ? sanitize_text_field($_POST['PRESELECTED_METHOD_' . $this->id]) : false;
803
804 if (!$selected) {
805 wc_add_notice(__('Please select suitable payment option!', 'wc_makecommerce_domain'), 'error');
806 } else {
807 $this->_getWooCommerce()->session->makecommerce_preselected_method = $selected;
808 }
809
810 return true;
811 }
812
813 function process_payment($orderId) {
814
815 $order = new WC_Order($orderId);
816
817 $selected = isset($_POST['PRESELECTED_METHOD_' . $this->id]) ? sanitize_text_field($_POST['PRESELECTED_METHOD_' . $this->id]) : false;
818
819 if(!empty($selected)) {
820
821 update_post_meta($order->id, '_makecommerce_preselected_method', $selected);
822
823 if(substr($selected, 0, 5) == 'card_') {
824
825 $request_body = array(
826 'transaction' => array(
827 'amount' => round($order->order_total, 2),
828 'currency' => $order->get_order_currency(),
829 'reference' => $order->id,
830 'transaction_url' => array(
831 'return_url' => array(
832 'url' => $this->return_url_cc,
833 'method' => 'POST',
834 ),
835 'cancel_url' => array(
836 'url' => $this->return_url_cc,
837 'method' => 'POST',
838 ),
839 'notification_url' => array(
840 'url' => $this->return_url_cc,
841 'method' => 'POST',
842 ),
843 ),
844 ),
845 'customer' => array(
846 'ip' => $_SERVER['REMOTE_ADDR'],
847 'country' => strtolower($order->billing_country),
848 'locale' => strtolower(substr(get_locale(), 0, 2)),
849 ),
850 );
851 $transaction = $this->_api->createTransaction($request_body);
852
853 if(isset($transaction->id)) {
854 update_post_meta($order->id, '_makecommerce_cc_transaction_id', $transaction->id);
855 return array(
856 'result' => 'success',
857 'redirect' => $this->_getOrderConfirmationUrl($order),
858 );
859 }
860
861 wc_add_notice(__('An error occured when trying to process payment!', 'wc_makecommerce_domain'), 'error');
862 return array(
863 'result' => 'failure',
864 );
865
866 } else {
867
868 $redirectUrl = $this->_getRedirectUrl($selected);
869
870 if($redirectUrl) {
871 $request_body = array(
872 'transaction' => array(
873 'amount' => round($order->order_total, 2),
874 'currency' => $order->get_order_currency(),
875 'reference' => $order->id,
876 'transaction_url' => array(
877 'return_url' => array(
878 'url' => $this->return_url,
879 'method' => 'POST',
880 ),
881 'cancel_url' => array(
882 'url' => $this->return_url,
883 'method' => 'POST',
884 ),
885 'notification_url' => array(
886 'url' => $this->return_url,
887 'method' => 'POST',
888 ),
889 ),
890 ),
891 'customer' => array(
892 'ip' => $_SERVER['REMOTE_ADDR'],
893 'country' => strtolower($order->billing_country),
894 'locale' => strtolower(substr(get_locale(), 0, 2)),
895 )
896 );
897 $transaction = $this->_api->createTransaction($request_body);
898
899 if(isset($transaction->id)) {
900 return array(
901 'result' => 'success',
902 'redirect' => $redirectUrl.$transaction->id,
903 );
904 }
905 }
906
907 wc_add_notice(__('An error occured when trying to process payment!', 'wc_makecommerce_domain'), 'error');
908 return array(
909 'result' => 'failure',
910 );
911
912 }
913
914 }
915
916 wc_add_notice(__('An error occured when trying to process payment!', 'wc_makecommerce_domain'), 'error');
917 return array(
918 'result' => 'failure',
919 );
920 }
921
922 protected function _getRedirectUrl($selected) {
923 foreach($this->_banklinks as $method) {
924 if($selected == $method->country.'_'.$method->name)
925 return $method->url;
926 }
927
928 return false;
929 }
930
931 protected function _getOrderConfirmationUrl($order) {
932 //$url = add_query_arg('order', $order->id, add_query_arg('key', $order->order_key, get_permalink(get_option('woocommerce_pay_page_id'))));
933 $url = site_url('?makecommerce_card_pay=1&order_id='.$order->id.'&lang='.(defined('ICL_LANGUAGE_CODE') ? ICL_LANGUAGE_CODE : strtolower(substr(get_locale(), 0, 2))));
934 return $url;
935 }
936
937 function receipt_page($orderId) {
938 echo '<p>' . __('Thank you for the order, please click on the button to start the payment.', 'wc_makecommerce_domain') . '</p>';
939
940 $order = new WC_Order($orderId);
941 if(substr(get_post_meta($orderId, '_makecommerce_preselected_method', true), 0, 5) == 'card_' && $order->get_status() == 'pending') {
942 echo $this->generateCardForm($order);
943 }
944 }
945
946 function generateCardForm($order) {
947 $scriptSrc = htmlspecialchars($this->_gateway_url_static.'checkout.js');
948 $transactionId = get_post_meta($order->id, '_makecommerce_cc_transaction_id', true);
949 $idReference = $order->id;
950 $jsParams = array(
951 'key' => $this->_api->getPublishableKey(),
952 'transaction' => $transactionId,
953 'selector' => '#submit_banklinkmakecommerce_payment_form',
954 'amount' => round($order->order_total, 2),
955 'locale' => !empty($_GET['lang']) ? $_GET['lang'] : (defined('ICL_LANGUAGE_CODE') ? ICL_LANGUAGE_CODE : strtolower(substr(get_locale(), 0, 2))),
956 'open-on-load' => 'true',
957 'client-name' => ($this->settings['cc_pass_cust_data'] == 'yes' ? (string) ($order->billing_first_name . ' ' . $order->billing_last_name) : ''),
958 'email' => ($this->settings['cc_pass_cust_data'] == 'yes' ? (string) $order->billing_email : ''),
959 'name' => $this->settings['cc_shop_name'],
960 'description' => (string)sprintf($this->settings['cc_cart_reference_string'], (string) $idReference),
961 'completed' => 'makecommerce_cc_complete',
962 'currency' => 'EUR',
963 );
964 ?>
965 <script type="text/javascript">
966 function makecommerce_cc_complete(data) {
967 if(data.paymentToken) {
968 jQuery('div.mc-processing-message').show();
969
970 var submitform = jQuery('<form action="<?php echo $this->return_url_cc; ?>" method="POST" style="display: none;"><input type="submit"/><input type="hidden" name="transaction" value="<?php echo $transactionId; ?>" /></form>');
971 for(var key in data) {
972 submitform.append('<input type="hidden" name="'+key+'" value="'+data[key]+'" />');
973 }
974 jQuery('body').append(submitform);
975 submitform.submit();
976 }
977 }
978 </script>
979 <form id="cc_form">
980 <input type="submit" class="button-alt" id="submit_banklinkmakecommerce_payment_form" value="<?php echo __('Pay', 'wc_makecommerce_domain'); ?>" />
981 <a class="button cancel" href="<?php echo esc_url($order->get_cancel_order_url()); ?>"><?php echo __('Cancel order &amp; restore cart', 'wc_makecommerce_domain'); ?></a>
982 <script type="text/javascript" src="<?php echo $scriptSrc; ?>" <?php echo $this->_toHtmlAttributes($jsParams); ?>></script>
983 </form>
984 <div class="mc-processing-message"><img src="<?php echo plugins_url('/images/loading.png', __FILE__); ?>"/> <?php echo __('Please wait, processing payment...', 'wc_makecommerce_domain'); ?></div>
985 <?php
986 }
987
988 protected function _toHtmlAttributes($input) {
989 $result = array();
990 foreach ($input as $key => $value) {
991 $result[] = 'data-' . htmlspecialchars($key) . '=' . '"' . htmlspecialchars($value) . '"';
992 }
993 return implode(' ', $result);
994 }
995
996 function makecommerce_return_trigger($vars) {
997 $vars[] = 'makecommerce_return';
998 $vars[] = 'makecommerce_card_pay';
999 return $vars;
1000 }
1001
1002 function makecommerce_return_trigger_check() {
1003 if(intval(get_query_var('makecommerce_return')) == 1) {
1004
1005 $returnUrl = home_url();
1006
1007 $request = stripslashes_deep($_POST);
1008 if($this->_api->verifySignature($request)) {
1009 $data = $this->_api->extractRequestData($request);
1010 $order = new WC_Order($data['reference']);
1011
1012 switch($data['status']) {
1013 case self::MC_CANCELLED:
1014 $order->update_status( 'cancelled' );
1015 wc_add_notice(__('Payment transaction cancelled', 'wc_makecommerce_domain'), 'error');
1016 $returnUrl = $this->_getWooCommerce()->cart->get_cart_url();
1017 break;
1018 case self::MC_COMPLETED:
1019 if($this->validate_completed_payment($order, $data)) {
1020 $orderNote = array();
1021 $orderNote[] = __('Transaction ID', 'wc_makecommerce_domain') . ': ' . $data['transaction'];
1022 $orderNote[] = __('Payment option', 'wc_makecommerce_domain') . ': ' . get_post_meta($order->id, '_makecommerce_preselected_method', true);
1023
1024 $order->add_order_note(implode("\r\n", $orderNote));
1025
1026 $order->payment_complete($data['transaction']);
1027 $order->update_status( 'processing' );
1028
1029 try {
1030 @ob_start();
1031 $this->receipt_page($order->id);
1032 @ob_end_clean();
1033
1034 $returnUrl = $this->get_return_url($order);
1035 } catch (Exception $ex) {
1036 @ob_end_clean();
1037 }
1038 } else {
1039 $returnUrl = $this->_getWooCommerce()->cart->get_cart_url();
1040 }
1041 break;
1042 }
1043
1044 //exit;
1045 }
1046 wp_redirect($returnUrl);
1047 exit;
1048
1049 } elseif(intval(get_query_var('makecommerce_return')) == 2) {
1050
1051 $returnUrl = home_url();
1052
1053 if(isset($_POST['paymentToken']) && isset($_POST['transaction'])) {
1054 $token = $_POST['paymentToken'];
1055 $transaction = $_POST['transaction'];
1056 }
1057
1058 if(isset($_POST['json'])) {
1059 $data = json_decode(stripslashes($_POST['json']), true);
1060 $token = $data['token']['id'];
1061 $transaction = $data['transaction']['id'];
1062 }
1063
1064 if(!empty($token) && !empty($transaction)) {
1065 global $wpdb;
1066 if($order = New WC_Order($wpdb->get_var('SELECT post_id from '.$wpdb->postmeta.' where meta_key = "_makecommerce_cc_transaction_id" AND meta_value = "'.$transaction.'"'))) {
1067 $request_body = array(
1068 'token' => $token,
1069 );
1070 $data = $this->_api->createPayment(get_post_meta($order->id, '_makecommerce_cc_transaction_id', true), $request_body);
1071 switch($data->status) {
1072 case self::MC_CANCELLED:
1073 $order->update_status( 'cancelled' );
1074 wc_add_notice(__('Payment transaction cancelled', 'wc_makecommerce_domain'), 'error');
1075 $returnUrl = $this->_getWooCommerce()->cart->get_cart_url();
1076 break;
1077 case self::MC_DEPOSITED:
1078 if($this->validate_completed_payment($order, $data)) {
1079 $orderNote = array();
1080 $orderNote[] = __('Transaction ID', 'wc_makecommerce_domain') . ': ' . $data->transaction->id;
1081 $orderNote[] = __('Payment option', 'wc_makecommerce_domain') . ': ' . get_post_meta($order->id, '_makecommerce_preselected_method', true);
1082
1083 $order->add_order_note(implode("\r\n", $orderNote));
1084
1085 $order->payment_complete($data->transaction->id);
1086 $order->update_status( 'processing' );
1087
1088 try {
1089 @ob_start();
1090 $this->receipt_page($order->id);
1091 @ob_end_clean();
1092
1093 $returnUrl = $this->get_return_url($order);
1094 } catch (Exception $ex) {
1095 @ob_end_clean();
1096 }
1097 } else {
1098 $returnUrl = $this->_getWooCommerce()->cart->get_cart_url();
1099 }
1100 break;
1101 }
1102 }
1103 }
1104
1105 wp_redirect($returnUrl);
1106 exit;
1107 }
1108
1109 if(intval(get_query_var('makecommerce_card_pay')) == 1) {
1110 if(isset($_GET['order_id'])) {
1111 if($order = New WC_Order($_GET['order_id'])) {
1112 New woocommerce_makecommerce(false);
1113 get_header();
1114 ?>
1115 <div id="primary" class="content-area">
1116 <?php $this->receipt_page($order->id); ?>
1117 </div>
1118 <?php
1119 get_sidebar();
1120 get_footer();
1121
1122 exit;
1123 }
1124 }
1125 }
1126 }
1127
1128 private function validate_completed_payment($order, $data) {
1129
1130 if(is_object($data)) {
1131 $data = json_decode(json_encode($data), true);
1132 }
1133
1134 if(empty($data['transaction'])) {
1135 $order->add_order_note(__('Payment error, missing transaction id', 'wc_makecommerce_domain'));
1136 wc_add_notice(__('Error verifying transaction', 'wc_makecommerce_domain'), 'error');
1137 return false;
1138 }
1139 if($data['amount'] != round($order->order_total, 2)) {
1140 $order->add_order_note(sprintf(__('Payment error, incorrect amount captured: %s', 'wc_makecommerce_domain'), $data['amount'].' '.$data['currency']));
1141 wc_add_notice(__('Error verifying transaction', 'wc_makecommerce_domain').', '.__('Incorrect amount captured', 'wc_makecommerce_domain'), 'error');
1142 return false;
1143 }
1144 if($data['currency'] != $order->get_order_currency()) {
1145 $order->add_order_note(sprintf(__('Payment error, incorrect currency captured: %s', 'wc_makecommerce_domain'), $data['currency']));
1146 wc_add_notice(__('Error verifying transaction', 'wc_makecommerce_domain').', '.__('Incorrect currency captured', 'wc_makecommerce_domain'), 'error');
1147 return false;
1148 }
1149
1150 return true;
1151 }
1152
1153 function admin_order_page($order) {
1154 //
1155 }
1156
1157 public function process_refund($order_id, $amount = null, $comment = '') {
1158 if ($this->_api) {
1159 try {
1160 $order = new WC_Order($order_id);
1161 $transactionId = $order->get_transaction_id();
1162
1163 if($response = $this->_api->createRefund($transactionId, array('amount' => $amount, 'comment' => ($comment ? : 'refund')))) {
1164 if($status = (string)$response->transaction->status) {
1165 switch($status) {
1166 case self::MC_REFUNDED:
1167 $order->add_order_note(sprintf(__('Refund completed for amount %s', 'wc_makecommerce_domain'), $amount));
1168 return true;
1169 break;
1170 case self::MC_PART_REFUNDED:
1171 $order->add_order_note(sprintf(__('Partial refund completed for amount %s', 'wc_makecommerce_domain'), $amount));
1172 return true;
1173 break;
1174 }
1175 }
1176 }
1177 return false;
1178
1179 } catch (Exception $e) {
1180 return new WP_Error('makecommerce_refund_error', $e->getMessage());
1181 }
1182
1183 return false;
1184 }
1185 return false;
1186 }
1187
1188 protected function getCountryName($slug) {
1189 switch($slug) {
1190 case 'ee': return __('Estonia', 'wc_makecommerce_domain'); break;
1191 case 'lv': return __('Latvia', 'wc_makecommerce_domain'); break;
1192 case 'lt': return __('Lithuania', 'wc_makecommerce_domain'); break;
1193 case 'fi': return __('Finland', 'wc_makecommerce_domain'); break;
1194 }
1195 return $slug;
1196 }
1197
1198 }
1199
1200 New woocommerce_makecommerce(true);
1201
1202 }
1203
1204 function woocommerce_payment_makecommerce_add($methods) {
1205 $methods[] = 'woocommerce_makecommerce';
1206 return $methods;
1207 }
1208
1209 add_action('plugins_loaded', 'woocommerce_payment_makecommerce_init');
1210 add_action('woocommerce_payment_gateways', 'woocommerce_payment_makecommerce_add');
1211
1212 function clear_wc_shipping_rates_cache(){
1213 $packages = WC()->cart->get_shipping_packages();
1214 foreach ($packages as $key => $value) {
1215 $shipping_session = "shipping_for_package_$key";
1216 unset(WC()->session->$shipping_session);
1217 }
1218 }
1219 add_filter('woocommerce_checkout_update_order_review', 'clear_wc_shipping_rates_cache');
1220
1221 // Parcel machine specific stuff
1222 function parcelmachine_add_method() {
1223
1224 // Delete all the wc_ship transient scum, you aren’t wanted around here, move along.
1225 // Same as being in shipping debug mode
1226 global $wpdb;
1227 $transients = $wpdb->get_col("SELECT option_name FROM $wpdb->options WHERE option_name LIKE '_transient_wc_ship%'");
1228 if (count($transients)) {
1229 foreach ($transients as $tr) {
1230 $hash = substr($tr, 11);
1231 delete_transient($hash);
1232 }
1233 }
1234 $transient_value = get_transient('shipping-transient-version');
1235 WC_Cache_Helper::delete_version_transients( $transient_value );
1236 if (WC()->session) {
1237 WC()->session->set('shipping_for_package', '');
1238 }
1239
1240 if ( ! class_exists( 'WC_ParcelMachine_Shipping_Method' ) ) {
1241
1242 class WC_ParcelMachine_Shipping_Method extends WC_Shipping_Method {
1243
1244
1245 function __construct($instance_id = 0) {
1246 if (!$this->ext) {
1247 throw new Exception('Do not call this class directly!');
1248 }
1249 $this->id = 'parcelmachine_' . mb_strtolower($this->ext);
1250 $this->instance_id = $instance_id;
1251 $this->method_title = $this->name_ext . __(' Parcel Machine by MC', 'wc_makecommerce_domain');
1252 $this->init();
1253 }
1254
1255 function init() {
1256 $this->init_form_fields();
1257 $this->init_settings();
1258
1259 $this->enable = $this->settings['active'];
1260 $this->title = $this->settings['method_name'];
1261 if (defined('ICL_LANGUAGE_CODE') && !empty($this->settings['method_name_'.ICL_LANGUAGE_CODE])) {
1262 $this->title = $this->settings['method_name_'.ICL_LANGUAGE_CODE];
1263 }
1264 $this->availability = 'specific';
1265 $this->countries = $this->settings['countries'];
1266 $this->prioritization = $this->settings['prioritization'];
1267 $this->free_shipping_min_amount = $this->settings['free_shipping_min_amount'];
1268 $this->maximum_weight = (double)$this->settings['maximum_weight'];
1269 $this->short_office_names = $this->settings['short_office_names'];
1270 $this->order_country = 'unknown';
1271 add_action('woocommerce_update_options_shipping_' . $this->id, array(&$this, 'process_admin_options'));
1272 add_filter('woocommerce_review_order_after_shipping' , array(&$this, 'add_parcelmachine_checkout_fields'));
1273 //add_filter('woocommerce_update_order_review_fragments' , array(&$this, 'add_parcelmachine_checkout_fields'));
1274 add_action('woocommerce_checkout_process', array(&$this, 'check_parcelmachine_checkout_fields'));
1275 add_action('woocommerce_after_checkout_validation', array(&$this, 'check_parcelmachine_checkout_fields'));
1276 add_action('woocommerce_checkout_update_order_meta', array(&$this, 'add_parcelmachine_order_meta'));
1277 add_filter('woocommerce_order_shipping_to_display_shipped_via', array(&$this, 'add_admin_parcelmachine_via_field'));
1278 }
1279
1280 function calculate_shipping($package = array()) {
1281
1282 $price = $this->settings['price_'.strtolower($package['destination']['country'])];
1283 if ($this->free_shipping_min_amount && $package['contents_cost'] >= $this->free_shipping_min_amount) {
1284 $price = 0;
1285 }
1286 $rate = array(
1287 'id' => $this->id,
1288 'label' => $this->title,
1289 'cost' => $price,
1290 'calc_tax' => 'per_order',
1291 );
1292 $this->add_rate( $rate );
1293 }
1294
1295 function calculate_weight($package) {
1296 $weight = 0;
1297 foreach ($package['contents'] as $line) {
1298 $weight += $line['data']->get_weight();
1299 }
1300 return $weight;
1301 }
1302 function fits_parcel_machine($package) {
1303 foreach ($package['contents'] as $line) {
1304 if (get_post_meta($line['product_id'], '_no_parcel_machine', true) === 'yes') {
1305 return false;
1306 }
1307 }
1308 return true;
1309 }
1310
1311
1312 function is_available($package) {
1313 if (!$this->fits_parcel_machine($package)) {
1314 return false;
1315 }
1316 $package_weight = $this->calculate_weight($package);
1317 if ($package_weight >= $this->maximum_weight) {
1318 return false;
1319 }
1320 $is_available = $this->enable === 'yes';
1321 if (!$is_available) {
1322 return false;
1323 }
1324 if (is_array($this->countries) && !in_array($package['destination']['country'], $this->countries)) {
1325 $is_available = false;
1326 }
1327 $this->order_country = $package['destination']['country'];
1328 return apply_filters('woocommerce_shipping_' . $this->id . '_is_available', $is_available, $package);
1329 }
1330
1331 function mk_get_machines() {
1332 $machines = mk_get_machines($this->ext, $this->order_country);
1333 if ($this->prioritization === 'yes') {
1334 usort($machines, function($a, $b) {
1335 $sortorder = array(
1336 'tallinn', 'tartu', 'narva', 'pärnu', 'viljandi', 'kohtla-järve', 'rakvere', 'maardu', 'sillamäe', 'kuressaare',
1337 'helsinki', 'espoo', 'tampere', 'vantaa', 'oulu', 'turku', 'jüväskülä', 'lahti', 'kuopio', 'kouvola',
1338 'riga', 'daugavpils', 'liepaja', 'jelgava', 'jurmala', 'ventspils', 'rezekne', 'valmiera', 'jekabpils',
1339 'vilnius', 'kaunas', 'klaipeda', 'siauliai', 'panevezys', 'alytus', 'mariampole', 'mazeikiai', 'jonava', 'utena'
1340 );
1341 $acity = mb_strtolower($a['city']);
1342 $bcity = mb_strtolower($b['city']);
1343 if (!$acity) { $acity = 'xxxxxxx'; }
1344 if (!$bcity) { $bcity = 'xxxxxxx'; }
1345 $aidx = array_search($acity, $sortorder);
1346 $bidx = array_search($bcity, $sortorder);
1347 if ($aidx !== false) {
1348 $acity = str_pad($aidx, 4, "0", STR_PAD_LEFT) . '-' . $acity;
1349 }
1350 if ($bidx !== false) {
1351 $bcity = str_pad($bidx, 4, "0", STR_PAD_LEFT) . '-' . $bcity;
1352 }
1353 $acity .= '-' . mb_strtolower($a['name']);
1354 $bcity .= '-' . mb_strtolower($b['name']);
1355 return $acity < $bcity ? -1 : 1;
1356 });
1357 }
1358 return $machines;
1359 }
1360
1361 function add_parcelmachine_checkout_fields($fragments) {
1362 $this->order_country = WC()->customer->get_shipping_country();
1363 $res = '';
1364 $res .= '<tr style="display: none;" class="parcel_machine_checkout parcel_machine_checkout_parcelmachine_'.mb_strtolower($this->ext).'">';
1365 // echo '<th>' . $this->title . '</th>';
1366 // echo '<td>';
1367 $res .= '<td colspan="2">';
1368 $options = array();
1369 $machines = $this->mk_get_machines();
1370 $res .= '<p class="form-row" id="'.esc_attr($this->id).'_field">';
1371 $res .= '<select class="select" name="'.esc_attr($this->id).'" id="'.esc_attr($this->id).'">';
1372 $res .= '<option value="">'.__('-- select parcel machine --', 'wc_makecommerce_domain').'</option>';
1373 $pcity = false;
1374 foreach ($machines as $machine) {
1375 $city = strtolower($machine['city']);
1376 if ($city !== $pcity) {
1377 if ($pcity) $res .= '</optgroup>';
1378 $res .= '<optgroup label="'.$machine['city'].'">';
1379 }
1380 $mname = $machine['name'];
1381 if ($this->short_office_names !== 'yes') $mname .= ' - ' . $machine['city'] . ', ' . $machine['address'];
1382 $res .= '<option value="'.esc_attr($machine['provider'].'||'.$machine['id']).'">'.$mname.'</option>';
1383 $pcity = $city;
1384 }
1385 if ($pcity) $res .= '</optgroup>';
1386 $res .= '</select>';
1387 $res .= '</p>';
1388 $res .= '</td></tr>';
1389 echo $res;
1390 }
1391
1392 function check_parcelmachine_checkout_fields() {
1393 $shipping_method = !empty($_POST['shipping_method']) ? $_POST['shipping_method'] : false;
1394 if (!empty($shipping_method[0])) { $shipping_method = $shipping_method[0]; }
1395 if ($shipping_method === $this->id && empty($_POST[$shipping_method])) {
1396 wc_add_notice(__('<strong>Parcel machine</strong> is a required field.'), 'error');
1397 }
1398 }
1399 function add_parcelmachine_order_meta($order_id) {
1400 $shipping_method = !empty($_POST['shipping_method']) ? $_POST['shipping_method'] : false;
1401 if (!empty($shipping_method[0])) { $shipping_method = $shipping_method[0]; }
1402 if ($shipping_method === $this->id && !empty($_POST[$this->id])) {
1403 update_post_meta($order_id, '_parcel_machine', sanitize_text_field($_POST[$this->id]));
1404 }
1405 }
1406 function add_admin_parcelmachine_via_field($order) {
1407 return ' <small>(via '.$this->name_ext.')</small>';
1408 }
1409 }
1410 class WC_ParcelMachine_Shipping_Method_Omniva extends WC_ParcelMachine_Shipping_Method {
1411 function __construct($instance_id = 0) {
1412 $this->ext = 'Omniva';
1413 $this->name_ext = 'Omniva';
1414 parent::__construct();
1415 }
1416 function init_form_fields() {
1417
1418 $this->form_fields = array();
1419 $this->form_fields['logo'] = array('type' => 'title', 'title' => __get_logo_html());
1420 $this->form_fields['generic'] = array('type' => 'title', 'title' => __('Generic and pricing options', 'wc_makecommerce_domain'));
1421 $this->form_fields['active'] = array(
1422 'title' => __('Enable', 'wc_makecommerce_domain'),
1423 'type' => 'checkbox',
1424 'label' => __('enabled', 'wc_makecommerce_domain'),
1425 'default' => 'no',
1426 'description' => __('You can always exclude a product from being available for the Parcel Machine delivery by clicking "Does not fit parcel machine" in the product\'s shipping options', 'wc_makecommerce_domain'),
1427 );
1428 $this->form_fields['price_ee'] = array(
1429 'title' => __('Price EE', 'wc_makecommerce_domain'),
1430 'type' => 'text',
1431 'default' => '2.99'
1432 );
1433 $this->form_fields['price_lv'] = array(
1434 'title' => __('Price LV', 'wc_makecommerce_domain'),
1435 'type' => 'text',
1436 'default' => '7.99'
1437 );
1438 $this->form_fields['price_lt'] = array(
1439 'title' => __('Price LT', 'wc_makecommerce_domain'),
1440 'type' => 'text',
1441 'default' => '8.99'
1442 );
1443 $this->form_fields['free_shipping_min_amount'] = array(
1444 'title' => __('Minimum amount for free shipping', 'wc_makecommerce_domain'),
1445 'type' => 'number',
1446 'default' => '0',
1447 'description' => '(0 means no free shipping)'
1448 );
1449 $this->form_fields['maximum_weight'] = array(
1450 'title' => __('Maximum weight allowed for shipping', 'wc_makecommerce_domain'),
1451 'type' => 'text',
1452 'default' => '15'
1453 );
1454 $this->form_fields['countries'] = array(
1455 'title' => __('Specific Countries', 'wc_makecommerce_domain'),
1456 'type' => 'multiselect',
1457 'class' => 'wp-enhanced-select',
1458 'css' => 'width: 450px;',
1459 'default' => array('EE','LV','LT'),
1460 'options' => array('EE' => __('Estonia'), 'LV' => __('Latvia'), 'LT' => __('Lithuania')),
1461 );
1462 $this->form_fields['look_and_feel'] = array( 'type' => 'title', 'title' => __('Look and feel options', 'wc_makecommerce_domain'), 'description' => __('Options for presentation on check-out page', 'wc_makecommerce_domain'));
1463 $languages = apply_filters('wpml_active_languages', NULL, 'skip_missing=0');
1464 if (empty($languages)) {
1465 $this->form_fields['method_name'] = array(
1466 'title' => __('Shipping Method Title', 'wc_makecommerce_domain'),
1467 'type' => 'text',
1468 'default' => __('Omniva Parcel Machine', 'wc_makecommerce_domain')
1469 );
1470 } else {
1471 foreach ($languages as $language_code => $language) {
1472 $language_name = !empty($language['translated_name']) ? $language['translated_name'] : $language_code;
1473 $this->form_fields['method_name_'.$language_code] = array(
1474 'title' => __('Shipping Method Title', 'wc_makecommerce_domain').sprintf(' (%s)', $language_code),
1475 'type' => 'text',
1476 'default' => __('Omniva Parcel Machine', 'wc_makecommerce_domain')
1477 );
1478 }
1479 }
1480 $this->form_fields['prioritization'] = array(
1481 'title' => __('Prioritize', 'wc_makecommerce_domain'),
1482 'type' => 'checkbox',
1483 'label' => __('Bigger cities will be on top of list, others sorted alphabetically', 'wc_makecommerce_domain'),
1484 'default' => 'yes'
1485 );
1486 $this->form_fields['short_office_names'] = array(
1487 'title' => __('Short names', 'wc_makecommerce_domain'),
1488 'type' => 'checkbox',
1489 'label' => __('Display only parcel machine names, without addresses', 'wc_makecommerce_domain'),
1490 'default' => 'no'
1491 );
1492 $this->form_fields['api_access'] = array( 'type' => 'title', 'title' => '<br>'.__('API access for', 'wc_makecommerce_domain').' '.$this->ext, 'description' => sprintf(__('You can automatically create shipments into Omniva system and print the out the package labels right here, at the shop orders view. <br> Please set your Omniva web servises account credentials below here. <br>(see more on <a href="https://makecommerce.net/en/integration-modules/makecommerce-woocommerce-payment-plugin/#carriers-integration">MakeCommerce plugin page</a>. Don\'t forget to enable also <a href="%s">MC API keys</a>!)', 'wc_makecommerce_domain'), admin_url('admin.php?page=wc-settings&tab=api&section=mk_api')));
1493 $this->form_fields['service_user'] = array(
1494 'title' => __('Omniva web services username', 'wc_makecommerce_domain'),
1495 'type' => 'text',
1496 'default' => ''
1497 );
1498 $this->form_fields['service_password'] = array(
1499 'title' => __('Omniva web services password', 'wc_makecommerce_domain'),
1500 'type' => 'text',
1501 'default' => ''
1502 );
1503 $this->form_fields['return_address'] = array( 'type' => 'title', 'title' => __('Return address', 'wc_makecommerce_domain'), 'description' => __('Please define return address for Omniva shipments', 'wc_makecommerce_domain'));
1504 $this->form_fields['shop_name'] = array(
1505 'type' => 'text',
1506 'title' => __('Shop name', 'wc_makecommerce_domain'),
1507 'class' => 'input-text regular-input',
1508 );
1509 $this->form_fields['shop_phone'] = array(
1510 'type' => 'text',
1511 'title' => __('Shop phone', 'wc_makecommerce_domain'),
1512 'class' => 'input-text regular-input',
1513 );
1514 $this->form_fields['shop_email'] = array(
1515 'type' => 'text',
1516 'title' => __('Shop email', 'wc_makecommerce_domain'),
1517 'class' => 'input-text regular-input',
1518 );
1519 $this->form_fields['shop_postal_code'] = array(
1520 'type' => 'text',
1521 'title' => __('Shop postal code', 'wc_makecommerce_domain'),
1522 'class' => 'input-text regular-input',
1523 'description' => __('Put here zip-code of the Parcel Terminal you use for returns, see: <a href="https://www.omniva.ee/era/kaart/asukohad">List of Omniva Terminals</a>', 'wc_makecommerce_domain')
1524 );
1525 }
1526
1527 }
1528 class WC_ParcelMachine_Shipping_Method_Smartpost extends WC_ParcelMachine_Shipping_Method {
1529 function __construct() {
1530 $this->ext = 'SmartPost';
1531 $this->name_ext = 'SmartPOST';
1532 parent::__construct();
1533 }
1534 function init_form_fields() {
1535 global $woocommerce;
1536
1537 $this->form_fields = array();
1538 $this->form_fields['logo'] = array('type' => 'title', 'title' => __get_logo_html());
1539 $this->form_fields['generic'] = array( 'type' => 'title', 'title' => __('Generic and pricing options', 'wc_makecommerce_domain'));
1540 $this->form_fields['active'] = array(
1541 'title' => __('Enable', 'wc_makecommerce_domain'),
1542 'type' => 'checkbox',
1543 'label' => __('enabled', 'wc_makecommerce_domain'),
1544 'default' => 'no',
1545 'description' => __('You can always exclude a product from being available for the Parcel Machine delivery by clicking "Does not fit parcel machine" in the product\'s shipping options', 'wc_makecommerce_domain'),
1546 );
1547 $this->form_fields['price_ee'] = array(
1548 'title' => __('Price EE', 'wc_makecommerce_domain'),
1549 'type' => 'text',
1550 'default' => '2.99'
1551 );
1552 $this->form_fields['price_fi'] = array(
1553 'title' => __('Price FI', 'wc_makecommerce_domain'),
1554 'type' => 'text',
1555 'default' => '10.99'
1556 );
1557 $this->form_fields['free_shipping_min_amount'] = array(
1558 'title' => __('Minimum amount for free shipping', 'wc_makecommerce_domain'),
1559 'type' => 'number',
1560 'default' => '0',
1561 'description' => __('(0 means no free shipping)', 'wc_makecommerce_domain'),
1562 );
1563 $this->form_fields['maximum_weight'] = array(
1564 'title' => __('Maximum weight allowed for shipping', 'wc_makecommerce_domain'),
1565 'type' => 'text',
1566 'default' => '15'
1567 );
1568 $this->form_fields['countries'] = array(
1569 'title' => __('Specific Countries', 'wc_makecommerce_domain'),
1570 'type' => 'multiselect',
1571 'class' => 'wp-enhanced-select',
1572 'css' => 'width: 450px;',
1573 'default' => array('EE','FI'),
1574 'options' => array('EE' => __('Estonia'), 'FI' => __('Finland')),
1575 );
1576 $this->form_fields['look_and_feel'] = array( 'type' => 'title', 'title' => __('Look and feel options', 'wc_makecommerce_domain'), 'description' => __('Options for presentation on checkout page', 'wc_makecommerce_domain'));
1577 $languages = apply_filters('wpml_active_languages', NULL, 'skip_missing=0');
1578 if (empty($languages)) {
1579 $this->form_fields['method_name'] = array(
1580 'title' => __('Shipping Method Title', 'wc_makecommerce_domain'),
1581 'type' => 'text',
1582 'default' => __('SmartPOST Parcel Machine', 'wc_makecommerce_domain')
1583 );
1584 } else {
1585 foreach ($languages as $language_code => $language) {
1586 $language_name = !empty($language['translated_name']) ? $language['translated_name'] : $language_code;
1587 $this->form_fields['method_name_'.$language_code] = array(
1588 'title' => __('Shipping Method Title', 'wc_makecommerce_domain').sprintf(' (%s)', $language_code),
1589 'type' => 'text',
1590 'default' => __('SmartPOST Parcel Machine', 'wc_makecommerce_domain')
1591 );
1592 }
1593 }
1594 $this->form_fields['prioritization'] = array(
1595 'title' => __('Prioritize', 'wc_makecommerce_domain'),
1596 'type' => 'checkbox',
1597 'label' => __('Bigger cities will be on top of list, others sorted alphabetically', 'wc_makecommerce_domain'),
1598 'default' => 'yes'
1599 );
1600 $this->form_fields['short_office_names'] = array(
1601 'title' => __('Short names', 'wc_makecommerce_domain'),
1602 'type' => 'checkbox',
1603 'label' => __('Display only parcel machine names, without addresses', 'wc_makecommerce_domain'),
1604 'default' => 'no'
1605 );
1606 $this->form_fields['api_access'] = array( 'type' => 'title', 'title' => '<br>'.__('API access for', 'wc_makecommerce_domain').' '.$this->ext, 'description' => sprintf(__('You can automatically create shipments into smartpost.ee system and print the out the package labels right here, at the shop orders view. <br> Please set your smartpost.ee account credentials below here. <br>(See more on <a href="https://makecommerce.net/en/integration-modules/makecommerce-woocommerce-payment-plugin/#carriers-integration">MakeCommerce plugin page</a>. Don\'t forget to enable also <a href="%s">MC API keys</a>!)', 'wc_makecommerce_domain'), admin_url('admin.php?page=wc-settings&tab=api&section=mk_api')));
1607 $this->form_fields['service_user'] = array(
1608 'title' => __('eteenindus.smartpost.ee username', 'wc_makecommerce_domain'),
1609 'type' => 'text',
1610 'default' => ''
1611 );
1612 $this->form_fields['service_password'] = array(
1613 'title' => __('eteenindus.smartpost.ee password', 'wc_makecommerce_domain'),
1614 'type' => 'text',
1615 'default' => ''
1616 );
1617 $this->form_fields['return_address'] = array( 'type' => 'title', 'title' => __('Return address', 'wc_makecommerce_domain'), 'description' => __('Please define return address for SmartPOST shipments (used on parcel labels)', 'wc_makecommerce_domain'));
1618 $this->form_fields['shop_name'] = array(
1619 'type' => 'text',
1620 'title' => __('Shop name', 'wc_makecommerce_domain'),
1621 'class' => 'input-text regular-input',
1622 );
1623 $this->form_fields['shop_phone'] = array(
1624 'type' => 'text',
1625 'title' => __('Shop phone', 'wc_makecommerce_domain'),
1626 'class' => 'input-text regular-input',
1627 );
1628 $this->form_fields['shop_email'] = array(
1629 'type' => 'text',
1630 'title' => __('Shop email', 'wc_makecommerce_domain'),
1631 'class' => 'input-text regular-input',
1632 );
1633 }
1634
1635 }
1636 }
1637 }
1638 add_action('woocommerce_shipping_init', 'parcelmachine_add_method');
1639
1640 function add_parcelmachine_shipping_method($methods) {
1641 $methods[] = 'WC_ParcelMachine_Shipping_Method_Omniva';
1642 $methods[] = 'WC_ParcelMachine_Shipping_Method_Smartpost';
1643 return $methods;
1644 }
1645 add_filter('woocommerce_shipping_methods', 'add_parcelmachine_shipping_method');
1646
1647 function mk_parcelmachine_add_assets() {
1648 wp_enqueue_style('parcelmachine-css', plugin_dir_url(__FILE__).'/css/parcelmachine.css');
1649 wp_enqueue_script('parcelmachine-js', plugin_dir_url(__FILE__).'/scripts/parcelmachine.js', array('jquery'));
1650 }
1651 add_action('wp_enqueue_scripts', 'mk_parcelmachine_add_assets');
1652
1653 function mk_admin_plugin_settings_link($links) {
1654 $settings_link = '<a href="admin.php?page=wc-settings&tab=api&section=mk_api">API '.__('Settings').'</a>';
1655 array_unshift($links, $settings_link);
1656 return $links;
1657 }
1658 $plugin = plugin_basename(__FILE__);
1659 add_filter("plugin_action_links_$plugin", 'mk_admin_plugin_settings_link' );
1660
1661 function mk_admin_add_settings($sections) {
1662 $sections['mk_api'] = __('MakeCommerce API access', 'wc_makecommerce_domain');
1663 return $sections;
1664 }
1665
1666 function mk_admin_all_settings($settings) {
1667 global $current_section;
1668 if ($current_section !== 'mk_api') {
1669 return $settings;
1670 }
1671 return array(
1672 array('type' => 'title', 'desc' => __get_logo_html()),
1673 array(
1674 'type' => 'title',
1675 'title' => __('MakeCommerce API access credentials', 'wc_makecommerce_domain'),
1676 'desc' => sprintf(__('To use MakeCommerce/Maksekeskus services you need to enter API credentials below here <br/> <br/>'.
1677 'To further configure the Payment methods please go to <a href="%s">MakeCommerce Checkout Options</a><br>'.
1678 'To use also Ominva or Itella SmartPOST integration please configure these API accesses: <a href="%s">Omniva API access</a> | '.
1679 '<a href="%s">SmartPOST API access</a><br/>','wc_makecommerce_domain'),
1680 'admin.php?page=wc-settings&tab=checkout&section=makecommerce',
1681 'admin.php?page=wc-settings&tab=-shipping&section=parcelmachine_omniva',
1682 'admin.php?page=wc-settings&tab=shipping&section=parcelmachine_smartpost'
1683 ),
1684 'id' => 'mk_api_settings'
1685 ),
1686 array(
1687 'type' => 'select',
1688 'title' => __('Current environment', 'wc_makecommerce_domain'),
1689 'desc' => __('See more about <a href="https://maksekeskus.ee/en/for-developers/test-environment/">MakeCommerce Test environment</a>', 'wc_makecommerce_domain'),
1690 'default' => 'live',
1691 'options' => array(
1692 'live' => __('Live', 'wc_makecommerce_domain'),
1693 'test' => __('Test', 'wc_makecommerce_domain'),
1694 ),
1695 'id' => 'mk_api_type'
1696 ),
1697 array(
1698 'id' => 'mk_shop_id',
1699 'type' => 'text',
1700 'title' => __('Shop ID (live)', 'wc_makecommerce_domain'),
1701 'desc' => __('Get it from <a href="https://merchant.maksekeskus.ee/api.html" target="_blank">Merchant Portal</a>','wc_makecommerce_domain'),
1702 'class' => 'input-text regular-input',
1703 ),
1704 array(
1705 'id' => 'mk_private_key',
1706 'type' => 'text',
1707 'title' => __('Secret key (live)', 'wc_makecommerce_domain'),
1708 'class' => 'input-text regular-input',
1709 ),
1710 array(
1711 'id' => 'mk_public_key',
1712 'type' => 'text',
1713 'title' => __('Publishable key (live)', 'wc_makecommerce_domain'),
1714 'class' => 'input-text regular-input',
1715 ),
1716 array(
1717 'id' => 'mk_test_shop_id',
1718 'type' => 'text',
1719 'title' => __('Shop ID (test)', 'wc_makecommerce_domain'),
1720 'class' => 'input-text regular-input',
1721 'desc' => __('Get it from <a href="https://merchant-test.maksekeskus.ee/api.html" target="_blank">Merchant Portal Test</a>','wc_makecommerce_domain'),
1722 ),
1723 array(
1724 'id' => 'mk_test_private_key',
1725 'type' => 'text',
1726 'title' => __('Secret key (test)', 'wc_makecommerce_domain'),
1727 'class' => 'input-text regular-input',
1728 ),
1729 array(
1730 'id' => 'mk_test_public_key',
1731 'type' => 'text',
1732 'title' => __('Publishable key (test)', 'wc_makecommerce_domain'),
1733 'class' => 'input-text regular-input',
1734 ),
1735 array('type' => 'sectionend', 'id' => 'mk_api_settings')
1736 );
1737 }
1738
1739 function mk_admin_save_settings() {
1740 // reload banklinks
1741 global $current_section;
1742 if ($current_section !== 'mk_api') {
1743 return;
1744 }
1745 $wcmc = New woocommerce_makecommerce(true);
1746 $wcmc->mc_banklinks_reload(true);
1747 }
1748
1749 function __get_logo_html() {
1750 return '<div class="makecommerce-info">'.
1751 '<div class="makecommerce-logo">'.
1752 '<a target="_blank" href="http://maksekeskus.ee"><img src="'. plugins_url('/images/makecommerce_logo_en.svg', __FILE__) .'" class="makecommerce-logo"></a>'.
1753 '</div>'.
1754 '<div class="makecommerce-links">'.
1755 '<div class="makecommerce-link"><a target="_blank" href="https://merchant.maksekeskus.ee">Merchant Portal</a></div>'.
1756 '<div class="makecommerce-link"><a target="_blank" href="https://makecommerce.net/">makecommerce.net</a></div>'.
1757 '<div class="makecommerce-link"><a target="_blank" href="http://maksekeskus.ee">maksekeskus.ee</a></div>'.
1758 '</div>'.
1759 '</div>';
1760 }
1761
1762 add_action('woocommerce_get_sections_api', 'mk_admin_add_settings');
1763 add_filter('woocommerce_get_settings_api', 'mk_admin_all_settings', 10, 2);
1764 add_action('woocommerce_settings_saved', 'mk_admin_save_settings', 30, 0);
1765
1766 function mk_admin_restrict_manage_posts() {
1767 global $typenow;
1768 if ( in_array( $typenow, wc_get_order_types( 'order-meta-boxes' ) ) ) {
1769 $selected_method = !empty($_REQUEST['_shipping_method']) ? $_REQUEST['_shipping_method'] : false;
1770 $methods = WC()->shipping->load_shipping_methods();
1771 echo '<select name="_shipping_method" id="shipping_type" class="enhanced">';
1772 echo '<option value="">'.__('-- filter by shipping method', 'wc_makecommerce_domain') . '</option>';
1773 foreach ($methods as $method) {
1774 echo '<option value="'.$method->id.'"'.($selected_method === $method->id ? ' selected="selected"' : '').'>'.$method->title.'</option>';
1775 }
1776 echo '</select>';
1777 }
1778 }
1779 function mk_admin_shipping_filter($where, &$wp_query) {
1780 global $pagenow, $wpdb;
1781 $method = !empty($_REQUEST['_shipping_method']) ? $_REQUEST['_shipping_method'] : false;
1782 if (is_admin() && $pagenow=='edit.php' && $wp_query->query_vars['post_type'] == 'shop_order' && !empty($method) ) {
1783 $where .= $GLOBALS['wpdb']->prepare( ' AND ID
1784 IN (
1785 SELECT items.order_id
1786 FROM '.$wpdb->prefix.'woocommerce_order_itemmeta meta, '.$wpdb->prefix.'woocommerce_order_items items
1787 WHERE meta.order_item_id = items.order_item_id
1788 AND meta.meta_key = "method_id"
1789 AND meta.meta_value = %s
1790 ) ', $method );
1791 }
1792 return $where;
1793 }
1794 add_filter('restrict_manage_posts', 'mk_admin_restrict_manage_posts');
1795 add_filter('posts_where', 'mk_admin_shipping_filter', 10, 2);
1796
1797 function mk_admin_bulk_actions() {
1798 global $post_type;
1799 if ('shop_order' === $post_type) {
1800 ?>
1801 <script type="text/javascript">
1802 jQuery(function() {
1803 jQuery('<option>').val('parcel_machine_labels').text('<?php _e( 'Register parcel machine shipments', 'wc_makecommerce_domain' )?>').appendTo('select[name="action"]');
1804 jQuery('<option>').val('parcel_machine_labels').text('<?php _e( 'Register parcel machine shipments', 'wc_makecommerce_domain' )?>').appendTo('select[name="action2"]');
1805 jQuery('<option>').val('parcel_machine_print_labels').text('<?php _e( 'Print parcel machine labels', 'wc_makecommerce_domain' )?>').appendTo('select[name="action"]');
1806 jQuery('<option>').val('parcel_machine_print_labels').text('<?php _e( 'Print parcel machine labels', 'wc_makecommerce_domain' )?>').appendTo('select[name="action2"]');
1807 });
1808 </script>
1809 <?php
1810 if (!empty($_REQUEST['mk_pdf'])) {
1811 ?>
1812 <script type="text/javascript">
1813 jQuery(function(){
1814 window.open('<?php echo $_REQUEST['mk_pdf'] ?>', 'pdf');
1815 });
1816 </script>
1817 <?php
1818 }
1819 }
1820 }
1821 function mk_admin_bulk_action_labels() {
1822 $wp_list_table = _get_list_table('WP_Posts_List_Table');
1823 $shipping_request = array('credentials' => array(), 'orders' => array());
1824 $post_ids = array_map('absint', (array)$_REQUEST['post']);
1825 mk_get_shipment_ids($post_ids);
1826 }
1827 function mk_admin_bulk_action_print() {
1828 $wp_list_table = _get_list_table('WP_Posts_List_Table');
1829 $shipping_request = array('credentials' => array(), 'orders' => array());
1830 $post_ids = array_map('absint', (array)$_REQUEST['post']);
1831 mk_get_labels($post_ids);
1832 }
1833 add_action('admin_footer', 'mk_admin_bulk_actions', 11);
1834 add_action('admin_action_parcel_machine_labels', 'mk_admin_bulk_action_labels');
1835 add_action('admin_action_parcel_machine_print_labels', 'mk_admin_bulk_action_print');
1836
1837
1838
1839 function mk_admin_parcelmachine_order_meta($order) {
1840 $machine_id = get_post_meta($order->id, '_parcel_machine', true);
1841 if (empty($machine_id)) return;
1842 list($provider, $machine) = explode('||', $machine_id);
1843 if (empty($machine)) return;
1844 $machine = mk_get_machine($provider, (int)$machine);
1845 if (!$machine) return;
1846 echo '<p><strong>'.__('Parcel machine').':</strong><br/>' . $machine['name'] . '<br/><small>' . $machine['address'] . '</small></p>';
1847 $shipment_id = get_post_meta($order->id, '_parcel_machine_shipment_id', true);
1848 $shipment_id_error = get_post_meta($order->id, '_parcel_machine_error', true);
1849 if ($shipment_id) {
1850 echo '<p><strong>'.__('Parcel machine shipment ID').':</strong><br/>' . $shipment_id . '</small></p>';
1851 }
1852 if ($shipment_id_error) {
1853 echo '<p><strong style="color: red;">'.__('Parcel machine shipment generation error').':</strong><br/>' . $shipment_id_error . '</small></p>';
1854 }
1855 }
1856
1857 function mk_admin_render_shop_order_columns( $column ) {
1858 global $post, $woocommerce, $the_order;
1859 if (empty($the_order) || $the_order->id != $post->ID) {
1860 $the_order = wc_get_order($post->ID);
1861 }
1862 if ($column === 'shipping_address') {
1863 $machine = get_post_meta($the_order->id, '_parcel_machine', true);
1864 $shipment_id = get_post_meta($the_order->id, '_parcel_machine_shipment_id', true);
1865 $shipment_id_error = get_post_meta($the_order->id, '_parcel_machine_error', true);
1866 if ($machine) {
1867 if ($shipment_id) echo __('Package shipment_id:', 'wc_makecommerce_domain') . ' ' . $shipment_id;
1868 else if ($shipment_id_error) echo '<span style="color: red;">'.__('Package shipment generation error:', 'wc_makecommerce_domain') . '</span><br/>' .$shipment_id_error;
1869 else echo __('Shipment ID not generated for delivery', 'wc_makecommerce_domain');
1870 }
1871 }
1872 }
1873 function mk_add_email_customer_details_fields($fields, $sent_to_admin, $order) {
1874 $machine_id = get_post_meta($order->id, '_parcel_machine', true);
1875 if (empty($machine_id)) return $fields;
1876 list($provider, $machine) = explode('||', $machine_id);
1877 if (empty($machine)) return $fields;
1878 $machine = mk_get_machine($provider, (int)$machine);
1879 if (!$machine) return $fields;
1880 $fields[] = array('label' => __('Parcel machine'), 'value' => $machine['name'].' - '.$machine['address']);
1881 return $fields;
1882 }
1883 function mk_add_order_customer_details_fields($order) {
1884 $machine_id = get_post_meta($order->id, '_parcel_machine', true);
1885 if (empty($machine_id)) return;
1886 list($provider, $machine) = explode('||', $machine_id);
1887 if (empty($machine)) return;
1888 $machine = mk_get_machine($provider, (int)$machine);
1889 if (!$machine) return;
1890 echo '<tr>';
1891 echo '<th>'.__('Parcel machine').'</th>';
1892 echo '<td>'.$machine['name'].'<br/>'.$machine['address'].'</td>';
1893 echo '</tr>';
1894 }
1895 function mk_admin_product_option_fields($fields) {
1896 echo '<div class="options_group">';
1897 woocommerce_wp_checkbox(
1898 array(
1899 'id' => '_no_parcel_machine',
1900 'wrapper_class' => 'show_if_simple',
1901 'label' => __('Does not fit parcel machine', 'wc_makecommerce_domain'),
1902 'description' => __('When this is checked, parcel machine shipping option is not available for a cart with this product', 'wc_makecommerce_domain')
1903 )
1904 );
1905 echo '</div>';
1906 }
1907 function mk_admin_product_fields_save($post_id) {
1908 $no_parcel_machine = isset($_POST['_no_parcel_machine']) ? 'yes' : 'no';
1909 update_post_meta($post_id, '_no_parcel_machine', $no_parcel_machine);
1910 }
1911
1912 add_action('woocommerce_product_options_shipping', 'mk_admin_product_option_fields');
1913 add_action('woocommerce_process_product_meta', 'mk_admin_product_fields_save');
1914 add_action('woocommerce_admin_order_data_after_shipping_address', 'mk_admin_parcelmachine_order_meta', 10, 1);
1915 add_action('manage_shop_order_posts_custom_column', 'mk_admin_render_shop_order_columns', 3);
1916 add_filter('woocommerce_email_customer_details_fields', 'mk_add_email_customer_details_fields', 10, 3 );
1917 add_action('woocommerce_order_details_after_customer_details', 'mk_add_order_customer_details_fields');
1918 add_action('woocommerce_order_status_processing', 'mk_get_shipment_ids');
1919
1920
1921
1922 function mk_get_shipment_ids($post_ids) {
1923 if (!is_array($post_ids)) {
1924 $post_ids = array($post_ids);
1925 }
1926 parcelmachine_add_method();
1927 $shipping_request = array('credentials' => array(), 'orders' => array());
1928 foreach ($post_ids as $post_id) {
1929 $parcel_machine = get_post_meta($post_id, '_parcel_machine', true);
1930 if (!$parcel_machine) { continue; }
1931 list($provider, $machine_id) = explode('||', $parcel_machine);
1932 if (!$provider || !$machine_id) { continue; }
1933 $provider_uc = mb_strtoupper($provider);
1934 switch ($provider_uc) {
1935 case "OMNIVA":
1936 $transport_class = new WC_ParcelMachine_Shipping_Method_Omniva();
1937 break;
1938 case "SMARTPOST":
1939 $transport_class = new WC_ParcelMachine_Shipping_Method_Smartpost();
1940 break;
1941 default:
1942 break 2;
1943 }
1944 if (empty($shipping_request['credentials'][$provider_uc])) {
1945 $api_user = $transport_class->settings['service_user'];
1946 $api_password = $transport_class->settings['service_password'];
1947 if (!$api_user || !$api_password) {
1948 add_action('admin_notices', 'mk_admin_error');
1949 continue;
1950 }
1951 $shipping_request['credentials'][$provider_uc] = array('carrier' => $provider_uc, 'username' => $api_user, 'password' => $api_password);
1952 }
1953 $order = wc_get_order($post_id);
1954 $sender = array(
1955 'name' => $transport_class->settings['shop_name'],
1956 'phone' => $transport_class->settings['shop_phone'],
1957 'email' => $transport_class->settings['shop_email'],
1958 'postalCode' => $transport_class->settings['shop_postal_code']
1959 );
1960 $shipping_request['orders'][] = array(
1961 'carrier' => $provider_uc,
1962 'orderId' => $order->id,
1963 'destination' => array('destinationId' => $machine_id),
1964 'recipient' => array('name' => $order->shipping_first_name . ' ' . $order->shipping_last_name, 'phone' => $order->billing_phone, 'email' => $order->billing_email),
1965 'sender' => $sender
1966 );
1967 }
1968 $shipping_request['credentials'] = array_values($shipping_request['credentials']);
1969 if (empty($shipping_request['orders'])) {
1970 return;
1971 }
1972 $MK = mk_get_api();
1973 if (!$MK) {
1974 return;
1975 }
1976 try {
1977 $response = $MK->createShipments($shipping_request);
1978 } catch (Exception $e) {
1979 echo $e->getMessage();
1980 exit();
1981 }
1982 foreach ($response as $order) {
1983 if (!empty($order->orderId) && !empty($order->shipmentId)) {
1984 update_post_meta((int)$order->orderId, '_parcel_machine_shipment_id', sanitize_text_field($order->shipmentId));
1985 } else if (!empty($order->orderId) && !empty($order->barCode)) {
1986 update_post_meta((int)$order->orderId, '_parcel_machine_shipment_id', sanitize_text_field($order->barCode));
1987 } else if (!empty($order->orderId) && !empty($order->errorMessage)) {
1988 update_post_meta((int)$order->orderId, '_parcel_machine_error', sanitize_text_field($order->errorMessage));
1989 }
1990 }
1991 }
1992
1993 function mk_get_labels($post_ids) {
1994 if (!is_array($post_ids)) {
1995 $post_ids = array($post_ids);
1996 }
1997 $sender = array(
1998 'name' => get_option('mk_shop_name', ''),
1999 'phone' => get_option('mk_shop_phone', ''),
2000 'email' => get_option('mk_shop_email', ''),
2001 'postalCode' => get_option('mk_shop_postal_code', '')
2002 );
2003 parcelmachine_add_method();
2004 $shipping_request = array('credentials' => array(), 'orders' => array(), 'printFormat' => 'A4');
2005 foreach ($post_ids as $post_id) {
2006 $parcel_machine = get_post_meta($post_id, '_parcel_machine', true);
2007 if (!$parcel_machine) { continue; }
2008 list($provider, $machine_id) = explode('||', $parcel_machine);
2009 if (!$provider || !$machine_id) { continue; }
2010 $provider_uc = mb_strtoupper($provider);
2011 if (empty($shipping_request['credentials'][$provider_uc])) {
2012 switch ($provider_uc) {
2013 case "OMNIVA":
2014 $transport_class = new WC_ParcelMachine_Shipping_Method_Omniva();
2015 break;
2016 case "SMARTPOST":
2017 $transport_class = new WC_ParcelMachine_Shipping_Method_Smartpost();
2018 break;
2019 default:
2020 break 2;
2021 }
2022 $api_user = $transport_class->settings['service_user'];
2023 $api_password = $transport_class->settings['service_password'];
2024 if (!$api_user || !$api_password) {
2025 add_action('admin_notices', 'mk_admin_error');
2026 continue;
2027 }
2028 $shipping_request['credentials'][$provider_uc] = array('carrier' => $provider_uc, 'username' => $api_user, 'password' => $api_password);
2029 }
2030 $order = wc_get_order($post_id);
2031 $shipment_id = get_post_meta($post_id, '_parcel_machine_shipment_id', true);
2032 if (!$shipment_id) { continue; }
2033 $shipping_request['orders'][] = array(
2034 'carrier' => $provider_uc,
2035 'orderId' => $order->id,
2036 'destination' => array('destinationId' => $machine_id),
2037 'recipient' => array('name' => $order->shipping_first_name . ' ' . $order->shipping_last_name, 'phone' => $order->billing_phone, 'email' => $order->billing_email),
2038 'sender' => $sender,
2039 'shipmentId' => $shipment_id
2040 );
2041 }
2042 $shipping_request['credentials'] = array_values($shipping_request['credentials']);
2043 if (empty($shipping_request['orders'])) {
2044 return;
2045 }
2046 $MK = mk_get_api();
2047 if (!$MK) {
2048 return;
2049 }
2050 try {
2051 $response = $MK->createLabels($shipping_request);
2052 } catch (Exception $e) {
2053 echo $e->getMessage();
2054 exit();
2055 }
2056 if (empty($response->labelUrl)) {
2057 return;
2058 }
2059 $sendback = add_query_arg(array('post_type' => 'shop_order', 'mk_pdf' => urlencode($response->labelUrl)), '');
2060 wp_redirect(esc_url_raw($sendback));
2061 exit();
2062 }
2063
2064
2065
2066 $MKAPI = false;
2067 function mk_get_api() {
2068 global $MKAPI;
2069 if ($MKAPI) {
2070 //return $MKAPI;
2071 }
2072 $mk_api_type = get_option('mk_api_type', false);
2073 if (!$mk_api_type) {
2074 return false;
2075 }
2076 if ($mk_api_type !== 'live') {
2077 $key_prefix = $mk_api_type.'_';
2078 } else {
2079 $key_prefix = '';
2080 }
2081 $mk_shop_id = get_option('mk_'.$key_prefix.'shop_id', '');
2082 $mk_public_key = get_option('mk_'.$key_prefix.'public_key', '');
2083 $mk_private_key = get_option('mk_'.$key_prefix.'private_key', '');
2084 if (!$mk_shop_id || !$mk_public_key || !$mk_shop_id) {
2085 return false;
2086 }
2087 $MKAPI = new Maksekeskus($mk_shop_id, $mk_public_key, $mk_private_key, $mk_api_type === 'live' ? false : true);
2088 return $MKAPI;
2089 }
2090 function mk_get_machines($provider, $country = 'EE') {
2091 $data = get_option('mk_machines_cache', false);
2092 $data_expires = get_option('mk_machines_expires', false);
2093 if (!$data || $data_expires < time()) {
2094 $MK = mk_get_api();
2095 $data = $MK->getDestinations(array('type' => 'APT'));
2096 update_option('mk_machines_cache', $data);
2097 update_option('mk_machines_expires', time()+3*60*60);
2098 }
2099
2100 $machines = array();
2101 if (!$data || empty($data)) {
2102 return array();
2103 }
2104 foreach ($data as $machine) {
2105 if ($machine->country === $country && $machine->type === 'APT' && ($provider === '*' || strtolower($provider) === strtolower($machine->provider))) {
2106 $machines[] = array(
2107 'provider' => strtolower($machine->provider),
2108 'id' => $machine->id,
2109 'name' => $machine->name,
2110 'city' => $machine->city,
2111 'address' => !empty($machine->address) ? $machine->address : '',
2112 );
2113 }
2114 }
2115 usort($machines, function($a, $b){
2116 if ($a['city'] === $b['city']) {
2117 return $a['name'] > $b['name'];
2118 }
2119 return $a['city'] > $b['city'];
2120 });
2121 return $machines;
2122 }
2123
2124 function mk_get_machine($provider, $id) {
2125 $machines = mk_get_machines($provider);
2126 foreach ($machines as $machine) {
2127 if ($machine['provider'] === $provider && $machine['id'] === $id) {
2128 return $machine;
2129 }
2130 }
2131 return false;
2132 }
2133 function mk_admin_error() {
2134 printf( '<div class="%1$s"><p>%2$s</p></div>', 'notice notice-error', __('Please check that you have configured correctly MakeCommerce API accesses', 'wc_makecommerce_domain'));
2135 }
2136 }
2137