PluginProbe
MakeCommerce for WooCommerce / 2.4.1
MakeCommerce for WooCommerce v2.4.1
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.4.1, at makecommerce.php

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