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: add option to disable one time donations for recurring enabled forms #6886

Merged
merged 15 commits into from
Aug 28, 2023
Merged
Show file tree
Hide file tree
Changes from 7 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
34 changes: 16 additions & 18 deletions src/DonationForms/Actions/ConvertDonationAmountBlockToFieldsApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
use Give\Framework\FieldsAPI\Radio;
use Give\Subscriptions\ValueObjects\SubscriptionPeriod;


class ConvertDonationAmountBlockToFieldsApi
{

Expand Down Expand Up @@ -56,7 +55,9 @@ public function __invoke(BlockModel $block, string $currency): DonationAmount

/** @var Amount $amountNode */
$amountNode = $group->getNodeByName('amount');
$defaultLevel = (float)$block->getAttribute('defaultLevel') > 0 ? (float)$block->getAttribute('defaultLevel') : 10;
$defaultLevel = (float)$block->getAttribute('defaultLevel') > 0 ? (float)$block->getAttribute(
'defaultLevel'
) : 10;
$amountNode
->label($block->getAttribute('label'))
->levels(...array_map('absint', $block->getAttribute('levels')))
Expand Down Expand Up @@ -133,19 +134,26 @@ protected function getRecurringAmountPeriodField(BlockModel $block): Field
->rules(new SubscriptionPeriodRule());
}

$recurringBillingPeriodOptions = $block->getAttribute('recurringBillingPeriodOptions');
if (count($block->getAttribute('recurringBillingPeriodOptions')) > 1) {
$recurringBillingPeriodOptions = $block->getAttribute('recurringBillingPeriodOptions');
} else {
$recurringBillingPeriodOptions = array_merge(['one-time'],
$block->getAttribute('recurringBillingPeriodOptions'));
}

$options = $this->mergePeriodOptionsWithOneTime(
array_map(static function ($option) {
$options = array_map(static function ($option) {
if (SubscriptionPeriod::isValid($option)) {
$subscriptionPeriod = new SubscriptionPeriod($option);

return new Option($subscriptionPeriod->getValue(), $subscriptionPeriod->label(0));
}, $recurringBillingPeriodOptions)
);
}

return new Option($option, $option === 'one-time' ? __('One Time', 'give') : ucfirst($option));
}, $recurringBillingPeriodOptions);

$recurringOptInDefault = $block->getAttribute('recurringOptInDefaultBillingPeriod');

