Skip to content

Latest commit

 

History

History
233 lines (157 loc) · 4.43 KB

File metadata and controls

233 lines (157 loc) · 4.43 KB

💳 Payment Processing System (Spring Framework)

A simple and extensible Payment Processing System built using the Spring Framework (Core + Dependency Injection). This project demonstrates how to use Factory Design Pattern + Spring’s Dependency Injection to dynamically select and process different payment methods.


🚀 Features

  • Supports multiple payment types:

    • UPI
    • Credit Card
    • Debit Card
  • Uses Spring Annotation-based Configuration

  • Implements Factory Design Pattern

  • Easily extendable for new payment methods

  • Clean and modular architecture


🧠 Concept Used

  • Spring IoC Container
  • Dependency Injection (DI)
  • Component Scanning
  • Factory Design Pattern
  • Interface-based programming

📁 Project Structure

com.nit
│
├── config
│   └── AppConfig.java
│
├── main
│   └── TestApp.java
│
└── sbeans
    ├── Payment.java
    ├── PaymentFactory.java
    ├── CreditCardPayment.java
    ├── DebitCardPayment.java
    └── UPIPayment.java

⚙️ Configuration

AppConfig.java

Spring configuration class with component scanning:

@Configuration
@ComponentScan(basePackages="com.nit.sbeans")
public class AppConfig {
}

🧩 Core Components

1. Payment Interface

public interface Payment {
    String pay(double amount);
}

2. Payment Implementations

💳 Credit Card

@Component("credit")
public class CreditCardPayment implements Payment {
    public String pay(double amount) {
        double discount = 10;
        double finalAmount = amount - (amount * discount);
        return "Paid " + finalAmount + " using Credit Card";
    }
}

💳 Debit Card

@Component("debit")
public class DebitCardPayment implements Payment {
    public String pay(double amount) {
        double discount = 5;
        double finalAmount = amount - (amount * discount);
        return "Paid " + finalAmount + " using Debit Card";
    }
}

📱 UPI

@Component("upi")
public class UPIPayment implements Payment {
    public String pay(double amount) {
        double discount = 15;
        double finalAmount = amount - (amount * discount);
        return "Paid " + finalAmount + " using UPI";
    }
}

3. Payment Factory

Handles dynamic selection of payment method using Spring's auto-wired map:

@Component
public class PaymentFactory {

    @Autowired
    private Map<String, Payment> paymentMap;

    public Payment getPayment(String type) {
        Payment payment = paymentMap.get(type.toLowerCase());

        if (payment == null) {
            throw new IllegalArgumentException("Invalid Payment Type");
        }
        return payment;
    }
}

▶️ Running the Application

TestApp.java

public class TestApp {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext ctx =
            new AnnotationConfigApplicationContext(AppConfig.class);

        PaymentFactory paymentFactory = ctx.getBean(PaymentFactory.class);

        try {
            Payment payment = paymentFactory.getPayment("upi");
            System.out.println(payment.pay(50000));
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
}

💡 Example Output

Paid -700000.0 using UPI

(Note: Discount calculation currently multiplies instead of percentage — can be improved)


🛠️ How It Works

  1. Spring scans all @Component classes.
  2. Each payment method is registered with a unique name (credit, debit, upi).
  3. Spring injects all implementations into a Map<String, Payment>.
  4. PaymentFactory selects the correct implementation dynamically.
  5. TestApp triggers the payment process.

🔥 Future Improvements

  • Fix discount calculation logic (use percentage properly)
  • Add new payment methods (Net Banking, Wallets)
  • Add logging (SLF4J / Log4j)
  • Add REST API layer (Spring Boot)
  • Add unit testing (JUnit)

📌 Key Takeaway

This project shows how Spring + Factory Pattern can eliminate complex if-else or switch statements and make the system clean, scalable, and maintainable.


🧑‍💻 Author

Built as a learning project for understanding Spring Core concepts and design patterns.


⭐ If you found this useful

Give it a ⭐ on GitHub and keep building 🚀