PluginProbe
MakeCommerce for WooCommerce / 2.4.5
MakeCommerce for WooCommerce v2.4.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.4.5, at makecommerce.php

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