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.
166 lines
4.4 KiB
166 lines
4.4 KiB
package mlpay |
|
|
|
import ( |
|
"encoding/json" |
|
"errors" |
|
"fmt" |
|
"net/http" |
|
"server/common" |
|
"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.HttpType = base.HttpTypeForm |
|
b.ShouldSignUpper = true |
|
b.SignKey = key |
|
b.Channel = values.MLPay |
|
if b.Opt == base.OPTPay { |
|
b.Resp = new(PayResp) |
|
b.ReqURL = baseUrl + payURL |
|
} else if b.Opt == base.OPTWithdraw { |
|
b.SignKey = withdrawKey |
|
b.Resp = new(WithdrawResp) |
|
b.ReqURL = baseUrl + withdrawURL |
|
} else if b.Opt == base.OPTPayCB { |
|
b.HttpType = base.HttpTypeForm |
|
b.CallbackReq = new(PayCallbackReq) |
|
b.CallbackResp.Msg = "0" |
|
} else if b.Opt == base.OPTWithdrawCB { |
|
b.SignKey = withdrawKey |
|
b.HttpType = base.HttpTypeForm |
|
b.CallbackReq = new(WithdrawCallbackReq) |
|
b.CallbackResp.Msg = "0" |
|
// } else if b.Opt == base.OPTQueryWithdraw { // 查询 |
|
// b.Resp = new(QueryWithdrawResp) |
|
// b.ReqURL = queryWithdrawURL |
|
// } else if b.Opt == base.OPTQueryPay { // 查询 |
|
// b.Resp = new(QueryPayResp) |
|
// b.ReqURL = queryPayURL |
|
} else { |
|
return |
|
} |
|
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() |
|
} |
|
return nil |
|
} |
|
|
|
func (s *Sub) GetResp() (proto.Message, error) { |
|
log.Debug("resp:%v", s.Base.Resp) |
|
if s.Base.Opt == base.OPTPay { |
|
resp := s.Base.Resp.(*PayResp) |
|
if resp.Code != "0000" { |
|
return nil, errors.New("pay fail " + resp.Msg) |
|
} |
|
return &pb.InnerRechargeResp{APIOrderID: s.Base.PayReq.OrderID, URL: resp.Data, Channel: uint32(values.MLPay)}, nil |
|
} else if s.Base.Opt == base.OPTWithdraw { |
|
resp := s.Base.Resp.(*WithdrawResp) |
|
if resp.Code != "0000" { |
|
return nil, errors.New("withdraw fail " + resp.Msg) |
|
} |
|
return &pb.InnerWithdrawResp{APIOrderID: resp.Data, Channel: uint32(values.MLPay)}, nil |
|
} |
|
return nil, errors.New("unknown opt") |
|
} |
|
|
|
func (s *Sub) PackPayReq() interface{} { |
|
r := s.Base.PayReq |
|
if len(r.Phone) == 12 { |
|
r.Phone = r.Phone[2:] |
|
} |
|
userDataStr, _ := json.Marshal(UserData{UserName: r.Name, UserEmail: r.Email, UserPhone: r.Phone}) |
|
send := &PayReq{ |
|
PartnerID: PartnerId, |
|
ApplicationID: ApplicationId, |
|
PayWay: 2, |
|
PartnerOrderNo: r.OrderID, |
|
Amount: r.Amount, |
|
Currency: "INR", |
|
Name: r.Name, |
|
GameId: int(r.UID), |
|
ClientIP: r.IP, |
|
NotifyURL: values.GetPayCallback(values.MLPay), |
|
Subject: "Shop", |
|
Body: "product", |
|
CallbackUrl: values.GetPayCallback(values.MLPay), |
|
Extra: string(userDataStr), |
|
Version: "1.0", |
|
} |
|
send.Sign = s.Base.SignMD5(send) |
|
return send |
|
} |
|
|
|
func (s *Sub) PackWithdrawReq() interface{} { |
|
r := s.Base.WithdrawReq |
|
if len(r.Phone) == 12 { |
|
r.Phone = r.Phone[2:] |
|
} |
|
send := &WithdrawReq{ |
|
PartnerID: PartnerId, |
|
PartnerWithdrawNo: r.OrderID, |
|
Amount: r.Amount, |
|
Currency: "INR", |
|
GameId: fmt.Sprintf("%d", r.UID), |
|
NotifyURL: values.GetWithdrawCallback(values.MLPay), |
|
AccountName: r.Name, |
|
AccountPhone: r.Phone, |
|
AccountEmail: r.Email, |
|
Version: "1.0", |
|
} |
|
if r.PayType == int64(common.PayTypeBank) { |
|
send.ReceiptMode = 1 |
|
send.AccountNumber = r.CardNo |
|
send.AccountExtra1 = r.PayCode |
|
send.AccountExtra2 = r.PayCode[:4] |
|
} else if r.PayType == int64(common.PayTypeUPI) { |
|
send.ReceiptMode = 0 |
|
send.AccountNumber = r.PayCode |
|
} else { |
|
return nil |
|
} |
|
send.Sign = s.Base.SignMD5(send) |
|
return send |
|
} |
|
|
|
func (s *Sub) CheckSign(str string) bool { |
|
log.Debug("callback:%v", s.Base.CallbackReq) |
|
sign := "" |
|
if s.Base.Opt == base.OPTPayCB { |
|
req := s.Base.CallbackReq.(*PayCallbackReq) |
|
s.Base.CallbackResp.OrderID = req.PartnerOrderNo |
|
s.Base.CallbackResp.APIOrderID = req.OrderNo |
|
s.Base.CallbackResp.Success = req.Status == 1 |
|
sign = req.Sign |
|
} else if s.Base.Opt == base.OPTWithdrawCB { |
|
req := s.Base.CallbackReq.(*WithdrawCallbackReq) |
|
s.Base.CallbackResp.OrderID = req.PartnerWithdrawNo |
|
s.Base.CallbackResp.Success = req.Status == 1 |
|
sign = req.Sign |
|
} |
|
signStr := base.GetSignStrFormURLEncode(s.Base.C, s.Base.CallbackReq, "sign") |
|
signStr2 := base.GetSignStrURLEncode(signStr, "sign") + "&key=" + s.Base.SignKey |
|
return strings.ToUpper(util.CalculateMD5(signStr2)) == sign |
|
}
|
|
|