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

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

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