/
payment.go
145 lines (109 loc) · 3.9 KB
/
payment.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
package payment
import (
"net/http"
"github.com/segmentio/encoding/json"
"github.com/go-chi/chi/v5"
"go.opentelemetry.io/otel/trace"
"google.golang.org/protobuf/encoding/protojson"
payment_application "github.com/batazor/shortlink/internal/services/billing/application/payment"
billing "github.com/batazor/shortlink/internal/services/billing/domain/billing/payment/v1"
)
type PaymentAPI struct {
jsonpb protojson.MarshalOptions // nolint:structcheck
paymentService *payment_application.PaymentService
}
func New(paymentService *payment_application.PaymentService) (*PaymentAPI, error) {
return &PaymentAPI{
paymentService: paymentService,
}, nil
}
// Routes creates a REST router
func (api *PaymentAPI) Routes(r chi.Router) {
r.Get("/payment/{id}", api.get)
r.Get("/payments", api.list)
r.Post("/payment", api.open)
r.Delete("/payment/{id}", api.close)
}
// open a new payment
func (api *PaymentAPI) open(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-type", "application/json")
// inject spanId in response header
w.Header().Add("trace-id", trace.LinkFromContext(r.Context()).SpanContext.TraceID().String())
// Parse request
decoder := json.NewDecoder(r.Body)
var request billing.Payment
err := decoder.Decode(&request)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
_, _ = w.Write([]byte(`{"error": "` + err.Error() + `"}`)) // nolint:errcheck
return
}
newPayment, err := api.paymentService.Add(r.Context(), &request)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
_, _ = w.Write([]byte(`{"error": "` + err.Error() + `"}`)) // nolint:errcheck
return
}
res, err := api.jsonpb.Marshal(newPayment)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
_, _ = w.Write([]byte(`{"error": "` + err.Error() + `"}`)) // nolint:errcheck
return
}
w.WriteHeader(http.StatusCreated)
_, _ = w.Write(res) // nolint:errcheck
}
// get payment by identity
func (api *PaymentAPI) get(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-type", "application/json")
// inject spanId in response header
w.Header().Add("trace-id", trace.LinkFromContext(r.Context()).SpanContext.TraceID().String())
aggregateId := chi.URLParam(r, "id")
if aggregateId == "" {
w.WriteHeader(http.StatusBadRequest)
_, _ = w.Write([]byte(`{"error": "need set payment of identity"}`)) // nolint:errcheck
return
}
getPayment, err := api.paymentService.Get(r.Context(), aggregateId)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
_, _ = w.Write([]byte(`{"error": "` + err.Error() + `"}`)) // nolint:errcheck
return
}
res, err := api.jsonpb.Marshal(getPayment)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
_, _ = w.Write([]byte(`{"error": "` + err.Error() + `"}`)) // nolint:errcheck
return
}
w.WriteHeader(http.StatusOK)
_, _ = w.Write(res) // nolint:errcheck
}
// list - get all payments of users
func (api *PaymentAPI) list(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-type", "application/json")
// inject spanId in response header
w.Header().Add("trace-id", trace.LinkFromContext(r.Context()).SpanContext.TraceID().String())
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte(`{}`))
}
// close a payment
func (api *PaymentAPI) close(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-type", "application/json")
// inject spanId in response header
w.Header().Add("trace-id", trace.LinkFromContext(r.Context()).SpanContext.TraceID().String())
aggregateId := chi.URLParam(r, "id")
if aggregateId == "" {
w.WriteHeader(http.StatusBadRequest)
_, _ = w.Write([]byte(`{"error": "need set payment of identity"}`)) // nolint:errcheck
return
}
err := api.paymentService.Close(r.Context(), aggregateId)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
_, _ = w.Write([]byte(`{"error": "` + err.Error() + `"}`)) // nolint:errcheck
return
}
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte(`{}`))
}