forked from microservices-demo/payment
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.go
69 lines (59 loc) · 1.64 KB
/
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
package payment
import (
"errors"
"fmt"
"time"
)
// Middleware decorates a service.
type Middleware func(Service) Service
type Service interface {
Authorise(total float32) (Authorisation, error) // GET /paymentAuth
Health() []Health // GET /health
}
type Authorisation struct {
Authorised bool `json:"authorised"`
Message string `json:"message"`
}
type Health struct {
Service string `json:"service"`
Status string `json:"status"`
Time string `json:"time"`
}
// NewFixedService returns a simple implementation of the Service interface,
// fixed over a predefined set of socks and tags. In a real service you'd
// probably construct this with a database handle to your socks DB, etc.
func NewAuthorisationService(declineOverAmount float32) Service {
return &service{
declineOverAmount: declineOverAmount,
}
}
type service struct {
declineOverAmount float32
}
func (s *service) Authorise(amount float32) (Authorisation, error) {
if amount == 0 {
return Authorisation{}, ErrInvalidPaymentAmount
}
if amount < 0 {
return Authorisation{}, ErrInvalidPaymentAmount
}
authorised := false
message := "Payment declined"
if amount <= s.declineOverAmount {
authorised = true
message = "Payment authorised"
} else {
message = fmt.Sprintf("Payment declined: amount exceeds %.2f", s.declineOverAmount)
}
return Authorisation{
Authorised: authorised,
Message: message,
}, nil
}
func (s *service) Health() []Health {
var health []Health
app := Health{"payment", "OK", time.Now().String()}
health = append(health, app)
return health
}
var ErrInvalidPaymentAmount = errors.New("Invalid payment amount")