Making a Deposit via the REST API (Java)
You can use our APIs to make money deposits.
This tutorial presents a simple approach to integrating the MOALA APIs for the purpose of performing a money deposit operation.
Prerequisites
Before getting started, make sure you have the following:
- An account on the MOALA portal with the necessary permissions to perform deposits. You can get a Moala account by following the steps described here: Getting Access Keys
- A development environment with Java installed
- The
Apache HttpClientlibrary to make HTTP requests
1. Authentication
The MOALA API requires three headers for authentication and transaction security:
- LP-ACCESS-KEY: your MOALA API access key (obtained from the MOALA portal)
- LP-ACCESS-TIMESTAMP: the request timestamp in seconds
- LP-ACCESS-SIGN: a signature generated using the LP-ACCESS-KEY secret key
The LP-ACCESS-SIGN signature is generated using the HMAC-SHA256 algorithm on a concatenation of the key, timestamp, and request body.
Signature Generation
The following code computes the signature for each request sent to the server.
The generateSignature function generates the signature for the outgoing request.
// Request signature generation
import org.apache.commons.codec.digest.HmacUtils;
public class Utils {
public static String generateSignature(String secretKey, String timestamp, String method, String endpoint, String payload) throws Exception {
// Construction du message a hasher
String message = timestamp + method + endpoint + payload;
// Calcul de la signature
String signature = HmacUtils.hmacSha256Hex(secretKey, message);
// Renvoi de la signature
return signature;
}
}
2. Verify Customer KYC
Before making a deposit, it's important to perform a KYC check to ensure the deposit is made to the correct recipient.
KYC Check Endpoint
To verify KYC, use the endpoint below, with the corresponding serviceCode and the number (phone number) of the customer.
GET https://api.moala.africa/v1/api/kyc/{serviceCode}/{number}
The allowed serviceCode values for this endpoint are:
- CASHIN_MTN_CM: Cashout MTN Cameroon
- CASHIN_ORANGE_CM: Cashout Orange Cameroon
- CASHIN_AIRTEL_GA: Cashout Airtel Gabon
- CASHIN_MOOV_GA: Cashout Moov Gabon
- PAIEMENTMARCHAND_MTN_CM: Merchant Payment MTN Cameroon
- PAIEMENTMARCHAND_ORANGE_CM: Merchant Payment Orange Cameroon
- PAIEMENTMARCHAND_AIRTEL_GA: Merchant Payment Airtel Gabon
- PAIEMENTMARCHAND_MOOV_GA: Merchant Payment Moov Gabon
Sending the KYC Verification Request
The code below checks the KYC status of the recipient. The API response allows the depositor to confirm the recipient’s identity.
import KYC;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import java.time.Instant;
public class KYCCheck {
private static final String API_KEY = "votre_api_key";
private static final String SECRET_KEY = "votre_secret_key";
public static void checkKYC(String serviceCode, String number) {
try (CloseableHttpClient client = HttpClients.createDefault()) {
String endpoint = "/v1/api/kyc/" + serviceCode + "/" + number;
String url = API_BASE_URL + endpoint;
long timestamp = Instant.now().getEpochSecond();
String payload = "";
String method = "GET";
String signature = Utils.generateSignature(SECRET_KEY, String.valueOf(timestamp), method, endpoint, payload);
HttpGet get = new HttpGet(url);
get.setHeader("LP-ACCESS-KEY", API_KEY);
get.setHeader("LP-ACCESS-TIMESTAMP", String.valueOf(timestamp));
get.setHeader("LP-ACCESS-SIGN", signature);
CloseableHttpResponse response = client.execute(get);
System.out.println(EntityUtils.toString(response.getEntity()));
} catch (Exception e) {
e.printStackTrace();
}
}
}
3. Make the Deposit
We will now perform a deposit using MOALA's REST APIs.
Deposit Endpoint
To perform a deposit, use the following endpoint with the corresponding serviceCode.
POST https://api.moala.africa/v1/api/transaction/withdrawal
The allowed serviceCode values for this endpoint are the same as those listed in the KYC section.
Sending the Deposit Request
We will now build and send a request using the deposit API.
import Utils;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import com.google.gson.Gson;
import java.time.Instant;
import java.util.HashMap;
import java.util.Map;
public class Deposit {
private static final String API_KEY = "votre_api_key";
private static final String SECRET_KEY = "votre_secret_key";
private static final String API_BASE_URL = "https://api.moala.africa";
public static void makeDeposit(Float amount, String serviceCode, String phoneNumber, String partnerId) {
try (CloseableHttpClient client = HttpClients.createDefault()) {
String endpoint = "/v1/api/transaction/withdrawal";
String url = API_BASE_URL + endpoint;
long timestamp = Instant.now().getEpochSecond();
Map body = new HashMap<>();
body.put("amount", amount);
body.put("transactionType", "deposit");
body.put("serviceCode", serviceCode);
body.put("phoneNumber", phoneNumber);
body.put("partnerId", partnerId);
String payload = new Gson().toJson(body);
String method = "POST";
String signature = Utils.generateSignature(SECRET_KEY, String.valueOf(timestamp), method, endpoint, payload);
HttpPost req = new HttpPost(url);
req.setHeader("LP-ACCESS-KEY", API_KEY);
req.setHeader("LP-ACCESS-TIMESTAMP", String.valueOf(timestamp));
req.setHeader("LP-ACCESS-SIGN", signature);
req.setHeader("Content-Type", "application/json");
req.setEntity(new org.apache.http.entity.StringEntity(payload));
CloseableHttpResponse response = client.execute(req);
System.out.println(EntityUtils.toString(response.getEntity()));
} catch (Exception e) {
e.printStackTrace();
}
}
}
The partnerId field must be unique for each transaction, otherwise the request will fail.
4. Check the Deposit Status
After sending the deposit request, you can check if the operation was successful using another API call.
Status Check Endpoint
Use the endpoint below to verify the status of the deposit transaction.
GET https://api.moala.africa/v1/api/transaction/check/{partnerId}
Sending the Status Check Request
import Utils;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import java.time.Instant;
public class StatusCheck {
private static final String API_KEY = "votre_api_key";
private static final String SECRET_KEY = "votre_secret_key";
private static final String API_BASE_URL = "https://api.moala.africa";
public static void checkStatus(String partnerId) {
try (CloseableHttpClient client = HttpClients.createDefault()) {
String endpoint = "/v1/api/transaction/check/" + partnerId;
String url = API_BASE_URL + endpoint;
long timestamp = Instant.now().getEpochSecond();
String payload = "";
String method = "GET";
String signature = Utils.generateSignature(SECRET_KEY, String.valueOf(timestamp), method, endpoint, payload);
HttpGet get = new HttpGet(url);
get.setHeader("LP-ACCESS-KEY", API_KEY);
get.setHeader("LP-ACCESS-TIMESTAMP", String.valueOf(timestamp));
get.setHeader("LP-ACCESS-SIGN", signature);
CloseableHttpResponse response = client.execute(get);
System.out.println(EntityUtils.toString(response.getEntity()));
} catch (Exception e) {
e.printStackTrace();
}
}
}
5. Running the Code
Now let’s run the code shown above.
We will attempt to make a deposit of 500F from an Orange Money number.
import Deposit;
import StatusCheck;
import KYC;
public static void main(String args[]) {
// Define Variables
String API_KEY = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
String SECRET_KEY = "yyyyyyyyyyyyyyyyyyyyyyyyyy";
String kycServiceCode = "CASHIN_ORANGE_CM";
String depositServiceCode = "CASHIN_ORANGE_CM";
String number = "XXXXXXXXX";
String partnerId = "xxxxxxxx";
float amount = 1000;
// 1. Verify recipient KYC
KYCCheck kyc = new KYCCheck();
kyc.API_KEY = API_KEY;
kyc.SECRET_KEY = SECRET_KEY;
kyc.checkKYC(kycServiceCode, number);
// 2. Perform the deposit
Deposit deposit = new Deposit();
deposit.API_KEY = API_KEY;
deposit.SECRET_KEY = SECRET_KEY;
deposit.makeDeposit(500f, depositServiceCode, number, partnerId);
// 3. Check transaction status
StatusCheck statusCheck = new StatusCheck();
statusCheck.API_KEY = API_KEY;
statusCheck.SECRET_KEY = SECRET_KEY;
statusCheck.checkStatus(partnerId);
}
6. Conclusion
At the end of this tutorial, you now know how to integrate the MOALA APIs to perform a money deposit operation.