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

1,071 lines 41.8 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.0';
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 ( empty($transactionId) ) {
535 $transactionId = get_post_meta( $order_id, '_makecommerce_transaction_id', true);
536 }
537
538 if($response = $this->MK->createRefund($transactionId, array('amount' => $amount, 'comment' => ($comment ? : 'refund')))) {
539 if($status = (string)$response->transaction->status) {
540 switch($status) {
541 case self::MC_REFUNDED:
542 $order->add_order_note(sprintf(__('Refund completed for amount %s', 'wc_makecommerce_domain'), $amount));
543 return true;
544 break;
545 case self::MC_PART_REFUNDED:
546 $order->add_order_note(sprintf(__('Partial refund completed for amount %s', 'wc_makecommerce_domain'), $amount));
547 return true;
548 break;
549 }
550 }
551 }
552 return false;
553
554 } catch (Exception $e) {
555 return new WP_Error('makecommerce_refund_error', $e->getMessage());
556 }
557
558 return false;
559 }
560 return false;
561 }
562
563
564 // ====== storefront related stuff =========
565
566 public function generate_ui_javascript_html( $key, $data ) {
567 ?>
568 <script type="text/javascript">
569 jQuery(document).ready(function($) {
570
571 var api_type = $('#woocommerce_<?php echo $this->id; ?>_api_type');
572
573 var ui_inline_uselogo_row = $('#woocommerce_<?php echo $this->id; ?>_ui_inline_uselogo').closest('tr');
574 var ui_widget_title_row = $('#woocommerce_<?php echo $this->id; ?>_ui_widget_title').closest('tr');
575 var ui_widget_logosize_row = $('#woocommerce_<?php echo $this->id; ?>_ui_widget_logosize').closest('tr');
576 var ui_widget_countryselector_row = $('#woocommerce_<?php echo $this->id; ?>_ui_widget_countryselector').closest('tr');
577 var ui_widget_groupcountries_row = $('#woocommerce_<?php echo $this->id; ?>_ui_widget_groupcountries').closest('tr');
578 var ui_widget_groupcc_row = $('#woocommerce_<?php echo $this->id; ?>_ui_widget_groupcc').closest('tr');
579 var ui_widget_groupcc_title_row = $('#woocommerce_<?php echo $this->id; ?>_ui_widget_groupcc_title').closest('tr');
580
581 var ui_mode = $('#woocommerce_<?php echo $this->id; ?>_ui_mode');
582 var ui_widget_groupcountries = $('#woocommerce_<?php echo $this->id; ?>_ui_widget_groupcountries');
583 var ui_widget_groupcc = $('#woocommerce_<?php echo $this->id; ?>_ui_widget_groupcc');
584
585 parseVisibility();
586 function parseVisibility() {
587 $('.ui-identifier').closest('tr').show();
588
589 if(api_type.val() == 'live') {
590 $('.mc-test-link').hide();
591 $('.api-test').closest('tr').hide();
592 } else {
593 $('.mc-test-link').show();
594 $('.api-live').closest('tr').hide();
595 }
596
597 if(ui_mode.val() == 'inline') {
598 ui_widget_title_row.hide();
599 ui_widget_logosize_row.hide();
600 ui_widget_countryselector_row.hide();
601 ui_widget_groupcountries_row.hide();
602 ui_widget_groupcc_row.hide();
603 ui_widget_groupcc_title_row.hide();
604 } else {
605 ui_inline_uselogo_row.hide();
606
607 if(ui_widget_groupcountries.prop('checked')) {
608 ui_widget_countryselector_row.hide();
609 }
610 if(!ui_widget_groupcc.prop('checked')) {
611 ui_widget_groupcc_title_row.hide();
612 }
613 }
614 }
615
616 api_type.on('change', parseVisibility);
617 ui_mode.on('change', parseVisibility);
618 ui_widget_groupcountries.on('change', parseVisibility);
619 ui_widget_groupcc.on('change', parseVisibility);
620
621 });
622 </script>
623 <?php
624 }
625
626 public function payment_fields() {
627
628 if($this->settings['ui_mode'] == 'inline') {
629
630 ?>
631 <ul class="makecommerce-picker">
632 <?php foreach($this->_banklinks as $method): ?>
633 <li class="makecommerce-picker-method">
634 <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; ?>"/>
635 <label for="makecommerce_method_picker_<?php echo $method->country.'_'.$method->name; ?>">
636 <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>
637 <?php if(in_array($this->settings['ui_inline_uselogo'], array('logo', 'text_logo'))) : ?>
638 <div><img src="<?php echo $this->getImageUrl($method->name); ?>" title="<?php echo ucfirst($method->name); ?>" /></div>
639 <?php endif; ?>
640 </label>
641 </li>
642 <?php endforeach; ?>
643 <?php foreach($this->_cards as $method): ?>
644 <li class="makecommerce-picker-method">
645 <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; ?>"/>
646 <label for="makecommerce_method_picker_<?php echo 'card_'.$method->name; ?>">
647 <span class="makecommerce-method-title"><?php if(in_array($this->settings['ui_inline_uselogo'], array('text', 'text_logo'))) { echo ucfirst($method->name); } ?></span>
648 <?php if(in_array($this->settings['ui_inline_uselogo'], array('logo', 'text_logo'))) : ?>
649 <div><img src="<?php echo $this->getImageUrl($method->name); ?>" title="<?php echo ucfirst($method->name); ?>" /></div>
650 <?php endif; ?>
651 </label>
652 </li>
653 <?php endforeach; ?>
654 </ul>
655 <?php
656
657 } else {
658 ?>
659 <select id="<?php echo $this->id; ?>" name="PRESELECTED_METHOD_<?php echo $this->id; ?>">
660 <option value=""></option>
661 <?php foreach($this->_banklinks as $method): ?>
662 <option value="<?php echo $method->country.'_'.$method->name; ?>"><?php echo strtoupper($method->country).' - '.ucfirst($method->name); ?></option>
663 <?php endforeach; ?>
664 <?php foreach($this->_cards as $method): ?>
665 <option value="card_<?php echo $method->name; ?>"><?php echo ucfirst($method->name); ?></option>
666 <?php endforeach; ?>
667 </select>
668 <ul class="makecommerce-picker">
669 <?php
670
671 if($this->_banklinks || $this->_cards) {
672 $defaultCountry = $this->getDefaultCountry();
673 ?>
674 <?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') ) : ?>
675 <div class="makecommerce_country_picker_countries">
676 <?php foreach(array_keys($this->_banklinks_grouped) as $country): ?>
677 <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 } ?>
678 <?php endforeach; ?>
679 <?php if($this->settings['ui_widget_countryselector'] == 'dropdown') : ?>
680 <select name="makecommerce_country_picker_select">
681 <?php foreach(array_keys($this->_banklinks_grouped) as $country): ?>
682 <option value="<?php echo $country; ?>" <?php if($defaultCountry == $country) echo 'selected="selected" '; ?>><?php echo $this->getCountryName($country); ?></option>
683 <?php endforeach; ?>
684 <option value="card" style="display:none;"></option>
685 </select>
686 <?php endif; ?>
687 </div>
688 <?php endif; ?>
689 <?php
690 $banklinks_grouped = $this->_banklinks_grouped;
691 $banklinks_grouped['other'] = array();
692 ?>
693 <?php foreach($banklinks_grouped as $country => $methods): ?>
694 <li class="makecommerce-picker-country">
695 <?php if($this->settings['ui_widget_groupcountries'] == 'yes') : ?>
696 <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>
697 <?php endif; ?>
698 <div class="makecommerce_country_picker_methods" id="makecommerce_country_picker_methods_<?php echo $country; ?>">
699 <?php foreach($methods as $method): ?>
700 <div class="makecommerce-banklink-picker" banklink_id="<?php echo $method->country.'_'.$method->name; ?>">
701 <img src="<?php echo $this->getImageUrl($method->name); ?>" title="<?php echo ucfirst($method->name); ?>" />
702 </div>
703 <?php endforeach; ?>
704 <?php if($this->_cards && $this->settings['ui_widget_groupcc'] == 'no') : ?>
705 <div class="breaker"></div>
706 <?php foreach($this->_cards as $method): ?>
707 <div class="makecommerce-banklink-picker" banklink_id="card_<?php echo $method->name; ?>">
708 <img src="<?php echo $this->getImageUrl($method->name); ?>" title="<?php echo ucfirst($method->name); ?>" />
709 </div>
710 <?php endforeach; ?>
711 <?php elseif($country == 'other') :?>
712 <p class="no-methods"><?php _e('No payment methods for selected country' ,'wc_makecommerce_domain');?></p>
713 <?php endif; ?>
714 </div>
715 </li>
716 <?php endforeach; ?>
717 <?php if($this->_cards && $this->settings['ui_widget_groupcc'] == 'yes') : ?>
718 <li class="makecommerce-picker-country">
719 <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>
720 <div class="makecommerce_country_picker_methods" id="makecommerce_country_picker_methods_card">
721 <?php foreach($this->_cards as $method): ?>
722 <div class="makecommerce-banklink-picker" banklink_id="card_<?php echo $method->name; ?>">
723 <img src="<?php echo $this->getImageUrl($method->name); ?>" title="<?php echo ucfirst($method->name); ?>" />
724 </div>
725 <?php endforeach; ?>
726 </div>
727 </li>
728 <?php endif; ?>
729 <?php
730 }
731 ?>
732 </ul>
733 <div class="mc-clear-both"></div>
734 <script type="text/javascript">
735 var makecommerceId = '<?php echo $this->id; ?>';
736 var selectedCountry = '<?php echo $defaultCountry; ?>';
737
738 var logosize = '<?php echo $this->settings['ui_widget_logosize']; ?>';
739 jQuery('div.makecommerce_country_picker_methods').addClass('logosize-'+logosize);
740
741 <?php if(count($banklinks_grouped) == 1): ?>
742 jQuery('li.makecommerce-picker-country').show();
743 jQuery('div.makecommerce_country_picker_countries').hide();
744 jQuery('li.makecommerce-picker-country > input, li.makecommerce-picker-country > label').hide();
745 <?php else: ?>
746 makecommercePick();
747
748 jQuery('body').on('change', 'select[name=makecommerce_country_picker_select]', function() {
749 selectedCountry = jQuery(this).val();
750 jQuery('input[name=makecommerce_country_picker]').removeAttr('checked');
751 makecommercePick();
752 });
753 jQuery('body').on('change', 'input[name=makecommerce_country_picker]', function() {
754 selectedCountry = jQuery(this).val();
755 jQuery('select[name=makecommerce_country_picker_select]').val(selectedCountry);
756 makecommercePick();
757 });
758
759 function makecommercePick() {
760 jQuery('select#'+makecommerceId).val('');
761 jQuery('div.makecommerce-banklink-picker').removeClass('selected');
762 jQuery('label.makecommerce_country_picker_label').removeClass('selected');
763
764 jQuery('li.makecommerce-picker-country').hide();
765 jQuery('div#makecommerce_country_picker_methods_' + selectedCountry).parent().show();
766 jQuery('label[for=makecommerce_country_picker_' + selectedCountry + ']').addClass('selected');
767 }
768 <?php endif; ?>
769
770 jQuery('div.makecommerce-banklink-picker').on('click', function() {
771 var banklink_id = jQuery(this).attr('banklink_id');
772 jQuery('select#'+makecommerceId).val(banklink_id);
773
774 jQuery('div.makecommerce-banklink-picker').removeClass('selected');
775 jQuery(this).addClass('selected');
776 });
777 </script>
778 <?php
779 }
780
781 if($this->settings['ui_open_by_default'] == 'yes') {
782 ?>
783 <script type="text/javascript">
784 jQuery('input#payment_method_<?php echo $this->id; ?>').trigger('click');
785 </script>
786 <?php
787 }
788 }
789
790
791
792
793
794 protected function getCountryName($slug) {
795 switch($slug) {
796 case 'ee': return __('Estonia', 'wc_makecommerce_domain'); break;
797 case 'lv': return __('Latvia', 'wc_makecommerce_domain'); break;
798 case 'lt': return __('Lithuania', 'wc_makecommerce_domain'); break;
799 case 'fi': return __('Finland', 'wc_makecommerce_domain'); break;
800 }
801 return $slug;
802 }
803
804 private function getDefaultCountry() {
805 if ($this->_getWooCommerce()->customer) {
806 $customerCountry = strtolower($this->_getWooCommerce()->customer->get_shipping_country());
807 if(array_key_exists($customerCountry, $this->_banklinks_grouped)) {
808 return $customerCountry;
809 } else {
810 return 'other';
811 }
812 }
813
814 $localeToCountry = array(
815 'et' => 'ee',
816 'lv' => 'lv',
817 'lt' => 'lt',
818 'fi' => 'fi',
819 );
820 if(array_key_exists(get_locale(), $localeToCountry)) {
821 return $localeToCountry[get_locale()];
822 }
823
824 return key($this->_banklinks_grouped);
825 }
826
827
828 protected function getImageUrl($methodName) {
829 //$imageUrlPath = 'https://static.maksekeskus.ee/img/channel/lnd/';
830 $imageUrlPath = $this->MK->getEnvUrls()->staticsUrl.'img/channel/lnd/';
831 return $imageUrlPath.$methodName.'.png';
832 }
833
834 public function validate_fields() {
835 $selected = isset($_POST['PRESELECTED_METHOD_' . $this->id]) ? sanitize_text_field($_POST['PRESELECTED_METHOD_' . $this->id]) : false;
836
837 if (!$selected) {
838 wc_add_notice(__('Please select suitable payment option!', 'wc_makecommerce_domain'), 'error');
839 } else {
840 // is this used??
841 $this->_getWooCommerce()->session->makecommerce_preselected_method = $selected;
842 }
843
844 return true;
845 }
846
847
848 public function process_payment($orderId) {
849
850 $order = new WC_Order($orderId);
851
852 $selected = isset($_POST['PRESELECTED_METHOD_' . $this->id]) ? sanitize_text_field($_POST['PRESELECTED_METHOD_' . $this->id]) : false;
853
854 if (!empty($selected)) {
855
856 update_post_meta($order->id, '_makecommerce_preselected_method', $selected);
857
858 $request_body = array(
859 'transaction' => array(
860 'amount' => round($order->order_total, 2),
861 'currency' => $order->get_order_currency(),
862 'reference' => $order->id,
863 'transaction_url' => array(
864 'return_url' => array(
865 'url' => $this->payment_return_url,
866 'method' => 'POST',
867 ),
868 'cancel_url' => array(
869 'url' => $this->payment_return_url_cancel,
870 'method' => 'POST',
871 ),
872 'notification_url' => array(
873 'url' => $this->payment_return_url_m2m,
874 'method' => 'POST',
875 ),
876 ),
877 ),
878 'customer' => array(
879 'ip' => $_SERVER['REMOTE_ADDR'],
880 'country' => strtolower($order->billing_country),
881 'locale' => strtolower(substr(get_locale(), 0, 2)),
882 ),
883 );
884 $transaction = $this->MK->createTransaction($request_body);
885
886 if(isset($transaction->id)) {
887 update_post_meta($order->id, '_makecommerce_transaction_id', $transaction->id);
888 if (substr($selected, 0, 5) == 'card_') {
889 $redirect_url = $order->get_checkout_payment_url( true );
890 } else {
891 $redirect_url = false;
892 foreach ($transaction->payment_methods->banklinks as $banklink) {
893 if ($banklink->country.'_'.$banklink->name === $selected) {
894 $redirect_url = $banklink->url;
895 }
896 }
897 if (!$redirect_url) {
898 $redirect_url = $this->_getRedirectUrl($selected).$transaction->id;
899 }
900 }
901 return array(
902 'result' => 'success',
903 'redirect' => $redirect_url
904 );
905 }
906 }
907
908 wc_add_notice(__('An error occured when trying to process payment!', 'wc_makecommerce_domain'), 'error');
909 return array(
910 'result' => 'failure',
911 );
912 }
913
914
915 protected function _getRedirectUrl($selected) {
916 foreach($this->_banklinks as $method) {
917 if($selected == $method->country.'_'.$method->name)
918 return $method->url;
919 }
920
921 return false;
922 }
923
924 /*
925 protected function _getOrderConfirmationUrl($order) {
926
927 global $sitepress;
928 if(isset($sitepress)) {
929 $baseUrl = $sitepress->language_url( ICL_LANGUAGE_CODE );
930 $lang = ICL_LANGUAGE_CODE;
931 } else {
932 $baseUrl = site_url();
933 $lang = strtolower(substr(get_locale(), 0, 2));
934 }
935 $url = add_query_arg( array(
936 'makecommerce_card_pay' => '1',
937 'order_id' => $order->id,
938 'lang' => $lang,
939 ), $baseUrl);
940
941 // $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))));
942 return $url;
943 }
944
945 */
946
947 public function receipt_page($orderId) {
948 $order = new WC_Order($orderId);
949 if (substr(get_post_meta($orderId, '_makecommerce_preselected_method', true), 0, 5) == 'card_' && $order->get_status() == 'pending') {
950 echo "<br>".__('The order is still awaiting your payment', 'wc_makecommerce_domain')."<br>";
951 echo $this->generateCardForm($order);
952 }
953 }
954
955
956 public function generateCardForm($orderId) {
957 $order = new WC_Order($orderId);
958 $scriptSrc = htmlspecialchars($this->MK->getEnvUrls()->checkoutjsUrl.'checkout.js');
959 $transactionId = get_post_meta($order->id, '_makecommerce_transaction_id', true);
960 $idReference = $order->id;
961 $jsParams = array(
962 'key' => $this->MK->getPublishableKey(),
963 'transaction' => $transactionId,
964 'selector' => '#submit_banklinkmakecommerce_payment_form',
965 'amount' => round($order->order_total, 2),
966 'locale' => !empty($_GET['lang']) ? $_GET['lang'] : (defined('ICL_LANGUAGE_CODE') ? ICL_LANGUAGE_CODE : strtolower(substr(get_locale(), 0, 2))),
967 'open-on-load' => 'true',
968 'client-name' => ($this->settings['cc_pass_cust_data'] == 'yes' ? (string) ($order->billing_first_name . ' ' . $order->billing_last_name) : ''),
969 'email' => ($this->settings['cc_pass_cust_data'] == 'yes' ? (string) $order->billing_email : ''),
970 'name' => get_option('mc_shop_name', ''),
971 'description' => __('Order', 'woocommerce') . '&nbsp;' . (string) $idReference,
972 'completed' => 'makecommerce_cc_complete',
973 'currency' => 'EUR',
974 'backdrop-close' => 'false',
975 );
976 ?>
977 <script type="text/javascript">
978 function makecommerce_cc_complete(data) {
979 if(data.paymentToken) {
980 jQuery('div.mc-processing-message').show();
981
982 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>');
983 for(var key in data) {
984 submitform.append(jQuery('<input type="hidden" name="'+key+'" />').val(data[key]));
985 }
986 jQuery('body').append(submitform);
987 submitform.submit();
988 }
989 }
990 </script>
991 <form id="cc_form" >
992 <input type="submit" class="button-alt" id="submit_banklinkmakecommerce_payment_form" value="<?php echo __('Pay with credit card', 'wc_makecommerce_domain'); ?>" />
993 <script type="text/javascript" src="<?php echo $scriptSrc; ?>" <?php echo $this->_toHtmlAttributes($jsParams); ?>></script>
994 </form>
995 <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>
996 <?php
997 }
998
999
1000 protected function _toHtmlAttributes($input) {
1001 $result = array();
1002 foreach ($input as $key => $value) {
1003 $result[] = 'data-' . htmlspecialchars($key) . '=' . '"' . htmlspecialchars($value) . '"';
1004 }
1005 return implode(' ', $result);
1006 }
1007
1008 public function makecommerce_return_trigger($vars) {
1009 $vars[] = 'makecommerce_return';
1010 $vars[] = 'ajax_content';
1011 $vars[] = 'makecommerce_card_pay';
1012 return $vars;
1013 }
1014
1015 public function makecommerce_return_trigger_check() {
1016 if (intval(get_query_var('makecommerce_return')) > 0) {
1017 $return_url = mk_check_payment();
1018 if (intval(get_query_var('ajax_content'))) {
1019 echo json_encode(array('redirect' => $return_url));
1020 } else {
1021 wp_redirect($return_url);
1022 }
1023 exit;
1024 }
1025 }
1026
1027
1028 } // class ends here
1029
1030 New woocommerce_makecommerce(true);
1031
1032 // Woocommerce Multilingual overrides
1033 if (is_plugin_active('woocommerce-multilingual/wpml-woocommerce.php')) {
1034 add_filter( 'get_post_metadata', 'mc_override_payment_method_string', 5, 4 );
1035 }
1036
1037 } // init ends here
1038
1039
1040
1041
1042 function woocommerce_payment_makecommerce_add($methods) {
1043 $methods[] = 'woocommerce_makecommerce';
1044 return $methods;
1045 }
1046
1047 add_action('plugins_loaded', 'woocommerce_payment_makecommerce_init');
1048 add_action('woocommerce_payment_gateways', 'woocommerce_payment_makecommerce_add');
1049
1050
1051 // Banklinks update wrapper for action
1052 function update_mc_banklinks() {
1053 $obj = new woocommerce_makecommerce(true);
1054 $obj->mc_banklinks_reload(true);
1055 }
1056
1057
1058 function mc_override_payment_method_string( $check, $object_id, $meta_key, $single ){
1059 if($meta_key == '_payment_method_title') {
1060 $payment_method = get_post_meta( $object_id, '_payment_method', true );
1061 if( $payment_method == 'makecommerce' ){
1062 $payment_gateways = WC()->payment_gateways->payment_gateways();
1063 if( isset( $payment_gateways[ $payment_method ] ) ){
1064 $title = $payment_gateways[ $payment_method ]->title;
1065 return $title;
1066 }
1067 }
1068 }
1069 return $check;
1070 }
1071