forked from lonng/nanoserver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.go
280 lines (238 loc) · 6.78 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package order
import (
"strconv"
"strings"
"time"
"github.com/pborman/uuid"
"github.com/lonnng/nanoserver/db"
"github.com/lonnng/nanoserver/internal/algoutil"
"github.com/lonnng/nanoserver/internal/errutil"
"github.com/lonnng/nanoserver/internal/protocol"
log "github.com/sirupsen/logrus"
"github.com/lonnng/nanoserver/cmd/mahjong/web/provider"
"github.com/lonnng/nanoserver/db/model"
"github.com/lonnng/nanoserver/internal/types"
)
var logger *log.Entry
type Provider interface {
CreateOrderResponse(*model.Order) (interface{}, error)
Notify(request interface{}) (*model.Trade, interface{}, error)
}
type Service interface {
CreateOrder(r *protocol.CreateOrderRequest) (interface{}, error) //创建订单
YXPayOrderList(r *protocol.PayOrderListRequest) ([]protocol.SnakePayOrderInfo, int, error) //pay订单的收支列表
OrderList(r *protocol.OrderListRequest) ([]protocol.OrderInfo, int, error) //获取订单的收支列表
TradeList(r *protocol.TradeListRequest) ([]protocol.TradeInfo, int, error) //获取交易列表
Notify(platform string, r interface{}) (resp interface{}, err error)
BalanceList(uids []string) (map[string]string, error)
GetOptions() string
}
type service struct {
}
var supportOptions = `{
"POST": "/v1/order/",
"POST": "/v1/order/callback/alipay",
"POST": "/v1/order/callback/wechat",
"POST": "/v1/order/callback/unionpay"
}`
//NewService new a service for user
func NewService(l *log.Entry) Service {
logger = l.WithField("service", "order")
return &service{}
}
func (s *service) CreateOrder(r *protocol.CreateOrderRequest) (interface{}, error) {
// todo: query user by uid
p := provider.WithName(types.YX)
if p == nil {
logger.Error(errutil.YXErrInvalidPayPlatform.Error())
return nil, errutil.YXErrInvalidPayPlatform
}
order := &model.Order{
OrderId: strings.Replace(uuid.New(), "-", "", -1),
AppId: r.AppID,
Uid: r.Uid,
ChannelId: r.ChannelID,
PayPlatform: r.Platform,
OrderPlatform: types.YX,
Extra: r.Extra,
ProductName: r.ProductionName,
ProductCount: r.ProductCount,
CreatedAt: time.Now().Unix(),
Status: db.OrderStatusCreated,
Remote: r.Device.Remote,
Ip: r.Device.Remote,
Imei: r.Device.IMEI,
Model: r.Device.Model,
Os: r.Device.OS,
}
resp, err := p.CreateOrderResponse(order)
if err != nil {
logger.Error(err.Error())
return nil, err
}
if err := db.InsertOrder(order); err != nil {
logger.Error(err.Error())
return nil, err
}
return resp, nil
}
func (s *service) BalanceList(uids []string) (map[string]string, error) {
if uids == nil {
return nil, errutil.YXErrIllegalParameter
}
return db.BalanceList(uids)
}
func (s *service) YXPayOrderList(r *protocol.PayOrderListRequest) ([]protocol.SnakePayOrderInfo, int, error) {
if r == nil || r.Type > db.OrderTypeTest || r.Type < db.OrderTypeUnknown {
return nil, 0, errutil.YXErrIllegalParameter
}
list, total, err := db.YXPayOrderList(
r.Uid,
r.AppID,
r.ChannelID,
r.OrderID,
r.Start,
r.End,
r.Type,
r.Offset,
r.Count)
if err != nil {
return nil, 0, err
}
result := make([]protocol.SnakePayOrderInfo, len(list))
for i, order := range list {
result[i] = protocol.SnakePayOrderInfo{
OrderId: order.OrderId,
Uid: strconv.FormatInt(order.Uid, 10),
Money: order.Money,
RealMoney: order.RealMoney,
ProductName: order.ProductName,
ProductCount: order.ProductCount,
ServerName: order.ServerName,
RoleID: order.RoleId,
Type: order.Type,
AppId: order.AppId,
ChannelId: order.ChannelId,
Imei: order.Imei,
Status: order.Status,
Extra: order.Extra,
CreatedAt: order.CreatedAt,
}
}
return result, total, nil
}
func (s *service) OrderList(r *protocol.OrderListRequest) ([]protocol.OrderInfo, int, error) {
if r == nil {
return nil, 0, errutil.YXErrIllegalParameter
}
list, total, err := db.OrderList(
algoutil.RetriveInt64OrDefault(r.Uid, 0),
r.AppID,
r.ChannelID,
r.OrderID,
r.PayBy,
r.Start,
r.End,
int(r.Status),
r.Offset,
r.Count)
if err != nil {
return nil, 0, err
}
result := make([]protocol.OrderInfo, len(list))
for i, order := range list {
result[i] = protocol.OrderInfo{
OrderId: order.OrderId,
Uid: strconv.FormatInt(order.Uid, 10),
Money: order.Money,
RealMoney: order.RealMoney,
ProductName: order.ProductName,
ProductCount: order.ProductCount,
ServerName: order.ServerName,
RoleID: order.RoleId,
PayBy: order.PayPlatform,
AppId: order.AppId,
Imei: order.Imei,
Status: order.Status,
Extra: order.Extra,
CreatedAt: order.CreatedAt,
}
}
return result, total, nil
}
func (s *service) TradeList(r *protocol.TradeListRequest) ([]protocol.TradeInfo, int, error) {
if r == nil {
return nil, 0, errutil.YXErrIllegalParameter
}
list, total, err := db.TradeList(
r.AppID,
r.ChannelID,
r.OrderID,
r.Start,
r.End,
r.Offset,
r.Count)
if err != nil {
return nil, 0, err
}
result := make([]protocol.TradeInfo, len(list))
for i, order := range list {
result[i] = protocol.TradeInfo{
OrderId: order.OrderId,
Uid: strconv.FormatInt(order.Uid, 10),
Money: order.Money,
RealMoney: order.RealMoney,
ProductName: order.ProductName,
ProductCount: order.ProductCount,
ServerName: order.ServerName,
RoleId: order.RoleId,
PayBy: order.PayPlatform,
AppId: order.AppId,
ChannelId: order.ChannelId,
PayPlatformUid: order.ComsumerId,
PayAt: order.PayAt,
Currency: order.Currency,
}
}
return result, total, nil
}
func (s *service) Notify(platform string, r interface{}) (resp interface{}, err error) {
var (
p = provider.WithName(platform)
trade *model.Trade
order *model.Order
)
if p == nil {
logger.Error("invalid pay platform: " + platform)
return nil, errutil.YXErrInvalidPayPlatform
}
if trade, resp, err = p.Notify(r); err != nil {
logger.Error(err.Error())
return nil, err
}
if order, err = db.QueryOrder(trade.OrderId); err != nil {
logger.Error(err.Error())
return nil, err
}
if err := db.InsertTrade(trade); err != nil {
//如果是重复通知,直接忽略之
if err == errutil.YXErrTradeExisted {
return resp, nil
}
logger.Error(err.Error())
return nil, err
}
prod, err := db.QueryProduction(order.ProductId)
if err != nil {
logger.Error(err.Error())
return nil, err
}
if err := db.UserAddCoin(order.Uid, int64(prod.Price)); err != nil {
logger.Error(err.Error())
return nil, err
}
return resp, nil
}
func (*service) GetOptions() string {
return supportOptions
}