PluginProbe
MakeCommerce for WooCommerce / 1.1.3
MakeCommerce for WooCommerce v1.1.3
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.3, at makecommerce.php

2,138 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.3
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_checkout_update_order_meta', array(&$this, 'add_parcelmachine_order_meta'));
1276 add_filter('woocommerce_order_shipping_to_display_shipped_via', array(&$this, 'add_admin_parcelmachine_via_field'));
1277 }
1278
1279 function calculate_shipping($package = array()) {
1280
1281 $price = $this->settings['price_'.strtolower($package['destination']['country'])];
1282 if ($this->free_shipping_min_amount && $package['contents_cost'] >= $this->free_shipping_min_amount) {
1283 $price = 0;
1284 }
1285 $rate = array(
1286 'id' => $this->id,
1287 'label' => $this->title,
1288 'cost' => $price,
1289 'calc_tax' => 'per_order',
1290 );
1291 $this->add_rate( $rate );
1292 }
1293
1294 function calculate_weight($package) {
1295 $weight = 0;
1296 foreach ($package['contents'] as $line) {
1297 $weight += $line['data']->get_weight();
1298 }
1299 return $weight;
1300 }
1301 function fits_parcel_machine($package) {
1302 foreach ($package['contents'] as $line) {
1303 if (get_post_meta($line['product_id'], '_no_parcel_machine', true) === 'yes') {
1304 return false;
1305 }
1306 }
1307 return true;
1308 }
1309
1310
1311 function is_available($package) {
1312 if (!$this->fits_parcel_machine($package)) {
1313 return false;
1314 }
1315 $package_weight = $this->calculate_weight($package);
1316 if ($package_weight >= $this->maximum_weight) {
1317 return false;
1318 }
1319 $is_available = $this->enable === 'yes';
1320 if (!$is_available) {
1321 return false;
1322 }
1323 if (is_array($this->countries) && !in_array($package['destination']['country'], $this->countries)) {
1324 $is_available = false;
1325 }
1326 $this->order_country = $package['destination']['country'];
1327 return apply_filters('woocommerce_shipping_' . $this->id . '_is_available', $is_available, $package);
1328 }
1329
1330 function mk_get_machines() {
1331 $machines = mk_get_machines($this->ext, $this->order_country);
1332 if ($this->prioritization === 'yes') {
1333 usort($machines, function($a, $b) {
1334 $sortorder = array(
1335 'tallinn', 'tartu', 'narva', 'pärnu', 'viljandi', 'kohtla-järve', 'rakvere', 'maardu', 'sillamäe', 'kuressaare',
1336 'helsinki', 'espoo', 'tampere', 'vantaa', 'oulu', 'turku', 'jüväskülä', 'lahti', 'kuopio', 'kouvola',
1337 'riga', 'daugavpils', 'liepaja', 'jelgava', 'jurmala', 'ventspils', 'rezekne', 'valmiera', 'jekabpils',
1338 'vilnius', 'kaunas', 'klaipeda', 'siauliai', 'panevezys', 'alytus', 'mariampole', 'mazeikiai', 'jonava', 'utena'
1339 );
1340 $acity = mb_strtolower($a['city']);
1341 $bcity = mb_strtolower($b['city']);
1342 if (!$acity) { $acity = 'xxxxxxx'; }
1343 if (!$bcity) { $bcity = 'xxxxxxx'; }
1344 $aidx = array_search($acity, $sortorder);
1345 $bidx = array_search($bcity, $sortorder);
1346 if ($aidx !== false) {
1347 $acity = str_pad($aidx, 4, "0", STR_PAD_LEFT) . '-' . $acity;
1348 }
1349 if ($bidx !== false) {
1350 $bcity = str_pad($bidx, 4, "0", STR_PAD_LEFT) . '-' . $bcity;
1351 }
1352 $acity .= '-' . mb_strtolower($a['name']);
1353 $bcity .= '-' . mb_strtolower($b['name']);
1354 return $acity < $bcity ? -1 : 1;
1355 });
1356 }
1357 return $machines;
1358 }
1359
1360 function add_parcelmachine_checkout_fields($fragments) {
1361 $this->order_country = WC()->customer->get_shipping_country();
1362 $res = '';
1363 $res .= '<tr style="display: none;" class="parcel_machine_checkout parcel_machine_checkout_parcelmachine_'.mb_strtolower($this->ext).'">';
1364 // echo '<th>' . $this->title . '</th>';
1365 // echo '<td>';
1366 $res .= '<td colspan="2">';
1367 $options = array();
1368 $machines = $this->mk_get_machines();
1369 $res .= '<p class="form-row" id="'.esc_attr($this->id).'_field">';
1370 $res .= '<select class="select" name="'.esc_attr($this->id).'" id="'.esc_attr($this->id).'">';
1371 $res .= '<option value="">'.__('-- select parcel machine --', 'wc_makecommerce_domain').'</option>';
1372 $pcity = false;
1373 foreach ($machines as $machine) {
1374 $city = strtolower($machine['city']);
1375 if ($city !== $pcity) {
1376 if ($pcity) $res .= '</optgroup>';
1377 $res .= '<optgroup label="'.$machine['city'].'">';
1378 }
1379 $mname = $machine['name'];
1380 if ($this->short_office_names !== 'yes') $mname .= ' - ' . $machine['city'] . ', ' . $machine['address'];
1381 $res .= '<option value="'.esc_attr($machine['provider'].'||'.$machine['id']).'">'.$mname.'</option>';
1382 $pcity = $city;
1383 }
1384 if ($pcity) $res .= '</optgroup>';
1385 $res .= '</select>';
1386 $res .= '</p>';
1387 $res .= '</td></tr>';
1388 echo $res;
1389 }
1390
1391 function check_parcelmachine_checkout_fields() {
1392 $shipping_method = !empty($_POST['shipping_method']) ? $_POST['shipping_method'] : false;
1393 if (!empty($shipping_method[0])) { $shipping_method = $shipping_method[0]; }
1394 error_log($shipping_method);
1395 error_log(print_r($_POST, 1));
1396 if ($shipping_method === $this->id && empty($_POST[$shipping_method])) {
1397 wc_add_notice(__('<strong>Parcel machine</strong> is a required field.'), 'error');
1398 }
1399 }
1400 function add_parcelmachine_order_meta($order_id) {
1401 $shipping_method = !empty($_POST['shipping_method']) ? $_POST['shipping_method'] : false;
1402 if (!empty($shipping_method[0])) { $shipping_method = $shipping_method[0]; }
1403 if ($shipping_method === $this->id && !empty($_POST[$this->id])) {
1404 update_post_meta($order_id, '_parcel_machine', sanitize_text_field($_POST[$this->id]));
1405 }
1406 }
1407 function add_admin_parcelmachine_via_field($order) {
1408 return ' <small>(via '.$this->name_ext.')</small>';
1409 }
1410 }
1411 class WC_ParcelMachine_Shipping_Method_Omniva extends WC_ParcelMachine_Shipping_Method {
1412 function __construct($instance_id = 0) {
1413 $this->ext = 'Omniva';
1414 $this->name_ext = 'Omniva';
1415 parent::__construct();
1416 }
1417 function init_form_fields() {
1418
1419 $this->form_fields = array();
1420 $this->form_fields['logo'] = array('type' => 'title', 'title' => __get_logo_html());
1421 $this->form_fields['generic'] = array('type' => 'title', 'title' => __('Generic and pricing options', 'wc_makecommerce_domain'));
1422 $this->form_fields['active'] = array(
1423 'title' => __('Enable', 'wc_makecommerce_domain'),
1424 'type' => 'checkbox',
1425 'label' => __('enabled', 'wc_makecommerce_domain'),
1426 'default' => 'no',
1427 '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'),
1428 );
1429 $this->form_fields['price_ee'] = array(
1430 'title' => __('Price EE', 'wc_makecommerce_domain'),
1431 'type' => 'text',
1432 'default' => '2.99'
1433 );
1434 $this->form_fields['price_lv'] = array(
1435 'title' => __('Price LV', 'wc_makecommerce_domain'),
1436 'type' => 'text',
1437 'default' => '7.99'
1438 );
1439 $this->form_fields['price_lt'] = array(
1440 'title' => __('Price LT', 'wc_makecommerce_domain'),
1441 'type' => 'text',
1442 'default' => '8.99'
1443 );
1444 $this->form_fields['free_shipping_min_amount'] = array(
1445 'title' => __('Minimum amount for free shipping', 'wc_makecommerce_domain'),
1446 'type' => 'number',
1447 'default' => '0',
1448 'description' => '(0 means no free shipping)'
1449 );
1450 $this->form_fields['maximum_weight'] = array(
1451 'title' => __('Maximum weight allowed for shipping', 'wc_makecommerce_domain'),
1452 'type' => 'text',
1453 'default' => '15'
1454 );
1455 $this->form_fields['countries'] = array(
1456 'title' => __('Specific Countries', 'wc_makecommerce_domain'),
1457 'type' => 'multiselect',
1458 'class' => 'wp-enhanced-select',
1459 'css' => 'width: 450px;',
1460 'default' => array('EE','LV','LT'),
1461 'options' => array('EE' => __('Estonia'), 'LV' => __('Latvia'), 'LT' => __('Lithuania')),
1462 );
1463 $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'));
1464 $languages = apply_filters('wpml_active_languages', NULL, 'skip_missing=0');
1465 if (empty($languages)) {
1466 $this->form_fields['method_name'] = array(
1467 'title' => __('Shipping Method Title', 'wc_makecommerce_domain'),
1468 'type' => 'text',
1469 'default' => __('Omniva Parcel Machine', 'wc_makecommerce_domain')
1470 );
1471 } else {
1472 foreach ($languages as $language_code => $language) {
1473 $language_name = !empty($language['translated_name']) ? $language['translated_name'] : $language_code;
1474 $this->form_fields['method_name_'.$language_code] = array(
1475 'title' => __('Shipping Method Title', 'wc_makecommerce_domain').sprintf(' (%s)', $language_code),
1476 'type' => 'text',
1477 'default' => __('Omniva Parcel Machine', 'wc_makecommerce_domain')
1478 );
1479 }
1480 }
1481 $this->form_fields['prioritization'] = array(
1482 'title' => __('Prioritize', 'wc_makecommerce_domain'),
1483 'type' => 'checkbox',
1484 'label' => __('Bigger cities will be on top of list, others sorted alphabetically', 'wc_makecommerce_domain'),
1485 'default' => 'yes'
1486 );
1487 $this->form_fields['short_office_names'] = array(
1488 'title' => __('Short names', 'wc_makecommerce_domain'),
1489 'type' => 'checkbox',
1490 'label' => __('Display only parcel machine names, without addresses', 'wc_makecommerce_domain'),
1491 'default' => 'no'
1492 );
1493 $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')));
1494 $this->form_fields['service_user'] = array(
1495 'title' => __('Omniva web services username', 'wc_makecommerce_domain'),
1496 'type' => 'text',
1497 'default' => ''
1498 );
1499 $this->form_fields['service_password'] = array(
1500 'title' => __('Omniva web services password', 'wc_makecommerce_domain'),
1501 'type' => 'text',
1502 'default' => ''
1503 );
1504 $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'));
1505 $this->form_fields['shop_name'] = array(
1506 'type' => 'text',
1507 'title' => __('Shop name', 'wc_makecommerce_domain'),
1508 'class' => 'input-text regular-input',
1509 );
1510 $this->form_fields['shop_phone'] = array(
1511 'type' => 'text',
1512 'title' => __('Shop phone', 'wc_makecommerce_domain'),
1513 'class' => 'input-text regular-input',
1514 );
1515 $this->form_fields['shop_email'] = array(
1516 'type' => 'text',
1517 'title' => __('Shop email', 'wc_makecommerce_domain'),
1518 'class' => 'input-text regular-input',
1519 );
1520 $this->form_fields['shop_postal_code'] = array(
1521 'type' => 'text',
1522 'title' => __('Shop postal code', 'wc_makecommerce_domain'),
1523 'class' => 'input-text regular-input',
1524 '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')
1525 );
1526 }
1527
1528 }
1529 class WC_ParcelMachine_Shipping_Method_Smartpost extends WC_ParcelMachine_Shipping_Method {
1530 function __construct() {
1531 $this->ext = 'SmartPost';
1532 $this->name_ext = 'SmartPOST';
1533 parent::__construct();
1534 }
1535 function init_form_fields() {
1536 global $woocommerce;
1537
1538 $this->form_fields = array();
1539 $this->form_fields['logo'] = array('type' => 'title', 'title' => __get_logo_html());
1540 $this->form_fields['generic'] = array( 'type' => 'title', 'title' => __('Generic and pricing options', 'wc_makecommerce_domain'));
1541 $this->form_fields['active'] = array(
1542 'title' => __('Enable', 'wc_makecommerce_domain'),
1543 'type' => 'checkbox',
1544 'label' => __('enabled', 'wc_makecommerce_domain'),
1545 'default' => 'no',
1546 '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'),
1547 );
1548 $this->form_fields['price_ee'] = array(
1549 'title' => __('Price EE', 'wc_makecommerce_domain'),
1550 'type' => 'text',
1551 'default' => '2.99'
1552 );
1553 $this->form_fields['price_fi'] = array(
1554 'title' => __('Price FI', 'wc_makecommerce_domain'),
1555 'type' => 'text',
1556 'default' => '10.99'
1557 );
1558 $this->form_fields['free_shipping_min_amount'] = array(
1559 'title' => __('Minimum amount for free shipping', 'wc_makecommerce_domain'),
1560 'type' => 'number',
1561 'default' => '0',
1562 'description' => __('(0 means no free shipping)', 'wc_makecommerce_domain'),
1563 );
1564 $this->form_fields['maximum_weight'] = array(
1565 'title' => __('Maximum weight allowed for shipping', 'wc_makecommerce_domain'),
1566 'type' => 'text',
1567 'default' => '15'
1568 );
1569 $this->form_fields['countries'] = array(
1570 'title' => __('Specific Countries', 'wc_makecommerce_domain'),
1571 'type' => 'multiselect',
1572 'class' => 'wp-enhanced-select',
1573 'css' => 'width: 450px;',
1574 'default' => array('EE','FI'),
1575 'options' => array('EE' => __('Estonia'), 'FI' => __('Finland')),
1576 );
1577 $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'));
1578 $languages = apply_filters('wpml_active_languages', NULL, 'skip_missing=0');
1579 if (empty($languages)) {
1580 $this->form_fields['method_name'] = array(
1581 'title' => __('Shipping Method Title', 'wc_makecommerce_domain'),
1582 'type' => 'text',
1583 'default' => __('SmartPOST Parcel Machine', 'wc_makecommerce_domain')
1584 );
1585 } else {
1586 foreach ($languages as $language_code => $language) {
1587 $language_name = !empty($language['translated_name']) ? $language['translated_name'] : $language_code;
1588 $this->form_fields['method_name_'.$language_code] = array(
1589 'title' => __('Shipping Method Title', 'wc_makecommerce_domain').sprintf(' (%s)', $language_code),
1590 'type' => 'text',
1591 'default' => __('SmartPOST Parcel Machine', 'wc_makecommerce_domain')
1592 );
1593 }
1594 }
1595 $this->form_fields['prioritization'] = array(
1596 'title' => __('Prioritize', 'wc_makecommerce_domain'),
1597 'type' => 'checkbox',
1598 'label' => __('Bigger cities will be on top of list, others sorted alphabetically', 'wc_makecommerce_domain'),
1599 'default' => 'yes'
1600 );
1601 $this->form_fields['short_office_names'] = array(
1602 'title' => __('Short names', 'wc_makecommerce_domain'),
1603 'type' => 'checkbox',
1604 'label' => __('Display only parcel machine names, without addresses', 'wc_makecommerce_domain'),
1605 'default' => 'no'
1606 );
1607 $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')));
1608 $this->form_fields['service_user'] = array(
1609 'title' => __('eteenindus.smartpost.ee username', 'wc_makecommerce_domain'),
1610 'type' => 'text',
1611 'default' => ''
1612 );
1613 $this->form_fields['service_password'] = array(
1614 'title' => __('eteenindus.smartpost.ee password', 'wc_makecommerce_domain'),
1615 'type' => 'text',
1616 'default' => ''
1617 );
1618 $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'));
1619 $this->form_fields['shop_name'] = array(
1620 'type' => 'text',
1621 'title' => __('Shop name', 'wc_makecommerce_domain'),
1622 'class' => 'input-text regular-input',
1623 );
1624 $this->form_fields['shop_phone'] = array(
1625 'type' => 'text',
1626 'title' => __('Shop phone', 'wc_makecommerce_domain'),
1627 'class' => 'input-text regular-input',
1628 );
1629 $this->form_fields['shop_email'] = array(
1630 'type' => 'text',
1631 'title' => __('Shop email', 'wc_makecommerce_domain'),
1632 'class' => 'input-text regular-input',
1633 );
1634 }
1635
1636 }
1637 }
1638 }
1639 add_action('woocommerce_shipping_init', 'parcelmachine_add_method');
1640
1641 function add_parcelmachine_shipping_method($methods) {
1642 $methods[] = 'WC_ParcelMachine_Shipping_Method_Omniva';
1643 $methods[] = 'WC_ParcelMachine_Shipping_Method_Smartpost';
1644 return $methods;
1645 }
1646 add_filter('woocommerce_shipping_methods', 'add_parcelmachine_shipping_method');
1647
1648 function mk_parcelmachine_add_assets() {
1649 wp_enqueue_style('parcelmachine-css', plugin_dir_url(__FILE__).'/css/parcelmachine.css');
1650 wp_enqueue_script('parcelmachine-js', plugin_dir_url(__FILE__).'/scripts/parcelmachine.js', array('jquery'));
1651 }
1652 add_action('wp_enqueue_scripts', 'mk_parcelmachine_add_assets');
1653
1654 function mk_admin_plugin_settings_link($links) {
1655 $settings_link = '<a href="admin.php?page=wc-settings&tab=api&section=mk_api">API '.__('Settings').'</a>';
1656 array_unshift($links, $settings_link);
1657 return $links;
1658 }
1659 $plugin = plugin_basename(__FILE__);
1660 add_filter("plugin_action_links_$plugin", 'mk_admin_plugin_settings_link' );
1661
1662 function mk_admin_add_settings($sections) {
1663 $sections['mk_api'] = __('MakeCommerce API access', 'wc_makecommerce_domain');
1664 return $sections;
1665 }
1666
1667 function mk_admin_all_settings($settings) {
1668 global $current_section;
1669 if ($current_section !== 'mk_api') {
1670 return $settings;
1671 }
1672 return array(
1673 array('type' => 'title', 'desc' => __get_logo_html()),
1674 array(
1675 'type' => 'title',
1676 'title' => __('MakeCommerce API access credentials', 'wc_makecommerce_domain'),
1677 'desc' => sprintf(__('To use MakeCommerce/Maksekeskus services you need to enter API credentials below here <br/> <br/>'.
1678 'To further configure the Payment methods please go to <a href="%s">MakeCommerce Checkout Options</a><br>'.
1679 'To use also Ominva or Itella SmartPOST integration please configure these API accesses: <a href="%s">Omniva API access</a> | '.
1680 '<a href="%s">SmartPOST API access</a><br/>','wc_makecommerce_domain'),
1681 'admin.php?page=wc-settings&tab=checkout&section=makecommerce',
1682 'admin.php?page=wc-settings&tab=-shipping&section=parcelmachine_omniva',
1683 'admin.php?page=wc-settings&tab=shipping&section=parcelmachine_smartpost'
1684 ),
1685 'id' => 'mk_api_settings'
1686 ),
1687 array(
1688 'type' => 'select',
1689 'title' => __('Current environment', 'wc_makecommerce_domain'),
1690 'desc' => __('See more about <a href="https://maksekeskus.ee/en/for-developers/test-environment/">MakeCommerce Test environment</a>', 'wc_makecommerce_domain'),
1691 'default' => 'live',
1692 'options' => array(
1693 'live' => __('Live', 'wc_makecommerce_domain'),
1694 'test' => __('Test', 'wc_makecommerce_domain'),
1695 ),
1696 'id' => 'mk_api_type'
1697 ),
1698 array(
1699 'id' => 'mk_shop_id',
1700 'type' => 'text',
1701 'title' => __('Shop ID (live)', 'wc_makecommerce_domain'),
1702 'desc' => __('Get it from <a href="https://merchant.maksekeskus.ee/api.html" target="_blank">Merchant Portal</a>','wc_makecommerce_domain'),
1703 'class' => 'input-text regular-input',
1704 ),
1705 array(
1706 'id' => 'mk_private_key',
1707 'type' => 'text',
1708 'title' => __('Secret key (live)', 'wc_makecommerce_domain'),
1709 'class' => 'input-text regular-input',
1710 ),
1711 array(
1712 'id' => 'mk_public_key',
1713 'type' => 'text',
1714 'title' => __('Publishable key (live)', 'wc_makecommerce_domain'),
1715 'class' => 'input-text regular-input',
1716 ),
1717 array(
1718 'id' => 'mk_test_shop_id',
1719 'type' => 'text',
1720 'title' => __('Shop ID (test)', 'wc_makecommerce_domain'),
1721 'class' => 'input-text regular-input',
1722 'desc' => __('Get it from <a href="https://merchant-test.maksekeskus.ee/api.html" target="_blank">Merchant Portal Test</a>','wc_makecommerce_domain'),
1723 ),
1724 array(
1725 'id' => 'mk_test_private_key',
1726 'type' => 'text',
1727 'title' => __('Secret key (test)', 'wc_makecommerce_domain'),
1728 'class' => 'input-text regular-input',
1729 ),
1730 array(
1731 'id' => 'mk_test_public_key',
1732 'type' => 'text',
1733 'title' => __('Publishable key (test)', 'wc_makecommerce_domain'),
1734 'class' => 'input-text regular-input',
1735 ),
1736 array('type' => 'sectionend', 'id' => 'mk_api_settings')
1737 );
1738 }
1739
1740 function mk_admin_save_settings() {
1741 // reload banklinks
1742 global $current_section;
1743 if ($current_section !== 'mk_api') {
1744 return;
1745 }
1746 $wcmc = New woocommerce_makecommerce(true);
1747 $wcmc->mc_banklinks_reload(true);
1748 }
1749
1750 function __get_logo_html() {
1751 return '<div class="makecommerce-info">'.
1752 '<div class="makecommerce-logo">'.
1753 '<a target="_blank" href="http://maksekeskus.ee"><img src="'. plugins_url('/images/makecommerce_logo_en.svg', __FILE__) .'" class="makecommerce-logo"></a>'.
1754 '</div>'.
1755 '<div class="makecommerce-links">'.
1756 '<div class="makecommerce-link"><a target="_blank" href="https://merchant.maksekeskus.ee">Merchant Portal</a></div>'.
1757 '<div class="makecommerce-link"><a target="_blank" href="https://makecommerce.net/">makecommerce.net</a></div>'.
1758 '<div class="makecommerce-link"><a target="_blank" href="http://maksekeskus.ee">maksekeskus.ee</a></div>'.
1759 '</div>'.
1760 '</div>';
1761 }
1762
1763 add_action('woocommerce_get_sections_api', 'mk_admin_add_settings');
1764 add_filter('woocommerce_get_settings_api', 'mk_admin_all_settings', 10, 2);
1765 add_action('woocommerce_settings_saved', 'mk_admin_save_settings', 30, 0);
1766
1767 function mk_admin_restrict_manage_posts() {
1768 global $typenow;
1769 if ( in_array( $typenow, wc_get_order_types( 'order-meta-boxes' ) ) ) {
1770 $selected_method = !empty($_REQUEST['_shipping_method']) ? $_REQUEST['_shipping_method'] : false;
1771 $methods = WC()->shipping->load_shipping_methods();
1772 echo '<select name="_shipping_method" id="shipping_type" class="enhanced">';
1773 echo '<option value="">'.__('-- filter by shipping method', 'wc_makecommerce_domain') . '</option>';
1774 foreach ($methods as $method) {
1775 echo '<option value="'.$method->id.'"'.($selected_method === $method->id ? ' selected="selected"' : '').'>'.$method->title.'</option>';
1776 }
1777 echo '</select>';
1778 }
1779 }
1780 function mk_admin_shipping_filter($where, &$wp_query) {
1781 global $pagenow, $wpdb;
1782 $method = !empty($_REQUEST['_shipping_method']) ? $_REQUEST['_shipping_method'] : false;
1783 if (is_admin() && $pagenow=='edit.php' && $wp_query->query_vars['post_type'] == 'shop_order' && !empty($method) ) {
1784 $where .= $GLOBALS['wpdb']->prepare( ' AND ID
1785 IN (
1786 SELECT items.order_id
1787 FROM '.$wpdb->prefix.'woocommerce_order_itemmeta meta, '.$wpdb->prefix.'woocommerce_order_items items
1788 WHERE meta.order_item_id = items.order_item_id
1789 AND meta.meta_key = "method_id"
1790 AND meta.meta_value = %s
1791 ) ', $method );
1792 }
1793 return $where;
1794 }
1795 add_filter('restrict_manage_posts', 'mk_admin_restrict_manage_posts');
1796 add_filter('posts_where', 'mk_admin_shipping_filter', 10, 2);
1797
1798 function mk_admin_bulk_actions() {
1799 global $post_type;
1800 if ('shop_order' === $post_type) {
1801 ?>
1802 <script type="text/javascript">
1803 jQuery(function() {
1804 jQuery('<option>').val('parcel_machine_labels').text('<?php _e( 'Register parcel machine shipments', 'wc_makecommerce_domain' )?>').appendTo('select[name="action"]');
1805 jQuery('<option>').val('parcel_machine_labels').text('<?php _e( 'Register parcel machine shipments', 'wc_makecommerce_domain' )?>').appendTo('select[name="action2"]');
1806 jQuery('<option>').val('parcel_machine_print_labels').text('<?php _e( 'Print parcel machine labels', 'wc_makecommerce_domain' )?>').appendTo('select[name="action"]');
1807 jQuery('<option>').val('parcel_machine_print_labels').text('<?php _e( 'Print parcel machine labels', 'wc_makecommerce_domain' )?>').appendTo('select[name="action2"]');
1808 });
1809 </script>
1810 <?php
1811 if (!empty($_REQUEST['mk_pdf'])) {
1812 ?>
1813 <script type="text/javascript">
1814 jQuery(function(){
1815 window.open('<?php echo $_REQUEST['mk_pdf'] ?>', 'pdf');
1816 });
1817 </script>
1818 <?php
1819 }
1820 }
1821 }
1822 function mk_admin_bulk_action_labels() {
1823 $wp_list_table = _get_list_table('WP_Posts_List_Table');
1824 $shipping_request = array('credentials' => array(), 'orders' => array());
1825 $post_ids = array_map('absint', (array)$_REQUEST['post']);
1826 mk_get_shipment_ids($post_ids);
1827 }
1828 function mk_admin_bulk_action_print() {
1829 $wp_list_table = _get_list_table('WP_Posts_List_Table');
1830 $shipping_request = array('credentials' => array(), 'orders' => array());
1831 $post_ids = array_map('absint', (array)$_REQUEST['post']);
1832 mk_get_labels($post_ids);
1833 }
1834 add_action('admin_footer', 'mk_admin_bulk_actions', 11);
1835 add_action('admin_action_parcel_machine_labels', 'mk_admin_bulk_action_labels');
1836 add_action('admin_action_parcel_machine_print_labels', 'mk_admin_bulk_action_print');
1837
1838
1839
1840 function mk_admin_parcelmachine_order_meta($order) {
1841 $machine_id = get_post_meta($order->id, '_parcel_machine', true);
1842 if (empty($machine_id)) return;
1843 list($provider, $machine) = explode('||', $machine_id);
1844 if (empty($machine)) return;
1845 $machine = mk_get_machine($provider, (int)$machine);
1846 if (!$machine) return;
1847 echo '<p><strong>'.__('Parcel machine').':</strong><br/>' . $machine['name'] . '<br/><small>' . $machine['address'] . '</small></p>';
1848 $shipment_id = get_post_meta($order->id, '_parcel_machine_shipment_id', true);
1849 $shipment_id_error = get_post_meta($order->id, '_parcel_machine_error', true);
1850 if ($shipment_id) {
1851 echo '<p><strong>'.__('Parcel machine shipment ID').':</strong><br/>' . $shipment_id . '</small></p>';
1852 }
1853 if ($shipment_id_error) {
1854 echo '<p><strong style="color: red;">'.__('Parcel machine shipment generation error').':</strong><br/>' . $shipment_id_error . '</small></p>';
1855 }
1856 }
1857
1858 function mk_admin_render_shop_order_columns( $column ) {
1859 global $post, $woocommerce, $the_order;
1860 if (empty($the_order) || $the_order->id != $post->ID) {
1861 $the_order = wc_get_order($post->ID);
1862 }
1863 if ($column === 'shipping_address') {
1864 $machine = get_post_meta($the_order->id, '_parcel_machine', true);
1865 $shipment_id = get_post_meta($the_order->id, '_parcel_machine_shipment_id', true);
1866 $shipment_id_error = get_post_meta($the_order->id, '_parcel_machine_error', true);
1867 if ($machine) {
1868 if ($shipment_id) echo __('Package shipment_id:', 'wc_makecommerce_domain') . ' ' . $shipment_id;
1869 else if ($shipment_id_error) echo '<span style="color: red;">'.__('Package shipment generation error:', 'wc_makecommerce_domain') . '</span><br/>' .$shipment_id_error;
1870 else echo __('Shipment ID not generated for delivery', 'wc_makecommerce_domain');
1871 }
1872 }
1873 }
1874 function mk_add_email_customer_details_fields($fields, $sent_to_admin, $order) {
1875 $machine_id = get_post_meta($order->id, '_parcel_machine', true);
1876 if (empty($machine_id)) return $fields;
1877 list($provider, $machine) = explode('||', $machine_id);
1878 if (empty($machine)) return $fields;
1879 $machine = mk_get_machine($provider, (int)$machine);
1880 if (!$machine) return $fields;
1881 $fields[] = array('label' => __('Parcel machine'), 'value' => $machine['name'].' - '.$machine['address']);
1882 return $fields;
1883 }
1884 function mk_add_order_customer_details_fields($order) {
1885 $machine_id = get_post_meta($order->id, '_parcel_machine', true);
1886 if (empty($machine_id)) return;
1887 list($provider, $machine) = explode('||', $machine_id);
1888 if (empty($machine)) return;
1889 $machine = mk_get_machine($provider, (int)$machine);
1890 if (!$machine) return;
1891 echo '<tr>';
1892 echo '<th>'.__('Parcel machine').'</th>';
1893 echo '<td>'.$machine['name'].'<br/>'.$machine['address'].'</td>';
1894 echo '</tr>';
1895 }
1896 function mk_admin_product_option_fields($fields) {
1897 echo '<div class="options_group">';
1898 woocommerce_wp_checkbox(
1899 array(
1900 'id' => '_no_parcel_machine',
1901 'wrapper_class' => 'show_if_simple',
1902 'label' => __('Does not fit parcel machine', 'wc_makecommerce_domain'),
1903 'description' => __('When this is checked, parcel machine shipping option is not available for a cart with this product', 'wc_makecommerce_domain')
1904 )
1905 );
1906 echo '</div>';
1907 }
1908 function mk_admin_product_fields_save($post_id) {
1909 $no_parcel_machine = isset($_POST['_no_parcel_machine']) ? 'yes' : 'no';
1910 update_post_meta($post_id, '_no_parcel_machine', $no_parcel_machine);
1911 }
1912
1913 add_action('woocommerce_product_options_shipping', 'mk_admin_product_option_fields');
1914 add_action('woocommerce_process_product_meta', 'mk_admin_product_fields_save');
1915 add_action('woocommerce_admin_order_data_after_shipping_address', 'mk_admin_parcelmachine_order_meta', 10, 1);
1916 add_action('manage_shop_order_posts_custom_column', 'mk_admin_render_shop_order_columns', 3);
1917 add_filter('woocommerce_email_customer_details_fields', 'mk_add_email_customer_details_fields', 10, 3 );
1918 add_action('woocommerce_order_details_after_customer_details', 'mk_add_order_customer_details_fields');
1919 add_action('woocommerce_order_status_processing', 'mk_get_shipment_ids');
1920
1921
1922
1923 function mk_get_shipment_ids($post_ids) {
1924 if (!is_array($post_ids)) {
1925 $post_ids = array($post_ids);
1926 }
1927 parcelmachine_add_method();
1928 $shipping_request = array('credentials' => array(), 'orders' => array());
1929 foreach ($post_ids as $post_id) {
1930 $parcel_machine = get_post_meta($post_id, '_parcel_machine', true);
1931 if (!$parcel_machine) { continue; }
1932 list($provider, $machine_id) = explode('||', $parcel_machine);
1933 if (!$provider || !$machine_id) { continue; }
1934 $provider_uc = mb_strtoupper($provider);
1935 switch ($provider_uc) {
1936 case "OMNIVA":
1937 $transport_class = new WC_ParcelMachine_Shipping_Method_Omniva();
1938 break;
1939 case "SMARTPOST":
1940 $transport_class = new WC_ParcelMachine_Shipping_Method_Smartpost();
1941 break;
1942 default:
1943 break 2;
1944 }
1945 if (empty($shipping_request['credentials'][$provider_uc])) {
1946 $api_user = $transport_class->settings['service_user'];
1947 $api_password = $transport_class->settings['service_password'];
1948 if (!$api_user || !$api_password) {
1949 add_action('admin_notices', 'mk_admin_error');
1950 continue;
1951 }
1952 $shipping_request['credentials'][$provider_uc] = array('carrier' => $provider_uc, 'username' => $api_user, 'password' => $api_password);
1953 }
1954 $order = wc_get_order($post_id);
1955 $sender = array(
1956 'name' => $transport_class->settings['shop_name'],
1957 'phone' => $transport_class->settings['shop_phone'],
1958 'email' => $transport_class->settings['shop_email'],
1959 'postalCode' => $transport_class->settings['shop_postal_code']
1960 );
1961 $shipping_request['orders'][] = array(
1962 'carrier' => $provider_uc,
1963 'orderId' => $order->id,
1964 'destination' => array('destinationId' => $machine_id),
1965 'recipient' => array('name' => $order->shipping_first_name . ' ' . $order->shipping_last_name, 'phone' => $order->billing_phone, 'email' => $order->billing_email),
1966 'sender' => $sender
1967 );
1968 }
1969 $shipping_request['credentials'] = array_values($shipping_request['credentials']);
1970 if (empty($shipping_request['orders'])) {
1971 return;
1972 }
1973 $MK = mk_get_api();
1974 if (!$MK) {
1975 return;
1976 }
1977 try {
1978 $response = $MK->createShipments($shipping_request);
1979 } catch (Exception $e) {
1980 echo $e->getMessage();
1981 exit();
1982 }
1983 foreach ($response as $order) {
1984 if (!empty($order->orderId) && !empty($order->shipmentId)) {
1985 update_post_meta((int)$order->orderId, '_parcel_machine_shipment_id', sanitize_text_field($order->shipmentId));
1986 } else if (!empty($order->orderId) && !empty($order->barCode)) {
1987 update_post_meta((int)$order->orderId, '_parcel_machine_shipment_id', sanitize_text_field($order->barCode));
1988 } else if (!empty($order->orderId) && !empty($order->errorMessage)) {
1989 update_post_meta((int)$order->orderId, '_parcel_machine_error', sanitize_text_field($order->errorMessage));
1990 }
1991 }
1992 }
1993
1994 function mk_get_labels($post_ids) {
1995 if (!is_array($post_ids)) {
1996 $post_ids = array($post_ids);
1997 }
1998 $sender = array(
1999 'name' => get_option('mk_shop_name', ''),
2000 'phone' => get_option('mk_shop_phone', ''),
2001 'email' => get_option('mk_shop_email', ''),
2002 'postalCode' => get_option('mk_shop_postal_code', '')
2003 );
2004 parcelmachine_add_method();
2005 $shipping_request = array('credentials' => array(), 'orders' => array(), 'printFormat' => 'A4');
2006 foreach ($post_ids as $post_id) {
2007 $parcel_machine = get_post_meta($post_id, '_parcel_machine', true);
2008 if (!$parcel_machine) { continue; }
2009 list($provider, $machine_id) = explode('||', $parcel_machine);
2010 if (!$provider || !$machine_id) { continue; }
2011 $provider_uc = mb_strtoupper($provider);
2012 if (empty($shipping_request['credentials'][$provider_uc])) {
2013 switch ($provider_uc) {
2014 case "OMNIVA":
2015 $transport_class = new WC_ParcelMachine_Shipping_Method_Omniva();
2016 break;
2017 case "SMARTPOST":
2018 $transport_class = new WC_ParcelMachine_Shipping_Method_Smartpost();
2019 break;
2020 default:
2021 break 2;
2022 }
2023 $api_user = $transport_class->settings['service_user'];
2024 $api_password = $transport_class->settings['service_password'];
2025 if (!$api_user || !$api_password) {
2026 add_action('admin_notices', 'mk_admin_error');
2027 continue;
2028 }
2029 $shipping_request['credentials'][$provider_uc] = array('carrier' => $provider_uc, 'username' => $api_user, 'password' => $api_password);
2030 }
2031 $order = wc_get_order($post_id);
2032 $shipment_id = get_post_meta($post_id, '_parcel_machine_shipment_id', true);
2033 if (!$shipment_id) { continue; }
2034 $shipping_request['orders'][] = array(
2035 'carrier' => $provider_uc,
2036 'orderId' => $order->id,
2037 'destination' => array('destinationId' => $machine_id),
2038 'recipient' => array('name' => $order->shipping_first_name . ' ' . $order->shipping_last_name, 'phone' => $order->billing_phone, 'email' => $order->billing_email),
2039 'sender' => $sender,
2040 'shipmentId' => $shipment_id
2041 );
2042 }
2043 $shipping_request['credentials'] = array_values($shipping_request['credentials']);
2044 if (empty($shipping_request['orders'])) {
2045 return;
2046 }
2047 $MK = mk_get_api();
2048 if (!$MK) {
2049 return;
2050 }
2051 try {
2052 $response = $MK->createLabels($shipping_request);
2053 } catch (Exception $e) {
2054 echo $e->getMessage();
2055 exit();
2056 }
2057 if (empty($response->labelUrl)) {
2058 return;
2059 }
2060 $sendback = add_query_arg(array('post_type' => 'shop_order', 'mk_pdf' => urlencode($response->labelUrl)), '');
2061 wp_redirect(esc_url_raw($sendback));
2062 exit();
2063 }
2064
2065
2066
2067 $MKAPI = false;
2068 function mk_get_api() {
2069 global $MKAPI;
2070 if ($MKAPI) {
2071 //return $MKAPI;
2072 }
2073 $mk_api_type = get_option('mk_api_type', false);
2074 if (!$mk_api_type) {
2075 return false;
2076 }
2077 if ($mk_api_type !== 'live') {
2078 $key_prefix = $mk_api_type.'_';
2079 } else {
2080 $key_prefix = '';
2081 }
2082 $mk_shop_id = get_option('mk_'.$key_prefix.'shop_id', '');
2083 $mk_public_key = get_option('mk_'.$key_prefix.'public_key', '');
2084 $mk_private_key = get_option('mk_'.$key_prefix.'private_key', '');
2085 if (!$mk_shop_id || !$mk_public_key || !$mk_shop_id) {
2086 return false;
2087 }
2088 $MKAPI = new Maksekeskus($mk_shop_id, $mk_public_key, $mk_private_key, $mk_api_type === 'live' ? false : true);
2089 return $MKAPI;
2090 }
2091 function mk_get_machines($provider, $country = 'EE') {
2092 $data = get_option('mk_machines_cache', false);
2093 $data_expires = get_option('mk_machines_expires', false);
2094 if (!$data || $data_expires < time()) {
2095 $MK = mk_get_api();
2096 $data = $MK->getDestinations(array('type' => 'APT'));
2097 update_option('mk_machines_cache', $data);
2098 update_option('mk_machines_expires', time()+3*60*60);
2099 }
2100
2101 $machines = array();
2102 if (!$data || empty($data)) {
2103 return array();
2104 }
2105 foreach ($data as $machine) {
2106 if ($machine->country === $country && $machine->type === 'APT' && ($provider === '*' || strtolower($provider) === strtolower($machine->provider))) {
2107 $machines[] = array(
2108 'provider' => strtolower($machine->provider),
2109 'id' => $machine->id,
2110 'name' => $machine->name,
2111 'city' => $machine->city,
2112 'address' => !empty($machine->address) ? $machine->address : '',
2113 );
2114 }
2115 }
2116 usort($machines, function($a, $b){
2117 if ($a['city'] === $b['city']) {
2118 return $a['name'] > $b['name'];
2119 }
2120 return $a['city'] > $b['city'];
2121 });
2122 return $machines;
2123 }
2124
2125 function mk_get_machine($provider, $id) {
2126 $machines = mk_get_machines($provider);
2127 foreach ($machines as $machine) {
2128 if ($machine['provider'] === $provider && $machine['id'] === $id) {
2129 return $machine;
2130 }
2131 }
2132 return false;
2133 }
2134 function mk_admin_error() {
2135 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'));
2136 }
2137 }
2138