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

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