PluginProbe
MakeCommerce for WooCommerce / 2.5.5
MakeCommerce for WooCommerce v2.5.5
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.5, at makecommerce.php

460 lines 17.6 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.5
6 Author: Maksekeskus AS
7 Author URI: https://MakeCommerce.net/
8 Text Domain: wc_makecommerce_domain
9 */
10
11 if (!defined('ABSPATH')) {
12 exit; // Exit if accessed directly.
13 }
14
15 include_once(ABSPATH.'wp-admin/includes/plugin.php');
16 if (is_plugin_active('woocommerce/woocommerce.php')) {
17
18 define('WC_MC_VERSION', 2.0);
19
20 $enable_experimental = get_option('mk_checkout_sco', 'no');
21 if ($enable_experimental === 'yes') {
22 require_once(__DIR__.'/makecommerce-simplecheckout.php');
23 }
24 require_once(__DIR__.'/makecommerce-transport.php');
25 require_once(__DIR__.'/makecommerce-payment.php');
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
36 global $mkDbTable;
37 $mkDbTable = 'mc_banklinks';
38
39 function mc_install() {
40 global $wpdb;
41 global $mkDbTable;
42
43 $tableName = $wpdb->prefix . $mkDbTable;
44
45 $charset = $wpdb->get_charset_collate();
46
47 $sql = "CREATE TABLE $tableName (
48 id mediumint(9) NOT NULL AUTO_INCREMENT,
49 type varchar(10) NOT NULL,
50 country char(2) NOT NULL,
51 name varchar(25) NOT NULL,
52 url varchar(250) NOT NULL,
53 logo_url varchar(250),
54 min_amount mediumint(9),
55 max_amount mediumint(9),
56 UNIQUE KEY id (id)
57 ) $charset;";
58
59 require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
60 dbDelta( $sql );
61 }
62
63 register_activation_hook( __FILE__, 'mc_install' );
64
65 // On uninstall remove scheduled hook
66 function mc_uninstall() {
67 if (wp_next_scheduled ( 'mc_banklinks_update_cron' )) {
68 wp_clear_scheduled_hook('mc_banklinks_update_cron');
69 }
70 }
71
72 register_deactivation_hook(__FILE__, 'mc_uninstall');
73
74 // WP schedule callable action
75 add_action('mc_banklinks_update_cron', 'update_mc_banklinks');
76 // Assign schedule if not set, update compatible no need to reactivate, format('U') php 5.2 compatible
77 // Assigns task at 23.59.59 by local time, task runs if time has passed and somebody navigates the site
78 if (! wp_next_scheduled ( 'mc_banklinks_update_cron' )) {
79 $date = new DateTime();
80 $date->setTime(04,10,00);
81 $timestamp = intval($date->format('U'));
82 //$local = 3600 * intval(get_option('gmt_offset'));
83 //wp_schedule_event($timestamp - $local, 'twicedaily', 'mc_banklinks_update_cron');
84 wp_schedule_event($timestamp, 'twicedaily', 'mc_banklinks_update_cron');
85 }
86
87 if (!class_exists('wc_makecommerce_domain')) {
88 require_once('includes/vendor/autoload.php');
89 require_once('includes/Maksekeskus.php');
90 }
91
92 function mk_check_payment() {
93
94 global $woocommerce;
95 $api = mk_get_api();
96
97 $returnUrl = home_url();
98
99 $request = stripslashes_deep($_POST);
100 if ($api->verifyMac($request)) {
101 $data = $api->extractRequestData($request);
102 $reference = false;
103
104 if (!empty($data['error']) ) {
105 wc_add_notice(__('Payment failed', 'wc_makecommerce_domain'), 'error');
106 return $returnUrl;
107 }
108
109 if (!empty($data['message_type']) ) {
110 if ($data['message_type'] == 'payment_return') {
111 $transactionId = $data['transaction'];
112 $reference = $data['reference'];
113 $paymentStatus = $data['status'];
114 }
115 if ($data['message_type'] == 'token_return') {
116 if (!empty($data['transaction']['reference'])) {
117 $reference = $data['transaction']['reference'];
118 }
119 $paymentStatus = $data['transaction']['status'];
120 $transactionId = $data['transaction']['id'];
121
122 // for API backward compatibility - if we didn't find reference in the mesage, or the transaction state vas still PENDING
123 // we'll fetch the transaction data from API, and make a payment request if needed
124 // this all can be removed after 2017-jan
125 if (!$reference or $paymentStatus == 'PENDING') {
126 $transaction = $api->getTransaction($transactionId);
127 if (!empty($transaction->reference)) {
128 $reference = $transaction->reference;
129 }
130
131 $paymentStatus = $transaction->status;
132
133 if ($paymentStatus == 'PENDING') {
134 $paymentRequest = array(
135 'amount' => (string)sprintf("%.2f", $transaction->amount),
136 'currency' => $transaction->currency,
137 'token' => $data['token']['id']
138 );
139 $payment = $api->createPayment($transactionId, $paymentRequest);
140 // TODO: validate
141 $paymentStatus = $payment->transaction->status;
142 }
143
144 }
145
146
147 }
148
149 }
150 }
151
152 // if we didn't find reference to an Order, we send user to shop landing-page. TODO: could be improved
153 if (!$reference) { return $returnUrl; }
154
155 $order = new WC_Order($reference);
156 // if we din't find the Order, we send user to shop landing-page. TODO: could be improved
157 if (!$order->get_order_number()) { return $returnUrl; }
158
159 $returnUrl = $order->get_checkout_order_received_url();
160
161 switch($paymentStatus) {
162
163 case 'CANCELLED':
164 $order->update_status( 'cancelled' );
165 wc_add_notice(__('Payment transaction cancelled', 'wc_makecommerce_domain'), 'error');
166 $returnUrl = $woocommerce->cart->get_cart_url();
167 break;
168
169 case 'COMPLETED':
170 $orderNote = array();
171 $orderNote[] = __('Transaction ID', 'wc_makecommerce_domain') . ': <a target=_blank href="'.$api->getEnvUrls()->merchantUrl.'/merchant/shop/deals/detail.html?id='. $transactionId .'">'.$transactionId.'</a>';
172 $orderNote[] = __('Payment option', 'wc_makecommerce_domain') . ': ' . get_post_meta($order->get_order_number(), '_makecommerce_preselected_method', true);
173 $order->add_order_note(implode("\r\n", $orderNote));
174 if (!empty($data['token']) && !empty($data['token']['multiuse'])) {
175 update_post_meta($order->get_order_number(), '_makecommerce_payment_token', $data['token']['id']);
176 update_post_meta($order->get_order_number(), '_makecommerce_payment_token_valid_until', $data['token']['valid_until']);
177 }
178
179 $order->payment_complete( $transactionId );
180 // $order->update_status( 'processing' );
181 $woocommerce->cart->empty_cart();
182 break;
183
184 }
185
186 return $returnUrl;
187 }
188
189
190
191
192 $MKAPI = false;
193 function mk_get_api() {
194 global $MKAPI;
195 if ($MKAPI) {
196 //return $MKAPI;
197 }
198 $mk_api_type = get_option('mk_api_type', false);
199 if (!$mk_api_type) {
200 return false;
201 }
202 if ($mk_api_type !== 'live') {
203 $key_prefix = $mk_api_type.'_';
204 } else {
205 $key_prefix = '';
206 }
207 $mk_shop_id = get_option('mk_'.$key_prefix.'shop_id', '');
208 $mk_public_key = get_option('mk_'.$key_prefix.'public_key', '');
209 $mk_private_key = get_option('mk_'.$key_prefix.'private_key', '');
210 if (!$mk_shop_id || !$mk_public_key || !$mk_shop_id) {
211 return false;
212 }
213 $MKAPI = new \Maksekeskus\Maksekeskus($mk_shop_id, $mk_public_key, $mk_private_key, $mk_api_type === 'live' ? false : true);
214 return $MKAPI;
215 }
216
217 function mk_is_api_set() {
218 $mk_api_type = get_option('mk_api_type', false);
219 if (!$mk_api_type) {
220 return false;
221 }
222 if ($mk_api_type !== 'live') {
223 $key_prefix = $mk_api_type.'_';
224 } else {
225 $key_prefix = '';
226 }
227 $mk_shop_id = get_option('mk_'.$key_prefix.'shop_id', '');
228 $mk_public_key = get_option('mk_'.$key_prefix.'public_key', '');
229 $mk_private_key = get_option('mk_'.$key_prefix.'private_key', '');
230 if (!$mk_shop_id || !$mk_public_key || !$mk_shop_id) {
231 return false;
232 }
233 return true;
234 }
235
236
237 function mk_admin_error() {
238 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'));
239 }
240
241
242
243 add_filter('query_vars', 'mk_cron_query_vars');
244 add_action('parse_request', 'mk_parse_cron_update_vars');
245
246 function mk_cron_query_vars($vars) {
247 $vars[] = 'mk-action';
248 $vars[] = 'mk-shop-id';
249 $vars[] = 'mk-test-shop-id';
250 return $vars;
251 }
252
253 function mk_parse_cron_update_vars($wp) {
254 if (array_key_exists('mk-action', $wp->query_vars) && $wp->query_vars['mk-action'] == 'mk-update'
255 && (array_key_exists('mk-shop-id', $wp->query_vars) || array_key_exists('mk-test-shop-id', $wp->query_vars) ) ) {
256 if( (strlen($wp->query_vars['mk-shop-id']) && $wp->query_vars['mk-shop-id'] == get_option('mk_shop_id', false) )
257 || (strlen($wp->query_vars['mk-test-shop-id']) && $wp->query_vars['mk-test-shop-id'] == get_option('mk_test_shop_id', false) ) ) {
258 update_mc_banklinks();
259 exit();
260 }
261 }
262 }
263
264 function mk_admin_login_update($user_login, $user) {
265 if(user_can( $user, 'administrator' )) {
266 update_mc_banklinks();
267 }
268 }
269 add_action('wp_login', 'mk_admin_login_update', 10, 2);
270
271
272
273 function mk_admin_add_api_settings($sections) {
274 $sections['mk_api'] = __('MakeCommerce API access', 'wc_makecommerce_domain');
275 return $sections;
276 }
277
278 function mk_admin_api_settings($settings) {
279 global $current_section;
280 if ($current_section !== 'mk_api') {
281 return $settings;
282 }
283 return array(
284 array('type' => 'title', 'desc' => __get_logo_html()),
285 array(
286 'type' => 'title',
287 'title' => __('MakeCommerce API access credentials', 'wc_makecommerce_domain'),
288 'desc' => __('To use MakeCommerce/Maksekeskus services you need to enter API credentials below here <br/> <br/>', 'wc_makecommerce_domain').
289 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'),
290 'id' => 'mk_api_settings'
291 ),
292 array(
293 'type' => 'select',
294 'title' => __('Current environment', 'wc_makecommerce_domain'),
295 'desc' => __('See more about <a href="https://maksekeskus.ee/en/for-developers/test-environment/">MakeCommerce Test environment</a>', 'wc_makecommerce_domain'),
296 'default' => 'live',
297 'options' => array(
298 'live' => __('Live', 'wc_makecommerce_domain'),
299 'test' => __('Test', 'wc_makecommerce_domain'),
300 ),
301 'id' => 'mk_api_type'
302 ),
303 array(
304 'id' => 'mk_shop_id',
305 'type' => 'text',
306 'title' => __('Shop ID (live)', 'wc_makecommerce_domain'),
307 'desc' => __('Get it from <a href="https://merchant.maksekeskus.ee/api.html" target="_blank">Merchant Portal</a>','wc_makecommerce_domain'),
308 'class' => 'input-text regular-input',
309 ),
310 array(
311 'id' => 'mk_private_key',
312 'type' => 'text',
313 'title' => __('Secret key (live)', 'wc_makecommerce_domain'),
314 'class' => 'input-text regular-input',
315 ),
316 array(
317 'id' => 'mk_public_key',
318 'type' => 'text',
319 'title' => __('Publishable key (live)', 'wc_makecommerce_domain'),
320 'class' => 'input-text regular-input',
321 ),
322 array(
323 'id' => 'mk_test_shop_id',
324 'type' => 'text',
325 'title' => __('Shop ID (test)', 'wc_makecommerce_domain'),
326 'class' => 'input-text regular-input',
327 'default' => 'f64b4f20-5ef9-4b7b-a6fa-d623d87f0b9c',
328 'desc' => __('Get it from <a href="https://merchant-test.maksekeskus.ee/api.html" target="_blank">Merchant Portal Test</a>','wc_makecommerce_domain'),
329 ),
330 array(
331 'id' => 'mk_test_private_key',
332 'type' => 'text',
333 'title' => __('Secret key (test)', 'wc_makecommerce_domain'),
334 'default' => 'MPjcVMoRZPAucsTuGK5ZukOlV7BlzgSvXOowJUkk9IFSQPVooRwcVOxzz3mhEpgM',
335 'class' => 'input-text regular-input',
336 ),
337 array(
338 'id' => 'mk_test_public_key',
339 'type' => 'text',
340 'title' => __('Publishable key (test)', 'wc_makecommerce_domain'),
341 'default' => '7Hog41ci2mKkmtviMycWxpNx14pNP70m',
342 'class' => 'input-text regular-input',
343 ),
344 array('type' => 'sectionend', 'id' => 'mk_api_settings'),
345 array(
346 'type' => 'title',
347 'desc' => '<br><hr><br>',
348 ),
349 array(
350 'id' => 'mk_module_title',
351 'type' => 'title',
352 'title' => __('Makecommerce modules', 'wc_makecommerce_domain'),
353 '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'),
354 'class' => '',
355 ),
356 array(
357 'id' => 'mk_transport_apt_omniva',
358 'type' => 'checkbox',
359 'default' => 'yes',
360 'title' => __('Omniva Parcel Machine', 'wc_makecommerce_domain'),
361 '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')).')',
362 'class' => '',
363 ),
364 array(
365 'id' => 'mk_transport_apt_smartpost',
366 'type' => 'checkbox',
367 'default' => 'yes',
368 'title' => __('SmartPOST Parcel Machine', 'wc_makecommerce_domain'),
369 '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')).')',
370 'class' => '',
371 ),
372 array(
373 'id' => 'mk_transport_apt_dpd',
374 'type' => 'checkbox',
375 'default' => 'no',
376 'title' => __('DPD Pickup Network', 'wc_makecommerce_domain'),
377 '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')).')',
378 'class' => '',
379 ),
380 array(
381 'id' => 'mk_transport_courier_omniva',
382 'type' => 'checkbox',
383 'default' => 'yes',
384 'title' => __('Omniva Courier', 'wc_makecommerce_domain'),
385 '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')).')',
386 'class' => '',
387 ),
388 array(
389 'id' => 'mk_transport_courier_smartpost',
390 'type' => 'checkbox',
391 'default' => 'yes',
392 'title' => __('SmartPOST Courier', 'wc_makecommerce_domain'),
393 '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')).')',
394 'class' => '',
395 ),
396 array(
397 'id' => 'mk_checkout_sco',
398 'type' => 'checkbox',
399 'title' => __('SimpleCheckout', 'wc_makecommerce_domain'),
400 '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')).')',
401 'class' => '',
402 ),
403 array(
404 'id' => 'mk_javascript_ui',
405 'type' => 'api_javascript_ui',
406 ),
407 array('type' => 'sectionend', 'id' => 'mk_api_settings')
408 );
409 }
410
411 function mk_wc_version($version) {
412 global $woocommerce;
413 if ( version_compare( $woocommerce->version, "3.4.0", ">=" ) ) {
414 return true;
415 } else {
416 return false;
417 }
418 }
419
420 function mk_admin_save_api_settings() {
421 // reload banklinks
422 global $current_section;
423 if ($current_section !== 'mk_api') {
424 return;
425 }
426 update_mc_banklinks();
427 }
428
429 function mk_admin_plugin_settings_link($links) {
430 if (mk_wc_version("3.4.0"))
431 $settings_link = '<a href="admin.php?page=wc-settings&tab=advanced&section=mk_api">API '.__('Settings').'</a>';
432 else
433 $settings_link = '<a href="admin.php?page=wc-settings&tab=api&section=mk_api">API '.__('Settings').'</a>';
434 array_unshift($links, $settings_link);
435 return $links;
436 }
437 // if (mk_wc_version("3.4.0")) {
438 add_filter('woocommerce_get_settings_advanced', 'mk_admin_api_settings', 10, 2);
439 add_action('woocommerce_get_sections_advanced', 'mk_admin_add_api_settings');
440 add_filter('woocommerce_get_settings_api', 'mk_admin_api_settings', 10, 2);
441 // } else {
442 add_action('woocommerce_get_sections_api', 'mk_admin_add_api_settings');
443 add_action('woocommerce_settings_saved', 'mk_admin_save_api_settings', 30, 0);
444 add_filter("plugin_action_links_".plugin_basename(__FILE__), 'mk_admin_plugin_settings_link' );
445 // }
446
447 function __get_logo_html() {
448 return '<div class="makecommerce-info">'.
449 '<div class="makecommerce-logo">'.
450 '<a target="_blank" href="http://maksekeskus.ee"><img src="'. plugins_url('/images/makecommerce_logo_en.svg', __FILE__) .'" class="makecommerce-logo"></a>'.
451 '</div>'.
452 '<div class="makecommerce-links">'.
453 '<div class="makecommerce-link"><a target="_blank" href="https://merchant.maksekeskus.ee">Merchant Portal</a></div>'.
454 '<div class="makecommerce-link"><a target="_blank" href="https://makecommerce.net/">makecommerce.net</a></div>'.
455 '<div class="makecommerce-link"><a target="_blank" href="http://maksekeskus.ee">maksekeskus.ee</a></div>'.
456 '</div>'.
457 '</div>';
458 }
459 }
460