package handler import ( "encoding/json" "fmt" "server/call" "server/config" "server/db" "server/modules/web/app" "server/pb" "server/util" "time" "server/common" "server/modules/web/values" "github.com/gin-gonic/gin" "github.com/liangdas/mqant/log" ) func CheckRechargeOrder(c *gin.Context) { a := app.NewApp(c) defer func() { a.Response() }() req := new(values.RechargeOrderReq) if !a.S(req) { return } // log.Debug("req:%+v", *req) one := &common.RechargeOrder{OrderID: req.OrderID} db.Mysql().Get(one) resp := values.RechargeOrderResp{} resp.Status = int(one.Status) a.Data = resp // log.Debug("resp:%+v", resp) } func RechargeInfo(c *gin.Context) { a := app.NewApp(c) defer func() { a.Response() }() list := call.GetConfigPayProduct() resp := &values.RechargeInfoResp{Tips: call.GetConfigPlatform().PayTips, SelectID: config.GetConfig().Web.SelectID} a.Data = resp resp.Channels = call.GetConfigPayChannels() // payData := call.GetPlayerPayData(a.UID) a.GetUID() re := &common.RechargeInfo{} if a.UID > 0 { re = call.GetRechargeInfo(a.UID) } for _, v := range call.GetConfigFirstPay() { one := values.OneConfigPayBonus{ Amount: v.Amount, } if re.TotalRecharge > 0 { one.Per = v.Per } else { one.Per = v.FirstPer } resp.ConfigPayBonus = append(resp.ConfigPayBonus, one) } for _, v := range list { one := *v if v.IfSell == 2 { continue } if v.Type == common.CurrencyUSDT { for _, j := range resp.Channels { if j.CurrencyType == common.CurrencyUSDT { if j.PayDown <= v.Amount && j.PayUp >= v.Amount { one.Channels = append(one.Channels, j.ChannelID) } } } resp.List = append(resp.List, one) } else if v.Type == common.CurrencyBrazil { for _, j := range resp.Channels { if j.CurrencyType == common.CurrencyBrazil { if j.PayDown <= v.Amount && j.PayUp >= v.Amount { one.Channels = append(one.Channels, j.ChannelID) } } } resp.List = append(resp.List, one) } } if a.UID > 0 { // 判断是否有折扣券 tickets := call.GetUserValidItems(a.UID, common.ItemDiscountTicket) discount := 0 for _, v := range tickets { thisDiscount := v.Exi1 diff := v.Time + common.ActivityWeekCardTicketExpireTime - time.Now().Unix() if diff <= 0 { id := v.ID util.Go(func() { db.Mysql().Update(&common.PlayerItems{ID: id}, map[string]interface{}{"status": common.ItemStatusInvalid}) }) continue } if discount < thisDiscount { discount = thisDiscount resp.DiscountTicket = &values.DiscountTicket{ // Level: con.Level, Discount: fmt.Sprintf("%d", 100-discount), TimeLeft: diff, // Range: , } con := call.GetConfigActivityWeekCardByLevel(1) if con != nil { resp.DiscountTicket.Range = con.SubRange } if discount > 50 { resp.DiscountTicket.Level = 1 } else { resp.DiscountTicket.Level = 2 } } } } } func RechargeHistory(c *gin.Context) { a := app.NewApp(c) defer func() { a.Response() }() req := new(values.RechargeHistoryReq) if !a.S(req) { return } ret := []common.RechargeOrder{} var count int64 var err error count, err = db.Mysql().QueryListW(req.Page, req.Num, "create_time desc", &common.RechargeOrder{UID: a.UID}, &ret, "uid = ? and event = ?", a.UID, common.CurrencyEventReCharge) if err != nil { log.Error("err:%v", err) a.Code = values.CodeRetry return } resp := values.RechargeHistoryResp{ Count: count, List: ret, } a.Data = resp } func PlayerRecharge(c *gin.Context) { a := app.NewApp(c) defer func() { a.Response() }() req := new(values.RechargeReq) if !a.S(req) { a.Code = values.CodeParam return } log.Debug("player %v recharge:%+v", a.UID, *req) if req.CurrencyType == 0 { req.CurrencyType = common.CurrencyBrazil } if req.ProductID > 0 { product := call.GetConfigPayProductByID(req.ProductID) if product == nil { a.Code = values.CodeRetry return } req.Amount = product.Amount } else { req.Amount = common.RoundCurrency(req.CurrencyType, req.Amount) } // product := call.GetConfigPayProductByID(req.ProductID) // if product == nil { // log.Error("unknow product:%v", req.ProductID) // a.Code = values.CodeParam // a.Msg = "Activity is over" // return // } // if !a.CanBuyProduct(product.ActivityID, product.ProductID) { // return // } // paySource := req.PaySource // if paySource >= common.PaySourceAll { // a.Code = values.CodeParam // log.Error("paysource err:%v", req.PaySource) // return // } // 判断充值间隔 if db.Redis().Exist(common.GetRedisKeyPlayerPayInterval(a.UID)) { a.Code = values.CodeParam a.Msg = "You have an order to pay,please process first" return } // if req.UserPhone == "" { // req.UserPhone = util.CheckPhone(req.UserPhone) // } payImp := NewRechargeImp(req, a.UID, a.Channel, a.GetRemoteIP()) if payImp == nil { a.Code = values.CodeRetry log.Error("req params err:%v", req) return } code := payImp.Recharge() if code != values.CodeOK { a.Code = code a.Msg = "channel unavailable" return } // ok a.Data = payImp.Data a.Msg = payImp.Order.OrderID log.Debug("player %v recharge resp:%v,msg:%v", a.UID, a.Data, a.Msg) uid := a.UID util.Go(func() { db.Redis().SetData(common.GetRedisKeyPlayerPayInterval(uid), 1, 15*time.Second) }) if !config.GetBase().Release && req.PayChannel == config.GetConfig().Web.TestPay { util.Go(func() { call.RechargeCallback(payImp.Order, true, "", "") }) } } func NewRechargeImp(req *values.RechargeReq, uid, cid int, ip string) *RechargeImp { r := new(RechargeImp) r.Channel = call.GetChannelByID(cid) if r.Channel == nil { log.Error("invalid cid:%v", cid) return nil } order := &common.RechargeOrder{ // ProductID: product.ProductID, Amount: product.Amount, CreateTime: time.Now().Unix(), CurrencyType: req.CurrencyType, Amount: req.Amount, ChannelID: cid, UID: uid, Status: common.StatusROrderCreate, Event: int(common.CurrencyEventReCharge), UPI: req.PayChannel, ProductID: req.ProductID, } // 只有商城购买才能使用优惠券 ticketCon := call.GetConfigActivityWeekCardByLevel(1) if req.ProductID == 0 { // 首先判断折扣券 if ticketCon != nil && len(ticketCon.SubRange) == 2 { if req.Amount >= ticketCon.SubRange[0] && req.Amount <= ticketCon.SubRange[1] { discount := -1 var ticket *common.PlayerItems // 判断是否有折扣券 tickets := call.GetUserValidItems(uid, common.ItemDiscountTicket) for i, v := range tickets { thisDiscount := v.Exi1 diff := v.Time + common.ActivityWeekCardTicketExpireTime - time.Now().Unix() if diff <= 0 { id := v.ID util.Go(func() { db.Mysql().Update(&common.PlayerItems{ID: id}, map[string]interface{}{"status": common.ItemStatusInvalid}) }) continue } if discount < 0 || thisDiscount < discount { discount = thisDiscount ticket = tickets[i] } } if discount > 0 { ticketData := common.ActivityRechargeData{ID: common.ItemDiscountTicket, I1: ticket.Exi1, I2: req.Amount} ticketByte, _ := json.Marshal(ticketData) order.Extra = string(ticketByte) req.Amount = common.RoundCurrency(common.CurrencyBrazil, req.Amount*int64(discount)/100) order.Amount = req.Amount } } } } r.UID = uid info, _ := call.GetUserXInfo(uid, "mobile") p := new(PayImp) p.CurrencyType = req.CurrencyType p.req = new(pb.InnerRechargeReq) p.req.Amount = req.Amount p.req.PlayerChannel = uint32(cid) if req.CurrencyType == common.CurrencyBrazil { // 判断黑名单 if call.BlackListAndKick(uid, &common.BlackList{Phone: info.Mobile}) { return nil } pinfo := &common.PayInfo{UID: uid} db.Mysql().Get(pinfo) // if req.UserName == nil { // log.Error("invalid param:%+v", req) // return nil // } p.req.Name = util.GenerateRandomString(5) p.req.Phone = pinfo.Mobile p.req.IP = ip p.req.Number = pinfo.Number if info.Mobile != "" { p.req.Phone = info.Mobile } if pinfo.Name != "" { p.req.Name = pinfo.Name } if pinfo.Email != "" { p.req.Email = pinfo.Email } } r.Order = order p.base = r r.RechargeIn = p r.PayChannel = req.PayChannel return r } type RechargeIn interface { Recharge() int } // RechargeImp 新充值对象 type RechargeImp struct { Data interface{} RechargeIn Order *common.RechargeOrder // product *common.ConfigPayProduct // tx *gorm.DB UID int Channel *common.Channel PayChannel int } func (r *RechargeImp) CreateRecharge() error { if r.Order.OrderID == "" { r.Order.OrderID = util.NewOrderID(r.UID) } // r.Order.UID = r.UID // r.Order.Status = common.StatusROrderCreate // r.Order.Event = int(common.CurrencyEventReCharge) // r.Order.ChannelID = int(r.Channel.ChannelID) if err := db.Mysql().C().Model(r.Order).Create(r.Order).Error; err != nil { log.Error("create order err:%v", err) return err } return nil } // pay模块支付 type PayImp struct { base *RechargeImp req *pb.InnerRechargeReq CurrencyType common.CurrencyType } func (p *PayImp) Recharge() int { orderID := util.NewOrderID(p.base.UID) // product := p.base.product var resp *pb.InnerRechargeResp var err error if p.CurrencyType == common.CurrencyBrazil { req := &pb.InnerRechargeReq{OrderID: orderID, Amount: p.req.Amount, Phone: p.req.Phone, Name: p.req.Name, UID: uint32(p.base.UID), Channel: uint32(p.base.PayChannel), IsPersonalCard: false, PaySource: common.PaySourceModulePay} p.base.Order.PaySource = common.PaySourceModulePay if p.base.PayChannel < 0 { req.IsPersonalCard = true } resp, err = call.Recharge(req) if err != nil { log.Error("err:%v", err) return values.CodeRetry } p.base.Order.PayChannel = int(resp.Channel) } else { orderID = "USDT" + orderID req := &pb.InnerRechargeReq{OrderID: orderID, Amount: p.req.Amount, UID: uint32(p.base.UID), Channel: uint32(p.base.PayChannel), PaySource: common.PaySourceBlockPay} p.base.Order.PaySource = common.PaySourceBlockPay resp, err = call.Recharge(req) if err != nil { log.Error("err:%v", err) return values.CodeRetry } p.base.Order.Extra = resp.URL } p.base.Data = values.PayResp{OrderID: orderID, Addr: resp.URL} p.base.Order.OrderID = orderID p.base.Order.APIPayID = resp.APIOrderID err = p.base.CreateRecharge() if err != nil { return values.CodeRetry } return values.CodeOK }