Skip to content

Commit

Permalink
Merge pull request #29580 from eileenmcnaughton/payment_object_3
Browse files Browse the repository at this point in the history
[Update Subscription Form] Align UpdateSubscription to more recent practices
  • Loading branch information
mattwire authored Mar 6, 2024
2 parents efc9494 + 6cb7280 commit a2764a9
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 68 deletions.
28 changes: 9 additions & 19 deletions CRM/Contribute/Form/CancelSubscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,20 +66,13 @@ public function preProcess() {
'selfService' => $this->isSelfService(),
];

if ($this->_crid) {
// Are we cancelling a recurring contribution that is linked to an auto-renew membership?
if ($this->getSubscriptionDetails()->membership_id) {
$this->_mid = $this->getSubscriptionDetails()->membership_id;
}
}

if ($this->_mid) {
if ($this->getMembershipID()) {
$this->_mode = 'auto_renew';
// CRM-18468: crid is more accurate than mid for getting
// subscriptionDetails, so don't get them again.

$membershipTypes = CRM_Member_PseudoConstant::membershipType();
$membershipTypeId = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_Membership', $this->_mid, 'membership_type_id');
$membershipTypeId = $this->getMembershipValue('membership_type_id');
$membershipType = $membershipTypes[$membershipTypeId] ?? '';
$this->assign('membershipType', $membershipType);
$cancelRecurTextParams['membershipType'] = $membershipType;
Expand All @@ -92,10 +85,7 @@ public function preProcess() {
$this->_paymentProcessorObj = CRM_Financial_BAO_PaymentProcessor::getProcessorForEntity($this->_coid, 'contribute', 'obj');
}

if (
(!$this->_crid && !$this->_coid && !$this->_mid) ||
(!$this->getSubscriptionDetails())
) {
if (!$this->getSubscriptionDetails()) {
CRM_Core_Error::statusBounce(ts('Required information missing.'));
}

Expand All @@ -104,7 +94,7 @@ public function preProcess() {
// handle context redirection
CRM_Contribute_BAO_ContributionRecur::setSubscriptionContext();

$this->setTitle($this->_mid ? ts('Cancel Auto-renewal') : ts('Cancel Recurring Contribution'));
$this->setTitle($this->getMembershipID() ? ts('Cancel Auto-renewal') : ts('Cancel Recurring Contribution'));
$this->assign('mode', $this->_mode);

if ($this->isSelfService() || !$this->_paymentProcessorObj->supports('cancelRecurring')) {
Expand Down Expand Up @@ -160,7 +150,7 @@ public function buildQuickForm() {
if (!empty($this->_donorEmail)) {
$this->add('checkbox', 'is_notify', ts('Notify Contributor?') . " ({$this->_donorEmail})");
}
if ($this->_mid) {
if ($this->getMembershipID()) {
$cancelButton = ts('Cancel Automatic Membership Renewal');
}
else {
Expand Down Expand Up @@ -234,16 +224,16 @@ public function postProcess() {
try {
civicrm_api3('ContributionRecur', 'cancel', [
'id' => $this->getSubscriptionDetails()->recur_id,
'membership_id' => $this->_mid,
'membership_id' => $this->getMembershipID(),
'processor_message' => $message,
'cancel_reason' => $this->getSubmittedValue('cancel_reason'),
]);

$tplParams = [];
if ($this->_mid) {
$inputParams = ['id' => $this->_mid];
if ($this->getMembershipID()) {
$inputParams = ['id' => $this->getMembershipID()];
CRM_Member_BAO_Membership::getValues($inputParams, $tplParams);
$tplParams = $tplParams[$this->_mid];
$tplParams = $tplParams[$this->getMembershipID()];
$tplParams['membership_status']
= CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipStatus', $tplParams['status_id']);
$tplParams['membershipType']
Expand Down
55 changes: 40 additions & 15 deletions CRM/Contribute/Form/ContributionRecur.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* @package CRM
* @copyright CiviCRM LLC https://civicrm.org/licensing
*/
use Civi\Api4\Membership;

/**
* Shared parent class for recurring contribution forms.
Expand All @@ -39,7 +40,7 @@ class CRM_Contribute_Form_ContributionRecur extends CRM_Core_Form {
*
* @var int
*
* @internal
* @deprecated
*/
protected $_crid = NULL;

Expand All @@ -53,16 +54,16 @@ class CRM_Contribute_Form_ContributionRecur extends CRM_Core_Form {
*
* @internal
*/
protected $contributionRecurID = NULL;
protected int $contributionRecurID;

/**
* Membership ID.
*
* @var int
* @var int|null
*
* @internal
*/
protected $_mid;
protected ?int $_mid;

/**
* Payment processor object.
Expand Down Expand Up @@ -188,15 +189,17 @@ protected function getSubscriptionContactID() {
* @return int
*/
public function getContributionRecurID(): int {
$id = CRM_Utils_Request::retrieve('crid', 'Integer', $this, FALSE);
if (!$id && $this->getContributionID()) {
$id = $this->getContributionValue('contribution_recur_id');
}
if (!$id) {
$id = $this->getMembershipValue('contribution_recur_id');
if (!isset($this->contributionRecurID)) {
$id = CRM_Utils_Request::retrieve('crid', 'Integer', $this, FALSE);
if (!$id && $this->getContributionID()) {
$id = $this->getContributionValue('contribution_recur_id');
}
if (!$id) {
$id = $this->getMembershipValue('contribution_recur_id');
}
$this->contributionRecurID = $this->_crid = $id;
}
$this->contributionRecurID = $this->_crid = $id;
return (int) $id;
return (int) $this->contributionRecurID;
}

/**
Expand All @@ -216,15 +219,37 @@ public function getContributionID(): ?int {
/**
* Get the membership ID.
*
* @return int
* @throws \CRM_Core_Exception
*
* @api This function will not change in a minor release and is supported for
* use outside of core. This annotation / external support for properties
* is only given where there is specific test cover.
*
* @return int
*/
protected function getMembershipID(): ?int {
$this->_mid = CRM_Utils_Request::retrieve('mid', 'Integer', $this, FALSE);
return $this->_mid ? (int) $this->_mid : NULL;
$membershipID = CRM_Utils_Request::retrieve('mid', 'Integer', $this);
if (!isset($this->contributionRecurID)) {
// This is being called before the contribution recur ID is set - return quickly to avoid a loop.
return $membershipID ? (int) $membershipID : NULL;
}
if (!isset($this->_mid)) {
$this->_mid = NULL;
if (!$this->isDefined('Membership')) {
$membership = Membership::get(FALSE)
->addWhere('contribution_recur_id', '=', $this->getContributionRecurID())
->execute()->first();
if ($membershipID && (!$membership || ($membership['id'] !== $membershipID))) {
// this feels unreachable
throw new CRM_Core_Exception(ts('invalid membership ID'));
}
if ($membership) {
$this->define('Membership', 'Membership', $membership);
$this->_mid = $membership['id'];
}
}
}
return $this->_mid;
}

/**
Expand Down
10 changes: 5 additions & 5 deletions CRM/Contribute/Form/UpdateBilling.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,16 @@ public function preProcess() {
$this->_paymentProcessor['object'] = CRM_Financial_BAO_PaymentProcessor::getProcessorForEntity($this->_coid, 'contribute', 'obj');
}

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');
if ($this->getMembershipID()) {
$this->_paymentProcessor = CRM_Financial_BAO_PaymentProcessor::getProcessorForEntity($this->getMembershipID(), 'membership', 'info');
$this->_paymentProcessor['object'] = CRM_Financial_BAO_PaymentProcessor::getProcessorForEntity($this->getMembershipID(), 'membership', 'obj');
$membershipTypes = CRM_Member_PseudoConstant::membershipType();
$membershipTypeId = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_Membership', $this->_mid, 'membership_type_id');
$membershipTypeId = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_Membership', $this->getMembershipID(), 'membership_type_id');
$this->assign('membershipType', CRM_Utils_Array::value($membershipTypeId, $membershipTypes));
$this->_mode = 'auto_renew';
}

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

Expand Down
42 changes: 17 additions & 25 deletions CRM/Contribute/Form/UpdateSubscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
* made here could potentially affect the API etc. Be careful, be aware, use unit tests.
*/
class CRM_Contribute_Form_UpdateSubscription extends CRM_Contribute_Form_ContributionRecur {
use CRM_Custom_Form_CustomDataTrait;

public $_paymentProcessor = NULL;

Expand Down Expand Up @@ -62,23 +63,13 @@ public function preProcess() {
CRM_Core_Error::statusBounce(ts('Required information missing.'));
}

if ($this->getSubscriptionDetails()->membership_id && $this->getSubscriptionDetails()->auto_renew) {
// Add Membership details to form
$membership = civicrm_api3('Membership', 'get', [
'contribution_recur_id' => $this->contributionRecurID,
]);
if (!empty($membership['count'])) {
$membershipDetails = reset($membership['values']);
$values['membership_id'] = $membershipDetails['id'];
$values['membership_name'] = $membershipDetails['membership_name'];
}
$this->assign('recurMembership', $values);
$this->assign('contactId', $this->getSubscriptionContactID());
}
$this->assign('contactId', $this->getSubscriptionContactID());
$this->assign('membershipID', $this->getMembershipID());
$this->assign('membershipName', $this->getMembershipValue('name'));

$this->assign('self_service', $this->isSelfService());
$this->assign('recur_frequency_interval', $this->getSubscriptionDetails()->frequency_interval);
$this->assign('recur_frequency_unit', $this->getSubscriptionDetails()->frequency_unit);
$this->assign('recur_frequency_interval', $this->getContributionRecurValue('frequency_interval'));
$this->assign('recur_frequency_unit', $this->getContributionRecurValue('frequency_unit'));

$this->editableScheduleFields = $this->getPaymentProcessorObject()->getEditableRecurringScheduleFields();

Expand All @@ -97,11 +88,14 @@ public function preProcess() {
}
}

// when custom data is included in this page
if (!empty($_POST['hidden_custom']) && !$this->isSelfService()) {
CRM_Custom_Form_CustomData::preProcess($this, NULL, NULL, 1, 'ContributionRecur', $this->contributionRecurID);
CRM_Custom_Form_CustomData::buildQuickForm($this);
CRM_Custom_Form_CustomData::setDefaultValues($this);
if ($this->isSubmitted() && !$this->isSelfService()) {
// The custom data fields are added to the form by an ajax form.
// However, if they are not present in the element index they will
// not be available from `$this->getSubmittedValue()` in post process.
// We do not have to set defaults or otherwise render - just add to the element index.
$this->addCustomDataFieldsToForm('ContributionRecur', array_filter([
'id' => $this->getContributionRecurID(),
]));
}

$this->assign('editableScheduleFields', array_diff($this->editableScheduleFields, $alreadyHardCodedFields));
Expand Down Expand Up @@ -171,13 +165,11 @@ public function buildQuickForm() {
CRM_Campaign_BAO_Campaign::addCampaign($this, $this->getSubscriptionDetails()->campaign_id);
}

if (CRM_Contribute_BAO_ContributionRecur::supportsFinancialTypeChange($this->contributionRecurID)) {
if (CRM_Contribute_BAO_ContributionRecur::supportsFinancialTypeChange($this->getContributionRecurID())) {
$this->addEntityRef('financial_type_id', ts('Financial Type'), ['entity' => 'FinancialType'], !$this->isSelfService());
}

// Add custom data
$this->assign('customDataType', 'ContributionRecur');
$this->assign('entityID', $this->contributionRecurID);
$this->assign('contributionRecurID', $this->getContributionRecurID());

$type = 'next';
if ($this->isSelfService()) {
Expand Down Expand Up @@ -233,7 +225,7 @@ public function postProcess() {
}
if ($updateSubscription) {
// Handle custom data
$params['custom'] = CRM_Core_BAO_CustomField::postProcess($params, $this->contributionRecurID, 'ContributionRecur');
$params['custom'] = CRM_Core_BAO_CustomField::postProcess($this->getSubmittedValues(), $this->getContributionRecurID(), 'ContributionRecur');
// save the changes
CRM_Contribute_BAO_ContributionRecur::add($params);
$status = ts('Recurring contribution has been updated to: %1, every %2 %3(s) for %4 installments.',
Expand Down
8 changes: 4 additions & 4 deletions templates/CRM/Contribute/Form/UpdateSubscription.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
{if $changeHelpText}
<div class="help">
{$changeHelpText}
{if $recurMembership}
{if $membershipID}
<br/><strong> {ts}WARNING: This recurring contribution is linked to membership:{/ts}
<a class="crm-hover-button" href='{crmURL p="civicrm/contact/view/membership" q="action=view&reset=1&cid=`$contactId`&id=`$recurMembership.membership_id`&context=membership&selectedChild=member"}'>{$recurMembership.membership_name}</a>
<a class="crm-hover-button" href='{crmURL p="civicrm/contact/view/membership" q="action=view&reset=1&cid=`$contactId`&id=`$membershipID`&context=membership&selectedChild=member"}'>{$membershipName|escape}</a>
</strong>
{/if}
</div>
Expand All @@ -27,7 +27,7 @@
<table class="form-layout">
<tr>
<td class="label">{$form.amount.label}</td>
<td>{$form.currency.html|crmAddClass:eight}&nbsp;{$form.amount.html|crmAddClass:eight} ({ts}every{/ts} {$recur_frequency_interval} {$recur_frequency_unit})</td>
<td>{$form.currency.html|crmAddClass:eight}&nbsp;{$form.amount.html|crmAddClass:eight} ({ts}every{/ts} {$recur_frequency_interval|escape} {$recur_frequency_unit|escape})</td>
</tr>
{if array_key_exists('installments', $form)}
<tr>
Expand All @@ -48,7 +48,7 @@
</table>

{if !$self_service}
{include file="CRM/common/customDataBlock.tpl"}
{include file="CRM/common/customDataBlock.tpl" groupID='' customDataType='ContributionRecur' customDataSubType=false entityID=$contributionRecurID cid=false}
{/if}

<div class="crm-submit-buttons">{include file="CRM/common/formButtons.tpl" location="bottom"}</div>
Expand Down

0 comments on commit a2764a9

Please sign in to comment.