Skip to main content

Make a Withdrawal via REST API (Java)

You can use our APIs to perform cash withdrawals.

This tutorial presents a simple approach to integrating the MOALA APIs to perform a cash withdrawal.

Prerequisites​

Before starting, make sure you have the following:

  • An account on the MOALA portal with the necessary permissions to make withdrawals. You can create a MOALA account by following the steps here: Get API Keys
  • A development environment with Java installed
  • The Apache HttpClient library 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 timestamp of the request in seconds
  • LP-ACCESS-SIGN: a signature generated using the secret key and LP-ACCESS-KEY

The LP-ACCESS-SIGN signature is generated using the HMAC-SHA256 algorithm on a concatenation of the key, timestamp, and request body.

2. Perform the Withdrawal​

We will now perform a cash withdrawal operation using MOALA's REST APIs.

Withdrawal Endpoint​

To make a withdrawal, use the following endpoint, with the serviceCode corresponding to the operator you want to use:

POST https://api.moala.africa/v1/api/transaction/payment
info

The valid serviceCode values for this endpoint are:

  • CASHOUT_MTN_CM: Cashout MTN Cameroon
  • CASHOUT_ORANGE_CM: Cashout Orange Cameroon
  • CASHOUT_AIRTEL_GA: Cashout Airtel Gabon
  • CASHOUT_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

Request Body​

Below are the required fields in the request body:

FieldTypeDescription
transactionTypestringType of transaction to perform (fixed value: deposit)
amountfloatAmount to withdraw
serviceCodestringCode of the service to use (see list above)
phoneNumberstringPhone number from which to make the withdrawal
partnerIdstringIdentifier of the partner initiating the withdrawal

Generating the Signature​

We will now write a function to calculate the signature for each request sent to the server.

// Generate request signature
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 {
String message = timestamp + method + endpoint + payload;
return HmacUtils.hmacSha256Hex(secretKey, message);
}
}

Sending the Withdrawal Request​

Now we build and send the withdrawal request.

import Utils;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
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 Withdrawal {
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 makeWithdrawal(Float amount, String serviceCode, String phoneNumber, String partnerId) {
try (CloseableHttpClient client = HttpClients.createDefault()) {
String endpoint = "/v1/api/transaction/payment";
String url = API_BASE_URL + endpoint;
long timestamp = Instant.now().getEpochSecond();

Map<String, Object> body = new HashMap<>();
body.put("amount", amount);
body.put("transactionType", "withdrawal");
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 StringEntity(payload));

CloseableHttpResponse response = client.execute(req);
System.out.println(EntityUtils.toString(response.getEntity()));
} catch (Exception e) {
e.printStackTrace();
}
}
}
warning

The partnerId field must be unique for each transaction, otherwise the request will fail.

3. Check Operation Status​

After sending the withdrawal request, you can verify the status by making another API call.

Status Check Endpoint​

Use the endpoint below to check the status of the previously submitted transaction:

GET https://api.moala.africa/v1/api/transaction/check/{partnerId}

Sending the Status 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();
}
}
}

4. Execute the Code​

Let’s now run the code above by attempting to withdraw 500 F.

import Withdrawal;
import StatusCheck;

public static void main(String args[]) {
String API_KEY = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
String SECRET_KEY = "yyyyyyyyyyyyyyyyyyyyyyyyyyy";
String serviceCode = "CASHOUT_ORANGE_CM";
String phoneNumber = "XXXXXXXXX";
String partnerId = "xxxxxxxx";
Float amount = 1000f;

Withdrawal withdrawal = new Withdrawal();
withdrawal.API_KEY = API_KEY;
withdrawal.SECRET_KEY = SECRET_KEY;
withdrawal.makeWithdrawal(amount, serviceCode, phoneNumber, partnerId);

StatusCheck statusCheck = new StatusCheck();
statusCheck.API_KEY = API_KEY;
statusCheck.SECRET_KEY = SECRET_KEY;
statusCheck.checkStatus(partnerId);
}

5. Conclusion​

At the end of this tutorial, you now know how to integrate MOALA's APIs to perform a cash withdrawal operation.