Commit a710ac41 authored by mattwire's avatar mattwire
Browse files

Update the way we cancel recur to match stripe etc and be compatible with more...

Update the way we cancel recur to match stripe etc and be compatible with more recent versions of CiviCRM
parent 2efc2756
......@@ -629,56 +629,54 @@ abstract class CRM_Core_Payment_AuthorizeNetCommon extends CRM_Core_Payment {
*
* @return bool
*/
/*protected function supportsCancelRecurringNotifyOptional() {
return FALSE;
}*/
protected function supportsCancelRecurringNotifyOptional() {
return TRUE;
}
/**
* Attempt to cancel the subscription at GoCardless.
*
* @param \Civi\Payment\PropertyBag $propertyBag
*
* @return array|null[]
* @throws \Civi\Payment\Exception\PaymentProcessorException
*/
public function doCancelRecurring(\Civi\Payment\PropertyBag $propertyBag) {
// By default we always notify Smartdebit and we don't give the user the option
// By default we always notify the processor and we don't give the user the option
// because supportsCancelRecurringNotifyOptional() = FALSE
if (!$propertyBag->has('isNotifyProcessorOnCancelRecur')) {
// @fixme setIsNotifyProcessorOnCancelRecur was added in 5.27 - remove method_exists once minVer is 5.27
// If isNotifyProcessorOnCancelRecur is NOT set then we set our default
if (method_exists($propertyBag, 'setIsNotifyProcessorOnCancelRecur')) {
// @fixme setIsNotifyProcessorOnCancelRecur was added in 5.27 - remove method_exists once minVer is 5.27
if (method_exists($propertyBag, 'setIsNotifyProcessorOnCancelRecur')) {
if (!$propertyBag->has('isNotifyProcessorOnCancelRecur')) {
// If isNotifyProcessorOnCancelRecur is NOT set then we set our default
$propertyBag->setIsNotifyProcessorOnCancelRecur(TRUE);
}
$notifyProcessor = $propertyBag->getIsNotifyProcessorOnCancelRecur();
}
else {
// CiviCRM < 5.27
$notifyProcessor = (boolean) CRM_Utils_Request::retrieveValue('send_cancel_request', 'Boolean', TRUE, FALSE, 'POST');
}
return parent::doCancelRecurring($propertyBag);
}
/**
* @param string $message
* @param array|\Civi\Payment\PropertyBag $params
*
* @return bool
* @throws \CiviCRM_API3_Exception
*/
protected function cancelSubscription(&$message = '', $params = []) {
/* @var \Civi\Payment\PropertyBag $paramsPb */
$paramsPb = \Civi\Payment\PropertyBag::cast($params);
// @todo From here on we are using the array instead of propertyBag. To be converted later...
$params = $this->getPropertyBagAsArray($paramsPb);
$contributionRecurId = $this->getRecurringContributionId($params);
try {
$contributionRecur = civicrm_api3('ContributionRecur', 'getsingle', [
'id' => $contributionRecurId,
]);
if (!$notifyProcessor) {
return ['message' => E::ts('Successfully cancelled the subscription in CiviCRM ONLY.')];
}
catch (Exception $e) {
return FALSE;
if (!$propertyBag->has('recurProcessorID')) {
$errorMessage = E::ts('The recurring contribution cannot be cancelled (No reference (trxn_id) found).');
\Civi::log()->error($errorMessage);
throw new \Civi\Payment\Exception\PaymentProcessorException($errorMessage);
}
$contributionRecur = civicrm_api3('ContributionRecur', 'getsingle', [
'id' => $propertyBag->getContributionRecurID(),
]);
if (empty($contributionRecur['trxn_id'])) {
CRM_Core_Session::setStatus(E::ts('The recurring contribution cannot be cancelled (No reference (trxn_id) found).'), 'Smart Debit', 'error');
return FALSE;
throw new \Civi\Payment\Exception\PaymentProcessorException(E::ts('The recurring contribution cannot be cancelled (No processor reference found).'));
}
// @todo From here on we are using the array instead of propertyBag. To be converted later...
$params = $this->getPropertyBagAsArray($propertyBag);
$merchantAuthentication = $this->getMerchantAuthentication($params);
$request = new AnetAPI\ARBCancelSubscriptionRequest();
......@@ -690,7 +688,7 @@ abstract class CRM_Core_Payment_AuthorizeNetCommon extends CRM_Core_Payment {
$response = $controller->executeWithApiResponse($this->getIsTestMode() ? AnetEnvironment::SANDBOX : AnetEnvironment::PRODUCTION);
if (!$response || !$response->getMessages()) {
$this->handleError(NULL, 'No response returned', $params['error_url']);
throw new \Civi\Payment\Exception\PaymentProcessorException($this->handleError(NULL, 'No response returned'));
}
if ($response->getMessages()->getResultCode() != "Ok") {
......@@ -704,9 +702,38 @@ abstract class CRM_Core_Payment_AuthorizeNetCommon extends CRM_Core_Payment {
$errorCode = NULL;
$errorMessage = NULL;
}
$this->handleError($errorCode, $errorMessage, $params['error_url']);
throw new \Civi\Payment\Exception\PaymentProcessorException($this->handleError($errorCode, $errorMessage));
}
return ['message' => E::ts('Successfully cancelled the subscription at Authorize.net.')];
}
/**
* Attempt to cancel the subscription.
* @deprecated Remove when min CiviCRM version is 5.25
*
* @see supportsCancelRecurring()
*
* @param string $message
* @param array|\Civi\Payment\PropertyBag $params
*
* @return bool
* @throws \CiviCRM_API3_Exception
* @throws \Civi\Payment\Exception\PaymentProcessorException
*/
public function cancelSubscription(&$message = '', $params = []) {
$propertyBag = \Civi\Payment\PropertyBag::cast($params);
if (!$propertyBag->has('recurProcessorID')) {
throw new PaymentProcessorException("cancelSubscription requires the recurProcessorID");
}
// contributionRecurID is set when doCancelRecurring is called directly (from 5.25)
if (!$propertyBag->has('contributionRecurID')) {
$contrib_recur = civicrm_api3('ContributionRecur', 'getsingle', ['processor_id' => $propertyBag->getRecurProcessorID()]);
$propertyBag->setContributionRecurID($contrib_recur['id']);
}
$message = $this->doCancelRecurring($propertyBag)['message'];
return TRUE;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment