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