PluginProbe
MakeCommerce for WooCommerce / 1.1.2
MakeCommerce for WooCommerce v1.1.2
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.2, at makecommerce.php

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