| @@ -1,46 +1,379 @@ | ||
| 1 | 1 | <?php |
| 2 | -/** | |
| 3 | - * @link https://makecommerce.net/ | |
| 4 | - * @since 4.0.0 | |
| 5 | - * @package Makecommerce | |
| 6 | - * | |
| 7 | - * @wordpress-plugin | |
| 8 | - * Plugin Name: MakeCommerce | |
| 9 | - * Plugin URI: https://makecommerce.net/ | |
| 10 | - * Description: Adds MakeCommerce payment gateway and shipping methods to WooCommerce checkout | |
| 11 | - * Version: 4.1.0 | |
| 12 | - * Author: Maksekeskus AS | |
| 13 | - * Author URI: https://makecommerce.net/ | |
| 14 | - * License: GPL-2.0+ | |
| 15 | - * License URI: http://www.gnu.org/licenses/gpl-2.0.txt | |
| 16 | - * Text Domain: makecommerce | |
| 17 | - * Domain Path: /languages | |
| 18 | - * Requires Plugins: woocommerce | |
| 19 | - * Requires at least: 6.8.1 | |
| 20 | - * Requires PHP: 8.1 | |
| 21 | - * WC requires at least: 9.9.3 | |
| 22 | - * WC tested up to: 11.0.1 | |
| 23 | - * WC HPOS compatibility: yes | |
| 24 | - * WC Legacy Order Table Compatibility: yes | |
| 25 | - */ | |
| 2 | +/* | |
| 3 | + Plugin Name: MakeCommerce for WooCommerce | |
| 4 | + Description: Adds MakeCommerce payment gateway and Itella/Omniva parcel machine shipping methods to Woocommerce checkout | |
| 5 | + Version: 2.1.4 | |
| 6 | + Author: Maksekeskus AS | |
| 7 | + Author URI: https://MakeCommerce.net/ | |
| 8 | + Text Domain: wc_makecommerce_domain | |
| 9 | +*/ | |
| 26 | 10 | |
| 27 | -function mc_table_exists(string $table): bool { | |
| 28 | - global $wpdb; | |
| 29 | - $table_name = $wpdb->prefix . $table; | |
| 30 | - return $wpdb->get_var($wpdb->prepare("SHOW TABLES LIKE %s", $table_name)) === $table_name; | |
| 11 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 12 | + exit; // Exit if accessed directly. | |
| 31 | 13 | } |
| 32 | 14 | |
| 15 | +if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) { | |
| 33 | 16 | |
| 34 | -if ( | |
| 35 | - ( | |
| 36 | - get_option('mc_version') !== false || | |
| 37 | - get_option('wc_mc_version') !== false || | |
| 38 | - mc_table_exists('mc_banklinks') | |
| 39 | - ) && | |
| 40 | - !filter_var(get_option('mc_shipping_plus'), FILTER_VALIDATE_BOOLEAN) | |
| 41 | -) { | |
| 42 | - require_once plugin_dir_path(__FILE__) . 'config.php'; | |
| 43 | -} else { | |
| 44 | - require_once plugin_dir_path(__FILE__) . 'makecommerce/makecommerce.php'; | |
| 17 | + | |
| 18 | + define('WC_MC_VERSION', 2.0); | |
| 19 | + | |
| 20 | + $enable_experimental = get_option('mk_experimental_features', false); | |
| 21 | + if ($enable_experimental && $enable_experimental !== 'no') { | |
| 22 | + require_once(__DIR__.'/makecommerce-simplecheckout.php'); | |
| 23 | + } | |
| 24 | + require_once(__DIR__.'/makecommerce-transport.php'); | |
| 25 | + require_once(__DIR__.'/makecommerce-payment.php'); | |
| 26 | + | |
| 27 | + global $mkDbTable; | |
| 28 | + $mkDbTable = 'mc_banklinks'; | |
| 29 | + | |
| 30 | + function mc_install() { | |
| 31 | + global $wpdb; | |
| 32 | + global $mkDbTable; | |
| 33 | + | |
| 34 | + $tableName = $wpdb->prefix . $mkDbTable; | |
| 35 | + | |
| 36 | + $charset = $wpdb->get_charset_collate(); | |
| 37 | + | |
| 38 | + $sql = "CREATE TABLE $tableName ( | |
| 39 | + id mediumint(9) NOT NULL AUTO_INCREMENT, | |
| 40 | + type varchar(10) NOT NULL, | |
| 41 | + country char(2) NOT NULL, | |
| 42 | + name varchar(25) NOT NULL, | |
| 43 | + url varchar(250) NOT NULL, | |
| 44 | + UNIQUE KEY id (id) | |
| 45 | + ) $charset;"; | |
| 46 | + | |
| 47 | + require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); | |
| 48 | + dbDelta( $sql ); | |
| 49 | + } | |
| 50 | + | |
| 51 | + register_activation_hook( __FILE__, 'mc_install' ); | |
| 52 | + | |
| 53 | + // On uninstall remove scheduled hook | |
| 54 | + function mc_uninstall() { | |
| 55 | + if (wp_next_scheduled ( 'mc_banklinks_update_cron' )) { | |
| 56 | + wp_clear_scheduled_hook('mc_banklinks_update_cron'); | |
| 57 | + } | |
| 58 | + } | |
| 59 | + | |
| 60 | + register_deactivation_hook(__FILE__, 'mc_uninstall'); | |
| 61 | + | |
| 62 | + // WP schedule callable action | |
| 63 | + add_action('mc_banklinks_update_cron', 'update_mc_banklinks'); | |
| 64 | + // Assign schedule if not set, update compatible no need to reactivate, format('U') php 5.2 compatible | |
| 65 | + // Assigns task at 23.59.59 by local time, task runs if time has passed and somebody navigates the site | |
| 66 | + if (! wp_next_scheduled ( 'mc_banklinks_update_cron' )) { | |
| 67 | + $date = new DateTime(); | |
| 68 | + $date->setTime(04,10,00); | |
| 69 | + $timestamp = intval($date->format('U')); | |
| 70 | + //$local = 3600 * intval(get_option('gmt_offset')); | |
| 71 | + //wp_schedule_event($timestamp - $local, 'twicedaily', 'mc_banklinks_update_cron'); | |
| 72 | + wp_schedule_event($timestamp, 'twicedaily', 'mc_banklinks_update_cron'); | |
| 73 | + } | |
| 74 | + | |
| 75 | + if (!class_exists('wc_makecommerce_domain')) { | |
| 76 | + require_once('includes/vendor/autoload.php'); | |
| 77 | + require_once('includes/Maksekeskus.php'); | |
| 78 | + } | |
| 79 | + | |
| 80 | + function mk_check_payment() { | |
| 81 | + | |
| 82 | + global $woocommerce; | |
| 83 | + $api = mk_get_api(); | |
| 84 | + | |
| 85 | + $returnUrl = home_url(); | |
| 86 | + | |
| 87 | + $request = stripslashes_deep($_POST); | |
| 88 | + if ($api->verifyMac($request)) { | |
| 89 | + $data = $api->extractRequestData($request); | |
| 90 | + $reference = false; | |
| 91 | + | |
| 92 | + if (!empty($data['error']) ) { | |
| 93 | + wc_add_notice(__('Payment failed', 'wc_makecommerce_domain'), 'error'); | |
| 94 | + return $returnUrl; | |
| 95 | + } | |
| 96 | + | |
| 97 | + if (!empty($data['message_type']) ) { | |
| 98 | + if ($data['message_type'] == 'payment_return') { | |
| 99 | + $transactionId = $data['transaction']; | |
| 100 | + $reference = $data['reference']; | |
| 101 | + $paymentStatus = $data['status']; | |
| 102 | + } | |
| 103 | + if ($data['message_type'] == 'token_return') { | |
| 104 | + if (!empty($data['transaction']['reference'])) { | |
| 105 | + $reference = $data['transaction']['reference']; | |
| 106 | + } | |
| 107 | + $paymentStatus = $data['transaction']['status']; | |
| 108 | + $transactionId = $data['transaction']['id']; | |
| 109 | + | |
| 110 | + // for API backward compatibility - if we didn't find reference in the mesage, or the transaction state vas still PENDING | |
| 111 | + // we'll fetch the transaction data from API, and make a payment request if needed | |
| 112 | + // this all can be removed after 2017-jan | |
| 113 | + if (!$reference or $paymentStatus == 'PENDING') { | |
| 114 | + $transaction = $api->getTransaction($transactionId); | |
| 115 | + if (!empty($transaction->reference)) { | |
| 116 | + $reference = $transaction->reference; | |
| 117 | + } | |
| 118 | + | |
| 119 | + $paymentStatus = $transaction->status; | |
| 120 | + | |
| 121 | + if ($paymentStatus == 'PENDING') { | |
| 122 | + $paymentRequest = array( | |
| 123 | + 'amount' => $transaction->amount, | |
| 124 | + 'currency' => $transaction->currency, | |
| 125 | + 'token' => $data['token']['id'] | |
| 126 | + ); | |
| 127 | + $payment = $api->createPayment($transactionId, $paymentRequest); | |
| 128 | + // TODO: validate | |
| 129 | + $paymentStatus = $payment->transaction->status; | |
| 130 | + } | |
| 131 | + | |
| 132 | + } | |
| 133 | + | |
| 134 | + | |
| 135 | + } | |
| 136 | + | |
| 137 | + } | |
| 138 | + } | |
| 139 | + | |
| 140 | + // if we didn't find reference to an Order, we send user to shop landing-page. TODO: could be improved | |
| 141 | + if (!$reference) { return $returnUrl; } | |
| 142 | + | |
| 143 | + $order = new WC_Order($reference); | |
| 144 | + // if we din't find the Order, we send user to shop landing-page. TODO: could be improved | |
| 145 | + if (!$order->id) { return $returnUrl; } | |
| 146 | + | |
| 147 | + $returnUrl = $order->get_checkout_order_received_url(); | |
| 148 | + | |
| 149 | + switch($paymentStatus) { | |
| 150 | + | |
| 151 | + case 'CANCELLED': | |
| 152 | + $order->update_status( 'cancelled' ); | |
| 153 | + wc_add_notice(__('Payment transaction cancelled', 'wc_makecommerce_domain'), 'error'); | |
| 154 | + $returnUrl = $woocommerce->cart->get_cart_url(); | |
| 155 | + break; | |
| 156 | + | |
| 157 | + case 'COMPLETED': | |
| 158 | + $orderNote = array(); | |
| 159 | + $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); | |
| 161 | + $order->add_order_note(implode("\r\n", $orderNote)); | |
| 162 | + | |
| 163 | + $order->payment_complete(); | |
| 164 | + $order->update_status( 'processing' ); | |
| 165 | + $woocommerce->cart->empty_cart(); | |
| 166 | + break; | |
| 167 | + | |
| 168 | + } | |
| 169 | + | |
| 170 | + return $returnUrl; | |
| 171 | + } | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + $MKAPI = false; | |
| 177 | + function mk_get_api() { | |
| 178 | + global $MKAPI; | |
| 179 | + if ($MKAPI) { | |
| 180 | + //return $MKAPI; | |
| 181 | + } | |
| 182 | + $mk_api_type = get_option('mk_api_type', false); | |
| 183 | + if (!$mk_api_type) { | |
| 184 | + return false; | |
| 185 | + } | |
| 186 | + if ($mk_api_type !== 'live') { | |
| 187 | + $key_prefix = $mk_api_type.'_'; | |
| 188 | + } else { | |
| 189 | + $key_prefix = ''; | |
| 190 | + } | |
| 191 | + $mk_shop_id = get_option('mk_'.$key_prefix.'shop_id', ''); | |
| 192 | + $mk_public_key = get_option('mk_'.$key_prefix.'public_key', ''); | |
| 193 | + $mk_private_key = get_option('mk_'.$key_prefix.'private_key', ''); | |
| 194 | + if (!$mk_shop_id || !$mk_public_key || !$mk_shop_id) { | |
| 195 | + return false; | |
| 196 | + } | |
| 197 | + $MKAPI = new \Maksekeskus\Maksekeskus($mk_shop_id, $mk_public_key, $mk_private_key, $mk_api_type === 'live' ? false : true); | |
| 198 | + return $MKAPI; | |
| 199 | + } | |
| 200 | + | |
| 201 | + function mk_is_api_set() { | |
| 202 | + $mk_api_type = get_option('mk_api_type', false); | |
| 203 | + if (!$mk_api_type) { | |
| 204 | + return false; | |
| 205 | + } | |
| 206 | + if ($mk_api_type !== 'live') { | |
| 207 | + $key_prefix = $mk_api_type.'_'; | |
| 208 | + } else { | |
| 209 | + $key_prefix = ''; | |
| 210 | + } | |
| 211 | + $mk_shop_id = get_option('mk_'.$key_prefix.'shop_id', ''); | |
| 212 | + $mk_public_key = get_option('mk_'.$key_prefix.'public_key', ''); | |
| 213 | + $mk_private_key = get_option('mk_'.$key_prefix.'private_key', ''); | |
| 214 | + if (!$mk_shop_id || !$mk_public_key || !$mk_shop_id) { | |
| 215 | + return false; | |
| 216 | + } | |
| 217 | + return true; | |
| 218 | + } | |
| 219 | + | |
| 220 | + | |
| 221 | + function mk_admin_error() { | |
| 222 | + 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')); | |
| 223 | + } | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + add_filter('query_vars', 'mk_cron_query_vars'); | |
| 228 | + add_action('parse_request', 'mk_parse_cron_update_vars'); | |
| 229 | + | |
| 230 | + function mk_cron_query_vars($vars) { | |
| 231 | + $vars[] = 'mk-action'; | |
| 232 | + $vars[] = 'mk-shop-id'; | |
| 233 | + $vars[] = 'mk-test-shop-id'; | |
| 234 | + return $vars; | |
| 235 | + } | |
| 236 | + | |
| 237 | + function mk_parse_cron_update_vars($wp) { | |
| 238 | + if (array_key_exists('mk-action', $wp->query_vars) && $wp->query_vars['mk-action'] == 'mk-update' | |
| 239 | + && (array_key_exists('mk-shop-id', $wp->query_vars) || array_key_exists('mk-test-shop-id', $wp->query_vars) ) ) { | |
| 240 | + if( (strlen($wp->query_vars['mk-shop-id']) && $wp->query_vars['mk-shop-id'] == get_option('mk_shop_id', false) ) | |
| 241 | + || (strlen($wp->query_vars['mk-test-shop-id']) && $wp->query_vars['mk-test-shop-id'] == get_option('mk_test_shop_id', false) ) ) { | |
| 242 | + update_mc_banklinks(); | |
| 243 | + exit(); | |
| 244 | + } | |
| 245 | + } | |
| 246 | + } | |
| 247 | + | |
| 248 | + function mk_admin_login_update($user_login, $user) { | |
| 249 | + if(user_can( $user, 'administrator' )) { | |
| 250 | + update_mc_banklinks(); | |
| 251 | + } | |
| 252 | + } | |
| 253 | + add_action('wp_login', 'mk_admin_login_update', 10, 2); | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + function mk_admin_add_api_settings($sections) { | |
| 258 | + $sections['mk_api'] = __('MakeCommerce API access', 'wc_makecommerce_domain'); | |
| 259 | + return $sections; | |
| 260 | + } | |
| 261 | + | |
| 262 | + function mk_admin_api_settings($settings) { | |
| 263 | + global $current_section; | |
| 264 | + if ($current_section !== 'mk_api') { | |
| 265 | + return $settings; | |
| 266 | + } | |
| 267 | + return array( | |
| 268 | + array('type' => 'title', 'desc' => __get_logo_html()), | |
| 269 | + array( | |
| 270 | + 'type' => 'title', | |
| 271 | + 'title' => __('MakeCommerce API access credentials', 'wc_makecommerce_domain'), | |
| 272 | + '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 | + ), | |
| 280 | + 'id' => 'mk_api_settings' | |
| 281 | + ), | |
| 282 | + array( | |
| 283 | + 'type' => 'select', | |
| 284 | + 'title' => __('Current environment', 'wc_makecommerce_domain'), | |
| 285 | + 'desc' => __('See more about <a href="https://maksekeskus.ee/en/for-developers/test-environment/">MakeCommerce Test environment</a>', 'wc_makecommerce_domain'), | |
| 286 | + 'default' => 'live', | |
| 287 | + 'options' => array( | |
| 288 | + 'live' => __('Live', 'wc_makecommerce_domain'), | |
| 289 | + 'test' => __('Test', 'wc_makecommerce_domain'), | |
| 290 | + ), | |
| 291 | + 'id' => 'mk_api_type' | |
| 292 | + ), | |
| 293 | + array( | |
| 294 | + 'id' => 'mk_shop_id', | |
| 295 | + 'type' => 'text', | |
| 296 | + 'title' => __('Shop ID (live)', 'wc_makecommerce_domain'), | |
| 297 | + 'desc' => __('Get it from <a href="https://merchant.maksekeskus.ee/api.html" target="_blank">Merchant Portal</a>','wc_makecommerce_domain'), | |
| 298 | + 'class' => 'input-text regular-input', | |
| 299 | + ), | |
| 300 | + array( | |
| 301 | + 'id' => 'mk_private_key', | |
| 302 | + 'type' => 'text', | |
| 303 | + 'title' => __('Secret key (live)', 'wc_makecommerce_domain'), | |
| 304 | + 'class' => 'input-text regular-input', | |
| 305 | + ), | |
| 306 | + array( | |
| 307 | + 'id' => 'mk_public_key', | |
| 308 | + 'type' => 'text', | |
| 309 | + 'title' => __('Publishable key (live)', 'wc_makecommerce_domain'), | |
| 310 | + 'class' => 'input-text regular-input', | |
| 311 | + ), | |
| 312 | + array( | |
| 313 | + 'id' => 'mk_test_shop_id', | |
| 314 | + 'type' => 'text', | |
| 315 | + 'title' => __('Shop ID (test)', 'wc_makecommerce_domain'), | |
| 316 | + 'class' => 'input-text regular-input', | |
| 317 | + 'default' => 'f64b4f20-5ef9-4b7b-a6fa-d623d87f0b9c', | |
| 318 | + 'desc' => __('Get it from <a href="https://merchant-test.maksekeskus.ee/api.html" target="_blank">Merchant Portal Test</a>','wc_makecommerce_domain'), | |
| 319 | + ), | |
| 320 | + array( | |
| 321 | + 'id' => 'mk_test_private_key', | |
| 322 | + 'type' => 'text', | |
| 323 | + 'title' => __('Secret key (test)', 'wc_makecommerce_domain'), | |
| 324 | + 'default' => 'MPjcVMoRZPAucsTuGK5ZukOlV7BlzgSvXOowJUkk9IFSQPVooRwcVOxzz3mhEpgM', | |
| 325 | + 'class' => 'input-text regular-input', | |
| 326 | + ), | |
| 327 | + array( | |
| 328 | + 'id' => 'mk_test_public_key', | |
| 329 | + 'type' => 'text', | |
| 330 | + 'title' => __('Publishable key (test)', 'wc_makecommerce_domain'), | |
| 331 | + 'default' => '7Hog41ci2mKkmtviMycWxpNx14pNP70m', | |
| 332 | + 'class' => 'input-text regular-input', | |
| 333 | + ), | |
| 334 | + array( | |
| 335 | + 'id' => 'mk_experimental_features', | |
| 336 | + 'type' => 'checkbox', | |
| 337 | + 'title' => __('Enable experimental features', 'wc_makecommerce_domain'), | |
| 338 | + 'class' => '', | |
| 339 | + ), | |
| 340 | + array( | |
| 341 | + 'id' => 'mk_javascript_ui', | |
| 342 | + 'type' => 'api_javascript_ui', | |
| 343 | + ), | |
| 344 | + array('type' => 'sectionend', 'id' => 'mk_api_settings') | |
| 345 | + ); | |
| 346 | + } | |
| 347 | + | |
| 348 | + function mk_admin_save_api_settings() { | |
| 349 | + // reload banklinks | |
| 350 | + global $current_section; | |
| 351 | + if ($current_section !== 'mk_api') { | |
| 352 | + return; | |
| 353 | + } | |
| 354 | + update_mc_banklinks(); | |
| 355 | + } | |
| 356 | + | |
| 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; | |
| 361 | + } | |
| 362 | + add_filter('woocommerce_get_settings_api', 'mk_admin_api_settings', 10, 2); | |
| 363 | + add_action('woocommerce_get_sections_api', 'mk_admin_add_api_settings'); | |
| 364 | + add_action('woocommerce_settings_saved', 'mk_admin_save_api_settings', 30, 0); | |
| 365 | + add_filter("plugin_action_links_".plugin_basename(__FILE__), 'mk_admin_plugin_settings_link' ); | |
| 366 | + | |
| 367 | + function __get_logo_html() { | |
| 368 | + return '<div class="makecommerce-info">'. | |
| 369 | + '<div class="makecommerce-logo">'. | |
| 370 | + '<a target="_blank" href="http://maksekeskus.ee"><img src="'. plugins_url('/images/makecommerce_logo_en.svg', __FILE__) .'" class="makecommerce-logo"></a>'. | |
| 371 | + '</div>'. | |
| 372 | + '<div class="makecommerce-links">'. | |
| 373 | + '<div class="makecommerce-link"><a target="_blank" href="https://merchant.maksekeskus.ee">Merchant Portal</a></div>'. | |
| 374 | + '<div class="makecommerce-link"><a target="_blank" href="https://makecommerce.net/">makecommerce.net</a></div>'. | |
| 375 | + '<div class="makecommerce-link"><a target="_blank" href="http://maksekeskus.ee">maksekeskus.ee</a></div>'. | |
| 376 | + '</div>'. | |
| 377 | + '</div>'; | |
| 378 | + } | |
| 45 | 379 | } |
| 46 | - | |