Adjust SEPA mandate data.

This commit is contained in:
Jens Schuppe 2018-10-16 10:55:30 +02:00
parent d938c0332a
commit 021a0a1e8f
2 changed files with 49 additions and 14 deletions

View file

@ -284,8 +284,8 @@ class CRM_Twingle_Submission {
'frequency_interval' => 3,
),
'yearly' => array(
'frequency_unit' => 'year',
'frequency_interval' => 1,
'frequency_unit' => 'month',
'frequency_interval' => 12,
),
'monthly' => array(
'frequency_unit' => 'month',
@ -297,4 +297,32 @@ class CRM_Twingle_Submission {
return $mapping[$donation_rhythm];
}
/**
* Retrieves the next possible cycle day for a SEPA mandate from a given start
* date of the mandate, depending on CiviSEPA creditor configuration.
*
* @param string $start_date
* A string representing a date in the format "Ymd".
*
* @param int $creditor_id
* The ID of the CiviSEPA creditor to use for determining the cycle day.
*
* @return int
* The next possible day of this or the next month to start collecting.
*/
public static function getSEPACycleDay($start_date, $creditor_id) {
$buffer_days = (int) CRM_Sepa_Logic_Settings::getSetting("pp_buffer_days");
$frst_notice_days = (int) CRM_Sepa_Logic_Settings::getSetting("batching.FRST.notice", $creditor_id);
$earliest_rcur_date = strtotime("$start_date + $frst_notice_days days + $buffer_days days");
// Find the next cycle day
$cycle_days = CRM_Sepa_Logic_Settings::getListSetting("cycledays", range(1, 28), $creditor_id);
$earliest_cycle_day = $earliest_rcur_date;
while (!in_array(date('j', $earliest_cycle_day), $cycle_days)) {
$earliest_cycle_day = strtotime("+ 1 day", $earliest_cycle_day);
}
return date('j', $earliest_cycle_day);
}
}

View file

@ -465,29 +465,36 @@ function civicrm_api3_twingle_donation_Submit($params) {
}
}
$creditor_id = $profile->getAttribute('sepa_creditor_id');
// Compose mandate data from contribution data, ...
$mandate_data =
$contribution_data
// Add CiviSEPA mandate attributes.
// ... CiviSEPA mandate attributes, ...
+ array(
'type' => ($params['donation_rhythm'] == 'one_time' ? 'OOFF' : 'RCUR'),
'iban' => $params['debit_iban'],
'bic' => $params['debit_bic'],
'reference' => $params['debit_mandate_reference'],
'date' => $params['confirmed_at'],
'creditor_id' => $profile->getAttribute('sepa_creditor_id'),
'date' => $params['confirmed_at'], // Signature date
'start_date' => $params['confirmed_at'], // Earliest collection date.
'creditor_id' => $creditor_id,
)
// Add frequency unit and interval from static mapping.
// ... and frequency unit and interval from a static mapping.
+ CRM_Twingle_Submission::getFrequencyMapping($params['donation_rhythm']);
// Let CiviSEPA set the correct payment instrument.
unset($mandate_data['payment_instrument_id']);
$mandate = civicrm_api3('SepaMandate', 'createfull', $mandate_data);
if ($mandate['is_error']) {
throw new CiviCRM_API3_Exception(
E::ts('Could not create SEPA mandate'),
'api_error'
);
// Add cycle day for recurring contributions.
if ($params['donation_rhythm'] != 'one_time') {
$mandate_data['cycle_day'] = CRM_Twingle_Submission::getSEPACycleDay($params['confirmed_at'], $creditor_id);
}
// Let CiviSEPA set the correct payment instrument depending on the
// mandate type.
unset($mandate_data['payment_instrument_id']);
// Create the mandate.
$mandate = civicrm_api3('SepaMandate', 'createfull', $mandate_data);
$result_values = $mandate['values'];
}
else {