diff --git a/api/v3/TwingleDonation/Endrecurring.php b/api/v3/TwingleDonation/Endrecurring.php index 005b6c9..83f20e0 100644 --- a/api/v3/TwingleDonation/Endrecurring.php +++ b/api/v3/TwingleDonation/Endrecurring.php @@ -87,6 +87,16 @@ function civicrm_api3_twingle_donation_endrecurring($params) { time(), date_create_from_format('Ymd', $params['cancelled_at'])->getTimestamp() )); + + // verify that the mandate has not been terminated in the past + $mandate_status = civicrm_api3('SepaMandate', 'getvalue', ['return' => 'status', 'id' => $mandate_id]); + if ($mandate_status != 'FRST' && $mandate_status != 'RCUR') { + throw new CiviCRM_API3_Exception( + E::ts("SEPA Mandate [%1] already terminated.", [1 => $mandate_id]), + 'api_error' + ); + } + if (!CRM_Sepa_BAO_SEPAMandate::terminateMandate( $mandate_id, $end_date,