This project is a Spring Boot application designed to integrate with Safaricom's Daraja API for M-Pesa services. The application implements various M-Pesa services such as C2B (Customer to Business) transactions, B2C (Business to Customer) payments, STK Push, checking account balance, and transaction status queries.
- Access Token Management: Fetches and manages OAuth 2.0 tokens for authenticating M-Pesa API requests.
- Register URLs: Registers confirmation and validation URLs for M-Pesa C2B transactions.
- Simulate C2B Transactions: Simulates M-Pesa transactions for testing purposes.
- Perform B2C Transactions: Processes business-to-customer payments.
- Transaction Status Query: Fetches the status of M-Pesa transactions.
- STK Push: Initiates an STK push for payments via M-Pesa.
- Check Account Balance: Queries the account balance for the business shortcode.
- Spring Boot: The core framework for building the REST API.
- OkHttp: A client for making HTTP requests to the M-Pesa Daraja API.
- Jackson: For parsing JSON responses.
- Lombok: To reduce boilerplate code (e.g., for logging).
- M-Pesa Daraja API: Safaricom’s M-Pesa API for mobile money services.
- Maven: For managing dependencies and building the project.
- Java 21 or higher installed.
- Maven installed.
- M-Pesa Daraja API account with valid credentials, including:
- Consumer Key
- Consumer Secret
- Shortcode
- B2C Initiator Name and Password
- Passkey for STK Push
-
Clone the repository:
git clone https://github.com/felixojiambo/MpesaBridge.git cd MpesaBridge
-
Configure the application by updating the
MpesaConfig
with your M-Pesa Daraja API credentials:mpesa: consumerKey: YOUR_CONSUMER_KEY consumerSecret: YOUR_CONSUMER_SECRET shortCode: YOUR_SHORT_CODE b2cInitiatorName: YOUR_INITIATOR_NAME b2cInitiatorPassword: YOUR_INITIATOR_PASSWORD stkPushShortCode: YOUR_STK_SHORT_CODE stkPassKey: YOUR_STK_PUSH_PASSKEY oauthEndpoint: https://sandbox.safaricom.co.ke/oauth/v1/generate?grant_type=client_credentials registerUrlEndpoint: https://sandbox.safaricom.co.ke/mpesa/c2b/v1/registerurl simulateTransactionEndpoint: https://sandbox.safaricom.co.ke/mpesa/c2b/v1/simulate b2cTransactionEndpoint: https://sandbox.safaricom.co.ke/mpesa/b2c/v1/paymentrequest transactionResultUrl: https://sandbox.safaricom.co.ke/mpesa/transactionstatus/v1/query checkAccountBalanceUrl: https://sandbox.safaricom.co.ke/mpesa/accountbalance/v1/query stkPushRequestUrl: https://sandbox.safaricom.co.ke/mpesa/stkpush/v1/processrequest lnmQueryRequestUrl: https://sandbox.safaricom.co.ke/mpesa/stkpushquery/v1/query
-
Build the project using Maven:
mvn clean install
-
Run the application:
mvn spring-boot:run
- Get Access Token: Automatically managed by the service.
- URL:
/mobile-money/c2b/simulate
- Method: POST
- Description: Simulates a C2B transaction.
- Request Body:
{ "shortCode": "YOUR_SHORT_CODE", "commandID": "CustomerPayBillOnline", "amount": 1000, "msisdn": "254708374149", "billRefNumber": "12345678" }
- URL:
/mobile-money/b2c/payment
- Method: POST
- Description: Sends payments from the business to a customer.
- Request Body:
{ "amount": 1000, "commandID": "BusinessPayment", "partyB": "254708374149", "remarks": "Payment", "occassion": "Occasion" }
- URL:
/mobile-money/stkpush
- Method: POST
- Description: Initiates an STK push request for mobile payment.
- Request Body:
{ "amount": 1000, "phoneNumber": "254708374149" }
- URL:
/mobile-money/transactionstatus
- Method: POST
- Description: Queries the status of a transaction.
- Request Body:
{ "transactionID": "ABC123DEF456" }
- URL:
/mobile-money/accountbalance
- Method: POST
- Description: Fetches the business account balance.
- HelperUtility: Utility class for encoding data and generating timestamps.
- Constants: Contains static values for HTTP headers, M-Pesa command IDs, and identifiers.
To run unit tests:
mvn test
The application uses Lombok’s @Slf4j
annotation for logging various events such as transaction failures, access token issues, and request/response details.
Feel free to fork the repository and submit pull requests for any bug fixes or feature enhancements.
This project is licensed under the MIT License.