Skip to content

Commit

Permalink
Don't allow auto-renew membership if frequencies do not match
Browse files Browse the repository at this point in the history
  • Loading branch information
mattwire committed Jan 19, 2018
1 parent d753245 commit 669f24e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CRM/Contribute/BAO/Contribution.php
Original file line number Diff line number Diff line change
Expand Up @@ -5395,6 +5395,10 @@ protected static function isPaymentInstrumentChange(&$params, $pendingStatuses)
protected static function updateMembershipBasedOnCompletionOfContribution($contribution, $memberships, $primaryContributionID, $changeDate, $contributionStatus = 'Completed') {
foreach ($memberships as $membershipTypeIdKey => $membership) {
if ($membership) {
if (!CRM_Member_BAO_Membership::isRecurFrequencyEqualToMembershipType($membership->membership_type_id, $membership->contribution_recur_id)) {
Civi::log()->warning('You have enabled auto-renew on membership (id=' . $membership->id . ') but the frequencies do not match! The membership will not be auto-renewed.');
continue;
}
$membershipParams = array(
'id' => $membership->id,
'contact_id' => $membership->contact_id,
Expand Down
28 changes: 28 additions & 0 deletions CRM/Member/BAO/Membership.php
Original file line number Diff line number Diff line change
Expand Up @@ -1700,6 +1700,34 @@ public static function isSubscriptionCancelled($mid) {
return FALSE;
}

/**
* Check if the membership type has the same frequency as the recurring contribution
* @param $membershipTypeId
* @param $contributionRecurId
*
* @return bool
*/
public static function isRecurFrequencyEqualToMembershipType($membershipTypeId, $contributionRecurId) {
try {
$membershipType = civicrm_api3('MembershipType', 'getsingle', array(
'return' => array("duration_unit", "duration_interval"),
'id' => $membershipTypeId,
));
$contributionRecur = civicrm_api3('ContributionRecur', 'getsingle', array(
'return' => array("frequency_unit", "frequency_interval"),
'id' => $contributionRecurId,
));
if (($membershipType['duration_unit'] === $contributionRecur['frequency_unit'])
&& ($membershipType['duration_interval'] === $contributionRecur['frequency_interval'])) {
return TRUE;
}
}
catch (CiviCRM_API3_Exception $e) {
return FALSE;
}
return FALSE;
}

/**
* Get membership joins for a specified membership type.
*
Expand Down

0 comments on commit 669f24e

Please sign in to comment.