/**
  * Any messages triggers for after successful gateway payments should go in here.
  * @param  EE_Transaction object
  * @param  EE_Payment object
  * @return void
  */
 public static function payment(EE_Transaction $transaction, EE_Payment $payment)
 {
     self::_load_controller();
     $data = array($transaction, $payment);
     $message_type = self::_get_payment_message_type($payment->STS_ID());
     //if payment amount is less than 0 then switch to payment_refund message type.
     $message_type = $payment->amount() < 0 ? 'payment_refund' : $message_type;
     //verify this message type is present and active.  If it isn't then no message is sent.
     $active_mts = self::$_EEMSG->get_active_message_types();
     $message_type = in_array($message_type, $active_mts) ? $message_type : false;
     do_action('AHEE_log', __FILE__, __FUNCTION__, current_time('mysql'), 'delivered');
     do_action('AHEE_log', __FILE__, __FUNCTION__, $message_type, '$message_type');
     do_action('AHEE_log', __FILE__, __FUNCTION__, $transaction->status_ID(), '$transaction->status_ID()');
     do_action('AHEE_log', __FILE__, __FUNCTION__, $payment->status(), '$payment->status()');
     do_action('AHEE_log', __FILE__, __FUNCTION__, print_r($active_mts, true), '$active_mts');
     if ($message_type) {
         if (self::$_EEMSG->send_message($message_type, $data)) {
             //self::log(
             //	__CLASS__, __FUNCTION__, __LINE__,
             //	$transaction,
             //	array(
             //		'delivered' 			=>  current_time( 'mysql' ),
             //		'message_type' 	=> $message_type,
             //		'txn_status' 			=> $transaction->status_obj()->code( false, 'sentence' ),
             //		'pay_status' 		=> $payment->status_obj()->code( false, 'sentence' ),
             //	)
             //);
         }
     }
 }
 /**
  * update registrations REG_paid field after successful payment and link registrations with payment
  *
  * @param EE_Transaction $transaction
  * @param EE_Payment $payment
  * @param EE_Registration[] $registrations
  * @throws \EE_Error
  */
 public function process_registration_payments(EE_Transaction $transaction, EE_Payment $payment, $registrations = array())
 {
     // only process if payment was successful
     if ($payment->status() !== EEM_Payment::status_id_approved) {
         return;
     }
     //EEM_Registration::instance()->show_next_x_db_queries();
     if (empty($registrations)) {
         // find registrations with monies owing that can receive a payment
         $registrations = $transaction->registrations(array(array('STS_ID' => array('IN', EEM_Registration::reg_statuses_that_allow_payment()), 'REG_final_price' => array('!=', 0), 'REG_final_price*' => array('!=', 'REG_paid', true))));
     }
     // still nothing ??!??
     if (empty($registrations)) {
         return;
     }
     // todo: break out the following logic into a separate strategy class
     // todo: named something like "Sequential_Reg_Payment_Strategy"
     // todo: which would apply payments using the capitalist "first come first paid" approach
     // todo: then have another strategy class like "Distributed_Reg_Payment_Strategy"
     // todo: which would be the socialist "everybody gets a piece of pie" approach,
     // todo: which would be better for deposits, where you want a bit of the payment applied to each registration
     $refund = $payment->is_a_refund();
     // how much is available to apply to registrations?
     $available_payment_amount = abs($payment->amount());
     foreach ($registrations as $registration) {
         if ($registration instanceof EE_Registration) {
             // nothing left?
             if ($available_payment_amount <= 0) {
                 break;
             }
             if ($refund) {
                 $available_payment_amount = $this->process_registration_refund($registration, $payment, $available_payment_amount);
             } else {
                 $available_payment_amount = $this->process_registration_payment($registration, $payment, $available_payment_amount);
             }
         }
     }
     if ($available_payment_amount > 0 && apply_filters('FHEE__EE_Payment_Processor__process_registration_payments__display_notifications', false)) {
         EE_Error::add_attention(sprintf(__('A remainder of %1$s exists after applying this payment to Registration(s) %2$s.%3$sPlease verify that the original payment amount of %4$s is correct. If so, you should edit this payment and select at least one additional registration in the "Registrations to Apply Payment to" section, so that the remainder of this payment can be applied to the additional registration(s).', 'event_espresso'), EEH_Template::format_currency($available_payment_amount), implode(', ', array_keys($registrations)), '<br/>', EEH_Template::format_currency($payment->amount())), __FILE__, __FUNCTION__, __LINE__);
     }
 }
 /**
  * _process_cancelled_payments
  * just makes sure that the payment status gets updated correctly
  * so tha tan error isn't generated during payment validation
  *
  * @access private
  * @param EE_Payment $payment
  * @return EE_Payment | FALSE
  */
 private function _process_cancelled_payments($payment = NULL)
 {
     if (isset($_REQUEST['ee_cancel_payment']) && $payment instanceof EE_Payment && $payment->status() == EEM_Payment::status_id_failed) {
         $payment->set_status(EEM_Payment::status_id_cancelled);
     }
     return $payment;
 }
    /**
     * 	get_payment_row_html
     *
     *  @access 	public
     *  @param 	EE_Payment	$payment
     *  @return 	string
     */
    public function get_payment_row_html($payment = NULL)
    {
        $html = '';
        if ($payment instanceof EE_Payment) {
            if ($payment->payment_method() instanceof EE_Payment_Method && $payment->payment_method()->is_off_site() && $payment->status() === EEM_Payment::status_id_failed) {
                // considering the registrant has made it to the Thank You page,
                // any failed payments may actually be pending and the IPN is just slow
                // so let's
                $payment->set_status(EEM_Payment::status_id_pending);
            }
            $payment_declined_msg = $payment->STS_ID() === EEM_Payment::status_id_declined ? '<br /><span class="small-text">' . $payment->gateway_response() . '</span>' : '';
            $html .= '
				<tr>
					<td>
						' . $payment->timestamp() . '
					</td>
					<td>
						' . ($payment->payment_method() instanceof EE_Payment_Method ? $payment->payment_method()->name() : __('Unknown', 'event_espresso')) . '
					</td>
					<td class="jst-rght">
						' . EEH_Template::format_currency($payment->amount()) . '
					</td>
					<td class="jst-rght" style="line-height:1;">
						' . $payment->pretty_status(TRUE) . $payment_declined_msg . '
					</td>
				</tr>';
            do_action('AHEE__thank_you_page_payment_details_template__after_each_payment', $payment);
        }
        return $html;
    }
 /**
  * update registrations REG_paid field after successful payment and link registrations with payment
  *
  * @param EE_Transaction $transaction
  * @param EE_Payment $payment
  * @param EE_Registration[] $registrations
  * @throws \EE_Error
  */
 public function process_registration_payments(EE_Transaction $transaction, EE_Payment $payment, $registrations = array())
 {
     // only process if payment was successful
     if ($payment->status() !== EEM_Payment::status_id_approved) {
         return;
     }
     //EEM_Registration::instance()->show_next_x_db_queries();
     if (empty($registrations)) {
         // find registrations with monies owing that can receive a payment
         $registrations = $transaction->registrations(array(array('STS_ID' => array('IN', EEM_Registration::reg_statuses_that_allow_payment()), 'REG_final_price' => array('!=', 0), 'REG_final_price*' => array('!=', 'REG_paid', true))));
     }
     // still nothing ??!??
     if (empty($registrations)) {
         return;
     }
     // todo: break out the following logic into a separate strategy class
     // todo: named something like "Sequential_Reg_Payment_Strategy"
     // todo: which would apply payments using the capitalist "first come first paid" approach
     // todo: then have another strategy class like "Distributed_Reg_Payment_Strategy"
     // todo: which would be the socialist "everybody gets a piece of pie" approach,
     // todo: which would be better for deposits, where you want a bit of the payment applied to each registration
     $refund = $payment->is_a_refund();
     // how much is available to apply to registrations?
     $available_payment_amount = abs($payment->amount());
     //EEH_Debug_Tools::printr( $available_payment_amount, '$available_payment_amount', __FILE__, __LINE__ );
     foreach ($registrations as $registration) {
         if ($registration instanceof EE_Registration) {
             // nothing left?
             if ($available_payment_amount <= 0) {
                 break;
             }
             if ($refund) {
                 $available_payment_amount = $this->process_registration_refund($registration, $payment, $available_payment_amount);
             } else {
                 $available_payment_amount = $this->process_registration_payment($registration, $payment, $available_payment_amount);
             }
         }
     }
 }