| @@ -1,30 +1,39 @@ | ||
| 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.1 | |
| 5 | + Version: 2.5.1 | |
| 6 | 6 | Author: Maksekeskus AS |
| 7 | 7 | Author URI: https://MakeCommerce.net/ |
| 8 | 8 | Text Domain: wc_makecommerce_domain |
| 9 | 9 | */ |
| 10 | 10 | |
| 11 | -if ( ! defined( 'ABSPATH' ) ) { | |
| 11 | +if (!defined('ABSPATH')) { | |
| 12 | 12 | exit; // Exit if accessed directly. |
| 13 | 13 | } |
| 14 | 14 | |
| 15 | -if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) { | |
| 15 | +include_once(ABSPATH.'wp-admin/includes/plugin.php'); | |
| 16 | +if (is_plugin_active('woocommerce/woocommerce.php')) { | |
| 16 | 17 | |
| 17 | - | |
| 18 | 18 | define('WC_MC_VERSION', 2.0); |
| 19 | 19 | |
| 20 | - $enable_experimental = get_option('mk_experimental_features', false); | |
| 21 | - if ($enable_experimental && $enable_experimental !== 'no') { | |
| 20 | + $enable_experimental = get_option('mk_checkout_sco', 'no'); | |
| 21 | + if ($enable_experimental === 'yes') { | |
| 22 | 22 | require_once(__DIR__.'/makecommerce-simplecheckout.php'); |
| 23 | 23 | } |
| 24 | 24 | require_once(__DIR__.'/makecommerce-transport.php'); |
| 25 | 25 | require_once(__DIR__.'/makecommerce-payment.php'); |
| 26 | 26 | |
| 27 | + if (is_admin() && !extension_loaded('curl')) { | |
| 28 | + add_action('admin_notices', function(){ | |
| 29 | + $class = 'notice notice-error'; | |
| 30 | + $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'); | |
| 31 | + printf( '<div class="%1$s"><p>%2$s</p></div>', esc_attr( $class ), esc_html( $message ) ); | |
| 32 | + }); | |
| 33 | + } | |
| 34 | + | |
| 35 | + | |
| 27 | 36 | global $mkDbTable; |
| 28 | 37 | $mkDbTable = 'mc_banklinks'; |
| 29 | 38 | |
| 30 | 39 | function mc_install() { |
| @@ -40,8 +49,9 @@ | ||
| 40 | 49 | type varchar(10) NOT NULL, |
| 41 | 50 | country char(2) NOT NULL, |
| 42 | 51 | name varchar(25) NOT NULL, |
| 43 | 52 | url varchar(250) NOT NULL, |
| 53 | + logo_url varchar(250), | |
| 44 | 54 | UNIQUE KEY id (id) |
| 45 | 55 | ) $charset;"; |
| 46 | 56 | |
| 47 | 57 | require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); |
| @@ -68,9 +78,9 @@ | ||
| 68 | 78 | $date->setTime(04,10,00); |
| 69 | 79 | $timestamp = intval($date->format('U')); |
| 70 | 80 | //$local = 3600 * intval(get_option('gmt_offset')); |
| 71 | 81 | //wp_schedule_event($timestamp - $local, 'twicedaily', 'mc_banklinks_update_cron'); |
| 72 | - wp_schedule_event($timestamp, 'hourly', 'mc_banklinks_update_cron'); | |
| 82 | + wp_schedule_event($timestamp, 'twicedaily', 'mc_banklinks_update_cron'); | |
| 73 | 83 | } |
| 74 | 84 | |
| 75 | 85 | if (!class_exists('wc_makecommerce_domain')) { |
| 76 | 86 | require_once('includes/vendor/autoload.php'); |
| @@ -88,8 +98,13 @@ | ||
| 88 | 98 | if ($api->verifyMac($request)) { |
| 89 | 99 | $data = $api->extractRequestData($request); |
| 90 | 100 | $reference = false; |
| 91 | 101 | |
| 102 | + if (!empty($data['error']) ) { | |
| 103 | + wc_add_notice(__('Payment failed', 'wc_makecommerce_domain'), 'error'); | |
| 104 | + return $returnUrl; | |
| 105 | + } | |
| 106 | + | |
| 92 | 107 | if (!empty($data['message_type']) ) { |
| 93 | 108 | if ($data['message_type'] == 'payment_return') { |
| 94 | 109 | $transactionId = $data['transaction']; |
| 95 | 110 | $reference = $data['reference']; |
| @@ -103,9 +118,9 @@ | ||
| 103 | 118 | $transactionId = $data['transaction']['id']; |
| 104 | 119 | |
| 105 | 120 | // for API backward compatibility - if we didn't find reference in the mesage, or the transaction state vas still PENDING |
| 106 | 121 | // we'll fetch the transaction data from API, and make a payment request if needed |
| 107 | - // this all can be removed avter 2017-jan | |
| 122 | + // this all can be removed after 2017-jan | |
| 108 | 123 | if (!$reference or $paymentStatus == 'PENDING') { |
| 109 | 124 | $transaction = $api->getTransaction($transactionId); |
| 110 | 125 | if (!empty($transaction->reference)) { |
| 111 | 126 | $reference = $transaction->reference; |
| @@ -114,9 +129,9 @@ | ||
| 114 | 129 | $paymentStatus = $transaction->status; |
| 115 | 130 | |
| 116 | 131 | if ($paymentStatus == 'PENDING') { |
| 117 | 132 | $paymentRequest = array( |
| 118 | - 'amount' => $transaction->amount, | |
| 133 | + 'amount' => (string)sprintf("%.2f", $transaction->amount), | |
| 119 | 134 | 'currency' => $transaction->currency, |
| 120 | 135 | 'token' => $data['token']['id'] |
| 121 | 136 | ); |
| 122 | 137 | $payment = $api->createPayment($transactionId, $paymentRequest); |
| @@ -136,30 +151,37 @@ | ||
| 136 | 151 | if (!$reference) { return $returnUrl; } |
| 137 | 152 | |
| 138 | 153 | $order = new WC_Order($reference); |
| 139 | 154 | // if we din't find the Order, we send user to shop landing-page. TODO: could be improved |
| 140 | - if (!$order->id) { return $returnUrl; } | |
| 155 | + if (!$order->get_order_number()) { return $returnUrl; } | |
| 141 | 156 | |
| 157 | + $returnUrl = $order->get_checkout_order_received_url(); | |
| 158 | + | |
| 142 | 159 | switch($paymentStatus) { |
| 143 | 160 | |
| 144 | 161 | case 'CANCELLED': |
| 145 | 162 | $order->update_status( 'cancelled' ); |
| 146 | 163 | wc_add_notice(__('Payment transaction cancelled', 'wc_makecommerce_domain'), 'error'); |
| 164 | + $returnUrl = $woocommerce->cart->get_cart_url(); | |
| 147 | 165 | break; |
| 148 | 166 | |
| 149 | 167 | case 'COMPLETED': |
| 150 | 168 | $orderNote = array(); |
| 151 | 169 | $orderNote[] = __('Transaction ID', 'wc_makecommerce_domain') . ': <a target=_blank href="'.$api->getEnvUrls()->merchantUrl.'/merchant/shop/deals/detail.html?id='. $transactionId .'">'.$transactionId.'</a>'; |
| 152 | - $orderNote[] = __('Payment option', 'wc_makecommerce_domain') . ': ' . get_post_meta($order->id, '_makecommerce_preselected_method', true); | |
| 170 | + $orderNote[] = __('Payment option', 'wc_makecommerce_domain') . ': ' . get_post_meta($order->get_order_number(), '_makecommerce_preselected_method', true); | |
| 153 | 171 | $order->add_order_note(implode("\r\n", $orderNote)); |
| 172 | + if (!empty($data['token']) && !empty($data['token']['multiuse'])) { | |
| 173 | + update_post_meta($order->get_order_number(), '_makecommerce_payment_token', $data['token']['id']); | |
| 174 | + update_post_meta($order->get_order_number(), '_makecommerce_payment_token_valid_until', $data['token']['valid_until']); | |
| 175 | + } | |
| 154 | 176 | |
| 155 | - $order->payment_complete(); | |
| 156 | - $order->update_status( 'processing' ); | |
| 177 | + $order->payment_complete( $transactionId ); | |
| 178 | + // $order->update_status( 'processing' ); | |
| 179 | + $woocommerce->cart->empty_cart(); | |
| 157 | 180 | break; |
| 158 | 181 | |
| 159 | 182 | } |
| 160 | 183 | |
| 161 | - $returnUrl = $order->get_checkout_order_received_url(); | |
| 162 | 184 | return $returnUrl; |
| 163 | 185 | } |
| 164 | 186 | |
| 165 | 187 | |
| @@ -245,9 +267,9 @@ | ||
| 245 | 267 | add_action('wp_login', 'mk_admin_login_update', 10, 2); |
| 246 | 268 | |
| 247 | 269 | |
| 248 | 270 | |
| 249 | - function mk_admin_add_api_settings($sections) { | |
| 271 | + function mk_admin_add_api_settings($sections) { | |
| 250 | 272 | $sections['mk_api'] = __('MakeCommerce API access', 'wc_makecommerce_domain'); |
| 251 | 273 | return $sections; |
| 252 | 274 | } |
| 253 | 275 | |
| @@ -261,15 +283,9 @@ | ||
| 261 | 283 | array( |
| 262 | 284 | 'type' => 'title', |
| 263 | 285 | 'title' => __('MakeCommerce API access credentials', 'wc_makecommerce_domain'), |
| 264 | 286 | 'desc' => __('To use MakeCommerce/Maksekeskus services you need to enter API credentials below here <br/> <br/>', 'wc_makecommerce_domain'). |
| 265 | - 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'). | |
| 266 | - sprintf( __('To use also Ominva or Itella SmartPOST integration please configure these API accesses: <a href="%s">Omniva Parcel Machine API access</a> | '. | |
| 267 | - '<a href="%s">SmartPOST API access</a> | <a href="%s">Omniva Courier API access</a><br/>','wc_makecommerce_domain'), | |
| 268 | - 'admin.php?page=wc-settings&tab=-shipping§ion=parcelmachine_omniva', | |
| 269 | - 'admin.php?page=wc-settings&tab=shipping§ion=parcelmachine_smartpost', | |
| 270 | - 'admin.php?page=wc-settings&tab=shipping§ion=courier_omniva' | |
| 271 | - ), | |
| 287 | + 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'), | |
| 272 | 288 | 'id' => 'mk_api_settings' |
| 273 | 289 | ), |
| 274 | 290 | array( |
| 275 | 291 | 'type' => 'select', |
| @@ -322,15 +338,68 @@ | ||
| 322 | 338 | 'title' => __('Publishable key (test)', 'wc_makecommerce_domain'), |
| 323 | 339 | 'default' => '7Hog41ci2mKkmtviMycWxpNx14pNP70m', |
| 324 | 340 | 'class' => 'input-text regular-input', |
| 325 | 341 | ), |
| 342 | + array('type' => 'sectionend', 'id' => 'mk_api_settings'), | |
| 343 | + array( | |
| 344 | + 'type' => 'title', | |
| 345 | + 'desc' => '<br><hr><br>', | |
| 346 | + ), | |
| 347 | + array( | |
| 348 | + 'id' => 'mk_module_title', | |
| 349 | + 'type' => 'title', | |
| 350 | + 'title' => __('Makecommerce modules', 'wc_makecommerce_domain'), | |
| 351 | + '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'), | |
| 352 | + 'class' => '', | |
| 353 | + ), | |
| 326 | 354 | array( |
| 327 | - 'id' => 'mk_experimental_features', | |
| 355 | + 'id' => 'mk_transport_apt_omniva', | |
| 328 | 356 | 'type' => 'checkbox', |
| 329 | - 'title' => __('Enable experimental features', 'wc_makecommerce_domain'), | |
| 357 | + 'default' => 'yes', | |
| 358 | + 'title' => __('Omniva Parcel Machine', 'wc_makecommerce_domain'), | |
| 359 | + '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')).')', | |
| 330 | 360 | 'class' => '', |
| 331 | 361 | ), |
| 332 | 362 | array( |
| 363 | + 'id' => 'mk_transport_apt_smartpost', | |
| 364 | + 'type' => 'checkbox', | |
| 365 | + 'default' => 'yes', | |
| 366 | + 'title' => __('SmartPOST Parcel Machine', 'wc_makecommerce_domain'), | |
| 367 | + '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')).')', | |
| 368 | + 'class' => '', | |
| 369 | + ), | |
| 370 | + array( | |
| 371 | + 'id' => 'mk_transport_apt_dpd', | |
| 372 | + 'type' => 'checkbox', | |
| 373 | + 'default' => 'no', | |
| 374 | + 'title' => __('DPD Pickup Network', 'wc_makecommerce_domain'), | |
| 375 | + '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')).')', | |
| 376 | + 'class' => '', | |
| 377 | + ), | |
| 378 | + array( | |
| 379 | + 'id' => 'mk_transport_courier_omniva', | |
| 380 | + 'type' => 'checkbox', | |
| 381 | + 'default' => 'yes', | |
| 382 | + 'title' => __('Omniva Courier', 'wc_makecommerce_domain'), | |
| 383 | + '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')).')', | |
| 384 | + 'class' => '', | |
| 385 | + ), | |
| 386 | + array( | |
| 387 | + 'id' => 'mk_transport_courier_smartpost', | |
| 388 | + 'type' => 'checkbox', | |
| 389 | + 'default' => 'yes', | |
| 390 | + 'title' => __('SmartPOST Courier', 'wc_makecommerce_domain'), | |
| 391 | + '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')).')', | |
| 392 | + 'class' => '', | |
| 393 | + ), | |
| 394 | + array( | |
| 395 | + 'id' => 'mk_checkout_sco', | |
| 396 | + 'type' => 'checkbox', | |
| 397 | + 'title' => __('SimpleCheckout', 'wc_makecommerce_domain'), | |
| 398 | + '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')).')', | |
| 399 | + 'class' => '', | |
| 400 | + ), | |
| 401 | + array( | |
| 333 | 402 | 'id' => 'mk_javascript_ui', |
| 334 | 403 | 'type' => 'api_javascript_ui', |
| 335 | 404 | ), |
| 336 | 405 | array('type' => 'sectionend', 'id' => 'mk_api_settings') |
| @@ -336,8 +405,17 @@ | ||
| 336 | 405 | array('type' => 'sectionend', 'id' => 'mk_api_settings') |
| 337 | 406 | ); |
| 338 | 407 | } |
| 339 | 408 | |
| 409 | + function mk_wc_version($version) { | |
| 410 | + global $woocommerce; | |
| 411 | + if ( version_compare( $woocommerce->version, "3.4.0", ">=" ) ) { | |
| 412 | + return true; | |
| 413 | + } else { | |
| 414 | + return false; | |
| 415 | + } | |
| 416 | + } | |
| 417 | + | |
| 340 | 418 | function mk_admin_save_api_settings() { |
| 341 | 419 | // reload banklinks |
| 342 | 420 | global $current_section; |
| 343 | 421 | if ($current_section !== 'mk_api') { |
| @@ -345,17 +423,25 @@ | ||
| 345 | 423 | } |
| 346 | 424 | update_mc_banklinks(); |
| 347 | 425 | } |
| 348 | 426 | |
| 349 | - function mk_admin_plugin_settings_link($links) { | |
| 350 | - $settings_link = '<a href="admin.php?page=wc-settings&tab=api§ion=mk_api">API '.__('Settings').'</a>'; | |
| 351 | - array_unshift($links, $settings_link); | |
| 352 | - return $links; | |
| 427 | + function mk_admin_plugin_settings_link($links) { | |
| 428 | + if (mk_wc_version("3.4.0")) | |
| 429 | + $settings_link = '<a href="admin.php?page=wc-settings&tab=advanced§ion=mk_api">API '.__('Settings').'</a>'; | |
| 430 | + else | |
| 431 | + $settings_link = '<a href="admin.php?page=wc-settings&tab=api§ion=mk_api">API '.__('Settings').'</a>'; | |
| 432 | + array_unshift($links, $settings_link); | |
| 433 | + return $links; | |
| 353 | 434 | } |
| 435 | + // if (mk_wc_version("3.4.0")) { | |
| 436 | + add_filter('woocommerce_get_settings_advanced', 'mk_admin_api_settings', 10, 2); | |
| 437 | + add_action('woocommerce_get_sections_advanced', 'mk_admin_add_api_settings'); | |
| 354 | 438 | add_filter('woocommerce_get_settings_api', 'mk_admin_api_settings', 10, 2); |
| 439 | + // } else { | |
| 355 | 440 | add_action('woocommerce_get_sections_api', 'mk_admin_add_api_settings'); |
| 356 | 441 | add_action('woocommerce_settings_saved', 'mk_admin_save_api_settings', 30, 0); |
| 357 | 442 | add_filter("plugin_action_links_".plugin_basename(__FILE__), 'mk_admin_plugin_settings_link' ); |
| 443 | + // } | |
| 358 | 444 | |
| 359 | 445 | function __get_logo_html() { |
| 360 | 446 | return '<div class="makecommerce-info">'. |
| 361 | 447 | '<div class="makecommerce-logo">'. |
| @@ -367,5 +453,12 @@ | ||
| 367 | 453 | '<div class="makecommerce-link"><a target="_blank" href="http://maksekeskus.ee">maksekeskus.ee</a></div>'. |
| 368 | 454 | '</div>'. |
| 369 | 455 | '</div>'; |
| 370 | 456 | } |
| 457 | + | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
| 461 | + | |
| 462 | + | |
| 463 | + | |
| 371 | 464 | } |