Git Product home page Git Product logo

mpesa-java-sdk-v2's Introduction

Mpesa Java SDK

Build Status Maven Central

A Java library for communicating with Mpesa Daraja rest API.

The SDK provides convenient access to the Mpesa Daraja API from applications written in Java.

Install

Maven

Use the following dependency in your project to grab via Maven:

 <dependency>
  <groupId>com.github.corneliouzbett</groupId>
  <artifactId>mpesa-sdk</artifactId>
  <version>1.1</version>
  <type>pom</type>
</dependency>

Gradle

For android developers you can install the library by adding dependency to build.gradle

implementation 'com.github.corneliouzbett:mpesa-sdk:1.1'

Gradle for Kotlin

For kotlin developers

implementation("com.github.corneliouzbett:mpesa-sdk:1.1")

Scala SBT

For developers using scala

libraryDependencies += "com.github.corneliouzbett" % "mpesa-sdk" % "1.1"

Usage

The SDK needs to be initialized with your consumer key and consumer secret, which you get from the safaricom developers portal. You are required to create an app.

You can use this SDK for either production or sandbox apps.

Initialize the client

  String consumerKey = "AXXXX-XXXX"; //consumer key
  String consumerSecret = "WEDX-XXXX-XXX-XXX"; //consumer secret

  Mpesa mpesa = new MpesaClient(consumerKey, consumerSecret); // initializing mpesa client

Authentication

Authenticate mpesa client and genarate accessToken used to access authorized resources (services) e.g C2B, B2C, MExpress, AccountBalance etc

mpesa.authenticate().getAccessToken().enqueue(new Callback<>() {

			@Override
			public void onResponse(Call<AccessToken> call, Response<AccessToken> response) {
				if (response.isSuccessful()) {
					String accessToken = response.body().getAccessToken();
				}
			}

			@Override
			public void onFailure(Call<AccessToken> call, Throwable throwable) {
				throw new MpesaException("Unable to authenticate Mpesa client", throwable);
			}
		});

Account Balance

This API entrypoint requests for the account balance of a shortcode (Till number).

  AccountBalance balance = new AccountBalance();
		balance.setCommandId("");
		balance.setInitiator("");
		balance.setIdentifierType("");
		balance.setPartyA("");
		balance.setQueueTimeOutURL("");
		balance.setResultUrl("");
		balance.setRemarks("");
		balance.setSecurityCredentials("");
  

MExpress

For Lipa Na M-Pesa payment using STK Push

  • Check status of lipa na mpesa payment
OnlinePaymentStatus paymentStatus =
   			new OnlinePaymentStatus(0000, "",
   					Timestamp.valueOf(LocalDateTime.now()), "");
 
   	mpesa.stkPush("accessToken").checkOnlinePaymentStatus(paymentStatus).enqueue(new Callback<>() {

   		@Override
   		public void onResponse(Call<OnlinePaymentStatusResponse> call, Response<OnlinePaymentStatusResponse> response) {

   		}

   		@Override
   		public void onFailure(Call<OnlinePaymentStatusResponse> call, Throwable throwable) {

   		}
   	});
  • Initiate stk push
 OnlinePayment onlinePayment = new OnlinePayment();
   	onlinePayment.setAccountReference("AccRef");
   	onlinePayment.setBusinessShortCode(123);
   	onlinePayment.setCallbackURL("callbackUrl");
   	onlinePayment.setAmount("amount");
   	onlinePayment.setPartyA("partyA");
   	onlinePayment.setPartyB("partyB");
   	onlinePayment.setPhoneNumber("phoneNumber");
   	onlinePayment.setPassword("passkey");
   	onlinePayment.setTimestamp("timestamp");
   	onlinePayment.setTransactionType("transaction type");
   	onlinePayment.setTransactionDescription("transaction desc");
   	
   	mpesa.stkPush("accessToken").initiateOnlinePayment(onlinePayment).enqueue(new Callback<OnlinePaymentResponse>() {

   		@Override
   		public void onResponse(Call<OnlinePaymentResponse> call, Response<OnlinePaymentResponse> response) {

   		}

   		@Override
   		public void onFailure(Call<OnlinePaymentResponse> call, Throwable throwable) {

   		}
   	});

C2B

This API entrypoint enables Paybill and Buy Goods merchants to integrate to M-Pesa and receive real time payments notifications

B2C

This API entrypoint enables Business to Customer (B2C) transactions between a company and customers who are the end-users of its products or services

Note

This Mpesa SDK is currently under active development, so expect rapid changes

License

MIT License

Copyright (c) 2020 corneliouz bett

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

mpesa-java-sdk-v2's People

Contributors

corneliouzbett avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

Forkers

austinegwa

mpesa-java-sdk-v2's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.