/**
  * Performs a credit card transaction for the given order and returns the
  * result
  *
  * @since 1.0.0
  * @param WC_Order $order the order object
  * @param SV_WC_Payment_Gateway_API_Response $response optional credit card transaction response
  * @return SV_WC_Payment_Gateway_API_Response the response
  * @throws SV_WC_Payment_Gateway_Exception network timeouts, etc
  */
 protected function do_credit_card_transaction($order, $response = null)
 {
     if (is_null($response)) {
         if ($this->perform_credit_card_charge()) {
             $response = $this->get_api()->credit_card_charge($order);
         } else {
             $response = $this->get_api()->credit_card_authorization($order);
         }
     }
     // success! update order record
     if ($response->transaction_approved()) {
         $last_four = substr($order->payment->account_number, -4);
         // use direct card type if set, or try to guess it from card number
         if (!empty($order->payment->card_type)) {
             $card_type = $order->payment->card_type;
         } elseif ($first_four = substr($order->payment->account_number, 0, 4)) {
             $card_type = SV_WC_Payment_Gateway_Helper::card_type_from_account_number($first_four);
         } else {
             $card_type = 'card';
         }
         // credit card order note
         $message = sprintf(_x('%s %s %s Approved: %s ending in %s (expires %s)', 'Supports direct credit card', $this->text_domain), $this->get_method_title(), $this->is_test_environment() ? _x('Test', 'Supports direct credit card', $this->text_domain) : '', $this->perform_credit_card_authorization() ? 'Authorization' : 'Charge', SV_WC_Payment_Gateway_Helper::payment_type_to_name($card_type), $last_four, $order->payment->exp_month . '/' . substr($order->payment->exp_year, -2));
         // adds the transaction id (if any) to the order note
         if ($response->get_transaction_id()) {
             $message .= ' ' . sprintf(_x('(Transaction ID %s)', 'Supports direct credit card', $this->text_domain), $response->get_transaction_id());
         }
         $message = apply_filters('wc_payment_gateway_' . $this->get_id() . '_credit_card_transaction_approved_order_note', $message, $order, $response, $this);
         $order->add_order_note($message);
     }
     return $response;
 }
 /**
  * Add payment-specific info to the data array used to build the token given
  * an order. This is used when creating a customer profile or payment profile,
  * as the response from Authorize.Net does not contain some useful information
  * (like the card/account type) that improves the token display
  *
  * @since 2.0.0
  * @param \WC_Order $order
  * @return array
  */
 protected function parse_data_from_order(WC_Order $order)
 {
     // defaults for both credit cards/eChecks
     $data = array('default' => true, 'type' => $order->payment->type, 'last_four' => $order->payment->last_four, 'customer_profile_id' => $order->customer_id);
     if ('credit_card' === $data['type']) {
         $data['card_type'] = isset($order->payment->card_type) ? $order->payment->card_type : SV_WC_Payment_Gateway_Helper::card_type_from_account_number($order->payment->account_number);
         $data['exp_month'] = $order->payment->exp_month;
         $data['exp_year'] = $order->payment->exp_year;
     } elseif ('echeck' === $data['type']) {
         $data['account_type'] = $order->payment->account_type;
     }
     return $data;
 }
 /**
  * Performs a credit card transaction for the given order and returns the
  * result
  *
  * @since 1.0.0
  * @param WC_Order $order the order object
  * @param SV_WC_Payment_Gateway_API_Response $response optional credit card transaction response
  * @return SV_WC_Payment_Gateway_API_Response the response
  * @throws SV_WC_Payment_Gateway_Exception network timeouts, etc
  */
 protected function do_credit_card_transaction($order, $response = null)
 {
     if (is_null($response)) {
         if ($this->perform_credit_card_charge()) {
             $response = $this->get_api()->credit_card_charge($order);
         } else {
             $response = $this->get_api()->credit_card_authorization($order);
         }
     }
     // success! update order record
     if ($response->transaction_approved()) {
         $last_four = substr($order->payment->account_number, -4);
         // use direct card type if set, or try to guess it from card number
         if (!empty($order->payment->card_type)) {
             $card_type = $order->payment->card_type;
         } elseif ($first_four = substr($order->payment->account_number, 0, 4)) {
             $card_type = SV_WC_Payment_Gateway_Helper::card_type_from_account_number($first_four);
         } else {
             $card_type = 'card';
         }
         // credit card order note
         $message = sprintf(esc_html__('%1$s %2$s %3$s Approved: %4$s ending in %5$s (expires %6$s)', 'woocommerce-plugin-framework'), $this->get_method_title(), $this->is_test_environment() ? esc_html_x('Test', 'noun, software environment', 'woocommerce-plugin-framework') : '', $this->perform_credit_card_authorization() ? esc_html_x('Authorization', 'credit card transaction type', 'woocommerce-plugin-framework') : esc_html_x('Charge', 'noun, credit card transaction type', 'woocommerce-plugin-framework'), SV_WC_Payment_Gateway_Helper::payment_type_to_name($card_type), $last_four, $order->payment->exp_month . '/' . substr($order->payment->exp_year, -2));
         // adds the transaction id (if any) to the order note
         if ($response->get_transaction_id()) {
             /* translators: Placeholders: %s - transaction ID */
             $message .= ' ' . sprintf(esc_html__('(Transaction ID %s)', 'woocommerce-plugin-framework'), $response->get_transaction_id());
         }
         /**
          * Direct Gateway Credit Card Transaction Approved Order Note Filter.
          *
          * Allow actors to modify the order note added when a Credit Card transaction
          * is approved.
          *
          * @since 4.1.0
          * @param string $message order note
          * @param \WC_Order $order order object
          * @param \SV_WC_Payment_Gateway_API_Response $response transaction response
          * @param \SV_WC_Payment_Gateway_Direct $this instance
          */
         $message = apply_filters('wc_payment_gateway_' . $this->get_id() . '_credit_card_transaction_approved_order_note', $message, $order, $response, $this);
         $order->add_order_note($message);
     }
     return $response;
 }
 /**
  * Determine the credit card type from the full account number
  *
  * @since 1.0.0
  * @deprecated since 4.0.0 in favor of SV_WC_Payment_Gateway_Helper::card_type_from_account_number()
  * @param string $account_number the credit card account number
  * @return string the credit card type
  */
 public static function type_from_account_number($account_number)
 {
     return SV_WC_Payment_Gateway_Helper::card_type_from_account_number($account_number);
 }