Skip to content

miftakhulaziz/pgmidtrans

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Payment Gateway Integration – Midtrans

A robust Virtual Account (VA) payment gateway integration service built with Hexagonal Architecture, enabling seamless bank transfer flows through Midtrans API.

Java Spring Boot Midtrans


πŸ“‹ Table of Contents


✨ Features

  • 🏦 Multi-Bank Virtual Account Support (BCA, BNI, Mandiri, Permata, etc.)
  • πŸ“§ Automated Email Notifications (payment reminders & confirmations)
  • πŸ”” Real-time Payment Callbacks from Midtrans
  • πŸ” Secure Payment Processing with transaction validation
  • πŸ“Š Payment History Tracking with comprehensive logging
  • 🧩 Clean Architecture with clear separation of concerns
  • πŸ§ͺ Sandbox Testing Support for safe development

🧱 Architecture Overview

This application implements Hexagonal Architecture (Ports & Adapters), ensuring maintainability, testability, and framework independence.

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                    INBOUND ADAPTERS                      β”‚
β”‚         (REST Controllers, Messaging Handlers)           β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                     β”‚
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                  APPLICATION LAYER                       β”‚
β”‚              (Use Cases & Orchestration)                 β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                     β”‚
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                    DOMAIN LAYER                          β”‚
β”‚         (Business Logic, Entities, Ports)                β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                     β”‚
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                   OUTBOUND ADAPTERS                      β”‚
β”‚      (Database, External APIs, Email Service)            β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Layer Responsibilities

Domain Layer - Core business logic and rules

  • Business entities (Payment, Customer, Bank, etc.)
  • Domain services with business operations
  • Port definitions (interfaces) for external dependencies
  • Framework and infrastructure independent

Application Layer - Use case orchestration

  • Coordinates domain services and external ports
  • Defines transaction boundaries
  • Executes business workflows
  • Input validation and error handling

Infrastructure Layer - Technical implementation

  • Inbound: REST controllers, messaging consumers
  • Outbound: Database adapters (JPA), external API clients (Midtrans, Email)
  • Configuration management
  • Framework-specific implementations

Shared Layer - Cross-cutting concerns

  • Custom annotations and AOP aspects
  • Common utilities (JSON, Base64, Time, Validation)
  • Exception handling and error responses
  • Enums and constants

πŸ“‚ Project Structure

app-midtrans/
β”œβ”€β”€ πŸ“± application/              # Use cases and orchestration
β”‚   β”œβ”€β”€ UseCaseExecutor.java
β”‚   └── usecase/
β”‚       β”œβ”€β”€ VaNotifyUseCase.java
β”‚       └── VaPaymentUseCase.java
β”‚
β”œβ”€β”€ 🧠 domain/                   # Core business logic
β”‚   β”œβ”€β”€ bank/
β”‚   β”œβ”€β”€ bankAccount/
β”‚   β”œβ”€β”€ callback/
β”‚   β”œβ”€β”€ customer/
β”‚   β”œβ”€β”€ email/
β”‚   └── payment/
β”‚
β”œβ”€β”€ πŸ”Œ infra/                    # Infrastructure & adapters
β”‚   β”œβ”€β”€ adapter/
β”‚   β”‚   β”œβ”€β”€ inbound/            # REST & Messaging
β”‚   β”‚   └── outbound/           # DB, APIs, Email
β”‚   └── config/                 # Spring configurations
β”‚
└── πŸ› οΈ shared/                   # Common utilities
    β”œβ”€β”€ annotation/
    β”œβ”€β”€ aop/
    β”œβ”€β”€ enums/
    β”œβ”€β”€ exception/
    β”œβ”€β”€ handler/
    β”œβ”€β”€ payload/
    └── utils/

View detailed structure β†’


πŸš€ Tech Stack

Core Technologies

  • Java 17+ - Programming language
  • Spring Boot 3.x - Application framework
  • Spring Data JPA - Data persistence
  • Hibernate - ORM framework
  • PostgreSQL/MySQL - Database (configurable)

External Integrations

  • Midtrans API - Payment gateway
  • Mailtrap - Email service (development)
  • SMTP - Email delivery (production)

Development Tools

  • Maven - Dependency management
  • Lombok - Code generation
  • SpringDoc OpenAPI - API documentation
  • SLF4J + Logback - Logging

🎯 Getting Started

Prerequisites

  • Java 17 or higher
  • Maven 3.6+
  • PostgreSQL/MySQL database
  • Midtrans account (sandbox for testing)
  • Email service credentials

Installation

  1. Clone the repository

    git clone https://github.com/yourusername/app-midtrans.git
    cd app-midtrans
  2. Configure application properties

    cp src/main/resources/application.properties.example src/main/resources/application.properties
  3. Set up environment variables

    export MIDTRANS_SERVER_KEY=your_server_key
    export MIDTRANS_CLIENT_KEY=your_client_key
    export DATABASE_URL=jdbc:postgresql://localhost:5432/midtrans_db
    export EMAIL_HOST=sandbox.smtp.mailtrap.io
    export EMAIL_USERNAME=your_username
    export EMAIL_PASSWORD=your_password
  4. Build the project

    mvn clean install
  5. Run the application

    mvn spring-boot:run

The application will start on http://localhost:8080

Database Setup

Run the following SQL to create the database:

