PluginProbe
MakeCommerce for WooCommerce / 3.0.2
MakeCommerce for WooCommerce v3.0.2
4.1.0 4.0.8 trunk 1.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 2.0.0 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.2.0 2.2.1 2.2.2 All 96 releases
makecommerce / payment / gateway / woocommerce / woocommerce.php

woocommerce.php in MakeCommerce for WooCommerce 3.0.2, at payment/gateway/woocommerce/woocommerce.php

557 lines 20.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace MakeCommerce\Payment\Gateway;
4
5 class WooCommerce extends \MakeCommerce\Payment\Gateway {
6
7 use WooCommerce\Banklink, WooCommerce\Creditcard;
8
9 public $id = 'makecommerce';
10 public $version = '3.0.0';
11
12 public $payment_return_url;
13 public $payment_return_url_m2m;
14 public $payment_return_url_cancel;
15
16 public $title = 'MakeCommerce';
17 public $method_title = 'MakeCommerce';
18 public $description = true;
19
20
21 protected $init;
22
23 /**
24 * Payment methods
25 */
26 private WooCommerce\Methods $methods;
27
28 /**
29 * Construct the parent and payment gateway
30 *
31 * @since 3.0.0
32 */
33 public function __construct( $init = false ) {
34
35 $this->init = $init;
36
37 parent::__construct();
38
39 //set return urls
40 $this->set_return_urls();
41
42 //initialize all payment methods
43 $this->methods = new WooCommerce\Methods( $this->id, $this->init, $this->settings );
44
45 $this->set_title_by_language();
46 }
47
48 /**
49 * Set return urls
50 *
51 * @since 3.0.0
52 */
53 private function set_return_urls() {
54
55 //set language
56 $langGet = "&lang1=" . \MakeCommerce\i18n::get_two_char_locale();
57
58 //set return url's
59 $this->payment_return_url = site_url( '/?makecommerce_return=1'.$langGet );
60 $this->payment_return_url_m2m = site_url( '/?makecommerce_return=1&ajax_content=1'.$langGet );
61 $this->payment_return_url_cancel = site_url( '/?makecommerce_return=1'.$langGet );
62 }
63
64
65
66 /**
67 * Set title according to language
68 *
69 * @since 3.0.0
70 */
71 private function set_title_by_language( ) {
72
73 //default
74 if ( !empty( $this->settings['ui_widget_title'] ) ) {
75 $this->title = $this->settings['ui_widget_title'];
76 }
77
78 $locale = "";
79
80 if ( isset( $_GET["lang1"] ) ) {
81
82 $locale = $_GET["lang1"];
83
84 if ( !empty( $this->settings['ui_widget_title_'.$locale] ) ) {
85 $this->title = $this->settings['ui_widget_title_'.$locale];
86 }
87 } else {
88
89 $locale = \MakeCommerce\i18n::get_two_char_locale();
90
91 //coming from admin, set language according to order wpml_language. This meta is set for both polylang wpml
92 if ( isset( $_POST["meta"] ) ) {
93 foreach ( $_POST["meta"] as $key=>$value ) {
94 if ( is_array( $value ) ) {
95 if ( $value["key"] == "wpml_language" ) {
96 $locale = strtolower( substr( $value["value"], 0, 2 ) );
97
98 }
99 }
100 }
101 }
102
103 if ( !empty( $this->settings['ui_widget_title_' . $locale] ) ) {
104 $this->title = $this->settings['ui_widget_title_' . $locale];
105 }
106 }
107 }
108
109 /**
110 * Loads all form fields specific to this payment gateway
111 *
112 * @since 3.0.0
113 */
114 public function initialize_gateway_type_form_fields() {
115
116 $this->form_fields['active'] = array(
117 'title' => __( 'Enable/Disable', 'wc_makecommerce_domain' ),
118 'type' => 'checkbox',
119 'label' => __( 'Enable MakeCommerce payments', 'wc_makecommerce_domain' ),
120 'default' => 'no'
121 );
122
123 $a = admin_url( 'admin.php?page=wc-settings&tab=api&section=mk_api' );
124 if ( \MakeCommerce::new_woo_version() ) {
125 $a = admin_url( 'admin.php?page=wc-settings&tab=advanced&section=mk_api' );
126 }
127
128 $this->form_fields['api_title'] = array(
129 'title' => __( 'MakeCommerce API', 'wc_makecommerce_domain' ),
130 'description' => sprintf( __( 'Go to <a href="%s">API settings</a> to fill in the credentials', 'wc_makecommerce_domain' ), $a ),
131 'type' => 'title',
132 );
133
134 $this->form_fields['ui_title'] = array(
135 'title' => '<br>'.__( 'User Interface', 'wc_makecommerce_domain' ),
136 'type' => 'title',
137 'class' => 'ui-identifier',
138 );
139
140 $this->form_fields['ui_open_by_default'] = array(
141 'title' => __( 'Set as default selection', 'wc_makecommerce_domain' ),
142 'label' => __( 'MakeCommerce payments widget will be selected by default', 'wc_makecommerce_domain' ),
143 'type' => 'checkbox',
144 'default' => 'yes',
145 'class' => 'ui-identifier',
146 );
147
148 $this->form_fields['ui_mode'] = array(
149 'title' => __( 'Display MC payment channels as', 'wc_makecommerce_domain' ),
150 'type' => 'select',
151 'default' => 'widget',
152 'options' => array(
153 'inline' => __( 'List', 'wc_makecommerce_domain' ),
154 'widget' => __( 'Grouped to widget', 'wc_makecommerce_domain' ),
155 ),
156 'class' => 'ui-identifier',
157 );
158
159 $languages = \MakeCommerce\i18n::get_active_languages();
160
161 if ( empty( $languages ) ) {
162
163 $this->form_fields['ui_widget_title'] = array(
164 'title' => __( 'Payments widget title', 'wc_makecommerce_domain' ),
165 'type' => 'text',
166 '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' ),
167 'default' => __( 'Pay with bank-links or credit card', 'wc_makecommerce_domain' ),
168 'class' => 'ui-identifier',
169 );
170 } else {
171
172 foreach ( $languages as $language_code => $language ) {
173 $this->form_fields['ui_widget_title_'.$language_code] = array(
174 'title' => __( 'Payments widget title', 'wc_makecommerce_domain' ).sprintf( ' (%s)', $language_code ),
175 'type' => 'text',
176 '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'),
177 'default' => \MakeCommerce\i18n::get_string_from_mo( 'Pay with bank-links or credit card', 'wc_makecommerce_domain', $language_code ),
178 'class' => 'ui-identifier',
179 );
180 }
181 }
182
183 $this->form_fields['ui_inline_uselogo'] = array(
184 'title' => __( 'MC payment channels display style', 'wc_makecommerce_domain' ),
185 'type' => 'select',
186 'default' => 'logo',
187 'options' => array(
188 'logo' => __( 'Logo', 'wc_makecommerce_domain' ),
189 'text_logo' => __( 'Text & logo', 'wc_makecommerce_domain' ),
190 'text' => __( 'Text', 'wc_makecommerce_domain' ),
191 ),
192 'class' => 'ui-identifier',
193 );
194
195 $this->form_fields['ui_widget_logosize'] = array(
196 'title' => __( 'Size of payment channel logos', 'wc_makecommerce_domain' ),
197 'type' => 'select',
198 'default' => 'medium',
199 'options' => array(
200 'small' => __( 'Small', 'wc_makecommerce_domain' ),
201 'medium' => __( 'Medium', 'wc_makecommerce_domain' ),
202 'large' => __( 'Large', 'wc_makecommerce_domain' )
203 ),
204 'class' => 'ui-identifier',
205 );
206
207 $this->form_fields['ui_widget_groupcountries'] = array(
208 'title' => __( 'Group bank-links by countries', 'wc_makecommerce_domain' ),
209 'type' => 'checkbox',
210 'default' => 'yes',
211 'class' => 'ui-identifier',
212 );
213
214 $this->form_fields['ui_widget_countries_hidden'] = array(
215 'title' => __( 'Hide country selector', 'wc_makecommerce_domain' ),
216 'label' => __( 'Do not display country selector (flags) at payment methods', 'wc_makecommerce_domain' ),
217 'type' => 'checkbox',
218 'default' => 'no',
219 );
220
221 $this->form_fields['ui_widget_countryselector'] = array(
222 'title' => __( 'Country selector style', 'wc_makecommerce_domain' ),
223 'type' => 'select',
224 'default' => 'flag',
225 'options' => array(
226 'flag' => __( 'Flag', 'wc_makecommerce_domain' ),
227 'dropdown' => __( 'Dropdown', 'wc_makecommerce_domain' ),
228 ),
229 'class' => 'ui-identifier',
230 );
231
232 $this->form_fields['ui_chorder'] = array(
233 'title' => __( 'Define custom order of payment channels', 'wc_makecommerce_domain' ),
234 'type' => 'text',
235 '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' ),
236 'class' => 'ui-identifier',
237 );
238
239 $this->form_fields['ui_javascript'] = array(
240 'type' => 'ui_javascript',
241 );
242
243 $this->form_fields['cc_title'] = array(
244 'title' => '<br>'.__( 'Credit Card Settings', 'wc_makecommerce_domain' ),
245 'type' => 'title',
246 );
247
248 $this->form_fields['cc_pass_cust_data'] = array(
249 'title' => __( 'Prefill Credit Card form with customer data', 'wc_makecommerce_domain' ),
250 'type' => 'checkbox',
251 'default' => 'yes',
252 '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' ),
253 );
254
255 $this->form_fields['adv_title'] = array(
256 'title' => '<hr><br>'.__( 'Advanced Settings', 'wc_makecommerce_domain' ),
257 'type' => 'title',
258 );
259
260 $this->form_fields['reload_links'] = array(
261 'type' => 'mc_banklinks_reload',
262 'title' => __( 'Update payment methods', 'wc_makecommerce_domain' ),
263 'description' => __( 'Update', 'wc_makecommerce_domain' ),
264 'desc_tip' => __( 'This will update shop configuration from MakeCommerce servers.', 'wc_makecommerce_domain' ),
265 );
266 }
267
268 /**
269 * Set gateway specific hooks/filters
270 *
271 * @since 3.0.0
272 */
273 public function set_gateway_hooks() {
274
275 add_filter( 'query_vars', array( $this, 'return_trigger' ) );
276 add_action( 'template_redirect', array( $this, 'return_trigger_check' ) );
277
278 add_action( 'woocommerce_scheduled_subscription_payment_' . $this->id, array( $this, 'process_subscription_payment_start' ), 10, 2 );
279 add_action( 'scheduled_subscription_payment_' . $this->id, array( $this, 'process_subscription_payment_start' ), 10, 2 );
280
281 if ( $this->init == false ) {
282 add_action( 'woocommerce_receipt_' . $this->id, array( $this, 'receipt_page' ) );
283 wp_enqueue_script( 'jquery');
284 wp_enqueue_style( 'makecommerce', plugins_url( '/css/makecommerce.css', __FILE__ ), array(), $this->version );
285 }
286
287 if ( is_admin() ) {
288 add_action( 'wp_ajax_mc_banklinks_reload', array( $this, 'mc_banklinks_reload' ) );
289 }
290 }
291
292 /**
293 * Javascript for user interface
294 *
295 * @since 3.0.0
296 */
297 public function generate_ui_javascript_html( $key, $data ) {
298
299 ?>
300 <script type="text/javascript">
301 jQuery(document).ready(function($) {
302
303 var api_type = $('#woocommerce_<?php echo $this->id; ?>_api_type');
304
305 var ui_inline_uselogo_row = $('#woocommerce_<?php echo $this->id; ?>_ui_inline_uselogo').closest('tr');
306 var ui_widget_title_row = $('#woocommerce_<?php echo $this->id; ?>_ui_widget_title').closest('tr');
307 var ui_widget_logosize_row = $('#woocommerce_<?php echo $this->id; ?>_ui_widget_logosize').closest('tr');
308 var ui_widget_countryselector_row = $('#woocommerce_<?php echo $this->id; ?>_ui_widget_countryselector').closest('tr');
309 var ui_widget_groupcountries_row = $('#woocommerce_<?php echo $this->id; ?>_ui_widget_groupcountries').closest('tr');
310 var ui_widget_countries_hidden_row = $('#woocommerce_<?php echo $this->id; ?>_ui_widget_countries_hidden').closest('tr');
311 var ui_widget_countries_hidden = $('#woocommerce_<?php echo $this->id; ?>_ui_widget_countries_hidden');
312
313 var ui_mode = $('#woocommerce_<?php echo $this->id; ?>_ui_mode');
314 var ui_widget_groupcountries = $('#woocommerce_<?php echo $this->id; ?>_ui_widget_groupcountries');
315 var ui_inline_uselogo = $('#woocommerce_<?php echo $this->id; ?>_ui_inline_uselogo');
316
317 parseVisibility();
318
319 function parseVisibility() {
320
321 $('.ui-identifier').closest('tr').show();
322
323 if (api_type.val() == 'live') {
324 $('.mc-test-link').hide();
325 $('.api-test').closest('tr').hide();
326 } else {
327 $('.mc-test-link').show();
328 $('.api-live').closest('tr').hide();
329 }
330
331 //hide/show logo size if text is selected
332 if ( ui_inline_uselogo.val() == "text" ) {
333 ui_widget_logosize_row.hide();
334 } else {
335 ui_widget_logosize_row.show();
336 }
337
338 if ( ui_mode.val() == "inline" ) {
339 ui_inline_uselogo_row.show();
340 ui_widget_logosize_row.hide();
341 } else {
342 ui_inline_uselogo_row.hide();
343 ui_widget_logosize_row.show();
344 }
345
346 if ( ui_widget_groupcountries.prop( 'checked' ) ) {
347 ui_widget_countries_hidden_row.hide();
348 ui_widget_countryselector_row.hide();
349 } else {
350 ui_widget_countries_hidden_row.show();
351 ui_widget_countryselector_row.show();
352 }
353
354 //hide/show country selector options
355 if ( ui_widget_countries_hidden.prop( 'checked' ) ) {
356 ui_widget_countryselector_row.hide();
357 } else {
358 if ( !ui_widget_groupcountries.prop( 'checked' ) ) {
359 ui_widget_countryselector_row.show();
360 }
361 }
362 }
363
364 ui_inline_uselogo.on('change', parseVisibility);
365 api_type.on('change', parseVisibility);
366 ui_mode.on('change', parseVisibility)
367 ui_widget_groupcountries.on('change', parseVisibility);
368 ui_widget_countries_hidden.on('change', parseVisibility);
369 });
370 </script>
371 <?php
372 }
373
374 /**
375 * Overrides \WC_Payment_Gateway payment fields
376 * Shows payment methods in checkout
377 *
378 * @since 3.0.0
379 */
380 public function payment_fields() {
381
382 $this->methods->show_methods();
383 }
384
385 /**
386 * Checks if a payment option has been selected
387 *
388 * @since 3.0.0
389 */
390 public function validate_fields() {
391
392 global $woocommerce;
393
394 $selected = isset( $_POST['PRESELECTED_METHOD_' . $this->id] ) ? sanitize_text_field( $_POST['PRESELECTED_METHOD_' . $this->id] ) : false;
395
396 if ( !$selected ) {
397 wc_add_notice( __( 'Please select suitable payment option!', 'wc_makecommerce_domain' ), 'error' );
398 } else {
399 // is this used??
400 $woocommerce->session->makecommerce_preselected_method = $selected;
401 }
402
403 return true;
404 }
405
406 /**
407 * Processes payments, called by \WC_Payment_Gateway
408 *
409 * @since 3.0.0
410 */
411 public function process_payment( $orderId ) {
412
413 $order = new \WC_Order( $orderId );
414
415 $selected = isset( $_POST['PRESELECTED_METHOD_' . $this->id] ) ? sanitize_text_field( $_POST['PRESELECTED_METHOD_' . $this->id] ) : false;
416
417 if ( !empty( $selected ) ) {
418
419 update_post_meta( $order->get_id(), '_makecommerce_preselected_method', $selected );
420
421 $request_body = array(
422 'transaction' => array(
423 'amount' => ( string )sprintf( "%.2f", $order->get_total() ),
424 'currency' => method_exists( $order, 'get_currency' ) ? $order->get_currency() : $order->currency,
425 'reference' => $order->get_id(),
426 'transaction_url' => array(
427 'return_url' => array(
428 'url' => $this->payment_return_url,
429 'method' => 'POST',
430 ),
431 'cancel_url' => array(
432 'url' => $this->payment_return_url_cancel,
433 'method' => 'POST',
434 ),
435 'notification_url' => array(
436 'url' => $this->payment_return_url_m2m,
437 'method' => 'POST',
438 ),
439 ),
440 ),
441 'customer' => array(
442 'ip' => $_SERVER['REMOTE_ADDR'],
443 'country' => strtolower( $order->get_billing_country() ),
444 'locale' => strtolower( \MakeCommerce\i18n::get_two_char_locale() ),
445 ),
446 );
447
448 $transaction = $this->MK->createTransaction( $request_body );
449
450 if ( isset( $transaction->id ) ) {
451
452 update_post_meta( $order->get_id(), '_makecommerce_transaction_id', $transaction->id );
453
454 if ( substr( $selected, 0, 5 ) == 'card_' ) {
455
456 $redirect_url = $order->get_checkout_payment_url( true );
457 } else {
458
459 $redirect_url = false;
460 foreach ( $transaction->payment_methods->banklinks as $banklink ) {
461 if ( $banklink->country.'_'.$banklink->name === $selected ) {
462 $redirect_url = $banklink->url;
463 }
464 }
465
466 if ( !$redirect_url ) {
467 $redirect_url = $this->_getRedirectUrl( $selected ).$transaction->id;
468 }
469 }
470
471 return array(
472 'result' => 'success',
473 'redirect' => $redirect_url
474 );
475 }
476 }
477
478 wc_add_notice( __( 'An error occured when trying to process payment!', 'wc_makecommerce_domain' ), 'error' );
479
480 return array(
481 'result' => 'failure',
482 );
483 }
484
485 /**
486 * Return redirect uri for payment method
487 *
488 * @since 3.0.0
489 */
490 protected function _getRedirectUrl( $selected ) {
491
492 foreach ( $this->methods->banklinks as $method ) {
493
494 if ( $selected == $method->country.'_'.$method->name ) {
495 return $method->url;
496 }
497 }
498
499 foreach ( $this->methods->paylater as $method ) {
500
501 if ( $selected == $method->country.'_'.$method->name ) {
502 return $method->url;
503 }
504 }
505
506 return false;
507 }
508
509 /**
510 * Variables that trigger return check
511 *
512 * @since 3.0.0
513 */
514 public function return_trigger( $vars ) {
515
516 $vars[] = 'makecommerce_return';
517 $vars[] = 'ajax_content';
518 $vars[] = 'makecommerce_card_pay';
519 $vars[] = 'lang1';
520
521 return $vars;
522 }
523
524 /**
525 * Process return from payment, also handles cart updates among other things
526 *
527 * @since 3.0.0
528 */
529 public function return_trigger_check() {
530
531 if ( intval( get_query_var( 'makecommerce_return' ) ) > 0 ) {
532
533 $return_url = \MakeCommerce\Payment::check_payment();
534
535 if ( intval( get_query_var( 'ajax_content' ) ) ) {
536 echo json_encode( array( 'redirect' => $return_url ) );
537 } else {
538 wp_redirect( $return_url );
539 }
540
541 exit;
542 }
543 }
544
545 /**
546 * Checks whether this payment gateway is enabled
547 * returns true or false
548 *
549 * This function always returns true as it is not possible to manually switch off MakeCommerce payment method in API options
550 *
551 * @since 3.0.0
552 */
553 public function enabled() {
554
555 return true;
556 }
557 }