Skip to content

Commit a7c24f4

Browse files
committedJun 21, 2024
Add credit/bnpl methods to blocks
1 parent de4bf02 commit a7c24f4

27 files changed

+494
-180
lines changed
 

‎Payu/Blocks/PayuBlocks.php

+6-5
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,12 @@ public function get_payment_method_script_handles(): array {
5454

5555
public function get_payment_method_data(): array {
5656
return [
57-
'available' => $this->is_active(),
58-
'showTermsInfo' => $this->payment_method->is_payu_show_terms_info(),
59-
'title' => $this->payment_method->get_payu_method_title(),
60-
'description' => $this->payment_method->get_payu_method_description(),
61-
'icon' => $this->payment_method->get_payu_method_icon()
57+
'available' => $this->is_active(),
58+
'showTermsInfo' => $this->payment_method->is_payu_show_terms_info(),
59+
'title' => $this->payment_method->get_payu_method_title(),
60+
'description' => $this->payment_method->get_payu_method_description(),
61+
'icon' => $this->payment_method->get_payu_method_icon(),
62+
'additionalData' => $this->payment_method->get_additional_data()
6263
];
6364
}
6465
}

‎Payu/Blocks/PayuCreditCardBlock.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Payu\PaymentGateway\Blocks;
44

5-
class PayuCreditCardBlock extends PayuBlocks
6-
{
5+
class PayuCreditCardBlock extends PayuBlocks {
76
protected $name = 'payucreditcard';
87
}

‎Payu/Blocks/PayuInstallmentsBlock.php

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace Payu\PaymentGateway\Blocks;
4+
5+
class PayuInstallmentsBlock extends PayuBlocks {
6+
protected $name = 'payuinstallments';
7+
}

‎Payu/Blocks/PayuKlarnaBlock.php

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace Payu\PaymentGateway\Blocks;
4+
5+
class PayuKlarnaBlock extends PayuBlocks {
6+
protected $name = 'payuklarna';
7+
}

‎Payu/Blocks/PayuPaypoBlock.php

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace Payu\PaymentGateway\Blocks;
4+
5+
class PayuPaypoBlock extends PayuBlocks {
6+
protected $name = 'payupaypo';
7+
}

‎Payu/Blocks/PayuStandardBlock.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Payu\PaymentGateway\Blocks;
44

5-
class PayuStandardBlock extends PayuBlocks
6-
{
5+
class PayuStandardBlock extends PayuBlocks {
76
protected $name = 'payustandard';
87
}

‎Payu/Blocks/PayuTwistoPlBlock.php

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace Payu\PaymentGateway\Blocks;
4+
5+
class PayuTwistoPlBlock extends PayuBlocks {
6+
protected $name = 'payutwistopl';
7+
}
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
use Payu\PaymentGateway\Gateways\WC_Payu_Gateways;
3+
namespace Payu\PaymentGateway\Gateways;
44

55
class WC_Gateway_PayuCreditCard extends WC_Payu_Gateways {
66
protected $paytype = 'c';
@@ -13,16 +13,11 @@ function __construct() {
1313
}
1414
}
1515

16-
public function is_available() {
16+
public function is_available(): bool {
1717
if ( ! $this->try_retrieve_banks() ) {
1818
return false;
1919
}
2020

2121
return parent::is_available();
2222
}
23-
24-
public function payment_fields() {
25-
parent::payment_fields();
26-
$this->agreements_field();
27-
}
2823
}
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
use Payu\PaymentGateway\Gateways\WC_Payu_Gateways;
3+
namespace Payu\PaymentGateway\Gateways;
44

55
class WC_Gateway_PayuInstallments extends WC_Payu_Gateways {
66
protected $paytype = 'ai';
@@ -9,58 +9,54 @@ function __construct() {
99
parent::__construct( 'payuinstallments' );
1010
}
1111

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() ) {
1414
return false;
1515
}
16+
$options = get_option( 'woocommerce_payuinstallments_settings', [] );
1617

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+
) {
2822
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;
4723
}
4824

49-
return $description;
25+
return true;
5026
}
5127

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+
];
5635
}
5736

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>';
6157
}
6258

