id = 'authorizenet_lightweight'; $this->icon = plugins_url( 'images/authorizenet_lightweight.png', __FILE__ ); $this->has_fields = true; $this->method_title = 'Authorize.Net Lightweight Cards Settings'; $this->init_form_fields(); $this->init_settings(); $this->supports = array( 'products'); $this->title = $this->get_option( 'authorizenet_lightweight_title' ); $this->authorizenet_lightweight_description = $this->get_option( 'authorizenet_lightweight_description'); $this->authorizenet_lightweight_apilogin = $this->get_option( 'authorizenet_lightweight_apilogin' ); $this->authorizenet_lightweight_transactionkey = $this->get_option( 'authorizenet_lightweight_transactionkey' ); $this->authorizenet_lightweight_sandbox = $this->get_option( 'authorizenet_lightweight_sandbox' ); $this->authorizenet_lightweight_authorize_only = $this->get_option( 'authorizenet_lightweight_authorize_only' ); $this->authorizenet_lightweight_cardtypes = $this->get_option( 'authorizenet_lightweight_cardtypes'); $this->authorizenet_lightweight_liveurl = 'https://secure2.authorize.net/gateway/transact.dll'; $this->authorizenet_lightweight_testurl = 'https://test.authorize.net/gateway/transact.dll'; if(!defined("AUTHORIZE_NET_SANDBOX")) {define("AUTHORIZE_NET_SANDBOX", ($this->authorizenet_lightweight_sandbox =='yes'? true : false));} if(!defined("AUTHORIZE_NET_TRANSACTION_MODE")) {define("AUTHORIZE_NET_TRANSACTION_MODE",($this->authorizenet_lightweight_authorize_only =='yes'? 'AUTH_ONLY':'AUTH_CAPTURE'));} if (is_admin()) { add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) ); } } public function admin_options() { ?>
';
}
return apply_filters( 'woocommerce_authorizenet_lightweight_icon', $icon, $this->id );
}
public function get_payment_method_image_url( $type ) {
$image_type = strtolower( $type );
return WC_HTTPS::force_https_url( plugins_url( 'images/' . $image_type . '.png' , __FILE__ ) );
}
/*Get Icon*/
/*Get Card Types*/
function get_card_type($number)
{
$number=preg_replace('/[^\d]/','',$number);
if (preg_match('/^3[47][0-9]{13}$/',$number))
{
return 'amex';
}
elseif (preg_match('/^3(?:0[0-5]|[68][0-9])[0-9]{11}$/',$number))
{
return 'dinersclub';
}
elseif (preg_match('/^6(?:011|5[0-9][0-9])[0-9]{12}$/',$number))
{
return 'discover';
}
elseif (preg_match('/^(?:2131|1800|35\d{3})\d{11}$/',$number))
{
return 'jcb';
}
elseif (preg_match('/^5[1-5][0-9]{14}$/',$number))
{
return 'mastercard';
}
elseif (preg_match('/^4[0-9]{12}(?:[0-9]{3})?$/',$number))
{
return 'visa';
}
else
{
return 'unknown card';
}
}// End of getcard type function
//Function to check IP
function get_client_ip()
{
$ipaddress = '';
if (getenv('HTTP_CLIENT_IP'))
$ipaddress = getenv('HTTP_CLIENT_IP');
else if(getenv('HTTP_X_FORWARDED_FOR'))
$ipaddress = getenv('HTTP_X_FORWARDED_FOR');
else if(getenv('HTTP_X_FORWARDED'))
$ipaddress = getenv('HTTP_X_FORWARDED');
else if(getenv('HTTP_FORWARDED_FOR'))
$ipaddress = getenv('HTTP_FORWARDED_FOR');
else if(getenv('HTTP_FORWARDED'))
$ipaddress = getenv('HTTP_FORWARDED');
else if(getenv('REMOTE_ADDR'))
$ipaddress = getenv('REMOTE_ADDR');
else
$ipaddress = '0.0.0.0';
return $ipaddress;
}
//End of function to check IP
/*Initialize Authorize.Net Parameters*/
public function authorizenet_lightweight_params($wc_order)
{
$exp_date = explode( "/", sanitize_text_field($_POST['authorizenet_lightweight-card-expiry']));
$exp_month = str_replace( ' ', '', $exp_date[0]);
$exp_year = str_replace( ' ', '',$exp_date[1]);
if (strlen($exp_year) == 2) {
$exp_year += 2000;
}
$authorizenet_lightweight_args = array(
'x_login' => $this->authorizenet_lightweight_apilogin,
'x_tran_key' => $this->authorizenet_lightweight_transactionkey,
'x_version' => '3.1',
'x_delim_data' => 'TRUE',
'x_relay_response' => 'FALSE',
'x_type' => AUTHORIZE_NET_TRANSACTION_MODE,
'x_method' => 'CC',
'x_delim_char' => '|',
'x_encap_char' => '',
'x_card_num' => sanitize_text_field(str_replace(' ','',$_POST['authorizenet_lightweight-card-number'])),
'x_exp_date' => $exp_month.$exp_year,
'x_card_code' => sanitize_text_field($_POST['authorizenet_lightweight-card-cvc']),
'x_invoice_num' => $wc_order->get_order_number(),
'x_description' => get_bloginfo('blogname').' Order #'.$wc_order->get_order_number(),
'x_amount' => $wc_order->order_total,
'x_first_name' => $wc_order->billing_first_name ,
'x_last_name' => $wc_order->billing_last_name ,
'x_company' => $wc_order->billing_company ,
'x_address' => $wc_order->billing_address_1 .','.$wc_order->billing_address_2,
'x_country' => $wc_order->billing_country,
'x_phone' => $wc_order->billing_phone,
'x_state' => $wc_order->billing_state,
'x_city' => $wc_order->billing_city,
'x_zip' => $wc_order->billing_postcode,
'x_email' => $wc_order->billing_email,
'x_cust_id' => $wc_order->user_id,
'x_ship_to_first_name' => $wc_order->shipping_first_name,
'x_ship_to_last_name' => $wc_order->shipping_last_name,
'x_ship_to_company' => $wc_order->shipping_company,
'x_ship_to_address' => $wc_order->shipping_address_1.','.$wc_order->shipping_address_2,
'x_ship_to_city' => $wc_order->shipping_city,
'x_ship_to_state' => $wc_order->shipping_state,
'x_ship_to_zip' => $wc_order->shipping_postcode,
'x_ship_to_country' => $wc_order->shipping_country,
'x_customer_ip' => $this->get_client_ip(),
'x_tax' => $wc_order->get_total_tax() ,
'x_freight' => $wc_order->get_total_shipping(),
'x_header_email_receipt' => 'Order Receipt '.get_bloginfo('blogname'),
'x_footer_email_receipt' => 'Thank you for Using '.get_bloginfo('blogname')
);
return $authorizenet_lightweight_args;
} // End of authorizenet_lightweight_params
/*Start of credit card form */
public function payment_fields() {
echo apply_filters( 'wc_authorizenet_lightweight_description', wpautop(wp_kses_post( wptexturize(trim($this->authorizenet_lightweight_description) ) ) ) );
$this->form();
}
public function field_name( $name ) {
return $this->supports( 'tokenization' ) ? '' : ' name="' . esc_attr( $this->id . '-' . $name ) . '" ';
}
public function form() {
wp_enqueue_script( 'wc-credit-card-form' );
$fields = array();
$cvc_field = 'field_name( 'card-cvc' ) . '/>
'; $default_fields = array( 'card-number-field' => 'field_name( 'card-number' ) . ' />
', 'card-expiry-field' => 'field_name( 'card-expiry' ) . ' />
', 'card-cvc-field' => $cvc_field ); $fields = wp_parse_args( $fields, apply_filters( 'woocommerce_credit_card_form_fields', $default_fields, $this->id ) ); ?> get_card_type(sanitize_text_field(str_replace(' ','',$_POST['authorizenet_lightweight-card-number']))); if(!in_array($cardtype ,$this->authorizenet_lightweight_cardtypes )) { wc_add_notice('Merchant do not support accepting in '.$cardtype, $notice_type = 'error' ); return array ( 'result' => 'success', 'redirect' => WC()->cart->get_checkout_url(), ); die; } if('yes' == AUTHORIZE_NET_SANDBOX) { $gatewayurl = $this->authorizenet_lightweight_testurl; } else { $gatewayurl = $this->authorizenet_lightweight_liveurl; } $params = $this->authorizenet_lightweight_params($wc_order); $post_string = ''; foreach( $params as $key => $value ) { $post_string .= urlencode( $key )."=".urlencode($value )."&"; } $post_string = rtrim($post_string,"&"); /*HTTP POST API*/ $response = wp_remote_post( $gatewayurl, array( 'method' => 'POST', 'body' => $post_string, 'redirection' => 0, 'timeout' => 70, 'sslverify' => false, ) ); if ( is_wp_error( $response ) ) throw new Exception( __( 'Problem connecting to the payment gateway.', 'woocommerce' ) ); if ( empty( $response['body'] ) ) throw new Exception( __( 'Empty Authorize.net response.','woocommerce') ); $content = $response['body']; foreach ( preg_split("/\r?\n/", $content) as $line ) { if ( preg_match("/^1|2|3\|/", $line ) ) { $response_array = explode( "|", $line ); } } /*HTTP POST API */ if ( count($response_array) > 1 ) { if( (1 == $response_array[0] ) || ( 4 == $response_array[0] ) ) { $wc_order->add_order_note( __( $response_array[3]. 'on '.date("d-m-Y h:i:s e").' with Transaction ID = '.$response_array[6].' using '.$response_array[11].', authorization code ='.$response_array[4].', card code verification='.$response_array[38].', cardholder authentication verification response code='.$response_array[39].', Card Type='.$response_array[51].', Last 4='.$response_array[50], 'woocommerce' ) ); $wc_order->payment_complete($response_array[6]); WC()->cart->empty_cart(); return array ( 'result' => 'success', 'redirect' => $this->get_return_url( $wc_order ), ); } else { $wc_order->add_order_note( __( 'Response: '.$response_array[3], 'woocommerce' ) ); wc_add_notice($response_array[3], $notice_type = 'error' ); } } else { $wc_order->add_order_note( __( 'Response: '.$response_array[3], 'woocommerce' ) ); wc_add_notice($response_array[3], $notice_type = 'error' ); } }// End of process_payment }// End of class WC_Authorizenet_Lightweight_Gateway } // End if WC_Payment_Gateway }// End of function authorizenet_lightweight_init add_action( 'plugins_loaded', 'authorizenet_lightweight_init' ); /*Plugin Settings Link*/ function authorizenet_lightweight_settings_link( $links ) { $settings_link = '' . __( 'Settings' ) . ''; array_push( $links, $settings_link ); return $links; } $plugin = plugin_basename( __FILE__ ); add_filter( "plugin_action_links_$plugin", 'authorizenet_lightweight_settings_link' );