PluginProbe
MakeCommerce for WooCommerce / 2.5.14
MakeCommerce for WooCommerce v2.5.14
4.1.0 4.0.8 trunk 1.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 2.0.0 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.2.0 2.2.1 2.2.2 All 96 releases
makecommerce / makecommerce.php

makecommerce.php in MakeCommerce for WooCommerce 2.5.14, at makecommerce.php

511 lines 20.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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: 2.5.14
6 Author: Maksekeskus AS
7 Author URI: https://MakeCommerce.net/
8 Text Domain: wc_makecommerce_domain
9 WC requires at least: 3.0
10 WC tested up to: 4.3.1
11 */
12
13 if (!defined('ABSPATH')) {
14 exit; // Exit if accessed directly.
15 }
16
17 include_once(ABSPATH.'wp-admin/includes/plugin.php');
18 if (is_plugin_active('woocommerce/woocommerce.php')) {
19
20 define('WC_MC_VERSION', 2.0);
21
22 $enable_experimental = get_option('mk_checkout_sco', 'no');
23 if ($enable_experimental === 'yes') {
24 require_once(__DIR__.'/makecommerce-simplecheckout.php');
25 }
26 require_once(__DIR__.'/makecommerce-transport.php');
27 require_once(__DIR__.'/makecommerce-payment.php');
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
38 global $mkDbTable;
39 $mkDbTable = 'mc_banklinks';
40
41 function mc_install() {
42 global $wpdb;
43 global $mkDbTable;
44
45 $tableName = $wpdb->prefix . $mkDbTable;
46
47 $charset = $wpdb->get_charset_collate();
48
49 $sql = "CREATE TABLE $tableName (
50 id mediumint(9) NOT NULL AUTO_INCREMENT,
51 type varchar(10) NOT NULL,
52 country char(2) NOT NULL,
53 name varchar(25) NOT NULL,
54 url varchar(250) NOT NULL,
55 logo_url varchar(250),
56 min_amount mediumint(9),
57 max_amount mediumint(9),
58 UNIQUE KEY id (id)
59 ) $charset;";
60
61 require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
62 dbDelta( $sql );
63 }
64
65 register_activation_hook( __FILE__, 'mc_install' );
66
67 // On uninstall remove scheduled hook
68 function mc_uninstall() {
69 if (wp_next_scheduled ( 'mc_banklinks_update_cron' )) {
70 wp_clear_scheduled_hook('mc_banklinks_update_cron');
71 }
72 }
73
74 register_deactivation_hook(__FILE__, 'mc_uninstall');
75
76 // WP schedule callable action
77 add_action('mc_banklinks_update_cron', 'update_mc_banklinks');
78 // Assign schedule if not set, update compatible no need to reactivate, format('U') php 5.2 compatible
79 // Assigns task at 23.59.59 by local time, task runs if time has passed and somebody navigates the site
80 if (! wp_next_scheduled ( 'mc_banklinks_update_cron' )) {
81 $date = new DateTime();
82 $date->setTime(rand(0,23),rand(0,59),00);
83 $timestamp = intval($date->format('U'));
84 //$local = 3600 * intval(get_option('gmt_offset'));
85 //wp_schedule_event($timestamp - $local, 'twicedaily', 'mc_banklinks_update_cron');
86 wp_schedule_event($timestamp, 'twicedaily', 'mc_banklinks_update_cron');
87 }
88
89 if (!class_exists('wc_makecommerce_domain')) {
90 require_once('includes/vendor/autoload.php');
91 require_once('includes/Maksekeskus.php');
92 }
93
94 function mk_check_payment() {
95
96 global $woocommerce;
97 $api = mk_get_api();
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
112 $returnUrl = home_url();
113
114 $request = stripslashes_deep($_POST);
115 if ($api->verifyMac($request)) {
116 $data = $api->extractRequestData($request);
117 $reference = false;
118
119 if (!empty($data['error']) ) {
120 wc_add_notice(__('Payment failed', 'wc_makecommerce_domain'), 'error');
121 return $returnUrl;
122 }
123
124 if (!empty($data['message_type']) ) {
125 if ($data['message_type'] == 'payment_return') {
126 $transactionId = $data['transaction'];
127 $reference = $data['reference'];
128 $paymentStatus = $data['status'];
129 }
130 if ($data['message_type'] == 'token_return') {
131 if (!empty($data['transaction']['reference'])) {
132 $reference = $data['transaction']['reference'];
133 }
134 $paymentStatus = $data['transaction']['status'];
135 $transactionId = $data['transaction']['id'];
136
137 // for API backward compatibility - if we didn't find reference in the mesage, or the transaction state vas still PENDING
138 // we'll fetch the transaction data from API, and make a payment request if needed
139 // this all can be removed after 2017-jan
140 if (!$reference or $paymentStatus == 'PENDING') {
141 $transaction = $api->getTransaction($transactionId);
142 if (!empty($transaction->reference)) {
143 $reference = $transaction->reference;
144 }
145
146 $paymentStatus = $transaction->status;
147
148 if ($paymentStatus == 'PENDING') {
149 $paymentRequest = array(
150 'amount' => (string)sprintf("%.2f", $transaction->amount),
151 'currency' => $transaction->currency,
152 'token' => $data['token']['id']
153 );
154 $payment = $api->createPayment($transactionId, $paymentRequest);
155 // TODO: validate
156 $paymentStatus = $payment->transaction->status;
157 }
158
159 }
160
161
162 }
163
164 }
165 }
166
167 // if we didn't find reference to an Order, we send user to shop landing-page. TODO: could be improved
168 if (!$reference) { return $returnUrl; }
169
170 $order = new WC_Order($reference);
171 // if we din't find the Order, we send user to shop landing-page. TODO: could be improved
172 if (!$order->get_id()) { return $returnUrl; }
173
174 $returnUrl = $order->get_checkout_order_received_url();
175
176 switch($paymentStatus) {
177
178 case 'CANCELLED':
179 $order->update_status( 'cancelled' );
180 wc_add_notice(__('Payment transaction cancelled', 'wc_makecommerce_domain'), 'error');
181 $returnUrl = wc_get_cart_url();
182 break;
183
184 case 'COMPLETED':
185 $orderNote = array();
186 $orderNote[] = __('Transaction ID', 'wc_makecommerce_domain') . ': <a target=_blank href="'.$api->getEnvUrls()->merchantUrl.'/merchant/shop/deals/detail.html?id='. $transactionId .'">'.$transactionId.'</a>';
187 $orderNote[] = __('Payment option', 'wc_makecommerce_domain') . ': ' . get_post_meta($order->get_id(), '_makecommerce_preselected_method', true);
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 }
193
194 if( isset($_GET['lang1']) ) $order->update_meta_data('wpml_language', $_GET["lang1"]);
195 $order->payment_complete( $transactionId );
196 $woocommerce->cart->empty_cart();
197 break;
198 }
199
200 return $returnUrl;
201 }
202
203
204
205
206 $MKAPI = false;
207 function mk_get_api() {
208 global $MKAPI;
209 if ($MKAPI) {
210 //return $MKAPI;
211 }
212 $mk_api_type = get_option('mk_api_type', false);
213 if (!$mk_api_type) {
214 return false;
215 }
216 if ($mk_api_type !== 'live') {
217 $key_prefix = $mk_api_type.'_';
218 } else {
219 $key_prefix = '';
220 }
221 $mk_shop_id = get_option('mk_'.$key_prefix.'shop_id', '');
222 $mk_public_key = get_option('mk_'.$key_prefix.'public_key', '');
223 $mk_private_key = get_option('mk_'.$key_prefix.'private_key', '');
224 if (!$mk_shop_id || !$mk_public_key || !$mk_shop_id) {
225 return false;
226 }
227 $MKAPI = new \Maksekeskus\Maksekeskus($mk_shop_id, $mk_public_key, $mk_private_key, $mk_api_type === 'live' ? false : true);
228 return $MKAPI;
229 }
230
231 function mk_is_api_set() {
232 $mk_api_type = get_option('mk_api_type', false);
233 if (!$mk_api_type) {
234 return false;
235 }
236 if ($mk_api_type !== 'live') {
237 $key_prefix = $mk_api_type.'_';
238 } else {
239 $key_prefix = '';
240 }
241 $mk_shop_id = get_option('mk_'.$key_prefix.'shop_id', '');
242 $mk_public_key = get_option('mk_'.$key_prefix.'public_key', '');
243 $mk_private_key = get_option('mk_'.$key_prefix.'private_key', '');
244 if (!$mk_shop_id || !$mk_public_key || !$mk_shop_id) {
245 return false;
246 }
247 return true;
248 }
249
250
251 function mk_admin_error() {
252 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'));
253 }
254
255
256
257 add_filter('query_vars', 'mk_cron_query_vars');
258 add_action('parse_request', 'mk_parse_cron_update_vars');
259
260 function mk_cron_query_vars($vars) {
261 $vars[] = 'mk-action';
262 $vars[] = 'mk-shop-id';
263 $vars[] = 'mk-test-shop-id';
264 return $vars;
265 }
266
267 function mk_parse_cron_update_vars($wp) {
268 if (array_key_exists('mk-action', $wp->query_vars) && $wp->query_vars['mk-action'] == 'mk-update'
269 && (array_key_exists('mk-shop-id', $wp->query_vars) || array_key_exists('mk-test-shop-id', $wp->query_vars) ) ) {
270 if( (strlen($wp->query_vars['mk-shop-id']) && $wp->query_vars['mk-shop-id'] == get_option('mk_shop_id', false) )
271 || (strlen($wp->query_vars['mk-test-shop-id']) && $wp->query_vars['mk-test-shop-id'] == get_option('mk_test_shop_id', false) ) ) {
272 update_mc_banklinks();
273 exit();
274 }
275 }
276 }
277
278 function mk_admin_login_update($user_login, $user) {
279 if(user_can( $user, 'administrator' )) {
280 update_mc_banklinks();
281 }
282 }
283 add_action('wp_login', 'mk_admin_login_update', 10, 2);
284
285
286
287 function mk_admin_add_api_settings($sections) {
288 $sections['mk_api'] = __('MakeCommerce API access', 'wc_makecommerce_domain');
289 return $sections;
290 }
291
292 function mk_admin_api_settings($settings) {
293 global $current_section;
294 if ($current_section !== 'mk_api') {
295 return $settings;
296 }
297 return array(
298 array('type' => 'title', 'desc' => __get_logo_html()),
299 array(
300 'type' => 'title',
301 'title' => __('MakeCommerce API access credentials', 'wc_makecommerce_domain'),
302 'desc' => __('To use MakeCommerce/Maksekeskus services you need to enter API credentials below here <br/> <br/>', 'wc_makecommerce_domain').
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&section=makecommerce'),
304 'id' => 'mk_api_settings'
305 ),
306 array(
307 'type' => 'select',
308 'title' => __('Current environment', 'wc_makecommerce_domain'),
309 'desc' => __('See more about <a href="https://maksekeskus.ee/en/for-developers/test-environment/">MakeCommerce Test environment</a>', 'wc_makecommerce_domain'),
310 'default' => 'live',
311 'options' => array(
312 'live' => __('Live', 'wc_makecommerce_domain'),
313 'test' => __('Test', 'wc_makecommerce_domain'),
314 ),
315 'id' => 'mk_api_type'
316 ),
317 array(
318 'id' => 'mk_shop_id',
319 'type' => 'text',
320 'title' => __('Shop ID (live)', 'wc_makecommerce_domain'),
321 'desc' => __('Get it from <a href="https://merchant.maksekeskus.ee/api.html" target="_blank">Merchant Portal</a>','wc_makecommerce_domain'),
322 'class' => 'input-text regular-input',
323 ),
324 array(
325 'id' => 'mk_private_key',
326 'type' => 'text',
327 'title' => __('Secret key (live)', 'wc_makecommerce_domain'),
328 'class' => 'input-text regular-input',
329 ),
330 array(
331 'id' => 'mk_public_key',
332 'type' => 'text',
333 'title' => __('Publishable key (live)', 'wc_makecommerce_domain'),
334 'class' => 'input-text regular-input',
335 ),
336 array(
337 'id' => 'mk_test_shop_id',
338 'type' => 'text',
339 'title' => __('Shop ID (test)', 'wc_makecommerce_domain'),
340 'class' => 'input-text regular-input',
341 'default' => 'f64b4f20-5ef9-4b7b-a6fa-d623d87f0b9c',
342 'desc' => __('Get it from <a href="https://merchant-test.maksekeskus.ee/api.html" target="_blank">Merchant Portal Test</a>','wc_makecommerce_domain'),
343 ),
344 array(
345 'id' => 'mk_test_private_key',
346 'type' => 'text',
347 'title' => __('Secret key (test)', 'wc_makecommerce_domain'),
348 'default' => 'MPjcVMoRZPAucsTuGK5ZukOlV7BlzgSvXOowJUkk9IFSQPVooRwcVOxzz3mhEpgM',
349 'class' => 'input-text regular-input',
350 ),
351 array(
352 'id' => 'mk_test_public_key',
353 'type' => 'text',
354 'title' => __('Publishable key (test)', 'wc_makecommerce_domain'),
355 'default' => '7Hog41ci2mKkmtviMycWxpNx14pNP70m',
356 'class' => 'input-text regular-input',
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 ),
370 array(
371 'id' => 'mk_transport_apt_omniva',
372 'type' => 'checkbox',
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&section=parcelmachine_omniva')).')',
376 'class' => '',
377 ),
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&section=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&section=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&section=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&section=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&trade; Checkout method', 'wc_makecommerce_domain').' ('. sprintf(__('<a href="%s">module settings</a>', 'wc_makecommerce_domain'), admin_url('admin.php?page=wc-settings&tab=checkout&section=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&section=makecommerce_sc')).')',
422 'class' => '',
423 ),
424 array(
425 'id' => 'mk_javascript_ui',
426 'type' => 'api_javascript_ui',
427 ),
428 array('type' => 'sectionend', 'id' => 'mk_api_settings')
429 );
430 }
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
441 function mk_admin_save_api_settings() {
442 // reload banklinks
443 global $current_section;
444 if ($current_section !== 'mk_api') {
445 return;
446 }
447 update_mc_banklinks();
448 }
449
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&section=mk_api">API '.__('Settings').'</a>';
453 else
454 $settings_link = '<a href="admin.php?page=wc-settings&tab=api&section=mk_api">API '.__('Settings').'</a>';
455 array_unshift($links, $settings_link);
456 return $links;
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"/>&nbsp;&nbsp;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');
491 add_filter('woocommerce_get_settings_api', 'mk_admin_api_settings', 10, 2);
492 // } else {
493 add_action('woocommerce_get_sections_api', 'mk_admin_add_api_settings');
494 add_action('woocommerce_settings_saved', 'mk_admin_save_api_settings', 30, 0);
495 add_filter("plugin_action_links_".plugin_basename(__FILE__), 'mk_admin_plugin_settings_link' );
496 // }
497
498 function __get_logo_html() {
499 return '<div class="makecommerce-info">'.
500 '<div class="makecommerce-logo">'.
501 '<a target="_blank" href="http://maksekeskus.ee"><img src="'. plugins_url('/images/makecommerce_logo_en.svg', __FILE__) .'" class="makecommerce-logo"></a>'.
502 '</div>'.
503 '<div class="makecommerce-links">'.
504 '<div class="makecommerce-link"><a target="_blank" href="https://merchant.maksekeskus.ee">Merchant Portal</a></div>'.
505 '<div class="makecommerce-link"><a target="_blank" href="https://makecommerce.net/">makecommerce.net</a></div>'.
506 '<div class="makecommerce-link"><a target="_blank" href="http://maksekeskus.ee">maksekeskus.ee</a></div>'.
507 '</div>'.
508 '</div>';
509 }
510 }
511