63-
protected function get_additional_gateway_fields() {
59+
protected function get_additional_gateway_fields(): array {
6460
return [
6561
'credit_widget_on_listings' => [
6662
'title' => __( 'Installments widget', 'woo-payu-payment-gateway' ),
@@ -88,4 +84,8 @@ protected function get_additional_gateway_fields() {
8884
],
8985
];
9086
}
87+
88+
private function is_checkout_page(): bool {
89+
return is_checkout() || has_block( 'woocommerce/checkout' );
90+
}
9191
}
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
use Payu\PaymentGateway\Gateways\WC_Payu_Gateways;
3+
namespace Payu\PaymentGateway\Gateways;
44

55
class WC_Gateway_PayuKlarna extends WC_Payu_Gateways {
66
protected $paytype = 'dpkl';
@@ -13,16 +13,11 @@ function __construct() {
1313
}
1414
}
1515

16-
public function is_available() {
16+
public function is_available(): bool {
1717
if ( ! $this->try_retrieve_banks() ) {
1818
return false;
1919
}
2020

2121
return parent::is_available();
2222
}
23-
24-
public function payment_fields() {
25-
parent::payment_fields();
26-
$this->agreements_field();
27-
}
2823
}
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
use Payu\PaymentGateway\Gateways\WC_Payu_Gateways;
3+
namespace Payu\PaymentGateway\Gateways;
44

55
class WC_Gateway_PayuPaypo extends WC_Payu_Gateways {
66
protected $paytype = 'dpp';
@@ -13,15 +13,11 @@ function __construct() {
1313
}
1414
}
1515

16-
public function is_available() {
16+
public function is_available(): bool {
1717
if ( ! $this->try_retrieve_banks() ) {
1818
return false;
1919
}
2020

2121
return parent::is_available();
2222
}
23-
public function payment_fields() {
24-
parent::payment_fields();
25-
$this->agreements_field();
26-
}
2723
}

‎includes/WC_Gateway_PayuStandard.php ‎Payu/Gateways/WC_Gateway_PayuStandard.php

+2-7
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
<?php
22

3-
use Payu\PaymentGateway\Gateways\WC_Payu_Gateways;
3+
namespace Payu\PaymentGateway\Gateways;
44

55
class WC_Gateway_PayuStandard extends WC_Payu_Gateways {
66

77
function __construct() {
88
parent::__construct( 'payustandard' );
99
}
1010

11-
public function is_available() {
11+
public function is_available(): bool {
1212
if ( ! $this->try_retrieve_banks() ) {
1313
return false;
1414
}
@@ -26,9 +26,4 @@ public function try_retrieve_banks(): bool {
2626

2727
return false;
2828
}
29-
30-
public function payment_fields() {
31-
parent::payment_fields();
32-
$this->agreements_field();
33-
}
3429
}
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
use Payu\PaymentGateway\Gateways\WC_Payu_Gateways;
3+
namespace Payu\PaymentGateway\Gateways;
44

55
class WC_Gateway_PayuTwistoPl extends WC_Payu_Gateways {
66
protected $paytype = 'dpt';
@@ -13,15 +13,11 @@ function __construct() {
1313
}
1414
}
1515

16-
public function is_available() {
16+
public function is_available(): bool {
1717
if ( ! $this->try_retrieve_banks() ) {
1818
return false;
1919
}
2020

2121
return parent::is_available();
2222
}
23-
public function payment_fields() {
24-
parent::payment_fields();
25-
$this->agreements_field();
26-
}
2723
}

‎Payu/Gateways/WC_PayuGateway.php

+6
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,14 @@
44

55
interface WC_PayuGateway {
66
public function is_enabled(): bool;
7+
78
public function get_payu_method_title(): string;
9+
810
public function get_payu_method_description(): string;
11+
912
public function get_payu_method_icon(): string;
13+
1014
public function is_payu_show_terms_info(): bool;
15+
16+
public function get_additional_data(): array;
1117
}

‎Payu/Gateways/WC_Payu_Gateways.php

+11-2
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public function get_payu_method_title(): string {
8686
}
8787

8888
public function get_payu_method_description(): string {
89-
return $this->get_description();
89+
return $this->description;
9090
}
9191

9292
public function get_payu_method_icon(): string {
@@ -97,6 +97,10 @@ public function is_payu_show_terms_info(): bool {
9797
return $this->show_terms_info;
9898
}
9999

100+
public function get_additional_data(): array {
101+
return [];
102+
}
103+
100104
public function enqueue_payu_gateway_assets() {
101105
wp_enqueue_script( 'payu-gateway', plugins_url( '/assets/js/payu-gateway.js', PAYU_PLUGIN_FILE ),
102106
[ 'jquery' ], PAYU_PLUGIN_VERSION, true );
@@ -144,6 +148,11 @@ protected function get_privacy_policy_url() {
144148
return get_locale() === 'pl_PL' ? self::PRIVACY_PL : self::PRIVACY_EN;
145149
}
146150

151+
public function payment_fields(): void {
152+
parent::payment_fields();
153+
$this->agreements_field();
154+
}
155+
147156
/**
148157
* @return void
149158
*/
@@ -580,7 +589,7 @@ public function init_OpenPayU( $currency = null ) {
580589
*
581590
* @return bool
582591
*/
583-
public function is_available() {
592+
public function is_available(): bool {
584593
$order = null;
585594
$needs_shipping = false;
586595

‎includes/WC_Gateway_PayuListBanks.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ function __construct() {
1313
}
1414
}
1515

16-
public function is_available() {
16+
public function is_available(): bool {
1717
if ( ! $this->try_retrieve_banks() ) {
1818
return false;
1919
}
@@ -35,7 +35,7 @@ protected function try_retrieve_banks(): bool {
3535
return false;
3636
}
3737

38-
public function payment_fields() {
38+
public function payment_fields(): void {
3939
parent::payment_fields();
4040

4141
$response = $this->get_payu_response();

0 commit comments

Comments
 (0)