PluginProbe
MakeCommerce for WooCommerce / 2.3.1
MakeCommerce for WooCommerce v2.3.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-payment.php

makecommerce-payment.php in MakeCommerce for WooCommerce 2.3.1, at makecommerce-payment.php

1,080 lines 42.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3
4
5 function woocommerce_payment_makecommerce_init() {
6
7 load_plugin_textdomain('wc_makecommerce_domain', false, dirname(plugin_basename(__FILE__)) . '/');
8
9 class woocommerce_makecommerce extends WC_Payment_Gateway {
10
11 const MC_CANCELLED = 'CANCELLED';
12 const MC_COMPLETED = 'COMPLETED';
13 const MC_DEPOSITED = 'DEPOSITED';
14
15 const MC_PART_REFUNDED = 'PART_REFUNDED';
16 const MC_REFUNDED = 'REFUNDED';
17
18 const module_name = 'MakeCommerce';
19
20 const billing_descriptor_dba = '';
21 const currencies_allowed = 'EUR';
22 const module_homepage_url = 'https://maksekeskus.ee/en/integration-modules/makecommerce-woocommerce-payment-plugin/';
23 const testenv_homepage_url = 'http://maksekeskus.ee/en/for-developers/test-environment/';
24
25 public $id = 'makecommerce';
26 public $version = '2.3.1';
27
28 public $payment_return_url;
29 public $payment_return_url_m2m;
30 public $payment_return_url_cancel;
31
32 //protected $_shop_id;
33 //protected $_api_key_secret;
34 //protected $_api_key_public;
35
36
37 protected $_banklinks = array();
38 protected $_banklinks_grouped;
39 protected $_cards = array();
40
41 protected $MK;
42 protected $_init;
43
44
45 public function __construct($init = false) {
46 $this->_init = $init;
47
48 $this->payment_return_url = site_url('/?makecommerce_return=1');
49 $this->payment_return_url_m2m = site_url('/?makecommerce_return=1&ajax_content=1');
50 $this->payment_return_url_cancel = site_url('/?makecommerce_return=1');
51
52 // Load the form fields.
53 $this->init_form_fields();
54
55 // Load the settings.
56 $this->init_settings();
57
58 $this->initBanklinks();
59
60 if (defined('ICL_LANGUAGE_CODE') && !empty($this->settings['ui_widget_title_'.ICL_LANGUAGE_CODE])) {
61 $this->title = $this->settings['ui_widget_title_'.ICL_LANGUAGE_CODE];
62 }
63 else {
64 $this->title = $this->settings['ui_widget_title'];
65 }
66
67 $this->method_title = 'MakeCommerce';
68 $this->description = true;
69
70 $this->MK = mk_get_api();
71 if (!$this->MK && $this->_init) {
72 add_action( 'admin_notices', array(&$this, 'makecommerce_api_info_missing') );
73 }
74
75 $this->supports = array(
76 'products',
77 'refunds',
78 );
79
80 add_filter('query_vars', array(&$this, 'makecommerce_return_trigger'));
81 add_action('template_redirect', array(&$this, 'makecommerce_return_trigger_check'));
82
83 if($this->_init == false) {
84 add_action('woocommerce_update_options_payment_gateways', array(&$this, 'process_admin_options'));
85 add_action('woocommerce_update_options_payment_gateways_' . $this->id, array(&$this, 'process_admin_options'));
86 add_action('woocommerce_receipt_' . $this->id, array($this, 'receipt_page'));
87
88 add_action('woocommerce_admin_order_data_after_order_details', array(&$this, 'admin_order_page'), 10, 1);
89
90 wp_enqueue_script('jquery');
91 wp_enqueue_style('makecommerce', plugins_url('/css/makecommerce.css', __FILE__), array(), $this->version);
92 }
93 if (is_admin()) {
94 add_action( 'wp_ajax_mc_banklinks_reload', array(&$this, 'mc_banklinks_reload') );
95
96 require_once(ABSPATH.'wp-admin/includes/plugin.php');
97 $wc_version = get_plugin_data(__DIR__.'/../woocommerce/woocommerce.php');
98 if ((double)$wc_version['Version'] < 2.6) {
99 function sample_admin_notice__error() {
100 $class = 'notice notice-error';
101 $wc_version = get_plugin_data(__DIR__.'/../woocommerce/woocommerce.php');
102 $message = sprintf(__( 'Makecommerce plugin needs at least <strong>WooCommerce 2.6</strong> to function correctly. Please update yours (currently %s)', 'wc_makecommerce_domain' ), $wc_version['Version']);
103 printf( '<div class="%1$s"><p>%2$s</p></div>', $class, $message );
104 }
105 add_action( 'admin_notices', 'sample_admin_notice__error' );
106 }
107 }
108
109 }
110
111 public function get_title() {
112 // TODO: add multilingual support
113 return $this->title;
114 }
115
116 protected function _getWooCommerce() {
117 global $woocommerce;
118 return $woocommerce;
119 }
120
121 public function is_valid_for_use() {
122 return true;
123 }
124
125 public function is_available() {
126 if ($this->settings['active'] == "yes" && mk_is_api_set()) {
127 return true;
128 }
129 }
130
131 private function initBanklinks() {
132 global $wpdb;
133 global $mkDbTable;
134 $this->_banklinks = array();
135
136 $tableName = $wpdb->prefix . $mkDbTable;
137 $methods = $wpdb->get_results('SELECT * FROM '.$tableName);
138
139 if(count($methods)) {
140 if(is_admin() && $this->_init == true && get_option( 'mc_banklinks_api_type' ) != get_option('mk_api_type', false)) {
141 // ??
142 add_action( 'admin_notices', array(&$this, 'makecommerce_banklinks_list_type_notice') );
143 }
144 $banklinks = array();
145 $banklinks_grouped = array();
146 $cards = array();
147 foreach($methods as $method) {
148 if($method->type == 'banklink') {
149 $banklinks[] = $banklinks_grouped[$method->country][] = $method;
150 } elseif($method->type == 'card') {
151 $cards[] = $method;
152 }
153 }
154
155 usort($banklinks, array($this, 'orderBanklinks'));
156 foreach($banklinks_grouped as &$country) {
157 usort($country, array($this, 'orderBanklinks'));
158 }
159
160 $this->_banklinks = $banklinks;
161 $this->_banklinks_grouped = $banklinks_grouped;
162 $this->_cards = $cards;
163 remove_action('admin_notices', array(&$this, 'makecommerce_banklinks_list_empty'), 30);
164 } elseif(is_admin() && $this->_init == true) {
165 add_action('admin_notices', array(&$this, 'makecommerce_banklinks_list_empty'), 30);
166 }
167 }
168
169 private function orderBanklinks($a, $b) {
170 $order = array_map('trim', explode(",", $this->settings['ui_chorder']));
171
172 $posA = array_search($a->name, $order);
173 $posB = array_search($b->name, $order);
174
175 if($posA === $posB) return $a->id > $b->id ? 1 : -1;
176 if($posA === FALSE) return 1;
177 if($posB === FALSE) return -1;
178
179 return $posA > $posB ? 1 : -1;
180 }
181
182
183
184 // ===== WC admin related stuff ======
185
186
187 public function makecommerce_api_info_missing() {
188 ?>
189 <div class="notice notice-error is-dismissible">
190 <p>
191 <?php echo __('You have not entered the Shop ID and keys for the MakeCommerce payment module. The module will not work without them.', 'wc_makecommerce_domain'); ?>
192 <a href="<?php echo admin_url('admin.php?page=wc-settings&tab=api&section=mk_api'); ?>"><?php echo __('Click here to enter them', 'wc_makecommerce_domain'); ?></a>
193 </p>
194 </div>
195 <?php
196 }
197
198 public function makecommerce_banklinks_list_empty() {
199 ?>
200 <div class="notice notice-error is-dismissible">
201 <p>
202 <?php echo __('The payment methods list for MakeCommerce payment module is empty.', 'wc_makecommerce_domain'); ?>
203 <a href="<?php echo admin_url('admin.php?page=wc-settings&tab=checkout&section=makecommerce'); ?>"><?php echo __('Go to the settings to update them', 'wc_makecommerce_domain'); ?></a>
204 </p>
205 </div>
206 <?php
207 }
208
209 public function makecommerce_banklinks_list_type_notice() {
210 ?>
211 <div class="notice notice-error is-dismissible">
212 <p>
213 <?php echo __('You have changed the environment for MakeCommerce payment module. The payment methods list has been loaded for a different environment.', 'wc_makecommerce_domain'); ?>
214 <a href="<?php echo admin_url('admin.php?page=wc-settings&tab=checkout&section=makecommerce'); ?>"><?php echo __('Go to the settings to update them', 'wc_makecommerce_domain'); ?></a>
215 </p>
216 </div>
217 <?php
218 }
219
220
221 public function init_form_fields() {
222
223 if ( function_exists('icl_object_id') && ! defined( 'POLYLANG_VERSION' ) ) {
224 $languages = apply_filters('wpml_active_languages', NULL, 'skip_missing=0');
225 }
226 else if ( defined( 'POLYLANG_VERSION' ) ) {
227 $pl_locales = pll_languages_list( array('fields'=>'locale') );
228 foreach ($pl_locales as $key => $locale_pl ) {
229 $language = array( 'id' => $key,
230 'code' => $locale_pl) ;
231 $languages[$locale_pl] = $language;
232 }
233 }
234
235 $this->form_fields = array();
236 $this->form_fields['logo'] = array('type' => 'title', 'description' => __get_logo_html());
237 $this->form_fields['active'] = array(
238 'title' => __('Enable/Disable', 'wc_makecommerce_domain'),
239 'type' => 'checkbox',
240 'label' => __('Enable MakeCommerce payments', 'wc_makecommerce_domain'),
241 'default' => 'no'
242 );
243 $this->form_fields['api_title'] = array(
244 'title' => __('MakeCommerce API', 'wc_makecommerce_domain'),
245 'description' => sprintf(__('Go to <a href="%s">API settings</a> to fill in the credentials', 'wc_makecommerce_domain'), admin_url('admin.php?page=wc-settings&tab=api&section=mk_api')),
246 'type' => 'title',
247 );
248 $this->form_fields['ui_title'] = array(
249 'title' => '<br>'.__('User Interface', 'wc_makecommerce_domain'),
250 'type' => 'title',
251 'class' => 'ui-identifier',
252 );
253 $this->form_fields['ui_open_by_default'] = array(
254 'title' => __('Set as default selection', 'wc_makecommerce_domain'),
255 'label' => __('MakeCommerce payments widget will be selected by default', 'wc_makecommerce_domain'),
256 'type' => 'checkbox',
257 'default' => 'yes',
258 'class' => 'ui-identifier',
259 );
260 $this->form_fields['ui_mode'] = array(
261 'title' => __('Display MC payment channels as', 'wc_makecommerce_domain'),
262 'type' => 'mc_hidden',
263 'default' => 'widget',
264 'options' => array(
265 'inline' => __('List', 'wc_makecommerce_domain'),
266 'widget' => __('Grouped to widget', 'wc_makecommerce_domain'),
267 ),
268 'class' => 'ui-identifier',
269 );
270
271
272 if (empty($languages)) {
273 $this->form_fields['ui_widget_title'] = array(
274 'title' => __('Payments widget title', 'wc_makecommerce_domain'),
275 'type' => 'text',
276 'desc_tip' => __("Appropriate title may depend on the configuration you have made, i.e. 'pay with bank-link or credit card', 'pay with bank-links' or 'payment methods'", 'wc_makecommerce_domain'),
277 'default' => __('Pay with bank-links or credit card', 'wc_makecommerce_domain'),
278 'class' => 'ui-identifier',
279 );
280 } else {
281 foreach ($languages as $language_code => $language) {
282 $language_name = !empty($language['translated_name']) ? $language['translated_name'] : $language_code;
283 $defaultTitle = __('Pay with bank-links or credit card', 'wc_makecommerce_domain');
284 switch ($language_code) {
285 case 'et':
286 $defaultTitle = 'Maksa pangalingi või krediitkaardiga';
287 break;
288 case 'ru':
289 $defaultTitle = 'оплата в интернет-банке или кредитной картой';
290 break;
291 case 'fi':
292 $defaultTitle = 'Maksaa verkkopankissa tai luottokortilla';
293 break;
294 }
295 $this->form_fields['ui_widget_title_'.$language_code] = array(
296 'title' => __('Payments widget title', 'wc_makecommerce_domain').sprintf(' (%s)', $language_code),
297 'type' => 'text',
298 'desc_tip' => __("Appropriate title may depend on the configuration you have made, i.e. 'pay with bank-link or credit card', 'pay with bank-links' or 'payment methods'", 'wc_makecommerce_domain'),
299
300 'default' => $defaultTitle,
301 'class' => 'ui-identifier',
302 );
303 }
304 }
305 $this->form_fields['ui_inline_uselogo'] = array(
306 'title' => __('MC payment channels display style', 'wc_makecommerce_domain'),
307 'type' => 'select',
308 'default' => 'logo',
309 'options' => array(
310 'logo' => __('Logo', 'wc_makecommerce_domain'),
311 'text_logo' => __('Text & logo', 'wc_makecommerce_domain'),
312 'text' => __('Text', 'wc_makecommerce_domain'),
313 ),
314 'class' => 'ui-identifier',
315 );
316 $this->form_fields['ui_widget_logosize'] = array(
317 'title' => __('Size of payment channel logos', 'wc_makecommerce_domain'),
318 'type' => 'select',
319 'default' => 'medium',
320 'options' => array(
321 'small' => __('Small', 'wc_makecommerce_domain'),
322 'medium' => __('Medium', 'wc_makecommerce_domain'),
323 'large' => __('Large', 'wc_makecommerce_domain')
324 ),
325 'class' => 'ui-identifier',
326 );
327 $this->form_fields['ui_widget_groupcountries'] = array(
328 'title' => __('Group bank-links by countries', 'wc_makecommerce_domain'),
329 'type' => 'mc_hidden',
330 'default' => 'no',
331 'class' => 'ui-identifier',
332 );
333 $this->form_fields['ui_widget_countries_hidden'] = array(
334 'title' => __('Hide country selector', 'wc_makecommerce_domain'),
335 'label' => __('Do not display country selector (flags) at payment methods', 'wc_makecommerce_domain'),
336 'type' => 'checkbox',
337 'default' => 'no',
338 );
339 $this->form_fields['ui_widget_countryselector'] = array(
340 'title' => __('Country selector style', 'wc_makecommerce_domain'),
341 'type' => 'mc_hidden',
342 'default' => 'flag',
343 'options' => array(
344 'flag' => __('Flag', 'wc_makecommerce_domain'),
345 'dropdown' => __('Dropdown', 'wc_makecommerce_domain'),
346 ),
347 'class' => 'ui-identifier',
348 );
349 $this->form_fields['ui_widget_groupcc'] = array(
350 'title' => __('Group credit card into separate widget', 'wc_makecommerce_domain'),
351 'type' => 'mc_hidden',
352 'default' => 'no',
353 'class' => 'ui-identifier',
354 );
355 $this->form_fields['ui_chorder'] = array(
356 'title' => __('Define custom order of payment channels', 'wc_makecommerce_domain'),
357 'type' => 'text',
358 'desc_tip' => __('If you want to change default order, put here comma separated list of channels. i,e, - seb,lhv,swedbank. see more on the module home page (link above)', 'wc_makecommerce_domain'),
359 'class' => 'ui-identifier',
360 );
361 $this->form_fields['ui_javascript'] = array(
362 'type' => 'ui_javascript',
363 );
364 $this->form_fields['cc_title'] = array(
365 'title' => '<br>'.__('Credit Card Settings', 'wc_makecommerce_domain'),
366 'type' => 'title',
367 );
368 $this->form_fields['cc_pass_cust_data'] = array(
369 'title' => __('Prefill Credit Card form with customer data', 'wc_makecommerce_domain'),
370 'type' => 'checkbox',
371 'default' => 'yes',
372 'desc_tip' => __('It will pass user Name and e-mail address to the Credit Card dialog to make the form filling easier', 'wc_makecommerce_domain'),
373 );
374 $this->form_fields['adv_title'] = array(
375 'title' => '<hr><br>'.__('Advanced Settings', 'wc_makecommerce_domain'),
376 'type' => 'title',
377 );
378 $this->form_fields['reload_links'] = array(
379 'type' => 'mc_banklinks_reload',
380 'title' => __('Update payment methods', 'wc_makecommerce_domain'),
381 'description' => __('Update', 'wc_makecommerce_domain'),
382 'desc_tip' => __('This will update shop configuration from MakeCommerce servers.', 'wc_makecommerce_domain'),
383 );
384 }
385
386 // Needed to render mc_hidden type fields in admin
387 public function generate_mc_hidden_html( $key, $data ) {
388 $field_key = $this->get_field_key($key);
389 ob_start();
390 ?>
391 <tr style="display: none;">
392 <td colspan="2">
393 <input type="hidden" name="<?php echo $field_key; ?>" value="<?php echo $data['default']; ?>" />
394 </td>
395 </tr>
396 <?php
397 return ob_get_clean();
398 }
399
400
401 public function generate_mc_banklinks_reload_html( $key, $data ) {
402 $field = $this->get_field_key( $key );
403 $defaults = array(
404 'title' => '',
405 'disabled' => false,
406 'class' => '',
407 'css' => '',
408 'placeholder' => '',
409 'type' => 'text',
410 'desc_tip' => false,
411 'description' => '',
412 'custom_attributes' => array()
413 );
414
415 $data = wp_parse_args( $data, $defaults );
416
417 ob_start();
418 ?>
419 <tr valign="top">
420 <th scope="row" class="titledesc">
421 <label for="<?php echo esc_attr( $field ); ?>"><?php echo wp_kses_post( $data['title'] ); ?></label>
422 <?php echo $this->get_tooltip_html( $data ); ?>
423 </th>
424 <td class="forminp">
425 <fieldset>
426 <legend class="screen-reader-text"><span><?php echo wp_kses_post( $data['title'] ); ?></span></legend>
427 <input id="mc_banklinks_reload" class="button <?php echo esc_attr( $data['class'] ); ?>" type="button" name="<?php echo esc_attr( $field ); ?>" id="<?php echo esc_attr( $field ); ?>" style="<?php echo esc_attr( $data['css'] ); ?>" value="<?php echo esc_attr( $data['description'] ); ?>" placeholder="<?php echo esc_attr( $data['placeholder'] ); ?>" <?php disabled( $data['disabled'], true ); ?> <?php echo $this->get_custom_attribute_html( $data ); ?> />
428 <script type="text/javascript">
429 jQuery('input#mc_banklinks_reload').on('click', function() {
430 init_mc_loading();
431 jQuery.ajax({
432 url: '<?php echo get_site_url(); ?>/wp-admin/admin-ajax.php',
433 type: 'POST',
434 data: 'action=mc_banklinks_reload',
435 success: function (output) {
436 if(output.data) {
437 alert(output.data);
438 } else {
439 alert('<?php echo __('There was an error with your update. Please try again.', 'wc_makecommerce_domain'); ?>');
440 }
441 },
442 complete: function() { stop_mc_loading(); }
443 });
444 });
445
446 function init_mc_loading() {
447 jQuery('input#mc_banklinks_reload').attr('disabled', 'disabled');
448 }
449
450 function stop_mc_loading() {
451 jQuery('input#mc_banklinks_reload').removeAttr('disabled');
452 }
453 </script>
454 </fieldset>
455 </td>
456 </tr>
457 <?php
458
459 return ob_get_clean();
460 }
461
462 public function mc_banklinks_reload($force = false) {
463 if ($force || (defined('DOING_AJAX') && DOING_AJAX)) {
464
465 global $wpdb;
466 global $mkDbTable;
467
468 $request_params = array(
469 'environment' => json_encode(array(
470 'platform' => 'woocommerce '.$this->_getWooCommerce()->version,
471 'module' => $this->id.' '.$this->version,
472 )),
473 );
474
475 if (!$this->MK) {
476 return false;
477 }
478
479 try {
480 $shopConfig = $this->MK->getShopConfig($request_params);
481 $methods = $shopConfig->paymentMethods;
482 } catch (Exception $e) {
483 error_log(print_r($e, 1));
484 return false;
485 }
486
487 $tableName = $wpdb->prefix . $mkDbTable;
488 $wpdb->query('TRUNCATE TABLE '.$tableName);
489
490 if(isset($methods->banklinks)) {
491 foreach($methods->banklinks as $method) {
492 $wpdb->insert($tableName, array('type' => 'banklink', 'country' => $method->country, 'name' => $method->name, 'url' => $method->url));
493 }
494 $updated = true;
495 }
496
497 if(isset($methods->cards)) {
498 foreach($methods->cards as $method) {
499 $wpdb->insert($tableName, array('type' => 'card', 'name' => $method->name));
500 }
501 $updated = true;
502 }
503
504 if(isset($shopConfig) && strlen($shopConfig->name)) {
505 update_option( 'mc_shop_name', $shopConfig->name );
506 }
507
508 if ($updated) {
509 update_option( 'mc_banklinks_api_type', get_option('mk_api_type', false) );
510 }
511
512 if ($force) {
513 $this->initBankLinks();
514 return $updated;
515 }
516
517 if($updated) {
518 wp_send_json(array('success' => 1, 'data' => __('Update successfully completed!', 'wc_makecommerce_domain')));
519 exit;
520 }
521
522 wp_send_json(array('success' => 0, 'data' => __('There was an error with your update. Please try again.', 'wc_makecommerce_domain')));
523 exit;
524 }
525 die();
526 }
527
528
529
530
531 public function admin_order_page($order) {
532 //
533 }
534
535 public function process_refund($order_id, $amount = null, $comment = '') {
536 if ($this->MK) {
537 try {
538 $order = new WC_Order($order_id);
539 $transactionId = $order->get_transaction_id();
540
541 if ( empty($transactionId) ) {
542 $transactionId = get_post_meta( $order_id, '_makecommerce_transaction_id', true);
543 }
544
545 if($response = $this->MK->createRefund($transactionId, array('amount' => sprintf("%.2f", $amount), 'comment' => ($comment ? : 'refund')))) {
546 if($status = (string)$response->transaction->status) {
547 switch($status) {
548 case self::MC_REFUNDED:
549 $order->add_order_note(sprintf(__('Refund completed for amount %s', 'wc_makecommerce_domain'), $amount));
550 return true;
551 break;
552 case self::MC_PART_REFUNDED:
553 $order->add_order_note(sprintf(__('Partial refund completed for amount %s', 'wc_makecommerce_domain'), $amount));
554 return true;
555 break;
556 }
557 }
558 }
559 return false;
560
561 } catch (Exception $e) {
562 return new WP_Error('makecommerce_refund_error', $e->getMessage());
563 }
564
565 return false;
566 }
567 return false;
568 }
569
570
571 // ====== storefront related stuff =========
572
573 public function generate_ui_javascript_html( $key, $data ) {
574 ?>
575 <script type="text/javascript">
576 jQuery(document).ready(function($) {
577
578 var api_type = $('#woocommerce_<?php echo $this->id; ?>_api_type');
579
580 var ui_inline_uselogo_row = $('#woocommerce_<?php echo $this->id; ?>_ui_inline_uselogo').closest('tr');
581 var ui_widget_title_row = $('#woocommerce_<?php echo $this->id; ?>_ui_widget_title').closest('tr');
582 var ui_widget_logosize_row = $('#woocommerce_<?php echo $this->id; ?>_ui_widget_logosize').closest('tr');
583 var ui_widget_countryselector_row = $('#woocommerce_<?php echo $this->id; ?>_ui_widget_countryselector').closest('tr');
584 var ui_widget_groupcountries_row = $('#woocommerce_<?php echo $this->id; ?>_ui_widget_groupcountries').closest('tr');
585 var ui_widget_groupcc_row = $('#woocommerce_<?php echo $this->id; ?>_ui_widget_groupcc').closest('tr');
586 var ui_widget_groupcc_title_row = $('#woocommerce_<?php echo $this->id; ?>_ui_widget_groupcc_title').closest('tr');
587
588 var ui_mode = $('#woocommerce_<?php echo $this->id; ?>_ui_mode');
589 var ui_widget_groupcountries = $('#woocommerce_<?php echo $this->id; ?>_ui_widget_groupcountries');
590 var ui_widget_groupcc = $('#woocommerce_<?php echo $this->id; ?>_ui_widget_groupcc');
591
592 parseVisibility();
593 function parseVisibility() {
594 $('.ui-identifier').closest('tr').show();
595
596 if(api_type.val() == 'live') {
597 $('.mc-test-link').hide();
598 $('.api-test').closest('tr').hide();
599 } else {
600 $('.mc-test-link').show();
601 $('.api-live').closest('tr').hide();
602 }
603
604 if(ui_mode.val() == 'inline') {
605 ui_widget_title_row.hide();
606 ui_widget_logosize_row.hide();
607 ui_widget_countryselector_row.hide();
608 ui_widget_groupcountries_row.hide();
609 ui_widget_groupcc_row.hide();
610 ui_widget_groupcc_title_row.hide();
611 } else {
612 ui_inline_uselogo_row.hide();
613
614 if(ui_widget_groupcountries.prop('checked')) {
615 ui_widget_countryselector_row.hide();
616 }
617 if(!ui_widget_groupcc.prop('checked')) {
618 ui_widget_groupcc_title_row.hide();
619 }
620 }
621 }
622
623 api_type.on('change', parseVisibility);
624 ui_mode.on('change', parseVisibility);
625 ui_widget_groupcountries.on('change', parseVisibility);
626 ui_widget_groupcc.on('change', parseVisibility);
627
628 });
629 </script>
630 <?php
631 }
632
633 public function payment_fields() {
634
635 if($this->settings['ui_mode'] == 'inline') {
636
637 ?>
638 <ul class="makecommerce-picker">
639 <?php foreach($this->_banklinks as $method): ?>
640 <li class="makecommerce-picker-method">
641 <input type="radio" id="makecommerce_method_picker_<?php echo $method->country.'_'.$method->name; ?>" name="PRESELECTED_METHOD_<?php echo $this->id; ?>" value="<?php echo $method->country.'_'.$method->name; ?>"/>
642 <label for="makecommerce_method_picker_<?php echo $method->country.'_'.$method->name; ?>">
643 <span class="makecommerce-method-title"><?php if(in_array($this->settings['ui_inline_uselogo'], array('text', 'text_logo'))) { echo ucfirst($method->name); if(count($this->_banklinks_grouped) > 1) { echo ' ('.$this->getCountryName($method->country).')'; } } ?></span>
644 <?php if(in_array($this->settings['ui_inline_uselogo'], array('logo', 'text_logo'))) : ?>
645 <div><img src="<?php echo $this->getImageUrl($method->name); ?>" title="<?php echo ucfirst($method->name); ?>" /></div>
646 <?php endif; ?>
647 </label>
648 </li>
649 <?php endforeach; ?>
650 <?php foreach($this->_cards as $method): ?>
651 <li class="makecommerce-picker-method">
652 <input type="radio" id="makecommerce_method_picker_<?php echo 'card_'.$method->name; ?>" name="PRESELECTED_METHOD_<?php echo $this->id; ?>" value="<?php echo 'card_'.$method->name; ?>"/>
653 <label for="makecommerce_method_picker_<?php echo 'card_'.$method->name; ?>">
654 <span class="makecommerce-method-title"><?php if(in_array($this->settings['ui_inline_uselogo'], array('text', 'text_logo'))) { echo ucfirst($method->name); } ?></span>
655 <?php if(in_array($this->settings['ui_inline_uselogo'], array('logo', 'text_logo'))) : ?>
656 <div><img src="<?php echo $this->getImageUrl($method->name); ?>" title="<?php echo ucfirst($method->name); ?>" /></div>
657 <?php endif; ?>
658 </label>
659 </li>
660 <?php endforeach; ?>
661 </ul>
662 <?php
663
664 } else {
665 ?>
666 <select id="<?php echo $this->id; ?>" name="PRESELECTED_METHOD_<?php echo $this->id; ?>">
667 <option value=""></option>
668 <?php foreach($this->_banklinks as $method): ?>
669 <option value="<?php echo $method->country.'_'.$method->name; ?>"><?php echo strtoupper($method->country).' - '.ucfirst($method->name); ?></option>
670 <?php endforeach; ?>
671 <?php foreach($this->_cards as $method): ?>
672 <option value="card_<?php echo $method->name; ?>"><?php echo ucfirst($method->name); ?></option>
673 <?php endforeach; ?>
674 </select>
675 <ul class="makecommerce-picker">
676 <?php
677
678 if($this->_banklinks || $this->_cards) {
679 $defaultCountry = $this->getDefaultCountry();
680 ?>
681 <?php if( ( empty($this->settings['ui_widget_groupcountries']) || $this->settings['ui_widget_groupcountries'] == 'no' ) && (!isset($this->settings['ui_widget_countries_hidden']) || $this->settings['ui_widget_countries_hidden'] == 'no') ) : ?>
682 <div class="makecommerce_country_picker_countries">
683 <?php foreach(array_keys($this->_banklinks_grouped) as $country): ?>
684 <input style="display: none;" type="radio" id="makecommerce_country_picker_<?php echo $country; ?>" name="makecommerce_country_picker" value="<?php echo $country; ?>" <?php if($defaultCountry == $country) echo 'checked="checked" '; ?>/><?php if($this->settings['ui_widget_countryselector'] == 'flag') { ?><label for="makecommerce_country_picker_<?php echo $country; ?>" class="makecommerce_country_picker_label" style="background-image: url(<?php echo plugins_url('/images/'.$country.'32.png', __FILE__); ?>);"></label><?php } ?>
685 <?php endforeach; ?>
686 <?php if($this->settings['ui_widget_countryselector'] == 'dropdown') : ?>
687 <select name="makecommerce_country_picker_select">
688 <?php foreach(array_keys($this->_banklinks_grouped) as $country): ?>
689 <option value="<?php echo $country; ?>" <?php if($defaultCountry == $country) echo 'selected="selected" '; ?>><?php echo $this->getCountryName($country); ?></option>
690 <?php endforeach; ?>
691 <option value="card" style="display:none;"></option>
692 </select>
693 <?php endif; ?>
694 </div>
695 <?php endif; ?>
696 <?php
697 $banklinks_grouped = $this->_banklinks_grouped;
698 $banklinks_grouped['other'] = array();
699 ?>
700 <?php foreach($banklinks_grouped as $country => $methods): ?>
701 <li class="makecommerce-picker-country">
702 <?php if($this->settings['ui_widget_groupcountries'] == 'yes') : ?>
703 <input type="radio" id="makecommerce_country_picker_<?php echo $country; ?>" name="makecommerce_country_picker" value="<?php echo $country; ?>" <?php if($defaultCountry == $country) echo 'checked="checked" '; ?>/><label for="makecommerce_country_picker_<?php echo $country; ?>"><img src="<?php echo plugins_url('/images/'.$country.'32.png', __FILE__); ?>" /></label>
704 <?php endif; ?>
705 <div class="makecommerce_country_picker_methods" id="makecommerce_country_picker_methods_<?php echo $country; ?>">
706 <?php foreach($methods as $method): ?>
707 <div class="makecommerce-banklink-picker" banklink_id="<?php echo $method->country.'_'.$method->name; ?>">
708 <img src="<?php echo $this->getImageUrl($method->name); ?>" title="<?php echo ucfirst($method->name); ?>" />
709 </div>
710 <?php endforeach; ?>
711 <?php if($this->_cards && $this->settings['ui_widget_groupcc'] == 'no') : ?>
712 <div class="breaker"></div>
713 <?php foreach($this->_cards as $method): ?>
714 <div class="makecommerce-banklink-picker" banklink_id="card_<?php echo $method->name; ?>">
715 <img src="<?php echo $this->getImageUrl($method->name); ?>" title="<?php echo ucfirst($method->name); ?>" />
716 </div>
717 <?php endforeach; ?>
718 <?php elseif($country == 'other') :?>
719 <p class="no-methods"><?php _e('No payment methods for selected country' ,'wc_makecommerce_domain');?></p>
720 <?php endif; ?>
721 </div>
722 </li>
723 <?php endforeach; ?>
724 <?php if($this->_cards && $this->settings['ui_widget_groupcc'] == 'yes') : ?>
725 <li class="makecommerce-picker-country">
726 <input type="radio" id="makecommerce_country_picker_card" name="makecommerce_country_picker" value="card"/><label for="makecommerce_country_picker_card"><?php echo $this->settings['ui_widget_groupcc_title']; ?></label>
727 <div class="makecommerce_country_picker_methods" id="makecommerce_country_picker_methods_card">
728 <?php foreach($this->_cards as $method): ?>
729 <div class="makecommerce-banklink-picker" banklink_id="card_<?php echo $method->name; ?>">
730 <img src="<?php echo $this->getImageUrl($method->name); ?>" title="<?php echo ucfirst($method->name); ?>" />
731 </div>
732 <?php endforeach; ?>
733 </div>
734 </li>
735 <?php endif; ?>
736 <?php
737 }
738 ?>
739 </ul>
740 <div class="mc-clear-both"></div>
741 <script type="text/javascript">
742 var makecommerceId = '<?php echo $this->id; ?>';
743 var selectedCountry = '<?php echo $defaultCountry; ?>';
744
745 var logosize = '<?php echo $this->settings['ui_widget_logosize']; ?>';
746 jQuery('div.makecommerce_country_picker_methods').addClass('logosize-'+logosize);
747
748 <?php if(count($banklinks_grouped) == 1): ?>
749 jQuery('li.makecommerce-picker-country').show();
750 jQuery('div.makecommerce_country_picker_countries').hide();
751 jQuery('li.makecommerce-picker-country > input, li.makecommerce-picker-country > label').hide();
752 <?php else: ?>
753 makecommercePick();
754
755 jQuery('body').on('change', 'select[name=makecommerce_country_picker_select]', function() {
756 selectedCountry = jQuery(this).val();
757 jQuery('input[name=makecommerce_country_picker]').removeAttr('checked');
758 makecommercePick();
759 });
760 jQuery('body').on('change', 'input[name=makecommerce_country_picker]', function() {
761 if(jQuery(this).is(":checked")) {
762 selectedCountry = jQuery(this).val();
763 jQuery('select[name=makecommerce_country_picker_select]').val(selectedCountry);
764 makecommercePick();
765 }
766 });
767
768 function makecommercePick() {
769 jQuery('select#'+makecommerceId).val('');
770 jQuery('div.makecommerce-banklink-picker').removeClass('selected');
771 jQuery('label.makecommerce_country_picker_label').removeClass('selected');
772
773 jQuery('li.makecommerce-picker-country').hide();
774 jQuery('div#makecommerce_country_picker_methods_' + selectedCountry).parent().show();
775 jQuery('label[for=makecommerce_country_picker_' + selectedCountry + ']').addClass('selected');
776 }
777 <?php endif; ?>
778
779 jQuery('div.makecommerce-banklink-picker').on('click', function() {
780 var banklink_id = jQuery(this).attr('banklink_id');
781 jQuery('select#'+makecommerceId).val(banklink_id);
782
783 jQuery('div.makecommerce-banklink-picker').removeClass('selected');
784 jQuery(this).addClass('selected');
785 });
786 </script>
787 <?php
788 }
789
790 if($this->settings['ui_open_by_default'] == 'yes') {
791 ?>
792 <script type="text/javascript">
793 jQuery('input#payment_method_<?php echo $this->id; ?>').trigger('click');
794 </script>
795 <?php
796 }
797 }
798
799
800
801
802
803 protected function getCountryName($slug) {
804 switch($slug) {
805 case 'ee': return __('Estonia', 'wc_makecommerce_domain'); break;
806 case 'lv': return __('Latvia', 'wc_makecommerce_domain'); break;
807 case 'lt': return __('Lithuania', 'wc_makecommerce_domain'); break;
808 case 'fi': return __('Finland', 'wc_makecommerce_domain'); break;
809 }
810 return $slug;
811 }
812
813 private function getDefaultCountry() {
814 if ($this->_getWooCommerce()->customer) {
815 $customerCountry = strtolower($this->_getWooCommerce()->customer->get_shipping_country());
816 if(array_key_exists($customerCountry, $this->_banklinks_grouped)) {
817 return $customerCountry;
818 } else {
819 return 'other';
820 }
821 }
822
823 $localeToCountry = array(
824 'et' => 'ee',
825 'lv' => 'lv',
826 'lt' => 'lt',
827 'fi' => 'fi',
828 );
829 if(array_key_exists(get_locale(), $localeToCountry)) {
830 return $localeToCountry[get_locale()];
831 }
832
833 return key($this->_banklinks_grouped);
834 }
835
836
837 protected function getImageUrl($methodName) {
838 //$imageUrlPath = 'https://static.maksekeskus.ee/img/channel/lnd/';
839 $imageUrlPath = $this->MK->getEnvUrls()->staticsUrl.'img/channel/lnd/';
840 return $imageUrlPath.$methodName.'.png';
841 }
842
843 public function validate_fields() {
844 $selected = isset($_POST['PRESELECTED_METHOD_' . $this->id]) ? sanitize_text_field($_POST['PRESELECTED_METHOD_' . $this->id]) : false;
845
846 if (!$selected) {
847 wc_add_notice(__('Please select suitable payment option!', 'wc_makecommerce_domain'), 'error');
848 } else {
849 // is this used??
850 $this->_getWooCommerce()->session->makecommerce_preselected_method = $selected;
851 }
852
853 return true;
854 }
855
856
857 public function process_payment($orderId) {
858
859 $order = new WC_Order($orderId);
860
861 $selected = isset($_POST['PRESELECTED_METHOD_' . $this->id]) ? sanitize_text_field($_POST['PRESELECTED_METHOD_' . $this->id]) : false;
862
863 if (!empty($selected)) {
864
865 update_post_meta($order->id, '_makecommerce_preselected_method', $selected);
866
867 $request_body = array(
868 'transaction' => array(
869 'amount' => sprintf("%.2f", $order->order_total),
870 'currency' => $order->get_order_currency(),
871 'reference' => $order->id,
872 'transaction_url' => array(
873 'return_url' => array(
874 'url' => $this->payment_return_url,
875 'method' => 'POST',
876 ),
877 'cancel_url' => array(
878 'url' => $this->payment_return_url_cancel,
879 'method' => 'POST',
880 ),
881 'notification_url' => array(
882 'url' => $this->payment_return_url_m2m,
883 'method' => 'POST',
884 ),
885 ),
886 ),
887 'customer' => array(
888 'ip' => $_SERVER['REMOTE_ADDR'],
889 'country' => strtolower($order->billing_country),
890 'locale' => strtolower(substr(get_locale(), 0, 2)),
891 ),
892 );
893 $transaction = $this->MK->createTransaction($request_body);
894
895 if(isset($transaction->id)) {
896 update_post_meta($order->id, '_makecommerce_transaction_id', $transaction->id);
897 if (substr($selected, 0, 5) == 'card_') {
898 $redirect_url = $order->get_checkout_payment_url( true );
899 } else {
900 $redirect_url = false;
901 foreach ($transaction->payment_methods->banklinks as $banklink) {
902 if ($banklink->country.'_'.$banklink->name === $selected) {
903 $redirect_url = $banklink->url;
904 }
905 }
906 if (!$redirect_url) {
907 $redirect_url = $this->_getRedirectUrl($selected).$transaction->id;
908 }
909 }
910 return array(
911 'result' => 'success',
912 'redirect' => $redirect_url
913 );
914 }
915 }
916
917 wc_add_notice(__('An error occured when trying to process payment!', 'wc_makecommerce_domain'), 'error');
918 return array(
919 'result' => 'failure',
920 );
921 }
922
923
924 protected function _getRedirectUrl($selected) {
925 foreach($this->_banklinks as $method) {
926 if($selected == $method->country.'_'.$method->name)
927 return $method->url;
928 }
929
930 return false;
931 }
932
933 /*
934 protected function _getOrderConfirmationUrl($order) {
935
936 global $sitepress;
937 if(isset($sitepress)) {
938 $baseUrl = $sitepress->language_url( ICL_LANGUAGE_CODE );
939 $lang = ICL_LANGUAGE_CODE;
940 } else {
941 $baseUrl = site_url();
942 $lang = strtolower(substr(get_locale(), 0, 2));
943 }
944 $url = add_query_arg( array(
945 'makecommerce_card_pay' => '1',
946 'order_id' => $order->id,
947 'lang' => $lang,
948 ), $baseUrl);
949
950 // $url = site_url('?makecommerce_card_pay=1&order_id='.$order->id.'&lang='.(defined('ICL_LANGUAGE_CODE') ? ICL_LANGUAGE_CODE : strtolower(substr(get_locale(), 0, 2))));
951 return $url;
952 }
953
954 */
955
956 public function receipt_page($orderId) {
957 $order = new WC_Order($orderId);
958 if (substr(get_post_meta($orderId, '_makecommerce_preselected_method', true), 0, 5) == 'card_' && $order->get_status() == 'pending') {
959 echo "<br>".__('The order is still awaiting your payment', 'wc_makecommerce_domain')."<br>";
960 echo $this->generateCardForm($order);
961 }
962 }
963
964
965 public function generateCardForm($orderId) {
966 $order = new WC_Order($orderId);
967 $scriptSrc = htmlspecialchars($this->MK->getEnvUrls()->checkoutjsUrl.'checkout.js');
968 $transactionId = get_post_meta($order->get_order_number(), '_makecommerce_transaction_id', true);
969 $idReference = $order->get_order_number();
970 $jsParams = array(
971 'key' => $this->MK->getPublishableKey(),
972 'transaction' => $transactionId,
973 'selector' => '#submit_banklinkmakecommerce_payment_form',
974 'amount' => sprintf("%.2f", $order->order_total),
975 'locale' => !empty($_GET['lang']) ? $_GET['lang'] : (defined('ICL_LANGUAGE_CODE') ? ICL_LANGUAGE_CODE : strtolower(substr(get_locale(), 0, 2))),
976 'open-on-load' => 'true',
977 'client-name' => ($this->settings['cc_pass_cust_data'] == 'yes' ? (string) ((method_exists($order, 'get_billing_first_name') ? $order->get_billing_first_name() : $order->billing_first_name) . ' ' . (method_exists($order, 'get_billing_last_name') ? $order->get_billing_last_name() : $order->billing_last_name)) : ''),
978 'email' => ($this->settings['cc_pass_cust_data'] == 'yes' ? (string) (method_exists($order, 'get_billing_email') ? $order->get_billing_email() : $order->billing_email) : ''),
979 'name' => get_option('mc_shop_name', ''),
980 'description' => __('Order', 'woocommerce') . '&nbsp;' . (string) $idReference,
981 'completed' => 'makecommerce_cc_complete',
982 'currency' => 'EUR',
983 'backdrop-close' => 'false',
984 );
985 ?>
986 <script type="text/javascript">
987 function makecommerce_cc_complete(data) {
988 if(data.paymentToken) {
989 jQuery('div.mc-processing-message').show();
990
991 var submitform = jQuery('<form action="<?php echo $this->payment_return_url; ?>" method="POST" style="display: none;"><input type="submit"/><input type="hidden" name="transaction" value="<?php echo $transactionId; ?>" /></form>');
992 for(var key in data) {
993 submitform.append(jQuery('<input type="hidden" name="'+key+'" />').val(data[key]));
994 }
995 jQuery('body').append(submitform);
996 submitform.submit();
997 }
998 }
999 </script>
1000 <form id="cc_form" >
1001 <input type="submit" class="button-alt" id="submit_banklinkmakecommerce_payment_form" value="<?php echo __('Pay with credit card', 'wc_makecommerce_domain'); ?>" />
1002 <script type="text/javascript" src="<?php echo $scriptSrc; ?>" <?php echo $this->_toHtmlAttributes($jsParams); ?>></script>
1003 </form>
1004 <div class="mc-processing-message"><img src="<?php echo plugins_url('/images/loading.png', __FILE__); ?>"/> <?php echo __('Please wait, processing payment...', 'wc_makecommerce_domain'); ?></div>
1005 <?php
1006 }
1007
1008
1009 protected function _toHtmlAttributes($input) {
1010 $result = array();
1011 foreach ($input as $key => $value) {
1012 $result[] = 'data-' . htmlspecialchars($key) . '=' . '"' . htmlspecialchars($value) . '"';
1013 }
1014 return implode(' ', $result);
1015 }
1016
1017 public function makecommerce_return_trigger($vars) {
1018 $vars[] = 'makecommerce_return';
1019 $vars[] = 'ajax_content';
1020 $vars[] = 'makecommerce_card_pay';
1021 return $vars;
1022 }
1023
1024 public function makecommerce_return_trigger_check() {
1025 if (intval(get_query_var('makecommerce_return')) > 0) {
1026 $return_url = mk_check_payment();
1027 if (intval(get_query_var('ajax_content'))) {
1028 echo json_encode(array('redirect' => $return_url));
1029 } else {
1030 wp_redirect($return_url);
1031 }
1032 exit;
1033 }
1034 }
1035
1036
1037 } // class ends here
1038
1039 New woocommerce_makecommerce(true);
1040
1041 // Woocommerce Multilingual overrides
1042 if (is_plugin_active('woocommerce-multilingual/wpml-woocommerce.php')) {
1043 add_filter( 'get_post_metadata', 'mc_override_payment_method_string', 5, 4 );
1044 }
1045
1046 } // init ends here
1047
1048
1049
1050
1051 function woocommerce_payment_makecommerce_add($methods) {
1052 $methods[] = 'woocommerce_makecommerce';
1053 return $methods;
1054 }
1055
1056 add_action('plugins_loaded', 'woocommerce_payment_makecommerce_init');
1057 add_action('woocommerce_payment_gateways', 'woocommerce_payment_makecommerce_add');
1058
1059
1060 // Banklinks update wrapper for action
1061 function update_mc_banklinks() {
1062 $obj = new woocommerce_makecommerce(true);
1063 $obj->mc_banklinks_reload(true);
1064 }
1065
1066
1067 function mc_override_payment_method_string( $check, $object_id, $meta_key, $single ){
1068 if($meta_key == '_payment_method_title') {
1069 $payment_method = get_post_meta( $object_id, '_payment_method', true );
1070 if( $payment_method == 'makecommerce' ){
1071 $payment_gateways = WC()->payment_gateways->payment_gateways();
1072 if( isset( $payment_gateways[ $payment_method ] ) ){
1073 $title = $payment_gateways[ $payment_method ]->title;
1074 return $title;
1075 }
1076 }
1077 }
1078 return $check;
1079 }
1080