forked from digota/digota
/
service.go
209 lines (164 loc) · 5.7 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
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// Digota <http://digota.com> - eCommerce microservice
// Copyright (c) 2018 Yaron Sumel <yaron@digota.com>
//
// MIT License
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
package service
import (
"github.com/digota/digota/locker"
paymentInterface "github.com/digota/digota/payment"
"github.com/digota/digota/payment/paymentpb"
"github.com/digota/digota/payment/service/providers"
"github.com/digota/digota/storage"
"github.com/digota/digota/storage/object"
"github.com/digota/digota/util"
"github.com/digota/digota/validation"
"golang.org/x/net/context"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"time"
)
const ns = "charge"
func init() {
paymentInterface.RegisterService(&paymentService{})
}
type charges []*paymentpb.Charge
func (c *charges) GetNamespace() string { return ns }
type charge struct {
paymentpb.Charge `bson:",inline"`
}
func (c *charge) GetNamespace() string { return ns }
func (c *charge) SetId(id string) { c.Id = id }
func (c *charge) SetUpdated(t int64) { c.Updated = t }
func (c *charge) SetCreated(t int64) { c.Created = t }
type paymentService struct{}
// Get implements the payment.pb/Get method.
// Get charge object by charge id or error.
func (p *paymentService) Get(ctx context.Context, req *paymentpb.GetRequest) (*paymentpb.Charge, error) {
if err := validation.Validate(req); err != nil {
return nil, err
}
c := &charge{
Charge: paymentpb.Charge{
Id: req.GetId(),
},
}
unlock, err := locker.Handler().TryLock(c, locker.DefaultTimeout)
if err != nil {
return nil, err
}
defer unlock()
return &c.Charge, storage.Handler().One(c)
}
// Get implements the payment.pb/Get method.
// Get charge object by charge id or error.
func (p *paymentService) List(ctx context.Context, req *paymentpb.ListRequest) (*paymentpb.ChargeList, error) {
if err := validation.Validate(req); err != nil {
return nil, err
}
slice := &charges{}
n, err := storage.Handler().List(slice, object.ListOpt{
Limit: req.GetLimit(),
Page: req.GetPage(),
Sort: object.SortNatural,
})
if err != nil {
return nil, err
}
return &paymentpb.ChargeList{Charges: *slice, Total: int32(n)}, nil
}
// Charge
func (p *paymentService) NewCharge(ctx context.Context, req *paymentpb.ChargeRequest) (*paymentpb.Charge, error) {
if err := validation.Validate(req); err != nil {
return nil, err
}
provider := providers.Provider(req.GetPaymentProviderId())
// check if card type is supported with payment provider
if err := func() error {
for _, v := range provider.SupportedCards() {
if req.GetCard().GetType() == v {
return nil
}
}
return status.Error(codes.Internal, "Card type is not supported with payment provider.")
}(); err != nil {
return nil, err
}
ch, err := provider.Charge(req)
if err != nil {
return nil, err
}
//if ch == nil {
// return nil, status.Error(codes.Internal, "Something went wrong with the charge. 0")
//}
charge := &charge{
Charge: *ch,
}
// critical operations wrapped util.Retry to keep trying when failing
if err := util.Retry(func() (err error) { return storage.Handler().Insert(charge) }); err != nil {
// if Insert failed => refund that amount instantly with the provider
if _, err := provider.Refund(ch.ProviderChargeId, uint64(req.GetTotal()), req.GetCurrency(), paymentpb.RefundReason_GeneralError); err != nil {
return nil, err
}
return nil, status.Error(codes.Internal, "Something went wrong with the charge.")
}
return &charge.Charge, nil
}
// Refund
//
//
//
//
func (p *paymentService) RefundCharge(ctx context.Context, req *paymentpb.RefundRequest) (*paymentpb.Charge, error) {
if err := validation.Validate(req); err != nil {
return nil, err
}
c := &charge{
Charge: paymentpb.Charge{
Id: req.GetId(),
},
}
unlock, err := locker.Handler().TryLock(c, time.Second)
if err != nil {
return nil, err
}
defer unlock()
if err := storage.Handler().One(c); err != nil {
return nil, err
}
if !c.Paid || c.GetChargeAmount() <= 0 || req.GetAmount() > c.GetChargeAmount() || c.GetRefundAmount()+req.GetAmount() > c.GetChargeAmount() {
return nil, status.Error(codes.Canceled, "Refund is unavailable for this charge.")
}
refund, err := providers.Provider(c.ProviderId).Refund(c.ProviderChargeId, uint64(req.GetAmount()), c.GetCurrency(), req.GetReason())
if err != nil {
return nil, err
}
// append refund to refunds
c.Refunds = append(c.Refunds, refund)
// sum amount of refunds
c.RefundAmount += refund.RefundAmount
// mark as refunded
c.Refunded = true
// update charge
if err := util.Retry(func() (err error) { return storage.Handler().Update(c) }); err != nil {
return nil, status.Error(codes.DataLoss, "Storage could not update object.")
}
// return response
return &c.Charge, nil
}