PluginProbe
MakeCommerce for WooCommerce / 2.5.9
MakeCommerce for WooCommerce v2.5.9
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.9, at makecommerce.php

481 lines 18.8 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.9
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 $returnUrl = home_url();
100
101 $request = stripslashes_deep($_POST);
102 if ($api->verifyMac($request)) {
103 $data = $api->extractRequestData($request);
104 $reference = false;
105
106 if (!empty($data['error']) ) {
107 wc_add_notice(__('Payment failed', 'wc_makecommerce_domain'), 'error');
108 return $returnUrl;
109 }
110
111 if (!empty($data['message_type']) ) {
112 if ($data['message_type'] == 'payment_return') {
113 $transactionId = $data['transaction'];
114 $reference = $data['reference'];
115 $paymentStatus = $data['status'];
116 }
117 if ($data['message_type'] == 'token_return') {
118 if (!empty($data['transaction']['reference'])) {
119 $reference = $data['transaction']['reference'];
120 }
121 $paymentStatus = $data['transaction']['status'];
122 $transactionId = $data['transaction']['id'];
123
124 // for API backward compatibility - if we didn't find reference in the mesage, or the transaction state vas still PENDING
125 // we'll fetch the transaction data from API, and make a payment request if needed
126 // this all can be removed after 2017-jan
127 if (!$reference or $paymentStatus == 'PENDING') {
128 $transaction = $api->getTransaction($transactionId);
129 if (!empty($transaction->reference)) {
130 $reference = $transaction->reference;
131 }
132
133 $paymentStatus = $transaction->status;
134
135 if ($paymentStatus == 'PENDING') {
136 $paymentRequest = array(
137 'amount' => (string)sprintf("%.2f", $transaction->amount),
138 'currency' => $transaction->currency,
139 'token' => $data['token']['id']
140 );
141 $payment = $api->createPayment($transactionId, $paymentRequest);
142 // TODO: validate
143 $paymentStatus = $payment->transaction->status;
144 }
145
146 }
147
148
149 }
150
151 }
152 }
153
154 // if we didn't find reference to an Order, we send user to shop landing-page. TODO: could be improved
155 if (!$reference) { return $returnUrl; }
156
157 $order = new WC_Order($reference);
158 // if we din't find the Order, we send user to shop landing-page. TODO: could be improved
159 if (!$order->get_id()) { return $returnUrl; }
160
161 $returnUrl = $order->get_checkout_order_received_url();
162
163 switch($paymentStatus) {
164
165 case 'CANCELLED':
166 $order->update_status( 'cancelled' );
167 wc_add_notice(__('Payment transaction cancelled', 'wc_makecommerce_domain'), 'error');
168 $returnUrl = wc_get_cart_url();
169 break;
170
171 case 'COMPLETED':
172 $orderNote = array();
173 $orderNote[] = __('Transaction ID', 'wc_makecommerce_domain') . ': <a target=_blank href="'.$api->getEnvUrls()->merchantUrl.'/merchant/shop/deals/detail.html?id='. $transactionId .'">'.$transactionId.'</a>';
174 $orderNote[] = __('Payment option', 'wc_makecommerce_domain') . ': ' . get_post_meta($order->get_id(), '_makecommerce_preselected_method', true);
175 $order->add_order_note(implode("\r\n", $orderNote));
176 if (!empty($data['token']) && !empty($data['token']['multiuse'])) {
177 update_post_meta($order->get_id(), '_makecommerce_payment_token', $data['token']['id']);
178 update_post_meta($order->get_id(), '_makecommerce_payment_token_valid_until', $data['token']['valid_until']);
179 }
180
181 $order->payment_complete( $transactionId );
182 // $order->update_status( 'processing' );
183 $woocommerce->cart->empty_cart();
184 break;
185
186 }
187
188 return $returnUrl;
189 }
190
191
192
193
194 $MKAPI = false;
195 function mk_get_api() {
196 global $MKAPI;
197 if ($MKAPI) {
198 //return $MKAPI;
199 }
200 $mk_api_type = get_option('mk_api_type', false);
201 if (!$mk_api_type) {
202 return false;
203 }
204 if ($mk_api_type !== 'live') {
205 $key_prefix = $mk_api_type.'_';
206 } else {
207 $key_prefix = '';
208 }
209 $mk_shop_id = get_option('mk_'.$key_prefix.'shop_id', '');
210 $mk_public_key = get_option('mk_'.$key_prefix.'public_key', '');
211 $mk_private_key = get_option('mk_'.$key_prefix.'private_key', '');
212 if (!$mk_shop_id || !$mk_public_key || !$mk_shop_id) {
213 return false;
214 }
215 $MKAPI = new \Maksekeskus\Maksekeskus($mk_shop_id, $mk_public_key, $mk_private_key, $mk_api_type === 'live' ? false : true);
216 return $MKAPI;
217 }
218
219 function mk_is_api_set() {
220 $mk_api_type = get_option('mk_api_type', false);
221 if (!$mk_api_type) {
222 return false;
223 }
224 if ($mk_api_type !== 'live') {
225 $key_prefix = $mk_api_type.'_';
226 } else {
227 $key_prefix = '';
228 }
229 $mk_shop_id = get_option('mk_'.$key_prefix.'shop_id', '');
230 $mk_public_key = get_option('mk_'.$key_prefix.'public_key', '');
231 $mk_private_key = get_option('mk_'.$key_prefix.'private_key', '');
232 if (!$mk_shop_id || !$mk_public_key || !$mk_shop_id) {
233 return false;
234 }
235 return true;
236 }
237
238
239 function mk_admin_error() {
240 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'));
241 }
242
243
244
245 add_filter('query_vars', 'mk_cron_query_vars');
246 add_action('parse_request', 'mk_parse_cron_update_vars');
247
248 function mk_cron_query_vars($vars) {
249 $vars[] = 'mk-action';
250 $vars[] = 'mk-shop-id';
251 $vars[] = 'mk-test-shop-id';
252 return $vars;
253 }
254
255 function mk_parse_cron_update_vars($wp) {
256 if (array_key_exists('mk-action', $wp->query_vars) && $wp->query_vars['mk-action'] == 'mk-update'
257 && (array_key_exists('mk-shop-id', $wp->query_vars) || array_key_exists('mk-test-shop-id', $wp->query_vars) ) ) {
258 if( (strlen($wp->query_vars['mk-shop-id']) && $wp->query_vars['mk-shop-id'] == get_option('mk_shop_id', false) )
259 || (strlen($wp->query_vars['mk-test-shop-id']) && $wp->query_vars['mk-test-shop-id'] == get_option('mk_test_shop_id', false) ) ) {
260 update_mc_banklinks();
261 exit();
262 }
263 }
264 }
265
266 function mk_admin_login_update($user_login, $user) {
267 if(user_can( $user, 'administrator' )) {
268 update_mc_banklinks();
269 }
270 }
271 add_action('wp_login', 'mk_admin_login_update', 10, 2);
272
273
274
275 function mk_admin_add_api_settings($sections) {
276 $sections['mk_api'] = __('MakeCommerce API access', 'wc_makecommerce_domain');
277 return $sections;
278 }
279
280 function mk_admin_api_settings($settings) {
281 global $current_section;
282 if ($current_section !== 'mk_api') {
283 return $settings;
284 }
285 return array(
286 array('type' => 'title', 'desc' => __get_logo_html()),
287 array(
288 'type' => 'title',
289 'title' => __('MakeCommerce API access credentials', 'wc_makecommerce_domain'),
290 'desc' => __('To use MakeCommerce/Maksekeskus services you need to enter API credentials below here <br/> <br/>', 'wc_makecommerce_domain').
291 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'),
292 'id' => 'mk_api_settings'
293 ),
294 array(
295 'type' => 'select',
296 'title' => __('Current environment', 'wc_makecommerce_domain'),
297 'desc' => __('See more about <a href="https://maksekeskus.ee/en/for-developers/test-environment/">MakeCommerce Test environment</a>', 'wc_makecommerce_domain'),
298 'default' => 'live',
299 'options' => array(
300 'live' => __('Live', 'wc_makecommerce_domain'),
301 'test' => __('Test', 'wc_makecommerce_domain'),
302 ),
303 'id' => 'mk_api_type'
304 ),
305 array(
306 'id' => 'mk_shop_id',
307 'type' => 'text',
308 'title' => __('Shop ID (live)', 'wc_makecommerce_domain'),
309 'desc' => __('Get it from <a href="https://merchant.maksekeskus.ee/api.html" target="_blank">Merchant Portal</a>','wc_makecommerce_domain'),
310 'class' => 'input-text regular-input',
311 ),
312 array(
313 'id' => 'mk_private_key',
314 'type' => 'text',
315 'title' => __('Secret key (live)', 'wc_makecommerce_domain'),
316 'class' => 'input-text regular-input',
317 ),
318 array(
319 'id' => 'mk_public_key',
320 'type' => 'text',
321 'title' => __('Publishable key (live)', 'wc_makecommerce_domain'),
322 'class' => 'input-text regular-input',
323 ),
324 array(
325 'id' => 'mk_test_shop_id',
326 'type' => 'text',
327 'title' => __('Shop ID (test)', 'wc_makecommerce_domain'),
328 'class' => 'input-text regular-input',
329 'default' => 'f64b4f20-5ef9-4b7b-a6fa-d623d87f0b9c',
330 'desc' => __('Get it from <a href="https://merchant-test.maksekeskus.ee/api.html" target="_blank">Merchant Portal Test</a>','wc_makecommerce_domain'),
331 ),
332 array(
333 'id' => 'mk_test_private_key',
334 'type' => 'text',
335 'title' => __('Secret key (test)', 'wc_makecommerce_domain'),
336 'default' => 'MPjcVMoRZPAucsTuGK5ZukOlV7BlzgSvXOowJUkk9IFSQPVooRwcVOxzz3mhEpgM',
337 'class' => 'input-text regular-input',
338 ),
339 array(
340 'id' => 'mk_test_public_key',
341 'type' => 'text',
342 'title' => __('Publishable key (test)', 'wc_makecommerce_domain'),
343 'default' => '7Hog41ci2mKkmtviMycWxpNx14pNP70m',
344 'class' => 'input-text regular-input',
345 ),
346 array('type' => 'sectionend', 'id' => 'mk_api_settings'),
347 array(
348 'type' => 'title',
349 'desc' => '<br><hr><br>',
350 ),
351 array(
352 'id' => 'mk_module_title',
353 'type' => 'title',
354 'title' => __('Makecommerce modules', 'wc_makecommerce_domain'),
355 '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'),
356 'class' => '',
357 ),
358 array(
359 'id' => 'mk_transport_apt_omniva',
360 'type' => 'checkbox',
361 'default' => 'yes',
362 'title' => __('Omniva Parcel Machine', 'wc_makecommerce_domain'),
363 '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')).')',
364 'class' => '',
365 ),
366 array(
367 'id' => 'mk_transport_apt_smartpost',
368 'type' => 'checkbox',
369 'default' => 'yes',
370 'title' => __('SmartPOST Parcel Machine', 'wc_makecommerce_domain'),
371 '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')).')',
372 'class' => '',
373 ),
374 array(
375 'id' => 'mk_transport_apt_dpd',
376 'type' => 'checkbox',
377 'default' => 'no',
378 'title' => __('DPD Pickup Network', 'wc_makecommerce_domain'),
379 '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')).')',
380 'class' => '',
381 ),
382 array(
383 'id' => 'mk_transport_courier_omniva',
384 'type' => 'checkbox',
385 'default' => 'yes',
386 'title' => __('Omniva Courier', 'wc_makecommerce_domain'),
387 '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')).')',
388 'class' => '',
389 ),
390 array(
391 'id' => 'mk_transport_courier_smartpost',
392 'type' => 'checkbox',
393 'default' => 'yes',
394 'title' => __('SmartPOST Courier', 'wc_makecommerce_domain'),
395 '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')).')',
396 'class' => '',
397 ),
398 array(
399 'id' => 'mk_checkout_sco',
400 'type' => 'checkbox',
401 'title' => __('SimpleCheckout', 'wc_makecommerce_domain'),
402 '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')).')',
403 'class' => '',
404 ),
405 array(
406 'id' => 'mk_product_slice',
407 'type' => 'checkbox',
408 'title' => __('Slice in 3 information in product view', 'wc_makecommerce_domain'),
409 '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')).')',
410 'class' => '',
411 ),
412 array(
413 'id' => 'mk_javascript_ui',
414 'type' => 'api_javascript_ui',
415 ),
416 array('type' => 'sectionend', 'id' => 'mk_api_settings')
417 );
418 }
419
420 function mk_wc_version($version) {
421 global $woocommerce;
422 if ( version_compare( $woocommerce->version, "3.4.0", ">=" ) ) {
423 return true;
424 } else {
425 return false;
426 }
427 }
428
429 function mk_admin_save_api_settings() {
430 // reload banklinks
431 global $current_section;
432 if ($current_section !== 'mk_api') {
433 return;
434 }
435 update_mc_banklinks();
436 }
437
438 function mk_admin_plugin_settings_link($links) {
439 if (mk_wc_version("3.4.0"))
440 $settings_link = '<a href="admin.php?page=wc-settings&tab=advanced&section=mk_api">API '.__('Settings').'</a>';
441 else
442 $settings_link = '<a href="admin.php?page=wc-settings&tab=api&section=mk_api">API '.__('Settings').'</a>';
443 array_unshift($links, $settings_link);
444 return $links;
445 }
446
447 function wc_mc_shop_slice_button() {
448 if (get_option('mk_product_slice', 'yes') == 'yes') {
449 global $product;
450 // $price_excl_tax = wc_get_price_excluding_tax( $product ); // price without VAT
451 $price_incl_tax = wc_get_price_including_tax( $product ); // price with VAT
452 echo '<br><br><div style="width:100%; margin-top: auto; margin-bottom: auto;"><img src="'.plugins_url('/images/slice_logo_dark_176x62.svg', __FILE__).'" width="88" weight="31"/>&nbsp;&nbsp;'.__('Buy now, pay later. In 3 equal parts', 'wc_makecommerce_domain').' 3 x '.sprintf("%.2f",$price_incl_tax/3).' €</div>';
453 }
454 }
455 // Slice Promo 1
456 add_action( 'woocommerce_after_add_to_cart_button', 'wc_mc_shop_slice_button', 20 );
457
458 // if (mk_wc_version("3.4.0")) {
459 add_filter('woocommerce_get_settings_advanced', 'mk_admin_api_settings', 10, 2);
460 add_action('woocommerce_get_sections_advanced', 'mk_admin_add_api_settings');
461 add_filter('woocommerce_get_settings_api', 'mk_admin_api_settings', 10, 2);
462 // } else {
463 add_action('woocommerce_get_sections_api', 'mk_admin_add_api_settings');
464 add_action('woocommerce_settings_saved', 'mk_admin_save_api_settings', 30, 0);
465 add_filter("plugin_action_links_".plugin_basename(__FILE__), 'mk_admin_plugin_settings_link' );
466 // }
467
468 function __get_logo_html() {
469 return '<div class="makecommerce-info">'.
470 '<div class="makecommerce-logo">'.
471 '<a target="_blank" href="http://maksekeskus.ee"><img src="'. plugins_url('/images/makecommerce_logo_en.svg', __FILE__) .'" class="makecommerce-logo"></a>'.
472 '</div>'.
473 '<div class="makecommerce-links">'.
474 '<div class="makecommerce-link"><a target="_blank" href="https://merchant.maksekeskus.ee">Merchant Portal</a></div>'.
475 '<div class="makecommerce-link"><a target="_blank" href="https://makecommerce.net/">makecommerce.net</a></div>'.
476 '<div class="makecommerce-link"><a target="_blank" href="http://maksekeskus.ee">maksekeskus.ee</a></div>'.
477 '</div>'.
478 '</div>';
479 }
480 }
481