-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconfirm_status.go
287 lines (262 loc) · 8.17 KB
/
confirm_status.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
281
282
283
284
285
286
287
package unipay
import (
"crypto/md5"
"das_sub_account/config"
"das_sub_account/dao"
"das_sub_account/encrypt"
"das_sub_account/notify"
"das_sub_account/tables"
"encoding/json"
"fmt"
"github.com/btcsuite/btcd/btcutil/base58"
"github.com/dotbitHQ/das-lib/common"
"github.com/dotbitHQ/das-lib/core"
"github.com/dotbitHQ/das-lib/http_api"
"github.com/labstack/gommon/random"
"strings"
"time"
)
func (t *ToolUniPay) RunConfirmStatus() {
tickerSearchStatus := time.NewTicker(time.Minute * 5)
t.Wg.Add(1)
go func() {
defer http_api.RecoverPanic()
for {
select {
case <-tickerSearchStatus.C:
log.Info("doConfirmStatus start")
if err := t.doConfirmStatus(); err != nil {
log.Errorf("doConfirmStatus err: %s", err.Error())
notify.SendLarkErrNotify("doConfirmStatus", err.Error())
}
log.Info("doConfirmStatus end")
case <-t.Ctx.Done():
log.Info("RunRefund done")
t.Wg.Done()
return
}
}
}()
}
func (t *ToolUniPay) doConfirmStatus() error {
// for check order pay status
pendingList, err := t.DbDao.GetPayHashStatusPendingList()
if err != nil {
return fmt.Errorf("GetPayHashStatusPendingList err: %s", err.Error())
}
var orderIdList []string
for _, v := range pendingList {
orderIdList = append(orderIdList, v.OrderId)
}
// for check refund status
refundingList, err := t.DbDao.GetRefundStatusRefundingList()
if err != nil {
return fmt.Errorf("GetRefundStatusRefundingList err: %s", err.Error())
}
var payHashList []string
for _, v := range refundingList {
payHashList = append(payHashList, v.PayHash)
}
if len(orderIdList) == 0 && len(payHashList) == 0 {
return nil
}
log.Info("doConfirmStatus:", len(orderIdList), len(payHashList))
// call unipay
resp, err := GetPaymentInfo(ReqPaymentInfo{
BusinessId: BusinessIdAutoSubAccount,
OrderIdList: orderIdList,
PayHashList: payHashList,
})
if err != nil {
return fmt.Errorf("OrderInfo err: %s", err.Error())
}
var orderIdMap = make(map[string][]PaymentInfo)
var payHashMap = make(map[string]PaymentInfo)
for i, v := range resp.PaymentList {
orderIdMap[v.OrderId] = append(orderIdMap[v.OrderId], resp.PaymentList[i])
payHashMap[v.PayHash] = resp.PaymentList[i]
}
// payment confirm
for _, pending := range pendingList {
paymentInfoList, ok := orderIdMap[pending.OrderId]
if !ok {
min := pending.PayHashUnconfirmedMin()
log.Info("PayHashUnconfirmedMin:", pending.OrderId, min)
//if min > 10 {
// notify.SendLarkErrNotify( "doConfirmStatus", pending.OrderId)
//}
//if min > 60 {
// if err := t.DbDao.UpdateOrderStatusToFailForUnconfirmedPayHash(pending.OrderId, pending.PayHash); err != nil {
// return fmt.Errorf("UpdateOrderStatusToFailForUnconfirmedPayHash err: %s", err.Error())
// }
//}
continue
}
for _, v := range paymentInfoList {
if v.PayHashStatus != tables.PayHashStatusConfirmed {
continue
}
if err = DoPaymentConfirm(t.DasCore, t.DbDao, v.OrderId, v.PayHash); err != nil {
log.Errorf("DoPaymentConfirm err: %s", err.Error())
}
}
}
// refund confirm
for _, v := range payHashList {
paymentInfo, ok := payHashMap[v]
if !ok {
continue
}
if paymentInfo.RefundStatus != tables.RefundStatusRefunded {
continue
}
if err = t.DbDao.UpdateRefundStatusToRefunded(paymentInfo.PayHash, paymentInfo.OrderId, paymentInfo.RefundHash); err != nil {
log.Error("UpdateRefundStatusToRefunded err: ", err.Error())
}
}
return nil
}
type ReqCouponOrderCreate struct {
core.ChainTypeAddress
Account string `json:"account" binding:"required"`
TokenId tables.TokenId `json:"token_id" binding:"required"`
Num int64 `json:"num" binding:"min=1,max=10000"`
Cid string `json:"cid" binding:"required"`
Name string `json:"name" binding:"required"`
Note string `json:"note"`
Price string `json:"price" binding:"required"`
BeginAt int64 `json:"begin_at"`
ExpiredAt int64 `json:"expired_at" binding:"required"`
}
func DoPaymentConfirm(dasCore *core.DasCore, dbDao *dao.DbDao, orderId, payHash string) error {
order, err := dbDao.GetOrderByOrderID(orderId)
if err != nil {
return fmt.Errorf("GetOrderByOrderID err: %s", err.Error())
}
if order.Id == 0 {
return fmt.Errorf("order[%s] not exist", orderId)
}
paymentInfo := tables.PaymentInfo{
PayHash: payHash,
OrderId: orderId,
PayHashStatus: tables.PayHashStatusConfirmed,
Timestamp: time.Now().UnixMilli(),
}
if order.ActionType == tables.ActionTypeMint ||
order.ActionType == tables.ActionTypeRenew {
smtRecord := tables.TableSmtRecordInfo{
SvrName: order.SvrName,
AccountId: order.AccountId,
RecordType: tables.RecordTypeDefault,
MintType: tables.MintTypeAutoMint,
OrderID: order.OrderId,
Action: common.DasActionUpdateSubAccount,
ParentAccountId: tables.GetParentAccountId(order.Account),
Account: order.Account,
Timestamp: time.Now().UnixNano() / 1e6,
}
switch order.ActionType {
case tables.ActionTypeMint:
owner := core.DasAddressHex{
DasAlgorithmId: order.AlgorithmId,
AddressHex: order.PayAddress,
}
args, err := dasCore.Daf().HexToArgs(owner, owner)
if err != nil {
return fmt.Errorf("HexToArgs err: %s", err.Error())
}
charsetList, err := dasCore.GetAccountCharSetList(order.Account)
if err != nil {
return fmt.Errorf("GetAccountCharSetList err: %s", err.Error())
}
content, err := json.Marshal(charsetList)
if err != nil {
return fmt.Errorf("json Marshal err: %s", err.Error())
}
smtRecord.RegisterYears = order.Years
smtRecord.RegisterArgs = common.Bytes2Hex(args)
smtRecord.Content = string(content)
smtRecord.SubAction = common.SubActionCreate
case tables.ActionTypeRenew:
smtRecord.RenewYears = order.Years
smtRecord.SubAction = common.SubActionRenew
accInfo, err := dbDao.GetAccountInfoByAccountId(order.AccountId)
if err != nil {
return err
}
if accInfo.Id == 0 {
return fmt.Errorf("account: [%s] no exist", order.Account)
}
smtRecord.Nonce = accInfo.Nonce + 1
}
rowsAffected, err := dbDao.UpdateOrderPayStatusOkWithSmtRecord(paymentInfo, smtRecord)
if err != nil {
return fmt.Errorf("UpdateOrderPayStatusOkWithSmtRecord err: %s", err.Error())
}
if rowsAffected == 0 {
log.Warnf("doUniPayNotice: %s %d", orderId, rowsAffected)
notify.SendLarkErrNotify("multiple orders success", orderId)
}
}
if order.ActionType == tables.ActionTypeCouponCreate {
req := &ReqCouponOrderCreate{}
if err := json.Unmarshal([]byte(order.MetaData), req); err != nil {
return err
}
couponCodes := make(map[string]struct{})
for {
if err := createCoupon(couponCodes, req); err != nil {
return err
}
exist, err := dbDao.CouponExists(couponCodes)
if err != nil {
return err
}
for _, v := range exist {
delete(couponCodes, v)
}
if len(exist) == 0 {
break
}
}
couponSetInfo, err := dbDao.GetCouponSetInfoByOrderId(orderId)
if err != nil {
return err
}
couponInfos := make([]tables.CouponInfo, 0, len(couponCodes))
for k := range couponCodes {
code := k
couponInfos = append(couponInfos, tables.CouponInfo{
Cid: couponSetInfo.Cid,
Code: code,
})
}
rowsAffected, err := dbDao.UpdateOrderPayStatusOkWithCoupon(paymentInfo, couponSetInfo, couponInfos)
if err != nil {
return fmt.Errorf("UpdateOrderPayStatusOkWithCouponSetInfo err: %s", err.Error())
}
if rowsAffected == 0 {
log.Warnf("doUniPayNotice: %s %d", orderId, rowsAffected)
notify.SendLarkErrNotify("multiple orders success", orderId)
}
}
return nil
}
func createCoupon(couponCodes map[string]struct{}, req *ReqCouponOrderCreate) error {
for {
md5Res := md5.Sum([]byte(fmt.Sprintf("%s%d%d%s", req.Price, time.Now().UnixNano(), req.ExpiredAt, random.String(8, random.Alphanumeric))))
base58Res := base58.Encode([]byte(fmt.Sprintf("%x", md5Res)))
code, err := encrypt.AesEncrypt(strings.ToUpper(base58Res[:8]), config.Cfg.Das.Coupon.EncryptionKey)
if err != nil {
return err
}
if _, ok := couponCodes[code]; ok {
continue
}
couponCodes[code] = struct{}{}
if int64(len(couponCodes)) >= req.Num {
break
}
}
return nil
}