You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
224 lines
5.9 KiB
224 lines
5.9 KiB
package jjpay |
|
|
|
import ( |
|
"errors" |
|
"fmt" |
|
"net/http" |
|
"server/common" |
|
"server/modules/pay/base" |
|
"server/modules/pay/values" |
|
"server/pb" |
|
"time" |
|
|
|
"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.KeyName = "signKey" |
|
if b.Opt == base.OPTPay { |
|
b.Resp = new(PayResp) |
|
b.ReqURL = baseUrl + payURL |
|
} else if b.Opt == base.OPTWithdraw { |
|
b.Resp = new(WithdrawResp) |
|
b.ReqURL = baseUrl + withdrawURL |
|
} else if b.Opt == base.OPTPayCB { |
|
b.SignPassStr = []string{"sign"} |
|
b.CallbackResp.Msg = "success" |
|
b.CallbackReq = new(CallbackReq) |
|
} else if b.Opt == base.OPTWithdrawCB { |
|
b.SignPassStr = []string{"sign"} |
|
b.CallbackResp.Msg = "success" |
|
b.CallbackReq = new(CallbackReq) |
|
} else if b.Opt == base.OPTQueryWithdraw { |
|
b.Resp = new(QueryResp) |
|
b.ReqURL = baseUrl + queryWithdrawURL |
|
} else if b.Opt == base.OPTQueryPay { |
|
b.Resp = new(QueryResp) |
|
b.ReqURL = baseUrl + queryPayURL |
|
} |
|
b.Sub = sub |
|
} |
|
|
|
type Sub struct { |
|
Base *base.Base |
|
} |
|
|
|
func (s *Sub) PackHeader(header http.Header) { |
|
} |
|
|
|
func (s *Sub) PackReq() interface{} { |
|
if s.Base.Opt == base.OPTPay { |
|
return s.PackPayReq() |
|
} else if s.Base.Opt == base.OPTWithdraw { |
|
return s.PackWithdrawReq() |
|
} else if s.Base.Opt == base.OPTQueryWithdraw { |
|
return s.PackQueryWithdrawReq() |
|
} else if s.Base.Opt == base.OPTQueryPay { |
|
return s.PackQueryPayReq() |
|
} |
|
return nil |
|
} |
|
|
|
func (s *Sub) GetResp() (proto.Message, error) { |
|
if s.Base.Opt == 1 { |
|
resp := s.Base.Resp.(*PayResp) |
|
if resp.Code != 200 || resp.Data.Url == "" { |
|
return nil, errors.New("pay fail") |
|
} |
|
return &pb.InnerRechargeResp{APIOrderID: resp.Data.SystemOrderId, URL: resp.Data.Url, Channel: uint32(values.JJPay)}, nil |
|
} else if s.Base.Opt == base.OPTWithdraw { |
|
resp := s.Base.Resp.(*WithdrawResp) |
|
if s.Base.Status == 0 && resp.Code != 200 { |
|
return nil, errors.New("withdraw fail") |
|
} |
|
return &pb.InnerWithdrawResp{APIOrderID: resp.Data.SystemOrderId, Channel: uint32(values.JJPay), Status: uint32(s.Base.Status)}, nil |
|
} else if s.Base.Opt == base.OPTQueryWithdraw { |
|
resp := s.Base.Resp.(*QueryResp) |
|
log.Debug("QueryWithdrawResp:%+v", resp) |
|
ret := &pb.InnerQueryWithdrawResp{APIOrderID: resp.Data.SystemOrderId, OrderID: resp.Data.OrderNo} |
|
s.Base.QueryWithdrawResp.OrderID = resp.Data.OrderNo |
|
s.Base.QueryWithdrawResp.APIOrderID = resp.Data.SystemOrderId |
|
if s.Base.Status == 0 { |
|
if resp.Code == 200 { |
|
if resp.Data.Status == 2 { |
|
s.Base.QueryWithdrawResp.Status = base.QueryStatusOrderSuccess |
|
} |
|
// else if resp.Status == 1 { |
|
// s.Base.QueryWithdrawResp.Status = base.QueryStatusSuccess |
|
// } |
|
} else { |
|
if resp.Data.Status == 3 { |
|
s.Base.QueryWithdrawResp.Status = base.QueryStatusFail |
|
} |
|
} |
|
} |
|
if s.Base.QueryWithdrawResp.Status == 0 { |
|
s.Base.QueryWithdrawResp.Status = base.QueryStatusUnknown |
|
} |
|
return ret, nil |
|
} else { |
|
resp := s.Base.Resp.(*QueryResp) |
|
log.Debug("QueryPayResp:%+v", resp) |
|
ret := &pb.InnerQueryWithdrawResp{APIOrderID: resp.Data.SystemOrderId, OrderID: resp.Data.OrderNo} |
|
s.Base.QueryPayResp.OrderID = resp.Data.OrderNo |
|
s.Base.QueryPayResp.APIOrderID = resp.Data.SystemOrderId |
|
if s.Base.Status == 0 { |
|
if resp.Code == 200 && resp.Data.Status == 2 { |
|
s.Base.QueryPayResp.Status = base.QueryStatusOrderSuccess |
|
} |
|
} |
|
if s.Base.QueryPayResp.Status == 0 { |
|
s.Base.QueryPayResp.Status = base.QueryStatusUnknown |
|
} |
|
return ret, nil |
|
} |
|
} |
|
|
|
func (s *Sub) PackPayReq() interface{} { |
|
r := s.Base.PayReq |
|
send := &PayReq{ |
|
MchId: mid, |
|
OrderNo: r.OrderID, |
|
ReturnUrl: values.GetFrontCallback(), |
|
NotifyUrl: s.Base.GetPayCallbackURL(), |
|
Name: r.Name, |
|
Mobile: r.Phone, |
|
Email: r.Email, |
|
Subject: "shopbuy", |
|
Amount: r.Amount * 100, |
|
Time: time.Now().Unix(), |
|
} |
|
send.Sign = s.Base.SignMD5(send) |
|
return send |
|
} |
|
|
|
func (s *Sub) PackWithdrawReq() interface{} { |
|
r := s.Base.WithdrawReq |
|
send := &WithdrawReq{ |
|
MchId: mid, |
|
OrderNo: r.OrderID, |
|
Amount: r.Amount * 100, |
|
NotifyUrl: s.Base.GetWithdrawCallbackURL(), |
|
// AccountType: , |
|
// AccountCode: , |
|
// IFSC: , |
|
// Account: , |
|
Name: r.Name, |
|
Mobile: r.Phone, |
|
Email: r.Email, |
|
Time: fmt.Sprintf("%d", time.Now().Unix()), |
|
} |
|
|
|
if fmt.Sprintf("%d", r.PayType) == common.WithdrawTypeBank { |
|
send.AccountType = "Bank" |
|
send.AccountCode = r.PayCode |
|
send.IFSC = r.PayCode |
|
send.Account = r.CardNo |
|
} else { |
|
return nil |
|
} |
|
|
|
send.Sign = s.Base.SignMD5(send) |
|
return send |
|
} |
|
|
|
func (s *Sub) PackQueryWithdrawReq() interface{} { |
|
r := s.Base.QueryWithdrawReq |
|
send := &QueryReq{ |
|
MchId: mid, |
|
OrderNo: r.OrderID, |
|
Time: time.Now().Unix(), |
|
Type: "payout", |
|
} |
|
send.Sign = s.Base.SignMD5(send) |
|
return send |
|
} |
|
|
|
func (s *Sub) PackQueryPayReq() interface{} { |
|
r := s.Base.QueryPayReq |
|
send := &QueryReq{ |
|
MchId: mid, |
|
OrderNo: r.OrderID, |
|
Time: time.Now().Unix(), |
|
Type: "payin", |
|
} |
|
send.Sign = s.Base.SignMD5(send) |
|
return send |
|
} |
|
|
|
func (s *Sub) CheckSign(str string) bool { |
|
// str += "&key=" + key |
|
checkSign := "" |
|
// checkSign := s.Base.C.GetHeader("signature") |
|
mySign := s.Base.SignMD5WithStr(str) |
|
// mySign := "" |
|
if s.Base.Opt == base.OPTPayCB { |
|
req := s.Base.CallbackReq.(*CallbackReq) |
|
log.Debug("checkSign pay:%+v", *req) |
|
s.Base.CallbackResp.OrderID = req.OrderNo |
|
s.Base.CallbackResp.Success = req.Status == 2 |
|
// mySign = s.Base.SignMD5(req) |
|
checkSign = req.Sign |
|
} else if s.Base.Opt == base.OPTWithdrawCB { |
|
req := s.Base.CallbackReq.(*CallbackReq) |
|
log.Debug("checkSign withdraw:%+v", *req) |
|
s.Base.CallbackResp.OrderID = req.OrderNo |
|
s.Base.CallbackResp.Success = req.Status == 2 |
|
s.Base.CallbackResp.APIOrderID = req.SystemOrderId |
|
// s.Base.CallbackResp.FailMessage = req.Msg |
|
// mySign = s.Base.SignMD5(req) |
|
checkSign = req.Sign |
|
} |
|
pass := mySign == checkSign |
|
if !pass { |
|
log.Error("check sign err,mySign:%s,checkSign:%s", mySign, checkSign) |
|
} |
|
return pass |
|
}
|
|
|