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

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

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