Skip to content

Commit 5c4ac69

Browse files
authored
Add Boleto expiration setting (#2412)
* Add expiration setting to the RESTapi * Add expiration field * Update text control type to number and set min max * Sanitize input value * Make options extensible * Display expiration field on boleto settings page * Fix php linting error * Add changelog entry * Fix linting errors * Add voucher settings section * Add payment gateway voucher expiration to API request body * Fix request body condition * Update get and add payment gateway settings to their own methods * Fix linting errors * Fix more linting errors * Fix lint errors * Change update request body filter to a protected function * Update to protected function * Fix linting error
1 parent e344ac7 commit 5c4ac69

File tree

8 files changed

+120
-15
lines changed

8 files changed

+120
-15
lines changed

changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
= 6.7.0 - 2022-09-06 =
88
* Fix - Check payment method before updating payment method title.
99
* Fix - Use the eslint config at the root of the repo.
10+
* Add - Add Boleto expiration setting.
1011

1112
= 6.6.0 - 2022-08-17 =
1213
* Fix - Fix "Pending" text instead of numeric amount on Payment Request button on iOS.

client/data/payment-gateway/hooks.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,8 @@ export const usePaymentGatewayDescription = makePaymentGatewayHook(
8484
makeFieldName( '%s_description' ),
8585
''
8686
);
87+
88+
export const usePaymentGatewayExpiration = makePaymentGatewayHook(
89+
makeFieldName( '%s_expiration' ),
90+
''
91+
);

client/settings/payment-gateway-manager/constants.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import { __ } from '@wordpress/i18n';
2+
import { TextControl } from '@wordpress/components';
3+
import { usePaymentGatewayExpiration } from '../../data/payment-gateway/hooks';
24

35
export const gatewaysInfo = {
46
stripe_sepa: {
@@ -91,6 +93,38 @@ export const gatewaysInfo = {
9193
),
9294
guide:
9395
'https://stripe.com/docs/payments/payment-methods/overview#vouchers',
96+
Fields: () => {
97+
const [
98+
gatewayExpiration,
99+
setGatewayExpiration,
100+
] = usePaymentGatewayExpiration();
101+
102+
return (
103+
<>
104+
<h4>
105+
{ __(
106+
'Voucher settings',
107+
'woocommerce-gateway-stripe'
108+
) }
109+
</h4>
110+
<TextControl
111+
type="number"
112+
min="0"
113+
max="60"
114+
help={ __(
115+
'Set the number of days until expiration from 0 to 60 days. The default is 3 days.',
116+
'woocommerce-gateway-stripe'
117+
) }
118+
label={ __(
119+
'Expiration',
120+
'woocommerce-gateway-stripe'
121+
) }
122+
value={ gatewayExpiration }
123+
onChange={ setGatewayExpiration }
124+
/>
125+
</>
126+
);
127+
},
94128
},
95129
stripe_oxxo: {
96130
title: __( 'OXXO', 'woocommerce-gateway-stripe' ),

client/settings/payment-gateway-section/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ const StyledCheckboxLabel = styled.span`
3434
const PaymentGatewaySection = () => {
3535
const { section } = getQuery();
3636
const info = gatewaysInfo[ section ];
37+
const { Fields } = info;
3738
const [ enableGateway, setEnableGateway ] = useEnabledPaymentGateway();
3839
const [ gatewayName, setGatewayName ] = usePaymentGatewayName();
3940
const [
@@ -106,6 +107,7 @@ const PaymentGatewaySection = () => {
106107
value={ gatewayDescription }
107108
onChange={ setGatewayDescription }
108109
/>
110+
{ Fields && <Fields /> }
109111
<h4>
110112
{ __(
111113
'Webhook endpoints',

includes/abstracts/abstract-wc-stripe-payment-gateway-voucher.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -306,13 +306,19 @@ public function create_or_update_payment_intent( $order ) {
306306
$intent_to_be_updated = '/' . $intent->id;
307307
}
308308

309+
$body = [
310+
'amount' => WC_Stripe_Helper::get_stripe_amount( $amount, strtolower( $currency ) ),
311+
'currency' => strtolower( $currency ),
312+
'payment_method_types' => [ $this->stripe_id ],
313+
'description' => __( 'stripe - Order', 'woocommerce-gateway-stripe' ) . ' ' . $order->get_id(),
314+
];
315+
316+
if ( method_exists( $this, 'update_request_body_on_create_or_update_payment_intent' ) ) {
317+
$body = $this->update_request_body_on_create_or_update_payment_intent( $body );
318+
}
319+
309320
$payment_intent = WC_Stripe_API::request(
310-
[
311-
'amount' => WC_Stripe_Helper::get_stripe_amount( $amount, strtolower( $currency ) ),
312-
'currency' => strtolower( $currency ),
313-
'payment_method_types' => [ $this->stripe_id ],
314-
'description' => __( 'stripe - Order', 'woocommerce-gateway-stripe' ) . ' ' . $order->get_id(),
315-
],
321+
$body,
316322
'payment_intents' . $intent_to_be_updated
317323
);
318324

includes/admin/class-wc-rest-stripe-payment-gateway-controller.php

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,15 @@ public function get_payment_gateway_settings( $request = null ) {
8686
try {
8787
$id = $request->get_param( 'payment_gateway_id' );
8888
$this->instantiate_gateway( $id );
89-
return new WP_REST_Response(
90-
[
91-
'is_' . $id . '_enabled' => $this->gateway->is_enabled(),
92-
$id . '_name' => $this->gateway->get_option( 'title' ),
93-
$id . '_description' => $this->gateway->get_option( 'description' ),
94-
]
95-
);
89+
$settings = [
90+
'is_' . $id . '_enabled' => $this->gateway->is_enabled(),
91+
$id . '_name' => $this->gateway->get_option( 'title' ),
92+
$id . '_description' => $this->gateway->get_option( 'description' ),
93+
];
94+
if ( method_exists( $this->gateway, 'get_unique_settings' ) ) {
95+
$settings = $this->gateway->get_unique_settings( $settings );
96+
}
97+
return new WP_REST_Response( $settings );
9698
} catch ( Exception $exception ) {
9799
return new WP_REST_Response( [ 'result' => 'bad_request' ], 400 );
98100
}
@@ -110,7 +112,9 @@ public function update_payment_gateway_settings( WP_REST_Request $request ) {
110112
$this->update_is_gateway_enabled( $request );
111113
$this->update_gateway_name( $request );
112114
$this->update_gateway_description( $request );
113-
115+
if ( method_exists( $this->gateway, 'update_unique_settings' ) ) {
116+
$this->gateway->update_unique_settings( $request );
117+
}
114118
return new WP_REST_Response( [], 200 );
115119
} catch ( Exception $exception ) {
116120
return new WP_REST_Response( [ 'result' => 'bad_request' ], 400 );
@@ -155,7 +159,7 @@ private function update_gateway_name( WP_REST_Request $request ) {
155159
}
156160

157161
/**
158-
* Updates payment gateway title.
162+
* Updates payment gateway description.
159163
*
160164
* @param WP_REST_Request $request Request object.
161165
*/

includes/admin/stripe-boleto-settings.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@
4040
'default' => __( "You'll be able to download or print the Boleto after checkout.", 'woocommerce-gateway-stripe' ),
4141
'desc_tip' => true,
4242
],
43+
'expiration' => [
44+
'title' => __( 'Expiration', 'woocommerce-gateway-stripe' ),
45+
'type' => 'number',
46+
'description' => __( 'This controls the expiration in number of days for the voucher.', 'woocommerce-gateway-stripe' ),
47+
'default' => 3,
48+
'desc_tip' => true,
49+
],
4350
'webhook' => [
4451
'title' => __( 'Webhook Endpoints', 'woocommerce-gateway-stripe' ),
4552
'type' => 'title',

includes/payment-methods/class-wc-gateway-stripe-boleto.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,52 @@ public function __construct() {
5555
add_filter( 'wc_stripe_allowed_payment_processing_statuses', [ $this, 'add_allowed_payment_processing_statuses' ], 10, 2 );
5656
}
5757

58+
/**
59+
* Add payment gateway voucher expiration to API request body.
60+
*
61+
* @param array $body API request body.
62+
* @return array
63+
*/
64+
protected function update_request_body_on_create_or_update_payment_intent( $body ) {
65+
$body['payment_method_options'] = [
66+
'boleto' => [
67+
'expires_after_days' => $this->get_option( 'expiration' ),
68+
],
69+
];
70+
return $body;
71+
}
72+
73+
/**
74+
* Add payment gateway voucher expiration.
75+
*
76+
* @param array $settings Settings array.
77+
* @return array
78+
*/
79+
public function get_unique_settings( $settings ) {
80+
$settings[ $this->id . '_expiration' ] = $this->get_option( 'expiration' );
81+
return $settings;
82+
}
83+
84+
/**
85+
* Updates payment gateway voucher expiration.
86+
*
87+
* @param WP_REST_Request $request Request object.
88+
* @return void
89+
*/
90+
public function update_unique_settings( WP_REST_Request $request ) {
91+
$field_name = $this->id . '_expiration';
92+
$expiration = $request->get_param( $field_name );
93+
94+
if ( null === $expiration ) {
95+
return;
96+
}
97+
98+
$value = absint( $expiration );
99+
$value = min( 60, $value );
100+
$value = max( 0, $value );
101+
$this->update_option( 'expiration', $value );
102+
}
103+
58104
/**
59105
* Adds on-hold as accepted status during webhook handling on orders paid with voucher
60106
*

0 commit comments

Comments
 (0)