| @@ -1,30 +1,41 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | /* |
| 3 | 3 | Plugin Name: MakeCommerce for WooCommerce |
| 4 | 4 | Description: Adds MakeCommerce payment gateway and Itella/Omniva parcel machine shipping methods to Woocommerce checkout |
| 5 | - Version: 2.1.4 | |
| 5 | + Version: 2.6.0 | |
| 6 | 6 | Author: Maksekeskus AS |
| 7 | 7 | Author URI: https://MakeCommerce.net/ |
| 8 | 8 | Text Domain: wc_makecommerce_domain |
| 9 | + WC requires at least: 3.0 | |
| 10 | + WC tested up to: 4.3.1 | |
| 9 | 11 | */ |
| 10 | 12 | |
| 11 | -if ( ! defined( 'ABSPATH' ) ) { | |
| 13 | +if (!defined('ABSPATH')) { | |
| 12 | 14 | exit; // Exit if accessed directly. |
| 13 | 15 | } |
| 14 | 16 | |
| 15 | -if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) { | |
| 17 | +include_once(ABSPATH.'wp-admin/includes/plugin.php'); | |
| 18 | +if (is_plugin_active('woocommerce/woocommerce.php')) { | |
| 16 | 19 | |
| 17 | - | |
| 18 | 20 | define('WC_MC_VERSION', 2.0); |
| 19 | 21 | |
| 20 | - $enable_experimental = get_option('mk_experimental_features', false); | |
| 21 | - if ($enable_experimental && $enable_experimental !== 'no') { | |
| 22 | + $enable_experimental = get_option('mk_checkout_sco', 'no'); | |
| 23 | + if ($enable_experimental === 'yes') { | |
| 22 | 24 | require_once(__DIR__.'/makecommerce-simplecheckout.php'); |
| 23 | 25 | } |
| 24 | 26 | require_once(__DIR__.'/makecommerce-transport.php'); |
| 25 | 27 | require_once(__DIR__.'/makecommerce-payment.php'); |
| 26 | 28 | |
| 29 | + if (is_admin() && !extension_loaded('curl')) { | |
| 30 | + add_action('admin_notices', function(){ | |
| 31 | + $class = 'notice notice-error'; | |
| 32 | + $message = __( 'You have enabled MakeCommerce module but it seems that you don\'t have CURL enabled. This way MakeCommerce unfortunately does not work!', 'wc_makecommerce_domain'); | |
| 33 | + printf( '<div class="%1$s"><p>%2$s</p></div>', esc_attr( $class ), esc_html( $message ) ); | |
| 34 | + }); | |
| 35 | + } | |
| 36 | + | |
| 37 | + | |
| 27 | 38 | global $mkDbTable; |
| 28 | 39 | $mkDbTable = 'mc_banklinks'; |
| 29 | 40 | |
| 30 | 41 | function mc_install() { |
| @@ -40,8 +51,11 @@ | ||
| 40 | 51 | type varchar(10) NOT NULL, |
| 41 | 52 | country char(2) NOT NULL, |
| 42 | 53 | name varchar(25) NOT NULL, |
| 43 | 54 | url varchar(250) NOT NULL, |
| 55 | + logo_url varchar(250), | |
| 56 | + min_amount mediumint(9), | |
| 57 | + max_amount mediumint(9), | |
| 44 | 58 | UNIQUE KEY id (id) |
| 45 | 59 | ) $charset;"; |
| 46 | 60 | |
| 47 | 61 | require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); |
| @@ -64,9 +78,9 @@ | ||
| 64 | 78 | // Assign schedule if not set, update compatible no need to reactivate, format('U') php 5.2 compatible |
| 65 | 79 | // Assigns task at 23.59.59 by local time, task runs if time has passed and somebody navigates the site |
| 66 | 80 | if (! wp_next_scheduled ( 'mc_banklinks_update_cron' )) { |
| 67 | 81 | $date = new DateTime(); |
| 68 | - $date->setTime(04,10,00); | |
| 82 | + $date->setTime(rand(0,23),rand(0,59),00); | |
| 69 | 83 | $timestamp = intval($date->format('U')); |
| 70 | 84 | //$local = 3600 * intval(get_option('gmt_offset')); |
| 71 | 85 | //wp_schedule_event($timestamp - $local, 'twicedaily', 'mc_banklinks_update_cron'); |
| 72 | 86 | wp_schedule_event($timestamp, 'twicedaily', 'mc_banklinks_update_cron'); |
| @@ -76,13 +90,26 @@ | ||
| 76 | 90 | require_once('includes/vendor/autoload.php'); |
| 77 | 91 | require_once('includes/Maksekeskus.php'); |
| 78 | 92 | } |
| 79 | 93 | |
| 80 | - function mk_check_payment() { | |
| 94 | + function mk_check_payment() { | |
| 81 | 95 | |
| 82 | 96 | global $woocommerce; |
| 83 | 97 | $api = mk_get_api(); |
| 84 | 98 | |
| 99 | + //set the correct language | |
| 100 | + if (isset($_GET["lang1"])) { | |
| 101 | + //set wpml language | |
| 102 | + if (function_exists('icl_object_id')) { | |
| 103 | + do_action('wpml_switch_language', $_GET["lang1"]); | |
| 104 | + } else { | |
| 105 | + //set polylang language | |
| 106 | + if (function_exists('pll_current_language')) { | |
| 107 | + //not implemented yet | |
| 108 | + } | |
| 109 | + } | |
| 110 | + } | |
| 111 | + | |
| 85 | 112 | $returnUrl = home_url(); |
| 86 | 113 | |
| 87 | 114 | $request = stripslashes_deep($_POST); |
| 88 | 115 | if ($api->verifyMac($request)) { |
| @@ -119,9 +146,9 @@ | ||
| 119 | 146 | $paymentStatus = $transaction->status; |
| 120 | 147 | |
| 121 | 148 | if ($paymentStatus == 'PENDING') { |
| 122 | 149 | $paymentRequest = array( |
| 123 | - 'amount' => $transaction->amount, | |
| 150 | + 'amount' => (string)sprintf("%.2f", $transaction->amount), | |
| 124 | 151 | 'currency' => $transaction->currency, |
| 125 | 152 | 'token' => $data['token']['id'] |
| 126 | 153 | ); |
| 127 | 154 | $payment = $api->createPayment($transactionId, $paymentRequest); |
| @@ -141,9 +168,9 @@ | ||
| 141 | 168 | if (!$reference) { return $returnUrl; } |
| 142 | 169 | |
| 143 | 170 | $order = new WC_Order($reference); |
| 144 | 171 | // if we din't find the Order, we send user to shop landing-page. TODO: could be improved |
| 145 | - if (!$order->id) { return $returnUrl; } | |
| 172 | + if (!$order->get_id()) { return $returnUrl; } | |
| 146 | 173 | |
| 147 | 174 | $returnUrl = $order->get_checkout_order_received_url(); |
| 148 | 175 | |
| 149 | 176 | switch($paymentStatus) { |
| @@ -150,22 +177,25 @@ | ||
| 150 | 177 | |
| 151 | 178 | case 'CANCELLED': |
| 152 | 179 | $order->update_status( 'cancelled' ); |
| 153 | 180 | wc_add_notice(__('Payment transaction cancelled', 'wc_makecommerce_domain'), 'error'); |
| 154 | - $returnUrl = $woocommerce->cart->get_cart_url(); | |
| 181 | + $returnUrl = wc_get_cart_url(); | |
| 155 | 182 | break; |
| 156 | 183 | |
| 157 | 184 | case 'COMPLETED': |
| 158 | 185 | $orderNote = array(); |
| 159 | 186 | $orderNote[] = __('Transaction ID', 'wc_makecommerce_domain') . ': <a target=_blank href="'.$api->getEnvUrls()->merchantUrl.'/merchant/shop/deals/detail.html?id='. $transactionId .'">'.$transactionId.'</a>'; |
| 160 | - $orderNote[] = __('Payment option', 'wc_makecommerce_domain') . ': ' . get_post_meta($order->id, '_makecommerce_preselected_method', true); | |
| 187 | + $orderNote[] = __('Payment option', 'wc_makecommerce_domain') . ': ' . get_post_meta($order->get_id(), '_makecommerce_preselected_method', true); | |
| 161 | 188 | $order->add_order_note(implode("\r\n", $orderNote)); |
| 189 | + if (!empty($data['token']) && !empty($data['token']['multiuse'])) { | |
| 190 | + update_post_meta($order->get_id(), '_makecommerce_payment_token', $data['token']['id']); | |
| 191 | + update_post_meta($order->get_id(), '_makecommerce_payment_token_valid_until', $data['token']['valid_until']); | |
| 192 | + } | |
| 162 | 193 | |
| 163 | - $order->payment_complete(); | |
| 164 | - $order->update_status( 'processing' ); | |
| 194 | + if( isset($_GET['lang1']) ) $order->update_meta_data('wpml_language', $_GET["lang1"]); | |
| 195 | + $order->payment_complete( $transactionId ); | |
| 165 | 196 | $woocommerce->cart->empty_cart(); |
| 166 | 197 | break; |
| 167 | - | |
| 168 | 198 | } |
| 169 | 199 | |
| 170 | 200 | return $returnUrl; |
| 171 | 201 | } |
| @@ -253,9 +283,9 @@ | ||
| 253 | 283 | add_action('wp_login', 'mk_admin_login_update', 10, 2); |
| 254 | 284 | |
| 255 | 285 | |
| 256 | 286 | |
| 257 | - function mk_admin_add_api_settings($sections) { | |
| 287 | + function mk_admin_add_api_settings($sections) { | |
| 258 | 288 | $sections['mk_api'] = __('MakeCommerce API access', 'wc_makecommerce_domain'); |
| 259 | 289 | return $sections; |
| 260 | 290 | } |
| 261 | 291 | |
| @@ -269,15 +299,9 @@ | ||
| 269 | 299 | array( |
| 270 | 300 | 'type' => 'title', |
| 271 | 301 | 'title' => __('MakeCommerce API access credentials', 'wc_makecommerce_domain'), |
| 272 | 302 | 'desc' => __('To use MakeCommerce/Maksekeskus services you need to enter API credentials below here <br/> <br/>', 'wc_makecommerce_domain'). |
| 273 | - sprintf( __('To further configure the Payment methods please go to <a href="%s">MakeCommerce Checkout Options</a><br>', 'wc_makecommerce_domain'), 'admin.php?page=wc-settings&tab=checkout§ion=makecommerce'). | |
| 274 | - sprintf( __('To use also Ominva or Itella SmartPOST integration please configure these API accesses: <a href="%s">Omniva Parcel Machine API access</a> | '. | |
| 275 | - '<a href="%s">SmartPOST API access</a> | <a href="%s">Omniva Courier API access</a><br/>','wc_makecommerce_domain'), | |
| 276 | - 'admin.php?page=wc-settings&tab=-shipping§ion=parcelmachine_omniva', | |
| 277 | - 'admin.php?page=wc-settings&tab=shipping§ion=parcelmachine_smartpost', | |
| 278 | - 'admin.php?page=wc-settings&tab=shipping§ion=courier_omniva' | |
| 279 | - ), | |
| 303 | + sprintf( __('To further configure the Payment methods please go to <a href=."%s">MakeCommerce Checkout Options</a>, links to settings of our Shipment methods are listed below', 'wc_makecommerce_domain'), 'admin.php?page=wc-settings&tab=checkout§ion=makecommerce'), | |
| 280 | 304 | 'id' => 'mk_api_settings' |
| 281 | 305 | ), |
| 282 | 306 | array( |
| 283 | 307 | 'type' => 'select', |
| @@ -330,15 +354,75 @@ | ||
| 330 | 354 | 'title' => __('Publishable key (test)', 'wc_makecommerce_domain'), |
| 331 | 355 | 'default' => '7Hog41ci2mKkmtviMycWxpNx14pNP70m', |
| 332 | 356 | 'class' => 'input-text regular-input', |
| 333 | 357 | ), |
| 358 | + array('type' => 'sectionend', 'id' => 'mk_api_settings'), | |
| 359 | + array( | |
| 360 | + 'type' => 'title', | |
| 361 | + 'desc' => '<br><hr><br>', | |
| 362 | + ), | |
| 363 | + array( | |
| 364 | + 'id' => 'mk_module_title', | |
| 365 | + 'type' => 'title', | |
| 366 | + 'title' => __('Makecommerce modules', 'wc_makecommerce_domain'), | |
| 367 | + 'desc' => __('Our plugin adds several modules to your shop. Here you can switch off modules that are not important for you, they will disappear from Woocommerce settings menus.<br> Each active module have their own settings dialog, where they must also be Enabled before use.', 'wc_makecommerce_domain'), | |
| 368 | + 'class' => '', | |
| 369 | + ), | |
| 334 | 370 | array( |
| 335 | - 'id' => 'mk_experimental_features', | |
| 371 | + 'id' => 'mk_transport_apt_omniva', | |
| 336 | 372 | 'type' => 'checkbox', |
| 337 | - 'title' => __('Enable experimental features', 'wc_makecommerce_domain'), | |
| 373 | + 'default' => 'yes', | |
| 374 | + 'title' => __('Omniva Parcel Machine', 'wc_makecommerce_domain'), | |
| 375 | + 'desc' => __('enable Omniva parcel machines shipping method', 'wc_makecommerce_domain').' ('. sprintf(__('<a href="%s">module settings</a>', 'wc_makecommerce_domain'), admin_url('admin.php?page=wc-settings&tab=shipping§ion=parcelmachine_omniva')).')', | |
| 338 | 376 | 'class' => '', |
| 339 | 377 | ), |
| 340 | 378 | array( |
| 379 | + 'id' => 'mk_transport_apt_smartpost', | |
| 380 | + 'type' => 'checkbox', | |
| 381 | + 'default' => 'yes', | |
| 382 | + 'title' => __('SmartPOST Parcel Machine', 'wc_makecommerce_domain'), | |
| 383 | + 'desc' => __('enable SmartPOST parcel machines shipping method', 'wc_makecommerce_domain').' ('. sprintf(__('<a href="%s">module settings</a>', 'wc_makecommerce_domain'), admin_url('admin.php?page=wc-settings&tab=shipping§ion=parcelmachine_smartpost')).')', | |
| 384 | + 'class' => '', | |
| 385 | + ), | |
| 386 | + array( | |
| 387 | + 'id' => 'mk_transport_apt_dpd', | |
| 388 | + 'type' => 'checkbox', | |
| 389 | + 'default' => 'no', | |
| 390 | + 'title' => __('DPD Pickup Network', 'wc_makecommerce_domain'), | |
| 391 | + 'desc' => __('enable DPD Pickup network shipping method', 'wc_makecommerce_domain').' ('. sprintf(__('<a href="%s">module settings</a>', 'wc_makecommerce_domain'), admin_url('admin.php?page=wc-settings&tab=shipping§ion=parcelmachine_dpd')).')', | |
| 392 | + 'class' => '', | |
| 393 | + ), | |
| 394 | + array( | |
| 395 | + 'id' => 'mk_transport_courier_omniva', | |
| 396 | + 'type' => 'checkbox', | |
| 397 | + 'default' => 'yes', | |
| 398 | + 'title' => __('Omniva Courier', 'wc_makecommerce_domain'), | |
| 399 | + 'desc' => __('enable Omniva courier shipping method', 'wc_makecommerce_domain').' ('. sprintf(__('<a href="%s">module settings</a>', 'wc_makecommerce_domain'), admin_url('admin.php?page=wc-settings&tab=shipping§ion=courier_omniva')).')', | |
| 400 | + 'class' => '', | |
| 401 | + ), | |
| 402 | + array( | |
| 403 | + 'id' => 'mk_transport_courier_smartpost', | |
| 404 | + 'type' => 'checkbox', | |
| 405 | + 'default' => 'yes', | |
| 406 | + 'title' => __('SmartPOST Courier', 'wc_makecommerce_domain'), | |
| 407 | + 'desc' => __('enable SmartPOST courier shipping method', 'wc_makecommerce_domain').' ('. sprintf(__('<a href="%s">module settings</a>', 'wc_makecommerce_domain'), admin_url('admin.php?page=wc-settings&tab=shipping§ion=courier_smartpost')).')', | |
| 408 | + 'class' => '', | |
| 409 | + ), | |
| 410 | + array( | |
| 411 | + 'id' => 'mk_checkout_sco', | |
| 412 | + 'type' => 'checkbox', | |
| 413 | + 'title' => __('SimpleCheckout', 'wc_makecommerce_domain'), | |
| 414 | + 'desc' => __('enable SimpleCheckout™ Checkout method', 'wc_makecommerce_domain').' ('. sprintf(__('<a href="%s">module settings</a>', 'wc_makecommerce_domain'), admin_url('admin.php?page=wc-settings&tab=checkout§ion=makecommerce_sc')).')', | |
| 415 | + 'class' => '', | |
| 416 | + ), | |
| 417 | + array( | |
| 418 | + 'id' => 'mk_product_slice', | |
| 419 | + 'type' => 'checkbox', | |
| 420 | + 'title' => __('Slice in 3 information in product view', 'wc_makecommerce_domain'), | |
| 421 | + 'desc' => __('enable Slice in product view', 'wc_makecommerce_domain').' ('. sprintf(__('<a href="%s">module settings</a>', 'wc_makecommerce_domain'), admin_url('admin.php?page=wc-settings&tab=checkout§ion=makecommerce_sc')).')', | |
| 422 | + 'class' => '', | |
| 423 | + ), | |
| 424 | + array( | |
| 341 | 425 | 'id' => 'mk_javascript_ui', |
| 342 | 426 | 'type' => 'api_javascript_ui', |
| 343 | 427 | ), |
| 344 | 428 | array('type' => 'sectionend', 'id' => 'mk_api_settings') |
| @@ -344,8 +428,17 @@ | ||
| 344 | 428 | array('type' => 'sectionend', 'id' => 'mk_api_settings') |
| 345 | 429 | ); |
| 346 | 430 | } |
| 347 | 431 | |
| 432 | + function mk_wc_version($version) { | |
| 433 | + global $woocommerce; | |
| 434 | + if ( version_compare( $woocommerce->version, "3.4.0", ">=" ) ) { | |
| 435 | + return true; | |
| 436 | + } else { | |
| 437 | + return false; | |
| 438 | + } | |
| 439 | + } | |
| 440 | + | |
| 348 | 441 | function mk_admin_save_api_settings() { |
| 349 | 442 | // reload banklinks |
| 350 | 443 | global $current_section; |
| 351 | 444 | if ($current_section !== 'mk_api') { |
| @@ -353,17 +446,55 @@ | ||
| 353 | 446 | } |
| 354 | 447 | update_mc_banklinks(); |
| 355 | 448 | } |
| 356 | 449 | |
| 357 | - function mk_admin_plugin_settings_link($links) { | |
| 358 | - $settings_link = '<a href="admin.php?page=wc-settings&tab=api§ion=mk_api">API '.__('Settings').'</a>'; | |
| 359 | - array_unshift($links, $settings_link); | |
| 360 | - return $links; | |
| 450 | + function mk_admin_plugin_settings_link($links) { | |
| 451 | + if (mk_wc_version("3.4.0")) | |
| 452 | + $settings_link = '<a href="admin.php?page=wc-settings&tab=advanced§ion=mk_api">API '.__('Settings').'</a>'; | |
| 453 | + else | |
| 454 | + $settings_link = '<a href="admin.php?page=wc-settings&tab=api§ion=mk_api">API '.__('Settings').'</a>'; | |
| 455 | + array_unshift($links, $settings_link); | |
| 456 | + return $links; | |
| 361 | 457 | } |
| 458 | + | |
| 459 | + function wc_mc_shop_slice_button() { | |
| 460 | + if (get_option('mk_product_slice', 'no') == 'yes') { | |
| 461 | + global $product; | |
| 462 | + // $price_excl_tax = wc_get_price_excluding_tax( $product ); // price without VAT | |
| 463 | + $price_incl_tax = wc_get_price_including_tax( $product ); // price with VAT | |
| 464 | + global $wpdb; | |
| 465 | + global $mkDbTable; | |
| 466 | + $tableName = $wpdb->prefix . $mkDbTable; | |
| 467 | + $methods = $wpdb->get_results('SELECT * FROM '.$tableName); | |
| 468 | + $slice = false; | |
| 469 | + if(count($methods)) { | |
| 470 | + foreach ($methods as $method) { | |
| 471 | + if ($method->name =="slice") { | |
| 472 | + if ( $price_incl_tax >= $method->min_amount ) { | |
| 473 | + if ( $price_incl_tax <= $method->max_amount ) { | |
| 474 | + $slice = true; | |
| 475 | + } | |
| 476 | + } | |
| 477 | + } | |
| 478 | + } | |
| 479 | + } | |
| 480 | + if ($slice == true) { | |
| 481 | + echo '<div style="width:100%; margin-top: 20px; margin-bottom: auto;"><img src="'.plugins_url('/images/slice_logo_dark_176x62.svg', __FILE__).'" width="88" weight="31"/> Osta kohe, maksa hiljem. Kolmes võrdses osas 3 x '.sprintf("%.2f",$price_incl_tax/3).' €</div>'; // sprintf("%.2f",$price_incl_tax/3).' in month | |
| 482 | + } | |
| 483 | + } | |
| 484 | + } | |
| 485 | + // Slice Promo 1 | |
| 486 | + add_action( 'woocommerce_after_add_to_cart_button', 'wc_mc_shop_slice_button', 20 ); | |
| 487 | + | |
| 488 | + // if (mk_wc_version("3.4.0")) { | |
| 489 | + add_filter('woocommerce_get_settings_advanced', 'mk_admin_api_settings', 10, 2); | |
| 490 | + add_action('woocommerce_get_sections_advanced', 'mk_admin_add_api_settings'); | |
| 362 | 491 | add_filter('woocommerce_get_settings_api', 'mk_admin_api_settings', 10, 2); |
| 492 | + // } else { | |
| 363 | 493 | add_action('woocommerce_get_sections_api', 'mk_admin_add_api_settings'); |
| 364 | 494 | add_action('woocommerce_settings_saved', 'mk_admin_save_api_settings', 30, 0); |
| 365 | 495 | add_filter("plugin_action_links_".plugin_basename(__FILE__), 'mk_admin_plugin_settings_link' ); |
| 496 | + // } | |
| 366 | 497 | |
| 367 | 498 | function __get_logo_html() { |
| 368 | 499 | return '<div class="makecommerce-info">'. |
| 369 | 500 | '<div class="makecommerce-logo">'. |