# Mpesa SDK for Java
A modern, robust, and easy-to-use Java SDK for the Safaricom M-Pesa Daraja API.
This SDK provides a clean, fluent interface for M-Pesa APIs, starting with the widely used M-Pesa Express (STK Push). It is designed to be lightweight, thread-safe, and highly configurable, making it suitable for any Java or Kotlin application.
## Features
* **Clean & Modern API:** A simple, intuitive client for all M-Pesa operations.
* **Thread-Safe:** Safe for use in multi-threaded server environments.
* **Automatic Authentication:** Handles OAuth token acquisition and caching automatically.
* **Robust Error Handling:** Clear, specific exceptions for API and network errors.
* **Immutable Models:** All data models are immutable for predictable state management.
* **Testable:** Designed with dependency injection for easy mocking and testing.
## Installation
This SDK is published to Maven Central. You can add it to your project using Maven or Gradle.
### Maven
Add this to your `pom.xml`:
```xml
io.github.openpaydev
mpesa-sdk-java
1.0.0
```
### Gradle
Add this to your `build.gradle` file:
```groovy
implementation 'io.github.openpaydev:mpesa-sdk-java:1.0.0' // Use the latest version
```
## Configuration
The SDK is configured via environment variables, which is a best practice for security. Before using the client, you must set the following:
* **MPESA_ENVIRONMENT:** The environment to use. Either SANDBOX (default) or PRODUCTION.
* **MPESA_CONSUMER_KEY:** Your app's consumer key from the Daraja portal.
* **MPESA_CONSUMER_SECRET:** Your app's consumer secret.
* **MPESA_SHORTCODE:** Your business shortcode (PayBill or Till Number).
* **MPESA_PASSKEY:** Your Lipa Na M-Pesa online passkey.
## Quick Start: Initiating an STK Push
Here is a complete example of how to configure the client and initiate a payment.
``` java
import io.github.openpaydev.mpesa.MpesaClient;
import io.github.openpaydev.mpesa.auth.MpesaTokenManager;
import io.github.openpaydev. …