Building A Payment Application using Python Flask Framework, Docker and the Beyonic API.
Tags: python, payment, flask, docker
Published: 2021-10-10 19:12:24

In this article, we are going to learn how to create a payment application using Python flask framework, Docker and the Beyonic API. 

If you want some basics on how to use Flask to build web applications or APIS, I recommend you to read my previous article on Getting Started with Python Web Development 👇🏻 {% link https://dev.to/grayhat/getting-started-with-python-web-development-1jl9 %}

### **Beyonic Api**

The Beyonic API is a REST based application programming interface that lets you extend the Beyonic dashboard features into your application and systems, allowing you to build amazing payment experiences.

**With the Beyonic API you can:**
- Receive and send money and prepaid airtime.
- List currencies and networks supported by the Beyonic API.
- Check whether a bank is supported by the Beyonic API.
- View your account transactions history.
- Add, retrieve, list, and update contacts to your Beyonic account.
- Use webhooks to send notifications to URLs on your server that when specific events occur in your Beyonic account (e.g. payments). 


**Getting Help**

For usage, general questions, and discussions about the Beyonic API the best place to go to is [Beyhive Slack Community](https://beyonic.com/join-community), also feel free to clone and edit this repository to meet your project, application or system requirements.

You can download all code snippets used in this article from this GitHub repository 👇🏻
{% github https://github.com/HarunMbaabu/BeyonicAPI-Python-Examples %}

To start using the Beyonic Python API, you need to start by downloading the Beyonic API official Python client library and setting your secret key.

**Install the Beyonic API Python Official client library.**

```python 
>>> pip install beyonic
``` 

**Setting your secret key.**

To set the secret key install the python-dotenv modeule, Python-dotenv is a Python module that allows you to specify environment variables in traditional UNIX-like “.env” (dot-env) file within your Python project directory, it helps us work with SECRETS and KEYS without exposing them to the outside world, and keep them safe during development too.

**Installing python-dotenv modeule**
 
```python 
>>> pip install python-dotenv
```

Creating a .env file to keep our secret keys.

```python
>>> touch .env
``` 
Inside your .env file specify the Beyonic API Token .

**.env file**

```python
BEYONIC_ACCESS_KEY = "enter your API key here"
```  
You will get your API Token by clicking your user name on the bottom left of the left sidebar menu in the Beyonic web portal and selecting ‘Manage my account’ from the dropdown menu. The API Token is shown at the very bottom of the page. 

**getExamples.py** 

```python 
import os 
import beyonic
from dotenv import load_dotenv 

load_dotenv()

myapi = os.environ['BEYONIC_ACCESS_KEY']

beyonic.api_key = myapi 

# Listing account: Working. 
accounts = beyonic.Account.list() 
print(accounts)


#Listing currencies: Not working yet.
'''
supported_currencies = beyonic.Currency.list()
print(supported_currencies)

Supported currencies are: USD, UGX, KES, BXC, GHS, TZS, RWF, ZMW, MWK, BIF, EUR, XAF, GNF, XOF, XOF
'''

#Listing networks: Not working yet.
"""
networks = beyonic.Network.list()
print(networks)
"""

#Listing transactions: Working. 
transactions = beyonic.Transaction.list()
print(transactions) 

#Listing contact: Working. 
mycontacts = beyonic.Contact.list() 
print(mycontacts) 


#Listing events: Not working yet.
'''
events = beyonic.Event.list()
print(events)

Error: AttributeError: module 'beyonic' has no attribute 'Event'
'''

``` 

You can get these and more examples implemented using flask and Fast API frameworks from the links below: 

1). [Flask framework examples](https://github.com/HarunMbaabu/BeyonicAPI-Python-Examples/tree/main/FLASK).

2). [Fast API framework examples](https://github.com/HarunMbaabu/BeyonicAPI-Python-Examples/tree/main/FASTAPI).

### **Dockerfile**

A **Dockerfile** is a text document that contains all the commands a user could call on the command line to assemble an image. Using docker build users can create an automated build that executes several command-line instructions in succession. 

```Dockerfile
FROM python:3.8-slim-buster

COPY . .

COPY ./requirements.txt ./requirements.txt

WORKDIR .

RUN pip install -r requirements.txt

CMD [ "python3", "getExamples.py" ]
``` 

Build docker image called demo


```python
>>> docker build -t bey .
``` 

Run docker image called demo 

```python
>>>docker run -t -i bey  
``` 

Now, let create a Docker compose file to build and run a docker container using the Docker image we just created.
 
## **docker-compose.yml**

A **docker-compose.yml** is a config file for docker-compose. it allows to deploy, combine and configure multiple docker-container at the same time. the Docker "rule" is to outsource every single process to an own docker container.

```yaml
version: "3.6"
services:
  app:
    build: .
    command: python getExamples.py
    volumes:
      - .:/pythonBeyonicExamples
```
 
Now we are going to run the following command from the same directory where the **docker-compose.yml** file is located. The docker compose up command will start and run the entire app.

```python. 
>>> docker compose up 
``` 

**Output**
NB: The screenshot below might differ according to your account deatils and your transcations in deatils. 

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tq80x9uds7w47owi7mu9.png)
 
To stop the container running on daemon mode use the below command.

```python
>>> docker compose stop
```

**Output**
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g3rfdgh8x0v2cfmj0xjc.png)
 
### **Contributing to this repository.**

All contributions, bug reports, bug fixes, enhancements, and ideas are welcomed. I hope you enjoy reading the article as much as i enjoyed writing it, you can connect with me on twitter 👉🏻 [@HarunMbaabu](https://twitter.com/HarunMbaabu).