/mpesa-java-sdk

A Java library aiming to help developers integrating their products with M-Pesa Platform

Primary LanguageJava

M-Pesa SDK for JAVA

M-Pesa SDK for JAVA is an unofficial library aiming to help develbusinesses integrating every M-Pesa operations to their JAVA applications.

Features

  • Receive money from a mobile account to a business account
  • Send money from a business account to a mobile account
  • Send money from a business account to a another business account
  • Revert a transaction
  • Query the status of a transaction

Usage

Receive Money from a Mobile Account

import org.paymentsds.mpesa.Callback;
import org.paymentsds.mpesa.Client;
import org.paymentsds.mpesa.Environment;
import org.paymentsds.mpesa.Request;
import org.paymentsds.mpesa.Response;

Client client = new Client.Builder()
    .apiKey("<REPLACE>")
    .publicKey("<REPLACE>")
    .serviceProviderCode("<REPLACE>")
    .initiatorIdentifier("<REPLACE>")
    .environment(Environment.PRODUCTION)
    .host("https://vm.co.mz")
    .build();    

Request paymentRequest = new Request.Builder()
    .amount(10.0)
    .from("841234567")
    .reference("12345")
    .transaction("12345")
    .build();

// Synchronous Call
try {
    Response response = client.receive(paymentRequest);
    // Handle success scenario
} catch (Exception e) {
    // Handle failure scenario
}

// Asynchronous Call
client.receive(paymentRequest, new Callback() {
    @Override
    public void onResponse(Response response) {
        // Handle success scenario
    }
    
    @Override
    public void onError(Exception e) {
        // Handle failure scenario
    }
});

Send Money to a Mobile Account

import org.paymentsds.mpesa.Callback;
import org.paymentsds.mpesa.Client;
import org.paymentsds.mpesa.Environment;
import org.paymentsds.mpesa.Request;
import org.paymentsds.mpesa.Response;

Client client = new Client.Builder()
    .apiKey("<REPLACE>")
    .publicKey("<REPLACE>")
    .serviceProviderCode("<REPLACE>")
    .initiatorIdentifier("<REPLACE>")
    .environment(Environment.PRODUCTION)
    .host("https://vm.co.mz")
    .build();    

Request paymentIntent = new Request.Builder()
    .amount(10.0)
    .to("841234567")
    .reference("12345")
    .transaction("12345")
    .build();

// Synchronous Call
try {
    Response response = client.send(paymentRequest);
    // Handle success scenario
} catch (Exception e) {
    // Handle failure scenario
}

// Asynchronous Call
client.send(paymentRequest, new Callback() {
    @Override
    public void onResponse(Response response) {
        // Handle success scenario
    }
    
    @Override
    public void onError(Exception e) {
        // Handle failure scenario
    }
});

Send Money to a Business Account

import org.paymentsds.mpesa.Callback;
import org.paymentsds.mpesa.Client;
import org.paymentsds.mpesa.Environment;
import org.paymentsds.mpesa.Request;
import org.paymentsds.mpesa.Response;

Client client = new Client.Builder()
    .apiKey("<REPLACE>")
    .publicKey("<REPLACE>")
    .serviceProviderCode("<REPLACE>")
    .initiatorIdentifier("<REPLACE>")
    .environment(Environment.PRODUCTION)
    .host("https://vm.co.mz")
    .build();    

Request paymentIntent = new Request.Builder()
    .amount(10.0)
    .to("54321")
    .reference("12345")
    .transaction("12345")
    .build();

// Synchronous Call
try {
    Response response = client.send(paymentRequest);
    // Handle success scenario
} catch (Exception e) {
    // Handle failure scenario
}

// Asynchronous Call
client.send(paymentRequest, new Callback() {
    @Override
    public void onResponse(Response response) {
        // Handle success scenario
    }
    
    @Override
    public void onError(Exception e) {
        // Handle failure scenario
    }
});

Revert a Transaction

import org.paymentsds.mpesa.Callback;
import org.paymentsds.mpesa.Client;
import org.paymentsds.mpesa.Environment;
import org.paymentsds.mpesa.Request;
import org.paymentsds.mpesa.Response;

Client client = new Client.Builder()
    .apiKey("<REPLACE>")
    .publicKey("<REPLACE>")
    .serviceProviderCode("<REPLACE>")
    .initiatorIdentifier("<REPLACE>")
    .environment(Environment.PRODUCTION)
    .host("https://vm.co.mz")
    .securityCredential("<REPLACE>")
    .build();    

Request reversalIntent = new Request.Builder()
    .amount(10.0)
    .reference("12345")
    .transaction("12345")
    .build();

// Synchronous Call
try {
    Response response = client.revert(reversalRequest);
    // Handle success scenario
} catch (Exception e) {
    // Handle failure scenario
}

// Asynchronous Call
client.revert(reversalRequest, new Callback() {
    @Override
    public void onResponse(Response response) {
        // Handle success scenario
    }
    
    @Override
    public void onError(Exception e) {
        // Handle failure scenario
    }
});

Query the status of a Transaction

import org.paymentsds.mpesa.Callback;
import org.paymentsds.mpesa.Client;
import org.paymentsds.mpesa.Environment;
import org.paymentsds.mpesa.Intent;
import org.paymentsds.mpesa.Response;

Client client = new Client.Builder()
    .apiKey("<REPLACE>")
    .publicKey("<REPLACE>")
    .serviceProviderCode("<REPLACE>")
    .build();    

Request queryIntent = new Request.Builder()
    .reference("12345") // input_ThirdPartyReference
    .subject("12345") // input_QueryReference
    .build();

// Synchronous Call
try {
    Response response = client.query(queryRequest);
    // Handle success scenario
} catch (Exception e) {
    // Handle failure scenario
}

// Asynchronous Call
client.query(queryRequest, new Callback() {
    @Override
    public void onResponse(Response response) {
        // Handle success scenario
    }
    
    @Override
    public void onError(Exception e) {
        // Handle failure scenario
    }
});

Installation

Using Composer

Manual Installation

Friends

Contributing

Changelog

Authors

Credits

License

Copyright 2020 Anísio Mandlate, Edson Michaque, Elton Laice and Nélio Macombo

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.