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

1,302 lines 54.5 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__)) . '/languages/');
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.6.2';
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 protected $_paylater = array();
41 protected $_paylater_grouped;
42
43 protected $MK;
44 protected $_init;
45
46 public function __construct($init = false) {
47 $this->_init = $init;
48 $langGet = "";
49
50 //set language
51 //wpml
52 if (function_exists('icl_object_id') && defined('ICL_LANGUAGE_CODE')) {
53 $langGet = "&lang1=".ICL_LANGUAGE_CODE;
54 } else {
55 //if wpml wasnt set, try polylang
56 if (function_exists('pll_current_language')) {
57 //language param will be set but is not implemented in further steps.
58 $langGet = "&lang1=".pll_current_language();
59 }
60 }
61
62 //set return url's
63 $this->payment_return_url = site_url('/?makecommerce_return=1'.$langGet);
64 $this->payment_return_url_m2m = site_url('/?makecommerce_return=1&ajax_content=1'.$langGet);
65 $this->payment_return_url_cancel = site_url('/?makecommerce_return=1'.$langGet);
66
67 // Load the form fields.
68 $this->init_form_fields();
69
70 // Load the settings.
71 $this->init_settings();
72
73 $this->initBanklinks();
74
75 $this->title = '';
76 if (!empty($this->settings['ui_widget_title'])) {
77 $this->title = $this->settings['ui_widget_title'];
78 }
79
80 //set the correct language
81 if (isset($_GET["lang1"])) {
82 do_action('wpml_switch_language', $_GET["lang1"]);
83
84 if (!empty($this->settings['ui_widget_title_'.get_locale()]) || !empty($this->settings['ui_widget_title_'.substr(get_locale(), 0, 2)])) {
85 if (!empty($this->settings['ui_widget_title_'.substr(get_locale(), 0, 2)])) {
86 $this->title = $this->settings['ui_widget_title_'.substr(get_locale(), 0, 2)];
87 } else {
88 $this->title = $this->settings['ui_widget_title_'.get_locale()];
89 }
90 } else if (!empty($this->settings['ui_widget_title'])) {
91 $this->title = $this->settings['ui_widget_title'];
92 } else {
93 $this->title = '';
94 }
95 } else {
96 //coming from admin, set language according to order wpml_language. This meta is set for both polylang wpml
97 if (isset($_POST["meta"])) {
98 foreach ($_POST["meta"] as $key=>$value) {
99 if (is_array($value)) {
100 if ($value["key"] == "wpml_language") {
101 do_action('wpml_switch_language', $value["value"]);
102 }
103 }
104 }
105 }
106
107 $glocale = get_locale();
108 if ($glocale == "en_US") {
109 $glocale = "en_GB";
110 }
111
112 if (function_exists('pll_languages_list') && (!empty($this->settings['ui_widget_title_'.pll_current_language('locale')]) || !empty($this->settings['ui_widget_title_'.get_locale()]))) {
113 if (!empty($this->settings['ui_widget_title_'.pll_current_language('locale')])) {
114 $this->title = $this->settings['ui_widget_title_'.pll_current_language('locale')];
115 } else {
116 $this->title = $this->settings['ui_widget_title_'.get_locale()];
117 }
118 } else {
119 if (defined('ICL_LANGUAGE_CODE') && (!empty($this->settings['ui_widget_title_'.ICL_LANGUAGE_CODE]) || !empty($this->settings['ui_widget_title_'.$glocale]))) {
120 //ICL_LANGUAGE_CODE seems to be in different format in different places. Perhaps its a mistake somewhere but we cant ignore or change it as it is already set previously
121 if (!empty($this->settings['ui_widget_title_'.ICL_LANGUAGE_CODE])) {
122 $this->title = $this->settings['ui_widget_title_'.ICL_LANGUAGE_CODE];
123 } else {
124 $this->title = $this->settings['ui_widget_title_'.$glocale];
125 }
126 }
127 }
128 }
129
130 $this->method_title = 'MakeCommerce';
131 $this->description = true;
132
133 $this->enabled = $this->settings['active'];
134
135 $this->MK = mk_get_api();
136 if (!$this->MK && $this->_init) {
137 add_action( 'admin_notices', array(&$this, 'makecommerce_api_info_missing') );
138 }
139
140 $this->supports = array(
141 'subscriptions',
142 'subscription_cancellation',
143 'subscription_suspension',
144 'subscription_reactivation',
145 'subscription_amount_changes',
146 'subscription_date_changes',
147 'subscription_payment_method_change',
148 'products',
149 'refunds'
150 );
151
152 add_filter('query_vars', array(&$this, 'makecommerce_return_trigger'));
153 add_action('template_redirect', array(&$this, 'makecommerce_return_trigger_check'));
154 add_action('woocommerce_scheduled_subscription_payment_' . $this->id, array($this, 'process_subscripion_payment_start'), 10, 2);
155 add_action('scheduled_subscription_payment_' . $this->id, array($this, 'process_subscripion_payment_start'), 10, 2);
156
157 if($this->_init == false) {
158 add_action('woocommerce_update_options_payment_gateways', array(&$this, 'process_admin_options'));
159 add_action('woocommerce_update_options_payment_gateways_' . $this->id, array(&$this, 'process_admin_options'));
160 add_action('woocommerce_receipt_' . $this->id, array($this, 'receipt_page'));
161
162 add_action('woocommerce_admin_order_data_after_order_details', array(&$this, 'admin_order_page'), 10, 1);
163
164 wp_enqueue_script('jquery');
165 wp_enqueue_style('makecommerce', plugins_url('/css/makecommerce.css', __FILE__), array(), $this->version);
166 }
167 if (is_admin()) {
168 add_action( 'wp_ajax_mc_banklinks_reload', array(&$this, 'mc_banklinks_reload') );
169
170 require_once(ABSPATH.'wp-admin/includes/plugin.php');
171 $wc_version = get_plugin_data(__DIR__.'/../woocommerce/woocommerce.php');
172 if ((double)$wc_version['Version'] < 2.6) {
173 function sample_admin_notice__error() {
174 $class = 'notice notice-error';
175 $wc_version = get_plugin_data(__DIR__.'/../woocommerce/woocommerce.php');
176 $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']);
177 printf( '<div class="%1$s"><p>%2$s</p></div>', $class, $message );
178 }
179 add_action( 'admin_notices', 'sample_admin_notice__error' );
180 }
181 }
182
183 }
184
185 public function get_title() {
186 // TODO: add multilingual support
187 return $this->title;
188 }
189
190 protected function _getWooCommerce() {
191 global $woocommerce;
192 return $woocommerce;
193 }
194
195 public function is_valid_for_use() {
196 return true;
197 }
198
199 public function is_available() {
200 if (!($this->settings['active'] == "yes" && mk_is_api_set())) {
201 return false;
202 }
203 return parent::is_available();
204 }
205
206 private function initBanklinks() {
207 global $wpdb;
208 global $mkDbTable;
209 $this->_banklinks = array();
210 $has_subscriptions = class_exists('WC_Subscriptions_Cart') && WC_Subscriptions_Cart::cart_contains_subscription();
211
212 $tableName = $wpdb->prefix . $mkDbTable;
213 $methods = $wpdb->get_results('SELECT * FROM '.$tableName);
214
215 if(count($methods)) {
216 if(is_admin() && $this->_init == true && get_option( 'mc_banklinks_api_type' ) != get_option('mk_api_type', false)) {
217 // ??
218 add_action( 'admin_notices', array(&$this, 'makecommerce_banklinks_list_type_notice') );
219 }
220 $banklinks = array();
221 $banklinks_grouped = array();
222 $cards = array();
223 $paylater = array();
224 $paylater_grouped = array();
225 foreach($methods as $method) {
226 if($method->type == 'banklink'|| $method->type == 'other') {
227 if (!$has_subscriptions) {
228 $banklinks[] = $banklinks_grouped[$method->country][] = $method;
229 }
230 } elseif($method->type == 'card') {
231 $cards[] = $method;
232 } elseif($method->type == 'payLater') {
233 if (!$has_subscriptions) {
234 $paylater[] = $paylater_grouped[$method->country][] = $method;
235 }
236 }
237 }
238
239 usort($banklinks, array($this, 'orderBanklinks'));
240 foreach($banklinks_grouped as &$country) {
241 usort($country, array($this, 'orderBanklinks'));
242 }
243
244 $this->_banklinks = $banklinks;
245 $this->_banklinks_grouped = $banklinks_grouped;
246 $this->_cards = $cards;
247 $this->_paylater = $paylater;
248 $this->_paylater_grouped = $paylater_grouped;
249 remove_action('admin_notices', array(&$this, 'makecommerce_banklinks_list_empty'), 30);
250 } elseif(is_admin() && $this->_init == true) {
251 add_action('admin_notices', array(&$this, 'makecommerce_banklinks_list_empty'), 30);
252 }
253 }
254
255 private function orderBanklinks($a, $b) {
256 $order = array_map('trim', explode(",", $this->settings['ui_chorder']));
257
258 $posA = array_search($a->name, $order);
259 $posB = array_search($b->name, $order);
260
261 if($posA === $posB) return $a->id > $b->id ? 1 : -1;
262 if($posA === FALSE) return 1;
263 if($posB === FALSE) return -1;
264
265 return $posA > $posB ? 1 : -1;
266 }
267
268
269
270 // ===== WC admin related stuff ======
271
272
273 public function makecommerce_api_info_missing() {
274 ?>
275 <div class="notice notice-error is-dismissible">
276 <p>
277 <?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'); ?>
278 <?php if (mk_wc_version("3.4.0")) { ?>
279 <a href="<?php echo admin_url('admin.php?page=wc-settings&tab=advanced&section=mk_api'); ?>"><?php echo __('Click here to enter them', 'wc_makecommerce_domain'); ?></a>
280 <?php } else { ?>
281 <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>
282 <?php } ?>
283 </p>
284 </div>
285 <?php
286 }
287
288 public function makecommerce_banklinks_list_empty() {
289 ?>
290 <div class="notice notice-error is-dismissible">
291 <p>
292 <?php echo __('The payment methods list for MakeCommerce payment module is empty.', 'wc_makecommerce_domain'); ?>
293 <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>
294 </p>
295 </div>
296 <?php
297 }
298
299 public function makecommerce_banklinks_list_type_notice() {
300 ?>
301 <div class="notice notice-error is-dismissible">
302 <p>
303 <?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'); ?>
304 <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>
305 </p>
306 </div>
307 <?php
308 }
309
310
311 public function init_form_fields() {
312
313 if ( function_exists('icl_object_id') && !function_exists('pll_languages_list')) {
314 $languages = apply_filters('wpml_active_languages', NULL, 'skip_missing=0');
315 }
316 else if (function_exists('pll_languages_list')) {
317 $pl_locales = pll_languages_list( array('fields'=>'locale') );
318 foreach ($pl_locales as $key => $locale_pl ) {
319 $language = array( 'id' => $key,
320 'code' => $locale_pl) ;
321 $languages[$locale_pl] = $language;
322 }
323 }
324
325 $this->form_fields = array();
326 $this->form_fields['logo'] = array('type' => 'title', 'description' => __get_logo_html());
327 $this->form_fields['active'] = array(
328 'title' => __('Enable/Disable', 'wc_makecommerce_domain'),
329 'type' => 'checkbox',
330 'label' => __('Enable MakeCommerce payments', 'wc_makecommerce_domain'),
331 'default' => 'no'
332 );
333 if (mk_wc_version("3.4.0")) $a = admin_url('admin.php?page=wc-settings&tab=advanced&section=mk_api'); else $a = admin_url('admin.php?page=wc-settings&tab=api&section=mk_api');
334 $this->form_fields['api_title'] = array(
335 'title' => __('MakeCommerce API', 'wc_makecommerce_domain'),
336 'description' => sprintf(__('Go to <a href="%s">API settings</a> to fill in the credentials', 'wc_makecommerce_domain'), $a),
337 'type' => 'title',
338 );
339 $this->form_fields['ui_title'] = array(
340 'title' => '<br>'.__('User Interface', 'wc_makecommerce_domain'),
341 'type' => 'title',
342 'class' => 'ui-identifier',
343 );
344 $this->form_fields['ui_open_by_default'] = array(
345 'title' => __('Set as default selection', 'wc_makecommerce_domain'),
346 'label' => __('MakeCommerce payments widget will be selected by default', 'wc_makecommerce_domain'),
347 'type' => 'checkbox',
348 'default' => 'yes',
349 'class' => 'ui-identifier',
350 );
351 $this->form_fields['ui_mode'] = array(
352 'title' => __('Display MC payment channels as', 'wc_makecommerce_domain'),
353 'type' => 'mc_hidden',
354 'default' => 'widget',
355 'options' => array(
356 'inline' => __('List', 'wc_makecommerce_domain'),
357 'widget' => __('Grouped to widget', 'wc_makecommerce_domain'),
358 ),
359 'class' => 'ui-identifier',
360 );
361
362
363 if (empty($languages)) {
364 $this->form_fields['ui_widget_title'] = array(
365 'title' => __('Payments widget title', 'wc_makecommerce_domain'),
366 'type' => 'text',
367 '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'),
368 'default' => __('Pay with bank-links or credit card', 'wc_makecommerce_domain'),
369 'class' => 'ui-identifier',
370 );
371 } else {
372 foreach ($languages as $language_code => $language) {
373 $this->form_fields['ui_widget_title_'.$language_code] = array(
374 'title' => __('Payments widget title', 'wc_makecommerce_domain').sprintf(' (%s)', $language_code),
375 'type' => 'text',
376 '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'),
377 'default' => $this->getLanguageVersion('Pay with bank-links or credit card', 'wc_makecommerce_domain', $language_code),
378 'class' => 'ui-identifier',
379 );
380 }
381 }
382 $this->form_fields['ui_inline_uselogo'] = array(
383 'title' => __('MC payment channels display style', 'wc_makecommerce_domain'),
384 'type' => 'select',
385 'default' => 'logo',
386 'options' => array(
387 'logo' => __('Logo', 'wc_makecommerce_domain'),
388 'text_logo' => __('Text & logo', 'wc_makecommerce_domain'),
389 'text' => __('Text', 'wc_makecommerce_domain'),
390 ),
391 'class' => 'ui-identifier',
392 );
393 $this->form_fields['ui_widget_logosize'] = array(
394 'title' => __('Size of payment channel logos', 'wc_makecommerce_domain'),
395 'type' => 'select',
396 'default' => 'medium',
397 'options' => array(
398 'small' => __('Small', 'wc_makecommerce_domain'),
399 'medium' => __('Medium', 'wc_makecommerce_domain'),
400 'large' => __('Large', 'wc_makecommerce_domain')
401 ),
402 'class' => 'ui-identifier',
403 );
404 $this->form_fields['ui_widget_groupcountries'] = array(
405 'title' => __('Group bank-links by countries', 'wc_makecommerce_domain'),
406 'type' => 'mc_hidden',
407 'default' => 'no',
408 'class' => 'ui-identifier',
409 );
410 $this->form_fields['ui_widget_countries_hidden'] = array(
411 'title' => __('Hide country selector', 'wc_makecommerce_domain'),
412 'label' => __('Do not display country selector (flags) at payment methods', 'wc_makecommerce_domain'),
413 'type' => 'checkbox',
414 'default' => 'no',
415 );
416 $this->form_fields['ui_widget_countryselector'] = array(
417 'title' => __('Country selector style', 'wc_makecommerce_domain'),
418 'type' => 'mc_hidden',
419 'default' => 'flag',
420 'options' => array(
421 'flag' => __('Flag', 'wc_makecommerce_domain'),
422 'dropdown' => __('Dropdown', 'wc_makecommerce_domain'),
423 ),
424 'class' => 'ui-identifier',
425 );
426 $this->form_fields['ui_widget_groupcc'] = array(
427 'title' => __('Group credit card into separate widget', 'wc_makecommerce_domain'),
428 'type' => 'mc_hidden',
429 'default' => 'no',
430 'class' => 'ui-identifier',
431 );
432 $this->form_fields['ui_chorder'] = array(
433 'title' => __('Define custom order of payment channels', 'wc_makecommerce_domain'),
434 'type' => 'text',
435 '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'),
436 'class' => 'ui-identifier',
437 );
438 $this->form_fields['ui_javascript'] = array(
439 'type' => 'ui_javascript',
440 );
441 $this->form_fields['cc_title'] = array(
442 'title' => '<br>'.__('Credit Card Settings', 'wc_makecommerce_domain'),
443 'type' => 'title',
444 );
445 $this->form_fields['cc_pass_cust_data'] = array(
446 'title' => __('Prefill Credit Card form with customer data', 'wc_makecommerce_domain'),
447 'type' => 'checkbox',
448 'default' => 'yes',
449 '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'),
450 );
451 $this->form_fields['adv_title'] = array(
452 'title' => '<hr><br>'.__('Advanced Settings', 'wc_makecommerce_domain'),
453 'type' => 'title',
454 );
455 $this->form_fields['reload_links'] = array(
456 'type' => 'mc_banklinks_reload',
457 'title' => __('Update payment methods', 'wc_makecommerce_domain'),
458 'description' => __('Update', 'wc_makecommerce_domain'),
459 'desc_tip' => __('This will update shop configuration from MakeCommerce servers.', 'wc_makecommerce_domain'),
460 );
461
462
463 }
464
465 //returns the translation for the given string from the specified locale.
466 public function getLanguageVersion($string, $textdomain, $locale)
467 {
468 $file = plugin_dir_path(__FILE__) . 'languages/'.$textdomain . '-' . $locale . '.mo';
469
470 if (file_exists($file)) {
471 $mo = new MO();
472 $mo->import_from_file($file);
473
474 if (isset($mo->entries)) {
475 if (isset($mo->entries[$string])) {
476 if (isset($mo->entries[$string]->translations)) {
477 if (isset($mo->entries[$string]->translations[0])) {
478 $string = $mo->entries[$string]->translations[0];
479 }
480 }
481 }
482 }
483 }
484
485 return $string;
486 }
487
488 // Needed to render mc_hidden type fields in admin
489 public function generate_mc_hidden_html( $key, $data ) {
490 $field_key = $this->get_field_key($key);
491 ob_start();
492 ?>
493 <tr style="display: none;">
494 <td colspan="2">
495 <input type="hidden" name="<?php echo $field_key; ?>" value="<?php echo $data['default']; ?>" />
496 </td>
497 </tr>
498 <?php
499 return ob_get_clean();
500 }
501
502
503 public function generate_mc_banklinks_reload_html( $key, $data ) {
504 $field = $this->get_field_key( $key );
505 $defaults = array(
506 'title' => '',
507 'disabled' => false,
508 'class' => '',
509 'css' => '',
510 'placeholder' => '',
511 'type' => 'text',
512 'desc_tip' => false,
513 'description' => '',
514 'custom_attributes' => array()
515 );
516
517 $data = wp_parse_args( $data, $defaults );
518
519 ob_start();
520 ?>
521 <tr valign="top">
522 <th scope="row" class="titledesc">
523 <label for="<?php echo esc_attr( $field ); ?>"><?php echo wp_kses_post( $data['title'] ); ?></label>
524 <?php echo $this->get_tooltip_html( $data ); ?>
525 </th>
526 <td class="forminp">
527 <fieldset>
528 <legend class="screen-reader-text"><span><?php echo wp_kses_post( $data['title'] ); ?></span></legend>
529 <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 ); ?> />
530 <script type="text/javascript">
531 jQuery('input#mc_banklinks_reload').on('click', function() {
532 init_mc_loading();
533 jQuery.ajax({
534 url: '<?php echo get_site_url(); ?>/wp-admin/admin-ajax.php',
535 type: 'POST',
536 data: 'action=mc_banklinks_reload',
537 success: function (output) {
538 if(output.data) {
539 alert(output.data);
540 } else {
541 alert('<?php echo __('There was an error with your update. Please try again.', 'wc_makecommerce_domain'); ?>');
542 }
543 },
544 complete: function() { stop_mc_loading(); }
545 });
546 });
547
548 function init_mc_loading() {
549 jQuery('input#mc_banklinks_reload').attr('disabled', 'disabled');
550 }
551
552 function stop_mc_loading() {
553 jQuery('input#mc_banklinks_reload').removeAttr('disabled');
554 }
555 </script>
556 </fieldset>
557 </td>
558 </tr>
559 <?php
560
561 return ob_get_clean();
562 }
563
564 public function mc_banklinks_reload($force = false) {
565 if ($force || (defined('DOING_AJAX') && DOING_AJAX)) {
566
567 global $wp_version;
568 global $wpdb;
569 global $mkDbTable;
570
571 $request_params = array(
572 'environment' => json_encode(array(
573 'system' => array('wordpress' => $wp_version, "php" => phpversion()),
574 'platform' => 'woocommerce '.$this->_getWooCommerce()->version,
575 'module' => $this->id.' '.$this->version,
576 )),
577 );
578
579 if (!$this->MK) {
580 return false;
581 }
582
583 try {
584 $shopConfig = $this->MK->getShopConfig($request_params);
585 $methods = $shopConfig->paymentMethods;
586 } catch (Exception $e) {
587 error_log(print_r($e, 1));
588 return false;
589 }
590
591 $tableName = $wpdb->prefix . $mkDbTable;
592 $sql = "select count(*) as isLogoUrlSet from information_schema.columns where table_name='".$tableName."' and column_name='logo_url'";
593 $methodstruct = $wpdb->get_results( $sql );
594 if ($methodstruct[0]->isLogoUrlSet == 0) {
595 $wpdb->query('ALTER TABLE '.$tableName.' ADD COLUMN logo_url varchar(250)');
596 }
597
598 $sql = "select count(*) as min_amount from information_schema.columns where table_name='".$tableName."' and column_name='min_amount'";
599 $methodstruct = $wpdb->get_results( $sql );
600 if ($methodstruct[0]->min_amount == 0) {
601 $wpdb->query('ALTER TABLE '.$tableName.' ADD COLUMN min_amount mediumint(9) default 0');
602 }
603 $sql = "select count(*) as max_amount from information_schema.columns where table_name='".$tableName."' and column_name='max_amount'";
604 $methodstruct = $wpdb->get_results( $sql );
605 if ($methodstruct[0]->max_amount == 0) {
606 $wpdb->query('ALTER TABLE '.$tableName.' ADD COLUMN max_amount mediumint(9) default 0');
607 }
608 $wpdb->query('TRUNCATE TABLE '.$tableName);
609
610 if(isset($methods->banklinks)) {
611 foreach($methods->banklinks as $method) {
612 $wpdb->insert($tableName, array('type' => 'banklink', 'country' => $method->country, 'name' => $method->name, 'url' => $method->url, 'logo_url' => $method->logo_url));
613 }
614 $updated = true;
615 }
616
617 if(isset($methods->cards)) {
618 foreach($methods->cards as $method) {
619 $wpdb->insert($tableName, array('type' => 'card', 'name' => $method->name, 'logo_url' => $method->logo_url));
620 }
621 $updated = true;
622 }
623
624 if(isset($methods->other)) {
625 foreach($methods->other as $method) {
626 $wpdb->insert($tableName, array('type' => 'other', 'country' => $method->country, 'name' => $method->name, 'url' => $method->url, 'logo_url' => $method->logo_url));
627 }
628 $updated = true;
629 }
630
631 if(isset($methods->payLater)) {
632 foreach($methods->payLater as $method) {
633 //not all payLater accounts have a max_amount parameter. Set them to NULL to avoid notice's
634 if (!isset($method->max_amount)) {
635 $method->max_amount = NULL;
636 }
637
638 $wpdb->insert($tableName, array('type' => 'payLater', 'country' => $method->country, 'name' => $method->name, 'url' => $method->url, 'logo_url' => $method->logo_url, 'min_amount' => $method->min_amount, 'max_amount' => $method->max_amount));
639 }
640 $updated = true;
641 }
642 if(isset($shopConfig) && strlen($shopConfig->name)) {
643 update_option( 'mc_shop_name', $shopConfig->name );
644 }
645
646 if ($updated) {
647 update_option( 'mc_banklinks_api_type', get_option('mk_api_type', false) );
648 }
649
650 if ($force) {
651 $this->initBankLinks();
652 return $updated;
653 }
654
655 if($updated) {
656 wp_send_json(array('success' => 1, 'data' => __('Update successfully completed!', 'wc_makecommerce_domain')));
657 exit;
658 }
659
660 wp_send_json(array('success' => 0, 'data' => __('There was an error with your update. Please try again.', 'wc_makecommerce_domain')));
661 exit;
662 }
663 die();
664 }
665
666
667 public function process_subscripion_payment_start($amount_to_charge, $renewal_order, $product_id = '') {
668 $result = $this->process_subscription_payment($renewal_order, $amount_to_charge);
669 if ( is_wp_error( $result ) ) {
670 WC_Subscriptions_Manager::process_subscription_payment_failure_on_order($renewal_order, $product_id);
671 } else {
672 WC_Subscriptions_Manager::process_subscription_payments_on_order($renewal_order);
673 }
674 }
675
676 public function process_subscription_payment($order, $amount = 0) {
677 if (0 === $amount) {
678 $order->payment_complete();
679 return true;
680 } // if ('processing' === $order->get_status() || 'completed' === $order->get_status()){ // ('processing' === $order->get_status())
681 if ('processing' === $order->get_status() || 'completed' === $order->get_status()){
682 return true;
683 }
684 $oorder_id = @WC_Subscriptions_Renewal_Order::get_parent_order_id($order);
685 $payment_token = get_post_meta($oorder_id, '_makecommerce_payment_token', true);
686 $payment_token_valid_until = get_post_meta($oorder_id, '_makecommerce_payment_token_valid_until', true);
687 error_log($payment_token.'=>'. $payment_token_valid_until.'=>'. $order->get_status());
688 $body = array(
689 'transaction' => array(
690 'amount' => (string)sprintf("%.2f", $amount),
691 'currency' => method_exists($order, 'get_currency') ? $order->get_currency() : $order->currency,
692 'reference' => $order->get_id(),
693 ),
694 'customer' => array(
695 'email' => $order->get_billing_email(),
696 'ip' => $order->get_customer_ip_address(),
697 'country' => strtolower($order->get_billing_country()),
698 'locale' => strtolower(substr(get_locale(), 0, 2)),
699 )
700 );
701 $transaction = $this->MK->createTransaction($body);
702 $paymentRequest = array(
703 'amount' => (string)sprintf("%.2f", $transaction->amount),
704 'currency' => $transaction->currency,
705 'token' => $payment_token
706 );
707 try {
708 $payment = $this->MK->createPayment($transaction->id, $paymentRequest);
709 } catch (Exception $e) {
710 error_log('Payment failed ['.$e->getMessage().']');
711 $order->add_order_note(__('Unable to renew subscription', 'wc_makecommerce_domain')."\r\n".$e->getMessage());
712 return new WP_Error('makecommerce_payment_declined', __( 'Renewal payment was declined', 'wc_makecommerce_domain' ) );
713 }
714 $orderNote = array();
715 $orderNote[] = __('Transaction ID', 'wc_makecommerce_domain') . ': <a target=_blank href="'.$this->MK->getEnvUrls()->merchantUrl.'/merchant/shop/deals/detail.html?id='. $transaction->id .'">'.$transaction->id.'</a>';
716 $orderNote[] = __('Payment option', 'wc_makecommerce_domain') . ': ' . get_post_meta($order->get_id(), '_makecommerce_preselected_method', true);
717 $order->add_order_note(implode("\r\n", $orderNote));
718 $order->payment_complete($transaction->id);
719 return true;
720 }
721
722 public function admin_order_page($order) {
723 //
724 }
725
726 public function process_refund($order_id, $amount = null, $comment = '') {
727 if ($this->MK) {
728 try {
729 $order = new WC_Order($order_id);
730 $transactionId = $order->get_transaction_id();
731
732 if ( empty($transactionId) ) {
733 $transactionId = get_post_meta( $order_id, '_makecommerce_transaction_id', true);
734 }
735
736 if($response = $this->MK->createRefund($transactionId, array('amount' => sprintf("%.2f", $amount), 'comment' => ($comment ? : 'refund')))) {
737 if($status = (string)$response->transaction->status) {
738 switch($status) {
739 case self::MC_REFUNDED:
740 $order->add_order_note(sprintf(__('Refund completed for amount %s', 'wc_makecommerce_domain'), $amount));
741 return true;
742 break;
743 case self::MC_PART_REFUNDED:
744 $order->add_order_note(sprintf(__('Partial refund completed for amount %s', 'wc_makecommerce_domain'), $amount));
745 return true;
746 break;
747 }
748 }
749 }
750 return false;
751
752 } catch (Exception $e) {
753 return new WP_Error('makecommerce_refund_error', $e->getMessage());
754 }
755
756 return false;
757 }
758 return false;
759 }
760
761
762 // ====== storefront related stuff =========
763
764 public function generate_ui_javascript_html( $key, $data ) {
765 ?>
766 <script type="text/javascript">
767 jQuery(document).ready(function($) {
768
769 var api_type = $('#woocommerce_<?php echo $this->id; ?>_api_type');
770
771 var ui_inline_uselogo_row = $('#woocommerce_<?php echo $this->id; ?>_ui_inline_uselogo').closest('tr');
772 var ui_widget_title_row = $('#woocommerce_<?php echo $this->id; ?>_ui_widget_title').closest('tr');
773 var ui_widget_logosize_row = $('#woocommerce_<?php echo $this->id; ?>_ui_widget_logosize').closest('tr');
774 var ui_widget_countryselector_row = $('#woocommerce_<?php echo $this->id; ?>_ui_widget_countryselector').closest('tr');
775 var ui_widget_groupcountries_row = $('#woocommerce_<?php echo $this->id; ?>_ui_widget_groupcountries').closest('tr');
776 var ui_widget_groupcc_row = $('#woocommerce_<?php echo $this->id; ?>_ui_widget_groupcc').closest('tr');
777 var ui_widget_groupcc_title_row = $('#woocommerce_<?php echo $this->id; ?>_ui_widget_groupcc_title').closest('tr');
778
779 var ui_mode = $('#woocommerce_<?php echo $this->id; ?>_ui_mode');
780 var ui_widget_groupcountries = $('#woocommerce_<?php echo $this->id; ?>_ui_widget_groupcountries');
781 var ui_widget_groupcc = $('#woocommerce_<?php echo $this->id; ?>_ui_widget_groupcc');
782
783 parseVisibility();
784 function parseVisibility() {
785 $('.ui-identifier').closest('tr').show();
786
787 if(api_type.val() == 'live') {
788 $('.mc-test-link').hide();
789 $('.api-test').closest('tr').hide();
790 } else {
791 $('.mc-test-link').show();
792 $('.api-live').closest('tr').hide();
793 }
794
795 if(ui_mode.val() == 'inline') {
796 ui_widget_title_row.hide();
797 ui_widget_logosize_row.hide();
798 ui_widget_countryselector_row.hide();
799 ui_widget_groupcountries_row.hide();
800 ui_widget_groupcc_row.hide();
801 ui_widget_groupcc_title_row.hide();
802 } else {
803 ui_inline_uselogo_row.hide();
804
805 if(ui_widget_groupcountries.prop('checked')) {
806 ui_widget_countryselector_row.hide();
807 }
808 if(!ui_widget_groupcc.prop('checked')) {
809 ui_widget_groupcc_title_row.hide();
810 }
811 }
812 }
813
814 api_type.on('change', parseVisibility);
815 ui_mode.on('change', parseVisibility);
816 ui_widget_groupcountries.on('change', parseVisibility);
817 ui_widget_groupcc.on('change', parseVisibility);
818
819 });
820 </script>
821 <?php
822 }
823
824 public function payment_fields() {
825
826
827 if($this->settings['ui_mode'] == 'inline') {
828
829 ?>
830 <ul class="makecommerce-picker">
831 <?php foreach($this->_banklinks as $method): ?>
832 <li class="makecommerce-picker-method">
833 <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; ?>"/>
834 <label for="makecommerce_method_picker_<?php echo $method->country.'_'.$method->name; ?>">
835 <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>
836 <?php if(in_array($this->settings['ui_inline_uselogo'], array('logo', 'text_logo'))) : ?>
837 <div><img src="<?php echo $method->logo_url; ?>" title="<?php echo ucfirst($method->name); ?>" /></div>
838 <?php endif; ?>
839 </label>
840 </li>
841 <?php endforeach; ?>
842 <?php foreach($this->_cards as $method): ?>
843 <li class="makecommerce-picker-method">
844 <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; ?>"/>
845 <label for="makecommerce_method_picker_<?php echo 'card_'.$method->name; ?>">
846 <span class="makecommerce-method-title"><?php if(in_array($this->settings['ui_inline_uselogo'], array('text', 'text_logo'))) { echo ucfirst($method->name); } ?></span>
847 <?php if(in_array($this->settings['ui_inline_uselogo'], array('logo', 'text_logo'))) : ?>
848 <div><img src="<?php echo $method->logo_url; ?>" title="<?php echo ucfirst($method->name); ?>" /></div>
849 <?php endif; ?>
850 </label>
851 </li>
852 <?php endforeach; ?>
853 <?php foreach($this->_paylater as $method): ?>
854 <li class="makecommerce-picker-method">
855 <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; ?>"/>
856 <label for="makecommerce_method_picker_<?php echo $method->country.'_'.$method->name; ?>">
857 <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>
858 <?php if(in_array($this->settings['ui_inline_uselogo'], array('logo', 'text_logo'))) : ?>
859 <div><img src="<?php echo $method->logo_url; ?>" title="<?php echo ucfirst($method->name); ?>" /></div>
860 <?php endif; ?>
861 </label>
862 </li>
863 <?php endforeach; ?>
864 </ul>
865 <?php
866
867 } else {
868 ?>
869 <select id="<?php echo $this->id; ?>" name="PRESELECTED_METHOD_<?php echo $this->id; ?>">
870 <option value=""></option>
871 <?php foreach($this->_banklinks as $method): ?>
872 <option value="<?php echo $method->country.'_'.$method->name; ?>"><?php echo strtoupper($method->country).' - '.ucfirst($method->name); ?></option>
873 <?php endforeach; ?>
874 <?php foreach($this->_cards as $method): ?>
875 <option value="card_<?php echo $method->name; ?>"><?php echo ucfirst($method->name); ?></option>
876 <?php endforeach; ?>
877 <?php foreach($this->_paylater as $method): ?>
878 <option value="<?php echo $method->country.'_'.$method->name; ?>"><?php echo strtoupper($method->country).' - '.ucfirst($method->name); ?></option>
879 <?php endforeach; ?>
880 </select>
881 <ul class="makecommerce-picker">
882 <?php
883 global $woocommerce;
884 $cartTotal = $woocommerce->cart->total;
885 if($this->_banklinks || $this->_cards) {
886 $defaultCountry = $this->getDefaultCountry();
887 ?>
888 <?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') ) : ?>
889 <div class="makecommerce_country_picker_countries">
890 <?php foreach(array_keys($this->_banklinks_grouped) as $country): ?>
891 <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 } ?>
892 <?php endforeach; ?>
893 <?php if($this->settings['ui_widget_countryselector'] == 'dropdown') : ?>
894 <select name="makecommerce_country_picker_select">
895 <?php foreach(array_keys($this->_banklinks_grouped) as $country): ?>
896 <option value="<?php echo $country; ?>" <?php if($defaultCountry == $country) echo 'selected="selected" '; ?>><?php echo $this->getCountryName($country); ?></option>
897 <?php endforeach; ?>
898 <option value="card" style="display:none;"></option>
899 </select>
900 <?php endif; ?>
901 </div>
902 <?php endif; ?>
903 <?php
904 $banklinks_grouped = $this->_banklinks_grouped;
905 $banklinks_grouped['other'] = array();
906 ?>
907 <?php foreach($banklinks_grouped as $country => $methods): ?>
908 <li class="makecommerce-picker-country">
909 <?php if($this->settings['ui_widget_groupcountries'] == 'yes') : ?>
910 <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>
911 <?php endif; ?>
912 <div class="makecommerce_country_picker_methods" id="makecommerce_country_picker_methods_<?php echo $country; ?>">
913 <?php foreach($methods as $method): ?>
914 <div class="makecommerce-banklink-picker" banklink_id="<?php echo $method->country.'_'.$method->name; ?>">
915 <img src="<?php echo $method->logo_url; ?>" title="<?php echo ucfirst($method->name); ?>" />
916 </div>
917 <?php endforeach; ?>
918 <?php if($this->_cards && $this->settings['ui_widget_groupcc'] == 'no') : ?>
919 <div class="breaker"></div>
920 <?php foreach($this->_cards as $method): ?>
921 <div class="makecommerce-banklink-picker" banklink_id="card_<?php echo $method->name; ?>">
922 <img src="<?php echo $method->logo_url; ?>" title="<?php echo ucfirst($method->name); ?>" />
923 </div>
924 <?php endforeach; ?>
925 <?php elseif($country == 'other') :?>
926 <p class="no-methods"><?php _e('No payment methods for selected country' ,'wc_makecommerce_domain');?></p>
927 <?php endif; ?>
928 </div>
929 </li>
930 <?php endforeach; ?>
931 <?php if($this->_cards && $this->settings['ui_widget_groupcc'] == 'yes') : ?>
932 <li class="makecommerce-picker-country">
933 <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>
934 <div class="makecommerce_country_picker_methods" id="makecommerce_country_picker_methods_card">
935 <?php foreach($this->_cards as $method): ?>
936 <div class="makecommerce-banklink-picker" banklink_id="card_<?php echo $method->name; ?>">
937 <img src="<?php echo $method->logo_url; ?>" title="<?php echo ucfirst($method->name); ?>" />
938 </div>
939 <?php endforeach; ?>
940 </div>
941 </li>
942 <?php endif; ?>
943 <?php
944 $paylater_grouped = $this->_paylater_grouped;
945 $paylater_grouped['other'] = array();
946 ?>
947 <?php foreach($paylater_grouped as $country => $methods): ?>
948 <li class="makecommerce-picker-country">
949 <?php if($this->settings['ui_widget_groupcountries'] == 'yes') : ?>
950 <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>
951 <?php endif; ?>
952 <div class="makecommerce_country_picker_methods" id="makecommerce_country_picker_methods_<?php echo $country; ?>">
953 <?php foreach($methods as $method): ?>
954 <?php if (($method->min_amount < $cartTotal && $method->max_amount == 0) || ($method->min_amount < $cartTotal && $method->max_amount > $cartTotal)): ?>
955 <div class="makecommerce-banklink-picker" banklink_id="<?php echo $method->country.'_'.$method->name; ?>" <?php /* // Slice Promo 3
956 if ((get_option('mk_checkout_slice', 'yes') == 'yes') && ($method->name == 'slice')) echo 'style="position: relative; display: inline-block;"'; */ ?>>
957 <img src="<?php echo $method->logo_url; ?>" title="<?php echo ucfirst($method->name); ?> "/>
958 </div>
959 <?php endif; ?>
960 <?php endforeach; ?>
961 <?php if($this->_cards && $this->settings['ui_widget_groupcc'] == 'no') : ?>
962 <?php elseif($country == 'other') :?>
963 <p class="no-methods"><?php _e('No payment methods for selected country' ,'wc_makecommerce_domain');?></p>
964 <?php endif; ?>
965 </div>
966 </li>
967 <?php endforeach; ?>
968 <?php
969 }
970 ?>
971 </ul>
972 <div class="mc-clear-both"></div>
973 <script type="text/javascript">
974 var makecommerceId = '<?php echo $this->id; ?>';
975 var selectedCountry = '<?php echo $defaultCountry; ?>';
976
977 var logosize = '<?php echo $this->settings['ui_widget_logosize']; ?>';
978 jQuery('div.makecommerce_country_picker_methods').addClass('logosize-'+logosize);
979
980 <?php if(count($banklinks_grouped) == 1): ?>
981 jQuery('li.makecommerce-picker-country').show();
982 jQuery('div.makecommerce_country_picker_countries').hide();
983 jQuery('li.makecommerce-picker-country > input, li.makecommerce-picker-country > label').hide();
984 <?php else: ?>
985 makecommercePick();
986
987 jQuery('body').on('change', 'select[name=makecommerce_country_picker_select]', function() {
988 selectedCountry = jQuery(this).val();
989 jQuery('input[name=makecommerce_country_picker]').removeAttr('checked');
990 makecommercePick();
991 });
992 jQuery('body').on('change', 'input[name=makecommerce_country_picker]', function() {
993 if(jQuery(this).is(":checked")) {
994 selectedCountry = jQuery(this).val();
995 jQuery('select[name=makecommerce_country_picker_select]').val(selectedCountry);
996 makecommercePick();
997 }
998 });
999
1000 function makecommercePick() {
1001 jQuery('select#'+makecommerceId).val('');
1002 jQuery('div.makecommerce-banklink-picker').removeClass('selected');
1003 jQuery('label.makecommerce_country_picker_label').removeClass('selected');
1004
1005 jQuery('li.makecommerce-picker-country').hide();
1006 jQuery('div#makecommerce_country_picker_methods_' + selectedCountry).parent().show();
1007 jQuery('label[for=makecommerce_country_picker_' + selectedCountry + ']').addClass('selected');
1008 }
1009 <?php endif; ?>
1010
1011 jQuery('div.makecommerce-banklink-picker').on('click', function() {
1012 var banklink_id = jQuery(this).attr('banklink_id');
1013 jQuery('select#'+makecommerceId).val(banklink_id);
1014
1015 jQuery('div.makecommerce-banklink-picker').removeClass('selected');
1016 jQuery(this).addClass('selected');
1017 });
1018 </script>
1019 <?php
1020 }
1021
1022 if($this->settings['ui_open_by_default'] == 'yes') {
1023 ?>
1024 <script type="text/javascript">
1025 jQuery('input#payment_method_<?php echo $this->id; ?>').trigger('click');
1026 </script>
1027 <?php
1028 }
1029 }
1030
1031
1032
1033
1034
1035 protected function getCountryName($slug) {
1036 switch($slug) {
1037 case 'ee': return __('Estonia', 'wc_makecommerce_domain'); break;
1038 case 'lv': return __('Latvia', 'wc_makecommerce_domain'); break;
1039 case 'lt': return __('Lithuania', 'wc_makecommerce_domain'); break;
1040 case 'fi': return __('Finland', 'wc_makecommerce_domain'); break;
1041 }
1042 return $slug;
1043 }
1044
1045 private function getDefaultCountry() {
1046 if ($this->_getWooCommerce()->customer) {
1047 $customerCountry = strtolower($this->_getWooCommerce()->customer->get_shipping_country());
1048 if(array_key_exists($customerCountry, $this->_banklinks_grouped)) {
1049 return $customerCountry;
1050 } else {
1051 return 'other';
1052 }
1053 }
1054
1055 $localeToCountry = array(
1056 'et' => 'ee',
1057 'lv' => 'lv',
1058 'lt' => 'lt',
1059 'fi' => 'fi',
1060 );
1061 if(array_key_exists(get_locale(), $localeToCountry)) {
1062 return $localeToCountry[get_locale()];
1063 }
1064
1065 return key($this->_banklinks_grouped);
1066 }
1067
1068
1069 protected function getImageUrl($methodName) { // remove it
1070 $imageUrlPath = $this->MK->getEnvUrls()->staticsUrl.'img/channel/lnd/';
1071 return $imageUrlPath.$methodName.'.png';
1072 }
1073
1074 public function validate_fields() {
1075 $selected = isset($_POST['PRESELECTED_METHOD_' . $this->id]) ? sanitize_text_field($_POST['PRESELECTED_METHOD_' . $this->id]) : false;
1076
1077 if (!$selected) {
1078 wc_add_notice(__('Please select suitable payment option!', 'wc_makecommerce_domain'), 'error');
1079 } else {
1080 // is this used??
1081 $this->_getWooCommerce()->session->makecommerce_preselected_method = $selected;
1082 }
1083
1084 return true;
1085 }
1086
1087
1088 public function process_payment($orderId) {
1089
1090 $order = new WC_Order($orderId);
1091
1092 $selected = isset($_POST['PRESELECTED_METHOD_' . $this->id]) ? sanitize_text_field($_POST['PRESELECTED_METHOD_' . $this->id]) : false;
1093
1094 if (!empty($selected)) {
1095
1096 update_post_meta($order->get_id(), '_makecommerce_preselected_method', $selected);
1097
1098 $request_body = array(
1099 'transaction' => array(
1100 'amount' => (string)sprintf("%.2f", $order->get_total()),
1101 'currency' => method_exists($order, 'get_currency') ? $order->get_currency() : $order->currency,
1102 'reference' => $order->get_id(),
1103 'transaction_url' => array(
1104 'return_url' => array(
1105 'url' => $this->payment_return_url,
1106 'method' => 'POST',
1107 ),
1108 'cancel_url' => array(
1109 'url' => $this->payment_return_url_cancel,
1110 'method' => 'POST',
1111 ),
1112 'notification_url' => array(
1113 'url' => $this->payment_return_url_m2m,
1114 'method' => 'POST',
1115 ),
1116 ),
1117 ),
1118 'customer' => array(
1119 'ip' => $_SERVER['REMOTE_ADDR'],
1120 'country' => strtolower($order->get_billing_country()),
1121 'locale' => strtolower(substr(get_locale(), 0, 2)),
1122 ),
1123 );
1124 $transaction = $this->MK->createTransaction($request_body);
1125
1126 if(isset($transaction->id)) {
1127 update_post_meta($order->get_id(), '_makecommerce_transaction_id', $transaction->id);
1128 if (substr($selected, 0, 5) == 'card_') {
1129 $redirect_url = $order->get_checkout_payment_url( true );
1130 } else {
1131 $redirect_url = false;
1132 foreach ($transaction->payment_methods->banklinks as $banklink) {
1133 if ($banklink->country.'_'.$banklink->name === $selected) {
1134 $redirect_url = $banklink->url;
1135 }
1136 }
1137 if (!$redirect_url) {
1138 $redirect_url = $this->_getRedirectUrl($selected).$transaction->id;
1139 }
1140 }
1141 return array(
1142 'result' => 'success',
1143 'redirect' => $redirect_url
1144 );
1145 }
1146 }
1147
1148 wc_add_notice(__('An error occured when trying to process payment!', 'wc_makecommerce_domain'), 'error');
1149 return array(
1150 'result' => 'failure',
1151 );
1152 }
1153
1154
1155 protected function _getRedirectUrl($selected) {
1156 foreach($this->_banklinks as $method) {
1157 if($selected == $method->country.'_'.$method->name)
1158 return $method->url;
1159 }
1160 foreach($this->_paylater as $method) {
1161 if($selected == $method->country.'_'.$method->name)
1162 return $method->url;
1163 }
1164
1165 return false;
1166 }
1167
1168 public function receipt_page($orderId) {
1169 $order = new WC_Order($orderId);
1170 if (substr(get_post_meta($orderId, '_makecommerce_preselected_method', true), 0, 5) == 'card_' && $order->get_status() == 'pending') {
1171 echo "<br>".__('The order is still awaiting your payment', 'wc_makecommerce_domain')."<br>";
1172 echo $this->generateCardForm($order);
1173 }
1174 }
1175
1176
1177 public function generateCardForm($orderId) {
1178 $order = new WC_Order($orderId);
1179 $has_subscription = function_exists('wcs_order_contains_subscription') && wcs_order_contains_subscription($order);
1180 $scriptSrc = htmlspecialchars($this->MK->getEnvUrls()->checkoutjsUrl.'checkout.js');
1181 $transactionId = get_post_meta($order->get_id(), '_makecommerce_transaction_id', true);
1182 $idReference = $order->get_id();
1183 $jsParams = array(
1184 'key' => $this->MK->getPublishableKey(),
1185 'transaction' => $transactionId,
1186 'selector' => '#submit_banklinkmakecommerce_payment_form',
1187 'amount' => sprintf("%.2F", $order->get_total()),
1188 'locale' => !empty($_GET['lang']) ? $_GET['lang'] : (defined('ICL_LANGUAGE_CODE') ? ICL_LANGUAGE_CODE : strtolower(substr(get_locale(), 0, 2))),
1189 'open-on-load' => 'true',
1190 'client-name' => ($this->settings['cc_pass_cust_data'] == 'yes' ? (string) ((method_exists($order, 'get_billing_first_name') ? $order->get_billing_first_name() : $order->billing_first_name) . ' ' . (method_exists($order, 'get_billing_last_name') ? $order->get_billing_last_name() : $order->billing_last_name)) : ''),
1191 'email' => ($this->settings['cc_pass_cust_data'] == 'yes' ? (string) (method_exists($order, 'get_billing_email') ? $order->get_billing_email() : $order->billing_email) : ''),
1192 'name' => get_option('mc_shop_name', ''),
1193 'description' => __('Order', 'woocommerce') . '&nbsp;' . (string) $idReference,
1194 'completed' => 'makecommerce_cc_complete',
1195 'currency' => method_exists($order, 'get_currency') ? $order->get_currency() : $order->currency, // $transaction->currency, // 'EUR',
1196 'backdrop-close' => 'false',
1197 );
1198 if ($has_subscription) {
1199 $jsParams['recurring-required'] = 'true';
1200 $jsParams['recurring-title'] = __('Pay for subscription', 'wc_makecommerce_domain');
1201 $jsParams['recurring-description'] = __('This order contains subscriptions', 'wc_makecommerce_domain');
1202 $jsParams['recurring-confirmation'] = __('I agree that my card will be recurringly billed by this store', 'wc_makecommerce_domain');
1203 }
1204 ?>
1205 <script type="text/javascript">
1206 function makecommerce_cc_complete(data) {
1207 if(data.paymentToken) {
1208 jQuery('div.mc-processing-message').show();
1209
1210 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>');
1211 for(var key in data) {
1212 submitform.append(jQuery('<input type="hidden" name="'+key+'" />').val(data[key]));
1213 }
1214 jQuery('body').append(submitform);
1215 submitform.submit();
1216 }
1217 }
1218 </script>
1219 <form id="cc_form" >
1220 <input type="submit" class="button-alt" id="submit_banklinkmakecommerce_payment_form" value="<?php echo __('Pay with credit card', 'wc_makecommerce_domain'); ?>" />
1221 <script type="text/javascript" src="<?php echo $scriptSrc; ?>" <?php echo $this->_toHtmlAttributes($jsParams); ?>></script>
1222 </form>
1223 <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>
1224 <?php
1225 }
1226
1227
1228 protected function _toHtmlAttributes($input) {
1229 $result = array();
1230 foreach ($input as $key => $value) {
1231 $result[] = 'data-' . htmlspecialchars($key) . '=' . '"' . htmlspecialchars($value) . '"';
1232 }
1233 return implode(' ', $result);
1234 }
1235
1236 public function makecommerce_return_trigger($vars) {
1237 $vars[] = 'makecommerce_return';
1238 $vars[] = 'ajax_content';
1239 $vars[] = 'makecommerce_card_pay';
1240 $vars[] = 'lang1';
1241 return $vars;
1242 }
1243
1244 public function makecommerce_return_trigger_check() {
1245 if (intval(get_query_var('makecommerce_return')) > 0) {
1246 $return_url = mk_check_payment();
1247 if (intval(get_query_var('ajax_content'))) {
1248 echo json_encode(array('redirect' => $return_url));
1249 } else {
1250 wp_redirect($return_url);
1251 }
1252 exit;
1253 }
1254 }
1255
1256
1257 } // class ends here
1258
1259 New woocommerce_makecommerce(true);
1260
1261 // Woocommerce Multilingual overrides
1262 if (is_plugin_active('woocommerce-multilingual/wpml-woocommerce.php') || function_exists('pll_languages_list')) {
1263 add_filter( 'get_post_metadata', 'mc_override_payment_method_string', 5, 4 );
1264 }
1265
1266 } // init ends here
1267
1268
1269
1270
1271 function woocommerce_payment_makecommerce_add($methods) {
1272 $methods[] = 'woocommerce_makecommerce';
1273 return $methods;
1274 }
1275
1276 add_action('plugins_loaded', 'woocommerce_payment_makecommerce_init');
1277 add_action('woocommerce_payment_gateways', 'woocommerce_payment_makecommerce_add');
1278
1279 // Banklinks update wrapper for action
1280 function update_mc_banklinks() {
1281 $obj = new woocommerce_makecommerce(true);
1282 $obj->mc_banklinks_reload(true);
1283 }
1284
1285 function mc_override_payment_method_string($check, $object_id, $meta_key, $single)
1286 {
1287 if ($meta_key == '_payment_method_title') {
1288 $payment_method = get_post_meta($object_id, '_payment_method', true);
1289
1290 if ($payment_method == 'makecommerce') {
1291 $payment_gateways = WC()->payment_gateways->payment_gateways();
1292
1293 if (isset($payment_gateways[$payment_method])) {
1294 $title = $payment_gateways[$payment_method]->title;
1295
1296 return $title;
1297 }
1298 }
1299 }
1300 return $check;
1301 }
1302