| @@ -1,46 +1,2927 @@ | ||
| 1 | 1 | <?php |
| 2 | -/** | |
| 3 | - * @link https://makecommerce.net/ | |
| 4 | - * @since 4.0.0 | |
| 5 | - * @package Makecommerce | |
| 6 | - * | |
| 7 | - * @wordpress-plugin | |
| 8 | - * Plugin Name: MakeCommerce | |
| 9 | - * Plugin URI: https://makecommerce.net/ | |
| 10 | - * Description: Adds MakeCommerce payment gateway and shipping methods to WooCommerce checkout | |
| 11 | - * Version: 4.1.0 | |
| 12 | - * Author: Maksekeskus AS | |
| 13 | - * Author URI: https://makecommerce.net/ | |
| 14 | - * License: GPL-2.0+ | |
| 15 | - * License URI: http://www.gnu.org/licenses/gpl-2.0.txt | |
| 16 | - * Text Domain: makecommerce | |
| 17 | - * Domain Path: /languages | |
| 18 | - * Requires Plugins: woocommerce | |
| 19 | - * Requires at least: 6.8.1 | |
| 20 | - * Requires PHP: 8.1 | |
| 21 | - * WC requires at least: 9.9.3 | |
| 22 | - * WC tested up to: 11.0.1 | |
| 23 | - * WC HPOS compatibility: yes | |
| 24 | - * WC Legacy Order Table Compatibility: yes | |
| 25 | - */ | |
| 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: 2.0.0 | |
| 6 | + Author: Maksekeskus AS | |
| 7 | + Author URI: https://MakeCommerce.net/ | |
| 8 | + Text Domain: wc_makecommerce_domain | |
| 9 | +*/ | |
| 26 | 10 | |
| 27 | -function mc_table_exists(string $table): bool { | |
| 28 | - global $wpdb; | |
| 29 | - $table_name = $wpdb->prefix . $table; | |
| 30 | - return $wpdb->get_var($wpdb->prepare("SHOW TABLES LIKE %s", $table_name)) === $table_name; | |
| 11 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 12 | + exit; // Exit if accessed directly. | |
| 31 | 13 | } |
| 32 | 14 | |
| 15 | +if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) { | |
| 33 | 16 | |
| 34 | -if ( | |
| 35 | - ( | |
| 36 | - get_option('mc_version') !== false || | |
| 37 | - get_option('wc_mc_version') !== false || | |
| 38 | - mc_table_exists('mc_banklinks') | |
| 39 | - ) && | |
| 40 | - !filter_var(get_option('mc_shipping_plus'), FILTER_VALIDATE_BOOLEAN) | |
| 41 | -) { | |
| 42 | - require_once plugin_dir_path(__FILE__) . 'config.php'; | |
| 43 | -} else { | |
| 44 | - require_once plugin_dir_path(__FILE__) . 'makecommerce/makecommerce.php'; | |
| 17 | + | |
| 18 | + define('WC_MC_VERSION', 2.0); | |
| 19 | + | |
| 20 | + global $mkDbTable; | |
| 21 | + $mkDbTable = 'mc_banklinks'; | |
| 22 | + | |
| 23 | + function mc_install() { | |
| 24 | + global $wpdb; | |
| 25 | + global $mkDbTable; | |
| 26 | + | |
| 27 | + $tableName = $wpdb->prefix . $mkDbTable; | |
| 28 | + | |
| 29 | + $charset = $wpdb->get_charset_collate(); | |
| 30 | + | |
| 31 | + $sql = "CREATE TABLE $tableName ( | |
| 32 | + id mediumint(9) NOT NULL AUTO_INCREMENT, | |
| 33 | + type varchar(10) NOT NULL, | |
| 34 | + country char(2) NOT NULL, | |
| 35 | + name varchar(25) NOT NULL, | |
| 36 | + url varchar(250) NOT NULL, | |
| 37 | + UNIQUE KEY id (id) | |
| 38 | + ) $charset;"; | |
| 39 | + | |
| 40 | + require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); | |
| 41 | + dbDelta( $sql ); | |
| 42 | + } | |
| 43 | + | |
| 44 | + register_activation_hook( __FILE__, 'mc_install' ); | |
| 45 | + | |
| 46 | + // On uninstall remove scheduled hook | |
| 47 | + function mc_uninstall() { | |
| 48 | + if (wp_next_scheduled ( 'mc_banklinks_update_cron' )) { | |
| 49 | + wp_clear_scheduled_hook('mc_banklinks_update_cron'); | |
| 50 | + } | |
| 51 | + } | |
| 52 | + | |
| 53 | + register_deactivation_hook(__FILE__, 'mc_uninstall'); | |
| 54 | + | |
| 55 | + // WP schedule callable action | |
| 56 | + add_action('mc_banklinks_update_cron', 'update_mc_banklinks'); | |
| 57 | + | |
| 58 | + if (!class_exists('wc_makecommerce_domain')) { | |
| 59 | + require_once('includes/Api.php'); | |
| 60 | + } | |
| 61 | + | |
| 62 | + | |
| 63 | + function woocommerce_payment_makecommerce_init() { | |
| 64 | + | |
| 65 | + load_plugin_textdomain('wc_makecommerce_domain', false, dirname(plugin_basename(__FILE__)) . '/'); | |
| 66 | + | |
| 67 | + class woocommerce_makecommerce extends WC_Payment_Gateway { | |
| 68 | + | |
| 69 | + const MC_CANCELLED = 'CANCELLED'; | |
| 70 | + const MC_COMPLETED = 'COMPLETED'; | |
| 71 | + const MC_DEPOSITED = 'DEPOSITED'; | |
| 72 | + | |
| 73 | + const MC_PART_REFUNDED = 'PART_REFUNDED'; | |
| 74 | + const MC_REFUNDED = 'REFUNDED'; | |
| 75 | + | |
| 76 | + const module_name = 'MakeCommerce'; | |
| 77 | + const api_url = 'https://api.maksekeskus.ee/v1/'; | |
| 78 | + const gateway_url = 'https://payment.maksekeskus.ee/pay/1/signed.html'; | |
| 79 | + const gateway_url_static = 'https://payment.maksekeskus.ee/checkout/dist/'; | |
| 80 | + const fo_url = 'https://merchant.maksekeskus.ee/'; | |
| 81 | + const test_api_url = 'https://api-test.maksekeskus.ee/v1/'; | |
| 82 | + const test_gateway_url = 'https://payment-test.maksekeskus.ee/pay/1/signed.html'; | |
| 83 | + const test_gateway_url_static = 'https://payment-test.maksekeskus.ee/checkout/dist/'; | |
| 84 | + const test_fo_url = 'https://merchant-test.maksekeskus.ee/'; | |
| 85 | + const billing_descriptor_dba = ''; | |
| 86 | + const currencies_allowed = 'EUR'; | |
| 87 | + const module_homepage_url = 'https://maksekeskus.ee/en/integration-modules/makecommerce-woocommerce-payment-plugin/'; | |
| 88 | + const testenv_homepage_url = 'http://maksekeskus.ee/en/for-developers/test-environment/'; | |
| 89 | + | |
| 90 | + public $id = 'makecommerce'; | |
| 91 | + public $version = '2.0.0'; | |
| 92 | + | |
| 93 | + public $return_url; | |
| 94 | + public $return_url_cancel; | |
| 95 | + | |
| 96 | + public $return_url_cc; | |
| 97 | + | |
| 98 | + protected $_shop_id; | |
| 99 | + protected $_api_key_secret; | |
| 100 | + protected $_api_key_public; | |
| 101 | + | |
| 102 | + protected $_gateway_url_static; | |
| 103 | + | |
| 104 | + protected $_banklinks = array(); | |
| 105 | + protected $_banklinks_grouped; | |
| 106 | + protected $_cards = array(); | |
| 107 | + | |
| 108 | + protected $_api; | |
| 109 | + | |
| 110 | + protected $_init; | |
| 111 | + | |
| 112 | + public function __construct($init = false) { | |
| 113 | + | |
| 114 | + $this->_init = $init; | |
| 115 | + | |
| 116 | + $this->return_url = site_url('/?makecommerce_return=1'); | |
| 117 | + $this->return_url_cancel = site_url('/?makecommerce_return=1'); | |
| 118 | + | |
| 119 | + $this->return_url_cc = site_url('/?makecommerce_return=2'); | |
| 120 | + | |
| 121 | + // Load the form fields. | |
| 122 | + $this->init_form_fields(); | |
| 123 | + | |
| 124 | + // Load the settings. | |
| 125 | + $this->init_settings(); | |
| 126 | + | |
| 127 | + $this->initBanklinks(); | |
| 128 | + | |
| 129 | + $this->title = $this->settings['ui_widget_title']; | |
| 130 | + if (defined('ICL_LANGUAGE_CODE') && !empty($this->settings['ui_widget_title_'.ICL_LANGUAGE_CODE])) { | |
| 131 | + $this->title = $this->settings['ui_widget_title_'.ICL_LANGUAGE_CODE]; | |
| 132 | + } | |
| 133 | + $this->method_title = 'MakeCommerce'; | |
| 134 | + $this->description = true; | |
| 135 | + | |
| 136 | + $this->_api = mk_get_api(); | |
| 137 | + if (!$this->_api && $this->_init) { | |
| 138 | + add_action( 'admin_notices', array(&$this, 'makecommerce_api_info_missing') ); | |
| 139 | + } | |
| 140 | + | |
| 141 | + if(get_option('mk_api_type', false) == 'live') { | |
| 142 | + $this->_gateway_url_static = self::gateway_url_static; | |
| 143 | + } elseif (get_option('mk_api_type', false) == 'test') { | |
| 144 | + $this->_gateway_url_static = self::test_gateway_url_static; | |
| 145 | + } | |
| 146 | + | |
| 147 | + $this->supports = array( | |
| 148 | + 'products', | |
| 149 | + 'refunds', | |
| 150 | + ); | |
| 151 | + | |
| 152 | + add_filter('query_vars', array(&$this, 'makecommerce_return_trigger')); | |
| 153 | + add_action('template_redirect', array(&$this, 'makecommerce_return_trigger_check')); | |
| 154 | + | |
| 155 | + add_action( 'wp_ajax_makecommerce_pay_token', array(&$this, 'makecommerce_pay_token') ); | |
| 156 | + add_action( 'wp_ajax_nopriv_makecommerce_pay_token', array(&$this, 'makecommerce_pay_token') ); | |
| 157 | + | |
| 158 | + if($this->_init == false) { | |
| 159 | + add_action('woocommerce_update_options_payment_gateways', array(&$this, 'process_admin_options')); | |
| 160 | + add_action('woocommerce_update_options_payment_gateways_' . $this->id, array(&$this, 'process_admin_options')); | |
| 161 | + | |
| 162 | + add_action('woocommerce_receipt_' . $this->id, array(&$this, 'receipt_page')); | |
| 163 | + add_action('woocommerce_admin_order_data_after_order_details', array(&$this, 'admin_order_page'), 10, 1); | |
| 164 | + | |
| 165 | + wp_enqueue_script('jquery'); | |
| 166 | + wp_enqueue_style('makecommerce', plugins_url('/css/makecommerce.css', __FILE__), array(), $this->version); | |
| 167 | + } | |
| 168 | + if (is_admin()) { | |
| 169 | + add_action( 'wp_ajax_mc_banklinks_reload', array(&$this, 'mc_banklinks_reload') ); | |
| 170 | + require_once(ABSPATH.'wp-admin/includes/plugin.php'); | |
| 171 | + $wc_version = get_plugin_data(__DIR__.'/../woocommerce/woocommerce.php'); | |
| 172 | + if ((double)$wc_version['Version'] < 2.6) { | |
| 173 | + function sample_admin_notice__error() { | |
| 174 | + $class = 'notice notice-error'; | |
| 175 | + $wc_version = get_plugin_data(__DIR__.'/../woocommerce/woocommerce.php'); | |
| 176 | + $message = sprintf(__( 'Makecommerce plugin needs at least <strong>WooCommerce 2.6</strong> to function correctly. Please update yours (currently %s)', 'wc_makecommerce_domain' ), $wc_version['Version']); | |
| 177 | + printf( '<div class="%1$s"><p>%2$s</p></div>', $class, $message ); | |
| 178 | + } | |
| 179 | + add_action( 'admin_notices', 'sample_admin_notice__error' ); | |
| 180 | + } | |
| 181 | + } | |
| 182 | + | |
| 183 | + } | |
| 184 | + | |
| 185 | + public function get_title() { | |
| 186 | + return $this->title; | |
| 187 | + } | |
| 188 | + | |
| 189 | + public function makecommerce_api_info_missing() { | |
| 190 | + ?> | |
| 191 | + <div class="notice notice-error is-dismissible"> | |
| 192 | + <p> | |
| 193 | + <?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'); ?> | |
| 194 | + <a href="<?php echo admin_url('admin.php?page=wc-settings&tab=api§ion=mk_api'); ?>"><?php echo __('Click here to enter them', 'wc_makecommerce_domain'); ?></a> | |
| 195 | + </p> | |
| 196 | + </div> | |
| 197 | + <?php | |
| 198 | + } | |
| 199 | + | |
| 200 | + public function makecommerce_banklinks_list_empty() { | |
| 201 | + ?> | |
| 202 | + <div class="notice notice-error is-dismissible"> | |
| 203 | + <p> | |
| 204 | + <?php echo __('The payment methods list for MakeCommerce payment module is empty.', 'wc_makecommerce_domain'); ?> | |
| 205 | + <a href="<?php echo admin_url('admin.php?page=wc-settings&tab=checkout§ion=makecommerce'); ?>"><?php echo __('Go to the settings to update them', 'wc_makecommerce_domain'); ?></a> | |
| 206 | + </p> | |
| 207 | + </div> | |
| 208 | + <?php | |
| 209 | + } | |
| 210 | + | |
| 211 | + public function makecommerce_banklinks_list_type_notice() { | |
| 212 | + ?> | |
| 213 | + <div class="notice notice-error is-dismissible"> | |
| 214 | + <p> | |
| 215 | + <?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'); ?> | |
| 216 | + <a href="<?php echo admin_url('admin.php?page=wc-settings&tab=checkout§ion=makecommerce'); ?>"><?php echo __('Go to the settings to update them', 'wc_makecommerce_domain'); ?></a> | |
| 217 | + </p> | |
| 218 | + </div> | |
| 219 | + <?php | |
| 220 | + } | |
| 221 | + | |
| 222 | + /** | |
| 223 | + * Initialise Gateway Settings Form Fields | |
| 224 | + */ | |
| 225 | + public function init_form_fields() { | |
| 226 | + | |
| 227 | + if ( function_exists('icl_object_id') && ! defined( 'POLYLANG_VERSION' ) ) { | |
| 228 | + $languages = apply_filters('wpml_active_languages', NULL, 'skip_missing=0'); | |
| 229 | + } | |
| 230 | + else if ( defined( 'POLYLANG_VERSION' ) ) { | |
| 231 | + $pl_locales = pll_languages_list( array('fields'=>'locale') ); | |
| 232 | + foreach ($pl_locales as $key => $locale_pl ) { | |
| 233 | + $language = array( 'id' => $key, | |
| 234 | + 'code' => $locale_pl) ; | |
| 235 | + $languages[$locale_pl] = $language; | |
| 236 | + } | |
| 237 | + } | |
| 238 | + | |
| 239 | + $this->form_fields = array(); | |
| 240 | + $this->form_fields['header'] = array( | |
| 241 | + 'type' => 'mc_header', | |
| 242 | + ); | |
| 243 | + $this->form_fields['active'] = array( | |
| 244 | + 'title' => __('Enable/Disable', 'wc_makecommerce_domain'), | |
| 245 | + 'type' => 'checkbox', | |
| 246 | + 'label' => __('Enable MakeCommerce payments', 'wc_makecommerce_domain'), | |
| 247 | + 'default' => 'no' | |
| 248 | + ); | |
| 249 | + $this->form_fields['api_title'] = array( | |
| 250 | + 'title' => __('MakeCommerce API', 'wc_makecommerce_domain'), | |
| 251 | + '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§ion=mk_api')), | |
| 252 | + 'type' => 'title', | |
| 253 | + ); | |
| 254 | + $this->form_fields['ui_title'] = array( | |
| 255 | + 'title' => '<br>'.__('User Interface', 'wc_makecommerce_domain'), | |
| 256 | + 'type' => 'title', | |
| 257 | + 'class' => 'ui-identifier', | |
| 258 | + ); | |
| 259 | + $this->form_fields['ui_open_by_default'] = array( | |
| 260 | + 'title' => __('Set as default selection', 'wc_makecommerce_domain'), | |
| 261 | + 'label' => __('MakeCommerce payments widget will be selected by default', 'wc_makecommerce_domain'), | |
| 262 | + 'type' => 'checkbox', | |
| 263 | + 'default' => 'yes', | |
| 264 | + 'class' => 'ui-identifier', | |
| 265 | + ); | |
| 266 | + $this->form_fields['ui_mode'] = array( | |
| 267 | + 'title' => __('Display MC payment channels as', 'wc_makecommerce_domain'), | |
| 268 | + 'type' => 'mc_hidden', | |
| 269 | + 'default' => 'widget', | |
| 270 | + 'options' => array( | |
| 271 | + 'inline' => __('List', 'wc_makecommerce_domain'), | |
| 272 | + 'widget' => __('Grouped to widget', 'wc_makecommerce_domain'), | |
| 273 | + ), | |
| 274 | + 'class' => 'ui-identifier', | |
| 275 | + ); | |
| 276 | + | |
| 277 | + | |
| 278 | + if (empty($languages)) { | |
| 279 | + $this->form_fields['ui_widget_title'] = array( | |
| 280 | + 'title' => __('Payments widget title', 'wc_makecommerce_domain'), | |
| 281 | + 'type' => 'text', | |
| 282 | + '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'), | |
| 283 | + 'default' => __('Pay with bank-links or credit card', 'wc_makecommerce_domain'), | |
| 284 | + 'class' => 'ui-identifier', | |
| 285 | + ); | |
| 286 | + } else { | |
| 287 | + foreach ($languages as $language_code => $language) { | |
| 288 | + $language_name = !empty($language['translated_name']) ? $language['translated_name'] : $language_code; | |
| 289 | + $this->form_fields['ui_widget_title_'.$language_code] = array( | |
| 290 | + 'title' => __('Payments widget title', 'wc_makecommerce_domain').sprintf(' (%s)', $language_code), | |
| 291 | + 'type' => 'text', | |
| 292 | + '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'), | |
| 293 | + 'default' => __('Pay with bank-links or credit card', 'wc_makecommerce_domain'), | |
| 294 | + 'class' => 'ui-identifier', | |
| 295 | + ); | |
| 296 | + } | |
| 297 | + } | |
| 298 | + $this->form_fields['ui_inline_uselogo'] = array( | |
| 299 | + 'title' => __('MC payment channels display style', 'wc_makecommerce_domain'), | |
| 300 | + 'type' => 'select', | |
| 301 | + 'default' => 'logo', | |
| 302 | + 'options' => array( | |
| 303 | + 'logo' => __('Logo', 'wc_makecommerce_domain'), | |
| 304 | + 'text_logo' => __('Text & logo', 'wc_makecommerce_domain'), | |
| 305 | + 'text' => __('Text', 'wc_makecommerce_domain'), | |
| 306 | + ), | |
| 307 | + 'class' => 'ui-identifier', | |
| 308 | + ); | |
| 309 | + $this->form_fields['ui_widget_logosize'] = array( | |
| 310 | + 'title' => __('Size of payment channel logos', 'wc_makecommerce_domain'), | |
| 311 | + 'type' => 'select', | |
| 312 | + 'default' => 'medium', | |
| 313 | + 'options' => array( | |
| 314 | + 'small' => __('Small', 'wc_makecommerce_domain'), | |
| 315 | + 'medium' => __('Medium', 'wc_makecommerce_domain'), | |
| 316 | + 'large' => __('Large', 'wc_makecommerce_domain') | |
| 317 | + ), | |
| 318 | + 'class' => 'ui-identifier', | |
| 319 | + ); | |
| 320 | + $this->form_fields['ui_widget_groupcountries'] = array( | |
| 321 | + 'title' => __('Group bank-links by countries', 'wc_makecommerce_domain'), | |
| 322 | + 'type' => 'mc_hidden', | |
| 323 | + 'default' => 'no', | |
| 324 | + 'class' => 'ui-identifier', | |
| 325 | + ); | |
| 326 | + $this->form_fields['ui_widget_countries_hidden'] = array( | |
| 327 | + 'title' => __('Hide country selector', 'wc_makecommerce_domain'), | |
| 328 | + 'label' => __('Do not display country selector (flags) at payment methods', 'wc_makecommerce_domain'), | |
| 329 | + 'type' => 'checkbox', | |
| 330 | + 'default' => 'no', | |
| 331 | + ); | |
| 332 | + $this->form_fields['ui_widget_countryselector'] = array( | |
| 333 | + 'title' => __('Country selector style', 'wc_makecommerce_domain'), | |
| 334 | + 'type' => 'mc_hidden', | |
| 335 | + 'default' => 'flag', | |
| 336 | + 'options' => array( | |
| 337 | + 'flag' => __('Flag', 'wc_makecommerce_domain'), | |
| 338 | + 'dropdown' => __('Dropdown', 'wc_makecommerce_domain'), | |
| 339 | + ), | |
| 340 | + 'class' => 'ui-identifier', | |
| 341 | + ); | |
| 342 | + $this->form_fields['ui_widget_groupcc'] = array( | |
| 343 | + 'title' => __('Group credit card into separate widget', 'wc_makecommerce_domain'), | |
| 344 | + 'type' => 'mc_hidden', | |
| 345 | + 'default' => 'no', | |
| 346 | + 'class' => 'ui-identifier', | |
| 347 | + ); | |
| 348 | + $this->form_fields['ui_chorder'] = array( | |
| 349 | + 'title' => __('Define custom order of payment channels', 'wc_makecommerce_domain'), | |
| 350 | + 'type' => 'text', | |
| 351 | + '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'), | |
| 352 | + 'class' => 'ui-identifier', | |
| 353 | + ); | |
| 354 | + $this->form_fields['ui_javascript'] = array( | |
| 355 | + 'type' => 'ui_javascript', | |
| 356 | + ); | |
| 357 | + $this->form_fields['cc_title'] = array( | |
| 358 | + 'title' => '<br>'.__('Credit Card Settings', 'wc_makecommerce_domain'), | |
| 359 | + 'type' => 'title', | |
| 360 | + ); | |
| 361 | + $this->form_fields['cc_pass_cust_data'] = array( | |
| 362 | + 'title' => __('Prefill Credit Card form with customer data', 'wc_makecommerce_domain'), | |
| 363 | + 'type' => 'checkbox', | |
| 364 | + 'default' => 'yes', | |
| 365 | + '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'), | |
| 366 | + ); | |
| 367 | + $this->form_fields['adv_title'] = array( | |
| 368 | + 'title' => '<hr><br>'.__('Advanced Settings', 'wc_makecommerce_domain'), | |
| 369 | + 'type' => 'title', | |
| 370 | + ); | |
| 371 | + $this->form_fields['reload_links'] = array( | |
| 372 | + 'type' => 'mc_banklinks_reload', | |
| 373 | + 'title' => __('Update payment methods', 'wc_makecommerce_domain'), | |
| 374 | + 'description' => __('Update', 'wc_makecommerce_domain'), | |
| 375 | + 'desc_tip' => __('This will update shop configuration from MakeCommerce servers.', 'wc_makecommerce_domain'), | |
| 376 | + ); | |
| 377 | + $this->form_fields['cron_info'] = array( | |
| 378 | + 'type' => 'cron_link', | |
| 379 | + 'title' => __('Update url', 'wc_makecommerce_domain'), | |
| 380 | + 'desc_tip' => __('Update url for cron job.', 'wc_makecommerce_domain'), | |
| 381 | + ); | |
| 382 | + } | |
| 383 | + | |
| 384 | + public function generate_mc_hidden_html( $key, $data ) { | |
| 385 | + $field_key = $this->get_field_key($key); | |
| 386 | + ob_start(); | |
| 387 | + ?> | |
| 388 | + <tr style="display: none;"> | |
| 389 | + <td colspan="2"> | |
| 390 | + <input type="hidden" name="<?php echo $field_key; ?>" value="<?php echo $data['default']; ?>" /> | |
| 391 | + </td> | |
| 392 | + </tr> | |
| 393 | + <?php | |
| 394 | + return ob_get_clean(); | |
| 395 | + } | |
| 396 | + | |
| 397 | + public function generate_cron_link_html( $key, $data ) { | |
| 398 | + $field = $this->get_field_key( $key ); | |
| 399 | + $defaults = array( | |
| 400 | + 'title' => '', | |
| 401 | + 'disabled' => false, | |
| 402 | + 'class' => '', | |
| 403 | + 'css' => '', | |
| 404 | + 'placeholder' => '', | |
| 405 | + 'type' => 'text', | |
| 406 | + 'desc_tip' => false, | |
| 407 | + 'description' => '', | |
| 408 | + 'custom_attributes' => array() | |
| 409 | + ); | |
| 410 | + | |
| 411 | + $data = wp_parse_args( $data, $defaults ); | |
| 412 | + $shop_id_text = get_option('mc_banklinks_api_type', false) == 'live' ? 'mk-shop-id': 'mk-test-shop-id'; | |
| 413 | + ob_start(); | |
| 414 | + ?> | |
| 415 | + <tr valign="top"> | |
| 416 | + <th scope="row" class="titledesc"> | |
| 417 | + <label for="<?php echo esc_attr( $field ); ?>"><?php echo wp_kses_post( $data['title'] ); ?></label> | |
| 418 | + <?php echo $this->get_tooltip_html( $data ); ?> | |
| 419 | + </th> | |
| 420 | + <td class="forminp"> | |
| 421 | + <fieldset> | |
| 422 | + <legend class="screen-reader-text"><span><?php echo wp_kses_post( $data['title'] ); ?></span></legend> | |
| 423 | + <p><?php echo get_site_url(null, '?mk-action=mk-update&'.$shop_id_text.'=') . get_option('mk_test_shop_id', false);?></p> | |
| 424 | + </fieldset> | |
| 425 | + </td> | |
| 426 | + </tr> | |
| 427 | + <?php | |
| 428 | + return ob_get_clean(); | |
| 429 | + } | |
| 430 | + | |
| 431 | + public function generate_mc_banklinks_reload_html( $key, $data ) { | |
| 432 | + | |
| 433 | + $field = $this->get_field_key( $key ); | |
| 434 | + $defaults = array( | |
| 435 | + 'title' => '', | |
| 436 | + 'disabled' => false, | |
| 437 | + 'class' => '', | |
| 438 | + 'css' => '', | |
| 439 | + 'placeholder' => '', | |
| 440 | + 'type' => 'text', | |
| 441 | + 'desc_tip' => false, | |
| 442 | + 'description' => '', | |
| 443 | + 'custom_attributes' => array() | |
| 444 | + ); | |
| 445 | + | |
| 446 | + $data = wp_parse_args( $data, $defaults ); | |
| 447 | + | |
| 448 | + ob_start(); | |
| 449 | + ?> | |
| 450 | + <tr valign="top"> | |
| 451 | + <th scope="row" class="titledesc"> | |
| 452 | + <label for="<?php echo esc_attr( $field ); ?>"><?php echo wp_kses_post( $data['title'] ); ?></label> | |
| 453 | + <?php echo $this->get_tooltip_html( $data ); ?> | |
| 454 | + </th> | |
| 455 | + <td class="forminp"> | |
| 456 | + <fieldset> | |
| 457 | + <legend class="screen-reader-text"><span><?php echo wp_kses_post( $data['title'] ); ?></span></legend> | |
| 458 | + <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 ); ?> /> | |
| 459 | + <script type="text/javascript"> | |
| 460 | + jQuery('input#mc_banklinks_reload').on('click', function() { | |
| 461 | + init_mc_loading(); | |
| 462 | + jQuery.ajax({ | |
| 463 | + url: '<?php echo get_site_url(); ?>/wp-admin/admin-ajax.php', | |
| 464 | + type: 'POST', | |
| 465 | + data: 'action=mc_banklinks_reload', | |
| 466 | + success: function (output) { | |
| 467 | + if(output.data) { | |
| 468 | + alert(output.data); | |
| 469 | + } else { | |
| 470 | + alert('<?php echo __('There was an error with your update. Please try again.', 'wc_makecommerce_domain'); ?>'); | |
| 471 | + } | |
| 472 | + }, | |
| 473 | + complete: function() { stop_mc_loading(); } | |
| 474 | + }); | |
| 475 | + }); | |
| 476 | + | |
| 477 | + function init_mc_loading() { | |
| 478 | + jQuery('input#mc_banklinks_reload').attr('disabled', 'disabled'); | |
| 479 | + } | |
| 480 | + | |
| 481 | + function stop_mc_loading() { | |
| 482 | + jQuery('input#mc_banklinks_reload').removeAttr('disabled'); | |
| 483 | + } | |
| 484 | + </script> | |
| 485 | + </fieldset> | |
| 486 | + </td> | |
| 487 | + </tr> | |
| 488 | + <?php | |
| 489 | + | |
| 490 | + return ob_get_clean(); | |
| 491 | + } | |
| 492 | + | |
| 493 | + public function generate_ui_javascript_html( $key, $data ) { | |
| 494 | + ?> | |
| 495 | + <script type="text/javascript"> | |
| 496 | + jQuery(document).ready(function($) { | |
| 497 | + | |
| 498 | + var api_type = $('#woocommerce_<?php echo $this->id; ?>_api_type'); | |
| 499 | + | |
| 500 | + var ui_inline_uselogo_row = $('#woocommerce_<?php echo $this->id; ?>_ui_inline_uselogo').closest('tr'); | |
| 501 | + var ui_widget_title_row = $('#woocommerce_<?php echo $this->id; ?>_ui_widget_title').closest('tr'); | |
| 502 | + var ui_widget_logosize_row = $('#woocommerce_<?php echo $this->id; ?>_ui_widget_logosize').closest('tr'); | |
| 503 | + var ui_widget_countryselector_row = $('#woocommerce_<?php echo $this->id; ?>_ui_widget_countryselector').closest('tr'); | |
| 504 | + var ui_widget_groupcountries_row = $('#woocommerce_<?php echo $this->id; ?>_ui_widget_groupcountries').closest('tr'); | |
| 505 | + var ui_widget_groupcc_row = $('#woocommerce_<?php echo $this->id; ?>_ui_widget_groupcc').closest('tr'); | |
| 506 | + var ui_widget_groupcc_title_row = $('#woocommerce_<?php echo $this->id; ?>_ui_widget_groupcc_title').closest('tr'); | |
| 507 | + | |
| 508 | + var ui_mode = $('#woocommerce_<?php echo $this->id; ?>_ui_mode'); | |
| 509 | + var ui_widget_groupcountries = $('#woocommerce_<?php echo $this->id; ?>_ui_widget_groupcountries'); | |
| 510 | + var ui_widget_groupcc = $('#woocommerce_<?php echo $this->id; ?>_ui_widget_groupcc'); | |
| 511 | + | |
| 512 | + parseVisibility(); | |
| 513 | + function parseVisibility() { | |
| 514 | + $('.ui-identifier').closest('tr').show(); | |
| 515 | + | |
| 516 | + if(api_type.val() == 'live') { | |
| 517 | + $('.mc-test-link').hide(); | |
| 518 | + $('.api-test').closest('tr').hide(); | |
| 519 | + } else { | |
| 520 | + $('.mc-test-link').show(); | |
| 521 | + $('.api-live').closest('tr').hide(); | |
| 522 | + } | |
| 523 | + | |
| 524 | + if(ui_mode.val() == 'inline') { | |
| 525 | + ui_widget_title_row.hide(); | |
| 526 | + ui_widget_logosize_row.hide(); | |
| 527 | + ui_widget_countryselector_row.hide(); | |
| 528 | + ui_widget_groupcountries_row.hide(); | |
| 529 | + ui_widget_groupcc_row.hide(); | |
| 530 | + ui_widget_groupcc_title_row.hide(); | |
| 531 | + } else { | |
| 532 | + ui_inline_uselogo_row.hide(); | |
| 533 | + | |
| 534 | + if(ui_widget_groupcountries.prop('checked')) { | |
| 535 | + ui_widget_countryselector_row.hide(); | |
| 536 | + } | |
| 537 | + if(!ui_widget_groupcc.prop('checked')) { | |
| 538 | + ui_widget_groupcc_title_row.hide(); | |
| 539 | + } | |
| 540 | + } | |
| 541 | + } | |
| 542 | + | |
| 543 | + api_type.on('change', parseVisibility); | |
| 544 | + ui_mode.on('change', parseVisibility); | |
| 545 | + ui_widget_groupcountries.on('change', parseVisibility); | |
| 546 | + ui_widget_groupcc.on('change', parseVisibility); | |
| 547 | + | |
| 548 | + }); | |
| 549 | + </script> | |
| 550 | + <?php | |
| 551 | + } | |
| 552 | + | |
| 553 | + public function generate_mc_header_html( $key, $data ) { | |
| 554 | + ?> | |
| 555 | + <div class="makecommerce-info"> | |
| 556 | + <div class="makecommerce-logo"> | |
| 557 | + <a target="_blank" href="http://makecommerce.net"><img src="<?php echo plugins_url('/images/makecommerce_logo_en.svg', __FILE__); ?>" class="makecommerce-logo"></a> | |
| 558 | + </div> | |
| 559 | + <div class="makecommerce-links"> | |
| 560 | + <div class="makecommerce-link"><a target="_blank" href="https://merchant.maksekeskus.ee">Merchant Portal</a></div> | |
| 561 | + <div class="makecommerce-link"><a target="_blank" href="https://makecommerce.net/">makecommerce.net</a></div> | |
| 562 | + <div class="makecommerce-link"><a target="_blank" href="http://maksekeskus.ee">maksekeskus.ee</a></div> | |
| 563 | + </div> | |
| 564 | + </div> | |
| 565 | + <?php | |
| 566 | + } | |
| 567 | + | |
| 568 | + public function mc_banklinks_reload($force = false) { | |
| 569 | + if ($force || (defined('DOING_AJAX') && DOING_AJAX)) { | |
| 570 | + | |
| 571 | + global $wpdb; | |
| 572 | + global $mkDbTable; | |
| 573 | + | |
| 574 | + $request_params = array( | |
| 575 | + 'environment' => json_encode(array( | |
| 576 | + 'platform' => 'woocommerce '.$this->_getWooCommerce()->version, | |
| 577 | + 'module' => $this->id.' '.$this->version, | |
| 578 | + )), | |
| 579 | + ); | |
| 580 | + | |
| 581 | + if (!$this->_api) { | |
| 582 | + return false; | |
| 583 | + } | |
| 584 | + | |
| 585 | + try { | |
| 586 | + $shopConfig = $this->_api->getShopConfig($request_params); | |
| 587 | + $methods = $shopConfig->payment_methods; | |
| 588 | + } catch (Exception $e) { | |
| 589 | + error_log(print_r($e, 1)); | |
| 590 | + return false; | |
| 591 | + } | |
| 592 | + | |
| 593 | + $tableName = $wpdb->prefix . $mkDbTable; | |
| 594 | + $wpdb->query('TRUNCATE TABLE '.$tableName); | |
| 595 | + | |
| 596 | + if(isset($methods->banklinks)) { | |
| 597 | + foreach($methods->banklinks as $method) { | |
| 598 | + $wpdb->insert($tableName, array('type' => 'banklink', 'country' => $method->country, 'name' => $method->name, 'url' => $method->url)); | |
| 599 | + } | |
| 600 | + $updated = true; | |
| 601 | + } | |
| 602 | + | |
| 603 | + if(isset($methods->cards)) { | |
| 604 | + foreach($methods->cards as $method) { | |
| 605 | + $wpdb->insert($tableName, array('type' => 'card', 'name' => $method->name)); | |
| 606 | + } | |
| 607 | + $updated = true; | |
| 608 | + } | |
| 609 | + | |
| 610 | + if(isset($shopConfig) && strlen($shopConfig->name)) { | |
| 611 | + update_option( 'mc_shop_name', $shopConfig->name ); | |
| 612 | + } | |
| 613 | + | |
| 614 | + if ($updated) { | |
| 615 | + update_option( 'mc_banklinks_api_type', get_option('mk_api_type', false) ); | |
| 616 | + } | |
| 617 | + | |
| 618 | + if ($force) { | |
| 619 | + $this->initBankLinks(); | |
| 620 | + return $updated; | |
| 621 | + } | |
| 622 | + | |
| 623 | + if($updated) { | |
| 624 | + wp_send_json(array('success' => 1, 'data' => __('Update successfully completed!', 'wc_makecommerce_domain'))); | |
| 625 | + exit; | |
| 626 | + } | |
| 627 | + | |
| 628 | + wp_send_json(array('success' => 0, 'data' => __('There was an error with your update. Please try again.', 'wc_makecommerce_domain'))); | |
| 629 | + exit; | |
| 630 | + } | |
| 631 | + die(); | |
| 632 | + } | |
| 633 | + | |
| 634 | + protected function _getWooCommerce() { | |
| 635 | + global $woocommerce; | |
| 636 | + return $woocommerce; | |
| 637 | + } | |
| 638 | + | |
| 639 | + public function is_valid_for_use() { | |
| 640 | + return true; | |
| 641 | + } | |
| 642 | + | |
| 643 | + public function is_available() { | |
| 644 | + if ($this->settings['active'] == "yes" && mk_is_api_set()) { | |
| 645 | + return true; | |
| 646 | + } | |
| 647 | + } | |
| 648 | + | |
| 649 | + public function payment_fields() { | |
| 650 | + | |
| 651 | + if($this->settings['ui_mode'] == 'inline') { | |
| 652 | + | |
| 653 | + ?> | |
| 654 | + <ul class="makecommerce-picker"> | |
| 655 | + <?php foreach($this->_banklinks as $method): ?> | |
| 656 | + <li class="makecommerce-picker-method"> | |
| 657 | + <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; ?>"/> | |
| 658 | + <label for="makecommerce_method_picker_<?php echo $method->country.'_'.$method->name; ?>"> | |
| 659 | + <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> | |
| 660 | + <?php if(in_array($this->settings['ui_inline_uselogo'], array('logo', 'text_logo'))) : ?> | |
| 661 | + <div><img src="<?php echo $this->getImageUrl($method->name); ?>" title="<?php echo ucfirst($method->name); ?>" /></div> | |
| 662 | + <?php endif; ?> | |
| 663 | + </label> | |
| 664 | + </li> | |
| 665 | + <?php endforeach; ?> | |
| 666 | + <?php foreach($this->_cards as $method): ?> | |
| 667 | + <li class="makecommerce-picker-method"> | |
| 668 | + <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; ?>"/> | |
| 669 | + <label for="makecommerce_method_picker_<?php echo 'card_'.$method->name; ?>"> | |
| 670 | + <span class="makecommerce-method-title"><?php if(in_array($this->settings['ui_inline_uselogo'], array('text', 'text_logo'))) { echo ucfirst($method->name); } ?></span> | |
| 671 | + <?php if(in_array($this->settings['ui_inline_uselogo'], array('logo', 'text_logo'))) : ?> | |
| 672 | + <div><img src="<?php echo $this->getImageUrl($method->name); ?>" title="<?php echo ucfirst($method->name); ?>" /></div> | |
| 673 | + <?php endif; ?> | |
| 674 | + </label> | |
| 675 | + </li> | |
| 676 | + <?php endforeach; ?> | |
| 677 | + </ul> | |
| 678 | + <?php | |
| 679 | + | |
| 680 | + } else { | |
| 681 | + ?> | |
| 682 | + <select id="<?php echo $this->id; ?>" name="PRESELECTED_METHOD_<?php echo $this->id; ?>"> | |
| 683 | + <option value=""></option> | |
| 684 | + <?php foreach($this->_banklinks as $method): ?> | |
| 685 | + <option value="<?php echo $method->country.'_'.$method->name; ?>"><?php echo strtoupper($method->country).' - '.ucfirst($method->name); ?></option> | |
| 686 | + <?php endforeach; ?> | |
| 687 | + <?php foreach($this->_cards as $method): ?> | |
| 688 | + <option value="card_<?php echo $method->name; ?>"><?php echo ucfirst($method->name); ?></option> | |
| 689 | + <?php endforeach; ?> | |
| 690 | + </select> | |
| 691 | + <ul class="makecommerce-picker"> | |
| 692 | + <?php | |
| 693 | + | |
| 694 | + if($this->_banklinks) { | |
| 695 | + $defaultCountry = $this->getDefaultCountry(); | |
| 696 | + ?> | |
| 697 | + <?php if( ( empty($this->settings['ui_widget_groupcountries']) || $this->settings['ui_widget_groupcountries'] == 'no' ) && (!isset($this->settings['ui_widget_countries_hidden']) || $this->settings['ui_widget_countries_hidden'] == 'no') ) : ?> | |
| 698 | + <div class="makecommerce_country_picker_countries"> | |
| 699 | + <?php foreach(array_keys($this->_banklinks_grouped) as $country): ?> | |
| 700 | + <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 } ?> | |
| 701 | + <?php endforeach; ?> | |
| 702 | + <?php if($this->settings['ui_widget_countryselector'] == 'dropdown') : ?> | |
| 703 | + <select name="makecommerce_country_picker_select"> | |
| 704 | + <?php foreach(array_keys($this->_banklinks_grouped) as $country): ?> | |
| 705 | + <option value="<?php echo $country; ?>" <?php if($defaultCountry == $country) echo 'selected="selected" '; ?>><?php echo $this->getCountryName($country); ?></option> | |
| 706 | + <?php endforeach; ?> | |
| 707 | + <option value="card" style="display:none;"></option> | |
| 708 | + </select> | |
| 709 | + <?php endif; ?> | |
| 710 | + </div> | |
| 711 | + <?php endif; ?> | |
| 712 | + <?php | |
| 713 | + $banklinks_grouped = $this->_banklinks_grouped; | |
| 714 | + if(count($banklinks_grouped) > 1) { | |
| 715 | + $banklinks_grouped['other'] = array(); | |
| 716 | + } | |
| 717 | + ?> | |
| 718 | + <?php foreach($banklinks_grouped as $country => $methods): ?> | |
| 719 | + <li class="makecommerce-picker-country"> | |
| 720 | + <?php if($this->settings['ui_widget_groupcountries'] == 'yes') : ?> | |
| 721 | + <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> | |
| 722 | + <?php endif; ?> | |
| 723 | + <div class="makecommerce_country_picker_methods" id="makecommerce_country_picker_methods_<?php echo $country; ?>"> | |
| 724 | + <?php foreach($methods as $method): ?> | |
| 725 | + <div class="makecommerce-banklink-picker" banklink_id="<?php echo $method->country.'_'.$method->name; ?>"> | |
| 726 | + <img src="<?php echo $this->getImageUrl($method->name); ?>" title="<?php echo ucfirst($method->name); ?>" /> | |
| 727 | + </div> | |
| 728 | + <?php endforeach; ?> | |
| 729 | + <?php if($this->_cards && $this->settings['ui_widget_groupcc'] == 'no') : ?> | |
| 730 | + <div class="breaker"></div> | |
| 731 | + <?php foreach($this->_cards as $method): ?> | |
| 732 | + <div class="makecommerce-banklink-picker" banklink_id="card_<?php echo $method->name; ?>"> | |
| 733 | + <img src="<?php echo $this->getImageUrl($method->name); ?>" title="<?php echo ucfirst($method->name); ?>" /> | |
| 734 | + </div> | |
| 735 | + <?php endforeach; ?> | |
| 736 | + <?php elseif($country == 'other') :?> | |
| 737 | + <p class="no-methods"><?php _e('No payment methods for selected country' ,'wc_makecommerce_domain');?></p> | |
| 738 | + <?php endif; ?> | |
| 739 | + </div> | |
| 740 | + </li> | |
| 741 | + <?php endforeach; ?> | |
| 742 | + <?php if($this->_cards && $this->settings['ui_widget_groupcc'] == 'yes') : ?> | |
| 743 | + <li class="makecommerce-picker-country"> | |
| 744 | + <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> | |
| 745 | + <div class="makecommerce_country_picker_methods" id="makecommerce_country_picker_methods_card"> | |
| 746 | + <?php foreach($this->_cards as $method): ?> | |
| 747 | + <div class="makecommerce-banklink-picker" banklink_id="card_<?php echo $method->name; ?>"> | |
| 748 | + <img src="<?php echo $this->getImageUrl($method->name); ?>" title="<?php echo ucfirst($method->name); ?>" /> | |
| 749 | + </div> | |
| 750 | + <?php endforeach; ?> | |
| 751 | + </div> | |
| 752 | + </li> | |
| 753 | + <?php endif; ?> | |
| 754 | + <?php | |
| 755 | + } | |
| 756 | + ?> | |
| 757 | + </ul> | |
| 758 | + <div class="mc-clear-both"></div> | |
| 759 | + <script type="text/javascript"> | |
| 760 | + var makecommerceId = '<?php echo $this->id; ?>'; | |
| 761 | + var selectedCountry = '<?php echo $defaultCountry; ?>'; | |
| 762 | + | |
| 763 | + var logosize = '<?php echo $this->settings['ui_widget_logosize']; ?>'; | |
| 764 | + jQuery('div.makecommerce_country_picker_methods').addClass('logosize-'+logosize); | |
| 765 | + | |
| 766 | + <?php if(count($this->_banklinks_grouped) == 1): ?> | |
| 767 | + jQuery('li.makecommerce-picker-country').show(); | |
| 768 | + jQuery('div.makecommerce_country_picker_countries').hide(); | |
| 769 | + jQuery('li.makecommerce-picker-country > input, li.makecommerce-picker-country > label').hide(); | |
| 770 | + <?php else: ?> | |
| 771 | + makecommercePick(); | |
| 772 | + | |
| 773 | + jQuery('body').on('change', 'select[name=makecommerce_country_picker_select]', function() { | |
| 774 | + selectedCountry = jQuery(this).val(); | |
| 775 | + jQuery('input[name=makecommerce_country_picker]').removeAttr('checked'); | |
| 776 | + makecommercePick(); | |
| 777 | + }); | |
| 778 | + jQuery('body').on('change', 'input[name=makecommerce_country_picker]', function() { | |
| 779 | + selectedCountry = jQuery(this).val(); | |
| 780 | + jQuery('select[name=makecommerce_country_picker_select]').val(selectedCountry); | |
| 781 | + makecommercePick(); | |
| 782 | + }); | |
| 783 | + | |
| 784 | + function makecommercePick() { | |
| 785 | + jQuery('select#'+makecommerceId).val(''); | |
| 786 | + jQuery('div.makecommerce-banklink-picker').removeClass('selected'); | |
| 787 | + jQuery('label.makecommerce_country_picker_label').removeClass('selected'); | |
| 788 | + | |
| 789 | + jQuery('li.makecommerce-picker-country').hide(); | |
| 790 | + jQuery('div#makecommerce_country_picker_methods_' + selectedCountry).parent().show(); | |
| 791 | + jQuery('label[for=makecommerce_country_picker_' + selectedCountry + ']').addClass('selected'); | |
| 792 | + } | |
| 793 | + <?php endif; ?> | |
| 794 | + | |
| 795 | + jQuery('div.makecommerce-banklink-picker').on('click', function() { | |
| 796 | + var banklink_id = jQuery(this).attr('banklink_id'); | |
| 797 | + jQuery('select#'+makecommerceId).val(banklink_id); | |
| 798 | + | |
| 799 | + jQuery('div.makecommerce-banklink-picker').removeClass('selected'); | |
| 800 | + jQuery(this).addClass('selected'); | |
| 801 | + }); | |
| 802 | + </script> | |
| 803 | + <?php | |
| 804 | + } | |
| 805 | + | |
| 806 | + if($this->settings['ui_open_by_default'] == 'yes') { | |
| 807 | + ?> | |
| 808 | + <script type="text/javascript"> | |
| 809 | + jQuery('input#payment_method_<?php echo $this->id; ?>').trigger('click'); | |
| 810 | + </script> | |
| 811 | + <?php | |
| 812 | + } | |
| 813 | + } | |
| 814 | + | |
| 815 | + private function getDefaultCountry() { | |
| 816 | + if ($this->_getWooCommerce()->customer) { | |
| 817 | + $customerCountry = strtolower($this->_getWooCommerce()->customer->get_shipping_country()); | |
| 818 | + if(array_key_exists($customerCountry, $this->_banklinks_grouped)) { | |
| 819 | + return $customerCountry; | |
| 820 | + } else { | |
| 821 | + return 'other'; | |
| 822 | + } | |
| 823 | + } | |
| 824 | + | |
| 825 | + $localeToCountry = array( | |
| 826 | + 'et' => 'ee', | |
| 827 | + 'lv' => 'lv', | |
| 828 | + 'lt' => 'lt', | |
| 829 | + 'fi' => 'fi', | |
| 830 | + ); | |
| 831 | + if(array_key_exists(get_locale(), $localeToCountry)) { | |
| 832 | + return $localeToCountry[get_locale()]; | |
| 833 | + } | |
| 834 | + | |
| 835 | + return key($this->_banklinks_grouped); | |
| 836 | + } | |
| 837 | + | |
| 838 | + private function initBanklinks() { | |
| 839 | + global $wpdb; | |
| 840 | + global $mkDbTable; | |
| 841 | + $this->_banklinks = array(); | |
| 842 | + | |
| 843 | + $tableName = $wpdb->prefix . $mkDbTable; | |
| 844 | + $methods = $wpdb->get_results('SELECT * FROM '.$tableName); | |
| 845 | + | |
| 846 | + if(count($methods)) { | |
| 847 | + if(is_admin() && $this->_init == true && get_option( 'mc_banklinks_api_type' ) != get_option('mk_api_type', false)) { | |
| 848 | + //add_action( 'admin_notices', array(&$this, 'makecommerce_banklinks_list_type_notice') ); | |
| 849 | + } | |
| 850 | + $banklinks = array(); | |
| 851 | + $banklinks_grouped = array(); | |
| 852 | + $cards = array(); | |
| 853 | + foreach($methods as $method) { | |
| 854 | + if($method->type == 'banklink') { | |
| 855 | + $banklinks[] = $banklinks_grouped[$method->country][] = $method; | |
| 856 | + } elseif($method->type == 'card') { | |
| 857 | + $cards[] = $method; | |
| 858 | + } | |
| 859 | + } | |
| 860 | + | |
| 861 | + usort($banklinks, array($this, 'orderBanklinks')); | |
| 862 | + foreach($banklinks_grouped as &$country) { | |
| 863 | + usort($country, array($this, 'orderBanklinks')); | |
| 864 | + } | |
| 865 | + | |
| 866 | + $this->_banklinks = $banklinks; | |
| 867 | + $this->_banklinks_grouped = $banklinks_grouped; | |
| 868 | + $this->_cards = $cards; | |
| 869 | + remove_action('admin_notices', array(&$this, 'makecommerce_banklinks_list_empty'), 30); | |
| 870 | + } elseif(is_admin() && $this->_init == true) { | |
| 871 | + add_action('admin_notices', array(&$this, 'makecommerce_banklinks_list_empty'), 30); | |
| 872 | + } | |
| 873 | + } | |
| 874 | + | |
| 875 | + private function orderBanklinks($a, $b) { | |
| 876 | + $order = array_map('trim', explode(",", $this->settings['ui_chorder'])); | |
| 877 | + | |
| 878 | + $posA = array_search($a->name, $order); | |
| 879 | + $posB = array_search($b->name, $order); | |
| 880 | + | |
| 881 | + if($posA === $posB) return $a->id > $b->id ? 1 : -1; | |
| 882 | + if($posA === FALSE) return 1; | |
| 883 | + if($posB === FALSE) return -1; | |
| 884 | + | |
| 885 | + return $posA > $posB ? 1 : -1; | |
| 886 | + } | |
| 887 | + | |
| 888 | + protected function getImageUrl($methodName) { | |
| 889 | + $imageUrlPath = 'https://static.maksekeskus.ee/img/channel/lnd/'; | |
| 890 | + | |
| 891 | + return $imageUrlPath.$methodName.'.png'; | |
| 892 | + } | |
| 893 | + | |
| 894 | + public function validate_fields() { | |
| 895 | + $selected = isset($_POST['PRESELECTED_METHOD_' . $this->id]) ? sanitize_text_field($_POST['PRESELECTED_METHOD_' . $this->id]) : false; | |
| 896 | + | |
| 897 | + if (!$selected) { | |
| 898 | + wc_add_notice(__('Please select suitable payment option!', 'wc_makecommerce_domain'), 'error'); | |
| 899 | + } else { | |
| 900 | + $this->_getWooCommerce()->session->makecommerce_preselected_method = $selected; | |
| 901 | + } | |
| 902 | + | |
| 903 | + return true; | |
| 904 | + } | |
| 905 | + | |
| 906 | + public function process_payment($orderId) { | |
| 907 | + | |
| 908 | + $order = new WC_Order($orderId); | |
| 909 | + | |
| 910 | + $selected = isset($_POST['PRESELECTED_METHOD_' . $this->id]) ? sanitize_text_field($_POST['PRESELECTED_METHOD_' . $this->id]) : false; | |
| 911 | + | |
| 912 | + if(!empty($selected)) { | |
| 913 | + | |
| 914 | + update_post_meta($order->id, '_makecommerce_preselected_method', $selected); | |
| 915 | + | |
| 916 | + if(substr($selected, 0, 5) == 'card_') { | |
| 917 | + | |
| 918 | + $request_body = array( | |
| 919 | + 'transaction' => array( | |
| 920 | + 'amount' => round($order->order_total, 2), | |
| 921 | + 'currency' => $order->get_order_currency(), | |
| 922 | + 'reference' => $order->id, | |
| 923 | + 'transaction_url' => array( | |
| 924 | + 'return_url' => array( | |
| 925 | + 'url' => $this->return_url_cc, | |
| 926 | + 'method' => 'POST', | |
| 927 | + ), | |
| 928 | + 'cancel_url' => array( | |
| 929 | + 'url' => $this->return_url_cc, | |
| 930 | + 'method' => 'POST', | |
| 931 | + ), | |
| 932 | + 'notification_url' => array( | |
| 933 | + 'url' => $this->return_url_cc, | |
| 934 | + 'method' => 'POST', | |
| 935 | + ), | |
| 936 | + ), | |
| 937 | + ), | |
| 938 | + 'customer' => array( | |
| 939 | + 'ip' => $_SERVER['REMOTE_ADDR'], | |
| 940 | + 'country' => strtolower($order->billing_country), | |
| 941 | + 'locale' => strtolower(substr(get_locale(), 0, 2)), | |
| 942 | + ), | |
| 943 | + ); | |
| 944 | + $transaction = $this->_api->createTransaction($request_body); | |
| 945 | + | |
| 946 | + if(isset($transaction->id)) { | |
| 947 | + update_post_meta($order->id, '_makecommerce_cc_transaction_id', $transaction->id); | |
| 948 | + return array( | |
| 949 | + 'result' => 'success', | |
| 950 | + 'redirect' => $this->_getOrderConfirmationUrl($order), | |
| 951 | + ); | |
| 952 | + } | |
| 953 | + | |
| 954 | + wc_add_notice(__('An error occured when trying to process payment!', 'wc_makecommerce_domain'), 'error'); | |
| 955 | + return array( | |
| 956 | + 'result' => 'failure', | |
| 957 | + ); | |
| 958 | + | |
| 959 | + } else { | |
| 960 | + | |
| 961 | + $redirectUrl = $this->_getRedirectUrl($selected); | |
| 962 | + | |
| 963 | + if($redirectUrl) { | |
| 964 | + $request_body = array( | |
| 965 | + 'transaction' => array( | |
| 966 | + 'amount' => round($order->order_total, 2), | |
| 967 | + 'currency' => $order->get_order_currency(), | |
| 968 | + 'reference' => $order->id, | |
| 969 | + 'transaction_url' => array( | |
| 970 | + 'return_url' => array( | |
| 971 | + 'url' => $this->return_url, | |
| 972 | + 'method' => 'POST', | |
| 973 | + ), | |
| 974 | + 'cancel_url' => array( | |
| 975 | + 'url' => $this->return_url, | |
| 976 | + 'method' => 'POST', | |
| 977 | + ), | |
| 978 | + 'notification_url' => array( | |
| 979 | + 'url' => $this->return_url, | |
| 980 | + 'method' => 'POST', | |
| 981 | + ), | |
| 982 | + ), | |
| 983 | + ), | |
| 984 | + 'customer' => array( | |
| 985 | + 'ip' => $_SERVER['REMOTE_ADDR'], | |
| 986 | + 'country' => strtolower($order->billing_country), | |
| 987 | + 'locale' => strtolower(substr(get_locale(), 0, 2)), | |
| 988 | + ) | |
| 989 | + ); | |
| 990 | + $transaction = $this->_api->createTransaction($request_body); | |
| 991 | + | |
| 992 | + if(isset($transaction->id)) { | |
| 993 | + return array( | |
| 994 | + 'result' => 'success', | |
| 995 | + 'redirect' => $redirectUrl.$transaction->id, | |
| 996 | + ); | |
| 997 | + } | |
| 998 | + } | |
| 999 | + | |
| 1000 | + wc_add_notice(__('An error occured when trying to process payment!', 'wc_makecommerce_domain'), 'error'); | |
| 1001 | + return array( | |
| 1002 | + 'result' => 'failure', | |
| 1003 | + ); | |
| 1004 | + | |
| 1005 | + } | |
| 1006 | + | |
| 1007 | + } | |
| 1008 | + | |
| 1009 | + wc_add_notice(__('An error occured when trying to process payment!', 'wc_makecommerce_domain'), 'error'); | |
| 1010 | + return array( | |
| 1011 | + 'result' => 'failure', | |
| 1012 | + ); | |
| 1013 | + } | |
| 1014 | + | |
| 1015 | + protected function _getRedirectUrl($selected) { | |
| 1016 | + foreach($this->_banklinks as $method) { | |
| 1017 | + if($selected == $method->country.'_'.$method->name) | |
| 1018 | + return $method->url; | |
| 1019 | + } | |
| 1020 | + | |
| 1021 | + return false; | |
| 1022 | + } | |
| 1023 | + | |
| 1024 | + protected function _getOrderConfirmationUrl($order) { | |
| 1025 | + | |
| 1026 | + global $sitepress; | |
| 1027 | + if(isset($sitepress)) { | |
| 1028 | + $baseUrl = $sitepress->language_url( ICL_LANGUAGE_CODE ); | |
| 1029 | + $lang = ICL_LANGUAGE_CODE; | |
| 1030 | + } else { | |
| 1031 | + $baseUrl = site_url(); | |
| 1032 | + $lang = strtolower(substr(get_locale(), 0, 2)); | |
| 1033 | + } | |
| 1034 | + $url = add_query_arg( array( | |
| 1035 | + 'makecommerce_card_pay' => '1', | |
| 1036 | + 'order_id' => $order->id, | |
| 1037 | + 'lang' => $lang, | |
| 1038 | + ), $baseUrl); | |
| 1039 | + | |
| 1040 | + // $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)))); | |
| 1041 | + return $url; | |
| 1042 | + } | |
| 1043 | + | |
| 1044 | + public function receipt_page($orderId) { | |
| 1045 | + echo '<p>' . __('Thank you for the order, please click on the button to start the payment.', 'wc_makecommerce_domain') . '</p>'; | |
| 1046 | + | |
| 1047 | + $order = new WC_Order($orderId); | |
| 1048 | + if(substr(get_post_meta($orderId, '_makecommerce_preselected_method', true), 0, 5) == 'card_' && $order->get_status() == 'pending') { | |
| 1049 | + echo $this->generateCardForm($order); | |
| 1050 | + } | |
| 1051 | + } | |
| 1052 | + | |
| 1053 | + public function generateCardForm($order) { | |
| 1054 | + $scriptSrc = htmlspecialchars($this->_gateway_url_static.'checkout.js'); | |
| 1055 | + $transactionId = get_post_meta($order->id, '_makecommerce_cc_transaction_id', true); | |
| 1056 | + $idReference = $order->id; | |
| 1057 | + $jsParams = array( | |
| 1058 | + 'key' => $this->_api->getPublishableKey(), | |
| 1059 | + 'transaction' => $transactionId, | |
| 1060 | + 'selector' => '#submit_banklinkmakecommerce_payment_form', | |
| 1061 | + 'amount' => round($order->order_total, 2), | |
| 1062 | + 'locale' => !empty($_GET['lang']) ? $_GET['lang'] : (defined('ICL_LANGUAGE_CODE') ? ICL_LANGUAGE_CODE : strtolower(substr(get_locale(), 0, 2))), | |
| 1063 | + 'open-on-load' => 'true', | |
| 1064 | + 'client-name' => ($this->settings['cc_pass_cust_data'] == 'yes' ? (string) ($order->billing_first_name . ' ' . $order->billing_last_name) : ''), | |
| 1065 | + 'email' => ($this->settings['cc_pass_cust_data'] == 'yes' ? (string) $order->billing_email : ''), | |
| 1066 | + 'name' => get_option('mc_shop_name', ''), | |
| 1067 | + 'description' => __('Order', 'woocommerce') . ' ' . (string) $idReference, | |
| 1068 | + 'completed' => 'makecommerce_cc_complete', | |
| 1069 | + 'currency' => 'EUR', | |
| 1070 | + ); | |
| 1071 | + ?> | |
| 1072 | + <script type="text/javascript"> | |
| 1073 | + function makecommerce_cc_complete(data) { | |
| 1074 | + if(data.paymentToken) { | |
| 1075 | + jQuery('div.mc-processing-message').show(); | |
| 1076 | + | |
| 1077 | + 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>'); | |
| 1078 | + for(var key in data) { | |
| 1079 | + submitform.append('<input type="hidden" name="'+key+'" value="'+data[key]+'" />'); | |
| 1080 | + } | |
| 1081 | + jQuery('body').append(submitform); | |
| 1082 | + submitform.submit(); | |
| 1083 | + } | |
| 1084 | + } | |
| 1085 | + </script> | |
| 1086 | + <form id="cc_form"> | |
| 1087 | + <input type="submit" class="button-alt" id="submit_banklinkmakecommerce_payment_form" value="<?php echo __('Pay', 'wc_makecommerce_domain'); ?>" /> | |
| 1088 | + <a class="button cancel" href="<?php echo esc_url($order->get_cancel_order_url()); ?>"><?php echo __('Cancel order & restore cart', 'wc_makecommerce_domain'); ?></a> | |
| 1089 | + <script type="text/javascript" src="<?php echo $scriptSrc; ?>" <?php echo $this->_toHtmlAttributes($jsParams); ?>></script> | |
| 1090 | + </form> | |
| 1091 | + <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> | |
| 1092 | + <?php | |
| 1093 | + } | |
| 1094 | + | |
| 1095 | + protected function _toHtmlAttributes($input) { | |
| 1096 | + $result = array(); | |
| 1097 | + foreach ($input as $key => $value) { | |
| 1098 | + $result[] = 'data-' . htmlspecialchars($key) . '=' . '"' . htmlspecialchars($value) . '"'; | |
| 1099 | + } | |
| 1100 | + return implode(' ', $result); | |
| 1101 | + } | |
| 1102 | + | |
| 1103 | + public function makecommerce_return_trigger($vars) { | |
| 1104 | + $vars[] = 'makecommerce_return'; | |
| 1105 | + $vars[] = 'makecommerce_card_pay'; | |
| 1106 | + return $vars; | |
| 1107 | + } | |
| 1108 | + | |
| 1109 | + public function makecommerce_return_trigger_check() { | |
| 1110 | + if(intval(get_query_var('makecommerce_return')) == 1) { | |
| 1111 | + | |
| 1112 | + $returnUrl = home_url(); | |
| 1113 | + | |
| 1114 | + $request = stripslashes_deep($_POST); | |
| 1115 | + if($this->_api->verifyMac($request)) { | |
| 1116 | + $data = $this->_api->extractRequestData($request); | |
| 1117 | + $order = new WC_Order($data['reference']); | |
| 1118 | + | |
| 1119 | + switch($data['status']) { | |
| 1120 | + case self::MC_CANCELLED: | |
| 1121 | + $order->update_status( 'cancelled' ); | |
| 1122 | + wc_add_notice(__('Payment transaction cancelled', 'wc_makecommerce_domain'), 'error'); | |
| 1123 | + $returnUrl = $this->_getWooCommerce()->cart->get_cart_url(); | |
| 1124 | + break; | |
| 1125 | + case self::MC_COMPLETED: | |
| 1126 | + if($this->validate_completed_payment($order, $data)) { | |
| 1127 | + $orderNote = array(); | |
| 1128 | + $orderNote[] = __('Transaction ID', 'wc_makecommerce_domain') . ': ' . $data['transaction']; | |
| 1129 | + $orderNote[] = __('Payment option', 'wc_makecommerce_domain') . ': ' . get_post_meta($order->id, '_makecommerce_preselected_method', true); | |
| 1130 | + | |
| 1131 | + $order->add_order_note(implode("\r\n", $orderNote)); | |
| 1132 | + | |
| 1133 | + $order->payment_complete($data['transaction']); | |
| 1134 | + $order->update_status( 'processing' ); | |
| 1135 | + | |
| 1136 | + try { | |
| 1137 | + @ob_start(); | |
| 1138 | + $this->receipt_page($order->id); | |
| 1139 | + @ob_end_clean(); | |
| 1140 | + | |
| 1141 | + $returnUrl = $this->get_return_url($order); | |
| 1142 | + } catch (Exception $ex) { | |
| 1143 | + @ob_end_clean(); | |
| 1144 | + } | |
| 1145 | + } else { | |
| 1146 | + $returnUrl = $this->_getWooCommerce()->cart->get_cart_url(); | |
| 1147 | + } | |
| 1148 | + break; | |
| 1149 | + } | |
| 1150 | + | |
| 1151 | + //exit; | |
| 1152 | + } | |
| 1153 | + wp_redirect($returnUrl); | |
| 1154 | + exit; | |
| 1155 | + | |
| 1156 | + } elseif(intval(get_query_var('makecommerce_return')) == 2) { | |
| 1157 | + | |
| 1158 | + $returnUrl = home_url(); | |
| 1159 | + if(isset($_POST['paymentToken']) && isset($_POST['transaction'])) { | |
| 1160 | + $token = $_POST['paymentToken']; | |
| 1161 | + $transaction = $_POST['transaction']; | |
| 1162 | + } | |
| 1163 | + | |
| 1164 | + if(isset($_POST['json']) && $this->_api->verifyMac(stripslashes_deep($_POST))) { | |
| 1165 | + $data = json_decode(stripslashes($_POST['json']), true); | |
| 1166 | + $token = $data['token']['id']; | |
| 1167 | + $transaction = $data['transaction']['id']; | |
| 1168 | + } | |
| 1169 | + | |
| 1170 | + if(isset($token) && isset($transaction) && !empty($token) && !empty($transaction)) { | |
| 1171 | + global $wpdb; | |
| 1172 | + 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.'"'))) { | |
| 1173 | + $request_body = array( | |
| 1174 | + 'token' => $token, | |
| 1175 | + ); | |
| 1176 | + $data = $this->_api->createPayment(get_post_meta($order->id, '_makecommerce_cc_transaction_id', true), $request_body); | |
| 1177 | + switch($data->status) { | |
| 1178 | + case self::MC_CANCELLED: | |
| 1179 | + $order->update_status( 'cancelled' ); | |
| 1180 | + wc_add_notice(__('Payment transaction cancelled', 'wc_makecommerce_domain'), 'error'); | |
| 1181 | + $returnUrl = $this->_getWooCommerce()->cart->get_cart_url(); | |
| 1182 | + break; | |
| 1183 | + case self::MC_DEPOSITED: | |
| 1184 | + if($this->validate_completed_payment($order, $data)) { | |
| 1185 | + $orderNote = array(); | |
| 1186 | + $orderNote[] = __('Transaction ID', 'wc_makecommerce_domain') . ': ' . $data->transaction->id; | |
| 1187 | + $orderNote[] = __('Payment option', 'wc_makecommerce_domain') . ': ' . get_post_meta($order->id, '_makecommerce_preselected_method', true); | |
| 1188 | + | |
| 1189 | + $order->add_order_note(implode("\r\n", $orderNote)); | |
| 1190 | + | |
| 1191 | + $order->payment_complete($data->transaction->id); | |
| 1192 | + $order->update_status( 'processing' ); | |
| 1193 | + | |
| 1194 | + try { | |
| 1195 | + @ob_start(); | |
| 1196 | + $this->receipt_page($order->id); | |
| 1197 | + @ob_end_clean(); | |
| 1198 | + | |
| 1199 | + $returnUrl = $this->get_return_url($order); | |
| 1200 | + } catch (Exception $ex) { | |
| 1201 | + @ob_end_clean(); | |
| 1202 | + } | |
| 1203 | + } else { | |
| 1204 | + $returnUrl = $this->_getWooCommerce()->cart->get_cart_url(); | |
| 1205 | + } | |
| 1206 | + break; | |
| 1207 | + } | |
| 1208 | + } | |
| 1209 | + } | |
| 1210 | + | |
| 1211 | + wp_redirect($returnUrl); | |
| 1212 | + exit; | |
| 1213 | + } | |
| 1214 | + | |
| 1215 | + if(intval(get_query_var('makecommerce_card_pay')) == 1) { | |
| 1216 | + if(isset($_GET['order_id'])) { | |
| 1217 | + if($order = New WC_Order($_GET['order_id'])) { | |
| 1218 | + New woocommerce_makecommerce(false); | |
| 1219 | + get_header(); | |
| 1220 | + ?> | |
| 1221 | + <div id="primary" class="content-area"> | |
| 1222 | + <?php $this->receipt_page($order->id); ?> | |
| 1223 | + </div> | |
| 1224 | + <?php | |
| 1225 | + get_sidebar(); | |
| 1226 | + get_footer(); | |
| 1227 | + | |
| 1228 | + exit; | |
| 1229 | + } | |
| 1230 | + } | |
| 1231 | + } | |
| 1232 | + } | |
| 1233 | + | |
| 1234 | + private function validate_completed_payment($order, $data) { | |
| 1235 | + | |
| 1236 | + if(is_object($data)) { | |
| 1237 | + $data = json_decode(json_encode($data), true); | |
| 1238 | + } | |
| 1239 | + | |
| 1240 | + if(empty($data['transaction'])) { | |
| 1241 | + $order->add_order_note(__('Payment error, missing transaction id', 'wc_makecommerce_domain')); | |
| 1242 | + wc_add_notice(__('Error verifying transaction', 'wc_makecommerce_domain'), 'error'); | |
| 1243 | + return false; | |
| 1244 | + } | |
| 1245 | + if($data['amount'] != round($order->order_total, 2)) { | |
| 1246 | + $order->add_order_note(sprintf(__('Payment error, incorrect amount captured: %s', 'wc_makecommerce_domain'), $data['amount'].' '.$data['currency'])); | |
| 1247 | + wc_add_notice(__('Error verifying transaction', 'wc_makecommerce_domain').', '.__('Incorrect amount captured', 'wc_makecommerce_domain'), 'error'); | |
| 1248 | + return false; | |
| 1249 | + } | |
| 1250 | + if($data['currency'] != $order->get_order_currency()) { | |
| 1251 | + $order->add_order_note(sprintf(__('Payment error, incorrect currency captured: %s', 'wc_makecommerce_domain'), $data['currency'])); | |
| 1252 | + wc_add_notice(__('Error verifying transaction', 'wc_makecommerce_domain').', '.__('Incorrect currency captured', 'wc_makecommerce_domain'), 'error'); | |
| 1253 | + return false; | |
| 1254 | + } | |
| 1255 | + | |
| 1256 | + return true; | |
| 1257 | + } | |
| 1258 | + | |
| 1259 | + public function admin_order_page($order) { | |
| 1260 | + // | |
| 1261 | + } | |
| 1262 | + | |
| 1263 | + public function process_refund($order_id, $amount = null, $comment = '') { | |
| 1264 | + if ($this->_api) { | |
| 1265 | + try { | |
| 1266 | + $order = new WC_Order($order_id); | |
| 1267 | + $transactionId = $order->get_transaction_id(); | |
| 1268 | + | |
| 1269 | + if($response = $this->_api->createRefund($transactionId, array('amount' => $amount, 'comment' => ($comment ? : 'refund')))) { | |
| 1270 | + if($status = (string)$response->transaction->status) { | |
| 1271 | + switch($status) { | |
| 1272 | + case self::MC_REFUNDED: | |
| 1273 | + $order->add_order_note(sprintf(__('Refund completed for amount %s', 'wc_makecommerce_domain'), $amount)); | |
| 1274 | + return true; | |
| 1275 | + break; | |
| 1276 | + case self::MC_PART_REFUNDED: | |
| 1277 | + $order->add_order_note(sprintf(__('Partial refund completed for amount %s', 'wc_makecommerce_domain'), $amount)); | |
| 1278 | + return true; | |
| 1279 | + break; | |
| 1280 | + } | |
| 1281 | + } | |
| 1282 | + } | |
| 1283 | + return false; | |
| 1284 | + | |
| 1285 | + } catch (Exception $e) { | |
| 1286 | + return new WP_Error('makecommerce_refund_error', $e->getMessage()); | |
| 1287 | + } | |
| 1288 | + | |
| 1289 | + return false; | |
| 1290 | + } | |
| 1291 | + return false; | |
| 1292 | + } | |
| 1293 | + | |
| 1294 | + protected function getCountryName($slug) { | |
| 1295 | + switch($slug) { | |
| 1296 | + case 'ee': return __('Estonia', 'wc_makecommerce_domain'); break; | |
| 1297 | + case 'lv': return __('Latvia', 'wc_makecommerce_domain'); break; | |
| 1298 | + case 'lt': return __('Lithuania', 'wc_makecommerce_domain'); break; | |
| 1299 | + case 'fi': return __('Finland', 'wc_makecommerce_domain'); break; | |
| 1300 | + } | |
| 1301 | + return $slug; | |
| 1302 | + } | |
| 1303 | + | |
| 1304 | + } | |
| 1305 | + | |
| 1306 | + New woocommerce_makecommerce(true); | |
| 1307 | + | |
| 1308 | + // Woocommerce Multilingual overrides | |
| 1309 | + if ( in_array( 'woocommerce-multilingual/wpml-woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) { | |
| 1310 | + add_filter( 'get_post_metadata', 'mc_override_payment_method_string', 5, 4 ); | |
| 1311 | + } | |
| 1312 | + | |
| 1313 | + // Assign schedule if not set, update compatible no need to reactivate, format('U') php 5.2 compatible | |
| 1314 | + // Assigns task at 23.59.59 by local time, task runs if time has passed and somebody navigates the site | |
| 1315 | + if (! wp_next_scheduled ( 'mc_banklinks_update_cron' )) { | |
| 1316 | + $date = new DateTime(); | |
| 1317 | + $date->setTime(23,59,59); | |
| 1318 | + $timestamp = intval($date->format('U')); | |
| 1319 | + $local = 3600 * intval(get_option('gmt_offset')); | |
| 1320 | + wp_schedule_event($timestamp - $local, 'twicedaily', 'mc_banklinks_update_cron'); | |
| 1321 | + } | |
| 1322 | + } | |
| 1323 | + | |
| 1324 | + function mc_override_payment_method_string( $check, $object_id, $meta_key, $single ){ | |
| 1325 | + if($meta_key == '_payment_method_title') { | |
| 1326 | + $payment_method = get_post_meta( $object_id, '_payment_method', true ); | |
| 1327 | + if( $payment_method == 'makecommerce' ){ | |
| 1328 | + $payment_gateways = WC()->payment_gateways->payment_gateways(); | |
| 1329 | + if( isset( $payment_gateways[ $payment_method ] ) ){ | |
| 1330 | + $title = $payment_gateways[ $payment_method ]->title; | |
| 1331 | + return $title; | |
| 1332 | + } | |
| 1333 | + } | |
| 1334 | + } | |
| 1335 | + return $check; | |
| 1336 | + } | |
| 1337 | + | |
| 1338 | + // Banklinks update wrapper for action | |
| 1339 | + function update_mc_banklinks() { | |
| 1340 | + $obj = new woocommerce_makecommerce(true); | |
| 1341 | + $obj->mc_banklinks_reload(true); | |
| 1342 | + } | |
| 1343 | + | |
| 1344 | + function woocommerce_payment_makecommerce_add($methods) { | |
| 1345 | + $methods[] = 'woocommerce_makecommerce'; | |
| 1346 | + return $methods; | |
| 1347 | + } | |
| 1348 | + | |
| 1349 | + add_action('plugins_loaded', 'woocommerce_payment_makecommerce_init'); | |
| 1350 | + add_action('woocommerce_payment_gateways', 'woocommerce_payment_makecommerce_add'); | |
| 1351 | + | |
| 1352 | + function clear_wc_shipping_rates_cache(){ | |
| 1353 | + $packages = WC()->cart->get_shipping_packages(); | |
| 1354 | + foreach ($packages as $key => $value) { | |
| 1355 | + $shipping_session = "shipping_for_package_$key"; | |
| 1356 | + unset(WC()->session->$shipping_session); | |
| 1357 | + } | |
| 1358 | + } | |
| 1359 | + add_filter('woocommerce_checkout_update_order_review', 'clear_wc_shipping_rates_cache'); | |
| 1360 | + | |
| 1361 | + | |
| 1362 | + | |
| 1363 | + | |
| 1364 | + // Parcel machine specific stuff | |
| 1365 | + function transport_add_method() { | |
| 1366 | + | |
| 1367 | + // Delete all the wc_ship transient scum, you aren’t wanted around here, move along. | |
| 1368 | + // Same as being in shipping debug mode | |
| 1369 | + global $wpdb; | |
| 1370 | + $transients = $wpdb->get_col("SELECT option_name FROM $wpdb->options WHERE option_name LIKE '_transient_wc_ship%'"); | |
| 1371 | + if (count($transients)) { | |
| 1372 | + foreach ($transients as $tr) { | |
| 1373 | + $hash = substr($tr, 11); | |
| 1374 | + delete_transient($hash); | |
| 1375 | + } | |
| 1376 | + } | |
| 1377 | + $transient_value = get_transient('shipping-transient-version'); | |
| 1378 | + WC_Cache_Helper::delete_version_transients( $transient_value ); | |
| 1379 | + if (WC()->session) { | |
| 1380 | + WC()->session->set('shipping_for_package', ''); | |
| 1381 | + } | |
| 1382 | + | |
| 1383 | + | |
| 1384 | + if ( ! class_exists( 'WC_ParcelMachine_Shipping_Method' ) ) { | |
| 1385 | + | |
| 1386 | + class WC_ParcelMachine_Shipping_Method extends WC_Shipping_Method { | |
| 1387 | + | |
| 1388 | + function __construct($instance_id = 0) { | |
| 1389 | + if (!$this->ext) { | |
| 1390 | + throw new Exception('Do not call this class directly!'); | |
| 1391 | + } | |
| 1392 | + $this->id = 'parcelmachine_' . mb_strtolower($this->ext); | |
| 1393 | + $this->instance_id = absint($instance_id); | |
| 1394 | + $this->method_title = $this->name_ext . __(' Parcel Machine (MC)', 'wc_makecommerce_domain'); | |
| 1395 | + // $this->method_description = sprintf(__('Parcel machine transport method for %s', 'wc_makecommerce_domain'), $this->ext); | |
| 1396 | + $this->supports = array('shipping-zones', 'instance-settings', 'instance-settings-modal', 'settings'); | |
| 1397 | + $this->init(); | |
| 1398 | + } | |
| 1399 | + | |
| 1400 | + function init() { | |
| 1401 | + $this->init_form_fields(); | |
| 1402 | + $this->init_settings(); | |
| 1403 | + | |
| 1404 | + $this->enable = $this->settings['active']; | |
| 1405 | + $this->title = $this->settings['method_name']; | |
| 1406 | + if (defined('ICL_LANGUAGE_CODE') && !empty($this->settings['method_name_'.ICL_LANGUAGE_CODE])) { | |
| 1407 | + $this->title = $this->settings['method_name_'.ICL_LANGUAGE_CODE]; | |
| 1408 | + } | |
| 1409 | + $this->availability = 'specific'; | |
| 1410 | + $this->countries = $this->settings['countries']; | |
| 1411 | + $this->prioritization = $this->settings['prioritization']; | |
| 1412 | + $this->free_shipping_min_amount = !empty($this->settings['free_shipping_min_amount']) ? $this->settings['free_shipping_min_amount'] : 0; | |
| 1413 | + $this->maximum_weight = (double)$this->settings['maximum_weight']; | |
| 1414 | + $this->short_office_names = $this->settings['short_office_names']; | |
| 1415 | + $this->order_country = 'unknown'; | |
| 1416 | + add_action('woocommerce_update_options_shipping_' . $this->id, array(&$this, 'process_admin_options')); | |
| 1417 | + add_filter('woocommerce_review_order_after_shipping' , array(&$this, 'add_parcelmachine_checkout_fields')); | |
| 1418 | + //add_filter('woocommerce_update_order_review_fragments' , array(&$this, 'add_parcelmachine_checkout_fields')); | |
| 1419 | + add_action('woocommerce_checkout_process', array(&$this, 'check_parcelmachine_checkout_fields')); | |
| 1420 | + add_action('woocommerce_after_checkout_validation', array(&$this, 'check_parcelmachine_checkout_fields')); | |
| 1421 | + add_action('woocommerce_checkout_update_order_meta', array(&$this, 'add_parcelmachine_order_meta')); | |
| 1422 | + add_filter('woocommerce_order_shipping_to_display_shipped_via', array(&$this, 'add_admin_parcelmachine_via_field')); | |
| 1423 | + | |
| 1424 | + // Woocommerce Multilingual overrides | |
| 1425 | + if ( in_array( 'woocommerce-multilingual/wpml-woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) { | |
| 1426 | + add_filter('woocommerce_package_rates', array($this, 'override_label_translation'), 50); | |
| 1427 | + add_filter('woocommerce_order_get_items', array($this, 'override_shipping_title_translation'), 50, 2); | |
| 1428 | + } | |
| 1429 | + } | |
| 1430 | + | |
| 1431 | + public function override_label_translation($rates) { | |
| 1432 | + $id_instance = $this->id . ':' . $this->instance_id; | |
| 1433 | + if(array_key_exists($id_instance, $rates)) { | |
| 1434 | + $rates[$id_instance]->label = $this->title; | |
| 1435 | + } | |
| 1436 | + return $rates; | |
| 1437 | + } | |
| 1438 | + | |
| 1439 | + public function override_shipping_title_translation($items, $order) { | |
| 1440 | + foreach ($items as $key => $item) { | |
| 1441 | + if($item['type'] == 'shipping') { | |
| 1442 | + foreach ($item['item_meta_array'] as $meta) { | |
| 1443 | + $tmp = explode(':', $meta->value); | |
| 1444 | + if($meta->key == 'method_id' && $tmp[0] == $this->id) { | |
| 1445 | + $items[$key]['name'] = $this->title; | |
| 1446 | + } | |
| 1447 | + } | |
| 1448 | + } | |
| 1449 | + } | |
| 1450 | + return $items; | |
| 1451 | + } | |
| 1452 | + | |
| 1453 | + public function generate_shipping_ui_javascript_html( $key, $data ) { ?> | |
| 1454 | + <script type="text/javascript"> | |
| 1455 | + jQuery(document).ready(function($) { | |
| 1456 | + var country_select = $('#woocommerce_parcelmachine_<?php echo strtolower($this->ext); ?>_countries'); | |
| 1457 | + var prices = [], free_shippings = []; | |
| 1458 | + $.each(country_select.find('option'), function(i, option) { | |
| 1459 | + prices[$(option).val()] = $('#woocommerce_parcelmachine_<?php echo strtolower($this->ext); ?>_price_' + $(option).val().toLowerCase()).closest('tr'); | |
| 1460 | + free_shippings[$(option).val()] = $('#woocommerce_parcelmachine_<?php echo strtolower($this->ext); ?>_free_shipping_min_amount_' + $(option).val().toLowerCase()).closest('tr'); | |
| 1461 | + }); | |
| 1462 | + country_select.on('change', function(){ | |
| 1463 | + var countries = $(this).val(); | |
| 1464 | + if(countries) { | |
| 1465 | + hidePrices(countries); | |
| 1466 | + } | |
| 1467 | + }); | |
| 1468 | + hidePrices(country_select.val()); | |
| 1469 | + function hidePrices(countries) { | |
| 1470 | + for (var key in prices) { | |
| 1471 | + if(countries.indexOf(key) == -1){ | |
| 1472 | + prices[key].hide(); | |
| 1473 | + free_shippings[key].hide(); | |
| 1474 | + } else { | |
| 1475 | + prices[key].show(); | |
| 1476 | + free_shippings[key].show(); | |
| 1477 | + } | |
| 1478 | + } | |
| 1479 | + } | |
| 1480 | + }); | |
| 1481 | + </script> | |
| 1482 | + <?php } | |
| 1483 | + | |
| 1484 | + function calculate_shipping($package = array()) { | |
| 1485 | + $price = isset($this->instance_settings['price']) ? $this->instance_settings['price'] : (isset($this->settings['price_'.$package['destination']['country']]) ? $this->settings['price_'.$package['destination']['country']] : 0); | |
| 1486 | + | |
| 1487 | + $free_shipping_min_amount = $this->instance_settings['free_shipping_min_amount']; | |
| 1488 | + | |
| 1489 | + if ($free_shipping_min_amount && $package['contents_cost'] >= $free_shipping_min_amount) { | |
| 1490 | + $price = 0; | |
| 1491 | + } | |
| 1492 | + | |
| 1493 | + $free_shipping = true; | |
| 1494 | + foreach ($package['contents'] as $line) { | |
| 1495 | + $free_shipping = get_post_meta($line['product_id'], '_no_shipping_cost', true) === 'yes' ? $free_shipping : false; | |
| 1496 | + } | |
| 1497 | + if($free_shipping) { | |
| 1498 | + $price = 0; | |
| 1499 | + } | |
| 1500 | + $rate = array( | |
| 1501 | + 'id' => $this->get_rate_id(), | |
| 1502 | + 'label' => $this->title, | |
| 1503 | + 'cost' => $price, | |
| 1504 | + 'package' => $package, | |
| 1505 | + 'calc_tax' => 'per_order', | |
| 1506 | + ); | |
| 1507 | + $this->add_rate( $rate ); | |
| 1508 | + } | |
| 1509 | + | |
| 1510 | + function calculate_weight($package) { | |
| 1511 | + $weight = 0; | |
| 1512 | + foreach ($package['contents'] as $line) { | |
| 1513 | + $weight += $line['data']->get_weight(); | |
| 1514 | + } | |
| 1515 | + return $weight; | |
| 1516 | + } | |
| 1517 | + | |
| 1518 | + function fits_parcel_machine($package) { | |
| 1519 | + foreach ($package['contents'] as $line) { | |
| 1520 | + if (get_post_meta($line['product_id'], '_no_parcel_machine', true) === 'yes') { | |
| 1521 | + return false; | |
| 1522 | + } | |
| 1523 | + } | |
| 1524 | + return true; | |
| 1525 | + } | |
| 1526 | + | |
| 1527 | + function is_available($package) { | |
| 1528 | + if (!$this->fits_parcel_machine($package)) { | |
| 1529 | + return false; | |
| 1530 | + } | |
| 1531 | + $package_weight = $this->calculate_weight($package); | |
| 1532 | + if ($package_weight >= $this->maximum_weight) { | |
| 1533 | + return false; | |
| 1534 | + } | |
| 1535 | + $is_available = $this->enable === 'yes'; | |
| 1536 | + if (!$is_available || !mk_is_api_set()) { | |
| 1537 | + return false; | |
| 1538 | + } | |
| 1539 | + if (is_array($this->countries) && !in_array($package['destination']['country'], $this->countries)) { | |
| 1540 | + $is_available = false; | |
| 1541 | + } | |
| 1542 | + $this->order_country = $package['destination']['country']; | |
| 1543 | + return apply_filters('woocommerce_shipping_' . $this->id . '_is_available', $is_available, $package); | |
| 1544 | + } | |
| 1545 | + | |
| 1546 | + function mk_get_machines() { | |
| 1547 | + $machines = mk_get_machines($this->ext, $this->order_country); | |
| 1548 | + if ($this->prioritization === 'yes') { | |
| 1549 | + usort($machines, function($a, $b) { | |
| 1550 | + $sortorder = array( | |
| 1551 | + 'tallinn', 'tartu linn','tartu', 'narva', 'pärnu linn','pärnu', 'viljandi', 'kohtla-järve', 'rakvere', 'maardu', 'sillamäe', 'kuressaare', | |
| 1552 | + 'helsinki', 'espoo', 'tampere', 'vantaa', 'oulu', 'turku', 'jüväskülä', 'lahti', 'kuopio', 'kouvola', | |
| 1553 | + 'rīga','riga', 'daugavpils', 'liepāja','liepaja', 'jelgava', 'jūrmala','jurmala', 'ventspils', 'rezekne', 'valmiera', 'jekabpils', | |
| 1554 | + 'vilnius', 'kaunas', 'klaipeda', 'siauliai', 'panevezys', 'alytus', 'mariampole', 'mazeikiai', 'jonava', 'utena' | |
| 1555 | + ); | |
| 1556 | + $acity = mb_strtolower($a['city']); | |
| 1557 | + $bcity = mb_strtolower($b['city']); | |
| 1558 | + if (!$acity) { $acity = 'xxxxxxx'; } | |
| 1559 | + if (!$bcity) { $bcity = 'xxxxxxx'; } | |
| 1560 | + $aidx = array_search($acity, $sortorder); | |
| 1561 | + $bidx = array_search($bcity, $sortorder); | |
| 1562 | + if ($aidx !== false) { | |
| 1563 | + $acity = str_pad($aidx, 4, "0", STR_PAD_LEFT) . '-' . $acity; | |
| 1564 | + } | |
| 1565 | + if ($bidx !== false) { | |
| 1566 | + $bcity = str_pad($bidx, 4, "0", STR_PAD_LEFT) . '-' . $bcity; | |
| 1567 | + } | |
| 1568 | + $acity .= '-' . mb_strtolower($a['name']); | |
| 1569 | + $bcity .= '-' . mb_strtolower($b['name']); | |
| 1570 | + return $acity < $bcity ? -1 : 1; | |
| 1571 | + }); | |
| 1572 | + } | |
| 1573 | + return $machines; | |
| 1574 | + } | |
| 1575 | + | |
| 1576 | + function add_parcelmachine_checkout_fields($fragments) { | |
| 1577 | + $this->order_country = WC()->customer->get_shipping_country(); | |
| 1578 | + $res = ''; | |
| 1579 | + $res .= '<tr style="display: none;" class="parcel_machine_checkout parcel_machine_checkout_parcelmachine_'.mb_strtolower($this->ext).'">'; | |
| 1580 | + // echo '<th>' . $this->title . '</th>'; | |
| 1581 | + // echo '<td>'; | |
| 1582 | + $res .= '<td colspan="2">'; | |
| 1583 | + $options = array(); | |
| 1584 | + $machines = $this->mk_get_machines(); | |
| 1585 | + $res .= '<p class="form-row" id="'.esc_attr($this->id).'_field">'; | |
| 1586 | + $res .= '<select class="select" name="'.esc_attr($this->id).'" id="'.esc_attr($this->id).'">'; | |
| 1587 | + $res .= '<option value="">'.__('-- select parcel machine --', 'wc_makecommerce_domain').'</option>'; | |
| 1588 | + $pcity = false; | |
| 1589 | + foreach ($machines as $machine) { | |
| 1590 | + $city = strtolower($machine['city']); | |
| 1591 | + if ($city !== $pcity) { | |
| 1592 | + if ($pcity) $res .= '</optgroup>'; | |
| 1593 | + $res .= '<optgroup label="'.$machine['city'].'">'; | |
| 1594 | + } | |
| 1595 | + $mname = $machine['name']; | |
| 1596 | + if ($this->short_office_names !== 'yes') $mname .= ' - ' . $machine['city'] . ', ' . $machine['address']; | |
| 1597 | + $res .= '<option value="'.esc_attr($machine['carrier'].'||'.$machine['id']).'">'.$mname.'</option>'; | |
| 1598 | + $pcity = $city; | |
| 1599 | + } | |
| 1600 | + if ($pcity) $res .= '</optgroup>'; | |
| 1601 | + $res .= '</select>'; | |
| 1602 | + $res .= '</p>'; | |
| 1603 | + $res .= '</td></tr>'; | |
| 1604 | + echo $res; | |
| 1605 | + } | |
| 1606 | + | |
| 1607 | + function check_parcelmachine_checkout_fields() { | |
| 1608 | + $shipping_method = !empty($_POST['shipping_method']) ? $_POST['shipping_method'] : false; | |
| 1609 | + if (!empty($shipping_method[0])) { $shipping_method_ext = explode(':', $shipping_method[0]); } | |
| 1610 | + if ($shipping_method_ext[0] === $this->id && empty($_POST[$shipping_method_ext[0]])) { | |
| 1611 | + wc_add_notice(__('<strong>Parcel machine</strong> is a required field.'), 'error'); | |
| 1612 | + } | |
| 1613 | + } | |
| 1614 | + | |
| 1615 | + function add_parcelmachine_order_meta($order_id) { | |
| 1616 | + $shipping_method = !empty($_POST['shipping_method']) ? $_POST['shipping_method'] : false; | |
| 1617 | + if (!empty($shipping_method[0])) { $shipping_method_ext = explode(':', $shipping_method[0]); } | |
| 1618 | + if ($shipping_method_ext[0] === $this->id && !empty($_POST[$this->id])) { | |
| 1619 | + update_post_meta($order_id, '_parcel_machine', sanitize_text_field($_POST[$this->id])); | |
| 1620 | + } | |
| 1621 | + } | |
| 1622 | + | |
| 1623 | + function add_admin_parcelmachine_via_field($order) { | |
| 1624 | + return ' <small>(via '.$this->name_ext.')</small>'; | |
| 1625 | + } | |
| 1626 | + } | |
| 1627 | + | |
| 1628 | + class WC_ParcelMachine_Shipping_Method_Omniva extends WC_ParcelMachine_Shipping_Method { | |
| 1629 | + function __construct($instance_id = 0) { | |
| 1630 | + $this->ext = 'Omniva'; | |
| 1631 | + $this->name_ext = 'Omniva'; | |
| 1632 | + $this->carrier = 'omniva'; | |
| 1633 | + $this->type = 'apt'; | |
| 1634 | + parent::__construct($instance_id); | |
| 1635 | + } | |
| 1636 | + function init_form_fields() { | |
| 1637 | + | |
| 1638 | + $this->instance_form_fields = array(); | |
| 1639 | + $this->instance_form_fields['logo'] = array('type' => 'title', 'title' => __get_logo_html()); | |
| 1640 | + $this->instance_form_fields['price'] = array( | |
| 1641 | + 'title' => __('Shipping price', 'wc_makecommerce_domain'), | |
| 1642 | + 'type' => 'text', | |
| 1643 | + 'default' => '2.99' | |
| 1644 | + ); | |
| 1645 | + $this->instance_form_fields['free_shipping_min_amount'] = array( | |
| 1646 | + 'title' => __('Free shipping amount', 'wc_makecommerce_domain'), | |
| 1647 | + 'type' => 'number', | |
| 1648 | + 'default' => '0', | |
| 1649 | + 'desc_tip' => __('(0 means no free shipping)', 'wc_makecommerce_domain'), | |
| 1650 | + ); | |
| 1651 | + | |
| 1652 | + $this->form_fields = array(); | |
| 1653 | + $this->form_fields['logo'] = array('type' => 'title', 'title' => __get_logo_html()); | |
| 1654 | + $this->form_fields['generic'] = array( | |
| 1655 | + 'type' => 'title', | |
| 1656 | + 'title' => __('Generic and pricing options', 'wc_makecommerce_domain'), | |
| 1657 | + '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').'<br>'.__('You can mark there also a product as "Free shipping in parcel machine."', 'wc_makecommerce_domain'), | |
| 1658 | + ); | |
| 1659 | + $this->form_fields['active'] = array( | |
| 1660 | + 'title' => __('Enable', 'wc_makecommerce_domain'), | |
| 1661 | + 'type' => 'checkbox', | |
| 1662 | + 'label' => __('enabled', 'wc_makecommerce_domain'), | |
| 1663 | + 'default' => 'no', | |
| 1664 | + ); | |
| 1665 | + $this->form_fields['maximum_weight'] = array( | |
| 1666 | + 'title' => sprintf(__('Maximum weight allowed for shipping (%s)', 'wc_makecommerce_domain'), get_option('woocommerce_weight_unit' )), | |
| 1667 | + 'type' => 'text', | |
| 1668 | + 'default' => '30' | |
| 1669 | + ); | |
| 1670 | + | |
| 1671 | + $this->form_fields['look_and_feel'] = array( 'type' => 'title', 'title' => '<hr><br>'.__('Look and feel options', 'wc_makecommerce_domain'), 'description' => __('Options for presentation on check-out page', 'wc_makecommerce_domain')); | |
| 1672 | + | |
| 1673 | + if ( function_exists('icl_object_id') && ! defined( 'POLYLANG_VERSION' ) ) { | |
| 1674 | + $languages = apply_filters('wpml_active_languages', NULL, 'skip_missing=0'); | |
| 1675 | + } | |
| 1676 | + else if ( defined( 'POLYLANG_VERSION' ) ) { | |
| 1677 | + $pl_locales = pll_languages_list( array('fields'=>'locale') ); | |
| 1678 | + foreach ($pl_locales as $key => $locale_pl ) { | |
| 1679 | + $language = array( 'id' => $key, | |
| 1680 | + 'code' => $locale_pl) ; | |
| 1681 | + $languages[$locale_pl] = $language; | |
| 1682 | + } | |
| 1683 | + } | |
| 1684 | + | |
| 1685 | + if (empty($languages)) { | |
| 1686 | + $this->form_fields['method_name'] = array( | |
| 1687 | + 'title' => __('Shipping Method Title', 'wc_makecommerce_domain'), | |
| 1688 | + 'type' => 'text', | |
| 1689 | + 'default' => __('Omniva Parcel Machine', 'wc_makecommerce_domain') | |
| 1690 | + ); | |
| 1691 | + } else { | |
| 1692 | + foreach ($languages as $language_code => $language) { | |
| 1693 | + $language_name = !empty($language['translated_name']) ? $language['translated_name'] : $language_code; | |
| 1694 | + $this->form_fields['method_name_'.$language_code] = array( | |
| 1695 | + 'title' => __('Shipping Method Title', 'wc_makecommerce_domain').sprintf(' (%s)', $language_code), | |
| 1696 | + 'type' => 'text', | |
| 1697 | + 'default' => __('Omniva Parcel Machine', 'wc_makecommerce_domain') | |
| 1698 | + ); | |
| 1699 | + } | |
| 1700 | + } | |
| 1701 | + $this->form_fields['prioritization'] = array( | |
| 1702 | + 'title' => __('Prioritize', 'wc_makecommerce_domain'), | |
| 1703 | + 'type' => 'checkbox', | |
| 1704 | + 'label' => __('Bigger cities will be on top of list, others sorted alphabetically', 'wc_makecommerce_domain'), | |
| 1705 | + 'default' => 'yes' | |
| 1706 | + ); | |
| 1707 | + $this->form_fields['short_office_names'] = array( | |
| 1708 | + 'title' => __('Short names', 'wc_makecommerce_domain'), | |
| 1709 | + 'type' => 'checkbox', | |
| 1710 | + 'label' => __('Display only parcel machine names, without addresses', 'wc_makecommerce_domain'), | |
| 1711 | + 'default' => 'no' | |
| 1712 | + ); | |
| 1713 | + $this->form_fields['api_access'] = array( 'type' => 'title', 'title' => '<hr><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§ion=mk_api'))); | |
| 1714 | + $this->form_fields['service_user'] = array( | |
| 1715 | + 'title' => __('Omniva web services username', 'wc_makecommerce_domain'), | |
| 1716 | + 'type' => 'text', | |
| 1717 | + 'default' => '' | |
| 1718 | + ); | |
| 1719 | + $this->form_fields['service_password'] = array( | |
| 1720 | + 'title' => __('Omniva web services password', 'wc_makecommerce_domain'), | |
| 1721 | + 'type' => 'text', | |
| 1722 | + 'default' => '' | |
| 1723 | + ); | |
| 1724 | + $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')); | |
| 1725 | + $this->form_fields['shop_name'] = array( | |
| 1726 | + 'type' => 'text', | |
| 1727 | + 'title' => __('Shop name', 'wc_makecommerce_domain'), | |
| 1728 | + 'class' => 'input-text regular-input', | |
| 1729 | + ); | |
| 1730 | + $this->form_fields['shop_phone'] = array( | |
| 1731 | + 'type' => 'text', | |
| 1732 | + 'title' => __('Shop phone', 'wc_makecommerce_domain'), | |
| 1733 | + 'class' => 'input-text regular-input', | |
| 1734 | + ); | |
| 1735 | + $this->form_fields['shop_email'] = array( | |
| 1736 | + 'type' => 'text', | |
| 1737 | + 'title' => __('Shop email', 'wc_makecommerce_domain'), | |
| 1738 | + 'class' => 'input-text regular-input', | |
| 1739 | + ); | |
| 1740 | + $this->form_fields['shop_address_country'] = array( | |
| 1741 | + 'type' => 'text', | |
| 1742 | + 'title' => __('Shop address country', 'wc_makecommerce_domain'), | |
| 1743 | + 'description' => __('Put here short country code, i.e. EE, LV, FI', 'wc_makecommerce_domain'), | |
| 1744 | + 'class' => 'input-text regular-input', | |
| 1745 | + ); | |
| 1746 | + $this->form_fields['shop_address_city'] = array( | |
| 1747 | + 'type' => 'text', | |
| 1748 | + 'title' => __('Shop address city', 'wc_makecommerce_domain'), | |
| 1749 | + 'class' => 'input-text regular-input', | |
| 1750 | + ); | |
| 1751 | + $this->form_fields['shop_address_street'] = array( | |
| 1752 | + 'type' => 'text', | |
| 1753 | + 'title' => __('Shop address street', 'wc_makecommerce_domain'), | |
| 1754 | + 'class' => 'input-text regular-input', | |
| 1755 | + ); | |
| 1756 | + $this->form_fields['shop_postal_code'] = array( | |
| 1757 | + 'type' => 'text', | |
| 1758 | + 'title' => __('Shop postal code', 'wc_makecommerce_domain'), | |
| 1759 | + 'class' => 'input-text regular-input', | |
| 1760 | + ); | |
| 1761 | + $this->form_fields['shipping_ui_javascript'] = array( | |
| 1762 | + 'type' => 'shipping_ui_javascript', | |
| 1763 | + ); | |
| 1764 | + } | |
| 1765 | + } | |
| 1766 | + | |
| 1767 | + class WC_ParcelMachine_Shipping_Method_Smartpost extends WC_ParcelMachine_Shipping_Method { | |
| 1768 | + function __construct($instance_id = 0) { | |
| 1769 | + $this->ext = 'SmartPost'; | |
| 1770 | + $this->name_ext = 'SmartPOST'; | |
| 1771 | + $this->carrier = 'smartpost'; | |
| 1772 | + $this->type = 'apt'; | |
| 1773 | + parent::__construct($instance_id); | |
| 1774 | + } | |
| 1775 | + function init_form_fields() { | |
| 1776 | + | |
| 1777 | + global $woocommerce; | |
| 1778 | + | |
| 1779 | + $this->instance_form_fields = array(); | |
| 1780 | + $this->instance_form_fields['logo'] = array('type' => 'title', 'title' => __get_logo_html()); | |
| 1781 | + | |
| 1782 | + $this->instance_form_fields['price'] = array( | |
| 1783 | + 'title' => __('Shipping price', 'wc_makecommerce_domain'), | |
| 1784 | + 'type' => 'text', | |
| 1785 | + 'default' => '2.99' | |
| 1786 | + ); | |
| 1787 | + $this->instance_form_fields['free_shipping_min_amount'] = array( | |
| 1788 | + 'title' => __('Free shipping amount', 'wc_makecommerce_domain'), | |
| 1789 | + 'type' => 'number', | |
| 1790 | + 'default' => '0', | |
| 1791 | + 'desc_tip' => __('(0 means no free shipping)', 'wc_makecommerce_domain'), | |
| 1792 | + ); | |
| 1793 | + | |
| 1794 | + $this->form_fields = array(); | |
| 1795 | + $this->form_fields['logo'] = array('type' => 'title', 'title' => __get_logo_html()); | |
| 1796 | + $this->form_fields['generic'] = array( | |
| 1797 | + 'type' => 'title', | |
| 1798 | + 'title' => __('Generic and pricing options', 'wc_makecommerce_domain'), | |
| 1799 | + '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').'<br>'.__('You can mark there also a product as "Free shipping in parcel machine."', 'wc_makecommerce_domain'), | |
| 1800 | + ); | |
| 1801 | + | |
| 1802 | + $this->form_fields['active'] = array( | |
| 1803 | + 'title' => __('Enable', 'wc_makecommerce_domain'), | |
| 1804 | + 'type' => 'checkbox', | |
| 1805 | + 'label' => __('enabled', 'wc_makecommerce_domain'), | |
| 1806 | + 'default' => 'no', | |
| 1807 | + ); | |
| 1808 | + $this->form_fields['maximum_weight'] = array( | |
| 1809 | + 'title' => sprintf(__('Maximum weight allowed for shipping (%s)', 'wc_makecommerce_domain'), get_option('woocommerce_weight_unit' )), | |
| 1810 | + 'type' => 'text', | |
| 1811 | + 'default' => '30' | |
| 1812 | + ); | |
| 1813 | + | |
| 1814 | + $this->form_fields['look_and_feel'] = array( 'type' => 'title', 'title' => '<hr><br>'.__('Look and feel options', 'wc_makecommerce_domain'), 'description' => __('Options for presentation on checkout page', 'wc_makecommerce_domain')); | |
| 1815 | + | |
| 1816 | + if ( function_exists('icl_object_id') && ! defined( 'POLYLANG_VERSION' ) ) { | |
| 1817 | + $languages = apply_filters('wpml_active_languages', NULL, 'skip_missing=0'); | |
| 1818 | + } | |
| 1819 | + else if ( defined( 'POLYLANG_VERSION' ) ) { | |
| 1820 | + $pl_locales = pll_languages_list( array('fields'=>'locale') ); | |
| 1821 | + foreach ($pl_locales as $key => $locale_pl ) { | |
| 1822 | + $language = array( 'id' => $key, | |
| 1823 | + 'code' => $locale_pl) ; | |
| 1824 | + $languages[$locale_pl] = $language; | |
| 1825 | + } | |
| 1826 | + } | |
| 1827 | + | |
| 1828 | + if (empty($languages)) { | |
| 1829 | + $this->form_fields['method_name'] = array( | |
| 1830 | + 'title' => __('Shipping Method Title', 'wc_makecommerce_domain'), | |
| 1831 | + 'type' => 'text', | |
| 1832 | + 'default' => __('SmartPOST Parcel Machine', 'wc_makecommerce_domain') | |
| 1833 | + ); | |
| 1834 | + } else { | |
| 1835 | + foreach ($languages as $language_code => $language) { | |
| 1836 | + $language_name = !empty($language['translated_name']) ? $language['translated_name'] : $language_code; | |
| 1837 | + $this->form_fields['method_name_'.$language_code] = array( | |
| 1838 | + 'title' => __('Shipping Method Title', 'wc_makecommerce_domain').sprintf(' (%s)', $language_code), | |
| 1839 | + 'type' => 'text', | |
| 1840 | + 'default' => __('SmartPOST Parcel Machine', 'wc_makecommerce_domain') | |
| 1841 | + ); | |
| 1842 | + } | |
| 1843 | + } | |
| 1844 | + $this->form_fields['prioritization'] = array( | |
| 1845 | + 'title' => __('Prioritize', 'wc_makecommerce_domain'), | |
| 1846 | + 'type' => 'checkbox', | |
| 1847 | + 'label' => __('Bigger cities will be on top of list, others sorted alphabetically', 'wc_makecommerce_domain'), | |
| 1848 | + 'default' => 'yes' | |
| 1849 | + ); | |
| 1850 | + $this->form_fields['short_office_names'] = array( | |
| 1851 | + 'title' => __('Short names', 'wc_makecommerce_domain'), | |
| 1852 | + 'type' => 'checkbox', | |
| 1853 | + 'label' => __('Display only parcel machine names, without addresses', 'wc_makecommerce_domain'), | |
| 1854 | + 'default' => 'no' | |
| 1855 | + ); | |
| 1856 | + $this->form_fields['api_access'] = array( 'type' => 'title', 'title' => '<hr><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§ion=mk_api'))); | |
| 1857 | + $this->form_fields['service_user'] = array( | |
| 1858 | + 'title' => __('eteenindus.smartpost.ee username', 'wc_makecommerce_domain'), | |
| 1859 | + 'type' => 'text', | |
| 1860 | + 'default' => '' | |
| 1861 | + ); | |
| 1862 | + $this->form_fields['service_password'] = array( | |
| 1863 | + 'title' => __('eteenindus.smartpost.ee password', 'wc_makecommerce_domain'), | |
| 1864 | + 'type' => 'text', | |
| 1865 | + 'default' => '' | |
| 1866 | + ); | |
| 1867 | + $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')); | |
| 1868 | + $this->form_fields['shop_name'] = array( | |
| 1869 | + 'type' => 'text', | |
| 1870 | + 'title' => __('Shop name', 'wc_makecommerce_domain'), | |
| 1871 | + 'class' => 'input-text regular-input', | |
| 1872 | + ); | |
| 1873 | + $this->form_fields['shop_phone'] = array( | |
| 1874 | + 'type' => 'text', | |
| 1875 | + 'title' => __('Shop phone', 'wc_makecommerce_domain'), | |
| 1876 | + 'class' => 'input-text regular-input', | |
| 1877 | + ); | |
| 1878 | + $this->form_fields['shop_email'] = array( | |
| 1879 | + 'type' => 'text', | |
| 1880 | + 'title' => __('Shop email', 'wc_makecommerce_domain'), | |
| 1881 | + 'class' => 'input-text regular-input', | |
| 1882 | + ); | |
| 1883 | + $this->form_fields['shipping_ui_javascript'] = array( | |
| 1884 | + 'type' => 'shipping_ui_javascript', | |
| 1885 | + ); | |
| 1886 | + } | |
| 1887 | + } | |
| 1888 | + | |
| 1889 | + if (($cur_ver = get_site_option('wc_mc_version', 0)) < WC_MC_VERSION) { | |
| 1890 | + error_log("Need to update from [{$cur_ver}] to [".WC_MC_VERSION."]"); | |
| 1891 | + if (WC_MC_VERSION === 2.0) { | |
| 1892 | + // Check if omniva / smartpost is enabled and in which countries | |
| 1893 | + // Convert to shipping zone | |
| 1894 | + foreach (array('parcelmachine_omniva' => 'WC_ParcelMachine_Shipping_Method_Omniva', 'parcelmachine_smartpost' => 'WC_ParcelMachine_Shipping_Method_SmartPost') as $method_name => $method_class) { | |
| 1895 | + $transport_class_omniva = new $method_class(); | |
| 1896 | + if ($transport_class_omniva->enable === 'yes') { | |
| 1897 | + $zones = WC_Shipping_Zones::get_zones(); | |
| 1898 | + foreach ($transport_class_omniva->countries as $ecountry) { | |
| 1899 | + $has_ecountry = false; | |
| 1900 | + foreach ($zones as $zone) { | |
| 1901 | + foreach ($zone['zone_locations'] as $location) { | |
| 1902 | + if ($location->code === $ecountry) { | |
| 1903 | + $has_ecountry = $zone['zone_id']; | |
| 1904 | + foreach ($zone['shipping_methods'] as $method) { | |
| 1905 | + if ($method->id === $method_name) { | |
| 1906 | + continue 4; | |
| 1907 | + } | |
| 1908 | + } | |
| 1909 | + } | |
| 1910 | + } | |
| 1911 | + } | |
| 1912 | + if (!$has_ecountry) { | |
| 1913 | + $zone = new WC_Shipping_Zone(); | |
| 1914 | + $zone->set_zone_name($ecountry); | |
| 1915 | + $zone->add_location($ecountry, 'country'); | |
| 1916 | + $zone->save(); | |
| 1917 | + } else { | |
| 1918 | + $zone = new WC_Shipping_Zone($has_ecountry); | |
| 1919 | + } | |
| 1920 | + $instance_id = $zone->add_shipping_method($method_name); | |
| 1921 | + $transport_class = new $method_class($instance_id); | |
| 1922 | + $price = !empty($transport_class->settings['price_'.strtolower($ecountry)]) ? $transport_class->settings['price_'.strtolower($ecountry)] : 0; | |
| 1923 | + $free_shipping_min_amount = !empty($transport_class->settings['free_shipping_min_amount_'.strtolower($ecountry)]) ? $transport_class->settings['free_shipping_min_amount_'.strtolower($ecountry)] : 0; | |
| 1924 | + $transport_class->set_post_data(array($transport_class->get_field_key('price') => $price, $transport_class->get_field_key('free_shipping_min_amount') => $free_shipping_min_amount)); | |
| 1925 | + $transport_class->process_admin_options(); | |
| 1926 | + } | |
| 1927 | + } | |
| 1928 | + } | |
| 1929 | + } | |
| 1930 | + update_site_option('wc_mc_version', WC_MC_VERSION); | |
| 1931 | + } | |
| 1932 | + } | |
| 1933 | + | |
| 1934 | + if ( ! class_exists( 'WC_Courier_Shipping_Method_Omniva' ) ) { | |
| 1935 | + | |
| 1936 | + class WC_Courier_Shipping_Method_Omniva extends WC_Shipping_Method { | |
| 1937 | + | |
| 1938 | + public function __construct($instance_id = 0) { | |
| 1939 | + $this->ext = 'Omniva'; | |
| 1940 | + $this->name_ext = 'Omniva'; | |
| 1941 | + $this->id = 'courier_omniva'; | |
| 1942 | + $this->carrier = 'omniva'; | |
| 1943 | + $this->type = 'courier'; | |
| 1944 | + $this->instance_id = absint($instance_id); | |
| 1945 | + $this->method_title = $this->name_ext .' '.__('courier (MC)', 'wc_makecommerce_domain'); | |
| 1946 | + // $this->method_description = sprintf(__('Courier transport method for %s', 'wc_makecommerce_domain'), $this->ext); | |
| 1947 | + $this->supports = array('shipping-zones', 'instance-settings', 'instance-settings-modal', 'settings'); | |
| 1948 | + $this->init(); | |
| 1949 | + } | |
| 1950 | + | |
| 1951 | + public function init() { | |
| 1952 | + $this->init_form_fields(); | |
| 1953 | + $this->init_settings(); | |
| 1954 | + | |
| 1955 | + $this->enable = $this->settings['active']; | |
| 1956 | + if (defined('ICL_LANGUAGE_CODE') && !empty($this->settings['method_name_'.ICL_LANGUAGE_CODE])) { | |
| 1957 | + $this->title = $this->settings['method_name_'.ICL_LANGUAGE_CODE]; | |
| 1958 | + } else { | |
| 1959 | + $this->title = $this->settings['method_name']; | |
| 1960 | + } | |
| 1961 | + $this->availability = 'specific'; | |
| 1962 | + $this->free_shipping_min_amount = !empty($this->settings['free_shipping_min_amount']) ? $this->settings['free_shipping_min_amount'] : 0; | |
| 1963 | + $this->maximum_weight = (double)$this->settings['maximum_weight']; | |
| 1964 | + $this->order_country = 'unknown'; | |
| 1965 | + add_action('woocommerce_update_options_shipping_' . $this->id, array(&$this, 'process_admin_options')); | |
| 1966 | + add_filter('woocommerce_order_shipping_to_display_shipped_via', array(&$this, 'add_admin_courier_via_field')); | |
| 1967 | + | |
| 1968 | + // Woocommerce Multilingual overrides | |
| 1969 | + if ( in_array( 'woocommerce-multilingual/wpml-woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) { | |
| 1970 | + add_filter('woocommerce_package_rates', array($this, 'override_label_translation'), 50); | |
| 1971 | + add_filter('woocommerce_order_get_items', array($this, 'override_shipping_title_translation'), 50, 2); | |
| 1972 | + } | |
| 1973 | + } | |
| 1974 | + | |
| 1975 | + public function override_label_translation($rates) { | |
| 1976 | + $id_instance = $this->id . ':' . $this->instance_id; | |
| 1977 | + if(array_key_exists($id_instance, $rates)) { | |
| 1978 | + $rates[$id_instance]->label = $this->title; | |
| 1979 | + } | |
| 1980 | + return $rates; | |
| 1981 | + } | |
| 1982 | + | |
| 1983 | + public function override_shipping_title_translation($items, $order) { | |
| 1984 | + foreach ($items as $key => $item) { | |
| 1985 | + if($item['type'] == 'shipping') { | |
| 1986 | + foreach ($item['item_meta_array'] as $meta) { | |
| 1987 | + $tmp = explode(':', $meta->value); | |
| 1988 | + if($meta->key == 'method_id' && $tmp[0] == $this->id) { | |
| 1989 | + $items[$key]['name'] = $this->title; | |
| 1990 | + } | |
| 1991 | + } | |
| 1992 | + } | |
| 1993 | + } | |
| 1994 | + return $items; | |
| 1995 | + } | |
| 1996 | + | |
| 1997 | + public function init_form_fields() { | |
| 1998 | + | |
| 1999 | + global $woocommerce; | |
| 2000 | + | |
| 2001 | + $this->instance_form_fields = array(); | |
| 2002 | + $this->instance_form_fields['logo'] = array('type' => 'title', 'title' => __get_logo_html()); | |
| 2003 | + $this->instance_form_fields['price'] = array( | |
| 2004 | + 'title' => __('Shipping price', 'wc_makecommerce_domain'), | |
| 2005 | + 'type' => 'text', | |
| 2006 | + 'default' => '4.95' | |
| 2007 | + ); | |
| 2008 | + $this->instance_form_fields['free_shipping_min_amount'] = array( | |
| 2009 | + 'title' => __('Free shipping amount', 'wc_makecommerce_domain'), | |
| 2010 | + 'type' => 'number', | |
| 2011 | + 'default' => '0', | |
| 2012 | + 'desc_tip' => __('(0 means no free shipping)', 'wc_makecommerce_domain'), | |
| 2013 | + ); | |
| 2014 | + | |
| 2015 | + $this->form_fields = array(); | |
| 2016 | + $this->form_fields['logo'] = array('type' => 'title', 'title' => __get_logo_html()); | |
| 2017 | + $this->form_fields['generic'] = array( | |
| 2018 | + 'type' => 'title', | |
| 2019 | + 'title' => __('Generic and pricing options', 'wc_makecommerce_domain'), | |
| 2020 | + // 'description' => __('', 'wc_makecommerce_domain'), | |
| 2021 | + ); | |
| 2022 | + $this->form_fields['active'] = array( | |
| 2023 | + 'title' => __('Enable', 'wc_makecommerce_domain'), | |
| 2024 | + 'type' => 'checkbox', | |
| 2025 | + 'label' => __('enabled', 'wc_makecommerce_domain'), | |
| 2026 | + 'default' => 'no', | |
| 2027 | + ); | |
| 2028 | + $this->form_fields['maximum_weight'] = array( | |
| 2029 | + 'title' => sprintf(__('Maximum weight allowed for shipping (%s)', 'wc_makecommerce_domain'), get_option('woocommerce_weight_unit' )), | |
| 2030 | + 'type' => 'text', | |
| 2031 | + 'default' => '60' | |
| 2032 | + ); | |
| 2033 | + | |
| 2034 | + $this->form_fields['look_and_feel'] = array( 'type' => 'title', 'title' => '<hr><br>'.__('Look and feel options', 'wc_makecommerce_domain'), 'description' => __('Options for presentation on check-out page', 'wc_makecommerce_domain')); | |
| 2035 | + | |
| 2036 | + if ( function_exists('icl_object_id') && ! defined( 'POLYLANG_VERSION' ) ) { | |
| 2037 | + $languages = apply_filters('wpml_active_languages', NULL, 'skip_missing=0'); | |
| 2038 | + } | |
| 2039 | + else if ( defined( 'POLYLANG_VERSION' ) ) { | |
| 2040 | + $pl_locales = pll_languages_list( array('fields'=>'locale') ); | |
| 2041 | + foreach ($pl_locales as $key => $locale_pl ) { | |
| 2042 | + $language = array( 'id' => $key, | |
| 2043 | + 'code' => $locale_pl) ; | |
| 2044 | + $languages[$locale_pl] = $language; | |
| 2045 | + } | |
| 2046 | + } | |
| 2047 | + | |
| 2048 | + if (empty($languages)) { | |
| 2049 | + $this->form_fields['method_name'] = array( | |
| 2050 | + 'title' => __('Shipping Method Title', 'wc_makecommerce_domain'), | |
| 2051 | + 'type' => 'text', | |
| 2052 | + 'default' => __('Omniva Courier', 'wc_makecommerce_domain') | |
| 2053 | + ); | |
| 2054 | + } else { | |
| 2055 | + foreach ($languages as $language_code => $language) { | |
| 2056 | + $language_name = !empty($language['translated_name']) ? $language['translated_name'] : $language_code; | |
| 2057 | + $this->form_fields['method_name_'.$language_code] = array( | |
| 2058 | + 'title' => __('Shipping Method Title', 'wc_makecommerce_domain').sprintf(' (%s)', $language_code), | |
| 2059 | + 'type' => 'text', | |
| 2060 | + 'default' => __('Omniva Courier', 'wc_makecommerce_domain') | |
| 2061 | + ); | |
| 2062 | + } | |
| 2063 | + } | |
| 2064 | + | |
| 2065 | + | |
| 2066 | + $this->form_fields['api_access'] = array( 'type' => 'title', 'title' => '<hr><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§ion=mk_api'))); | |
| 2067 | + | |
| 2068 | + $this->form_fields['registerOnPaymentCode'] = array( | |
| 2069 | + 'title' => __('Register shipments automatically as', 'wc_makecommerce_domain'), | |
| 2070 | + 'type' => 'select', | |
| 2071 | + 'label' => __('register on Payment', 'wc_makecommerce_domain'), | |
| 2072 | + 'description' => __('Shipments will be automatically registered on payment using the selected Omniva service code', 'wc_makecommerce_domain'), | |
| 2073 | + 'options' => array( | |
| 2074 | + 'QP' => __('QP - handover to Omniva at Post Office', 'wc_makecommerce_domain'), | |
| 2075 | + 'PK' => __('PK - handover to Omniva via Parcel Machine', 'wc_makecommerce_domain'), | |
| 2076 | + '' => __('None - do not register automatically', 'wc_makecommerce_domain'), | |
| 2077 | + ), | |
| 2078 | + 'default' => 'QP', | |
| 2079 | + ); | |
| 2080 | + | |
| 2081 | + $this->form_fields['service_user'] = array( | |
| 2082 | + 'title' => __('Omniva web services username', 'wc_makecommerce_domain'), | |
| 2083 | + 'type' => 'text', | |
| 2084 | + 'default' => '' | |
| 2085 | + ); | |
| 2086 | + $this->form_fields['service_password'] = array( | |
| 2087 | + 'title' => __('Omniva web services password', 'wc_makecommerce_domain'), | |
| 2088 | + 'type' => 'text', | |
| 2089 | + 'default' => '' | |
| 2090 | + ); | |
| 2091 | + $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')); | |
| 2092 | + $this->form_fields['shop_name'] = array( | |
| 2093 | + 'type' => 'text', | |
| 2094 | + 'title' => __('Shop name', 'wc_makecommerce_domain'), | |
| 2095 | + 'class' => 'input-text regular-input', | |
| 2096 | + ); | |
| 2097 | + $this->form_fields['shop_phone'] = array( | |
| 2098 | + 'type' => 'text', | |
| 2099 | + 'title' => __('Shop phone', 'wc_makecommerce_domain'), | |
| 2100 | + 'class' => 'input-text regular-input', | |
| 2101 | + ); | |
| 2102 | + $this->form_fields['shop_email'] = array( | |
| 2103 | + 'type' => 'text', | |
| 2104 | + 'title' => __('Shop email', 'wc_makecommerce_domain'), | |
| 2105 | + 'class' => 'input-text regular-input', | |
| 2106 | + ); | |
| 2107 | + $this->form_fields['shop_address_country'] = array( | |
| 2108 | + 'type' => 'text', | |
| 2109 | + 'title' => __('Shop address country', 'wc_makecommerce_domain'), | |
| 2110 | + 'description' => __('Put here short country code, i.e. EE, LV, FI', 'wc_makecommerce_domain'), | |
| 2111 | + 'class' => 'input-text regular-input', | |
| 2112 | + ); | |
| 2113 | + $this->form_fields['shop_address_city'] = array( | |
| 2114 | + 'type' => 'text', | |
| 2115 | + 'title' => __('Shop address city', 'wc_makecommerce_domain'), | |
| 2116 | + 'class' => 'input-text regular-input', | |
| 2117 | + ); | |
| 2118 | + $this->form_fields['shop_address_street'] = array( | |
| 2119 | + 'type' => 'text', | |
| 2120 | + 'title' => __('Shop address street', 'wc_makecommerce_domain'), | |
| 2121 | + 'class' => 'input-text regular-input', | |
| 2122 | + ); | |
| 2123 | + $this->form_fields['shop_postal_code'] = array( | |
| 2124 | + 'type' => 'text', | |
| 2125 | + 'title' => __('Shop postal code', 'wc_makecommerce_domain'), | |
| 2126 | + 'class' => 'input-text regular-input', | |
| 2127 | + ); | |
| 2128 | + } | |
| 2129 | + | |
| 2130 | + public function calculate_shipping($package = array()) { | |
| 2131 | + | |
| 2132 | + $price = $this->instance_settings['price']; | |
| 2133 | + | |
| 2134 | + $free_shipping_min_amount = $this->instance_settings['free_shipping_min_amount']; | |
| 2135 | + | |
| 2136 | + if ($free_shipping_min_amount && $package['contents_cost'] >= $free_shipping_min_amount) { | |
| 2137 | + $price = 0; | |
| 2138 | + } | |
| 2139 | + | |
| 2140 | + $rate = array( | |
| 2141 | + 'id' => $this->get_rate_id(), | |
| 2142 | + 'label' => $this->title, | |
| 2143 | + 'cost' => $price, | |
| 2144 | + 'package' => $package, | |
| 2145 | + 'calc_tax' => 'per_order', | |
| 2146 | + ); | |
| 2147 | + $this->add_rate( $rate ); | |
| 2148 | + } | |
| 2149 | + | |
| 2150 | + public function calculate_weight($package) { | |
| 2151 | + $weight = 0; | |
| 2152 | + foreach ($package['contents'] as $line) { | |
| 2153 | + $weight += $line['data']->get_weight(); | |
| 2154 | + } | |
| 2155 | + return $weight; | |
| 2156 | + } | |
| 2157 | + | |
| 2158 | + public function is_available($package) { | |
| 2159 | + | |
| 2160 | + $package_weight = $this->calculate_weight($package); | |
| 2161 | + if ($package_weight >= $this->maximum_weight) { | |
| 2162 | + return false; | |
| 2163 | + } | |
| 2164 | + $is_available = $this->enable === 'yes'; | |
| 2165 | + if (!$is_available || !mk_is_api_set()) { | |
| 2166 | + return false; | |
| 2167 | + } | |
| 2168 | + | |
| 2169 | + $this->order_country = $package['destination']['country']; | |
| 2170 | + return apply_filters('woocommerce_shipping_' . $this->id . '_is_available', $is_available, $package); | |
| 2171 | + } | |
| 2172 | + | |
| 2173 | + public function add_admin_courier_via_field($order) { | |
| 2174 | + return ' <small>(via ' . $this->name_ext . ' ' . __('Courier', 'wc_makecommerce_domain') . ')</small>'; | |
| 2175 | + } | |
| 2176 | + } | |
| 2177 | + } | |
| 2178 | + } | |
| 2179 | + add_action('woocommerce_shipping_init', 'transport_add_method'); | |
| 2180 | + | |
| 2181 | + function add_mc_shipping_method($methods) { | |
| 2182 | + $methods['parcelmachine_omniva'] = 'WC_ParcelMachine_Shipping_Method_Omniva'; | |
| 2183 | + $methods['parcelmachine_smartpost'] = 'WC_ParcelMachine_Shipping_Method_Smartpost'; | |
| 2184 | + $methods['courier_omniva'] = 'WC_Courier_Shipping_Method_Omniva'; | |
| 2185 | + return $methods; | |
| 2186 | + } | |
| 2187 | + add_filter('woocommerce_shipping_methods', 'add_mc_shipping_method'); | |
| 2188 | + | |
| 2189 | + function mk_mc_add_assets() { | |
| 2190 | + wp_enqueue_style('parcelmachine-css', plugin_dir_url(__FILE__).'/css/parcelmachine.css'); | |
| 2191 | + wp_enqueue_script('parcelmachine-js', plugin_dir_url(__FILE__).'/scripts/parcelmachine.js', array('jquery')); | |
| 2192 | + } | |
| 2193 | + add_action('wp_enqueue_scripts', 'mk_mc_add_assets'); | |
| 2194 | + | |
| 2195 | + function mk_admin_plugin_settings_link($links) { | |
| 2196 | + $settings_link = '<a href="admin.php?page=wc-settings&tab=api§ion=mk_api">API '.__('Settings').'</a>'; | |
| 2197 | + array_unshift($links, $settings_link); | |
| 2198 | + return $links; | |
| 2199 | + } | |
| 2200 | + $plugin = plugin_basename(__FILE__); | |
| 2201 | + add_filter("plugin_action_links_$plugin", 'mk_admin_plugin_settings_link' ); | |
| 2202 | + | |
| 2203 | + function mk_admin_add_settings($sections) { | |
| 2204 | + $sections['mk_api'] = __('MakeCommerce API access', 'wc_makecommerce_domain'); | |
| 2205 | + return $sections; | |
| 2206 | + } | |
| 2207 | + | |
| 2208 | + function mk_admin_all_settings($settings) { | |
| 2209 | + global $current_section; | |
| 2210 | + if ($current_section !== 'mk_api') { | |
| 2211 | + return $settings; | |
| 2212 | + } | |
| 2213 | + return array( | |
| 2214 | + array('type' => 'title', 'desc' => __get_logo_html()), | |
| 2215 | + array( | |
| 2216 | + 'type' => 'title', | |
| 2217 | + 'title' => __('MakeCommerce API access credentials', 'wc_makecommerce_domain'), | |
| 2218 | + 'desc' => __('To use MakeCommerce/Maksekeskus services you need to enter API credentials below here <br/> <br/>', 'wc_makecommerce_domain'). | |
| 2219 | + sprintf( __('To further configure the Payment methods please go to <a href="%s">MakeCommerce Checkout Options</a><br>', 'wc_makecommerce_domain'), 'admin.php?page=wc-settings&tab=checkout§ion=makecommerce'). | |
| 2220 | + sprintf( __('To use also Ominva or Itella SmartPOST integration please configure these API accesses: <a href="%s">Omniva Parcel Machine API access</a> | '. | |
| 2221 | + '<a href="%s">SmartPOST API access</a> | <a href="%s">Omniva Courier API access</a><br/>','wc_makecommerce_domain'), | |
| 2222 | + 'admin.php?page=wc-settings&tab=-shipping§ion=parcelmachine_omniva', | |
| 2223 | + 'admin.php?page=wc-settings&tab=shipping§ion=parcelmachine_smartpost', | |
| 2224 | + 'admin.php?page=wc-settings&tab=shipping§ion=courier_omniva' | |
| 2225 | + ), | |
| 2226 | + 'id' => 'mk_api_settings' | |
| 2227 | + ), | |
| 2228 | + array( | |
| 2229 | + 'type' => 'select', | |
| 2230 | + 'title' => __('Current environment', 'wc_makecommerce_domain'), | |
| 2231 | + 'desc' => __('See more about <a href="https://maksekeskus.ee/en/for-developers/test-environment/">MakeCommerce Test environment</a>', 'wc_makecommerce_domain'), | |
| 2232 | + 'default' => 'live', | |
| 2233 | + 'options' => array( | |
| 2234 | + 'live' => __('Live', 'wc_makecommerce_domain'), | |
| 2235 | + 'test' => __('Test', 'wc_makecommerce_domain'), | |
| 2236 | + ), | |
| 2237 | + 'id' => 'mk_api_type' | |
| 2238 | + ), | |
| 2239 | + array( | |
| 2240 | + 'id' => 'mk_shop_id', | |
| 2241 | + 'type' => 'text', | |
| 2242 | + 'title' => __('Shop ID (live)', 'wc_makecommerce_domain'), | |
| 2243 | + 'desc' => __('Get it from <a href="https://merchant.maksekeskus.ee/api.html" target="_blank">Merchant Portal</a>','wc_makecommerce_domain'), | |
| 2244 | + 'class' => 'input-text regular-input', | |
| 2245 | + ), | |
| 2246 | + array( | |
| 2247 | + 'id' => 'mk_private_key', | |
| 2248 | + 'type' => 'text', | |
| 2249 | + 'title' => __('Secret key (live)', 'wc_makecommerce_domain'), | |
| 2250 | + 'class' => 'input-text regular-input', | |
| 2251 | + ), | |
| 2252 | + array( | |
| 2253 | + 'id' => 'mk_public_key', | |
| 2254 | + 'type' => 'text', | |
| 2255 | + 'title' => __('Publishable key (live)', 'wc_makecommerce_domain'), | |
| 2256 | + 'class' => 'input-text regular-input', | |
| 2257 | + ), | |
| 2258 | + array( | |
| 2259 | + 'id' => 'mk_test_shop_id', | |
| 2260 | + 'type' => 'text', | |
| 2261 | + 'title' => __('Shop ID (test)', 'wc_makecommerce_domain'), | |
| 2262 | + 'class' => 'input-text regular-input', | |
| 2263 | + 'desc' => __('Get it from <a href="https://merchant-test.maksekeskus.ee/api.html" target="_blank">Merchant Portal Test</a>','wc_makecommerce_domain'), | |
| 2264 | + ), | |
| 2265 | + array( | |
| 2266 | + 'id' => 'mk_test_private_key', | |
| 2267 | + 'type' => 'text', | |
| 2268 | + 'title' => __('Secret key (test)', 'wc_makecommerce_domain'), | |
| 2269 | + 'class' => 'input-text regular-input', | |
| 2270 | + ), | |
| 2271 | + array( | |
| 2272 | + 'id' => 'mk_test_public_key', | |
| 2273 | + 'type' => 'text', | |
| 2274 | + 'title' => __('Publishable key (test)', 'wc_makecommerce_domain'), | |
| 2275 | + 'class' => 'input-text regular-input', | |
| 2276 | + ), | |
| 2277 | + array( | |
| 2278 | + 'id' => 'mk_javascript_ui', | |
| 2279 | + 'type' => 'api_javascript_ui', | |
| 2280 | + ), | |
| 2281 | + array('type' => 'sectionend', 'id' => 'mk_api_settings') | |
| 2282 | + ); | |
| 2283 | + } | |
| 2284 | + | |
| 2285 | + function mk_admin_save_settings() { | |
| 2286 | + // reload banklinks | |
| 2287 | + global $current_section; | |
| 2288 | + if ($current_section !== 'mk_api') { | |
| 2289 | + return; | |
| 2290 | + } | |
| 2291 | + update_mc_banklinks(); | |
| 2292 | + } | |
| 2293 | + | |
| 2294 | + function __get_logo_html() { | |
| 2295 | + return '<div class="makecommerce-info">'. | |
| 2296 | + '<div class="makecommerce-logo">'. | |
| 2297 | + '<a target="_blank" href="http://maksekeskus.ee"><img src="'. plugins_url('/images/makecommerce_logo_en.svg', __FILE__) .'" class="makecommerce-logo"></a>'. | |
| 2298 | + '</div>'. | |
| 2299 | + '<div class="makecommerce-links">'. | |
| 2300 | + '<div class="makecommerce-link"><a target="_blank" href="https://merchant.maksekeskus.ee">Merchant Portal</a></div>'. | |
| 2301 | + '<div class="makecommerce-link"><a target="_blank" href="https://makecommerce.net/">makecommerce.net</a></div>'. | |
| 2302 | + '<div class="makecommerce-link"><a target="_blank" href="http://maksekeskus.ee">maksekeskus.ee</a></div>'. | |
| 2303 | + '</div>'. | |
| 2304 | + '</div>'; | |
| 2305 | + } | |
| 2306 | + | |
| 2307 | + add_action('woocommerce_get_sections_api', 'mk_admin_add_settings'); | |
| 2308 | + add_filter('woocommerce_get_settings_api', 'mk_admin_all_settings', 10, 2); | |
| 2309 | + add_action('woocommerce_settings_saved', 'mk_admin_save_settings', 30, 0); | |
| 2310 | + add_action('woocommerce_admin_field_api_javascript_ui', 'api_javascript_ui'); | |
| 2311 | + function api_javascript_ui( $data ) { | |
| 2312 | + ?> | |
| 2313 | + <script type="text/javascript"> | |
| 2314 | + jQuery(document).ready(function($) { | |
| 2315 | + var env_select = $('#mk_api_type'); | |
| 2316 | + var test_fields = ['mk_test_shop_id', 'mk_test_private_key', 'mk_test_public_key']; | |
| 2317 | + var live_fields = ['mk_shop_id', 'mk_private_key', 'mk_public_key']; | |
| 2318 | + | |
| 2319 | + env_select.on('change', function(){ | |
| 2320 | + hideFileds($(this).val()); | |
| 2321 | + }); | |
| 2322 | + hideFileds(env_select.val()); | |
| 2323 | + function hideFileds(type) { | |
| 2324 | + var hide = type == 'live' ? test_fields : live_fields; | |
| 2325 | + var show = type == 'live' ? live_fields : test_fields; | |
| 2326 | + $.each(hide, function(i, elem) { | |
| 2327 | + $('#'+elem).closest('tr').hide(); | |
| 2328 | + }); | |
| 2329 | + $.each(show, function(i, elem) { | |
| 2330 | + $('#'+elem).closest('tr').show(); | |
| 2331 | + }); | |
| 2332 | + } | |
| 2333 | + }); | |
| 2334 | + </script> | |
| 2335 | + <?php | |
| 2336 | + } | |
| 2337 | + | |
| 2338 | + function mk_admin_restrict_manage_posts() { | |
| 2339 | + global $typenow; | |
| 2340 | + if ( in_array( $typenow, wc_get_order_types( 'order-meta-boxes' ) ) ) { | |
| 2341 | + $selected_method = !empty($_REQUEST['_shipping_method']) ? $_REQUEST['_shipping_method'] : false; | |
| 2342 | + $methods = WC()->shipping->load_shipping_methods(); | |
| 2343 | + echo '<select name="_shipping_method" id="shipping_type" class="enhanced">'; | |
| 2344 | + echo '<option value="">'.__('-- filter by shipping method', 'wc_makecommerce_domain') . '</option>'; | |
| 2345 | + foreach ($methods as $method) { | |
| 2346 | + echo '<option value="'.$method->id.'"'.($selected_method === $method->id ? ' selected="selected"' : '').'>'.$method->title.'</option>'; | |
| 2347 | + } | |
| 2348 | + echo '</select>'; | |
| 2349 | + } | |
| 2350 | + } | |
| 2351 | + function mk_admin_shipping_filter($where, &$wp_query) { | |
| 2352 | + global $pagenow, $wpdb; | |
| 2353 | + $method = !empty($_REQUEST['_shipping_method']) ? $_REQUEST['_shipping_method'] : false; | |
| 2354 | + if (is_admin() && $pagenow=='edit.php' && $wp_query->query_vars['post_type'] == 'shop_order' && !empty($method) ) { | |
| 2355 | + $where .= $GLOBALS['wpdb']->prepare( ' AND ID | |
| 2356 | + IN ( | |
| 2357 | + SELECT items.order_id | |
| 2358 | + FROM '.$wpdb->prefix.'woocommerce_order_itemmeta meta, '.$wpdb->prefix.'woocommerce_order_items items | |
| 2359 | + WHERE meta.order_item_id = items.order_item_id | |
| 2360 | + AND meta.meta_key = "method_id" | |
| 2361 | + AND meta.meta_value = %s | |
| 2362 | + ) ', $method ); | |
| 2363 | + } | |
| 2364 | + return $where; | |
| 2365 | + } | |
| 2366 | + add_filter('restrict_manage_posts', 'mk_admin_restrict_manage_posts'); | |
| 2367 | + add_filter('posts_where', 'mk_admin_shipping_filter', 10, 2); | |
| 2368 | + | |
| 2369 | + function mk_admin_bulk_actions() { | |
| 2370 | + wp_enqueue_script('wc_mk_timepicker', plugins_url('/scripts/jquery-ui.timepicker.js', __FILE__)); | |
| 2371 | + wp_enqueue_style('wc_mk_timepicker-css', plugins_url('/css/jquery-ui.timepicker.css', __FILE__)); | |
| 2372 | + global $post_type; | |
| 2373 | + if ('shop_order' === $post_type) { ?> | |
| 2374 | + <script type="text/javascript"> | |
| 2375 | + jQuery(function() { | |
| 2376 | + jQuery('<option>').val('parcel_machine_labels').text('<?php _e( 'Register parcel machine shipments', 'wc_makecommerce_domain' )?>').appendTo('select[name="action"]'); | |
| 2377 | + jQuery('<option>').val('parcel_machine_labels').text('<?php _e( 'Register parcel machine shipments', 'wc_makecommerce_domain' )?>').appendTo('select[name="action2"]'); | |
| 2378 | + jQuery('<option>').val('parcel_machine_print_labels').text('<?php _e( 'Print parcel machine labels', 'wc_makecommerce_domain' )?>').appendTo('select[name="action"]'); | |
| 2379 | + jQuery('<option>').val('parcel_machine_print_labels').text('<?php _e( 'Print parcel machine labels', 'wc_makecommerce_domain' )?>').appendTo('select[name="action2"]'); | |
| 2380 | + //jQuery('<option>').val('parcel_machine_order_courier').text('<?php _e( 'Order courier for parcel machine deliveries', 'wc_makecommerce_domain' )?>').appendTo('select[name="action"]'); | |
| 2381 | + //jQuery('<option>').val('parcel_machine_order_courier').text('<?php _e( 'Order courier for parcel machine deliveries', 'wc_makecommerce_domain' )?>').appendTo('select[name="action2"]'); | |
| 2382 | + jQuery('select[name="action"]').after('<input type="text" name="transport_time" class="hidden timepicker"/>'); | |
| 2383 | + jQuery('select[name="action"], select[name="action2"]').on('change', function(){ | |
| 2384 | + var val = jQuery(this).val(); | |
| 2385 | + if (val === 'parcel_machine_order_courier') { | |
| 2386 | + jQuery('.timepicker').removeClass('hidden'); | |
| 2387 | + } else { | |
| 2388 | + jQuery('.timepicker').addClass('hidden'); | |
| 2389 | + } | |
| 2390 | + }); | |
| 2391 | + jQuery('.tablenav.bottom .actions.bulkactions').hide(); | |
| 2392 | + var now = new Date(); | |
| 2393 | + var mins = now.getMinutes(); | |
| 2394 | + var quarterHours = Math.ceil(mins/15); | |
| 2395 | + if (quarterHours == 4) { now.setHours(now.getHours()+1); } | |
| 2396 | + var rounded = (quarterHours*15)%60; | |
| 2397 | + now.setMinutes(rounded); | |
| 2398 | + jQuery('.timepicker').datetimepicker({dateFormat: 'dd/mm/yy', timeFormat: 'HH:mm', stepMinute: 15, minDateTime: now, devaultValue: jQuery.datepicker.formatTime('dd/mm/yyyy HH:mm', now)}); | |
| 2399 | + }); | |
| 2400 | + </script> | |
| 2401 | + <?php if (!empty($_REQUEST['mk_pdf'])) { ?> | |
| 2402 | + <script type="text/javascript"> | |
| 2403 | + jQuery(function(){ | |
| 2404 | + window.open('<?php echo $_REQUEST['mk_pdf'] ?>', 'pdf'); | |
| 2405 | + }); | |
| 2406 | + </script> | |
| 2407 | + <?php } | |
| 2408 | + } | |
| 2409 | + } | |
| 2410 | + function mk_admin_bulk_action_labels() { | |
| 2411 | + $wp_list_table = _get_list_table('WP_Posts_List_Table'); | |
| 2412 | + $post_ids = array_map('absint', (array)$_REQUEST['post']); | |
| 2413 | + mk_get_shipment_ids($post_ids); | |
| 2414 | + } | |
| 2415 | + function mk_admin_bulk_action_print() { | |
| 2416 | + $wp_list_table = _get_list_table('WP_Posts_List_Table'); | |
| 2417 | + $post_ids = array_map('absint', (array)$_REQUEST['post']); | |
| 2418 | + mk_get_labels($post_ids); | |
| 2419 | + } | |
| 2420 | + function mk_admin_bulk_action_order_courier() { | |
| 2421 | + return; | |
| 2422 | + $wp_list_table = _get_list_table('WP_Posts_List_Table'); | |
| 2423 | + $post_ids = array_map('absint', (array)$_REQUEST['post']); | |
| 2424 | + $transport_time = !empty($_REQUEST['transport_time']) ? strtotime(str_replace('/', '-', $_REQUEST['transport_time'])) : false; | |
| 2425 | + if ($transport_time && $transport_time > time()) { | |
| 2426 | + mk_get_shipment_ids($post_ids, 'omniva'); | |
| 2427 | + } else { | |
| 2428 | + die('Please select transport time that is in the future'); | |
| 2429 | + } | |
| 2430 | + } | |
| 2431 | + add_action('admin_footer', 'mk_admin_bulk_actions', 11); | |
| 2432 | + add_action('admin_action_parcel_machine_labels', 'mk_admin_bulk_action_labels'); | |
| 2433 | + add_action('admin_action_parcel_machine_print_labels', 'mk_admin_bulk_action_print'); | |
| 2434 | + add_action('admin_action_parcel_machine_order_courier', 'mk_admin_bulk_action_order_courier'); | |
| 2435 | + | |
| 2436 | + | |
| 2437 | + | |
| 2438 | + function mk_admin_parcelmachine_order_meta($order) { | |
| 2439 | + $machine_id = get_post_meta($order->id, '_parcel_machine', true); | |
| 2440 | + if (!empty($machine_id)) { | |
| 2441 | + list($carrier, $machine) = explode('||', $machine_id); | |
| 2442 | + if (!empty($machine)) { | |
| 2443 | + $machine = mk_get_machine($carrier, (int)$machine); | |
| 2444 | + if ($machine) { | |
| 2445 | + echo '<p><strong>'.__('Parcel machine').':</strong><br/>' . $machine['name'] . '<br/><small>' . $machine['address'] . '</small></p>'; | |
| 2446 | + } | |
| 2447 | + } | |
| 2448 | + } | |
| 2449 | + $shipment_id = get_post_meta($order->id, '_parcel_machine_shipment_id', true); | |
| 2450 | + $shipment_id_error = get_post_meta($order->id, '_parcel_machine_error', true); | |
| 2451 | + $shipment_manifest = get_post_meta($order->id, '_parcel_machine_manifest', true); | |
| 2452 | + if ($shipment_id) { | |
| 2453 | + echo '<p><strong>'.__('Shipment tracking code').':</strong><br/>' . $shipment_id . '</small></p>'; | |
| 2454 | + } | |
| 2455 | + if ($shipment_manifest) { | |
| 2456 | + echo '<p><strong>'.__('Parcel machine pickup manifest').':</strong><br/><a href="' . $shipment_manifest . '" target="_blank">link</a></small></p>'; | |
| 2457 | + } | |
| 2458 | + if ($shipment_id_error) { | |
| 2459 | + echo '<p><strong style="color: red;">'.__('Parcel machine shipment generation error').':</strong><br/>' . $shipment_id_error . '</small></p>'; | |
| 2460 | + } | |
| 2461 | + } | |
| 2462 | + | |
| 2463 | + function mk_admin_render_shop_order_columns( $column ) { | |
| 2464 | + global $post, $woocommerce, $the_order; | |
| 2465 | + if (empty($the_order) || $the_order->id != $post->ID) { | |
| 2466 | + $the_order = wc_get_order($post->ID); | |
| 2467 | + } | |
| 2468 | + if ($column === 'shipping_address') { | |
| 2469 | + $machine = get_post_meta($the_order->id, '_parcel_machine', true); | |
| 2470 | + $shipment_id = get_post_meta($the_order->id, '_parcel_machine_shipment_id', true); | |
| 2471 | + $shipment_id_error = get_post_meta($the_order->id, '_parcel_machine_error', true); | |
| 2472 | + if ($machine && !$shipment_id) { | |
| 2473 | + //echo __('Shipment ID not generated for delivery', 'wc_makecommerce_domain'); | |
| 2474 | + } | |
| 2475 | + if ($shipment_id) echo __('Shipment tracking code', 'wc_makecommerce_domain') . ': ' . $shipment_id; | |
| 2476 | + else if ($shipment_id_error) echo '<span style="color: red;">'.__('Package shipment generation error:', 'wc_makecommerce_domain') . '</span><br/>' .$shipment_id_error; | |
| 2477 | + } | |
| 2478 | + } | |
| 2479 | + function mk_add_email_customer_details_fields($fields, $sent_to_admin, $order) { | |
| 2480 | + $machine_id = get_post_meta($order->id, '_parcel_machine', true); | |
| 2481 | + if (empty($machine_id)) return $fields; | |
| 2482 | + list($carrier, $machine) = explode('||', $machine_id); | |
| 2483 | + if (empty($machine)) return $fields; | |
| 2484 | + $machine = mk_get_machine($carrier, (int)$machine); | |
| 2485 | + if (!$machine) return $fields; | |
| 2486 | + $fields[] = array('label' => __('Parcel machine'), 'value' => $machine['name'].' - '.$machine['address']); | |
| 2487 | + return $fields; | |
| 2488 | + } | |
| 2489 | + function mk_add_order_customer_details_fields($order) { | |
| 2490 | + $machine_id = get_post_meta($order->id, '_parcel_machine', true); | |
| 2491 | + if (empty($machine_id)) return; | |
| 2492 | + list($carrier, $machine) = explode('||', $machine_id); | |
| 2493 | + if (empty($machine)) return; | |
| 2494 | + $machine = mk_get_machine($carrier, (int)$machine); | |
| 2495 | + if (!$machine) return; | |
| 2496 | + echo '<tr>'; | |
| 2497 | + echo '<th>'.__('Parcel machine').'</th>'; | |
| 2498 | + echo '<td>'.$machine['name'].'<br/>'.$machine['address'].'</td>'; | |
| 2499 | + echo '</tr>'; | |
| 2500 | + } | |
| 2501 | + function mk_admin_product_option_fields($fields) { | |
| 2502 | + echo '<div class="options_group">'; | |
| 2503 | + woocommerce_wp_checkbox( | |
| 2504 | + array( | |
| 2505 | + 'id' => '_no_parcel_machine', | |
| 2506 | + 'wrapper_class' => 'show_if_simple', | |
| 2507 | + 'label' => __('Does not fit parcel machine', 'wc_makecommerce_domain'), | |
| 2508 | + 'description' => __('When this is checked, parcel machine shipping option is not available for a cart with this product', 'wc_makecommerce_domain') | |
| 2509 | + ) | |
| 2510 | + ); | |
| 2511 | + woocommerce_wp_checkbox( | |
| 2512 | + array( | |
| 2513 | + 'id' => '_no_shipping_cost', | |
| 2514 | + 'wrapper_class' => 'show_if_simple', | |
| 2515 | + 'label' => __('Free parcel machine', 'wc_makecommerce_domain'), | |
| 2516 | + 'description' => __('When this is checked, parcel machine if free for this product', 'wc_makecommerce_domain') | |
| 2517 | + ) | |
| 2518 | + ); | |
| 2519 | + echo '</div>'; | |
| 2520 | + } | |
| 2521 | + function mk_admin_product_fields_save($post_id) { | |
| 2522 | + $no_parcel_machine = isset($_POST['_no_parcel_machine']) ? 'yes' : 'no'; | |
| 2523 | + update_post_meta($post_id, '_no_parcel_machine', $no_parcel_machine); | |
| 2524 | + $no_shipping_cost = isset($_POST['_no_shipping_cost']) ? 'yes' : 'no'; | |
| 2525 | + update_post_meta($post_id, '_no_shipping_cost', $no_shipping_cost); | |
| 2526 | + } | |
| 2527 | + | |
| 2528 | + add_action('woocommerce_product_options_shipping', 'mk_admin_product_option_fields'); | |
| 2529 | + add_action('woocommerce_process_product_meta', 'mk_admin_product_fields_save'); | |
| 2530 | + add_action('woocommerce_admin_order_data_after_shipping_address', 'mk_admin_parcelmachine_order_meta', 10, 1); | |
| 2531 | + add_action('manage_shop_order_posts_custom_column', 'mk_admin_render_shop_order_columns', 3); | |
| 2532 | + add_filter('woocommerce_email_customer_details_fields', 'mk_add_email_customer_details_fields', 10, 3 ); | |
| 2533 | + add_action('woocommerce_order_details_after_customer_details', 'mk_add_order_customer_details_fields'); | |
| 2534 | + add_action('woocommerce_order_status_processing', 'mk_get_shipment_ids'); | |
| 2535 | + | |
| 2536 | + | |
| 2537 | + | |
| 2538 | + function mk_get_shipment_ids($post_ids, $select_carrier = null) { | |
| 2539 | + if (!is_array($post_ids)) { | |
| 2540 | + $post_ids = array($post_ids); | |
| 2541 | + } | |
| 2542 | + transport_add_method(); | |
| 2543 | + $shipping_request = array('credentials' => array(), 'orders' => array()); | |
| 2544 | + $shipping_classes_map = array( | |
| 2545 | + 'parcelmachine_omniva' => 'WC_ParcelMachine_Shipping_Method_Omniva', | |
| 2546 | + 'parcelmachine_smartpost' => 'WC_ParcelMachine_Shipping_Method_Smartpost', | |
| 2547 | + 'courier_omniva' => 'WC_Courier_Shipping_Method_Omniva' | |
| 2548 | + ); | |
| 2549 | + foreach ($post_ids as $post_id) { | |
| 2550 | + $order = new WC_Order($post_id); | |
| 2551 | + $shipping_methods = $order->get_shipping_methods(); | |
| 2552 | + if (empty($shipping_methods)) { continue; } | |
| 2553 | + foreach ($shipping_methods as $shipping_method) { | |
| 2554 | + $shipping_id = explode(':', $shipping_method['method_id']); | |
| 2555 | + $shipping_class = $shipping_id[0]; | |
| 2556 | + $shipping_instance = !empty($shipping_id[1]) ? $shipping_id[1] : null; | |
| 2557 | + if (empty($shipping_classes_map[$shipping_class])) { | |
| 2558 | + continue; | |
| 2559 | + } | |
| 2560 | + | |
| 2561 | + $transport_class = new $shipping_classes_map[$shipping_class]($shipping_instance); | |
| 2562 | + $carrier_uc = mb_strtoupper($transport_class->carrier); | |
| 2563 | + | |
| 2564 | + if ($transport_class->type === 'apt') { | |
| 2565 | + $parcel_machine = get_post_meta($post_id, '_parcel_machine', true); | |
| 2566 | + if (!$parcel_machine) { continue; } | |
| 2567 | + list($carrier, $machine_id) = explode('||', $parcel_machine); | |
| 2568 | + if (!$carrier || !$machine_id) { continue; } | |
| 2569 | + } | |
| 2570 | + | |
| 2571 | + if (empty($shipping_request['credentials'][$carrier_uc])) { | |
| 2572 | + $api_user = $transport_class->settings['service_user']; | |
| 2573 | + $api_password = $transport_class->settings['service_password']; | |
| 2574 | + if (!$api_user || !$api_password) { | |
| 2575 | + add_action('admin_notices', 'mk_admin_error'); | |
| 2576 | + continue; | |
| 2577 | + } | |
| 2578 | + $shipping_request['credentials'][$carrier_uc] = array('carrier' => $carrier_uc, 'username' => $api_user, 'password' => $api_password); | |
| 2579 | + } | |
| 2580 | + $sender = array( | |
| 2581 | + 'name' => !empty($transport_class->settings['shop_name']) ? $transport_class->settings['shop_name'] : '', | |
| 2582 | + 'phone' => !empty($transport_class->settings['shop_phone']) ? $transport_class->settings['shop_phone'] : '', | |
| 2583 | + 'email' => !empty($transport_class->settings['shop_email']) ? $transport_class->settings['shop_email'] : '', | |
| 2584 | + 'country' => !empty($transport_class->settings['shop_address_country']) ? $transport_class->settings['shop_address_country'] : '', | |
| 2585 | + 'city' => !empty($transport_class->settings['shop_address_city']) ? $transport_class->settings['shop_address_city'] : '', | |
| 2586 | + 'street' => !empty($transport_class->settings['shop_address_street']) ? $transport_class->settings['shop_address_street'] : '', | |
| 2587 | + 'postalCode' => !empty($transport_class->settings['shop_postal_code']) ? $transport_class->settings['shop_postal_code'] : '' | |
| 2588 | + ); | |
| 2589 | + $sr_order = array( | |
| 2590 | + 'carrier' => $carrier_uc, | |
| 2591 | + 'orderId' => $order->id, | |
| 2592 | + 'recipient' => array('name' => $order->shipping_first_name . ' ' . $order->shipping_last_name, 'phone' => $order->billing_phone, 'email' => $order->billing_email), | |
| 2593 | + 'sender' => $sender, | |
| 2594 | + 'destination' => array(), | |
| 2595 | + ); | |
| 2596 | + $m_service_type = null; | |
| 2597 | + if ($transport_class->carrier === 'omniva' && $transport_class->type === 'atp') { | |
| 2598 | + $m_service_type = 'PA'; | |
| 2599 | + } else if (!empty($transport_class->settings['registerOnPaymentCode'])) { | |
| 2600 | + $m_service_type = $transport_class->settings['registerOnPaymentCode']; | |
| 2601 | + } | |
| 2602 | + | |
| 2603 | + if (!empty($machine_id)) { | |
| 2604 | + $sr_order['destination']['destinationId'] = $machine_id; | |
| 2605 | + } | |
| 2606 | + if ($transport_class->carrier === 'omniva' && $transport_class->type === 'courier') { | |
| 2607 | + $sr_order['destination'] = array( | |
| 2608 | + 'postalCode' => $order->shipping_postcode, | |
| 2609 | + 'country' => $order->shipping_country, | |
| 2610 | + 'county' => $order->shipping_state, | |
| 2611 | + 'city' => $order->shipping_city, | |
| 2612 | + 'street' => $order->shipping_address_1 . ' ' . $order->shipping_address_2 | |
| 2613 | + ); | |
| 2614 | + } | |
| 2615 | + | |
| 2616 | + if ($m_service_type) { | |
| 2617 | + $sr_order['services'] = array('serviceType' => $m_service_type); | |
| 2618 | + } | |
| 2619 | + | |
| 2620 | + $shipping_request['orders'][] = $sr_order; | |
| 2621 | + } | |
| 2622 | + } | |
| 2623 | + $shipping_request['credentials'] = array_values($shipping_request['credentials']); | |
| 2624 | + if (empty($shipping_request['orders'])) { | |
| 2625 | + return; | |
| 2626 | + } | |
| 2627 | + $MK = mk_get_api(); | |
| 2628 | + if (!$MK) { | |
| 2629 | + return; | |
| 2630 | + } | |
| 2631 | + try { | |
| 2632 | + $response = $MK->createShipments($shipping_request); | |
| 2633 | + } catch (Exception $e) { | |
| 2634 | + echo $e->getMessage(); | |
| 2635 | + exit(); | |
| 2636 | + } | |
| 2637 | + $shipments = !empty($response->shipments) ? $response->shipments : $response; | |
| 2638 | + $manifest = !empty($response->manifests) ? $response->manifests[0] : false; | |
| 2639 | + | |
| 2640 | + foreach ($shipments as $order) { | |
| 2641 | + if ($manifest) { | |
| 2642 | + update_post_meta((int)$order->orderId, '_parcel_machine_manifest', sanitize_text_field($manifest)); | |
| 2643 | + } | |
| 2644 | + if (!empty($order->orderId) && !empty($order->shipmentId)) { | |
| 2645 | + update_post_meta((int)$order->orderId, '_parcel_machine_shipment_id', sanitize_text_field($order->shipmentId)); | |
| 2646 | + delete_post_meta((int)$order->orderId, '_parcel_machine_error'); | |
| 2647 | + } else if (!empty($order->orderId) && !empty($order->barCode)) { | |
| 2648 | + update_post_meta((int)$order->orderId, '_parcel_machine_shipment_id', sanitize_text_field($order->barCode)); | |
| 2649 | + delete_post_meta((int)$order->orderId, '_parcel_machine_error'); | |
| 2650 | + } else if (!empty($order->orderId) && !empty($order->errorMessage)) { | |
| 2651 | + update_post_meta((int)$order->orderId, '_parcel_machine_error', sanitize_text_field($order->errorMessage)); | |
| 2652 | + } | |
| 2653 | + } | |
| 2654 | + } | |
| 2655 | + | |
| 2656 | + function mk_get_labels($post_ids, $ajax = false) { | |
| 2657 | + if (!is_array($post_ids)) { | |
| 2658 | + $post_ids = array($post_ids); | |
| 2659 | + } | |
| 2660 | + transport_add_method(); | |
| 2661 | + $shipping_request = array('credentials' => array(), 'orders' => array(), 'printFormat' => 'A4'); | |
| 2662 | + $shipping_classes_map = array( | |
| 2663 | + 'parcelmachine_omniva' => 'WC_ParcelMachine_Shipping_Method_Omniva', | |
| 2664 | + 'parcelmachine_smartpost' => 'WC_ParcelMachine_Shipping_Method_Smartpost', | |
| 2665 | + 'courier_omniva' => 'WC_Courier_Shipping_Method_Omniva' | |
| 2666 | + ); | |
| 2667 | + foreach ($post_ids as $post_id) { | |
| 2668 | + $shipment_id = get_post_meta($post_id, '_parcel_machine_shipment_id', true); | |
| 2669 | + if (!$shipment_id) { continue; } | |
| 2670 | + $order = new WC_Order($post_id); | |
| 2671 | + $shipping_methods = $order->get_shipping_methods(); | |
| 2672 | + if (empty($shipping_methods)) { continue; } | |
| 2673 | + foreach ($shipping_methods as $shipping_method) { | |
| 2674 | + $shipping_id = explode(':', $shipping_method['method_id']); | |
| 2675 | + $shipping_class = $shipping_id[0]; | |
| 2676 | + $shipping_instance = !empty($shipping_id[1]) ? $shipping_id[1] : null; | |
| 2677 | + if (empty($shipping_classes_map[$shipping_class])) { | |
| 2678 | + continue; | |
| 2679 | + } | |
| 2680 | + | |
| 2681 | + $transport_class = new $shipping_classes_map[$shipping_class]($shipping_instance); | |
| 2682 | + $carrier_uc = mb_strtoupper($transport_class->carrier); | |
| 2683 | + | |
| 2684 | + if ($transport_class->type === 'apt') { | |
| 2685 | + $parcel_machine = get_post_meta($post_id, '_parcel_machine', true); | |
| 2686 | + if (!$parcel_machine) { continue; } | |
| 2687 | + list($carrier, $machine_id) = explode('||', $parcel_machine); | |
| 2688 | + if (!$carrier || !$machine_id) { continue; } | |
| 2689 | + } | |
| 2690 | + if (empty($shipping_request['credentials'][$carrier_uc])) { | |
| 2691 | + $api_user = $transport_class->settings['service_user']; | |
| 2692 | + $api_password = $transport_class->settings['service_password']; | |
| 2693 | + if (!$api_user || !$api_password) { | |
| 2694 | + add_action('admin_notices', 'mk_admin_error'); | |
| 2695 | + continue; | |
| 2696 | + } | |
| 2697 | + $shipping_request['credentials'][$carrier_uc] = array('carrier' => $carrier_uc, 'username' => $api_user, 'password' => $api_password); | |
| 2698 | + } | |
| 2699 | + $sender = array( | |
| 2700 | + 'name' => $transport_class->settings['shop_name'], | |
| 2701 | + 'phone' => $transport_class->settings['shop_phone'], | |
| 2702 | + 'email' => $transport_class->settings['shop_email'], | |
| 2703 | + 'country' => $transport_class->settings['shop_address_country'], | |
| 2704 | + 'city' => $transport_class->settings['shop_address_city'], | |
| 2705 | + 'street' => $transport_class->settings['shop_address_street'], | |
| 2706 | + 'postalCode' => $transport_class->settings['shop_postal_code'] | |
| 2707 | + ); | |
| 2708 | + $sr_order = array( | |
| 2709 | + 'carrier' => $carrier_uc, | |
| 2710 | + 'orderId' => $order->id, | |
| 2711 | + 'recipient' => array('name' => $order->shipping_first_name . ' ' . $order->shipping_last_name, 'phone' => $order->billing_phone, 'email' => $order->billing_email), | |
| 2712 | + 'sender' => $sender, | |
| 2713 | + 'shipmentId' => $shipment_id | |
| 2714 | + ); | |
| 2715 | + $m_service_type = null; | |
| 2716 | + if ($transport_class->carrier === 'omniva' && $transport_class->type === 'atp') { | |
| 2717 | + $m_service_type = 'PA'; | |
| 2718 | + } else if (!empty($transport_class->settings['registerOnPaymentCode'])) { | |
| 2719 | + $m_service_type = $transport_class->settings['registerOnPaymentCode']; | |
| 2720 | + } | |
| 2721 | + | |
| 2722 | + if (!empty($machine_id)) { | |
| 2723 | + $sr_order['destination']['destinationId'] = $machine_id; | |
| 2724 | + } | |
| 2725 | + if ($transport_class->carrier === 'omniva' && $transport_class->type === 'courier') { | |
| 2726 | + $sr_order['destination'] = array( | |
| 2727 | + 'postalCode' => $order->shipping_postcode, | |
| 2728 | + 'country' => $order->shipping_country, | |
| 2729 | + 'county' => $order->shipping_state, | |
| 2730 | + 'city' => $order->shipping_city, | |
| 2731 | + 'street' => $order->shipping_address_1 . ' ' . $order->shipping_address_2 | |
| 2732 | + ); | |
| 2733 | + } | |
| 2734 | + | |
| 2735 | + if ($m_service_type) { | |
| 2736 | + $sr_order['services'] = array('serviceType' => $m_service_type); | |
| 2737 | + } | |
| 2738 | + | |
| 2739 | + $shipping_request['orders'][] = $sr_order; | |
| 2740 | + } | |
| 2741 | + } | |
| 2742 | + $shipping_request['credentials'] = array_values($shipping_request['credentials']); | |
| 2743 | + if (empty($shipping_request['orders'])) { | |
| 2744 | + return; | |
| 2745 | + } | |
| 2746 | + $MK = mk_get_api(); | |
| 2747 | + if (!$MK) { | |
| 2748 | + return; | |
| 2749 | + } | |
| 2750 | + try { | |
| 2751 | + $response = $MK->createLabels($shipping_request); | |
| 2752 | + } catch (Exception $e) { | |
| 2753 | + echo $e->getMessage(); | |
| 2754 | + exit(); | |
| 2755 | + } | |
| 2756 | + if (empty($response->labelUrl)) { | |
| 2757 | + return; | |
| 2758 | + } | |
| 2759 | + if ($ajax) { | |
| 2760 | + return $response->labelUrl; | |
| 2761 | + } | |
| 2762 | + $sendback = add_query_arg(array('post_type' => 'shop_order', 'mk_pdf' => urlencode($response->labelUrl)), ''); | |
| 2763 | + wp_redirect(esc_url_raw($sendback)); | |
| 2764 | + exit(); | |
| 2765 | + } | |
| 2766 | + | |
| 2767 | + $MKAPI = false; | |
| 2768 | + function mk_get_api() { | |
| 2769 | + global $MKAPI; | |
| 2770 | + if ($MKAPI) { | |
| 2771 | + //return $MKAPI; | |
| 2772 | + } | |
| 2773 | + $mk_api_type = get_option('mk_api_type', false); | |
| 2774 | + if (!$mk_api_type) { | |
| 2775 | + return false; | |
| 2776 | + } | |
| 2777 | + if ($mk_api_type !== 'live') { | |
| 2778 | + $key_prefix = $mk_api_type.'_'; | |
| 2779 | + } else { | |
| 2780 | + $key_prefix = ''; | |
| 2781 | + } | |
| 2782 | + $mk_shop_id = get_option('mk_'.$key_prefix.'shop_id', ''); | |
| 2783 | + $mk_public_key = get_option('mk_'.$key_prefix.'public_key', ''); | |
| 2784 | + $mk_private_key = get_option('mk_'.$key_prefix.'private_key', ''); | |
| 2785 | + if (!$mk_shop_id || !$mk_public_key || !$mk_shop_id) { | |
| 2786 | + return false; | |
| 2787 | + } | |
| 2788 | + $MKAPI = new Maksekeskus($mk_shop_id, $mk_public_key, $mk_private_key, $mk_api_type === 'live' ? false : true); | |
| 2789 | + return $MKAPI; | |
| 2790 | + } | |
| 2791 | + | |
| 2792 | + function mk_is_api_set() { | |
| 2793 | + $mk_api_type = get_option('mk_api_type', false); | |
| 2794 | + if (!$mk_api_type) { | |
| 2795 | + return false; | |
| 2796 | + } | |
| 2797 | + if ($mk_api_type !== 'live') { | |
| 2798 | + $key_prefix = $mk_api_type.'_'; | |
| 2799 | + } else { | |
| 2800 | + $key_prefix = ''; | |
| 2801 | + } | |
| 2802 | + $mk_shop_id = get_option('mk_'.$key_prefix.'shop_id', ''); | |
| 2803 | + $mk_public_key = get_option('mk_'.$key_prefix.'public_key', ''); | |
| 2804 | + $mk_private_key = get_option('mk_'.$key_prefix.'private_key', ''); | |
| 2805 | + if (!$mk_shop_id || !$mk_public_key || !$mk_shop_id) { | |
| 2806 | + return false; | |
| 2807 | + } | |
| 2808 | + return true; | |
| 2809 | + } | |
| 2810 | + | |
| 2811 | + function mk_get_machines($carrier, $country = null) { | |
| 2812 | + $data = get_option('mk_machines_cache', false); | |
| 2813 | + $data_expires = get_option('mk_machines_expires', false); | |
| 2814 | + if (!$data || $data_expires < time()) { | |
| 2815 | + $MK = mk_get_api(); | |
| 2816 | + if(!$MK) { | |
| 2817 | + return array(); | |
| 2818 | + } | |
| 2819 | + $data = $MK->getDestinations(array('type' => 'APT')); | |
| 2820 | + update_option('mk_machines_cache', $data); | |
| 2821 | + update_option('mk_machines_expires', time()+3*60*60); | |
| 2822 | + } | |
| 2823 | + | |
| 2824 | + $machines = array(); | |
| 2825 | + if (!$data || empty($data)) { | |
| 2826 | + return array(); | |
| 2827 | + } | |
| 2828 | + foreach ($data as $machine) { | |
| 2829 | + if ((!$country || $machine->country === $country) && $machine->type === 'APT' && ($carrier === '*' || strtolower($carrier) === strtolower($machine->carrier))) { | |
| 2830 | + $machines[] = array( | |
| 2831 | + 'carrier' => strtolower($machine->carrier), | |
| 2832 | + 'id' => $machine->id, | |
| 2833 | + 'name' => $machine->name, | |
| 2834 | + 'city' => $machine->city, | |
| 2835 | + 'address' => !empty($machine->address) ? $machine->address : '', | |
| 2836 | + ); | |
| 2837 | + } | |
| 2838 | + } | |
| 2839 | + usort($machines, function($a, $b){ | |
| 2840 | + if ($a['city'] === $b['city']) { | |
| 2841 | + return $a['name'] > $b['name']; | |
| 2842 | + } | |
| 2843 | + return $a['city'] > $b['city']; | |
| 2844 | + }); | |
| 2845 | + return $machines; | |
| 2846 | + } | |
| 2847 | + | |
| 2848 | + function mk_get_machine($carrier, $id) { | |
| 2849 | + $machines = mk_get_machines($carrier); | |
| 2850 | + foreach ($machines as $machine) { | |
| 2851 | + if ($machine['carrier'] === $carrier && $machine['id'] === $id) { | |
| 2852 | + return $machine; | |
| 2853 | + } | |
| 2854 | + } | |
| 2855 | + return false; | |
| 2856 | + } | |
| 2857 | + | |
| 2858 | + function mk_admin_error() { | |
| 2859 | + 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')); | |
| 2860 | + } | |
| 2861 | + | |
| 2862 | + add_filter('query_vars', 'mk_cron_query_vars'); | |
| 2863 | + add_action('parse_request', 'mk_parse_cron_update_vars'); | |
| 2864 | + | |
| 2865 | + function mk_cron_query_vars($vars) { | |
| 2866 | + $vars[] = 'mk-action'; | |
| 2867 | + $vars[] = 'mk-shop-id'; | |
| 2868 | + $vars[] = 'mk-test-shop-id'; | |
| 2869 | + return $vars; | |
| 2870 | + } | |
| 2871 | + | |
| 2872 | + function mk_parse_cron_update_vars($wp) { | |
| 2873 | + if (array_key_exists('mk-action', $wp->query_vars) && $wp->query_vars['mk-action'] == 'mk-update' | |
| 2874 | + && (array_key_exists('mk-shop-id', $wp->query_vars) || array_key_exists('mk-test-shop-id', $wp->query_vars) ) ) { | |
| 2875 | + if( (strlen($wp->query_vars['mk-shop-id']) && $wp->query_vars['mk-shop-id'] == get_option('mk_shop_id', false) ) | |
| 2876 | + || (strlen($wp->query_vars['mk-test-shop-id']) && $wp->query_vars['mk-test-shop-id'] == get_option('mk_test_shop_id', false) ) ) { | |
| 2877 | + update_mc_banklinks(); | |
| 2878 | + exit(); | |
| 2879 | + } | |
| 2880 | + } | |
| 2881 | + } | |
| 2882 | + | |
| 2883 | + function print_parcel_machine_label_action( $post_id ) { | |
| 2884 | + ?> | |
| 2885 | + <li class="wide"> | |
| 2886 | + <a id="print_parcel_machine_label" href="#" class="button"><?php _e('Print parcel label', 'wc_makecommerce_domain');?></a> | |
| 2887 | + <img class="mc_loading" src="<?php echo site_url('/wp-admin/images/loading.gif');?>"> | |
| 2888 | + <script type="text/javascript"> | |
| 2889 | + jQuery(function($){ | |
| 2890 | + var data = { | |
| 2891 | + 'action': 'print_pml', | |
| 2892 | + 'id': '<?php echo $post_id; ?>' | |
| 2893 | + }; | |
| 2894 | + var loading = $('.mc_loading'); | |
| 2895 | + $('#print_parcel_machine_label').click(function(e){ | |
| 2896 | + e.preventDefault(); | |
| 2897 | + var button = $(this); | |
| 2898 | + button.addClass('disabled'); | |
| 2899 | + loading.show(); | |
| 2900 | + $.post(ajaxurl, data, function(response) { | |
| 2901 | + button.removeClass('disabled'); | |
| 2902 | + loading.hide(); | |
| 2903 | + window.open(response, 'pdf'); | |
| 2904 | + }); | |
| 2905 | + }); | |
| 2906 | + }); | |
| 2907 | + </script> | |
| 2908 | + </li> | |
| 2909 | + <?php | |
| 2910 | + } | |
| 2911 | + add_action('woocommerce_order_actions_end', 'print_parcel_machine_label_action'); | |
| 2912 | + | |
| 2913 | + function ajax_print_pml() { | |
| 2914 | + if(!current_user_can('manage_woocommerce') ) { | |
| 2915 | + die(); | |
| 2916 | + } | |
| 2917 | + die(mk_get_labels(intval($_POST['id']), true)); | |
| 2918 | + } | |
| 2919 | + add_action( 'wp_ajax_print_pml', 'ajax_print_pml' ); | |
| 2920 | + | |
| 2921 | + function admin_login_update($user_login, $user) { | |
| 2922 | + if(user_can( $user, 'administrator' )) { | |
| 2923 | + update_mc_banklinks(); | |
| 2924 | + } | |
| 2925 | + } | |
| 2926 | + add_action('wp_login', 'admin_login_update', 10, 2); | |
| 45 | 2927 | } |
| 46 | - | |