From e8bb3a42cc754decf6bd292a9a06dff9d40cfba7 Mon Sep 17 00:00:00 2001 From: "B. Endres" Date: Mon, 25 Nov 2019 08:52:46 +0100 Subject: [PATCH] [#13] removed SEPA::getMandateFor dependency --- CRM/Twingle/Tools.php | 37 +++++++++++++++++++++++++ api/v3/TwingleDonation/Cancel.php | 30 +++++++++++++++----- api/v3/TwingleDonation/Endrecurring.php | 18 +++++++++--- 3 files changed, 74 insertions(+), 11 deletions(-) diff --git a/CRM/Twingle/Tools.php b/CRM/Twingle/Tools.php index 787d10c..08a596f 100644 --- a/CRM/Twingle/Tools.php +++ b/CRM/Twingle/Tools.php @@ -91,4 +91,41 @@ class CRM_Twingle_Tools { } return in_array($payment_instrument_id, $sepa_payment_instruments); } + + /** + * Get a CiviSEPA mandate for the given contribution ID + * + * @param $contribution_id integer contribution ID *or* recurring contribution ID + * @return integer mandate ID or null + */ + public static function getMandateFor($contribution_id) { + $contribution_id = (int) $contribution_id; + if ($contribution_id) { + try { + // try recurring mandate + $rcur_mandate = civicrm_api3('SepaMandate', 'get', [ + 'entity_id' => $contribution_id, + 'entity_table' => 'civicrm_contribution_recur', + 'type' => 'RCUR', + ]); + if ($rcur_mandate['count'] == 1) { + return reset($rcur_mandate['values']); + } + + // try OOFF mandate + // try recurring mandate + $ooff_mandate = civicrm_api3('SepaMandate', 'get', [ + 'entity_id' => $contribution_id, + 'entity_table' => 'civicrm_contribution', + 'type' => 'OOFF', + ]); + if ($ooff_mandate['count'] == 1) { + return reset($ooff_mandate['values']); + } + } catch (Exception $ex) { + Civi::log()->error("CRM_Twingle_Tools::getMandate failde for [{$contribution_id}]: " . $ex->getMessage()); + } + } + return NULL; + } } diff --git a/api/v3/TwingleDonation/Cancel.php b/api/v3/TwingleDonation/Cancel.php index bfd49e3..32a4e95 100644 --- a/api/v3/TwingleDonation/Cancel.php +++ b/api/v3/TwingleDonation/Cancel.php @@ -72,7 +72,7 @@ function civicrm_api3_twingle_donation_Cancel($params) { try { // Validate date for parameter "cancelled_at". - if (!DateTime::createFromFormat('YmdHis', $params['cancelled_at'])) { + if (!DateTime::createFromFormat('Ymd', $params['cancelled_at'])) { throw new CiviCRM_API3_Exception( E::ts('Invalid date for parameter "cancelled_at".'), 'invalid_format' @@ -94,17 +94,32 @@ function civicrm_api3_twingle_donation_Cancel($params) { $contribution_type = 'ContributionRecur'; } - // End SEPA mandate if applicable. if ( CRM_Twingle_Submission::civiSepaEnabled() && CRM_Twingle_Tools::isSDD($contribution['payment_instrument_id']) ) { - $mandate_id = CRM_Sepa_Logic_Settings::getMandateFor($contribution['id']); + // End SEPA mandate if applicable. + $mandate = CRM_Twingle_Tools::getMandateFor($contribution['id']); + if (!$mandate) { + throw new CiviCRM_API3_Exception( + E::ts("SEPA Mandate for contribution [%1 not found.", [1 => $contribution['id']]), + 'api_error' + ); + } + $mandate_id = (int) $mandate['id']; + // Mandates can not be terminated in the past. - $end_date = date('Ymd', max( - time(), - date_create_from_format('Ymd', $params['cancelled_at'])->getTimestamp() - )); + $end_date = date_create_from_format('Ymd', $params['cancelled_at']); + if ($end_date) { + // Mandates can not be terminated in the past: + $end_date = date('Ymd', max( + time(), + $end_date->getTimestamp())); + } else { + // end date couldn't be parsed, use 'now' + $end_date = date('Ymd'); + } + if (!CRM_Sepa_BAO_SEPAMandate::terminateMandate( $mandate_id, $end_date, @@ -122,6 +137,7 @@ function civicrm_api3_twingle_donation_Cancel($params) { )); } else { + // regular contribution CRM_Twingle_Tools::$protection_suspended = TRUE; $contribution = civicrm_api3($contribution_type, 'create', array( 'id' => $contribution['id'], diff --git a/api/v3/TwingleDonation/Endrecurring.php b/api/v3/TwingleDonation/Endrecurring.php index cdd0533..318b115 100644 --- a/api/v3/TwingleDonation/Endrecurring.php +++ b/api/v3/TwingleDonation/Endrecurring.php @@ -76,14 +76,24 @@ function civicrm_api3_twingle_donation_endrecurring($params) { $contribution = civicrm_api3('ContributionRecur', 'getsingle', array( 'trxn_id' => $default_profile->getTransactionID($params['trx_id']), )); + // End SEPA mandate (which ends the associated recurring contribution) or // recurring contributions. if ( CRM_Twingle_Submission::civiSepaEnabled() && CRM_Twingle_Tools::isSDD($contribution['payment_instrument_id']) ) { - $mandate_id = CRM_Sepa_Logic_Settings::getMandateFor($contribution['id']); - $end_date = date_create_from_format('Ymd', $params['ended_at']); + // END SEPA MANDATE + $mandate = CRM_Twingle_Tools::getMandateFor($contribution['id']); + if (!$mandate) { + throw new CiviCRM_API3_Exception( + E::ts("SEPA Mandate for recurring contribution [%1 not found.", [1 => $contribution['id']]), + 'api_error' + ); + } + + $mandate_id = $mandate['id']; + $end_date = date_create_from_format('YmdHis', $params['ended_at']); if ($end_date) { // Mandates can not be terminated in the past: $end_date = date('Ymd', max( @@ -95,8 +105,7 @@ function civicrm_api3_twingle_donation_endrecurring($params) { } // 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') { + if ($mandate['status'] != 'FRST' && $mandate['status'] != 'RCUR') { throw new CiviCRM_API3_Exception( E::ts("SEPA Mandate [%1] already terminated.", [1 => $mandate_id]), 'api_error' @@ -118,6 +127,7 @@ function civicrm_api3_twingle_donation_endrecurring($params) { )); } else { + // END RECURRING CONTRIBUTION CRM_Twingle_Tools::$protection_suspended = TRUE; $contribution = civicrm_api3('ContributionRecur', 'create', array( 'id' => $contribution['id'],