How to Build a WooCommerce Payment Gateway Plugin

by Owais Alam  May 9, 2017

The good thing about creating a WooCommerce payment extension is that the process is very simple. The most important challenge is the interaction with a merchant’s API for submission of payment requests.

 WooCommerce Payment Gateway Plugin

For the purpose of this article, I will use Authorize.net. If you do not have one, signup for a sandbox account to test the plugin.

Sandbox Account

The sandbox is a functional payment gateway with two important differences – No actual credit cards are processed and specific errors and responses could be triggered on demand to test various situations.

API Credentials

Structure of the WooCommerce Payment Extension Plugin

To make things easy to handle during the development and testing process, I will split the plugin in two files. These two files will be located in the cloudways-authorize-woocommerce-gateway directory (in the Plugin directory).  The first file registers the payment gateway and custom action link (cloudways-authorize-woocommerce-gateway.php) . The second file will have the class that will create administration fields (cloudways-authorize-woocommerce.php).

Development Of Plugin

Open up the file cloudways-authorize-woocommerce-gateway.php and add the following code to it:

Class Constructor

  • id — This is the global ID for the Payment method.
  • method_title — This is the title that appears on the top of the payment gateways Page.
  • method_description — This is the description for the payment gateway.
  • title — This is the title to be used for the vertical tabs.
  • icon — This is the optional image that appears with the gateway’s name on the front-end.
  • has_fields — This is a boolean option that could be set to ‘true’ if you want payment fields to show on the checkout .
  • supports — This indicates that the method supports the default credit card form.

Insert the above code in cloudways-authorize-woocommerce.php

Activate the Plugin

Now that the plugin has been created, activate the plugin.

Make sure that there are no errors. Go to WooCommerce >> Settings. This will take you to the Checkout Administration options:

WooCommerce setting

At this point, it is a good practice to include an option to set the test mode of the gateway.

  • enabled — Enable/Disable this payment gateway.
  • title — Payment title that the customer will see during the checkout process.
  • description — Payment description that the customer will see during the checkout process.
  • api_login — API Login provided by Authorize.net when you signed up for an account.
  • trans_key — Transaction Key provided by Authorize.net when you signed up for an account.
  • environment — Place the payment gateway in test mode or production.

Save changes

Payment Processing Method

The payment processing is handled by the method process_payment($order_id ).The form will create the following default form fields ( obtained from $_POST):

  • GATEWAYID-card-number
  • GATEWAYID-card-cvc
  • GATEWAYID-card-expiry

Payment Processing Method

Next, I will build the function process_payment(). This function handles all the transactions:

Now test the value of response_code. If the transaction goes through, you will receive a message that signifies success. Here is how the order notes change throughout the process:

Conclusion

Creating the WooCommerce payment extension is not a huge project or a difficult project to tackle. It usually takes just a few hours to create the extension. however, testing  is the key to success. this is where you should thoroughly test the code so that the WooCommerce store remains operational.

If you need more help, do leave a comment and I will get back to you.

Start Creating Web Apps on Managed Cloud Servers Now!

Easy Web App Deployment for Agencies, Developers and E-Commerce Industry

About Owais Alam

is the WordPress Community Manager at Cloudways - A Managed WooCommerce Hosting Platform and a seasoned PHP developer. He loves to develop all sorts of websites on WordPress and is in love with WooCommerce in particular. You can email him at owais.alam@cloudways.com

Stay Connected:

You Might Also Like...