package pluspay import ( "errors" "fmt" "net/http" "server/common" "server/config" "server/modules/pay/base" "server/modules/pay/values" "server/pb" "server/util" "strings" "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.HttpTypeForm b.ShouldSignUpper = true b.WhiteIPs = whiteIPs 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.RetCode != "SUCCESS" || resp.PayURL == "" { return nil, errors.New("pay fail") } return &pb.InnerRechargeResp{APIOrderID: resp.PlatOrder, URL: resp.PayURL, Channel: uint32(values.PlusPay)}, nil } resp := s.Base.Resp.(*WithdrawResp) if s.Base.Status == 0 && resp.RetCode != "SUCCESS" { return nil, errors.New("withdraw fail") } return &pb.InnerWithdrawResp{APIOrderID: resp.PlatOrder, Channel: uint32(values.PlusPay)}, nil } func (s *Sub) PackPayReq() interface{} { r := s.Base.PayReq send := &PayReq{ MchID: mid, OrderNo: r.OrderID, Amount: util.RoundFloat(float64(r.Amount)/common.DecimalDigits, 2), Product: "baxipix", BankCode: "all", Goods: fmt.Sprintf("email:%v/name:%v/phone:%v", r.Email, r.Name, r.Phone), // email:520155@gmail.com/name:tom/phone:7894561230 // Goods: "testtests", NotifyURL: values.GetPayCallback(values.PlusPay), // NotifyURL: "asdasd", // ReturnURL: "testtest", ReturnURL: values.GetFrontCallback(), } send.Sign = s.Base.SignMD5(send) return send } func (s *Sub) PackWithdrawReq() interface{} { r := s.Base.WithdrawReq if common.PayType(r.PayType) == common.PayTypeCPF { r.Number = strings.ReplaceAll(r.Number, "-", "") r.Number = strings.ReplaceAll(r.Number, ".", "") } bc := strings.ToLower(common.PayType(r.PayType).String()) send := &WithdrawReq{ Type: "api", MchID: mid, MchTransNo: r.OrderID, Amount: util.RoundFloat(float64(r.Amount)/common.DecimalDigits, 2), NotifyURL: values.GetWithdrawCallback(values.PlusPay), AccountName: r.Name, AccountNo: r.Number, BankCode: bc, RemarkInfo: fmt.Sprintf("email:%v/phone:%v/mode:pix/%v:%v", r.Email, r.Phone, bc, r.Number), // email:520155@gmail.com/phone:9784561230/mode:pix/cpf:555555555(必须是真实的) } send.Sign = s.Base.SignMD5(send) return send } func (s *Sub) CheckSign(str string) bool { str += "&key=" + key checkSign := "" s.Base.CallbackResp.Msg = "success" if s.Base.Opt == 3 { req := s.Base.CallbackReq.(*PayCallbackReq) log.Debug("checkSign pay:%+v", *req) checkSign = req.Sign s.Base.CallbackResp.OrderID = req.OrderNo s.Base.CallbackResp.Success = req.Status == "2" } else if s.Base.Opt == 4 { req := s.Base.CallbackReq.(*WithdrawCallbackReq) log.Debug("checkSign withdraw:%+v", *req) if req.Status == "1" { return false } checkSign = req.Sign s.Base.CallbackResp.OrderID = req.MchTransNo s.Base.CallbackResp.Success = req.Status == "2" s.Base.CallbackResp.APIOrderID = req.MchTransNo s.Base.CallbackResp.FailMessage = req.Msg } return strings.ToUpper(util.CalculateMD5(str)) == checkSign }