├── src ├── main │ ├── resources │ │ ├── .gitignore │ │ ├── application.yml │ │ └── application-local.yml │ └── java │ │ └── com │ │ └── example │ │ └── mpesaintegrationspringboot │ │ ├── dto │ │ ├── stkcallback │ │ │ ├── Body.java │ │ │ ├── StkPushResultBody.java │ │ │ ├── StkPushResult.java │ │ │ └── StkCallback.java │ │ ├── AccessToken.java │ │ ├── QrCodeResponse.java │ │ ├── RegisterUrlResponse.java │ │ ├── ConfirmationValidationResponse.java │ │ ├── StkPushResponse.java │ │ ├── QrCodeRequest.java │ │ ├── StkPushQuery.java │ │ ├── RegisterUrlRequest.java │ │ ├── PaymentRequest.java │ │ ├── StkPushQueryResponse.java │ │ ├── StkPushPayload.java │ │ └── ConfirmationValidationDto.java │ │ ├── utils │ │ └── MpesaPaymentStatus.java │ │ ├── repository │ │ ├── MpesaPaymentRepository.java │ │ └── MpesaExpressRepository.java │ │ ├── http │ │ ├── MpesaAuthClient.java │ │ └── MpesaClient.java │ │ ├── MpesaIntegrationSpringbootApplication.java │ │ ├── controller │ │ ├── MpesaTokenController.java │ │ ├── MpesaExpressController.java │ │ └── MpesaPaymentController.java │ │ ├── config │ │ ├── AuthClientConfig.java │ │ └── MpesaClientConfig.java │ │ ├── entity │ │ ├── MpesaPayment.java │ │ └── MpesaExpress.java │ │ └── service │ │ ├── MpesaAuthenticationService.java │ │ ├── MpesaPaymentService.java │ │ └── MpesaExpressService.java └── test │ └── java │ └── com │ └── example │ └── mpesaintegrationspringboot │ └── MpesaIntegrationSpringbootApplicationTests.java ├── .mvn └── wrapper │ ├── maven-wrapper.jar │ └── maven-wrapper.properties ├── docker-compose.yml ├── .gitignore ├── README.md ├── pom.xml ├── mvnw.cmd ├── mpesa integration.postman_collection.json └── mvnw /src/main/resources/.gitignore: -------------------------------------------------------------------------------- 1 | application-local.yml -------------------------------------------------------------------------------- /.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/murethi/mpesa-integration-springboot/HEAD/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | jpa: 3 | hibernate: 4 | ddl-auto: update 5 | profiles: 6 | active: local 7 | 8 | -------------------------------------------------------------------------------- /src/main/java/com/example/mpesaintegrationspringboot/dto/stkcallback/Body.java: -------------------------------------------------------------------------------- 1 | package com.example.mpesaintegrationspringboot.dto.stkcallback; 2 | 3 | public record Body(StkCallback stkCallback) { 4 | } 5 | -------------------------------------------------------------------------------- /src/main/java/com/example/mpesaintegrationspringboot/dto/AccessToken.java: -------------------------------------------------------------------------------- 1 | package com.example.mpesaintegrationspringboot.dto; 2 | 3 | 4 | public record AccessToken(String access_token,long expires_in) { 5 | } 6 | -------------------------------------------------------------------------------- /src/main/java/com/example/mpesaintegrationspringboot/dto/stkcallback/StkPushResultBody.java: -------------------------------------------------------------------------------- 1 | package com.example.mpesaintegrationspringboot.dto.stkcallback; 2 | 3 | public record StkPushResultBody(Body Body) { 4 | } 5 | -------------------------------------------------------------------------------- /src/main/java/com/example/mpesaintegrationspringboot/dto/stkcallback/StkPushResult.java: -------------------------------------------------------------------------------- 1 | package com.example.mpesaintegrationspringboot.dto.stkcallback; 2 | 3 | 4 | public record StkPushResult(StkPushResultBody Body) { 5 | 6 | } 7 | -------------------------------------------------------------------------------- /src/main/java/com/example/mpesaintegrationspringboot/dto/QrCodeResponse.java: -------------------------------------------------------------------------------- 1 | package com.example.mpesaintegrationspringboot.dto; 2 | 3 | public record QrCodeResponse(String ResponseCode,String RequestID,String ResponseDescription,String QRCode) { 4 | 5 | } 6 | -------------------------------------------------------------------------------- /src/main/java/com/example/mpesaintegrationspringboot/dto/RegisterUrlResponse.java: -------------------------------------------------------------------------------- 1 | package com.example.mpesaintegrationspringboot.dto; 2 | 3 | public record RegisterUrlResponse(String OriginatorCoversationID,String ResponseCode,String ResponseDescription) { 4 | } 5 | -------------------------------------------------------------------------------- /.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.5/apache-maven-3.9.5-bin.zip 2 | wrapperUrl=https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.2.0/maven-wrapper-3.2.0.jar 3 | -------------------------------------------------------------------------------- /src/main/java/com/example/mpesaintegrationspringboot/dto/stkcallback/StkCallback.java: -------------------------------------------------------------------------------- 1 | package com.example.mpesaintegrationspringboot.dto.stkcallback; 2 | 3 | public record StkCallback(String MerchantRequestID,String CheckoutRequestID,String ResultCode,String ResultDesc) { 4 | } 5 | -------------------------------------------------------------------------------- /src/main/java/com/example/mpesaintegrationspringboot/dto/ConfirmationValidationResponse.java: -------------------------------------------------------------------------------- 1 | package com.example.mpesaintegrationspringboot.dto; 2 | 3 | import lombok.Builder; 4 | 5 | @Builder 6 | public record ConfirmationValidationResponse(String ResultCode, String ResultDesc) { 7 | } 8 | -------------------------------------------------------------------------------- /src/main/java/com/example/mpesaintegrationspringboot/dto/StkPushResponse.java: -------------------------------------------------------------------------------- 1 | package com.example.mpesaintegrationspringboot.dto; 2 | 3 | public record StkPushResponse(String MerchantRequestID,String CheckoutRequestID,int ResponseCode,String ResponseDescription,String CustomerMessage) { 4 | 5 | } 6 | -------------------------------------------------------------------------------- /src/main/java/com/example/mpesaintegrationspringboot/dto/QrCodeRequest.java: -------------------------------------------------------------------------------- 1 | package com.example.mpesaintegrationspringboot.dto; 2 | 3 | import java.math.BigDecimal; 4 | 5 | public record QrCodeRequest(String MerchantName, String RefNo, BigDecimal Amount,String TrxCode,String CPI,int Size) { 6 | 7 | } 8 | -------------------------------------------------------------------------------- /src/main/java/com/example/mpesaintegrationspringboot/dto/StkPushQuery.java: -------------------------------------------------------------------------------- 1 | package com.example.mpesaintegrationspringboot.dto; 2 | 3 | import lombok.Builder; 4 | 5 | @Builder 6 | public record StkPushQuery(String BusinessShortCode, String Password, String Timestamp,String CheckoutRequestID) { 7 | } 8 | -------------------------------------------------------------------------------- /src/main/resources/application-local.yml: -------------------------------------------------------------------------------- 1 | app: 2 | integrations: 3 | mpesa: 4 | consumer-key: YOUR_ACCESS_KEY 5 | consumer-secret: YOUR_ACCESS_TOKEN 6 | domain: http://sandbox.safaricom.co.ke 7 | shortCode: MPESA_SHORT_CODE 8 | express: 9 | shortCode: MPESA_SHORT_CODE -------------------------------------------------------------------------------- /src/main/java/com/example/mpesaintegrationspringboot/dto/RegisterUrlRequest.java: -------------------------------------------------------------------------------- 1 | package com.example.mpesaintegrationspringboot.dto; 2 | 3 | import lombok.Builder; 4 | 5 | @Builder 6 | public record RegisterUrlRequest(String ShortCode,String ResponseType,String ConfirmationURL,String ValidationURL) { 7 | } 8 | -------------------------------------------------------------------------------- /src/main/java/com/example/mpesaintegrationspringboot/utils/MpesaPaymentStatus.java: -------------------------------------------------------------------------------- 1 | package com.example.mpesaintegrationspringboot.utils; 2 | 3 | import lombok.RequiredArgsConstructor; 4 | 5 | @RequiredArgsConstructor 6 | public enum MpesaPaymentStatus { 7 | VALIDATED("VALIDATED"), 8 | CONFIRMED("CONFIRMED"); 9 | 10 | public final String value; 11 | 12 | 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/example/mpesaintegrationspringboot/repository/MpesaPaymentRepository.java: -------------------------------------------------------------------------------- 1 | package com.example.mpesaintegrationspringboot.repository; 2 | 3 | 4 | import com.example.mpesaintegrationspringboot.entity.MpesaPayment; 5 | import org.springframework.data.jpa.repository.JpaRepository; 6 | 7 | public interface MpesaPaymentRepository extends JpaRepository { 8 | } 9 | -------------------------------------------------------------------------------- /src/test/java/com/example/mpesaintegrationspringboot/MpesaIntegrationSpringbootApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.example.mpesaintegrationspringboot; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class MpesaIntegrationSpringbootApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/example/mpesaintegrationspringboot/http/MpesaAuthClient.java: -------------------------------------------------------------------------------- 1 | package com.example.mpesaintegrationspringboot.http; 2 | 3 | import com.example.mpesaintegrationspringboot.dto.AccessToken; 4 | import org.springframework.web.service.annotation.GetExchange; 5 | 6 | public interface MpesaAuthClient { 7 | @GetExchange("/oauth/v1/generate?grant_type=client_credentials") 8 | AccessToken generateToken(); 9 | 10 | 11 | } 12 | -------------------------------------------------------------------------------- /src/main/java/com/example/mpesaintegrationspringboot/dto/PaymentRequest.java: -------------------------------------------------------------------------------- 1 | package com.example.mpesaintegrationspringboot.dto; 2 | 3 | import java.math.BigDecimal; 4 | 5 | public record PaymentRequest(BigDecimal amount, 6 | String phoneNumber, 7 | String accountReference, 8 | String transactionDescription, 9 | String callbackUrl 10 | ) { 11 | } 12 | -------------------------------------------------------------------------------- /src/main/java/com/example/mpesaintegrationspringboot/dto/StkPushQueryResponse.java: -------------------------------------------------------------------------------- 1 | package com.example.mpesaintegrationspringboot.dto; 2 | 3 | public record StkPushQueryResponse(int ResponseCode, 4 | String ResponseDescription, 5 | String MerchantRequestID, 6 | String CheckoutRequestID, 7 | 8 | String ResultCode, 9 | String ResultDesc) { 10 | } 11 | -------------------------------------------------------------------------------- /src/main/java/com/example/mpesaintegrationspringboot/MpesaIntegrationSpringbootApplication.java: -------------------------------------------------------------------------------- 1 | package com.example.mpesaintegrationspringboot; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class MpesaIntegrationSpringbootApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(MpesaIntegrationSpringbootApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: '3' 2 | services: 3 | db: 4 | container_name: mpesa-integration 5 | image: mysql:8.0 6 | command: mysqld --default-authentication-plugin=mysql_native_password --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci 7 | environment: 8 | MYSQL_ROOT_PASSWORD: 'admin@Database' 9 | MYSQL_DATABASE: 'mpesa_integration' 10 | MYSQL_USER: 'myuser' 11 | MYSQL_PASSWORD: 'admin@Database' 12 | MYSQL_ALLOW_EMPTY_PASSWORD: "no" 13 | ports: 14 | - '3308:3306' -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | HELP.md 2 | target/ 3 | !.mvn/wrapper/maven-wrapper.jar 4 | !**/src/main/**/target/ 5 | !**/src/test/**/target/ 6 | 7 | ### STS ### 8 | .apt_generated 9 | .classpath 10 | .factorypath 11 | .project 12 | .settings 13 | .springBeans 14 | .sts4-cache 15 | 16 | ### IntelliJ IDEA ### 17 | .idea 18 | *.iws 19 | *.iml 20 | *.ipr 21 | 22 | ### NetBeans ### 23 | /nbproject/private/ 24 | /nbbuild/ 25 | /dist/ 26 | /nbdist/ 27 | /.nb-gradle/ 28 | build/ 29 | !**/src/main/**/build/ 30 | !**/src/test/**/build/ 31 | 32 | ### VS Code ### 33 | .vscode/ 34 | -------------------------------------------------------------------------------- /src/main/java/com/example/mpesaintegrationspringboot/repository/MpesaExpressRepository.java: -------------------------------------------------------------------------------- 1 | package com.example.mpesaintegrationspringboot.repository; 2 | 3 | import com.example.mpesaintegrationspringboot.entity.MpesaExpress; 4 | import org.springframework.data.jpa.repository.JpaRepository; 5 | 6 | import java.util.Optional; 7 | import java.util.UUID; 8 | 9 | public interface MpesaExpressRepository extends JpaRepository { 10 | Optional findByCheckoutRequestIdAndMerchantRequestId(String checkoutRequestId,String erchantRequestId); 11 | } 12 | -------------------------------------------------------------------------------- /src/main/java/com/example/mpesaintegrationspringboot/dto/StkPushPayload.java: -------------------------------------------------------------------------------- 1 | package com.example.mpesaintegrationspringboot.dto; 2 | 3 | import lombok.Builder; 4 | 5 | import java.math.BigDecimal; 6 | 7 | @Builder 8 | public record StkPushPayload(String BusinessShortCode,String Password,String Timestamp, 9 | String TransactionType, BigDecimal Amount, 10 | String PartyA,String PartyB,String PhoneNumber, 11 | String CallBackURL, String AccountReference, String TransactionDesc 12 | ) { 13 | 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/com/example/mpesaintegrationspringboot/http/MpesaClient.java: -------------------------------------------------------------------------------- 1 | package com.example.mpesaintegrationspringboot.http; 2 | 3 | 4 | import com.example.mpesaintegrationspringboot.dto.*; 5 | import org.springframework.web.bind.annotation.RequestBody; 6 | import org.springframework.web.service.annotation.PostExchange; 7 | 8 | public interface MpesaClient { 9 | @PostExchange("/mpesa/c2b/v1/registerurl") 10 | RegisterUrlResponse registerUrl(@RequestBody RegisterUrlRequest registerUrlRequest); 11 | @PostExchange("/mpesa/stkpush/v1/processrequest") 12 | StkPushResponse mpesaExpress(@RequestBody StkPushPayload stkPushPayload); 13 | 14 | @PostExchange("/mpesa/stkpushquery/v1/query") 15 | StkPushQueryResponse mpesaExpressQuery(@RequestBody StkPushQuery stkPushQuery); 16 | 17 | 18 | @PostExchange("/mpesa/qrcode/v1/generate") 19 | QrCodeResponse generateQrCode(@RequestBody QrCodeRequest qrCodeRequest); 20 | } 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # mpesa-integration-springboot 2 | This is a demo project on how to integrate to mpesa using springboot. 3 | 4 | # Environment Requirements 5 | * Java 17 6 | * Docker 7 | 8 | # How to run 9 | * Spin up your docker-engine 10 | * run the following command on the project root mvn spring-boot:run 11 | 12 | > This project makes use of the springboot docker-compose dependancy to automatically configure database settings. 13 | 14 | # This repo covers the following M-Pesa APIs 15 | * C2B Register URLs 16 | * C2B Validate Payments 17 | * C2B Confirm Payment 18 | * M-Pesa Express (STK PUSH / NI PUSH) 19 | * M-PESA Express Callback 20 | * Mpesa Express Query Transaction 21 | * Generate QR Code 22 | 23 | 24 | # Resources 25 | * [Safaricom Developer Portal (Daraja)](https://developer.safaricom.co.ke/Documentation) 26 | * [Postman collection - Sample payload you can use to test APIS] (https://github.com/murethi/mpesa-integration-springboot/blob/07-qr-code-generation/mpesa%20integration.postman_collection.json) 27 | -------------------------------------------------------------------------------- /src/main/java/com/example/mpesaintegrationspringboot/controller/MpesaTokenController.java: -------------------------------------------------------------------------------- 1 | package com.example.mpesaintegrationspringboot.controller; 2 | 3 | import com.example.mpesaintegrationspringboot.dto.AccessToken; 4 | import com.example.mpesaintegrationspringboot.service.MpesaAuthenticationService; 5 | import lombok.RequiredArgsConstructor; 6 | import org.springframework.http.HttpStatus; 7 | import org.springframework.web.bind.annotation.GetMapping; 8 | import org.springframework.web.bind.annotation.RequestMapping; 9 | import org.springframework.web.bind.annotation.ResponseStatus; 10 | import org.springframework.web.bind.annotation.RestController; 11 | 12 | @RestController 13 | @RequestMapping("/auth") 14 | @RequiredArgsConstructor 15 | public class MpesaTokenController { 16 | private final MpesaAuthenticationService mpesaAuthenticationService; 17 | @GetMapping("token") 18 | @ResponseStatus(HttpStatus.OK) 19 | String getToken(){ 20 | return mpesaAuthenticationService.accessToken(); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/com/example/mpesaintegrationspringboot/dto/ConfirmationValidationDto.java: -------------------------------------------------------------------------------- 1 | package com.example.mpesaintegrationspringboot.dto; 2 | 3 | 4 | import java.math.BigDecimal; 5 | 6 | /** 7 | * Once a customer makes a payment, mpesa will check whether you have enabled payment validation 8 | * if validation is enabled, 9 | * @param TransactionType 10 | * @param TransID 11 | * @param TransTime 12 | * @param TransAmount 13 | * @param BusinessShortCode 14 | * @param BillRefNumber 15 | * @param InvoiceNumber 16 | * @param OrgAccountBalance 17 | * @param ThirdPartyTransID 18 | * @param FirstName 19 | * @param MiddleName 20 | * @param MiddleName 21 | */ 22 | public record ConfirmationValidationDto(String TransactionType, 23 | String TransID, 24 | String TransTime, 25 | BigDecimal TransAmount, 26 | int BusinessShortCode, String BillRefNumber, 27 | String InvoiceNumber, BigDecimal OrgAccountBalance, String ThirdPartyTransID, 28 | String FirstName, 29 | String MiddleName, 30 | String LastName) { 31 | 32 | 33 | } -------------------------------------------------------------------------------- /src/main/java/com/example/mpesaintegrationspringboot/config/AuthClientConfig.java: -------------------------------------------------------------------------------- 1 | package com.example.mpesaintegrationspringboot.config; 2 | 3 | import com.example.mpesaintegrationspringboot.http.MpesaAuthClient; 4 | import org.springframework.beans.factory.annotation.Value; 5 | import org.springframework.context.annotation.Bean; 6 | import org.springframework.context.annotation.Configuration; 7 | import org.springframework.web.reactive.function.client.WebClient; 8 | import org.springframework.web.reactive.function.client.support.WebClientAdapter; 9 | import org.springframework.web.service.invoker.HttpServiceProxyFactory; 10 | 11 | @Configuration 12 | public class AuthClientConfig { 13 | @Value("${app.integrations.mpesa.consumer-key}") 14 | private String accessKey; 15 | 16 | @Value("${app.integrations.mpesa.consumer-secret}") 17 | private String secretKey; 18 | 19 | @Value("${app.integrations.mpesa.domain}") 20 | private String domain; 21 | @Bean 22 | MpesaAuthClient mpesaAuthClient(){ 23 | WebClient webClient = WebClient.builder() 24 | .baseUrl(domain) 25 | .defaultHeaders(httpHeaders -> httpHeaders.setBasicAuth(accessKey,secretKey)) 26 | .build(); 27 | 28 | HttpServiceProxyFactory factory = HttpServiceProxyFactory.builder(WebClientAdapter.forClient(webClient)).build(); 29 | return factory.createClient(MpesaAuthClient.class); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/com/example/mpesaintegrationspringboot/config/MpesaClientConfig.java: -------------------------------------------------------------------------------- 1 | package com.example.mpesaintegrationspringboot.config; 2 | 3 | import com.example.mpesaintegrationspringboot.http.MpesaClient; 4 | import com.example.mpesaintegrationspringboot.service.MpesaAuthenticationService; 5 | import lombok.RequiredArgsConstructor; 6 | import org.springframework.beans.factory.annotation.Value; 7 | import org.springframework.context.annotation.Bean; 8 | import org.springframework.context.annotation.Configuration; 9 | import org.springframework.web.reactive.function.client.WebClient; 10 | import org.springframework.web.reactive.function.client.support.WebClientAdapter; 11 | import org.springframework.web.service.invoker.HttpServiceProxyFactory; 12 | 13 | @Configuration 14 | @RequiredArgsConstructor 15 | public class MpesaClientConfig { 16 | private final MpesaAuthenticationService mpesaAuthenticationService; 17 | 18 | @Value("${app.integrations.mpesa.domain}") 19 | private String domain; 20 | @Bean 21 | MpesaClient mpesaClient(){ 22 | WebClient webClient = WebClient.builder() 23 | .baseUrl(domain) 24 | .defaultHeader("Authorization",mpesaAuthenticationService.bearerString()) 25 | .build(); 26 | 27 | HttpServiceProxyFactory factory = HttpServiceProxyFactory.builder(WebClientAdapter.forClient(webClient)).build(); 28 | return factory.createClient(MpesaClient.class); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/com/example/mpesaintegrationspringboot/entity/MpesaPayment.java: -------------------------------------------------------------------------------- 1 | package com.example.mpesaintegrationspringboot.entity; 2 | 3 | 4 | import jakarta.persistence.Column; 5 | import jakarta.persistence.Entity; 6 | import jakarta.persistence.Id; 7 | import jakarta.persistence.Table; 8 | import lombok.*; 9 | 10 | import java.math.BigDecimal; 11 | 12 | @Entity 13 | @Table(name = "mpesa_payment") 14 | @Builder 15 | @AllArgsConstructor 16 | @NoArgsConstructor 17 | @Getter 18 | @Setter 19 | public class MpesaPayment { 20 | @Id 21 | @Column(name = "transaction_id",unique = true) 22 | private String TransID; 23 | @Column(name = "transaction_type") 24 | private String TransactionType; 25 | @Column(name = "transaction_time") 26 | private String TransTime; 27 | @Column(name = "transaction_amount") 28 | private BigDecimal TransAmount; 29 | @Column(name = "business_short_code") 30 | private int BusinessShortCode; 31 | @Column(name = "account_number") 32 | private String BillRefNumber; 33 | @Column(name = "invoice_number") 34 | private String InvoiceNumber; 35 | @Column(name = "organisation_balance") 36 | private BigDecimal OrgAccountBalance; 37 | @Column(name = "third_party_transaction_id") 38 | private String ThirdPartyTransID; 39 | @Column(name = "first_name") 40 | private String FirstName; 41 | @Column(name = "middle_name") 42 | private String MiddleName; 43 | @Column(name = "last_name") 44 | private String LastName; 45 | private String status; 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/com/example/mpesaintegrationspringboot/entity/MpesaExpress.java: -------------------------------------------------------------------------------- 1 | package com.example.mpesaintegrationspringboot.entity; 2 | 3 | import jakarta.persistence.*; 4 | import lombok.*; 5 | 6 | import java.util.UUID; 7 | 8 | @Entity 9 | @Table(name = "mpesa_express") 10 | @Getter 11 | @Setter 12 | @NoArgsConstructor 13 | @AllArgsConstructor 14 | @Builder 15 | public class MpesaExpress { 16 | @Id 17 | @GeneratedValue(strategy = GenerationType.UUID) 18 | private UUID id; 19 | @Column(name = "business_short_code") 20 | private int BusinessShortCode; 21 | @Column(name = "timestamp") 22 | private String Timestamp; 23 | @Column(name = "transaction_type") 24 | 25 | private String TransactionType; 26 | @Column(name = "amount") 27 | private String Amount; 28 | @Column(name = "PartyA") 29 | private String PartyA; 30 | @Column(name = "PartyB") 31 | private String PartyB; 32 | @Column(name = "phone_number") 33 | private String PhoneNumber; 34 | @Column(name = "callback_url") 35 | private String CallBackURL; 36 | @Column(name = "account_reference") 37 | private String AccountReference; 38 | @Column(name = "transaction_description") 39 | private String TransactionDesc; 40 | @Column(name = "checkout_request_id",unique = true) 41 | private String checkoutRequestId; 42 | @Column(name = "merchant_request_id",unique = true) 43 | private String merchantRequestId; 44 | private int responseCode; 45 | private String responseDescription; 46 | private String customerMessage; 47 | private String resultCode; 48 | private String resultDescription; 49 | 50 | } 51 | -------------------------------------------------------------------------------- /src/main/java/com/example/mpesaintegrationspringboot/controller/MpesaExpressController.java: -------------------------------------------------------------------------------- 1 | package com.example.mpesaintegrationspringboot.controller; 2 | 3 | import com.example.mpesaintegrationspringboot.dto.PaymentRequest; 4 | import com.example.mpesaintegrationspringboot.dto.StkPushQueryResponse; 5 | import com.example.mpesaintegrationspringboot.dto.StkPushResponse; 6 | import com.example.mpesaintegrationspringboot.dto.stkcallback.StkCallback; 7 | import com.example.mpesaintegrationspringboot.dto.stkcallback.StkPushResultBody; 8 | import com.example.mpesaintegrationspringboot.service.MpesaExpressService; 9 | import lombok.RequiredArgsConstructor; 10 | import org.springframework.http.HttpStatus; 11 | import org.springframework.web.bind.annotation.*; 12 | 13 | import java.util.UUID; 14 | 15 | @RestController 16 | @RequestMapping("/express-payment") 17 | @RequiredArgsConstructor 18 | public class MpesaExpressController { 19 | private final MpesaExpressService mpesaExpressService; 20 | 21 | 22 | @PostMapping 23 | @ResponseStatus(HttpStatus.OK) 24 | StkPushResponse paymentRequest(@RequestBody PaymentRequest paymentRequest){ 25 | return mpesaExpressService.paymentRequest(paymentRequest); 26 | } 27 | 28 | @PostMapping("callback") 29 | @ResponseStatus(HttpStatus.OK) 30 | void callback(@RequestBody StkPushResultBody stkPushResultBody){ 31 | mpesaExpressService.stkpushResult(stkPushResultBody); 32 | } 33 | 34 | @PostMapping("query/{id}") 35 | @ResponseStatus(HttpStatus.OK) 36 | StkPushQueryResponse stkPushPaymentQuery(@PathVariable UUID id){ 37 | return mpesaExpressService.stkPushPaymentQuery(id); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/com/example/mpesaintegrationspringboot/service/MpesaAuthenticationService.java: -------------------------------------------------------------------------------- 1 | package com.example.mpesaintegrationspringboot.service; 2 | 3 | import com.example.mpesaintegrationspringboot.dto.AccessToken; 4 | import com.example.mpesaintegrationspringboot.http.MpesaAuthClient; 5 | import lombok.RequiredArgsConstructor; 6 | import org.springframework.stereotype.Service; 7 | 8 | import java.time.LocalDateTime; 9 | import java.util.HashMap; 10 | 11 | @Service 12 | @RequiredArgsConstructor 13 | public class MpesaAuthenticationService { 14 | private final MpesaAuthClient mpesaAuthClient; 15 | //this map will be used to store the access token in memory 16 | HashMap tokenData = new HashMap<>(); 17 | 18 | public String accessToken() { 19 | if (tokenData != null && tokenData.containsKey("access_token") && tokenData.containsKey("expires_in")) { 20 | //the token expires in 1 hour 21 | // we regenerate it if it has a minute or less before it expires. 22 | if (LocalDateTime.parse(tokenData.get("expires_in")).isAfter(LocalDateTime.now().plusSeconds(60L))) { 23 | return tokenData.get("access_token"); 24 | } 25 | } 26 | 27 | return generateToken(); 28 | } 29 | 30 | private String generateToken() { 31 | AccessToken accessToken = mpesaAuthClient.generateToken(); 32 | tokenData.put("access_token", accessToken.access_token()); 33 | tokenData.put("expires_in", LocalDateTime.now().plusSeconds(accessToken.expires_in()).toString()); 34 | return accessToken.access_token(); 35 | } 36 | 37 | /** 38 | * this method 39 | * @return 40 | */ 41 | public String bearerString(){ 42 | return "Bearer "+accessToken(); 43 | } 44 | } -------------------------------------------------------------------------------- /src/main/java/com/example/mpesaintegrationspringboot/controller/MpesaPaymentController.java: -------------------------------------------------------------------------------- 1 | package com.example.mpesaintegrationspringboot.controller; 2 | 3 | import com.example.mpesaintegrationspringboot.dto.*; 4 | import com.example.mpesaintegrationspringboot.service.MpesaPaymentService; 5 | import lombok.RequiredArgsConstructor; 6 | import org.springframework.http.HttpStatus; 7 | import org.springframework.web.bind.annotation.*; 8 | 9 | @RestController 10 | @RequestMapping("/payment") 11 | @RequiredArgsConstructor 12 | public class MpesaPaymentController { 13 | private final MpesaPaymentService mpesaPaymentService; 14 | 15 | 16 | /** 17 | * This is the URL that is only used when a Merchant 18 | * (Partner) requires to validate the 19 | * details of the payment before accepting. 20 | * For example, a bank would want to verify if an account number exists 21 | * in their platform before accepting a payment from the customer. 22 | * @param payment 23 | * @return 24 | */ 25 | @PostMapping("validate") 26 | @ResponseStatus(HttpStatus.OK) 27 | ConfirmationValidationResponse validatePayment(@RequestBody ConfirmationValidationDto payment){ 28 | return mpesaPaymentService.validatePayment(payment); 29 | } 30 | 31 | /** 32 | * This is the URL that receives payment notification once payment has been 33 | * completed successfully on M-PESA. 34 | * @param payment 35 | * @return 36 | */ 37 | @PostMapping("confirm") 38 | @ResponseStatus(HttpStatus.OK) 39 | ConfirmationValidationResponse confirmPayment(@RequestBody ConfirmationValidationDto payment){ 40 | return mpesaPaymentService.confirmPayment(payment); 41 | } 42 | 43 | @PostMapping("register-urls") 44 | @ResponseStatus(HttpStatus.OK) 45 | RegisterUrlResponse registerUrls(@RequestBody RegisterUrlRequest registerUrlRequest){ 46 | return mpesaPaymentService.registerUrl(registerUrlRequest); 47 | } 48 | 49 | @PostMapping("/generate-qr") 50 | @ResponseStatus(HttpStatus.OK) 51 | QrCodeResponse generateQrCode(@RequestBody QrCodeRequest qrCodeRequest){ 52 | return mpesaPaymentService.generateQrCode(qrCodeRequest); 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 3.1.5 9 | 10 | 11 | com.example 12 | mpesa-integration-springboot 13 | 0.0.1-SNAPSHOT 14 | mpesa-integration-springboot 15 | mpesa-integration-springboot 16 | 17 | 17 18 | 19 | 20 | 21 | org.springframework.boot 22 | spring-boot-starter-actuator 23 | 24 | 25 | org.springframework.boot 26 | spring-boot-starter-data-jpa 27 | 28 | 29 | org.springframework.boot 30 | spring-boot-starter-web 31 | 32 | 33 | org.springframework.boot 34 | spring-boot-starter-webflux 35 | 36 | 37 | 38 | org.springframework.boot 39 | spring-boot-devtools 40 | runtime 41 | true 42 | 43 | 44 | org.springframework.boot 45 | spring-boot-docker-compose 46 | runtime 47 | true 48 | 49 | 50 | com.mysql 51 | mysql-connector-j 52 | runtime 53 | 54 | 55 | org.projectlombok 56 | lombok 57 | true 58 | 59 | 60 | org.springframework.boot 61 | spring-boot-starter-test 62 | test 63 | 64 | 65 | io.projectreactor 66 | reactor-test 67 | test 68 | 69 | 70 | 71 | 72 | 73 | 74 | org.springframework.boot 75 | spring-boot-maven-plugin 76 | 77 | 78 | paketobuildpacks/builder-jammy-base:latest 79 | 80 | 81 | 82 | org.projectlombok 83 | lombok 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | -------------------------------------------------------------------------------- /src/main/java/com/example/mpesaintegrationspringboot/service/MpesaPaymentService.java: -------------------------------------------------------------------------------- 1 | package com.example.mpesaintegrationspringboot.service; 2 | 3 | import com.example.mpesaintegrationspringboot.dto.*; 4 | import com.example.mpesaintegrationspringboot.entity.MpesaPayment; 5 | import com.example.mpesaintegrationspringboot.http.MpesaClient; 6 | import com.example.mpesaintegrationspringboot.repository.MpesaPaymentRepository; 7 | import com.example.mpesaintegrationspringboot.utils.MpesaPaymentStatus; 8 | import lombok.RequiredArgsConstructor; 9 | import org.springframework.beans.BeanUtils; 10 | import org.springframework.stereotype.Service; 11 | 12 | import java.util.List; 13 | 14 | @Service 15 | @RequiredArgsConstructor 16 | public class MpesaPaymentService { 17 | 18 | private final MpesaPaymentRepository mpesaPaymentRepository; 19 | private final MpesaClient mpesaClient; 20 | 21 | //This is a list of account numbers that we have registered on out application 22 | private List allowedAccounts = List.of("ACCOUNT_ONE","ACCOUNT_TWO","ACCOUNT_THREE"); 23 | 24 | /** 25 | * on validation logic, I check whether the account is present 26 | * I check whether this payment has already been recorded if yes, we return response code 0 27 | * otherwise we record the apyment and return response code 0 28 | * @param payment ConfirmationValidationDto 29 | * @return ConfirmationValidationResponse 30 | */ 31 | public ConfirmationValidationResponse validatePayment(ConfirmationValidationDto payment){ 32 | if(allowedAccounts.contains(payment.BillRefNumber())){ 33 | return mpesaPaymentRepository.findById(payment.TransID()) 34 | .map(mpesaPayment -> { 35 | //We had already registered this transaction 36 | // we return a result code of 0 37 | return ConfirmationValidationResponse.builder() 38 | .ResultCode("0") 39 | .ResultDesc("Accepted") 40 | .build(); 41 | }) 42 | .orElseGet(()->{ 43 | //We record the payment and return a result code of 0 44 | return savePayment(payment); 45 | 46 | }); 47 | }else{ 48 | return ConfirmationValidationResponse.builder() 49 | .ResultCode("C2B00012") 50 | .ResultDesc("Rejected") 51 | .build(); 52 | } 53 | 54 | 55 | } 56 | 57 | /** 58 | * for confirmation, we check if payment had been validated 59 | * we go ahead and mark the transaction as confirmed 60 | * @param payment 61 | * @return ConfirmationValidationResponse 62 | */ 63 | public ConfirmationValidationResponse confirmPayment(ConfirmationValidationDto payment){ 64 | if(allowedAccounts.contains(payment.BillRefNumber())){ 65 | return mpesaPaymentRepository.findById(payment.TransID()) 66 | .map(mpesaPayment -> { 67 | //Payment had already been validated 68 | mpesaPayment.setStatus(MpesaPaymentStatus.CONFIRMED.value); 69 | mpesaPaymentRepository.save(mpesaPayment); 70 | return ConfirmationValidationResponse.builder() 71 | .ResultCode("0") 72 | .ResultDesc("Accepted") 73 | .build(); 74 | }) 75 | .orElseGet(()->{ 76 | // if you require validation you can throw an exception here 77 | //this is because validation has to happen before confirmation 78 | return savePayment(payment); 79 | 80 | }); 81 | }else{ 82 | return ConfirmationValidationResponse.builder() 83 | .ResultCode("C2B00012") 84 | .ResultDesc("Rejected") 85 | .build(); 86 | } 87 | 88 | 89 | } 90 | 91 | public RegisterUrlResponse registerUrl(RegisterUrlRequest registerUrlRequest){ 92 | 93 | return mpesaClient.registerUrl(registerUrlRequest); 94 | } 95 | 96 | 97 | 98 | private ConfirmationValidationResponse savePayment(ConfirmationValidationDto payment){ 99 | MpesaPayment model = new MpesaPayment(); 100 | BeanUtils.copyProperties(payment,model); 101 | model.setStatus(MpesaPaymentStatus.VALIDATED.value); 102 | mpesaPaymentRepository.save(model); 103 | return ConfirmationValidationResponse.builder() 104 | .ResultCode("0") 105 | .ResultDesc("Accepted") 106 | .build(); 107 | } 108 | 109 | public QrCodeResponse generateQrCode(QrCodeRequest qrCodeRequest) { 110 | return mpesaClient.generateQrCode(qrCodeRequest); 111 | } 112 | } 113 | -------------------------------------------------------------------------------- /src/main/java/com/example/mpesaintegrationspringboot/service/MpesaExpressService.java: -------------------------------------------------------------------------------- 1 | package com.example.mpesaintegrationspringboot.service; 2 | 3 | import com.example.mpesaintegrationspringboot.dto.*; 4 | import com.example.mpesaintegrationspringboot.dto.stkcallback.StkPushResultBody; 5 | import com.example.mpesaintegrationspringboot.entity.MpesaExpress; 6 | import com.example.mpesaintegrationspringboot.http.MpesaClient; 7 | import com.example.mpesaintegrationspringboot.repository.MpesaExpressRepository; 8 | import jakarta.persistence.EntityNotFoundException; 9 | import lombok.RequiredArgsConstructor; 10 | import lombok.extern.slf4j.Slf4j; 11 | import org.springframework.beans.BeanUtils; 12 | import org.springframework.beans.factory.annotation.Value; 13 | import org.springframework.stereotype.Service; 14 | 15 | import java.text.SimpleDateFormat; 16 | import java.util.Base64; 17 | import java.util.Date; 18 | import java.util.UUID; 19 | 20 | @Service 21 | @RequiredArgsConstructor 22 | @Slf4j 23 | public class MpesaExpressService { 24 | private final MpesaClient mpesaClient; 25 | private final MpesaExpressRepository mpesaExpressRepository; 26 | 27 | 28 | @Value("${app.integrations.mpesa.express.shortCode}") 29 | private String shortCode; 30 | @Value("${app.integrations.mpesa.passKey}") 31 | private String passKey; 32 | 33 | public StkPushResponse paymentRequest(PaymentRequest paymentRequest){ 34 | 35 | String timestamp = timestamp(); 36 | //generate password 37 | String password = password(timestamp); 38 | StkPushPayload stkPushPayload = StkPushPayload.builder() 39 | .AccountReference(paymentRequest.accountReference()) 40 | .TransactionType("CustomerPayBillOnline") 41 | .Amount(paymentRequest.amount()) 42 | .PhoneNumber(paymentRequest.phoneNumber()) 43 | .TransactionDesc(paymentRequest.transactionDescription()) 44 | .Password(password) 45 | .PartyA("254708374149") 46 | .PartyB(shortCode) 47 | .CallBackURL(paymentRequest.callbackUrl()) 48 | .Timestamp(timestamp) 49 | .BusinessShortCode(shortCode) 50 | .build(); 51 | log.info(stkPushPayload.toString()); 52 | 53 | StkPushResponse stkPushResponse = mpesaClient.mpesaExpress(stkPushPayload); 54 | 55 | //we record request on the db 56 | MpesaExpress model = new MpesaExpress(); 57 | BeanUtils.copyProperties(stkPushPayload,model); 58 | model.setResponseCode(stkPushResponse.ResponseCode()); 59 | model.setResponseDescription(stkPushResponse.ResponseDescription()); 60 | model.setCustomerMessage(stkPushResponse.CustomerMessage()); 61 | model.setMerchantRequestId(stkPushResponse.MerchantRequestID()); 62 | model.setCheckoutRequestId(stkPushResponse.CheckoutRequestID()); 63 | 64 | mpesaExpressRepository.save(model); 65 | return stkPushResponse; 66 | } 67 | 68 | 69 | private String timestamp(){ 70 | SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss"); 71 | Date currentDate = new Date(); 72 | return dateFormat.format(currentDate); 73 | } 74 | private String password(String timestamp){ 75 | 76 | 77 | return Base64.getEncoder().encodeToString((shortCode + passKey + timestamp).getBytes()); 78 | 79 | } 80 | 81 | public void stkpushResult(StkPushResultBody stkPushResultBody) { 82 | log.info(stkPushResultBody.toString()); 83 | String checkoutRequestId = stkPushResultBody.Body().stkCallback().CheckoutRequestID(); 84 | String merchantRequestID = stkPushResultBody.Body().stkCallback().MerchantRequestID(); 85 | String resultCode = stkPushResultBody.Body().stkCallback().ResultCode(); 86 | String resultDesc = stkPushResultBody.Body().stkCallback().ResultDesc(); 87 | mpesaExpressRepository.findByCheckoutRequestIdAndMerchantRequestId(checkoutRequestId,merchantRequestID) 88 | .ifPresent(mpesaExpress -> { 89 | mpesaExpress.setResultCode(resultCode); 90 | mpesaExpress.setResultDescription(resultDesc); 91 | mpesaExpressRepository.save(mpesaExpress); 92 | }); 93 | } 94 | 95 | public StkPushQueryResponse stkPushPaymentQuery(UUID id){ 96 | return mpesaExpressRepository.findById(id) 97 | .map(mpesaExpress -> { 98 | 99 | String timestamp = timestamp(); 100 | String password = password(timestamp); 101 | StkPushQuery stkPushQuery = StkPushQuery.builder() 102 | .Timestamp(timestamp) 103 | .Password(password) 104 | .CheckoutRequestID(mpesaExpress.getCheckoutRequestId()) 105 | .BusinessShortCode(shortCode) 106 | .build(); 107 | StkPushQueryResponse stkPushQueryResponse = mpesaClient.mpesaExpressQuery(stkPushQuery); 108 | mpesaExpress.setResultCode(stkPushQueryResponse.ResultCode()); 109 | mpesaExpress.setResultDescription(stkPushQueryResponse.ResultDesc()); 110 | mpesaExpressRepository.save(mpesaExpress); 111 | return stkPushQueryResponse; 112 | }) 113 | .orElseThrow(()->new EntityNotFoundException("Transaction not found")); 114 | } 115 | } 116 | -------------------------------------------------------------------------------- /mvnw.cmd: -------------------------------------------------------------------------------- 1 | @REM ---------------------------------------------------------------------------- 2 | @REM Licensed to the Apache Software Foundation (ASF) under one 3 | @REM or more contributor license agreements. See the NOTICE file 4 | @REM distributed with this work for additional information 5 | @REM regarding copyright ownership. The ASF licenses this file 6 | @REM to you under the Apache License, Version 2.0 (the 7 | @REM "License"); you may not use this file except in compliance 8 | @REM with the License. You may obtain a copy of the License at 9 | @REM 10 | @REM https://www.apache.org/licenses/LICENSE-2.0 11 | @REM 12 | @REM Unless required by applicable law or agreed to in writing, 13 | @REM software distributed under the License is distributed on an 14 | @REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | @REM KIND, either express or implied. See the License for the 16 | @REM specific language governing permissions and limitations 17 | @REM under the License. 18 | @REM ---------------------------------------------------------------------------- 19 | 20 | @REM ---------------------------------------------------------------------------- 21 | @REM Apache Maven Wrapper startup batch script, version 3.2.0 22 | @REM 23 | @REM Required ENV vars: 24 | @REM JAVA_HOME - location of a JDK home dir 25 | @REM 26 | @REM Optional ENV vars 27 | @REM MAVEN_BATCH_ECHO - set to 'on' to enable the echoing of the batch commands 28 | @REM MAVEN_BATCH_PAUSE - set to 'on' to wait for a keystroke before ending 29 | @REM MAVEN_OPTS - parameters passed to the Java VM when running Maven 30 | @REM e.g. to debug Maven itself, use 31 | @REM set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 32 | @REM MAVEN_SKIP_RC - flag to disable loading of mavenrc files 33 | @REM ---------------------------------------------------------------------------- 34 | 35 | @REM Begin all REM lines with '@' in case MAVEN_BATCH_ECHO is 'on' 36 | @echo off 37 | @REM set title of command window 38 | title %0 39 | @REM enable echoing by setting MAVEN_BATCH_ECHO to 'on' 40 | @if "%MAVEN_BATCH_ECHO%" == "on" echo %MAVEN_BATCH_ECHO% 41 | 42 | @REM set %HOME% to equivalent of $HOME 43 | if "%HOME%" == "" (set "HOME=%HOMEDRIVE%%HOMEPATH%") 44 | 45 | @REM Execute a user defined script before this one 46 | if not "%MAVEN_SKIP_RC%" == "" goto skipRcPre 47 | @REM check for pre script, once with legacy .bat ending and once with .cmd ending 48 | if exist "%USERPROFILE%\mavenrc_pre.bat" call "%USERPROFILE%\mavenrc_pre.bat" %* 49 | if exist "%USERPROFILE%\mavenrc_pre.cmd" call "%USERPROFILE%\mavenrc_pre.cmd" %* 50 | :skipRcPre 51 | 52 | @setlocal 53 | 54 | set ERROR_CODE=0 55 | 56 | @REM To isolate internal variables from possible post scripts, we use another setlocal 57 | @setlocal 58 | 59 | @REM ==== START VALIDATION ==== 60 | if not "%JAVA_HOME%" == "" goto OkJHome 61 | 62 | echo. 63 | echo Error: JAVA_HOME not found in your environment. >&2 64 | echo Please set the JAVA_HOME variable in your environment to match the >&2 65 | echo location of your Java installation. >&2 66 | echo. 67 | goto error 68 | 69 | :OkJHome 70 | if exist "%JAVA_HOME%\bin\java.exe" goto init 71 | 72 | echo. 73 | echo Error: JAVA_HOME is set to an invalid directory. >&2 74 | echo JAVA_HOME = "%JAVA_HOME%" >&2 75 | echo Please set the JAVA_HOME variable in your environment to match the >&2 76 | echo location of your Java installation. >&2 77 | echo. 78 | goto error 79 | 80 | @REM ==== END VALIDATION ==== 81 | 82 | :init 83 | 84 | @REM Find the project base dir, i.e. the directory that contains the folder ".mvn". 85 | @REM Fallback to current working directory if not found. 86 | 87 | set MAVEN_PROJECTBASEDIR=%MAVEN_BASEDIR% 88 | IF NOT "%MAVEN_PROJECTBASEDIR%"=="" goto endDetectBaseDir 89 | 90 | set EXEC_DIR=%CD% 91 | set WDIR=%EXEC_DIR% 92 | :findBaseDir 93 | IF EXIST "%WDIR%"\.mvn goto baseDirFound 94 | cd .. 95 | IF "%WDIR%"=="%CD%" goto baseDirNotFound 96 | set WDIR=%CD% 97 | goto findBaseDir 98 | 99 | :baseDirFound 100 | set MAVEN_PROJECTBASEDIR=%WDIR% 101 | cd "%EXEC_DIR%" 102 | goto endDetectBaseDir 103 | 104 | :baseDirNotFound 105 | set MAVEN_PROJECTBASEDIR=%EXEC_DIR% 106 | cd "%EXEC_DIR%" 107 | 108 | :endDetectBaseDir 109 | 110 | IF NOT EXIST "%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config" goto endReadAdditionalConfig 111 | 112 | @setlocal EnableExtensions EnableDelayedExpansion 113 | for /F "usebackq delims=" %%a in ("%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config") do set JVM_CONFIG_MAVEN_PROPS=!JVM_CONFIG_MAVEN_PROPS! %%a 114 | @endlocal & set JVM_CONFIG_MAVEN_PROPS=%JVM_CONFIG_MAVEN_PROPS% 115 | 116 | :endReadAdditionalConfig 117 | 118 | SET MAVEN_JAVA_EXE="%JAVA_HOME%\bin\java.exe" 119 | set WRAPPER_JAR="%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.jar" 120 | set WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain 121 | 122 | set WRAPPER_URL="https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.2.0/maven-wrapper-3.2.0.jar" 123 | 124 | FOR /F "usebackq tokens=1,2 delims==" %%A IN ("%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.properties") DO ( 125 | IF "%%A"=="wrapperUrl" SET WRAPPER_URL=%%B 126 | ) 127 | 128 | @REM Extension to allow automatically downloading the maven-wrapper.jar from Maven-central 129 | @REM This allows using the maven wrapper in projects that prohibit checking in binary data. 130 | if exist %WRAPPER_JAR% ( 131 | if "%MVNW_VERBOSE%" == "true" ( 132 | echo Found %WRAPPER_JAR% 133 | ) 134 | ) else ( 135 | if not "%MVNW_REPOURL%" == "" ( 136 | SET WRAPPER_URL="%MVNW_REPOURL%/org/apache/maven/wrapper/maven-wrapper/3.2.0/maven-wrapper-3.2.0.jar" 137 | ) 138 | if "%MVNW_VERBOSE%" == "true" ( 139 | echo Couldn't find %WRAPPER_JAR%, downloading it ... 140 | echo Downloading from: %WRAPPER_URL% 141 | ) 142 | 143 | powershell -Command "&{"^ 144 | "$webclient = new-object System.Net.WebClient;"^ 145 | "if (-not ([string]::IsNullOrEmpty('%MVNW_USERNAME%') -and [string]::IsNullOrEmpty('%MVNW_PASSWORD%'))) {"^ 146 | "$webclient.Credentials = new-object System.Net.NetworkCredential('%MVNW_USERNAME%', '%MVNW_PASSWORD%');"^ 147 | "}"^ 148 | "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; $webclient.DownloadFile('%WRAPPER_URL%', '%WRAPPER_JAR%')"^ 149 | "}" 150 | if "%MVNW_VERBOSE%" == "true" ( 151 | echo Finished downloading %WRAPPER_JAR% 152 | ) 153 | ) 154 | @REM End of extension 155 | 156 | @REM If specified, validate the SHA-256 sum of the Maven wrapper jar file 157 | SET WRAPPER_SHA_256_SUM="" 158 | FOR /F "usebackq tokens=1,2 delims==" %%A IN ("%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.properties") DO ( 159 | IF "%%A"=="wrapperSha256Sum" SET WRAPPER_SHA_256_SUM=%%B 160 | ) 161 | IF NOT %WRAPPER_SHA_256_SUM%=="" ( 162 | powershell -Command "&{"^ 163 | "$hash = (Get-FileHash \"%WRAPPER_JAR%\" -Algorithm SHA256).Hash.ToLower();"^ 164 | "If('%WRAPPER_SHA_256_SUM%' -ne $hash){"^ 165 | " Write-Output 'Error: Failed to validate Maven wrapper SHA-256, your Maven wrapper might be compromised.';"^ 166 | " Write-Output 'Investigate or delete %WRAPPER_JAR% to attempt a clean download.';"^ 167 | " Write-Output 'If you updated your Maven version, you need to update the specified wrapperSha256Sum property.';"^ 168 | " exit 1;"^ 169 | "}"^ 170 | "}" 171 | if ERRORLEVEL 1 goto error 172 | ) 173 | 174 | @REM Provide a "standardized" way to retrieve the CLI args that will 175 | @REM work with both Windows and non-Windows executions. 176 | set MAVEN_CMD_LINE_ARGS=%* 177 | 178 | %MAVEN_JAVA_EXE% ^ 179 | %JVM_CONFIG_MAVEN_PROPS% ^ 180 | %MAVEN_OPTS% ^ 181 | %MAVEN_DEBUG_OPTS% ^ 182 | -classpath %WRAPPER_JAR% ^ 183 | "-Dmaven.multiModuleProjectDirectory=%MAVEN_PROJECTBASEDIR%" ^ 184 | %WRAPPER_LAUNCHER% %MAVEN_CONFIG% %* 185 | if ERRORLEVEL 1 goto error 186 | goto end 187 | 188 | :error 189 | set ERROR_CODE=1 190 | 191 | :end 192 | @endlocal & set ERROR_CODE=%ERROR_CODE% 193 | 194 | if not "%MAVEN_SKIP_RC%"=="" goto skipRcPost 195 | @REM check for post script, once with legacy .bat ending and once with .cmd ending 196 | if exist "%USERPROFILE%\mavenrc_post.bat" call "%USERPROFILE%\mavenrc_post.bat" 197 | if exist "%USERPROFILE%\mavenrc_post.cmd" call "%USERPROFILE%\mavenrc_post.cmd" 198 | :skipRcPost 199 | 200 | @REM pause the script if MAVEN_BATCH_PAUSE is set to 'on' 201 | if "%MAVEN_BATCH_PAUSE%"=="on" pause 202 | 203 | if "%MAVEN_TERMINATE_CMD%"=="on" exit %ERROR_CODE% 204 | 205 | cmd /C exit /B %ERROR_CODE% 206 | -------------------------------------------------------------------------------- /mpesa integration.postman_collection.json: -------------------------------------------------------------------------------- 1 | { 2 | "info": { 3 | "_postman_id": "c5f6f040-f27d-49a8-a6a8-b3d347f9dab9", 4 | "name": "mpesa integration", 5 | "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json", 6 | "_exporter_id": "4001477" 7 | }, 8 | "item": [ 9 | { 10 | "name": "gateway", 11 | "item": [ 12 | { 13 | "name": "Get Token", 14 | "request": { 15 | "auth": { 16 | "type": "basic", 17 | "basic": [ 18 | { 19 | "key": "password", 20 | "value": "consumer-secret", 21 | "type": "string" 22 | }, 23 | { 24 | "key": "username", 25 | "value": "consumer-key", 26 | "type": "string" 27 | } 28 | ] 29 | }, 30 | "method": "GET", 31 | "header": [], 32 | "url": { 33 | "raw": "https://sandbox.safaricom.co.ke/oauth/v1/generate?grant_type=client_credentials", 34 | "protocol": "https", 35 | "host": [ 36 | "sandbox", 37 | "safaricom", 38 | "co", 39 | "ke" 40 | ], 41 | "path": [ 42 | "oauth", 43 | "v1", 44 | "generate" 45 | ], 46 | "query": [ 47 | { 48 | "key": "grant_type", 49 | "value": "client_credentials" 50 | } 51 | ] 52 | } 53 | }, 54 | "response": [] 55 | }, 56 | { 57 | "name": "register urls", 58 | "request": { 59 | "method": "POST", 60 | "header": [], 61 | "body": { 62 | "mode": "raw", 63 | "raw": "{ \n \"ShortCode\": \"600990\",\n \"ResponseType\":\"Completed\",\n \"ConfirmationURL\":\"https://ed7f-105-163-2-84.ngrok-free.app/payment/confirm\",\n \"ValidationURL\":\"https://ed7f-105-163-2-84.ngrok-free.app/payment/validate\"\n}", 64 | "options": { 65 | "raw": { 66 | "language": "json" 67 | } 68 | } 69 | }, 70 | "url": { 71 | "raw": "https://sandbox.safaricom.co.ke/mpesa/c2b/v1/registerurl", 72 | "protocol": "https", 73 | "host": [ 74 | "sandbox", 75 | "safaricom", 76 | "co", 77 | "ke" 78 | ], 79 | "path": [ 80 | "mpesa", 81 | "c2b", 82 | "v1", 83 | "registerurl" 84 | ] 85 | } 86 | }, 87 | "response": [] 88 | }, 89 | { 90 | "name": "stk", 91 | "request": { 92 | "method": "POST", 93 | "header": [], 94 | "body": { 95 | "mode": "raw", 96 | "raw": "{\n \"BusinessShortCode\": \"174379\",\n \"Password\": \"MTc0Mzc5YmZiMjc5ZjlhYTliZGJjZjE1OGU5N2RkNzFhNDY3Y2QyZTBjODkzMDU5YjEwZjc4ZTZiNzJhZGExZWQyYzkxOTIwMjMxMTIyMDQ0MjU3\",\n \"Timestamp\": \"20231122044257\",\n \"TransactionType\": \"CustomerPayBillOnline\",\n \"Amount\": \"1\",\n \"PartyA\": \"254708374149\",\n \"PartyB\": \"174379\",\n \"PhoneNumber\": \"254797625778\",\n \"CallBackURL\": \"https://ed7f-105-163-2-84.ngrok-free.app/express-payment/callback\",\n \"AccountReference\": \"Test\",\n \"TransactionDesc\": \"Test\"\n}", 97 | "options": { 98 | "raw": { 99 | "language": "json" 100 | } 101 | } 102 | }, 103 | "url": { 104 | "raw": "https://sandbox.safaricom.co.ke/mpesa/stkpush/v1/processrequest", 105 | "protocol": "https", 106 | "host": [ 107 | "sandbox", 108 | "safaricom", 109 | "co", 110 | "ke" 111 | ], 112 | "path": [ 113 | "mpesa", 114 | "stkpush", 115 | "v1", 116 | "processrequest" 117 | ] 118 | } 119 | }, 120 | "response": [] 121 | }, 122 | { 123 | "name": "qr code", 124 | "request": { 125 | "method": "GET", 126 | "header": [], 127 | "url": { 128 | "raw": "https://sandbox.safaricom.co.ke/mpesa/qrcode/v1/generate", 129 | "protocol": "https", 130 | "host": [ 131 | "sandbox", 132 | "safaricom", 133 | "co", 134 | "ke" 135 | ], 136 | "path": [ 137 | "mpesa", 138 | "qrcode", 139 | "v1", 140 | "generate" 141 | ] 142 | } 143 | }, 144 | "response": [] 145 | } 146 | ], 147 | "auth": { 148 | "type": "bearer", 149 | "bearer": [ 150 | { 151 | "key": "token", 152 | "value": "5mHal0El9yPShtkonVZVQk6uGDG7", 153 | "type": "string" 154 | } 155 | ] 156 | }, 157 | "event": [ 158 | { 159 | "listen": "prerequest", 160 | "script": { 161 | "type": "text/javascript", 162 | "exec": [ 163 | "" 164 | ] 165 | } 166 | }, 167 | { 168 | "listen": "test", 169 | "script": { 170 | "type": "text/javascript", 171 | "exec": [ 172 | "" 173 | ] 174 | } 175 | } 176 | ] 177 | }, 178 | { 179 | "name": "validation", 180 | "request": { 181 | "method": "POST", 182 | "header": [], 183 | "body": { 184 | "mode": "raw", 185 | "raw": "{ \n \"TransactionType\": \"Pay Bill\",\n \"TransID\":\"RKTQDM7W6S\",\n \"TransTime\":\"20191122063845\",\n \"TransAmount\":\"10\",\n \"BusinessShortCode\": \"600638\",\n \"BillRefNumber\":\"invoice008\",\n \"InvoiceNumber\":\"\",\n \"OrgAccountBalance\":\"\",\n \"ThirdPartyTransID\": \"\",\n \"MSISDN\":\"25470****149\",\n \"FirstName\":\"John\",\n \"MiddleName\":\"\",\n \"LastName\":\"Doe\"\n}", 186 | "options": { 187 | "raw": { 188 | "language": "json" 189 | } 190 | } 191 | }, 192 | "url": { 193 | "raw": "localhost:8080/payment/validate", 194 | "host": [ 195 | "localhost" 196 | ], 197 | "port": "8080", 198 | "path": [ 199 | "payment", 200 | "validate" 201 | ] 202 | } 203 | }, 204 | "response": [] 205 | }, 206 | { 207 | "name": "confirm", 208 | "request": { 209 | "method": "POST", 210 | "header": [], 211 | "body": { 212 | "mode": "raw", 213 | "raw": "{ \n \"TransactionType\": \"Pay Bill\",\n \"TransID\":\"RKTQDM7W6S\",\n \"TransTime\":\"20191122063845\",\n \"TransAmount\":\"10\",\n \"BusinessShortCode\": \"600638\",\n \"BillRefNumber\":\"invoice008\",\n \"InvoiceNumber\":\"\",\n \"OrgAccountBalance\":\"\",\n \"ThirdPartyTransID\": \"\",\n \"MSISDN\":\"25470****149\",\n \"FirstName\":\"John\",\n \"MiddleName\":\"\",\n \"LastName\":\"Doe\"\n}", 214 | "options": { 215 | "raw": { 216 | "language": "json" 217 | } 218 | } 219 | }, 220 | "url": { 221 | "raw": "localhost:8080/payment/confirm", 222 | "host": [ 223 | "localhost" 224 | ], 225 | "port": "8080", 226 | "path": [ 227 | "payment", 228 | "confirm" 229 | ] 230 | } 231 | }, 232 | "response": [] 233 | }, 234 | { 235 | "name": "getToken", 236 | "request": { 237 | "method": "GET", 238 | "header": [] 239 | }, 240 | "response": [] 241 | }, 242 | { 243 | "name": "register-urls", 244 | "request": { 245 | "method": "POST", 246 | "header": [], 247 | "body": { 248 | "mode": "raw", 249 | "raw": "{ \n \"ShortCode\": \"600990\",\n \"ResponseType\":\"Cancelled\",\n \"ConfirmationURL\":\"https://1792-105-163-2-84.ngrok-free.app/payment/confirm\",\n \"ValidationURL\":\"https://1792-105-163-2-84.ngrok-free.app/payment/validate\"\n}", 250 | "options": { 251 | "raw": { 252 | "language": "json" 253 | } 254 | } 255 | }, 256 | "url": { 257 | "raw": "localhost:8080/payment/register-urls", 258 | "host": [ 259 | "localhost" 260 | ], 261 | "port": "8080", 262 | "path": [ 263 | "payment", 264 | "register-urls" 265 | ] 266 | } 267 | }, 268 | "response": [] 269 | }, 270 | { 271 | "name": "stkpush", 272 | "request": { 273 | "method": "POST", 274 | "header": [], 275 | "body": { 276 | "mode": "raw", 277 | "raw": "{\n \"amount\":1,\n \"phoneNumber\":\"254797625778\",\n \"transactionDescription\":\"Cool shats\",\n \"accountReference\":\"ACCOUNT_TWO\",\n \"callbackUrl\":\"https://ed7f-105-163-2-84.ngrok-free.app/express-payment/callback\"\n}", 278 | "options": { 279 | "raw": { 280 | "language": "json" 281 | } 282 | } 283 | }, 284 | "url": { 285 | "raw": "http://localhost:8080/express-payment", 286 | "protocol": "http", 287 | "host": [ 288 | "localhost" 289 | ], 290 | "port": "8080", 291 | "path": [ 292 | "express-payment" 293 | ] 294 | } 295 | }, 296 | "response": [] 297 | }, 298 | { 299 | "name": "stkpush Query", 300 | "request": { 301 | "method": "POST", 302 | "header": [], 303 | "body": { 304 | "mode": "raw", 305 | "raw": "{\n \"amount\":1,\n \"phoneNumber\":\"254797625778\",\n \"transactionDescription\":\"Cool shats\",\n \"accountReference\":\"ACCOUNT_TWO\",\n \"callbackUrl\":\"https://ed7f-105-163-2-84.ngrok-free.app/express-payment/callback\"\n}", 306 | "options": { 307 | "raw": { 308 | "language": "json" 309 | } 310 | } 311 | }, 312 | "url": { 313 | "raw": "http://localhost:8080/express-payment", 314 | "protocol": "http", 315 | "host": [ 316 | "localhost" 317 | ], 318 | "port": "8080", 319 | "path": [ 320 | "express-payment" 321 | ] 322 | } 323 | }, 324 | "response": [] 325 | }, 326 | { 327 | "name": "generate qr", 328 | "request": { 329 | "method": "GET", 330 | "header": [], 331 | "url": { 332 | "raw": "http://localhost:8080/payment/generate-qr", 333 | "protocol": "http", 334 | "host": [ 335 | "localhost" 336 | ], 337 | "port": "8080", 338 | "path": [ 339 | "payment", 340 | "generate-qr" 341 | ] 342 | } 343 | }, 344 | "response": [] 345 | } 346 | ] 347 | } -------------------------------------------------------------------------------- /mvnw: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # ---------------------------------------------------------------------------- 3 | # Licensed to the Apache Software Foundation (ASF) under one 4 | # or more contributor license agreements. See the NOTICE file 5 | # distributed with this work for additional information 6 | # regarding copyright ownership. The ASF licenses this file 7 | # to you under the Apache License, Version 2.0 (the 8 | # "License"); you may not use this file except in compliance 9 | # with the License. You may obtain a copy of the License at 10 | # 11 | # https://www.apache.org/licenses/LICENSE-2.0 12 | # 13 | # Unless required by applicable law or agreed to in writing, 14 | # software distributed under the License is distributed on an 15 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 | # KIND, either express or implied. See the License for the 17 | # specific language governing permissions and limitations 18 | # under the License. 19 | # ---------------------------------------------------------------------------- 20 | 21 | # ---------------------------------------------------------------------------- 22 | # Apache Maven Wrapper startup batch script, version 3.2.0 23 | # 24 | # Required ENV vars: 25 | # ------------------ 26 | # JAVA_HOME - location of a JDK home dir 27 | # 28 | # Optional ENV vars 29 | # ----------------- 30 | # MAVEN_OPTS - parameters passed to the Java VM when running Maven 31 | # e.g. to debug Maven itself, use 32 | # set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 33 | # MAVEN_SKIP_RC - flag to disable loading of mavenrc files 34 | # ---------------------------------------------------------------------------- 35 | 36 | if [ -z "$MAVEN_SKIP_RC" ] ; then 37 | 38 | if [ -f /usr/local/etc/mavenrc ] ; then 39 | . /usr/local/etc/mavenrc 40 | fi 41 | 42 | if [ -f /etc/mavenrc ] ; then 43 | . /etc/mavenrc 44 | fi 45 | 46 | if [ -f "$HOME/.mavenrc" ] ; then 47 | . "$HOME/.mavenrc" 48 | fi 49 | 50 | fi 51 | 52 | # OS specific support. $var _must_ be set to either true or false. 53 | cygwin=false; 54 | darwin=false; 55 | mingw=false 56 | case "$(uname)" in 57 | CYGWIN*) cygwin=true ;; 58 | MINGW*) mingw=true;; 59 | Darwin*) darwin=true 60 | # Use /usr/libexec/java_home if available, otherwise fall back to /Library/Java/Home 61 | # See https://developer.apple.com/library/mac/qa/qa1170/_index.html 62 | if [ -z "$JAVA_HOME" ]; then 63 | if [ -x "/usr/libexec/java_home" ]; then 64 | JAVA_HOME="$(/usr/libexec/java_home)"; export JAVA_HOME 65 | else 66 | JAVA_HOME="/Library/Java/Home"; export JAVA_HOME 67 | fi 68 | fi 69 | ;; 70 | esac 71 | 72 | if [ -z "$JAVA_HOME" ] ; then 73 | if [ -r /etc/gentoo-release ] ; then 74 | JAVA_HOME=$(java-config --jre-home) 75 | fi 76 | fi 77 | 78 | # For Cygwin, ensure paths are in UNIX format before anything is touched 79 | if $cygwin ; then 80 | [ -n "$JAVA_HOME" ] && 81 | JAVA_HOME=$(cygpath --unix "$JAVA_HOME") 82 | [ -n "$CLASSPATH" ] && 83 | CLASSPATH=$(cygpath --path --unix "$CLASSPATH") 84 | fi 85 | 86 | # For Mingw, ensure paths are in UNIX format before anything is touched 87 | if $mingw ; then 88 | [ -n "$JAVA_HOME" ] && [ -d "$JAVA_HOME" ] && 89 | JAVA_HOME="$(cd "$JAVA_HOME" || (echo "cannot cd into $JAVA_HOME."; exit 1); pwd)" 90 | fi 91 | 92 | if [ -z "$JAVA_HOME" ]; then 93 | javaExecutable="$(which javac)" 94 | if [ -n "$javaExecutable" ] && ! [ "$(expr "\"$javaExecutable\"" : '\([^ ]*\)')" = "no" ]; then 95 | # readlink(1) is not available as standard on Solaris 10. 96 | readLink=$(which readlink) 97 | if [ ! "$(expr "$readLink" : '\([^ ]*\)')" = "no" ]; then 98 | if $darwin ; then 99 | javaHome="$(dirname "\"$javaExecutable\"")" 100 | javaExecutable="$(cd "\"$javaHome\"" && pwd -P)/javac" 101 | else 102 | javaExecutable="$(readlink -f "\"$javaExecutable\"")" 103 | fi 104 | javaHome="$(dirname "\"$javaExecutable\"")" 105 | javaHome=$(expr "$javaHome" : '\(.*\)/bin') 106 | JAVA_HOME="$javaHome" 107 | export JAVA_HOME 108 | fi 109 | fi 110 | fi 111 | 112 | if [ -z "$JAVACMD" ] ; then 113 | if [ -n "$JAVA_HOME" ] ; then 114 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 115 | # IBM's JDK on AIX uses strange locations for the executables 116 | JAVACMD="$JAVA_HOME/jre/sh/java" 117 | else 118 | JAVACMD="$JAVA_HOME/bin/java" 119 | fi 120 | else 121 | JAVACMD="$(\unset -f command 2>/dev/null; \command -v java)" 122 | fi 123 | fi 124 | 125 | if [ ! -x "$JAVACMD" ] ; then 126 | echo "Error: JAVA_HOME is not defined correctly." >&2 127 | echo " We cannot execute $JAVACMD" >&2 128 | exit 1 129 | fi 130 | 131 | if [ -z "$JAVA_HOME" ] ; then 132 | echo "Warning: JAVA_HOME environment variable is not set." 133 | fi 134 | 135 | # traverses directory structure from process work directory to filesystem root 136 | # first directory with .mvn subdirectory is considered project base directory 137 | find_maven_basedir() { 138 | if [ -z "$1" ] 139 | then 140 | echo "Path not specified to find_maven_basedir" 141 | return 1 142 | fi 143 | 144 | basedir="$1" 145 | wdir="$1" 146 | while [ "$wdir" != '/' ] ; do 147 | if [ -d "$wdir"/.mvn ] ; then 148 | basedir=$wdir 149 | break 150 | fi 151 | # workaround for JBEAP-8937 (on Solaris 10/Sparc) 152 | if [ -d "${wdir}" ]; then 153 | wdir=$(cd "$wdir/.." || exit 1; pwd) 154 | fi 155 | # end of workaround 156 | done 157 | printf '%s' "$(cd "$basedir" || exit 1; pwd)" 158 | } 159 | 160 | # concatenates all lines of a file 161 | concat_lines() { 162 | if [ -f "$1" ]; then 163 | # Remove \r in case we run on Windows within Git Bash 164 | # and check out the repository with auto CRLF management 165 | # enabled. Otherwise, we may read lines that are delimited with 166 | # \r\n and produce $'-Xarg\r' rather than -Xarg due to word 167 | # splitting rules. 168 | tr -s '\r\n' ' ' < "$1" 169 | fi 170 | } 171 | 172 | log() { 173 | if [ "$MVNW_VERBOSE" = true ]; then 174 | printf '%s\n' "$1" 175 | fi 176 | } 177 | 178 | BASE_DIR=$(find_maven_basedir "$(dirname "$0")") 179 | if [ -z "$BASE_DIR" ]; then 180 | exit 1; 181 | fi 182 | 183 | MAVEN_PROJECTBASEDIR=${MAVEN_BASEDIR:-"$BASE_DIR"}; export MAVEN_PROJECTBASEDIR 184 | log "$MAVEN_PROJECTBASEDIR" 185 | 186 | ########################################################################################## 187 | # Extension to allow automatically downloading the maven-wrapper.jar from Maven-central 188 | # This allows using the maven wrapper in projects that prohibit checking in binary data. 189 | ########################################################################################## 190 | wrapperJarPath="$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.jar" 191 | if [ -r "$wrapperJarPath" ]; then 192 | log "Found $wrapperJarPath" 193 | else 194 | log "Couldn't find $wrapperJarPath, downloading it ..." 195 | 196 | if [ -n "$MVNW_REPOURL" ]; then 197 | wrapperUrl="$MVNW_REPOURL/org/apache/maven/wrapper/maven-wrapper/3.2.0/maven-wrapper-3.2.0.jar" 198 | else 199 | wrapperUrl="https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.2.0/maven-wrapper-3.2.0.jar" 200 | fi 201 | while IFS="=" read -r key value; do 202 | # Remove '\r' from value to allow usage on windows as IFS does not consider '\r' as a separator ( considers space, tab, new line ('\n'), and custom '=' ) 203 | safeValue=$(echo "$value" | tr -d '\r') 204 | case "$key" in (wrapperUrl) wrapperUrl="$safeValue"; break ;; 205 | esac 206 | done < "$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.properties" 207 | log "Downloading from: $wrapperUrl" 208 | 209 | if $cygwin; then 210 | wrapperJarPath=$(cygpath --path --windows "$wrapperJarPath") 211 | fi 212 | 213 | if command -v wget > /dev/null; then 214 | log "Found wget ... using wget" 215 | [ "$MVNW_VERBOSE" = true ] && QUIET="" || QUIET="--quiet" 216 | if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then 217 | wget $QUIET "$wrapperUrl" -O "$wrapperJarPath" || rm -f "$wrapperJarPath" 218 | else 219 | wget $QUIET --http-user="$MVNW_USERNAME" --http-password="$MVNW_PASSWORD" "$wrapperUrl" -O "$wrapperJarPath" || rm -f "$wrapperJarPath" 220 | fi 221 | elif command -v curl > /dev/null; then 222 | log "Found curl ... using curl" 223 | [ "$MVNW_VERBOSE" = true ] && QUIET="" || QUIET="--silent" 224 | if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then 225 | curl $QUIET -o "$wrapperJarPath" "$wrapperUrl" -f -L || rm -f "$wrapperJarPath" 226 | else 227 | curl $QUIET --user "$MVNW_USERNAME:$MVNW_PASSWORD" -o "$wrapperJarPath" "$wrapperUrl" -f -L || rm -f "$wrapperJarPath" 228 | fi 229 | else 230 | log "Falling back to using Java to download" 231 | javaSource="$MAVEN_PROJECTBASEDIR/.mvn/wrapper/MavenWrapperDownloader.java" 232 | javaClass="$MAVEN_PROJECTBASEDIR/.mvn/wrapper/MavenWrapperDownloader.class" 233 | # For Cygwin, switch paths to Windows format before running javac 234 | if $cygwin; then 235 | javaSource=$(cygpath --path --windows "$javaSource") 236 | javaClass=$(cygpath --path --windows "$javaClass") 237 | fi 238 | if [ -e "$javaSource" ]; then 239 | if [ ! -e "$javaClass" ]; then 240 | log " - Compiling MavenWrapperDownloader.java ..." 241 | ("$JAVA_HOME/bin/javac" "$javaSource") 242 | fi 243 | if [ -e "$javaClass" ]; then 244 | log " - Running MavenWrapperDownloader.java ..." 245 | ("$JAVA_HOME/bin/java" -cp .mvn/wrapper MavenWrapperDownloader "$wrapperUrl" "$wrapperJarPath") || rm -f "$wrapperJarPath" 246 | fi 247 | fi 248 | fi 249 | fi 250 | ########################################################################################## 251 | # End of extension 252 | ########################################################################################## 253 | 254 | # If specified, validate the SHA-256 sum of the Maven wrapper jar file 255 | wrapperSha256Sum="" 256 | while IFS="=" read -r key value; do 257 | case "$key" in (wrapperSha256Sum) wrapperSha256Sum=$value; break ;; 258 | esac 259 | done < "$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.properties" 260 | if [ -n "$wrapperSha256Sum" ]; then 261 | wrapperSha256Result=false 262 | if command -v sha256sum > /dev/null; then 263 | if echo "$wrapperSha256Sum $wrapperJarPath" | sha256sum -c > /dev/null 2>&1; then 264 | wrapperSha256Result=true 265 | fi 266 | elif command -v shasum > /dev/null; then 267 | if echo "$wrapperSha256Sum $wrapperJarPath" | shasum -a 256 -c > /dev/null 2>&1; then 268 | wrapperSha256Result=true 269 | fi 270 | else 271 | echo "Checksum validation was requested but neither 'sha256sum' or 'shasum' are available." 272 | echo "Please install either command, or disable validation by removing 'wrapperSha256Sum' from your maven-wrapper.properties." 273 | exit 1 274 | fi 275 | if [ $wrapperSha256Result = false ]; then 276 | echo "Error: Failed to validate Maven wrapper SHA-256, your Maven wrapper might be compromised." >&2 277 | echo "Investigate or delete $wrapperJarPath to attempt a clean download." >&2 278 | echo "If you updated your Maven version, you need to update the specified wrapperSha256Sum property." >&2 279 | exit 1 280 | fi 281 | fi 282 | 283 | MAVEN_OPTS="$(concat_lines "$MAVEN_PROJECTBASEDIR/.mvn/jvm.config") $MAVEN_OPTS" 284 | 285 | # For Cygwin, switch paths to Windows format before running java 286 | if $cygwin; then 287 | [ -n "$JAVA_HOME" ] && 288 | JAVA_HOME=$(cygpath --path --windows "$JAVA_HOME") 289 | [ -n "$CLASSPATH" ] && 290 | CLASSPATH=$(cygpath --path --windows "$CLASSPATH") 291 | [ -n "$MAVEN_PROJECTBASEDIR" ] && 292 | MAVEN_PROJECTBASEDIR=$(cygpath --path --windows "$MAVEN_PROJECTBASEDIR") 293 | fi 294 | 295 | # Provide a "standardized" way to retrieve the CLI args that will 296 | # work with both Windows and non-Windows executions. 297 | MAVEN_CMD_LINE_ARGS="$MAVEN_CONFIG $*" 298 | export MAVEN_CMD_LINE_ARGS 299 | 300 | WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain 301 | 302 | # shellcheck disable=SC2086 # safe args 303 | exec "$JAVACMD" \ 304 | $MAVEN_OPTS \ 305 | $MAVEN_DEBUG_OPTS \ 306 | -classpath "$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.jar" \ 307 | "-Dmaven.multiModuleProjectDirectory=${MAVEN_PROJECTBASEDIR}" \ 308 | ${WRAPPER_LAUNCHER} $MAVEN_CONFIG "$@" 309 | --------------------------------------------------------------------------------