Skip to content

Commit 161cba7

Browse files
glaubersilvaJon Waldstein
and
Jon Waldstein
authored
Refactor: replace useWatch with useFormData to show the Fee Recovery amount on the modal for Apple/Google Pay (Stripe) (#7878)
Co-authored-by: Jon Waldstein <[email protected]>
1 parent e5cd80a commit 161cba7

File tree

1 file changed

+5
-41
lines changed

1 file changed

+5
-41
lines changed

src/PaymentGateways/Gateways/Stripe/StripePaymentElementGateway/stripePaymentElementGateway.tsx

+5-41
Original file line numberDiff line numberDiff line change
@@ -14,39 +14,6 @@ let stripePromise = null;
1414
let stripePaymentMethod = null;
1515
let stripePaymentMethodIsCreditCard = false;
1616

17-
// @see https://stripe.com/docs/currencies#zero-decimal
18-
const zeroDecimalCurrencies = [
19-
'BIF',
20-
'CLP',
21-
'DJF',
22-
'GNF',
23-
'JPY',
24-
'KMF',
25-
'KRW',
26-
'MGA',
27-
'PYG',
28-
'RWF',
29-
'UGX',
30-
'VND',
31-
'VUV',
32-
'XAF',
33-
'XOF',
34-
'XPF',
35-
];
36-
37-
/**
38-
* Takes in an amount value in dollar units and returns the calculated cents amount
39-
*
40-
* @since 3.0.0
41-
*/
42-
const dollarsToCents = (amount: string, currency: string) => {
43-
if (zeroDecimalCurrencies.includes(currency)) {
44-
return Math.round(parseFloat(amount));
45-
}
46-
47-
return Math.round(parseFloat(amount) * 100);
48-
};
49-
5017
const StripeFields = ({gateway}) => {
5118
const stripe = useStripe();
5219
const elements = useElements();
@@ -91,6 +58,7 @@ interface StripeGateway extends Gateway {
9158
}
9259

9360
/**
61+
* @unreleased Replace useWatch with useFormData
9462
* @since 3.18.0 added fields conditional when donation amount is zero
9563
* @since 3.13.0 Use only stripeKey to load the Stripe script (when stripeConnectedAccountId is missing) to prevent errors when the account is connected through API keys
9664
* @since 3.12.1 updated afterCreatePayment response type to include billing details address
@@ -200,20 +168,16 @@ const stripePaymentElementGateway: StripeGateway = {
200168
throw new Error('Stripe library was not able to load. Check your Stripe settings.');
201169
}
202170

203-
const {useWatch} = window.givewp.form.hooks;
204-
const donationType = useWatch({name: 'donationType'});
205-
const donationCurrency = useWatch({name: 'currency'});
206-
const donationAmount = useWatch({name: 'amount'});
207-
const stripeAmount = dollarsToCents(donationAmount, donationCurrency.toString().toUpperCase());
171+
const {isRecurring, currency, amountInMinorUnits: stripeAmount} = window.givewp.form.hooks.useFormData();
208172

209173
const stripeElementOptions: StripeElementsOptionsMode = {
210-
mode: donationType === 'subscription' ? 'subscription' : 'payment',
174+
mode: isRecurring ? 'subscription' : 'payment',
211175
amount: stripeAmount,
212-
currency: donationCurrency.toLowerCase(),
176+
currency: currency.toLowerCase(),
213177
appearance: appearanceOptions,
214178
};
215179

216-
if (donationAmount <= 0) {
180+
if (stripeAmount <= 0) {
217181
return <>{__('Donation amount must be greater than zero to proceed.', 'give')}</>;
218182
}
219183

0 commit comments

Comments
 (0)