forked from Chapa-Et/chapa-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chapa_example_service.go
154 lines (127 loc) · 3.39 KB
/
chapa_example_service.go
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
142
143
144
145
146
147
148
149
150
151
152
153
154
package chapa
import (
"context"
"errors"
"fmt"
"log"
"sync"
"time"
"github.com/shopspring/decimal"
)
// Placeholder data
var (
firstName1 = "Jon"
firstName2 = "Do"
lastName1 = "Mary"
lastName2 = "Josef"
email1 = RandomString(5) + "@gmail.com"
email2 = RandomString(5) + "@gmail.com"
Customers = []Customer{
{
ID: 1002,
FirstName: firstName1,
LastName: lastName1,
Email: email1,
},
{
ID: 1032,
FirstName: firstName2,
LastName: lastName2,
Email: email2,
},
}
transactions = []Transaction{
{
TransID: RandomString(10),
Amount: decimal.NewFromInt(10),
Charge: "0.35",
Currency: "ETB",
CreatedAt: time.Now().String(),
Customer: Customers[0],
},
{
TransID: RandomString(10),
Amount: decimal.NewFromInt(20),
Charge: "0.40",
Currency: "ETB",
CreatedAt: time.Now().String(),
Customer: Customers[0],
},
}
)
type (
ExamplePaymentService interface {
Checkout(ctx context.Context, CustomerID int64, form *CheckoutForm) (*PaymentResponse, error)
ListTransactions(ctx context.Context) (*TransactionList, error)
}
AppExamplePaymentService struct {
mu *sync.Mutex
paymentGatewayProvider API
}
)
func NewExamplePaymentService(
paymentGatewayProvider API,
) *AppExamplePaymentService {
return &AppExamplePaymentService{
mu: &sync.Mutex{},
paymentGatewayProvider: paymentGatewayProvider,
}
}
func (s *AppExamplePaymentService) Checkout(ctx context.Context, CustomerID int64, form CheckoutForm) (*Transaction, error) {
Customer, err := s.CustomerByID(ctx, CustomerID)
if err != nil {
return &Transaction{}, err
}
invoice := &PaymentRequest{
Amount: form.Amount,
Currency: form.Currency,
Email: Customer.Email,
FirstName: Customer.FirstName,
LastName: Customer.LastName,
CallbackURL: "https://webhook.site/077164d6-29cb-40df-ba29-8a00e59a7e60",
TransactionRef: RandomString(10),
}
response, err := s.paymentGatewayProvider.PaymentRequest(invoice)
if err != nil {
return &Transaction{}, err
}
if response.Status != "success" {
log.Printf("[ERROR] Failed to checkout Customer request response = [%+v]", response)
return &Transaction{}, fmt.Errorf("failed to checkout err = %v", response.Message)
}
transaction := Transaction{
TransID: invoice.TransactionRef,
Amount: form.Amount,
Currency: form.Currency,
Customer: Customer,
Status: PendingTransactionStatus,
CreatedAt: time.Now().String(),
}
err = s.SaveTransaction(ctx, transaction)
if err != nil {
return &Transaction{}, err
}
return &transaction, nil
}
func (s *AppExamplePaymentService) ListTransactions(_ context.Context) (TransactionList, error) {
// validations here
transactionList := TransactionList{
Transactions: transactions,
}
return transactionList, nil
}
func (s *AppExamplePaymentService) SaveTransaction(_ context.Context, transaction Transaction) error {
s.mu.Lock()
defer s.mu.Unlock()
transactions = append([]Transaction{transaction}, transactions...)
return nil
}
// CustomerByID you'd fetch Customer from the db
func (s *AppExamplePaymentService) CustomerByID(_ context.Context, CustomerID int64) (Customer, error) {
for index := range Customers {
if Customers[index].ID == CustomerID {
return Customers[index], nil
}
}
return Customer{}, errors.New("Customer not found")
}