PluginProbe
MakeCommerce for WooCommerce / 2.5.11
MakeCommerce for WooCommerce v2.5.11
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.11, at makecommerce.php

512 lines 19.9 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.11
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.0.0
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["lang"])) {
101 //set wpml language
102 if (function_exists('icl_object_id')) {
103 do_action('wpml_switch_language', $_GET["lang"]);
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 $order->payment_complete( $transactionId );
195 // $order->update_status( 'processing' );
196 $woocommerce->cart->empty_cart();
197 break;
198
199 }
200
201 return $returnUrl;
202 }
203
204
205
206
207 $MKAPI = false;
208 function mk_get_api() {
209 global $MKAPI;
210 if ($MKAPI) {
211 //return $MKAPI;
212 }
213 $mk_api_type = get_option('mk_api_type', false);
214 if (!$mk_api_type) {
215 return false;
216 }
217 if ($mk_api_type !== 'live') {
218 $key_prefix = $mk_api_type.'_';
219 } else {
220 $key_prefix = '';
221 }
222 $mk_shop_id = get_option('mk_'.$key_prefix.'shop_id', '');
223 $mk_public_key = get_option('mk_'.$key_prefix.'public_key', '');
224 $mk_private_key = get_option('mk_'.$key_prefix.'private_key', '');
225 if (!$mk_shop_id || !$mk_public_key || !$mk_shop_id) {
226 return false;
227 }
228 $MKAPI = new \Maksekeskus\Maksekeskus($mk_shop_id, $mk_public_key, $mk_private_key, $mk_api_type === 'live' ? false : true);
229 return $MKAPI;
230 }
231
232 function mk_is_api_set() {
233 $mk_api_type = get_option('mk_api_type', false);
234 if (!$mk_api_type) {
235 return false;
236 }
237 if ($mk_api_type !== 'live') {
238 $key_prefix = $mk_api_type.'_';
239 } else {
240 $key_prefix = '';
241 }
242 $mk_shop_id = get_option('mk_'.$key_prefix.'shop_id', '');
243 $mk_public_key = get_option('mk_'.$key_prefix.'public_key', '');
244 $mk_private_key = get_option('mk_'.$key_prefix.'private_key', '');
245 if (!$mk_shop_id || !$mk_public_key || !$mk_shop_id) {
246 return false;
247 }
248 return true;
249 }
250
251
252 function mk_admin_error() {
253 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'));
254 }
255
256
257
258 add_filter('query_vars', 'mk_cron_query_vars');
259 add_action('parse_request', 'mk_parse_cron_update_vars');
260
261 function mk_cron_query_vars($vars) {
262 $vars[] = 'mk-action';
263 $vars[] = 'mk-shop-id';
264 $vars[] = 'mk-test-shop-id';
265 return $vars;
266 }
267
268 function mk_parse_cron_update_vars($wp) {
269 if (array_key_exists('mk-action', $wp->query_vars) && $wp->query_vars['mk-action'] == 'mk-update'
270 && (array_key_exists('mk-shop-id', $wp->query_vars) || array_key_exists('mk-test-shop-id', $wp->query_vars) ) ) {
271 if( (strlen($wp->query_vars['mk-shop-id']) && $wp->query_vars['mk-shop-id'] == get_option('mk_shop_id', false) )
272 || (strlen($wp->query_vars['mk-test-shop-id']) && $wp->query_vars['mk-test-shop-id'] == get_option('mk_test_shop_id', false) ) ) {
273 update_mc_banklinks();
274 exit();
275 }
276 }
277 }
278
279 function mk_admin_login_update($user_login, $user) {
280 if(user_can( $user, 'administrator' )) {
281 update_mc_banklinks();
282 }
283 }
284 add_action('wp_login', 'mk_admin_login_update', 10, 2);
285
286
287
288 function mk_admin_add_api_settings($sections) {
289 $sections['mk_api'] = __('MakeCommerce API access', 'wc_makecommerce_domain');
290 return $sections;
291 }
292
293 function mk_admin_api_settings($settings) {
294 global $current_section;
295 if ($current_section !== 'mk_api') {
296 return $settings;
297 }
298 return array(
299 array('type' => 'title', 'desc' => __get_logo_html()),
300 array(
301 'type' => 'title',
302 'title' => __('MakeCommerce API access credentials', 'wc_makecommerce_domain'),
303 'desc' => __('To use MakeCommerce/Maksekeskus services you need to enter API credentials below here <br/> <br/>', 'wc_makecommerce_domain').
304 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'),
305 'id' => 'mk_api_settings'
306 ),
307 array(
308 'type' => 'select',
309 'title' => __('Current environment', 'wc_makecommerce_domain'),
310 'desc' => __('See more about <a href="https://maksekeskus.ee/en/for-developers/test-environment/">MakeCommerce Test environment</a>', 'wc_makecommerce_domain'),
311 'default' => 'live',
312 'options' => array(
313 'live' => __('Live', 'wc_makecommerce_domain'),
314 'test' => __('Test', 'wc_makecommerce_domain'),
315 ),
316 'id' => 'mk_api_type'
317 ),
318 array(
319 'id' => 'mk_shop_id',
320 'type' => 'text',
321 'title' => __('Shop ID (live)', 'wc_makecommerce_domain'),
322 'desc' => __('Get it from <a href="https://merchant.maksekeskus.ee/api.html" target="_blank">Merchant Portal</a>','wc_makecommerce_domain'),
323 'class' => 'input-text regular-input',
324 ),
325 array(
326 'id' => 'mk_private_key',
327 'type' => 'text',
328 'title' => __('Secret key (live)', 'wc_makecommerce_domain'),
329 'class' => 'input-text regular-input',
330 ),
331 array(
332 'id' => 'mk_public_key',
333 'type' => 'text',
334 'title' => __('Publishable key (live)', 'wc_makecommerce_domain'),
335 'class' => 'input-text regular-input',
336 ),
337 array(
338 'id' => 'mk_test_shop_id',
339 'type' => 'text',
340 'title' => __('Shop ID (test)', 'wc_makecommerce_domain'),
341 'class' => 'input-text regular-input',
342 'default' => 'f64b4f20-5ef9-4b7b-a6fa-d623d87f0b9c',
343 'desc' => __('Get it from <a href="https://merchant-test.maksekeskus.ee/api.html" target="_blank">Merchant Portal Test</a>','wc_makecommerce_domain'),
344 ),
345 array(
346 'id' => 'mk_test_private_key',
347 'type' => 'text',
348 'title' => __('Secret key (test)', 'wc_makecommerce_domain'),
349 'default' => 'MPjcVMoRZPAucsTuGK5ZukOlV7BlzgSvXOowJUkk9IFSQPVooRwcVOxzz3mhEpgM',
350 'class' => 'input-text regular-input',
351 ),
352 array(
353 'id' => 'mk_test_public_key',
354 'type' => 'text',
355 'title' => __('Publishable key (test)', 'wc_makecommerce_domain'),
356 'default' => '7Hog41ci2mKkmtviMycWxpNx14pNP70m',
357 'class' => 'input-text regular-input',
358 ),
359 array('type' => 'sectionend', 'id' => 'mk_api_settings'),
360 array(
361 'type' => 'title',
362 'desc' => '<br><hr><br>',
363 ),
364 array(
365 'id' => 'mk_module_title',
366 'type' => 'title',
367 'title' => __('Makecommerce modules', 'wc_makecommerce_domain'),
368 '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'),
369 'class' => '',
370 ),
371 array(
372 'id' => 'mk_transport_apt_omniva',
373 'type' => 'checkbox',
374 'default' => 'yes',
375 'title' => __('Omniva Parcel Machine', 'wc_makecommerce_domain'),
376 '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')).')',
377 'class' => '',
378 ),
379 array(
380 'id' => 'mk_transport_apt_smartpost',
381 'type' => 'checkbox',
382 'default' => 'yes',
383 'title' => __('SmartPOST Parcel Machine', 'wc_makecommerce_domain'),
384 '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')).')',
385 'class' => '',
386 ),
387 array(
388 'id' => 'mk_transport_apt_dpd',
389 'type' => 'checkbox',
390 'default' => 'no',
391 'title' => __('DPD Pickup Network', 'wc_makecommerce_domain'),
392 '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')).')',
393 'class' => '',
394 ),
395 array(
396 'id' => 'mk_transport_courier_omniva',
397 'type' => 'checkbox',
398 'default' => 'yes',
399 'title' => __('Omniva Courier', 'wc_makecommerce_domain'),
400 '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')).')',
401 'class' => '',
402 ),
403 array(
404 'id' => 'mk_transport_courier_smartpost',
405 'type' => 'checkbox',
406 'default' => 'yes',
407 'title' => __('SmartPOST Courier', 'wc_makecommerce_domain'),
408 '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')).')',
409 'class' => '',
410 ),
411 array(
412 'id' => 'mk_checkout_sco',
413 'type' => 'checkbox',
414 'title' => __('SimpleCheckout', 'wc_makecommerce_domain'),
415 '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')).')',
416 'class' => '',
417 ),
418 array(
419 'id' => 'mk_product_slice',
420 'type' => 'checkbox',
421 'title' => __('Slice in 3 information in product view', 'wc_makecommerce_domain'),
422 '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')).')',
423 'class' => '',
424 ),
425 array(
426 'id' => 'mk_javascript_ui',
427 'type' => 'api_javascript_ui',
428 ),
429 array('type' => 'sectionend', 'id' => 'mk_api_settings')
430 );
431 }
432
433 function mk_wc_version($version) {
434 global $woocommerce;
435 if ( version_compare( $woocommerce->version, "3.4.0", ">=" ) ) {
436 return true;
437 } else {
438 return false;
439 }
440 }
441
442 function mk_admin_save_api_settings() {
443 // reload banklinks
444 global $current_section;
445 if ($current_section !== 'mk_api') {
446 return;
447 }
448 update_mc_banklinks();
449 }
450
451 function mk_admin_plugin_settings_link($links) {
452 if (mk_wc_version("3.4.0"))
453 $settings_link = '<a href="admin.php?page=wc-settings&tab=advanced&section=mk_api">API '.__('Settings').'</a>';
454 else
455 $settings_link = '<a href="admin.php?page=wc-settings&tab=api&section=mk_api">API '.__('Settings').'</a>';
456 array_unshift($links, $settings_link);
457 return $links;
458 }
459
460 function wc_mc_shop_slice_button() {
461 if (get_option('mk_product_slice', 'no') == 'yes') {
462 global $product;
463 // $price_excl_tax = wc_get_price_excluding_tax( $product ); // price without VAT
464 $price_incl_tax = wc_get_price_including_tax( $product ); // price with VAT
465 global $wpdb;
466 global $mkDbTable;
467 $tableName = $wpdb->prefix . $mkDbTable;
468 $methods = $wpdb->get_results('SELECT * FROM '.$tableName);
469 $slice = false;
470 if(count($methods)) {
471 foreach ($methods as $method) {
472 if ($method->name =="slice") {
473 if ( $price_incl_tax >= $method->min_amount ) {
474 if ( $price_incl_tax <= $method->max_amount ) {
475 $slice = true;
476 }
477 }
478 }
479 }
480 }
481 if ($slice == true) {
482 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
483 }
484 }
485 }
486 // Slice Promo 1
487 add_action( 'woocommerce_after_add_to_cart_button', 'wc_mc_shop_slice_button', 20 );
488
489 // if (mk_wc_version("3.4.0")) {
490 add_filter('woocommerce_get_settings_advanced', 'mk_admin_api_settings', 10, 2);
491 add_action('woocommerce_get_sections_advanced', 'mk_admin_add_api_settings');
492 add_filter('woocommerce_get_settings_api', 'mk_admin_api_settings', 10, 2);
493 // } else {
494 add_action('woocommerce_get_sections_api', 'mk_admin_add_api_settings');
495 add_action('woocommerce_settings_saved', 'mk_admin_save_api_settings', 30, 0);
496 add_filter("plugin_action_links_".plugin_basename(__FILE__), 'mk_admin_plugin_settings_link' );
497 // }
498
499 function __get_logo_html() {
500 return '<div class="makecommerce-info">'.
501 '<div class="makecommerce-logo">'.
502 '<a target="_blank" href="http://maksekeskus.ee"><img src="'. plugins_url('/images/makecommerce_logo_en.svg', __FILE__) .'" class="makecommerce-logo"></a>'.
503 '</div>'.
504 '<div class="makecommerce-links">'.
505 '<div class="makecommerce-link"><a target="_blank" href="https://merchant.maksekeskus.ee">Merchant Portal</a></div>'.
506 '<div class="makecommerce-link"><a target="_blank" href="https://makecommerce.net/">makecommerce.net</a></div>'.
507 '<div class="makecommerce-link"><a target="_blank" href="http://maksekeskus.ee">maksekeskus.ee</a></div>'.
508 '</div>'.
509 '</div>';
510 }
511 }
512