Skip to content
This repository has been archived by the owner on Jun 25, 2021. It is now read-only.

Support fixed day of month #104

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
8 changes: 7 additions & 1 deletion CRM/Core/Payment/GoCardless.php
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,12 @@ public function doPayment(&$params, $component = 'contribute') {
];
}

// Added via javascript so "removed" by quickform after submission
$dayOfMonth = CRM_Utils_Request::retrieveValue('day_of_month', 'Integer', NULL, FALSE, 'POST');
if ($dayOfMonth) {
$params['day_of_month'] = $dayOfMonth;
}

$url = $this->createRedirectFlow($params, $component);
CRM_Utils_System::redirect($url);
}
Expand Down Expand Up @@ -338,7 +344,7 @@ public function createRedirectFlow(&$params, $component) {
'payment_processor_id' => $this->_paymentProcessor['id'],
"description" => $params['description'],
];
foreach (['contributionID', 'contributionRecurID', 'contactID', 'membershipID'] as $_) {
foreach (['contributionID', 'contributionRecurID', 'contactID', 'membershipID', 'day_of_month'] as $_) {
if (!empty($params[$_])) {
$sesh_store[$redirect_flow->id][$_] = $params[$_];
}
Expand Down
73 changes: 72 additions & 1 deletion CRM/GoCardlessUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,9 @@ public static function completeRedirectFlowWithGoCardless($deets) {
'links' => ['mandate' => $redirect_flow->links->mandate],
'metadata' => ['civicrm' => $metadata],
];
if ($deets['day_of_month']) {
mattwire marked this conversation as resolved.
Show resolved Hide resolved
$params['day_of_month'] = $deets['day_of_month'];
}

if (isset($installments)) {
$params['count'] = $installments;
Expand Down Expand Up @@ -483,9 +486,77 @@ public static function handleContributeFormHacks() {
->addWhere('is_test', 'IS NOT NULL')
->execute()
->column('id');

$js = file_get_contents(E::path('js/gcform.js'));
$js = str_replace('var goCardlessProcessorIDs = [];', 'var goCardlessProcessorIDs = ' . json_encode($paymentProcessorsIDs) . ';', $js);
$js = str_replace([
'var goCardlessProcessorIDs = [];',
'var dayOfMonthOptions = {};'
],
[
'var goCardlessProcessorIDs = ' . json_encode($paymentProcessorsIDs) . ';',
'var dayOfMonthOptions = ' . json_encode(self::getDayOfMonthOptions()) . ';'
],
$js
);
CRM_Core_Region::instance('page-body')->add(['markup' => "<script>$js</script>"]);
}
}

/**
* Get the formatted options for daysOfMonth eg. [1 => '1st']
* This is used for frontend presentation and backend settings
*
* @param bool $all
*
* @return array
*/
public static function getDayOfMonthOptions($all = FALSE) {
if ($all) {
$options = [0];
for ($i = 1; $i <= 28; $i++) {
// Add days 1 to 28 (29-31 are excluded because don't exist for some months)
$options[] = $i;
}
$options[] = -1;
}
else {
$options = static::getSettings()['daysOfMonth'];
}

foreach ($options as $option) {
switch ($option) {
case 0:
$dayOfMonthOptions[$option] = 'auto';
break;

case -1:
$dayOfMonthOptions[$option] = 'last';
break;

default:
$dayOfMonthOptions[$option] = self::formatPreferredCollectionDay($option);
mattwire marked this conversation as resolved.
Show resolved Hide resolved
}
}
return $dayOfMonthOptions;
}

/**
* Format collection day like 1st, 2nd, 3rd, 4th etc.
*
* @param $collectionDay
*
* @return string
*/
public static function formatPreferredCollectionDay($collectionDay) {
$ends = ['th', 'st', 'nd', 'rd', 'th', 'th', 'th', 'th', 'th', 'th'];
if ((($collectionDay%100) >= 11) && (($collectionDay%100) <= 13)) {
$abbreviation = $collectionDay . 'th';
}
else {
$abbreviation = $collectionDay . $ends[$collectionDay % 10];
}

return $abbreviation;
}
mattwire marked this conversation as resolved.
Show resolved Hide resolved

}
18 changes: 18 additions & 0 deletions ang/gocardless/GoCardlessSettings.html
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,24 @@ <h1 crm-page-title>{{ts('GoCardless settings')}}</h1>
</div>
</div>

<div class="crm-section">
<div crm-ui-field="{name: 'myForm.daysOfMonth', title: ts('Allowed days of month')}">
<select
crm-ui-id="myForm.daysOfMonth"
ng-model="gcSettings.daysOfMonth"
name="daysOfMonth"
class="crm-form-select"
multiple
size="15"
>
<option ng-repeat="item in gcDaysOfMonth" value="{{item.key}}">{{item.value}}</option>
</select>
<p>The allowed days of the month. Set to "auto" for the next available date or "last" for the last day of the month.
<br/><em>This only works if "Force Recurring" is enabled and "Monthly" or "Yearly" is selected.</em>
</p>
</div>
</div>

</div>

<div>
Expand Down
9 changes: 8 additions & 1 deletion ang/gocardless/GoCardlessSettings.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@
// Annoyingly this is duplicated from CRM_GoCardlessUtils::getSettings()
const defaults = {
forceRecurring: false,
sendReceiptsForCustomPayments: 'never'
sendReceiptsForCustomPayments: 'never',
daysOfMonth: ["0"]
};
Object.keys(defaults).forEach(k => {
if (!(k in gcSettings)) {
Expand All @@ -61,6 +62,12 @@
});
$scope.gcSettings = gcSettings;

const m={1:'1st', 2:'2nd', 3:'3rd', 21:'21st', 22:'22nd', 23:'23rd'};
const daysOfMonthOpts = [{key: '0', value: 'any day (earliest possible)'}];
for (var i=1;i<29;i++) daysOfMonthOpts.push({key: i.toString(), value: m[i] || (i + 'th')});
daysOfMonthOpts.push({key: '-1', value: 'last day of month'});
$scope.gcDaysOfMonth = daysOfMonthOpts;

// Make pay processors accessible
var ppTable = [];
var ppNames = {};
Expand Down
Binary file removed docs/gc-settings.jpg
Binary file not shown.
Binary file added docs/gc-settings.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 14 additions & 1 deletion docs/reference/settings.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Settings Page

![Screnshot of settings page](../gc-settings.jpg)
![Screnshot of settings page](../gc-settings.png)

## Force Recurring

Expand Down Expand Up @@ -60,3 +60,16 @@ in some custom code, you might not have set this `is_email_receipt` value.
and will pass 0 or 1 into the payment APIs.

- `defer` will pass in the value from the recurring contribution record.

## Allowed days of the month

The allowed days of the month.
Set to "auto" for the next available date or "last" for the last day of the month.
You can specify multiple options and it will give the user a choice when setting up the payment.

This only works if "Force Recurring" is enabled and "Monthly" or "Yearly" is selected.

#### Developers notes
* The GoCardless API also supports specifying "month" for "Yearly subscriptions" but that is not currently implemented.
* The "Force recurring" setting is required because we are sharing the `gcform.js` code and it was simpler to implement that way.

3 changes: 3 additions & 0 deletions info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
<compatibility>
<ver>5.35</ver>
</compatibility>
<classloader>
<psr4 prefix="Civi\" path="Civi"/>
</classloader>
<civix>
<namespace>CRM/GoCardless</namespace>
</civix>
Expand Down
Loading