From 021a0a1e8f08dfcf3b707e26749eda1dfcde38c8 Mon Sep 17 00:00:00 2001 From: Jens Schuppe Date: Tue, 16 Oct 2018 10:55:30 +0200 Subject: [PATCH] Adjust SEPA mandate data. --- CRM/Twingle/Submission.php | 32 +++++++++++++++++++++++++++++-- api/v3/TwingleDonation/Submit.php | 31 ++++++++++++++++++------------ 2 files changed, 49 insertions(+), 14 deletions(-) diff --git a/CRM/Twingle/Submission.php b/CRM/Twingle/Submission.php index b742533..86cb660 100644 --- a/CRM/Twingle/Submission.php +++ b/CRM/Twingle/Submission.php @@ -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); + } + } diff --git a/api/v3/TwingleDonation/Submit.php b/api/v3/TwingleDonation/Submit.php index 03a1cd5..3a44c73 100644 --- a/api/v3/TwingleDonation/Submit.php +++ b/api/v3/TwingleDonation/Submit.php @@ -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 {