[#13] SEPA Code depedency (merged dev_13)

This commit is contained in:
B. Endres 2020-01-30 07:13:53 +01:00
commit d6b90dcec2
5 changed files with 90 additions and 35 deletions

View file

@ -627,20 +627,7 @@ class CRM_Twingle_Form_Profile extends CRM_Core_Form {
// enabled. // enabled.
if ( if (
CRM_Twingle_Submission::civiSepaEnabled() CRM_Twingle_Submission::civiSepaEnabled()
&& ( && CRM_Twingle_Tools::isSDD($payment_instrument['value'])
(
method_exists('CRM_Sepa_Logic_PaymentInstruments', 'isSDD')
&& CRM_Sepa_Logic_PaymentInstruments::isSDD(array(
'payment_instrument_id' => $payment_instrument['value'],
))
)
|| (
method_exists('CRM_Sepa_Logic_Settings', 'isSDD')
&& CRM_Sepa_Logic_Settings::isSDD(array(
'payment_instrument_id' => $payment_instrument['value'],
))
)
)
) { ) {
if (!isset(self::$_paymentInstruments['sepa'])) { if (!isset(self::$_paymentInstruments['sepa'])) {
self::$_paymentInstruments['sepa'] = E::ts('CiviSEPA'); self::$_paymentInstruments['sepa'] = E::ts('CiviSEPA');

View file

@ -55,7 +55,7 @@ class CRM_Twingle_Tools {
'id' => $recurring_contribution_id]); 'id' => $recurring_contribution_id]);
// check if this is a SEPA transaction // check if this is a SEPA transaction
//if (self::isSDD($recurring_contribution['payment_instrument_id'])) return; if (self::isSDD($recurring_contribution['payment_instrument_id'])) return;
// check if it's really a termination (i.e. current status is 2 or 5) // 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; if (!in_array($recurring_contribution['contribution_status_id'], [2,5])) return;
@ -91,4 +91,41 @@ class CRM_Twingle_Tools {
} }
return in_array($payment_instrument_id, $sepa_payment_instruments); 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;
}
} }

View file

@ -94,17 +94,32 @@ function civicrm_api3_twingle_donation_Cancel($params) {
$contribution_type = 'ContributionRecur'; $contribution_type = 'ContributionRecur';
} }
// End SEPA mandate if applicable.
if ( if (
CRM_Twingle_Submission::civiSepaEnabled() CRM_Twingle_Submission::civiSepaEnabled()
&& CRM_Sepa_Logic_Settings::isSDD($contribution) && 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. // Mandates can not be terminated in the past.
$end_date = date_create_from_format('YmdHis', $params['cancelled_at']);
if ($end_date) {
// Mandates can not be terminated in the past:
$end_date = date('Ymd', max( $end_date = date('Ymd', max(
time(), time(),
date_create_from_format('Ymd', $params['cancelled_at'])->getTimestamp() $end_date->getTimestamp()));
)); } else {
// end date couldn't be parsed, use 'now'
$end_date = date('Ymd');
}
if (!CRM_Sepa_BAO_SEPAMandate::terminateMandate( if (!CRM_Sepa_BAO_SEPAMandate::terminateMandate(
$mandate_id, $mandate_id,
$end_date, $end_date,
@ -122,6 +137,7 @@ function civicrm_api3_twingle_donation_Cancel($params) {
)); ));
} }
else { else {
// regular contribution
CRM_Twingle_Tools::$protection_suspended = TRUE; CRM_Twingle_Tools::$protection_suspended = TRUE;
$contribution = civicrm_api3($contribution_type, 'create', array( $contribution = civicrm_api3($contribution_type, 'create', array(
'id' => $contribution['id'], 'id' => $contribution['id'],

View file

@ -76,22 +76,36 @@ function civicrm_api3_twingle_donation_endrecurring($params) {
$contribution = civicrm_api3('ContributionRecur', 'getsingle', array( $contribution = civicrm_api3('ContributionRecur', 'getsingle', array(
'trxn_id' => $default_profile->getTransactionID($params['trx_id']), 'trxn_id' => $default_profile->getTransactionID($params['trx_id']),
)); ));
// End SEPA mandate (which ends the associated recurring contribution) or // End SEPA mandate (which ends the associated recurring contribution) or
// recurring contributions. // recurring contributions.
if ( if (
CRM_Twingle_Submission::civiSepaEnabled() CRM_Twingle_Submission::civiSepaEnabled()
&& CRM_Sepa_Logic_Settings::isSDD($contribution) && CRM_Twingle_Tools::isSDD($contribution['payment_instrument_id'])
) { ) {
$mandate_id = CRM_Sepa_Logic_Settings::getMandateFor($contribution['id']); // END SEPA MANDATE
// Mandates can not be terminated in the past. $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( $end_date = date('Ymd', max(
time(), time(),
date_create_from_format('Ymd', $params['cancelled_at'])->getTimestamp() $end_date->getTimestamp()));
)); } else {
// end date couldn't be parsed, use 'now'
$end_date = date('Ymd');
}
// verify that the mandate has not been terminated in the past // 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( throw new CiviCRM_API3_Exception(
E::ts("SEPA Mandate [%1] already terminated.", [1 => $mandate_id]), E::ts("SEPA Mandate [%1] already terminated.", [1 => $mandate_id]),
'api_error' 'api_error'
@ -113,6 +127,7 @@ function civicrm_api3_twingle_donation_endrecurring($params) {
)); ));
} }
else { else {
// END RECURRING CONTRIBUTION
CRM_Twingle_Tools::$protection_suspended = TRUE; CRM_Twingle_Tools::$protection_suspended = TRUE;
$contribution = civicrm_api3('ContributionRecur', 'create', array( $contribution = civicrm_api3('ContributionRecur', 'create', array(
'id' => $contribution['id'], 'id' => $contribution['id'],

View file

@ -163,7 +163,7 @@ function _civicrm_api3_twingle_donation_Submit_spec(&$params) {
); );
$params['user_email'] = array( $params['user_email'] = array(
'name' => 'user_email', 'name' => 'user_email',
'title' => E::ts('E-mail address'), 'title' => E::ts('Email address'),
'type' => CRM_Utils_Type::T_STRING, 'type' => CRM_Utils_Type::T_STRING,
'api.required' => 0, 'api.required' => 0,
'description' => E::ts('The e-mail address of the contact.'), 'description' => E::ts('The e-mail address of the contact.'),