Custom Payment Gateway Integration
We have had a huge demand for adding a custom payment integration to your own EasyCart install and it is finally here. Starting with the release of version 3.0.19, you can add a live payment gateway that is customized by you, the programmer! Fair warning to those who are not familiar with PHP, this may be a little difficult to manage and is meant for those with a good understanding of programming.
The highest demand for this service is for those companies that use an Authorize.net emulator. This type of custom gateway is by far the easiest and will the first example provided here.
To start, lets explain how the custom payment gateway option is integrated in general, minus the programming details of customizing the class for your own payment gateway.
[divider]
Basic Custom Payment Gateway Class Setup
1. Start by going to your WordPress admin -> EasyCart Admin -> Store Setup -> Payment?Setup and in the Live Payment Gateways section, and select the last option “custom payment gateway”.
2. The system is now looking to include your new payment gateway file, so lets copy an existing payment gateway file into place and rename it so that it is included in the load of the plugin. Do this by copying the file “plugins/wp-easycart/inc/classes/gateway/ec_authorize.php” to “plugins/wp-easycart-data/ec_authorize.php” and rename the file here to “plugins/wp-easycart-data/ec_customgateway.php”.
3. The system is also looking for a class named “ec_customgateway” so lets fix this up. Open the newly copied ec_customgateway.php file and rename the class to read on the first line:
class ec_customgateway extends ec_gateway{
4. At this point you have a custom gateway setup, but need to customize this to function as a custom class to your system. Please read the next section about editing the class to work as an Authorize.net gateway emulator.
[divider]
Authorize.net Emulator Class Setup
If you have not completed the “Basic Custom Payment Gateway Class Setup”, please complete that section first. There are many companies that use the Authorize.net API as their method for collecting payment from their merchants. The reason they do this is that Authorize.net is one of the most popular, if not the most popular integration in the e-commerce industry and by leveraging their API, they can quickly integrate with any platform. Their system requires you to change the endpoint (the server that receives the transaction information) to point to their own payment servers. The following steps will help you alter the newly created payment class:
1. The first thing to do is enter your custom settings and there are two ways to do this. The easiest way is to first return to?WordPress admin -> EasyCart Admin -> Store Setup -> Payment?Setup and in the Live Payment Gateways section and change back to Authorize.net as your payment gateway. Enter your login id, transaction key, test mode off, and your currency code, then click save. At this point the authorize values are saved to your system, no matter what gateway you select from this point on. So you can now open the live payment gateway section again once the page reloads and switch it back to the custom payment gateway option. The other option here is to replace the following code:
$authorize_login_id = get_option( ‘ec_option_authorize_login_id’ );
$authorize_trans_key = get_option( ‘ec_option_authorize_trans_key’ );
$authorize_test_mode = get_option( ‘ec_option_authorize_test_mode’ );
$authorize_currency_code = get_option( ‘ec_option_authorize_currency_code’ );
TO:
$authorize_login_id =?”YOURLOGINID”;
$authorize_trans_key = “YOURTRANSKEY”;
$authorize_test_mode = false;
$authorize_currency_code = “USD”;
2. Edit the get_gateway_url method to use the emulator endpoint instead of the authorize endpoint.?The edit will change:
$is_developer_account = get_option( ‘ec_option_authorize_developer_account’ );
if( $is_developer_account )
return “https://test.authorize.net/gateway/transact.dll”;
else
return “https://secure.authorize.net/gateway/transact.dll”;
TO:
return “https://yourendpoint.com/endpoint”;
3. At this point you should be able to upload your custom gateway and run it successfully. We have noticed some errors occur in that the response may not be returned in the “body” as Authorize.net does. If this is the case, you will need to investigate the format of the response and is easily done by changing:
function handle_gateway_response( $response ){
TO:
function handle_gateway_response( $response ){
print_r( $response ); die( );
which will print the response to the screen and stop once printed. Use this to point yourself to the right response information to process and remove it when you are happy with your understanding of the response data.
[divider]
Custom Payment Gateway Company
We also know there may be some of you out there that have a company you would like to integrate with that does not follow closely to any other payment gateway. We will do our best here to describe our process for custom integration with payment gateways.
1. We recommend using the ec_securenet.php payment gateway class as a starting point. The reason for this is that it covers many of the most common requirements for custom gateway integration.
2. Typically there are two types of data format that are sent to your payment gateway, an array or XML string. The ec_securenet.php shows gateway data in the format of an array, use this is a starting point if you are going that route. If you are using a gateway that accepts XML data, look into the ec_securepay.php file instead.
3. Notice that ec_securenet.php does not use the method “get_gateway_data” as this is not necessary when you need to use a custom CURL call (ec_securenet.php does this). Some gateways work great with the default call and in this case, follow the format of returning the data through the “get_gateway_data” method. This is a technical option based on the requirements of the payment gateway. Following the ec_securenet method is the easiest way to avoid technical difficulties.
4. Some gateways require an authorization header and ec_securenet as an example of this. If your gateway does not require this, you can remove the line that adds this to the header array.
5. Once you are able to build and send your gateway data, the next, and most difficult, part is to process the response. ?We always send the response data to the “handle_gateway_response” method for processing. Please notice that if you are viewing the securenet version, the response from them is in JSON formatting and is why we process it through the json_decode function first, but many gateways return their data as an array in the $response[‘body’] array element. Our recommendation is to start by sending the whole response into the “handle_gateway_response” method and use a print out to determine where to go from there. Once you send the data to the response function, we should start by adding “print_r( $response ); die( );” to the top of the “handle_gateway_response” method. This will show you the path to getting the response messages out of the data and applying them to the correct class data elements.
6. The first processing step is determining the attribute that signifies a successful payment, typically an “APPROVED” or “SUCCESS” value and may be accessed through $response[‘body’]->result or $response[‘result’]. If XML is returned, you may need to first run the $response data through:
$response_body = $response[“body”];
$xml = new SimpleXMLElement($response_body);
and access the value by $xml->result or something similar. This is the most difficult task because it requires you to have a complete knowledge of the type of response and if it is formatted as an?array, object, xml, JSON, etc… Once you are able to access and check for success, set the $this->is_success to true or false.
7. We also ask that if there is an error, try to set $this->error_message = $xml->response_message; or whatever your response error message is.
8. The last thing to do is insert the response to the database for later access and troubleshooting. This is done by editing the following line to fit your needs:
$this->mysqli->insert_response( $this->order_id, !$this->is_success, “Gateway NAME”, print_r( $response, true ) );
If you are able to get through all of this, get a response from your gateway, process the return data?successfully, and set the success value to true or false, then you are done. The EasyCart will insert and process the order based on your success value and requires nothing else once you get your gateway setup to process the credit card through your own gateway.
[divider]
Successful Custom Payment Integration
If and when you complete your custom payment integration, you are welcome to send to us for review and to add the gateway as a default option in the cart.?Having?us add new gateways to the cart is especially useful for developers that reuse the EasyCart for multiple projects and want the same gateway to be available quickly. We are always open to adding new gateways, so please contact us in the event that you would like your gateway in the cart by default!