Skip to content

Commit

Permalink
MAE-179: Adding memberships Auto-renewal support
Browse files Browse the repository at this point in the history
  • Loading branch information
omarabuhussein committed Jul 27, 2021
1 parent 1d06a28 commit 5f24ff4
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 4 deletions.
9 changes: 9 additions & 0 deletions includes/utils.inc
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ function wf_crm_field_options($field, $context, $data) {
elseif ($table == 'membership' && $name == 'num_terms') {
$ret = drupal_map_assoc(range(1, 9));
}
elseif ($table == 'membership' && $name == 'auto_renew') {
$ret = array(1 => t('Yes'), 0 => t('No'));
}
// Aside from the above special cases, most lists can be fetched from api.getoptions
else {
$params = ['field' => $name, 'context' => 'create'];
Expand Down Expand Up @@ -1235,6 +1238,12 @@ function wf_crm_get_fields($var = 'fields') {
'name' => t('End Date'),
'type' => 'date',
];
$fields['membership_auto_renew'] = [
'name' => t('Auto-renew Membership?'),
'type' => 'select',
'expose_list' => TRUE,
'value' => 0,
];
}
// Add campaign fields
if (in_array('CiviCampaign', $components)) {
Expand Down
52 changes: 48 additions & 4 deletions includes/wf_crm_webform_postprocess.inc
Original file line number Diff line number Diff line change
Expand Up @@ -1173,8 +1173,22 @@ class wf_crm_webform_postprocess extends wf_crm_webform_base {
// The api won't let us manually set status without this weird param
$params['skipStatusCal'] = !empty($params['status_id']);

if (isset($this->ent['contribution_recur'][1]['id'])) {
$params['contribution_recur_id'] = $this->ent['contribution_recur'][1]['id'];
/**
* If the webform configured with a recurring contribution
* and auto-renew for the membership set to True, then we
* set the membership contribution_recur_id field to
* point to the recurring contribution, since for a membership
* to be considered auto-renew in CiviCRM core, it should
* have both this field set to the related recurring contribution
* and for the related recurring contribution auto_renew field
* to be set to TRUE.
*/
if (isset($this->ent['contribution_recur'][1]['id']) && !empty($params['auto_renew'])) {
$contributionRecurId = $this->ent['contribution_recur'][1]['id'];

$params['contribution_recur_id'] = $contributionRecurId;

unset($params['auto_renew']);
}

$result = wf_civicrm_api('membership', 'create', $params);
Expand Down Expand Up @@ -1776,7 +1790,12 @@ class wf_crm_webform_postprocess extends wf_crm_webform_base {
// Only if #installments = 1, do we process a single transaction/contribution. #installments = 0 (or not set) in CiviCRM Core means open ended commitment;
$numInstallments = wf_crm_aval($contributionParams, 'installments', NULL, TRUE);
$frequencyInterval = wf_crm_aval($contributionParams, 'frequency_unit');
if ($numInstallments != 1 && !empty($frequencyInterval)) {
$isThereMembershipToBeAutoRenewed = $this->isThereMembershipToBeAutoRenewed();
if (($numInstallments != 1 || $isThereMembershipToBeAutoRenewed) && !empty($frequencyInterval)) {
if ($isThereMembershipToBeAutoRenewed) {
$contributionParams['auto_renew'] = 1;
}

$result = $this->contributionRecur($contributionParams);
}
else {
Expand Down Expand Up @@ -1834,6 +1853,10 @@ class wf_crm_webform_postprocess extends wf_crm_webform_base {
'financial_type_id' => $contributionParams['financial_type_id'],
];

if (!empty($contributionParams['auto_renew'])) {
$contributionRecurParams['auto_renew'] = 1;
}

if(empty($contributionParams['payment_processor_id'])) {
$contributionRecurParams['payment_processor_id'] = 'null';
}
Expand Down Expand Up @@ -1901,7 +1924,12 @@ class wf_crm_webform_postprocess extends wf_crm_webform_base {

$numInstallments = wf_crm_aval($params, 'installments', NULL, TRUE);
$frequencyInterval = wf_crm_aval($params, 'frequency_unit');
if ($numInstallments != 1 && !empty($frequencyInterval) && $this->contributionIsPayLater) {
$isThereMembershipToBeAutoRenewed = $this->isThereMembershipToBeAutoRenewed();
if (($numInstallments != 1 || $isThereMembershipToBeAutoRenewed) && !empty($frequencyInterval)) {
if ($isThereMembershipToBeAutoRenewed) {
$params['auto_renew'] = 1;
}

$result = $this->contributionRecur($params, 'deferred');
}
else {
Expand All @@ -1911,6 +1939,22 @@ class wf_crm_webform_postprocess extends wf_crm_webform_base {
$this->ent['contribution'][1]['id'] = $result['id'];
}

/**
* Determines if the webform is configured to have
* any membership to be auto-renewed or not.
*/
private function isThereMembershipToBeAutoRenewed() {
$autoRenewMembership = FALSE;
foreach ($this->data['membership'] as $contactMemberships) {
foreach($contactMemberships['membership'] as $membership) {
if (!empty($membership['auto_renew'])) {
$autoRenewMembership = TRUE;
}
}
}
return $autoRenewMembership;
}

/**
* Call IPN payment processor to redirect to payment site
*/
Expand Down

0 comments on commit 5f24ff4

Please sign in to comment.