This is open-source mobile payment library with support for M-Pesa, Tigo Pesa, Airtel Money and Halopesa. It provides a simple and intuitive API for integrating mobile payments into your applications.
- Easy integration with Vodacom (M-Pesa), Tigo (Tigo Pesa), Airtel (Airtel Money), PesaPal, SelcomPay, AzamPay and Halotel (HaloPesa)
- Support to various payment methods
- Extensible for other mobile payment providers
- Detailed error handling and logging
Add the following dependency to your build.gradle
file:
dependencies {
implementation 'com.kwawingu:mobile-payment:1.0.0'
}
Add the following dependency to your pom.xml
file:
<dependency>
<groupId>com.kwawingu</groupId>
<artifactId>mobile-payment</artifactId>
<version>1.0.0</version>
</dependency>
First, initialize the library with your M-Pesa credentials from environment variables.
MpesaKeyProviderFromEnvironment.Config config =
new MpesaKeyProviderFromEnvironment.Config.Builder()
.setApiKeyEnvName("MPESA_API_KEY")
.setPublicKeyEnvName("MPESA_PUBLIC_KEY")
.build();
mpesaSessionKeyGenerator = new SessionKeyGenerator();
apiEndpoint = new ApiEndpoint(Environment.SANDBOX, Market.VODACOM_TANZANIA);
keyProvider = new MpesaKeyProviderFromEnvironment(config);
This configuration sets up the MpesaKeyProviderFromEnvironment to retrieve the API key and public key from the specified environment variables. The SessionKeyGenerator and ApiEndpoint are also initialized for generating session keys and defining the API endpoint, respectively.
The C2B API call is used as a standard customer-to-business transaction. Funds from the customer’s mobile money wallet will be deducted and be transferred to the mobile money wallet of the business. To authenticate and authorize this transaction, M-Pesa Payments Gateway will initiate a USSD Push message to the customer to gather and verify the mobile money PIN number. This number is not stored and is used only to authorize the transaction.
Payload customerToBusinessPayload =
new Payload.Builder()
.setAmount("10.00")
.setCustomerMSISDN("+255-762-578-467")
.setCountry(Market.VODACOM_TANZANIA.getInputCountryValue())
.setCurrency(Market.VODACOM_TANZANIA.getInputCurrencyValue())
.setServiceProviderCode("ORG001")
.setTransactionReference("T12344C")
.setThirdPartyConversationID("1e9b774d1da34af78412a498cbc28f5e")
.setPurchasedItemsDesc("Lenovo ThinkPad X1 Carbon Gen 12")
.build();
CustomerToBusinessTransaction customerToBusinessTransaction =
new CustomerToBusinessTransaction.Builder()
.setApiEndpoint(new ApiEndpoint(Environment.SANDBOX, Market.VODACOM_TANZANIA))
.setEncryptedSessionKey(session.getEncryptedSessionKey())
.setPayload(customerToBusinessPayload)
.build();
Contributions are welcome! Please read the CONTRIBUTING.md for guidelines on how to contribute to this project.
This project is licensed under the MIT License - see the LICENSE file for details.