项目作者: corneliouzbett

项目描述 :
A Java library for communicating with Mpesa Daraja rest API
高级语言: Java
项目地址: git://github.com/corneliouzbett/mpesa-java-sdk-v2.git
创建时间: 2020-02-18T12:07:35Z
项目社区:https://github.com/corneliouzbett/mpesa-java-sdk-v2

开源协议:MIT License

下载


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:

  1. <dependency>
  2. <groupId>com.github.corneliouzbett</groupId>
  3. <artifactId>mpesa-sdk</artifactId>
  4. <version>1.1</version>
  5. <type>pom</type>
  6. </dependency>

Gradle

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

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

Gradle for Kotlin

For kotlin developers

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

Scala SBT

For developers using scala

  1. 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

  1. String consumerKey = "AXXXX-XXXX"; //consumer key
  2. String consumerSecret = "WEDX-XXXX-XXX-XXX"; //consumer secret
  3. 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

  1. mpesa.authenticate().getAccessToken().enqueue(new Callback<>() {
  2. @Override
  3. public void onResponse(Call<AccessToken> call, Response<AccessToken> response) {
  4. if (response.isSuccessful()) {
  5. String accessToken = response.body().getAccessToken();
  6. }
  7. }
  8. @Override
  9. public void onFailure(Call<AccessToken> call, Throwable throwable) {
  10. throw new MpesaException("Unable to authenticate Mpesa client", throwable);
  11. }
  12. });

Account Balance

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

  1. AccountBalance balance = new AccountBalance();
  2. balance.setCommandId("");
  3. balance.setInitiator("");
  4. balance.setIdentifierType("");
  5. balance.setPartyA("");
  6. balance.setQueueTimeOutURL("");
  7. balance.setResultUrl("");
  8. balance.setRemarks("");
  9. balance.setSecurityCredentials("");

MExpress

For Lipa Na M-Pesa payment using STK Push

  • Check status of lipa na mpesa payment

    1. OnlinePaymentStatus paymentStatus =
    2. new OnlinePaymentStatus(0000, "",
    3. Timestamp.valueOf(LocalDateTime.now()), "");
    4. mpesa.stkPush("accessToken").checkOnlinePaymentStatus(paymentStatus).enqueue(new Callback<>() {
    5. @Override
    6. public void onResponse(Call<OnlinePaymentStatusResponse> call, Response<OnlinePaymentStatusResponse> response) {
    7. }
    8. @Override
    9. public void onFailure(Call<OnlinePaymentStatusResponse> call, Throwable throwable) {
    10. }
    11. });
  • Initiate stk push

    1. OnlinePayment onlinePayment = new OnlinePayment();
    2. onlinePayment.setAccountReference("AccRef");
    3. onlinePayment.setBusinessShortCode(123);
    4. onlinePayment.setCallbackURL("callbackUrl");
    5. onlinePayment.setAmount("amount");
    6. onlinePayment.setPartyA("partyA");
    7. onlinePayment.setPartyB("partyB");
    8. onlinePayment.setPhoneNumber("phoneNumber");
    9. onlinePayment.setPassword("passkey");
    10. onlinePayment.setTimestamp("timestamp");
    11. onlinePayment.setTransactionType("transaction type");
    12. onlinePayment.setTransactionDescription("transaction desc");
    13. mpesa.stkPush("accessToken").initiateOnlinePayment(onlinePayment).enqueue(new Callback<OnlinePaymentResponse>() {
    14. @Override
    15. public void onResponse(Call<OnlinePaymentResponse> call, Response<OnlinePaymentResponse> response) {
    16. }
    17. @Override
    18. public void onFailure(Call<OnlinePaymentResponse> call, Throwable throwable) {
    19. }
    20. });

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.