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

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