if (!empty($recurringOptInDefault) && $recurringOptInDefault !== 'one-time') {
if (!empty($recurringOptInDefault) && SubscriptionPeriod::isValid($recurringOptInDefault)) {
$subscriptionPeriod = new SubscriptionPeriod($recurringOptInDefault);

$defaultValue = $subscriptionPeriod->getValue();
Expand All @@ -159,14 +167,4 @@ protected function getRecurringAmountPeriodField(BlockModel $block): Field
->options(...$options)
->rules(new SubscriptionPeriodRule());
}

/**
* @since 3.0.0
*/
protected function mergePeriodOptionsWithOneTime(array $options): array
{
return array_merge([
new Option('one-time', __('One Time', 'give'))
], $options);
}
}
3 changes: 2 additions & 1 deletion src/FormBuilder/resources/js/form-builder/src/blocks.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@
"customAmount": "true",
"customAmountMin": 1,
"recurringBillingPeriodOptions": [
"one-time",
"month"
],
"recurringBillingPeriod": "month",
"recurringBillingInterval": 1,
"recurringDonationChoice": "admin",
"recurringDonationChoice": "donor",
"recurringEnabled": false,
"recurringLengthOfTime": "0",
"recurringOptInDefaultBillingPeriod": "month",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ const Edit = ({attributes, setAttributes}) => {
label={__('Billing Period', 'give')}
hideLabelFromVision={true}
selected={recurringOptInDefaultBillingPeriod ?? options[0]}
options={['one-time'].concat(options).map((option) => {
options={options.map((option) => {
return {
label: 'one-time' === option ? __('One Time', 'give') : periodLookup[option].adjective,
value: option,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {getFormBuilderData} from '@givewp/form-builder/common/getWindowData';
import {useCallback, useState} from '@wordpress/element';
import Options from '@givewp/form-builder/components/OptionsPanel';
import {OptionProps} from '@givewp/form-builder/components/OptionsPanel/types';
import {useEffect} from 'react';

const compareBillingPeriods = (val1: string, val2: string): number => {
const index1 = Object.keys(periodLookup).indexOf(val1);
Expand Down Expand Up @@ -43,6 +44,20 @@ const Inspector = ({attributes, setAttributes}) => {
recurringOptInDefaultBillingPeriod,
} = attributes;

useEffect(() => {
// recurringBillingPeriodOptions are meant to be selected by the donor so there should always be at least two options.
// If there is only one recurring billing period option, add 'one-time' to the options as a fallback.
if (recurringBillingPeriodOptions.length === 1 && !recurringBillingPeriodOptions.includes('one-time')) {
const options = Array.from(new Set(recurringBillingPeriodOptions.concat(['one-time'])));

options.sort(compareBillingPeriods);

setAttributes({
recurringBillingPeriodOptions: options,
});
}
}, [recurringBillingPeriodOptions]);

const addBillingPeriodOption = useCallback(
(value) => {
const options = Array.from(new Set(recurringBillingPeriodOptions.concat([value])));
Expand Down Expand Up @@ -218,121 +233,126 @@ const Inspector = ({attributes, setAttributes}) => {
</PanelRow>
)}
{!!isRecurring && (
<PanelRow>
<SelectControl
label={__('Donation choice', 'give')}
options={[
{label: __('Admin', 'give'), value: 'admin'},
{label: __('Donor', 'give'), value: 'donor'},
]}
value={recurringDonationChoice}
onChange={(recurringDonationChoice) => setAttributes({recurringDonationChoice})}
/>
</PanelRow>
)}
{!!isRecurring && (
<PanelRow>
<SelectControl
label={__('Billing interval', 'give')}
options={[
{label: __('Every', 'give'), value: '1'},
{label: __('Every 2nd', 'give'), value: '2'},
{label: __('Every 3rd', 'give'), value: '3'},
{label: __('Every 4th', 'give'), value: '4'},
{label: __('Every 5th', 'give'), value: '5'},
{label: __('Every 6th', 'give'), value: '6'},
]}
value={recurringBillingInterval}
onChange={(recurringBillingInterval) => setAttributes({recurringBillingInterval})}
/>
</PanelRow>
)}
{!!isRecurring && (
<PanelRow>
{'admin' === recurringDonationChoice && (
<>
<PanelRow>
<SelectControl
label={__('Billing period', 'give')}
label={__('Donation choice', 'give')}
options={[
{label: __('Day', 'give'), value: 'day'},
{label: __('Week', 'give'), value: 'week'},
{label: __('Month', 'give'), value: 'month'},
{label: __('Quarter', 'give'), value: 'quarter'},
{label: __('Year', 'give'), value: 'year'},
{label: __('Admin', 'give'), value: 'admin'},
{label: __('Donor', 'give'), value: 'donor'},
]}
value={recurringBillingPeriod}
onChange={(recurringBillingPeriod) => setAttributes({recurringBillingPeriod})}
value={recurringDonationChoice}
onChange={(recurringDonationChoice) => setAttributes({recurringDonationChoice})}
/>
)}
{'donor' === recurringDonationChoice && (
<BaseControl id={'recurringBillingPeriodOptions'} label={__('Billing period', 'give')}>
<div
style={{
width: '100%',
display: 'grid',
gridTemplateColumns: '1fr 1fr',
}}
>
{[
</PanelRow>
<PanelRow>
<SelectControl
label={__('Billing interval', 'give')}
options={[
{label: __('Every', 'give'), value: '1'},
{label: __('Every 2nd', 'give'), value: '2'},
{label: __('Every 3rd', 'give'), value: '3'},
{label: __('Every 4th', 'give'), value: '4'},
{label: __('Every 5th', 'give'), value: '5'},
{label: __('Every 6th', 'give'), value: '6'},
]}
value={recurringBillingInterval}
onChange={(recurringBillingInterval) => setAttributes({recurringBillingInterval})}
/>
</PanelRow>
<PanelRow>
{'admin' === recurringDonationChoice && (
<SelectControl
label={__('Billing period', 'give')}
options={[
{label: __('Day', 'give'), value: 'day'},
{label: __('Week', 'give'), value: 'week'},
{label: __('Month', 'give'), value: 'month'},
{label: __('Quarter', 'give'), value: 'quarter'},
{label: __('Year', 'give'), value: 'year'},
].map((option) => (
<CheckboxControl
key={option.value}
label={option.label}
checked={recurringBillingPeriodOptions.includes(option.value)}
onChange={(checked) =>
checked
? addBillingPeriodOption(option.value)
: removeBillingPeriodOption(option.value)
}
disabled={
recurringBillingPeriodOptions.length === 1 &&
recurringBillingPeriodOptions.includes(option.value) // This is the last checked option.
}
//@ts-ignore
__nextHasNoMarginBottom={true}
/>
))}
</div>
</BaseControl>
)}
</PanelRow>
)}
{isRecurring && 'donor' === recurringDonationChoice && (
<PanelRow>
<SelectControl
label={__('Default billing period', 'give')}
value={recurringOptInDefaultBillingPeriod ?? 'month'}
options={['one-time'].concat(recurringBillingPeriodOptions).map((value) => ({
label: periodLookup[value].singular
.toLowerCase()
.replace(/\w/, (firstLetter) => firstLetter.toUpperCase()),
value: value,
}))}
onChange={(recurringOptInDefaultBillingPeriod) =>
setAttributes({recurringOptInDefaultBillingPeriod})
}
/>
</PanelRow>
)}
{isRecurring && (
<PanelRow>
<SelectControl
label={__('Number of Payments', 'give')}
//@ts-ignore
options={[{label: __('Ongoing', 'give'), value: 0}].concat(
[...Array(24 + 1).keys()].slice(2).map((value) => ({
label: sprintf(__('%d payments', 'give'), value),
value: value,
}))
]}
value={recurringBillingPeriod}
onChange={(recurringBillingPeriod) => setAttributes({recurringBillingPeriod})}
/>
)}
value={recurringLengthOfTime}
onChange={(recurringLengthOfTime) => setAttributes({recurringLengthOfTime})}
/>
</PanelRow>
{'donor' === recurringDonationChoice && (
<BaseControl
id={'recurringBillingPeriodOptions'}
label={__('Billing period', 'give')}
help={__(
'Select the billing period options you want donors to choose from. There must always be at least two enabled.',
'give'
)}
>
<div
style={{
width: '100%',
display: 'grid',
gridTemplateColumns: '1fr 1fr',
}}
>
{[
{label: __('One Time', 'give'), value: 'one-time'},
{label: __('Day', 'give'), value: 'day'},
{label: __('Week', 'give'), value: 'week'},
{label: __('Month', 'give'), value: 'month'},
{label: __('Quarter', 'give'), value: 'quarter'},
{label: __('Year', 'give'), value: 'year'},
].map((option) => (
<CheckboxControl
key={option.value}
label={option.label}
checked={recurringBillingPeriodOptions.includes(option.value)}
onChange={(checked) =>
checked
? addBillingPeriodOption(option.value)
: removeBillingPeriodOption(option.value)
}
disabled={
// There must always be at least two recurring billing period options.
recurringBillingPeriodOptions.length <= 2 &&
recurringBillingPeriodOptions.includes(option.value) // This is the last checked option.
}
//@ts-ignore
__nextHasNoMarginBottom={true}
/>
))}
</div>
</BaseControl>
)}
</PanelRow>
{'donor' === recurringDonationChoice && (
<PanelRow>
<SelectControl
label={__('Default billing period', 'give')}
value={recurringOptInDefaultBillingPeriod ?? 'month'}
options={recurringBillingPeriodOptions.map((value) => ({
label: periodLookup[value].singular
.toLowerCase()
.replace(/\w/, (firstLetter) => firstLetter.toUpperCase()),
value: value,
}))}
onChange={(recurringOptInDefaultBillingPeriod) =>
setAttributes({recurringOptInDefaultBillingPeriod})
}
/>
</PanelRow>
)}
<PanelRow>
<SelectControl
label={__('Number of Payments', 'give')}
//@ts-ignore
options={[{label: __('Ongoing', 'give'), value: 0}].concat(
[...Array(24 + 1).keys()].slice(2).map((value) => ({
label: sprintf(__('%d payments', 'give'), value),
value: value,
}))
)}
value={recurringLengthOfTime}
onChange={(recurringLengthOfTime) => setAttributes({recurringLengthOfTime})}
/>
</PanelRow>
</>
)}
</PanelBody>
</InspectorControls>
Expand Down
Loading