/** * Process payment & return result * * @since 2.0 */ public function process_payment($order_id) { $order = new WC_Order($order_id); $testmode = 'yes' == $this->testmode ? 'TRUE' : 'FALSE'; try { $authnet_request = array("x_tran_key" => $this->transkey, "x_login" => $this->apilogin, "x_amount" => number_format($order->get_total(), 2, '.', ''), "x_card_num" => $_POST['ccnum'], "x_card_code" => isset($_POST['cvv']) ? $_POST['cvv'] : '', "x_exp_date" => $_POST['expmonth'] . "-" . $_POST['expyear'], "x_type" => $this->salemethod, "x_version" => "3.1", "x_delim_data" => "TRUE", "x_relay_response" => "FALSE", "x_method" => "CC", "x_first_name" => $order->billing_first_name, "x_last_name" => $order->billing_last_name, "x_address" => $order->billing_address_1, "x_city" => $order->billing_city, "x_state" => $order->billing_state, "x_zip" => $order->billing_postcode, "x_country" => $order->billing_country, "x_phone" => $order->billing_phone, "x_email" => $order->billing_email, "x_ship_to_first_name" => $order->shipping_first_name, "x_ship_to_last_name" => $order->shipping_last_name, "x_ship_to_company" => $order->shipping_company, "x_ship_to_address" => $order->shipping_address_1, "x_ship_to_city" => $order->shipping_city, "x_ship_to_country" => $order->shipping_country, "x_ship_to_state" => $order->shipping_state, "x_ship_to_zip" => $order->shipping_postcode, "x_cust_id" => $order->user_id, "x_customer_ip" => $_SERVER['REMOTE_ADDR'], "x_tax" => "Order Tax<|>Order Tax<|>" . SV_WC_Plugin_Compatibility::wc_format_decimal($order->order_tax, 2), "x_invoice_num" => ltrim($order->get_order_number(), '#'), "x_test_request" => $testmode, "x_delim_char" => '|', "x_encap_char" => ''); // Don't send card details in the debug email $authnet_debug_request = $authnet_request; $authnet_debug_request['x_card_num'] = "XXXX"; $authnet_debug_request['x_card_code'] = "XXXX"; $authnet_debug_request['x_exp_date'] = "XXXX"; $this->send_debugging_email("URL: " . $this->gatewayurl . "\n\nSENDING REQUEST:" . print_r($authnet_debug_request, true)); // Send request $post = ''; foreach ($authnet_request as $key => $val) { $post .= urlencode($key) . "=" . urlencode($val) . "&"; } $post = substr($post, 0, -1); $response = wp_remote_post($this->gatewayurl, array('method' => 'POST', 'body' => $post, 'redirection' => 0, 'timeout' => 70, 'sslverify' => false)); if (is_wp_error($response)) { throw new Exception(__('There was a problem connecting to the payment gateway.', WC_Authorize_Net_AIM::TEXT_DOMAIN)); } if (empty($response['body'])) { throw new Exception(__('Empty Authorize.net response.', WC_Authorize_Net_AIM::TEXT_DOMAIN)); } $content = $response['body']; // prep response foreach (preg_split("/\r?\n/", $content) as $line) { if (preg_match("/^1|2|3\\|/", $line)) { $data = explode("|", $line); } } // store response $response['response_code'] = $data[0]; $response['response_sub_code'] = $data[1]; $response['response_reason_code'] = $data[2]; $response['response_reason_text'] = $data[3]; $response['approval_code'] = $data[4]; $response['avs_code'] = $data[5]; $response['transaction_id'] = $data[6]; $response['invoice_number_echo'] = $data[7]; $response['description_echo'] = $data[8]; $response['amount_echo'] = $data[9]; $response['method_echo'] = $data[10]; $response['transaction_type_echo'] = $data[11]; $response['customer_id_echo'] = $data[12]; $response['first_name_echo'] = $data[13]; $response['last_name_echo'] = $data[14]; $response['company_echo'] = $data[15]; $response['billing_address_echo'] = $data[16]; $response['city_echo'] = $data[17]; $response['state_echo'] = $data[18]; $response['zip_echo'] = $data[19]; $response['country_echo'] = $data[20]; $response['phone_echo'] = $data[21]; $response['fax_echo'] = $data[22]; $response['email_echo'] = $data[23]; $response['ship_first_name_echo'] = $data[24]; $response['ship_last_name_echo'] = $data[25]; $response['ship_company_echo'] = $data[26]; $response['ship_billing_address_echo'] = $data[27]; $response['ship_city_echo'] = $data[28]; $response['ship_state_echo'] = $data[29]; $response['ship_zip_echo'] = $data[30]; $response['ship_country_echo'] = $data[31]; $response['tax_echo'] = $data[32]; $response['duty_echo'] = $data[33]; $response['freight_echo'] = $data[34]; $response['tax_exempt_echo'] = $data[35]; $response['po_number_echo'] = $data[36]; $response['md5_hash'] = $data[37]; $response['cvv_response_code'] = $data[38]; $response['cavv_response_code'] = $data[39]; $this->send_debugging_email("RESPONSE RAW: " . $content . "\n\nRESPONSE:" . print_r($response, true)); // Retreive response if (1 == $response['response_code'] || 4 == $response['response_code']) { // Successful payment $order->add_order_note(__('Authorize.net payment completed', WC_Authorize_Net_AIM::TEXT_DOMAIN) . ' (Response Code: ' . $response['response_code'] . ')'); $order->payment_complete(); SV_WC_Plugin_Compatibility::WC()->cart->empty_cart(); // Return thank you redirect return array('result' => 'success', 'redirect' => $this->get_return_url($order)); } else { $this->send_debugging_email("AUTHORIZE.NET ERROR:\nresponse_code:" . $response['response_code'] . "\nresponse_reasib_text:" . $response['response_reason_text']); $cancelNote = __('Authorize.net payment failed', WC_Authorize_Net_AIM::TEXT_DOMAIN) . ' (Response Code: ' . $response['response_code'] . '). ' . __('Payment was rejected due to an error', WC_Authorize_Net_AIM::TEXT_DOMAIN) . ': "' . $response['response_reason_text'] . '". '; $this->mark_order_as_failed($order, $cancelNote); } } catch (Exception $e) { $this->mark_order_as_failed($order, sprintf(__('Connection error: %s', WC_Authorize_Net_AIM::TEXT_DOMAIN), $e->getMessage())); } }
/** * Perform a credit card capture for the given order * * @since 1.0 * @param $order WC_Order the order * @return null|SV_WC_Payment_Gateway_API_Response the response of the capture attempt */ public function do_credit_card_capture($order) { $order = $this->get_order_for_capture($order); try { $response = $this->get_api()->credit_card_capture($order); if ($response->transaction_approved()) { $message = sprintf(_x('%s Capture of %s Approved', 'Supports capture charge', $this->text_domain), $this->get_method_title(), get_woocommerce_currency_symbol() . SV_WC_Plugin_Compatibility::wc_format_decimal($order->capture_total)); // adds the transaction id (if any) to the order note if ($response->get_transaction_id()) { $message .= ' ' . sprintf(_x('(Transaction ID %s)', 'Supports capture charge', $this->text_domain), $response->get_transaction_id()); } $order->add_order_note($message); // prevent stock from being reduced when payment is completed as this is done when the charge was authorized add_filter('woocommerce_payment_complete_reduce_order_stock', '__return_false', 100); // complete the order $order->payment_complete(); // add the standard capture data to the order $this->add_capture_data($order, $response); // let payment gateway implementations add their own data $this->add_payment_gateway_capture_data($order, $response); } else { $message = sprintf(_x('%s Capture Failed: %s - %s', 'Supports capture charge', $this->text_domain), $this->get_method_title(), $response->get_status_code(), $response->get_status_message()); $order->add_order_note($message); } return $response; } catch (Exception $e) { $message = sprintf(_x('%s Capture Failed: %s', 'Supports capture charge', $this->text_domain), $this->get_method_title(), $e->getMessage()); $order->add_order_note($message); return null; } }