/**
  * test_build_payment_json_response_for_deleted_payment
  * @since    4.8
  * @group    8620
  */
 public function test_build_payment_json_response_for_deleted_payment()
 {
     $this->_admin_page = new Transactions_Admin_Page_Mock();
     $this->_setup_standard_transaction_and_payment(40.0, 4, 15.0);
     // need to make sure relation is set between payment and payment method
     $this->_payment->_add_relation_to($this->_payment_method(), 'Payment_Method');
     $registrations = $this->_get_x_number_of_registrations_from_transaction($this->_transaction, 1);
     $this->_apply_payment_to_registrations($registrations);
     $json_response_data = $this->_admin_page->build_payment_json_response($this->_payment, array(), true);
     $this->assertEquals($this->_payment->ID(), $json_response_data['PAY_ID']);
     $this->assertEquals(15.0, $json_response_data['amount']);
     // total paid is still zero, because we haven't actually updated the TXN with the payment info
     $this->assertEquals(0, $json_response_data['total_paid']);
     $this->assertEquals(EEM_Transaction::incomplete_status_code, $json_response_data['txn_status']);
     $this->assertEquals(EEM_Payment::status_id_approved, $json_response_data['pay_status']);
     $this->assertTrue($json_response_data['delete_txn_reg_status_change']);
     // will validate $json_response_data[ 'registrations' ] in test_registration_payment_data_array()
 }
 /**
  * Gets the actual payment status in the database (useful for verifying a payment has actually been updated)
  * @global WPDB $wpdb
  * @param EE_Payment $payment
  * @return string
  */
 protected function _get_payment_status_in_db(EE_Payment $payment)
 {
     global $wpdb;
     $esp_payment = $payment->get_model()->table();
     return $wpdb->get_var($wpdb->prepare("SELECT STS_ID FROM {$esp_payment} WHERE PAY_ID = %d", $payment->ID()));
 }
 /**
  * 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;
 }
 /**
  * _build_payment_json_response
  *
  * @access public
  * @param \EE_Payment $payment
  * @param array       $REG_IDs
  * @param bool | null        $delete_txn_reg_status_change
  * @return array
  */
 protected function _build_payment_json_response(EE_Payment $payment, $REG_IDs = array(), $delete_txn_reg_status_change = null)
 {
     // was the payment deleted ?
     if (is_bool($delete_txn_reg_status_change)) {
         return array('PAY_ID' => $payment->ID(), 'amount' => $payment->amount(), 'total_paid' => $payment->transaction()->paid(), 'txn_status' => $payment->transaction()->status_ID(), 'pay_status' => $payment->STS_ID(), 'registrations' => $this->_registration_payment_data_array($REG_IDs), 'delete_txn_reg_status_change' => $delete_txn_reg_status_change);
     } else {
         $this->_get_payment_status_array();
         return array('amount' => $payment->amount(), 'total_paid' => $payment->transaction()->paid(), 'txn_status' => $payment->transaction()->status_ID(), 'pay_status' => $payment->STS_ID(), 'PAY_ID' => $payment->ID(), 'STS_ID' => $payment->STS_ID(), 'status' => self::$_pay_status[$payment->STS_ID()], 'date' => $payment->timestamp('Y-m-d', 'h:i a'), 'method' => strtoupper($payment->source()), 'PM_ID' => $payment->payment_method() ? $payment->payment_method()->ID() : 1, 'gateway' => $payment->payment_method() ? $payment->payment_method()->admin_name() : __("Unknown", 'event_espresso'), 'gateway_response' => $payment->gateway_response(), 'txn_id_chq_nmbr' => $payment->txn_id_chq_nmbr(), 'po_number' => $payment->po_number(), 'extra_accntng' => $payment->extra_accntng(), 'registrations' => $this->_registration_payment_data_array($REG_IDs));
     }
 }