/** * _get_registration_payment_IDs * * generates an array of Payment IDs and their corresponding Registration IDs * * @access protected * @param EE_Payment[] $payments * @return array */ protected function _get_registration_payment_IDs($payments = array()) { $existing_reg_payments = array(); // get all reg payments for these payments $reg_payments = EEM_Registration_Payment::instance()->get_all(array(array('PAY_ID' => array('IN', array_keys($payments))))); if (!empty($reg_payments)) { foreach ($payments as $payment) { if (!$payment instanceof EE_Payment) { continue; } else { if (!isset($existing_reg_payments[$payment->ID()])) { $existing_reg_payments[$payment->ID()] = array(); } } foreach ($reg_payments as $reg_payment) { if ($reg_payment instanceof EE_Registration_Payment && $reg_payment->payment_ID() === $payment->ID()) { $existing_reg_payments[$payment->ID()][] = $reg_payment->registration_ID(); } } } } return $existing_reg_payments; }
/** * update registration REG_paid field after successful payment and link registration with payment * * @param EE_Registration $registration * @param EE_Payment $payment * @param float $payment_amount * @return float * @throws \EE_Error */ protected function _apply_registration_payment(EE_Registration $registration, EE_Payment $payment, $payment_amount = 0.0) { // find any existing reg payment records for this registration and payment $existing_reg_payment = EEM_Registration_Payment::instance()->get_one(array(array('REG_ID' => $registration->ID(), 'PAY_ID' => $payment->ID()))); // if existing registration payment exists if ($existing_reg_payment instanceof EE_Registration_Payment) { // then update that record $existing_reg_payment->set_amount($payment_amount); $existing_reg_payment->save(); } else { // or add new relation between registration and payment and set amount $registration->_add_relation_to($payment, 'Payment', array('RPY_amount' => $payment_amount)); // make it stick $registration->save(); } }
/** * delete_registration_payments_and_update_registrations * * removes all registration payment records associated with a payment * and subtracts their amounts from the corresponding registrations REG_paid field * * @param EE_Payment $payment * @param array $reg_payment_query_params * @return bool * @throws \EE_Error */ public function delete_registration_payments_and_update_registrations(EE_Payment $payment, $reg_payment_query_params = array()) { $save_payment = false; $reg_payment_query_params = !empty($reg_payment_query_params) ? $reg_payment_query_params : array(array('PAY_ID' => $payment->ID())); $registration_payments = EEM_Registration_Payment::instance()->get_all($reg_payment_query_params); if (!empty($registration_payments)) { foreach ($registration_payments as $registration_payment) { if ($registration_payment instanceof EE_Registration_Payment) { $amount_paid = $registration_payment->amount(); $registration = $registration_payment->registration(); if ($registration instanceof EE_Registration) { $registration->set_paid($registration->paid() - $amount_paid); if ($registration->save()) { if ($registration_payment->delete()) { $registration->_remove_relation_to($payment, 'Payment'); $payment->_remove_relation_to($registration, 'Registration'); } $save_payment = true; } } else { EE_Error::add_error(sprintf(__('An invalid Registration object was associated with Registration Payment ID# %1$d.', 'event_espresso'), $registration_payment->ID()), __FILE__, __FUNCTION__, __LINE__); return false; } } else { EE_Error::add_error(sprintf(__('An invalid Registration Payment object was associated with payment ID# %1$d.', 'event_espresso'), $payment->ID()), __FILE__, __FUNCTION__, __LINE__); return false; } } } if ($save_payment) { $payment->save(); } return true; }