PluginProbe
MakeCommerce for WooCommerce / 2.2.7
MakeCommerce for WooCommerce v2.2.7
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.2.7, at makecommerce-payment.php

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