Skip to content

Commit

Permalink
Clean up UpdateBilling/UpdateSubscription/ContributionRecur to use ge…
Browse files Browse the repository at this point in the history
…tters
  • Loading branch information
mattwire committed Sep 23, 2021
1 parent 3f52749 commit cc929f4
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 43 deletions.
9 changes: 9 additions & 0 deletions CRM/Contribute/Form/ContributionRecur.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,15 @@ protected function getSubscriptionContactID() {
return $sub->contact_id ? (int) $sub->contact_id : FALSE;
}

/**
* Get the recurring contribution ID.
*
* @return int
*/
protected function getContributionRecurID(): int {
return $this->getSubscriptionDetails()->recur_id;
}

/**
* Is this being used by a front end user to update their own recurring.
*
Expand Down
64 changes: 32 additions & 32 deletions CRM/Contribute/Form/UpdateBilling.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ public function preProcess() {
parent::preProcess();
if ($this->_crid) {
// Are we cancelling a recurring contribution that is linked to an auto-renew membership?
if ($this->_subscriptionDetails->membership_id) {
$this->_mid = $this->_subscriptionDetails->membership_id;
if ($this->getSubscriptionDetails()->membership_id) {
$this->_mid = $this->getSubscriptionDetails()->membership_id;
}
}

Expand All @@ -47,14 +47,13 @@ public function preProcess() {
if ($this->_mid) {
$this->_paymentProcessor = CRM_Financial_BAO_PaymentProcessor::getProcessorForEntity($this->_mid, 'membership', 'info');
$this->_paymentProcessor['object'] = CRM_Financial_BAO_PaymentProcessor::getProcessorForEntity($this->_mid, 'membership', 'obj');
$this->_subscriptionDetails = CRM_Contribute_BAO_ContributionRecur::getSubscriptionDetails($this->_mid, 'membership');
$membershipTypes = CRM_Member_PseudoConstant::membershipType();
$membershipTypeId = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_Membership', $this->_mid, 'membership_type_id');
$this->assign('membershipType', CRM_Utils_Array::value($membershipTypeId, $membershipTypes));
$this->_mode = 'auto_renew';
}

if ((!$this->_crid && !$this->_coid && !$this->_mid) || (!$this->_subscriptionDetails)) {
if ((!$this->_crid && !$this->_coid && !$this->_mid) || (!$this->getSubscriptionDetails())) {
throw new CRM_Core_Exception('Required information missing.');
}

Expand All @@ -67,10 +66,10 @@ public function preProcess() {

$this->assignBillingType();

$this->assign('frequency_unit', $this->_subscriptionDetails->frequency_unit);
$this->assign('frequency_interval', $this->_subscriptionDetails->frequency_interval);
$this->assign('amount', $this->_subscriptionDetails->amount);
$this->assign('installments', $this->_subscriptionDetails->installments);
$this->assign('frequency_unit', $this->getSubscriptionDetails()->frequency_unit);
$this->assign('frequency_interval', $this->getSubscriptionDetails()->frequency_interval);
$this->assign('amount', $this->getSubscriptionDetails()->amount);
$this->assign('installments', $this->getSubscriptionDetails()->installments);
$this->assign('mode', $this->_mode);

// handle context redirection
Expand All @@ -86,7 +85,7 @@ public function preProcess() {
public function setDefaultValues() {
$this->_defaults = [];

if ($this->_subscriptionDetails->contact_id) {
if ($this->getSubscriptionDetails()->contact_id) {
$fields = [];
$names = array(
'first_name',
Expand All @@ -106,7 +105,7 @@ public function setDefaultValues() {
$fields["email-{$this->_bltID}"] = 1;
$fields['email-Primary'] = 1;

CRM_Core_BAO_UFGroup::setProfileDefaults($this->_subscriptionDetails->contact_id, $fields, $this->_defaults);
CRM_Core_BAO_UFGroup::setProfileDefaults($this->getSubscriptionDetails()->contact_id, $fields, $this->_defaults);

// use primary email address if billing email address is empty
if (empty($this->_defaults["email-{$this->_bltID}"]) &&
Expand Down Expand Up @@ -202,28 +201,29 @@ public function postProcess() {
$processorParams['country'] = CRM_Core_PseudoConstant::country($params["billing_country_id-{$this->_bltID}"], FALSE);
$processorParams['month'] = CRM_Core_Payment_Form::getCreditCardExpirationMonth($processorParams);
$processorParams['year'] = CRM_Core_Payment_Form::getCreditCardExpirationYear($processorParams);
$processorParams['subscriptionId'] = $this->getSubscriptionDetails()->processor_id;
$processorParams['amount'] = $this->_subscriptionDetails->amount;
$processorParams['recurProcessorID'] = $processorParams['subscriptionId'] = $this->getSubscriptionDetails()->processor_id;
$processorParams['amount'] = $this->getSubscriptionDetails()->amount;
$processorParams['contributionRecurID'] = $this->getContributionRecurID();
$message = '';
$updateSubscription = $this->_paymentProcessor['object']->updateSubscriptionBillingInfo($message, $processorParams);
if (is_a($updateSubscription, 'CRM_Core_Error')) {
CRM_Core_Error::displaySessionError($updateSubscription);
}
elseif ($updateSubscription) {
$ctype = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $this->_subscriptionDetails->contact_id, 'contact_type');
$ctype = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $this->getSubscriptionDetails()->contact_id, 'contact_type');
CRM_Contact_BAO_Contact::createProfileContact($params,
$fields,
$this->_subscriptionDetails->contact_id,
$this->getSubscriptionDetails()->contact_id,
NULL,
NULL,
$ctype
);

// build tpl params
if ($this->_subscriptionDetails->membership_id) {
$inputParams = array('id' => $this->_subscriptionDetails->membership_id);
if ($this->getSubscriptionDetails()->membership_id) {
$inputParams = array('id' => $this->getSubscriptionDetails()->membership_id);
CRM_Member_BAO_Membership::getValues($inputParams, $tplParams);
$tplParams = $tplParams[$this->_subscriptionDetails->membership_id];
$tplParams = $tplParams[$this->getSubscriptionDetails()->membership_id];
$tplParams['membership_status'] = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipStatus', $tplParams['status_id']);
$tplParams['membershipType'] = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', $tplParams['membership_type_id']);
$status = ts('Billing details for your automatically renewed %1 membership have been updated.',
Expand All @@ -235,18 +235,18 @@ public function postProcess() {
else {
$status = ts('Billing details for the recurring contribution of %1, every %2 %3 have been updated.',
array(
1 => $this->_subscriptionDetails->amount,
2 => $this->_subscriptionDetails->frequency_interval,
3 => $this->_subscriptionDetails->frequency_unit,
1 => $this->getSubscriptionDetails()->amount,
2 => $this->getSubscriptionDetails()->frequency_interval,
3 => $this->getSubscriptionDetails()->frequency_unit,
)
);
$msgTitle = ts('Details Updated');
$msgType = 'success';

$tplParams = array(
'recur_frequency_interval' => $this->_subscriptionDetails->frequency_interval,
'recur_frequency_unit' => $this->_subscriptionDetails->frequency_unit,
'amount' => $this->_subscriptionDetails->amount,
'recur_frequency_interval' => $this->getSubscriptionDetails()->frequency_interval,
'recur_frequency_unit' => $this->getSubscriptionDetails()->frequency_unit,
'amount' => $this->getSubscriptionDetails()->amount,
);
}

Expand Down Expand Up @@ -297,7 +297,7 @@ public function postProcess() {
<br/>{$this->_defaults['address']}";

$activityParams = array(
'source_contact_id' => $this->_subscriptionDetails->contact_id,
'source_contact_id' => $this->getSubscriptionDetails()->contact_id,
'activity_type_id' => CRM_Core_PseudoConstant::getKey(
'CRM_Activity_BAO_Activity',
'activity_type_id',
Expand All @@ -317,31 +317,31 @@ public function postProcess() {
CRM_Activity_BAO_Activity::create($activityParams);

// send notification
if ($this->_subscriptionDetails->contribution_page_id) {
if ($this->getSubscriptionDetails()->contribution_page_id) {
CRM_Core_DAO::commonRetrieveAll('CRM_Contribute_DAO_ContributionPage', 'id',
$this->_subscriptionDetails->contribution_page_id, $value, array(
$this->getSubscriptionDetails()->contribution_page_id, $value, array(
'title',
'receipt_from_name',
'receipt_from_email',
)
);
$receiptFrom = '"' . CRM_Utils_Array::value('receipt_from_name', $value[$this->_subscriptionDetails->contribution_page_id]) . '" <' . $value[$this->_subscriptionDetails->contribution_page_id]['receipt_from_email'] . '>';
$receiptFrom = '"' . CRM_Utils_Array::value('receipt_from_name', $value[$this->getSubscriptionDetails()->contribution_page_id]) . '" <' . $value[$this->getSubscriptionDetails()->contribution_page_id]['receipt_from_email'] . '>';
}
else {
$domainValues = CRM_Core_BAO_Domain::getNameAndEmail();
$receiptFrom = "$domainValues[0] <$domainValues[1]>";
}
list($donorDisplayName, $donorEmail) = CRM_Contact_BAO_Contact::getContactDetails($this->_subscriptionDetails->contact_id);
list($donorDisplayName, $donorEmail) = CRM_Contact_BAO_Contact::getContactDetails($this->getSubscriptionDetails()->contact_id);
$tplParams['contact'] = array('display_name' => $donorDisplayName);

$tplParams = array_merge($tplParams, CRM_Contribute_Form_AbstractEditPayment::formatCreditCardDetails($processorParams));

$sendTemplateParams = array(
'groupName' => $this->_subscriptionDetails->membership_id ? 'msg_tpl_workflow_membership' : 'msg_tpl_workflow_contribution',
'valueName' => $this->_subscriptionDetails->membership_id ? 'membership_autorenew_billing' : 'contribution_recurring_billing',
'contactId' => $this->_subscriptionDetails->contact_id,
'groupName' => $this->getSubscriptionDetails()->membership_id ? 'msg_tpl_workflow_membership' : 'msg_tpl_workflow_contribution',
'valueName' => $this->getSubscriptionDetails()->membership_id ? 'membership_autorenew_billing' : 'contribution_recurring_billing',
'contactId' => $this->getSubscriptionDetails()->contact_id,
'tplParams' => $tplParams,
'isTest' => $this->_subscriptionDetails->is_test,
'isTest' => $this->getSubscriptionDetails()->is_test,
'PDFFilename' => 'receipt.pdf',
'from' => $receiptFrom,
'toName' => $donorDisplayName,
Expand Down
14 changes: 3 additions & 11 deletions CRM/Contribute/Form/UpdateSubscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,10 +206,11 @@ public function postProcess() {
}

// if this is an update of an existing recurring contribution, pass the ID
$params['id'] = $this->getContributionRecurID();
$params['contributionRecurID'] = $params['id'] = $this->getContributionRecurID();
$message = '';

$params['subscriptionId'] = $this->getSubscriptionDetails()->processor_id;
$params['recurProcessorID'] = $params['subscriptionId'] = $this->getSubscriptionDetails()->processor_id;

$updateSubscription = TRUE;
if ($this->_paymentProcessorObj->supports('changeSubscriptionAmount')) {
try {
Expand Down Expand Up @@ -317,13 +318,4 @@ public function postProcess() {
}
}

/**
* Get the recurring contribution ID.
*
* @return int
*/
protected function getContributionRecurID(): int {
return $this->_subscriptionDetails->recur_id;
}

}

0 comments on commit cc929f4

Please sign in to comment.