CREATE DATABASE midtrans_db;

Tables will be auto-created by Hibernate on first run (if spring.jpa.hibernate.ddl-auto=update).


πŸ“§ Email Notifications

The system sends automated email notifications at key points in the payment lifecycle.

Payment Reminder Email

Sent when payment is pending or approaching due time, containing payment instructions and virtual account details.

Remainder Email

Payment Success Email

Sent immediately after successful payment confirmation, including transaction summary and order details.

Payment Success

Email Configuration

Customize email templates in src/main/resources/templates/email/:

  • reminder-email.html - Payment reminder template
  • success-email.html - Payment confirmation template

Templates support dynamic variables like ${customerName}, ${amount}, ${vaNumber}, etc.


πŸ§ͺ Testing in Sandbox

Midtrans provides a comprehensive sandbox environment for testing all payment scenarios without real money.

How to Test VA Payments

  1. Create a payment request via API
  2. Use the test VA number provided by Midtrans
  3. Simulate payment using Midtrans Simulator

Test Virtual Account Numbers

Bank VA Number Format Example
BCA 5XXXX + order_id 500012345678
BNI 8XXXX + order_id 800012345678
Mandiri 7XXXX + order_id 700012345678
Permata 10-digit number 8562000001234567

Testing Resources


πŸ“š API Documentation

Interactive API Docs

Once the application is running, access the interactive API documentation:

  • Swagger UI: http://localhost:8080/swagger-ui.html
  • OpenAPI JSON: http://localhost:8080/v3/api-docs

Key Endpoints

Create Virtual Account Payment

POST /api/v1.0/va/transfer
Content-Type: application/json

{
  "customerId": "12345",
  "bankCode": "bca",
  "amount": 100000,
  "orderId": "ORDER-2024-001",
  "itemDetails": [...]
}

Payment Notification Callback

POST /api/v1.0/va/notify
Content-Type: application/json

{
  "transaction_id": "...",
  "order_id": "...",
  "transaction_status": "settlement",
  ...
}

Midtrans API Reference

For complete API capabilities and integration guidelines:


βš™οΈ Configuration

Application Properties

Key configuration properties:

# Server
server.port=8080

# Database
spring.datasource.url=${DATABASE_URL}
spring.datasource.username=${DB_USERNAME}
spring.datasource.password=${DB_PASSWORD}
spring.jpa.hibernate.ddl-auto=update

# Midtrans
midtrans.server.key=${MIDTRANS_SERVER_KEY}
midtrans.client.key=${MIDTRANS_CLIENT_KEY}
midtrans.is.production=false
midtrans.api.url=https://api.sandbox.midtrans.com/v2

# Email
spring.mail.host=${EMAIL_HOST}
spring.mail.port=587
spring.mail.username=${EMAIL_USERNAME}
spring.mail.password=${EMAIL_PASSWORD}
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true

Environment Variables

Variable Description Required
MIDTRANS_SERVER_KEY Midtrans server key Yes
MIDTRANS_CLIENT_KEY Midtrans client key Yes
DATABASE_URL Database connection URL Yes
DB_USERNAME Database username Yes
DB_PASSWORD Database password Yes
EMAIL_HOST SMTP host Yes
EMAIL_USERNAME SMTP username Yes
EMAIL_PASSWORD SMTP password Yes

πŸ”„ Payment Flow Example

sequenceDiagram
    participant Client
    participant Controller
    participant UseCase
    participant Domain
    participant Midtrans
    participant Database
    participant Email

    Client->>Controller: POST /api/v1.0/va/transfer
    Controller->>UseCase: Execute VaPaymentUseCase
    UseCase->>Domain: Validate Customer & Bank
    Domain-->>UseCase: Validation OK
    UseCase->>Midtrans: Create VA Payment
    Midtrans-->>UseCase: VA Number & Details
    UseCase->>Database: Save Payment Record
    UseCase->>Email: Send Reminder Email
    UseCase-->>Controller: Payment Response
    Controller-->>Client: 200 OK + VA Details
    
    Note over Midtrans,Database: Customer makes payment
    
    Midtrans->>Controller: POST /api/v1.0/va/notify (callback)
    Controller->>UseCase: Execute VaNotifyUseCase
    UseCase->>Database: Update Payment Status
    UseCase->>Email: Send Success Email
    UseCase-->>Controller: Notification Processed
    Controller-->>Midtrans: 200 OK
Loading

πŸ§ͺ Testing

Unit Tests

mvn test

Integration Tests

mvn verify

Test Coverage

mvn clean test jacoco:report

View coverage report at target/site/jacoco/index.html


πŸ“ Contributing

Contributions are welcome! Please follow these steps:

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

πŸ› Known Issues & Roadmap

Current Limitations

  • Email service limited to Mailtrap in development
  • Single currency support (IDR only)
  • No retry mechanism for failed callbacks

Planned Features

  • Multi-currency support
  • Payment retry mechanism
  • Webhook signature verification
  • Admin dashboard
  • Payment analytics
  • Refund support

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.


🀝 Support

For questions or issues:


πŸ™ Acknowledgments

  • Midtrans for the payment gateway platform
  • Spring Framework for the excellent ecosystem
  • The clean architecture community for architectural patterns

Made with ❀️ by Your Team

About

Integration payment gateway midtrans using spring-boot java

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •  

Languages