1
1
<?php
2
2
3
- use Payu \PaymentGateway \Gateways \ WC_Payu_Gateways ;
3
+ namespace Payu \PaymentGateway \Gateways ;
4
4
5
5
class WC_Gateway_PayuInstallments extends WC_Payu_Gateways {
6
6
protected $ paytype = 'ai ' ;
@@ -9,58 +9,54 @@ function __construct() {
9
9
parent ::__construct ( 'payuinstallments ' );
10
10
}
11
11
12
- public function is_available () {
13
- if ( ! $ this ->try_retrieve_banks () ) {
12
+ public function is_available (): bool {
13
+ if ( ! $ this ->try_retrieve_banks () || ! parent :: is_available () ) {
14
14
return false ;
15
15
}
16
+ $ options = get_option ( 'woocommerce_payuinstallments_settings ' , [] );
16
17
17
- wp_enqueue_style ( 'payu-installments-widget ' , plugins_url ( '/assets/css/payu-installments-widget.css ' , PAYU_PLUGIN_FILE ), [], PAYU_PLUGIN_VERSION );
18
- add_filter ( 'woocommerce_gateway_description ' , [
19
- $ this ,
20
- 'installments_filter_gateway_description '
21
- ], 10 , 2 );
22
-
23
- return parent ::is_available ();
24
- }
25
-
26
- public function installments_filter_gateway_description ( $ description , $ id ) {
27
- if ( $ this ->should_display_installments_widget ( $ id ) ) {
18
+ if ( $ options ['credit_widget_on_checkout_page ' ] === 'yes ' &&
19
+ get_woocommerce_currency () === 'PLN ' &&
20
+ $ this ->is_checkout_page ()
21
+ ) {
28
22
wp_enqueue_script ( 'payu-installments-widget ' , 'https://static.payu.com/res/v2/widget-mini-installments.js ' , [], PAYU_PLUGIN_VERSION );
29
- wp_enqueue_script ( 'payu-installments-widget-checkout ' , plugins_url ( '/assets/js/payu-installments-widget-checkout.js ' , PAYU_PLUGIN_FILE ), [
30
- 'jquery ' ,
31
- 'payu-installments-widget '
32
- ], PAYU_PLUGIN_VERSION );
33
- $ posId = $ this ->pos_id ;
34
- $ widgetKey = $ this ->pos_widget_key ;
35
- $ priceTotal = WC ()->cart ->get_total ( '' );
36
- $ transformedDescription =
37
- $ description .
38
- '<script type="text/javascript"> ' .
39
- 'var PayUInstallmentsWidgetCartData = { ' .
40
- ' priceTotal: ' . $ priceTotal . ', ' .
41
- ' posId: \'' . $ posId . '\', ' .
42
- ' widgetKey: \'' . $ widgetKey . '\'' .
43
- '} ' .
44
- '</script> ' ;
45
-
46
- return $ transformedDescription ;
47
23
}
48
24
49
- return $ description ;
25
+ return true ;
50
26
}
51
27
52
- function should_display_installments_widget ( $ id ) {
53
- return $ id === 'payuinstallments ' &&
54
- get_option ( 'woocommerce_payuinstallments_settings ' )['credit_widget_on_checkout_page ' ] === 'yes ' &&
55
- get_woocommerce_currency () === 'PLN ' ;
28
+ // Additional data for Blocks
29
+ public function get_additional_data (): array {
30
+ return [
31
+ 'posId ' => $ this ->pos_id ,
32
+ 'widgetKey ' => $ this ->pos_widget_key ,
33
+ 'total ' => WC ()->cart ->get_total ( '' )
34
+ ];
56
35
}
57
36
58
- public function payment_fields () {
59
- parent ::payment_fields ();
60
- $ this ->agreements_field ();
37
+ public function get_description (): string {
38
+ wp_enqueue_style ( 'payu-installments-widget ' , plugins_url ( '/assets/css/payu-installments-widget.css ' , PAYU_PLUGIN_FILE ), [], PAYU_PLUGIN_VERSION );
39
+
40
+ wp_enqueue_script ( 'payu-installments-widget-checkout ' , plugins_url ( '/assets/js/payu-installments-widget-checkout.js ' , PAYU_PLUGIN_FILE ), [
41
+ 'jquery ' ,
42
+ 'payu-installments-widget '
43
+ ], PAYU_PLUGIN_VERSION );
44
+ $ posId = $ this ->pos_id ;
45
+ $ widgetKey = $ this ->pos_widget_key ;
46
+ $ priceTotal = WC ()->cart ->get_total ( '' );
47
+
48
+ return
49
+ $ this ->description .
50
+ '<script type="text/javascript"> ' .
51
+ 'var PayUInstallmentsWidgetCartData = { ' .
52
+ ' priceTotal: ' . $ priceTotal . ', ' .
53
+ ' posId: \'' . $ posId . '\', ' .
54
+ ' widgetKey: \'' . $ widgetKey . '\'' .
55
+ '} ' .
56
+ '</script> ' ;
61
57
}
62
58
63
- protected function get_additional_gateway_fields () {
59
+ protected function get_additional_gateway_fields (): array {
64
60
return [
65
61
'credit_widget_on_listings ' => [
66
62
'title ' => __ ( 'Installments widget ' , 'woo-payu-payment-gateway ' ),
@@ -88,4 +84,8 @@ protected function get_additional_gateway_fields() {
88
84
],
89
85
];
90
86
}
87
+
88
+ private function is_checkout_page (): bool {
89
+ return is_checkout () || has_block ( 'woocommerce/checkout ' );
90
+ }
91
91
}
0 commit comments