-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathpayment_demo.go
More file actions
141 lines (123 loc) · 4.25 KB
/
payment_demo.go
File metadata and controls
141 lines (123 loc) · 4.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package main
import (
"bytes"
"encoding/json"
"fmt"
"log"
"net/http"
"time"
"github.com/ducnpdev/godev-kit/internal/entity"
)
// PaymentDemo demonstrates the payment system
func PaymentDemo() {
fmt.Println("=== Payment System Demo ===")
// Demo 1: Register a payment
fmt.Println("\n1. Registering a payment...")
paymentReq := map[string]interface{}{
"user_id": 1,
"amount": 500000,
"currency": "VND",
"payment_type": "electric",
"meter_number": "EVN001234567",
"customer_code": "CUST001",
"description": "Thanh toán tiền điện tháng 12/2024",
"payment_method": "bank_transfer",
}
reqBody, _ := json.Marshal(paymentReq)
resp, err := http.Post("http://localhost:8080/api/v1/payments", "application/json", bytes.NewBuffer(reqBody))
if err != nil {
log.Printf("Error registering payment: %v", err)
return
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusCreated {
var paymentResp entity.PaymentResponse
json.NewDecoder(resp.Body).Decode(&paymentResp)
fmt.Printf("Payment registered successfully!\n")
fmt.Printf("Payment ID: %d\n", paymentResp.ID)
fmt.Printf("Transaction ID: %s\n", paymentResp.TransactionID)
fmt.Printf("Status: %s\n", paymentResp.Status)
} else {
fmt.Printf("Failed to register payment. Status: %d\n", resp.StatusCode)
}
// Demo 2: Get payment by ID
fmt.Println("\n2. Getting payment by ID...")
resp, err = http.Get("http://localhost:8080/api/v1/payments/1")
if err != nil {
log.Printf("Error getting payment: %v", err)
return
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusOK {
var payment entity.PaymentResponse
json.NewDecoder(resp.Body).Decode(&payment)
fmt.Printf("Payment details:\n")
fmt.Printf(" ID: %d\n", payment.ID)
fmt.Printf(" Amount: %.2f %s\n", payment.Amount, payment.Currency)
fmt.Printf(" Status: %s\n", payment.Status)
fmt.Printf(" Meter Number: %s\n", payment.MeterNumber)
fmt.Printf(" Customer Code: %s\n", payment.CustomerCode)
fmt.Printf(" Created At: %s\n", payment.CreatedAt.Format(time.RFC3339))
} else {
fmt.Printf("Failed to get payment. Status: %d\n", resp.StatusCode)
}
// Demo 3: Get payments by user ID
fmt.Println("\n3. Getting payments by user ID...")
resp, err = http.Get("http://localhost:8080/api/v1/users/1/payments")
if err != nil {
log.Printf("Error getting user payments: %v", err)
return
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusOK {
var payments []entity.PaymentResponse
json.NewDecoder(resp.Body).Decode(&payments)
fmt.Printf("Found %d payments for user:\n", len(payments))
for i, payment := range payments {
fmt.Printf(" %d. Payment ID: %d, Amount: %.2f %s, Status: %s\n",
i+1, payment.ID, payment.Amount, payment.Currency, payment.Status)
}
} else {
fmt.Printf("Failed to get user payments. Status: %d\n", resp.StatusCode)
}
// Demo 4: Wait for payment processing
fmt.Println("\n4. Waiting for payment processing...")
time.Sleep(5 * time.Second)
// Check payment status again
resp, err = http.Get("http://localhost:8080/api/v1/payments/1")
if err != nil {
log.Printf("Error getting payment: %v", err)
return
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusOK {
var payment entity.PaymentResponse
json.NewDecoder(resp.Body).Decode(&payment)
fmt.Printf("Payment status after processing: %s\n", payment.Status)
if payment.Status == "completed" {
fmt.Println("✅ Payment processed successfully!")
} else if payment.Status == "failed" {
fmt.Println("❌ Payment processing failed!")
} else {
fmt.Printf("⏳ Payment is still %s\n", payment.Status)
}
}
fmt.Println("\n=== Demo completed ===")
}
// KafkaConsumerDemo demonstrates Kafka consumer
func KafkaConsumerDemo() {
fmt.Println("\n=== Kafka Consumer Demo ===")
fmt.Println("Starting payment consumer...")
fmt.Println("Consumer will process payment events from Kafka topic 'payment-events'")
fmt.Println("Press Ctrl+C to stop the consumer")
// In a real application, you would start the consumer here
// consumer := payment.NewPaymentConsumer(brokers, groupID, useCase, logger)
// consumer.Start(context.Background())
}
// RunPaymentDemo runs the payment demo
func RunPaymentDemo() {
// Run payment demo
PaymentDemo()
// Run Kafka consumer demo
KafkaConsumerDemo()
}