get('twingle_prefix'); if (empty($prefix)) return; // check if protection is turned on $protection_on = Civi::settings()->get('twingle_protect_recurring'); if (empty($protection_on)) return; // load the recurring contribution $recurring_contribution = civicrm_api3('ContributionRecur', 'getsingle', [ 'return' => 'trxn_id,contribution_status_id,payment_instrument_id', 'id' => $recurring_contribution_id]); // check if this is a SEPA transaction if (self::isSDD($recurring_contribution['payment_instrument_id'])) return; // check if it's really a termination (i.e. current status is 2 or 5) if (!in_array($recurring_contribution['contribution_status_id'], [2,5])) return; // check if it's a Twingle contribution if (substr($recurring_contribution['trxn_id'], 0, strlen($prefix)) == $prefix) { // this is a Twingle contribution that is about to be terminated throw new Exception(E::ts("This is a Twingle recurring contribution. It should be terminated through the Twingle interface, otherwise it will still be collected.")); } } /** * Check if the given payment instrument is SEPA * * @param $payment_instrument_id string payment instrument * @return boolean */ public static function isSDD($payment_instrument_id) { static $sepa_payment_instruments = NULL; if ($sepa_payment_instruments === NULL) { // init with instrument names $sepa_payment_instruments = ['FRST', 'RCUR', 'OOFF']; // lookup and add instrument IDs $lookup = civicrm_api3('OptionValue', 'get', [ 'option_group_id' => 'payment_instrument', 'name' => ['IN' => $sepa_payment_instruments], 'return' => 'value' ]); foreach ($lookup['values'] as $payment_instrument) { $sepa_payment_instruments[] = $payment_instrument['value']; } } 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()->debug("CRM_Twingle_Tools::getMandate failed for [{$contribution_id}]: " . $ex->getMessage()); } } return NULL; } }