/
paymentServiceStub.go
134 lines (113 loc) · 3.77 KB
/
paymentServiceStub.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
package paymentservice
import (
"context"
"time"
"github.com/Falokut/cinema_orders_service/internal/models"
"github.com/sirupsen/logrus"
)
type OrdersRepository interface {
ChangeOrderStatus(ctx context.Context, orderId string, newStatus models.OrderItemStatus) error
ChangeOrderItemsStatus(ctx context.Context, orderId string,
itemsIds []string, newStatus models.OrderItemStatus) error
GetOrderTotalPrice(ctx context.Context, orderId string) (uint32, error)
GetOrderItemsTotalPrice(ctx context.Context, orderId string, itemsIds []string) (uint32, error)
}
type PaymentServiceStub struct {
paymenturl string
paymentSleepTime time.Duration
refundSleepTime time.Duration
repo OrdersRepository
logger *logrus.Logger
}
func NewPaymentServiceStub(paymentStubUrl string,
paymentSleepTime time.Duration,
refundSleepTime time.Duration,
repo OrdersRepository,
logger *logrus.Logger,
) *PaymentServiceStub {
return &PaymentServiceStub{
paymenturl: paymentStubUrl,
paymentSleepTime: paymentSleepTime,
refundSleepTime: refundSleepTime,
repo: repo,
logger: logger,
}
}
func (s *PaymentServiceStub) ChangeOrderStatus(ctx context.Context, orderId string, newStatus models.OrderItemStatus) (err error) {
retry := 0
for retry <= 2 {
err = s.repo.ChangeOrderStatus(ctx, orderId, newStatus)
if err == nil {
return nil
}
s.logger.Errorf("error while changing order status %v", err)
retry++
time.Sleep(time.Millisecond * 200)
}
return
}
func (s *PaymentServiceStub) ChangeOrderItemsStatus(ctx context.Context, orderId string,
itemsIds []string, newStatus models.OrderItemStatus) (err error) {
retry := 0
for retry <= 2 {
err = s.repo.ChangeOrderItemsStatus(ctx, orderId, itemsIds, newStatus)
if err == nil {
return nil
}
s.logger.Errorf("error while changing order items status %v", err)
retry++
time.Sleep(time.Millisecond * 200)
}
return
}
func (s *PaymentServiceStub) PreparePaymentUrl(ctx context.Context, email string, total uint32, orderId string) (paymenturl string, err error) {
go func() {
time.Sleep(s.paymentSleepTime)
err := s.ChangeOrderStatus(context.Background(), orderId, models.ORDER_ITEM_STATUS_PAID)
if err != nil {
s.orderRefundRequest(context.Background(), float64(total), orderId)
}
}()
paymenturl = s.paymenturl
return
}
func (s *PaymentServiceStub) RequestOrderRefund(ctx context.Context, percent uint32, orderId string) (err error) {
total, err := s.repo.GetOrderTotalPrice(ctx, orderId)
if err != nil {
return
}
go func() {
s.orderRefundRequest(ctx, float64(total)*float64(percent)/100.0, orderId)
}()
return
}
func (s *PaymentServiceStub) RequestOrderItemsRefund(ctx context.Context,
percent uint32, orderId string, itemsIds []string) (err error) {
total, err := s.repo.GetOrderItemsTotalPrice(ctx, orderId, itemsIds)
if err != nil {
return
}
go func() {
s.orderItemsRefundRequest(ctx, float64(total)*float64(percent)/100.0, orderId, itemsIds)
}()
return
}
func (s *PaymentServiceStub) orderItemsRefundRequest(ctx context.Context,
total float64, orderId string, itemsIds []string) (err error) {
err = s.ChangeOrderItemsStatus(context.Background(), orderId, itemsIds, models.ORDER_ITEM_STATUS_REFUND_AWAITING)
if err != nil {
return
}
time.Sleep(s.refundSleepTime)
err = s.ChangeOrderItemsStatus(context.Background(), orderId, itemsIds, models.ORDER_ITEM_STATUS_REFUNDED)
return
}
func (s *PaymentServiceStub) orderRefundRequest(ctx context.Context, total float64, orderId string) (err error) {
err = s.ChangeOrderStatus(context.Background(), orderId, models.ORDER_ITEM_STATUS_REFUND_AWAITING)
if err != nil {
return
}
time.Sleep(s.refundSleepTime)
err = s.ChangeOrderStatus(context.Background(), orderId, models.ORDER_ITEM_STATUS_REFUNDED)
return
}