package gopay import ( "errors" "fmt" "net/http" "server/common" "server/config" "server/modules/pay/base" "server/modules/pay/values" "server/pb" "github.com/gogo/protobuf/proto" "github.com/liangdas/mqant/log" ) func NewSub(b *base.Base) { sub := &Sub{ Base: b, } b.SignKey = key b.HttpType = base.HttpTypeJson b.ShouldSignUpper = true b.Channel = values.GoPay if b.Opt == 1 { b.Resp = new(PayResp) b.ReqURL = payURL } else if b.Opt == 2 { b.Resp = new(WithdrawResp) b.ReqURL = withdrawURL } else if b.Opt == 3 { b.SignPassStr = []string{"sign"} b.CallbackResp.Msg = "SUCCESS" b.CallbackReq = new(PayCallbackReq) } else if b.Opt == 4 { b.SignPassStr = []string{"sign", "msg"} b.CallbackResp.Msg = "SUCCESS" b.CallbackReq = new(WithdrawCallbackReq) } b.Sub = sub } type Sub struct { Base *base.Base } func (s *Sub) PackHeader(header http.Header) { header.Set("Merchant-Id", config.GetConfig().Pay.IGeek.MID) } func (s *Sub) PackReq() interface{} { if s.Base.Opt == 1 { return s.PackPayReq() } return s.PackWithdrawReq() } func (s *Sub) GetResp() (proto.Message, error) { if s.Base.Opt == 1 { resp := s.Base.Resp.(*PayResp) if resp.Code != 200 || resp.Data.PayLink == "" { return nil, errors.New("pay fail") } return &pb.InnerRechargeResp{APIOrderID: fmt.Sprintf("%v", resp.Data.ID), URL: resp.Data.PayLink, Channel: uint32(values.GoPay)}, nil } resp := s.Base.Resp.(*WithdrawResp) if s.Base.Status == 0 && resp.Code != 200 { return nil, errors.New("withdraw fail") } return &pb.InnerWithdrawResp{APIOrderID: fmt.Sprintf("%v", resp.Data.ID), Channel: uint32(values.GoPay)}, nil } func (s *Sub) PackPayReq() interface{} { r := s.Base.PayReq send := &PayReq{ Amount: r.Amount / 100, Currency: "INR", MerID: mid, NotifyURL: values.GetPayCallback(values.GoPay), OrderID: r.OrderID, Type: 1, ReturnURL: values.GetFrontCallback(), UserName: r.Name, Email: r.Email, Mobile: r.Phone, } send.Sign = s.Base.SignMD5(send) return send } func (s *Sub) PackWithdrawReq() interface{} { r := s.Base.WithdrawReq if r.PayType != int64(common.PayTypeBank) { return nil } send := &WithdrawReq{ Amount: r.Amount / 100, Currency: "INR", MerID: mid, NotifyURL: values.GetWithdrawCallback(values.GoPay), OrderID: r.OrderID, Type: 1, BankCode: r.PayCode, Account: r.CardNo, UserName: r.Name, Email: r.Email, Mobile: r.Phone, } send.Sign = s.Base.SignMD5(send) return send } func (s *Sub) CheckSign(str string) bool { // str += "&key=" + key checkSign := "" s.Base.CallbackResp.Msg = "success" mySign := "" if s.Base.Opt == 3 { req := s.Base.CallbackReq.(*PayCallbackReq) log.Debug("checkSign pay:%+v", *req) checkSign = req.Data.Sign s.Base.CallbackResp.OrderID = req.Data.OrderID s.Base.CallbackResp.Success = req.Code == 200 mySign = s.Base.SignMD5(req.Data) } else if s.Base.Opt == 4 { req := s.Base.CallbackReq.(*WithdrawCallbackReq) log.Debug("checkSign withdraw:%+v", *req) checkSign = req.Data.Sign s.Base.CallbackResp.OrderID = req.Data.OrderID s.Base.CallbackResp.Success = req.Code == 200 s.Base.CallbackResp.APIOrderID = req.Data.ID s.Base.CallbackResp.FailMessage = req.Message mySign = s.Base.SignMD5(req.Data) } return mySign == checkSign }