$transaction->commit();
return TRUE;
}
- else {
- // if already cancelled, return true
- $recur->whereAdd();
- $recur->whereAdd("contribution_status_id = $cancelledId");
- if ($recur->find(TRUE)) {
- return TRUE;
- }
- }
-
- return FALSE;
}
/**
* @package CiviCRM_APIv3
*/
+use Civi\Api4\ContributionRecur;
+
/**
* Create or update a ContributionRecur.
*
* @throws \CRM_Core_Exception
*/
function civicrm_api3_contribution_recur_cancel(array $params): array {
- return CRM_Contribute_BAO_ContributionRecur::cancelRecurContribution($params) ? civicrm_api3_create_success() : civicrm_api3_create_error(ts('Error while cancelling recurring contribution'));
+ $existing = ContributionRecur::get(TRUE)
+ ->addWhere('id', '=', $params['id'])
+ ->addSelect('contribution_status_id:name')
+ ->execute()->first();
+ if (!$existing) {
+ throw new CRM_Core_Exception('record not found');
+ }
+ if ($existing['contribution_status_id:name'] === 'Cancelled') {
+ return civicrm_api3_create_success([$existing['id'] => $existing]);
+ }
+
+ CRM_Contribute_BAO_ContributionRecur::cancelRecurContribution($params);
+ return civicrm_api3_create_success();
}
/**