Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: hide gateways when donation amount is zero #7599

Merged
merged 12 commits into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,50 @@ import {createInterpolateElement, useEffect, useMemo} from '@wordpress/element';
import cx from 'classnames';

/**
* @since 3.0.0
* @unreleased
*/
function GatewayMissingMessage({currencyNotSupported}: {currencyNotSupported?: boolean}) {
function EmptyMessage({message}: {message: string}) {
return (
<em>
{currencyNotSupported
? __(
'The selected currency is not supported by any of the available payment gateways. Please select a different currency or contact the site administrator for assistance.',
'give'
)
: __(
'No gateways have been enabled yet. To get started accepting donations, enable a compatible payment gateway in your settings.',
'give'
)}
</em>
<div className="givewp-fields-gateways__gateway--empty">
<p>
<b>{__('Payment options are not available:', 'give')}</b>
</p>

<p>
<em>{message}</em>
</p>
</div>
);
}

/**
* @unreleased updated message to account for minimum donation amount
* @since 3.0.0
*/
function GatewayMissingMessage({
donationAmountMinimumNotReached,
currencyNotSupported,
}: {
donationAmountMinimumNotReached?: boolean;
currencyNotSupported?: boolean;
}) {
let message = __(
'No gateways have been enabled yet. To get started accepting donations, enable a compatible payment gateway in your settings.',
'give'
);

if (donationAmountMinimumNotReached) {
message = __('Donation amount must be greater than zero.', 'give');
} else if (currencyNotSupported) {
message = __(
'The selected currency is not supported by any of the available payment gateways. Please select a different currency or contact the site administrator for assistance.',
'give'
);
}

return <EmptyMessage message={message} />;
}

/**
* @since 3.0.0
*/
Expand Down Expand Up @@ -82,11 +108,14 @@ export default function Gateways({isTestMode, defaultValue, inputProps, gateways
const {setValue} = useFormContext();
const {currencySwitcherSettings} = useDonationFormSettings();

const donationAmount = useWatch({name: 'amount'});
const currency = useWatch({name: 'currency'});
const activeGatewayId = useWatch({name: 'gatewayId'});

const donationAmountMinimumNotReached = donationAmount === 0;

// filter gateway options if currency switcher settings are present
const gatewayOptions = useMemo(() => {
const gatewayOptionsWithCurrencySettings = useMemo(() => {
if (currencySwitcherSettings.length <= 1) {
return gateways;
}
Expand All @@ -100,6 +129,10 @@ export default function Gateways({isTestMode, defaultValue, inputProps, gateways
return gateways.filter(({id}) => currencySwitcherSetting.gateways.includes(id));
}, [currency]);

const gatewayOptions = useMemo(() => {
return donationAmountMinimumNotReached ? [] : gatewayOptionsWithCurrencySettings;
}, [donationAmountMinimumNotReached, gatewayOptionsWithCurrencySettings]);

// reset the default /selected gateway based on the gateway options available
useEffect(() => {
if (gatewayOptions.length > 0) {
Expand Down Expand Up @@ -129,7 +162,10 @@ export default function Gateways({isTestMode, defaultValue, inputProps, gateways
</ul>
</>
) : (
<GatewayMissingMessage currencyNotSupported={currencySwitcherSettings.length > 1} />
<GatewayMissingMessage
donationAmountMinimumNotReached={donationAmountMinimumNotReached}
currencyNotSupported={gatewayOptionsWithCurrencySettings.length > 1}
/>
)}

<ErrorMessage
Expand Down
18 changes: 18 additions & 0 deletions src/DonationForms/resources/styles/components/_gateways.scss
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,24 @@
display: block;
}

&--empty {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
border-style: solid;
border-width: 1px;
border-color: var(--givewp-red-500);
padding: var(--givewp-spacing-8) var(--givewp-spacing-12);
border-radius: var(--givewp-rounded-4);
font-size: var(--givewp-font-weight-paragraph-md);
min-height: 100px;
p {
text-align: center;
margin-bottom: 0;
}
}

&--test-gateway {
.no-fields {
display: flex;
Expand Down
Loading