'project_id', 'title' => 'Project ID', 'type' => CRM_Utils_Type::T_STRING, 'api.required' => 1, 'description' => 'The Twingle project ID.', ); $params['trx_id'] = array( 'name' => 'trx_id', 'title' => 'Transaction ID', 'type' => CRM_Utils_Type::T_STRING, 'api.required' => 1, 'description' => 'The unique transaction ID of the donation', ); $params['ended_at'] = array( 'name' => 'ended_at', 'title' => 'Ended at', 'type' => CRM_Utils_Type::T_INT, 'api.required' => 1, 'description' => 'The date when the recurring donation was ended, format: YYYYMMDD.', ); } /** * TwingleDonation.EndRecurring API * * @param array $params * @return array API result descriptor * @see civicrm_api3_create_success * @see civicrm_api3_create_error */ function civicrm_api3_twingle_donation_EndRecurring($params) { // Log call if debugging is enabled within civicrm.settings.php. if (defined('TWINGLE_API_LOGGING') && TWINGLE_API_LOGGING) { CRM_Core_Error::debug_log_message('TwingleDonation.EndRecurring: ' . json_encode($params, JSON_PRETTY_PRINT)); } try { // Validate date for parameter "ended_at". if (!DateTime::createFromFormat('Ymd', $params['ended_at'])) { throw new CiviCRM_API3_Exception( E::ts('Invalid date for parameter "ended_at".'), 'invalid_format' ); } $contribution = civicrm_api3('ContributionRecur', 'getsingle', array( 'trxn_id' => $params['trx_id'], )); // End SEPA mandate (which ends the associated recurring contribution) or // recurring contributions. if ( CRM_Twingle_Submission::civiSepaEnabled() && CRM_Sepa_Logic_Settings::isSDD($contribution) ) { $mandate_id = CRM_Sepa_Logic_Settings::getMandateFor($contribution['id']); if (!CRM_Sepa_BAO_SEPAMandate::terminateMandate($mandate_id, $params['ended_at'])) { throw new CiviCRM_API3_Exception( E::ts('Could not terminate SEPA mandate'), 'api_error' ); } $contribution = civicrm_api3('ContributionRecur', 'getsingle', array( 'id' => $contribution['id'], )); } else { $contribution = civicrm_api3('ContributionRecur', 'create', array( 'id' => $contribution['id'], 'end_date' => $params['ended_at'], 'contribution_status_id' => 'Completed', )); } $result = civicrm_api3_create_success($contribution); } catch (CiviCRM_API3_Exception $exception) { $result = civicrm_api3_create_error($exception->getMessage()); } return $result; }