Skip to content

Instantly share code, notes, and snippets.

@Dostonlv
Created November 27, 2024 10:20
Show Gist options
  • Select an option

  • Save Dostonlv/ead4be619ac73ed054f07ff85d5bea88 to your computer and use it in GitHub Desktop.

Select an option

Save Dostonlv/ead4be619ac73ed054f07ff85d5bea88 to your computer and use it in GitHub Desktop.
dependency injection usage
package main
import (
"errors"
"fmt"
"log"
"time"
)
// Models
// Transaction Model
type Transaction struct {
ID string
Amount float64
Currency string
SenderID string
ReceiverID string
Gateway string
Status string
CreatedAt time.Time
}
// User Model
type User struct {
ID string
Name string
Email string
PhoneNumber string
AccountType string
Balance float64
}
//interfaces
// PaymentGateway -> Payment Gateway Interface
type PaymentGateway interface {
ProcessPayment(transaction Transaction) error
RefundPayment(transaction Transaction) error
}
// NotificationService -> Notification Service Interface
type NotificationService interface {
SendAlert(user User, message string) error
}
// LoggingService -> Logging Service Interface
type LoggingService interface {
LogTransaction(transaction Transaction)
LogError(err error)
}
// FraudDetectionService -> Fraud Detection Service Interface
type FraudDetectionService interface {
CheckTransaction(transaction Transaction) bool
}
// Payment Gateway Implementations
// PaymePaymentGateway -> Payme Payment Gateway
type PaymePaymentGateway struct{}
func (pay *PaymePaymentGateway) ProcessPayment(transaction Transaction) error {
if transaction.Amount <= 0 {
return errors.New("summani to'g'ri kiriting")
}
log.Printf("Payme: $%.2f to'lovi amalga oshirildi\n", transaction.Amount)
return nil
}
func (pay *PaymePaymentGateway) RefundPayment(transaction Transaction) error {
log.Printf("Payme: $%.2f to'lovi qaytarildi\n", transaction.Amount)
return nil
}
// ClickPaymentGateway -> Click Payment Gateway
type ClickPaymentGateway struct{}
func (click *ClickPaymentGateway) ProcessPayment(transaction Transaction) error {
if transaction.Amount <= 0 {
return errors.New("summani to'g'ri kiriting")
}
log.Printf("Click: $%.2f to'lovi amalga oshirildi\n", transaction.Amount)
return nil
}
func (click *ClickPaymentGateway) RefundPayment(transaction Transaction) error {
log.Printf("Click: $%.2f to'lovi qaytarildi\n", transaction.Amount)
return nil
}
// Notification Service Implementations
// EmailNotificationService -> Email Notification Service
type EmailNotificationService struct{}
func (email *EmailNotificationService) SendAlert(user User, message string) error {
log.Printf("Email: %s ga xabar yuborildi: %s\n", user.Email, message)
return nil
}
// SMSNotificationService -> SMS Notification Service
type SMSNotificationService struct{}
func (sms *SMSNotificationService) SendAlert(user User, message string) error {
log.Printf("SMS: %s ga xabar yuborildi: %s\n", user.PhoneNumber, message)
return nil
}
// Logging Service Implementations
// ConsoleLoggingService -> Console Logging Service
type ConsoleLoggingService struct{}
func (console *ConsoleLoggingService) LogTransaction(transaction Transaction) {
log.Printf("Transaction: %s\n", transaction.ID)
}
func (console *ConsoleLoggingService) LogError(err error) {
log.Printf("Error Log: %s\n", err)
}
// Fraud Detection Service Implementations
// SimpleFraudDetectionService -> Fraud Detection Service
type SimpleFraudDetectionService struct{}
func (fraud *SimpleFraudDetectionService) CheckTransaction(transaction Transaction) bool {
//if transaction.Amount < 5000 {
// return true
//}
//return false
return transaction.Amount < 5000
}
// PaymentService -> Payment Service - Main Business Logic
type PaymentService struct {
paymentGateway PaymentGateway
notificationService NotificationService
loggingService LoggingService
fraudDetectionService FraudDetectionService
}
// NewPaymentService -> Payment Service Constructor
func NewPaymentService(
pg PaymentGateway,
ns NotificationService,
ls LoggingService,
fd FraudDetectionService,
) *PaymentService {
return &PaymentService{
paymentGateway: pg,
notificationService: ns,
loggingService: ls,
fraudDetectionService: fd,
}
}
// ProcessTransaction -> Process Payment Transaction
func (ps *PaymentService) ProcessTransaction(
sender, receiver User,
amount float64,
gateway string,
) error {
// create transaction
transaction := Transaction{
ID: fmt.Sprintf("TX-%d", time.Now().UnixNano()),
Amount: amount,
SenderID: sender.ID,
ReceiverID: receiver.ID,
Gateway: gateway,
CreatedAt: time.Now(),
}
// check sender balance
if sender.Balance < amount {
ps.loggingService.LogError(errors.New("yetarli mablag' yo'q"))
return errors.New("yetarli mablag' yo'q")
}
// check fraud
if !ps.fraudDetectionService.CheckTransaction(transaction) {
ps.loggingService.LogError(errors.New("shubhali tranzaksiya"))
return errors.New("shubhali tranzaksiya")
}
// process payment with gateway
err := ps.paymentGateway.ProcessPayment(transaction)
if err != nil {
ps.loggingService.LogError(err)
return err
}
// log transaction
ps.loggingService.LogTransaction(transaction)
// send notification
err = ps.notificationService.SendAlert(
sender,
fmt.Sprintf("$%.2f miqdorda to'lov muvaffaqiyatli amalga oshirildi", amount),
)
if err != nil {
ps.loggingService.LogError(err)
}
return nil
}
func main() {
// create users
sender := User{
ID: "001",
Name: "John Doe",
Email: "john@example.com",
PhoneNumber: "+1234567890",
AccountType: "personal",
Balance: 3000,
}
receiver := User{
ID: "002",
Name: "Jane Smith",
Email: "jane@example.com",
PhoneNumber: "+111222232",
AccountType: "personal",
Balance: 0,
}
// Dependency Injection
paymentService := NewPaymentService(
&PaymePaymentGateway{},
&EmailNotificationService{},
&ConsoleLoggingService{},
&SimpleFraudDetectionService{},
)
// process transaction
err := paymentService.ProcessTransaction(sender, receiver, 4000, "payme")
if err != nil {
fmt.Println("To'lovda xatolik:", err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment