Payment is one of the most used integrations in modern application development. Without it, we would not be able to finance our business and enable our customers to get the services they require from us.
There are many available API’s that can help us achieve this, including Daraja API that enables you to accept payment through Lipa na Mpesa. One limitation of this API is that you can only receive payment using Lipa Na Mpesa. To overcome this, let’s introduce Africa’s Talking API.
Africa’s Talking Payment APIs enables you to seamlessly receive payments from payment providers (such as Consumer-to-Business mobile payments), as well as initiate payments going out to your customers (such as Business-to-Consumer mobile payments). Not only can you receive payment from mobile payment but also from Banks!
In this article, we will be focusing on Mobile Checkout and how to receive payment from customers using mobile money.
- You should have an Africa’s talking developer account. If you don’t have one, you can get it from here.
- You should have the knowledge of developing android apps with Kotlin.
Let’s get started.
Africa’s Talking provides a sandbox where we can develop and test our implementation of their payment API. To get started, let’s create a payment product that we will be using to receive payment from the user. The product name will be used when creating a payment request. Enter the product name e.g. Apewe Fish and press submit.
The last thing we need to set-up is the payment channel; this will act as your business number or pay bill number. To set it up, click on the three dots on your product and click add channel. After you have added a channel click submit.
A callback URL is used to notify us when the user has authorized the payment or not. Africa’s Talking API uses Notification to notify you when something has occurred. In the case of payment, they use Payment Notification. We will cover this in the next part of our subsequent article.
Let’s now generate an API key that helps Africa’s Talking authenticate requests that you make to their APIs. To generate the API key head over to the Settings section of the Dashboard, enter your account password and an API key will be generated for you.
Let’s get started with adding Africa’s Talking SDK to our project.
To add the library name and version. Add the following line of code under dependencies.
To begin using the SDK, we need to initialize it. The initialization function accepts two parameters i.e. USER_NAME and API_KEY. As we are using SANDBOX, the username will be “sandbox” and API_KEY you can generate it here.
After initializing the SDK, we get a payment service that will be used to start our mobile checkout process.
The mobile checkout function accepts five parameters, product name, phone number, currency code, amount metadata and provider channel.
- Product name is the name of the payment product you created. e.g. “Apewe Fish.”
- Phone Number should be in the international format, e.g. +254790689—
- Currency Code should be 3-Letter ISO currency Code, e.g. KES.
- The amount should be a floating-point variable.
- Metadata should be any information that you seek to send with the payment request that will be included in the final payment notification.
- Provider Channel the business number of pay bill number of the product you want to receive payment from, e.g. 11001.
To start the payment request, we pass the required values to the mobile checkout function and wait to receive the payment API response.
The API response is a JSON object that contains; status, description, transaction id and provider channel.
status shows the status of the request which are “PendingConfirmation”, “InvalidRequest”, “NotSupported” and “Failed”.
description show a detailed description of the payment request.
When we receive a “PendingConfirmation” response, we know our request is successful, and we are only waiting for the confirmation from the user; otherwise, our request failed due to some reason you can find out more on the description.
After a successful full request, a prompt will be initiated in the user mobile phone with the details of the payment, including name and channel id for confirmation.
Testing Code in Sandbox
Africa’s Talking provides a Phone Simulator that we can use to test our code in the sandbox before proceeding to live. Using the sandbox prevents us from incurring any cost associated with our payment.
To set up the simulator click, here and enter the mobile phone you will be using to test your code.
After configuring the simulator, run the code on your physical device and see what happens in the simulator.
After a Successful request, a confirmation prompt is displayed for the user to confirm the payment. Once the user confirms the payment, the transaction is done and can be viewed in the transactions part of your Payment Product.
Africa’s Talking provides a seamless payment API that enables developers to receive payment from Mobile and also Banks. This removes the need for using various APIs to tackle payment. With them, all is covered in one single API.
- Never Store your API Keys on your application or Repository. There are many ways of storing your API key. You can find some insights here.
- You can develop and deploy your callback URL using NodeJs and Deploy it in Firebase Cloud Functions. To find more about Notification Callbacks proceed to Africa’s Talking Payment Notification page.
Find code here