/** * This is the counterpart to the doDirectPayment method. This method creates * partial mandates, where the subsequent payment processess produces a payment. * * This function here should be called after the payment process was completed. * It will process all the PARTIAL mandates and connect them with created contributions. */ public static function processPartialMandates() { // load all the PARTIAL mandates $partial_mandates = civicrm_api3('SepaMandate', 'get', array('version' => 3, 'status' => 'PARTIAL', 'option.limit' => 9999)); foreach ($partial_mandates['values'] as $mandate_id => $mandate) { if ($mandate['type'] == 'OOFF') { // in the OOFF case, we need to find the contribution, and connect it $contribution = civicrm_api('Contribution', 'getsingle', array('version' => 3, 'trxn_id' => $mandate['reference'])); if (empty($contribution['is_error'])) { // check collection date $ooff_notice = (int) CRM_Sepa_Logic_Settings::getSetting("batching.OOFF.notice", $mandate['creditor_id']); $first_collection_date = strtotime("+{$ooff_notice} days"); $collection_date = strtotime($contribution['receive_date']); if ($collection_date < $first_collection_date) { // adjust collection date to the earliest possible one $collection_date = $first_collection_date; } // FOUND! Update the contribution... $contribution_bao = new CRM_Contribute_BAO_Contribution(); $contribution_bao->get('id', $contribution['id']); $contribution_bao->is_pay_later = 0; $contribution_bao->receive_date = date('YmdHis', $collection_date); $contribution_bao->contribution_status_id = (int) CRM_Core_OptionGroup::getValue('contribution_status', 'Pending', 'name'); $contribution_bao->payment_instrument_id = (int) CRM_Core_OptionGroup::getValue('payment_instrument', 'OOFF', 'name'); $contribution_bao->save(); // ...and connect it to the mandate $mandate_update = array(); $mandate_update['id'] = $mandate['id']; $mandate_update['entity_id'] = $contribution['id']; $mandate_update['type'] = $mandate['type']; if (empty($mandate['contact_id'])) { // this happens when the payment gets created AFTER the doDirectPayment method $mandate_update['contact_id'] = $contribution_bao->contact_id; } // initialize according to the creditor settings CRM_Sepa_BAO_SEPACreditor::initialiseMandateData($mandate['creditor_id'], $mandate_update); // finally, write the changes to the mandate civicrm_api3('SepaMandate', 'create', $mandate_update); } else { // if NOT FOUND or error, delete the partial mandate civicrm_api3('SepaMandate', 'delete', array('id' => $mandate_id)); } } elseif ($mandate['type'] == 'RCUR') { // in the RCUR case, we also need to find the contribution, and connect it // load the contribution AND the associated recurring contribution $contribution = civicrm_api('Contribution', 'getsingle', array('version' => 3, 'trxn_id' => $mandate['reference'])); $rcontribution = civicrm_api('ContributionRecur', 'getsingle', array('version' => 3, 'trxn_id' => $mandate['reference'])); if (empty($contribution['is_error']) && empty($rcontribution['is_error'])) { // we need to set the receive date to the correct collection date, otherwise it will be created again (w/o) $rcur_notice = (int) CRM_Sepa_Logic_Settings::getSetting("batching.RCUR.notice", $mandate['creditor_id']); $now = strtotime(date('Y-m-d', strtotime("now +{$rcur_notice} days"))); // round to full day $collection_date = CRM_Sepa_Logic_Batching::getNextExecutionDate($rcontribution, $now); // fix contribution $contribution_bao = new CRM_Contribute_BAO_Contribution(); $contribution_bao->get('id', $contribution['id']); $contribution_bao->is_pay_later = 0; $contribution_bao->contribution_status_id = (int) CRM_Core_OptionGroup::getValue('contribution_status', 'Pending', 'name'); $contribution_bao->payment_instrument_id = (int) CRM_Core_OptionGroup::getValue('payment_instrument', 'FRST', 'name'); $contribution_bao->receive_date = date('YmdHis', strtotime($collection_date)); $contribution_bao->save(); // fix recurring contribution $rcontribution_bao = new CRM_Contribute_BAO_ContributionRecur(); $rcontribution_bao->get('id', $rcontribution['id']); $rcontribution_bao->start_date = date('YmdHis', strtotime($rcontribution_bao->start_date)); $rcontribution_bao->create_date = date('YmdHis', strtotime($rcontribution_bao->create_date)); $rcontribution_bao->modified_date = date('YmdHis', strtotime($rcontribution_bao->modified_date)); $rcontribution_bao->contribution_status_id = (int) CRM_Core_OptionGroup::getValue('contribution_status', 'Pending', 'name'); $rcontribution_bao->payment_instrument_id = (int) CRM_Core_OptionGroup::getValue('payment_instrument', 'FRST', 'name'); $rcontribution_bao->save(); // ...and connect it to the mandate $mandate_update = array(); $mandate_update['id'] = $mandate['id']; $mandate_update['entity_id'] = $rcontribution['id']; $mandate_update['type'] = $mandate['type']; if (empty($mandate['contact_id'])) { $mandate_update['contact_id'] = $contribution['contact_id']; $mandate['contact_id'] = $contribution['contact_id']; } //NO: $mandate_update['first_contribution_id'] = $contribution['id']; // initialize according to the creditor settings CRM_Sepa_BAO_SEPACreditor::initialiseMandateData($mandate['creditor_id'], $mandate_update); // finally, write the changes to the mandate civicrm_api3('SepaMandate', 'create', $mandate_update); // ...and trigger notification // FIXME: WORKAROUND, see https://github.com/Project60/org.project60.sepa/issues/296) CRM_Contribute_BAO_ContributionPage::recurringNotify(CRM_Core_Payment::RECURRING_PAYMENT_START, $mandate['contact_id'], $contribution_bao->contribution_page_id, $rcontribution_bao); } else { // something went wrong, delete partial error_log("org.project60.sepa: deleting partial mandate " . $mandate['reference']); civicrm_api3('SepaMandate', 'delete', array('id' => $mandate_id)); } } } }
/** * getContract() returns the contribution or recurring contribution this mandate uses as a contract */ function getContract() { $etp = $this->entity_table; $eid = $this->entity_id; switch ($etp) { case 'civicrm_contribution_recur': $recur = new CRM_Contribute_BAO_ContributionRecur(); $recur->get('id', $eid); return $recur; break; case 'civicrm_contribution': $contr = new CRM_Contribute_BAO_Contribution(); $contr->get('id', $eid); return $contr; break; default: echo 'Huh ? ' . $etp; } return null; }