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

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