Unlimited Plugins, WordPress themes, videos & courses! Unlimited asset downloads! From $16.50/m
  1. Code
  2. OpenCart

Integrate Bitcoin Payment Gateway Into OpenCart: Part 2

This post is part of a series called Integrate Bitcoin Payment Gateway Into OpenCart.
Integrate Bitcoin Payment Gateway Into OpenCart: Part 1

In the previous tutorial, we began implementing a Bitcoin payment system. Specifically, we introduced a dashboard to enable our users to administer the plugin. In this tutorial, we're going to continue working on the plugin by building the store front-end.

1. The Controller

  1. Navigate to catalog/controller/payment
  2. Create a PHP file and name it to bitpay.php
  3. Open the file in your favorite IDE and write a class of this module using the OpenCart class naming convention, for example: class ControllerPaymentBitpay extends Controller {}
  4. Inside the class, create the default OpenCart index function.
  5. Inside the index() function, write the code as below.

1.1 Loading Languages & Parsing Values

The first line of the following code loads its language into the controller, and the next lines parse links and language texts for the view use.

1.2. Template & Rendering

To set the template destination:

To render the template: $this->render();

2. An API Request Function

Up to this point, we've only written code that deals specifically with the module's settings and its layout. Here, we're going to focus specifically on communicating with the API. In this regard we will create another function inside our controller which is responsible for communicating with the BitPay API.

So we will be creating a public function send(){} to achieve our goal, and right inside this function the code is explained line by line as below:

2.1 Loading Required Libraries/Classes Into Controller

We have two required libraries/classes to be loaded into our controller, i.e. the BitPay Library API and the Order Model:

(As the BitPay Library is not an OpenCart-based library, we directly included its functions into our controller as shown in the first line above.)

2.2 Getting Order Details & Formatting

To get the complete Order Details we can use this:

 The assigned variables get all the necessary details in the form of an array.

And for formatting the order default selected currency, we use:

2.3 Interacting with API

The following code is responsible for interacting with the API and parsing the required data to the API function.

2.4 Error Handling

For some invalid actions the API throws an error back to user. So the following code throws a response in JSON format to us:

3. Callback Function

It should be clear from its name that this would be the API callback function. It returns the necessary payment information and throws it to user. Follow the steps as below:

  1. Create a public function callback()
  2. Inside the function, include the library: DIR_APPLICATION.'../bitpay/bp_lib.php';.
  3. Get the API Response Array Key using the following code:
    $apiKey   = $this->config->get('bitpay_api_key');
    $response = bpVerifyNotification($apiKey); 
  4. Check for a response. If the response is a string, there must be an error: 
    if (is_string($response))
    // Display the response error code here            
  5. And inside the else statement we need to confirm the order by using the code shown below.

The View

The view we created is quite simple, just one button with an Ajax request to the API:


This series serves as an introduction to building more advanced OpenCart modules. From here, you should be able to follow the OpenCart documentation in order to continue building out this plugin (and even some of your own).

Please leave all questions, comments, and feedback in the form below.

Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.