/**
  *
  * @param EE_Transaction       $transaction
  * @param float                $amount
  * @param EE_Billing_Info_Form $billing_info
  * @param string               $return_url
  * @param string                 $fail_url
  * @param string               $method
  * @param bool           $by_admin
  * @return EE_Payment
  * @throws EE_Error
  */
 function process_payment(EE_Transaction $transaction, $amount = null, $billing_info = null, $return_url = null, $fail_url = '', $method = 'CART', $by_admin = false)
 {
     // @todo: add surcharge for the payment method, if any
     if ($this->_gateway) {
         //there is a gateway, so we're going to make a payment object
         //but wait! do they already have a payment in progress that we thought was failed?
         $duplicate_properties = array('STS_ID' => EEM_Payment::status_id_failed, 'TXN_ID' => $transaction->ID(), 'PMD_ID' => $this->_pm_instance->ID(), 'PAY_source' => $method, 'PAY_amount' => $amount !== null ? $amount : $transaction->remaining(), 'PAY_gateway_response' => null);
         $payment = EEM_Payment::instance()->get_one(array($duplicate_properties));
         //if we didn't already have a payment in progress for the same thing,
         //then we actually want to make a new payment
         if (!$payment instanceof EE_Payment) {
             $payment = EE_Payment::new_instance(array_merge($duplicate_properties, array('PAY_timestamp' => time(), 'PAY_txn_id_chq_nmbr' => null, 'PAY_po_number' => null, 'PAY_extra_accntng' => null, 'PAY_details' => null)));
         }
         //make sure the payment has been saved to show we started it, and so it has an ID should the gateway try to log it
         $payment->save();
         $billing_values = $this->_get_billing_values_from_form($billing_info);
         //  Offsite Gateway
         if ($this->_gateway instanceof EE_Offsite_Gateway) {
             $payment = $this->_gateway->set_redirection_info($payment, $billing_values, $return_url, EE_Config::instance()->core->txn_page_url(array('e_reg_url_link' => $transaction->primary_registration()->reg_url_link(), 'ee_payment_method' => $this->_pm_instance->slug())), $fail_url);
             $payment->save();
             //  Onsite Gateway
         } elseif ($this->_gateway instanceof EE_Onsite_Gateway) {
             $payment = $this->_gateway->do_direct_payment($payment, $billing_values);
             $payment->save();
         } else {
             throw new EE_Error(sprintf(__('Gateway for payment method type "%s" is "%s", not a subclass of either EE_Offsite_Gateway or EE_Onsite_Gateway, or null (to indicate NO gateway)', 'event_espresso'), get_class($this), gettype($this->_gateway)));
         }
     } else {
         // no gateway provided
         // there is no payment. Must be an offline gateway
         // create a payment object anyways, but dont save it
         $payment = EE_Payment::new_instance(array('STS_ID' => EEM_Payment::status_id_pending, 'TXN_ID' => $transaction->ID(), 'PMD_ID' => $transaction->payment_method_ID(), 'PAY_amount' => 0.0, 'PAY_timestamp' => time()));
     }
     // if there is billing info, clean it and save it now
     if ($billing_info instanceof EE_Billing_Attendee_Info_Form) {
         $this->_save_billing_info_to_attendee($billing_info, $transaction);
     }
     return $payment;
 }
 /**
  * _activate_payment_method_button
  *
  * @access protected
  * @param \EE_Payment_Method $payment_method
  * @return \EE_Form_Section_Proper
  */
 protected function _activate_payment_method_button(EE_Payment_Method $payment_method)
 {
     $link_text_and_title = sprintf(__('Activate %1$s Payment Method?', 'event_espresso'), $payment_method->admin_name());
     return new EE_Form_Section_Proper(array('name' => 'activate_' . $payment_method->slug() . '_settings_form', 'html_id' => 'activate_' . $payment_method->slug() . '_settings_form', 'action' => '#', 'layout_strategy' => new EE_Admin_Two_Column_Layout(), 'subsections' => apply_filters('FHEE__Payments_Admin_Page___activate_payment_method_button__form_subsections', array(new EE_Form_Section_HTML(EEH_HTML::tr(EEH_HTML::th(EEH_HTML::label(__('Click to Activate ', 'event_espresso'))) . EEH_HTML::td(EEH_HTML::link(EE_Admin_Page::add_query_args_and_nonce(array('action' => 'activate_payment_method', 'payment_method_type' => $payment_method->type()), EE_PAYMENTS_ADMIN_URL), $link_text_and_title, $link_text_and_title, 'activate_' . $payment_method->slug(), 'espresso-button-green button-primary'))))), $payment_method)));
 }
 /**
  *    _payment_method_billing_info
  *
  * @access 	private
  * @param 	EE_Payment_Method $payment_method
  * @return 	\EE_Form_Section_Proper
  */
 private function _payment_method_billing_info(EE_Payment_Method $payment_method)
 {
     $currently_selected = $this->checkout->selected_method_of_payment == $payment_method->slug() ? TRUE : FALSE;
     // generate the billing form for payment method
     $billing_form = $currently_selected ? $this->_get_billing_form_for_payment_method($payment_method) : new EE_Form_Section_HTML();
     $this->checkout->billing_form = $currently_selected ? $billing_form : $this->checkout->billing_form;
     // it's all in the details
     $info_html = EEH_HTML::h3(__('Important information regarding your payment', 'event_espresso'), '', 'spco-payment-method-hdr');
     // add some info regarding the step, either from what's saved in the admin, or a default string depending on whether the PM has a billing form or not
     if ($payment_method->description()) {
         $payment_method_info = $payment_method->description();
     } elseif ($billing_form instanceof EE_Billing_Info_Form) {
         $payment_method_info = sprintf(__('Please provide the following billing information, then click the "%1$s" button below in order to proceed.', 'event_espresso'), $this->submit_button_text());
     } else {
         $payment_method_info = sprintf(__('Please click the "%1$s" button below in order to proceed.', 'event_espresso'), $this->submit_button_text());
     }
     $info_html .= EEH_HTML::p(apply_filters('FHEE__EE_SPCO_Reg_Step_Payment_Options___payment_method_billing_info__payment_method_info', $payment_method_info), '', 'spco-payment-method-desc ee-attention');
     return new EE_Form_Section_Proper(array('html_id' => 'spco-payment-method-info-' . $payment_method->slug(), 'html_class' => 'spco-payment-method-info-dv', 'html_style' => $currently_selected ? '' : 'display:none;', 'layout_strategy' => new EE_Div_Per_Section_Layout(), 'subsections' => array('info' => new EE_Form_Section_HTML($info_html), 'billing_form' => $currently_selected ? $billing_form : new EE_Form_Section_HTML())));
 }