Skip to content

Instantly share code, notes, and snippets.

@yojance
Created August 2, 2014 18:23
Show Gist options
  • Select an option

  • Save yojance/15018144242f2a988f72 to your computer and use it in GitHub Desktop.

Select an option

Save yojance/15018144242f2a988f72 to your computer and use it in GitHub Desktop.
Processing the Payment
// Submit payment and handle response
public function process_payment( $order_id ) {
global $woocommerce;
// Get this Order's information so that we know
// who to charge and how much
$customer_order = new WC_Order( $order_id );
// Are we testing right now or is it a real transaction
$environment = ( $this->environment == "yes" ) ? 'TRUE' : 'FALSE';
// Decide which URL to post to
$environment_url = ( "FALSE" == $environment )
? 'https://secure.authorize.net/gateway/transact.dll'
: 'https://test.authorize.net/gateway/transact.dll';
// This is where the fun stuff begins
$payload = array(
// Authorize.net Credentials and API Info
"x_tran_key" => $this->trans_key,
"x_login" => $this->api_login,
"x_version" => "3.1",
// Order total
"x_amount" => $customer_order->order_total,
// Credit Card Information
"x_card_num" => str_replace( array(' ', '-' ), '', $_POST['spyr_authorizenet_aim-card-number'] ),
"x_card_code" => ( isset( $_POST['spyr_authorizenet_aim-card-cvc'] ) ) ? $_POST['spyr_authorizenet_aim-card-cvc'] : '',
"x_exp_date" => str_replace( array( '/', ' '), '', $_POST['spyr_authorizenet_aim-card-expiry'] ),
"x_type" => 'AUTH_CAPTURE',
"x_invoice_num" => str_replace( "#", "", $customer_order->get_order_number() ),
"x_test_request" => $environment,
"x_delim_char" => '|',
"x_encap_char" => '',
"x_delim_data" => "TRUE",
"x_relay_response" => "FALSE",
"x_method" => "CC",
// Billing Information
"x_first_name" => $customer_order->billing_first_name,
"x_last_name" => $customer_order->billing_last_name,
"x_address" => $customer_order->billing_address_1,
"x_city" => $customer_order->billing_city,
"x_state" => $customer_order->billing_state,
"x_zip" => $customer_order->billing_postcode,
"x_country" => $customer_order->billing_country,
"x_phone" => $customer_order->billing_phone,
"x_email" => $customer_order->billing_email,
// Shipping Information
"x_ship_to_first_name" => $customer_order->shipping_first_name,
"x_ship_to_last_name" => $customer_order->shipping_last_name,
"x_ship_to_company" => $customer_order->shipping_company,
"x_ship_to_address" => $customer_order->shipping_address_1,
"x_ship_to_city" => $customer_order->shipping_city,
"x_ship_to_country" => $customer_order->shipping_country,
"x_ship_to_state" => $customer_order->shipping_state,
"x_ship_to_zip" => $customer_order->shipping_postcode,
// Some Customer Information
"x_cust_id" => $customer_order->user_id,
"x_customer_ip" => $_SERVER['REMOTE_ADDR'],
);
// Send this payload to Authorize.net for processing
$response = wp_remote_post( $environment_url, array(
'method' => 'POST',
'body' => http_build_query( $payload ),
'timeout' => 90,
'sslverify' => false,
) );
if ( is_wp_error( $response ) )
throw new Exception( __( 'We are currently experiencing problems trying to connect to this payment gateway. Sorry for the inconvenience.', 'spyr-authorizenet-aim' ) );
if ( empty( $response['body'] ) )
throw new Exception( __( 'Authorize.net\'s Response was empty.', 'spyr-authorizenet-aim' ) );
// Retrieve the body's resopnse if no errors found
$response_body = wp_remote_retrieve_body( $response );
// Parse the response into something we can read
foreach ( preg_split( "/\r?\n/", $response_body ) as $line ) {
$resp = explode( "|", $line );
}
// Get the values we need
$r['response_code'] = $resp[0];
$r['response_sub_code'] = $resp[1];
$r['response_reason_code'] = $resp[2];
$r['response_reason_text'] = $resp[3];
// Test the code to know if the transaction went through or not.
// 1 or 4 means the transaction was a success
if ( ( $r['response_code'] == 1 ) || ( $r['response_code'] == 4 ) ) {
// Payment has been successful
$customer_order->add_order_note( __( 'Authorize.net payment completed.', 'spyr-authorizenet-aim' ) );
// Mark order as Paid
$customer_order->payment_complete();
// Empty the cart (Very important step)
$woocommerce->cart->empty_cart();
// Redirect to thank you page
return array(
'result' => 'success',
'redirect' => $this->get_return_url( $customer_order ),
);
} else {
// Transaction was not succesful
// Add notice to the cart
wc_add_notice( $r['response_reason_text'], 'error' );
// Add note to the order for your reference
$customer_order->add_order_note( 'Error: '. $r['response_reason_text'] );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment