this repo has no description
1package domain
2
3import (
4 "errors"
5 "regexp"
6 "sync"
7 "time"
8
9 "github.com/Tulkdan/payment-gateway/internal/constants"
10)
11
12type Status string
13
14const (
15 StatusPending Status = "pending" // when it needs to send payment to provider
16 StatusApproved Status = "approved" // when provider successfully charged
17 StatusRejected Status = "rejected" // when provider rejected payment
18 StatusFailed Status = "failed" // when provider fails to charge
19)
20
21type PaymentCard struct {
22 Number string
23 HolderName string
24 CVV string
25 ExpirationDate string
26 Installments uint
27}
28
29type Payment struct {
30 Amount uint
31 Currency string
32 Description string
33 PaymentType string
34 Card PaymentCard
35 Status Status
36 CreatedAt time.Time
37
38 mu sync.Mutex
39}
40
41func NewPayment(amount uint, currency string, description string, paymentType string, card PaymentCard) (*Payment, error) {
42 if paymentType != "card" {
43 return nil, errors.New("We only accept payments with type 'card'")
44 }
45
46 isoFormatRgx := regexp.MustCompile(`^[A-Z]{3}$`)
47 if !isoFormatRgx.Match([]byte(currency)) || !constants.Lookup(currency) {
48 return nil, errors.New("Invalid Currency")
49 }
50
51 expirationDateRgx := regexp.MustCompile(`\d{2}\/\d{4}`)
52 if !expirationDateRgx.Match([]byte(card.ExpirationDate)) {
53 return nil, errors.New("Invalid ExpirationDate format")
54 }
55
56 return &Payment{
57 Amount: amount,
58 Currency: currency,
59 Description: description,
60 PaymentType: paymentType,
61 Card: card,
62 Status: StatusPending,
63 CreatedAt: time.Now(),
64 }, nil
65}
66
67func (p *Payment) UpdateStatus(status Status) {
68 p.mu.Lock()
69 defer p.mu.Unlock()
70
71 p.Status = status
72}