/**
  * Process payments and transaction after payment process completed.
  * ultimately this will send the TXN and payment details off so that notifications can be sent out.
  * if this request happens to be processing an IPN,
  * then we will also set the Payment Options Reg Step to completed,
  * and attempt to completely finalize the TXN if all of the other Reg Steps are completed as well.
  *
  * @param EE_Transaction $transaction
  * @param EE_Payment     $payment
  * @param bool           $IPN
  * @throws \EE_Error
  */
 protected function _post_payment_processing(EE_Transaction $transaction, EE_Payment $payment, $IPN = false)
 {
     /** @type EE_Transaction_Processor $transaction_processor */
     $transaction_processor = EE_Registry::instance()->load_class('Transaction_Processor');
     // is the Payment Options Reg Step completed ?
     $payment_options_step_completed = $transaction_processor->reg_step_completed($transaction, 'payment_options');
     // if the Payment Options Reg Step is completed...
     $revisit = $payment_options_step_completed === true ? true : false;
     // then this is kinda sorta a revisit with regards to payments at least
     $transaction_processor->set_revisit($revisit);
     // if this is an IPN, let's consider the Payment Options Reg Step completed if not already
     if ($IPN && $payment_options_step_completed !== true && ($payment->is_approved() || $payment->is_pending())) {
         $payment_options_step_completed = $transaction_processor->set_reg_step_completed($transaction, 'payment_options');
     }
     /** @type EE_Transaction_Payments $transaction_payments */
     $transaction_payments = EE_Registry::instance()->load_class('Transaction_Payments');
     // maybe update status, but don't save transaction just yet
     $transaction_payments->update_transaction_status_based_on_total_paid($transaction, false);
     // check if 'finalize_registration' step has been completed...
     $finalized = $transaction_processor->reg_step_completed($transaction, 'finalize_registration');
     //  if this is an IPN and the final step has not been initiated
     if ($IPN && $payment_options_step_completed && $finalized === false) {
         // and if it hasn't already been set as being started...
         $finalized = $transaction_processor->set_reg_step_initiated($transaction, 'finalize_registration');
     }
     $transaction->save();
     // because the above will return false if the final step was not fully completed, we need to check again...
     if ($IPN && $finalized !== false) {
         // and if we are all good to go, then send out notifications
         add_filter('FHEE__EED_Messages___maybe_registration__deliver_notifications', '__return_true');
         //ok, now process the transaction according to the payment
         $transaction_processor->update_transaction_and_registrations_after_checkout_or_payment($transaction, $payment);
     }
     // DEBUG LOG
     $payment_method = $payment->payment_method();
     if ($payment_method instanceof EE_Payment_Method) {
         $payment_method_type_obj = $payment_method->type_obj();
         if ($payment_method_type_obj instanceof EE_PMT_Base) {
             $gateway = $payment_method_type_obj->get_gateway();
             if ($gateway instanceof EE_Gateway) {
                 $gateway->log(array('message' => __('Post Payment Transaction Details', 'event_espresso'), 'transaction' => $transaction->model_field_array(), 'finalized' => $finalized, 'IPN' => $IPN, 'deliver_notifications' => has_filter('FHEE__EED_Messages___maybe_registration__deliver_notifications')), $payment);
             }
         }
     }
 }
 /**
  * Process payments and transaction after payment process completed.
  * ultimately this will send the TXN and payment details off so that notifications can be sent out.
  * if this request happens to be processing an IPN,
  * then we will also set the Payment Options Reg Step to completed,
  * and attempt to completely finalize the TXN if all of the other Reg Steps are completed as well.
  *
  * @param EE_Transaction $transaction
  * @param EE_Payment     $payment
  * @param bool           $IPN
  */
 protected function _post_payment_processing(EE_Transaction $transaction, EE_Payment $payment, $IPN = false)
 {
     /** @type EE_Transaction_Processor $transaction_processor */
     $transaction_processor = EE_Registry::instance()->load_class('Transaction_Processor');
     // is the Payment Options Reg Step completed ?
     $payment_options_step_completed = $transaction_processor->reg_step_completed($transaction, 'payment_options');
     // DEBUG LOG
     //$this->log(
     //	__CLASS__, __FUNCTION__, __LINE__,
     //	$transaction,
     //	array(
     //		'IPN'             => $IPN,
     //		'payment_options' => $payment_options_step_completed,
     //	)
     //);
     // if the Payment Options Reg Step is completed...
     $revisit = $payment_options_step_completed === true ? true : false;
     // then this is kinda sorta a revisit with regards to payments at least
     $transaction_processor->set_revisit($revisit);
     // if this is an IPN, let's consider the Payment Options Reg Step completed if not already
     if ($IPN && $payment_options_step_completed !== true && ($payment->is_approved() || $payment->is_pending())) {
         $payment_options_step_completed = $transaction_processor->set_reg_step_completed($transaction, 'payment_options');
     }
     // DEBUG LOG
     //$this->log(
     //	__CLASS__, __FUNCTION__, __LINE__,
     //	$transaction,
     //	array(
     //		'IPN'             => $IPN,
     //		'payment_options' => $payment_options_step_completed,
     //	)
     //);
     /** @type EE_Transaction_Payments $transaction_payments */
     $transaction_payments = EE_Registry::instance()->load_class('Transaction_Payments');
     // maybe update status, but don't save transaction just yet
     $transaction_payments->update_transaction_status_based_on_total_paid($transaction, false);
     // check if 'finalize_registration' step has been completed...
     $finalized = $transaction_processor->reg_step_completed($transaction, 'finalize_registration');
     // DEBUG LOG
     //$this->log(
     //	__CLASS__, __FUNCTION__, __LINE__,
     //	$transaction,
     //	array(
     //		'IPN'       => $IPN,
     //		'finalized' => $finalized,
     //	)
     //);
     //  if this is an IPN and the final step has not been initiated
     if ($IPN && $payment_options_step_completed && $finalized === false) {
         // and if it hasn't already been set as being started...
         $finalized = $transaction_processor->set_reg_step_initiated($transaction, 'finalize_registration');
         // DEBUG LOG
         //$this->log(
         //	__CLASS__, __FUNCTION__, __LINE__,
         //	$transaction,
         //	array(
         //		'IPN'                   => $IPN,
         //		'finalized'             => $finalized,
         //	)
         //);
     }
     $transaction->save();
     // because the above will return false if the final step was not fully completed, we need to check again...
     if ($IPN && $finalized !== false) {
         // and if we are all good to go, then send out notifications
         add_filter('FHEE__EED_Messages___maybe_registration__deliver_notifications', '__return_true');
         // DEBUG LOG
         //$this->log( __CLASS__, __FUNCTION__, __LINE__, $transaction );
         //ok, now process the transaction according to the payment
         $transaction_processor->update_transaction_and_registrations_after_checkout_or_payment($transaction, $payment);
     }
     // DEBUG LOG
     //$this->log(
     //	__CLASS__, __FUNCTION__, __LINE__,
     //	$transaction,
     //	array(
     //		'IPN'  => $IPN,
     //		'finalized' => $finalized,
     //		'payment' => $payment,
     //		'payment_method' => $payment->payment_method() instanceof EE_Payment_Method ? $payment->payment_method
     //()->name() : 'off-line',
     //		'deliver_notifications' => has_filter( 'FHEE__EED_Messages___maybe_registration__deliver_notifications' ),
     //	)
     //);
 }