印度包网
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.

171 lines
4.5 KiB

1 year ago
package agropay
import (
"errors"
"fmt"
"net/http"
"server/common"
"server/modules/pay/base"
"server/modules/pay/values"
"server/pb"
"server/util"
"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
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(PayCallbackReq)
} else if b.Opt == base.OPTWithdrawCB {
b.SignPassStr = []string{"sign", "msg"}
b.CallbackResp.Msg = "SUCCESS"
b.CallbackReq = new(WithdrawCallbackReq)
} else if b.Opt == base.OPTQueryWithdraw {
b.Resp = new(QueryWithdrawResp)
b.ReqURL = baseURL + queryWithdrawURL
} else if b.Opt == base.OPTQueryPay {
b.Resp = new(QueryPayResp)
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()
}
return nil
}
func (s *Sub) GetResp() (proto.Message, error) {
if s.Base.Opt == 1 {
resp := s.Base.Resp.(*PayResp)
if resp.Code != 2000 || resp.Data.PayData == "" {
return nil, errors.New("pay fail")
}
return &pb.InnerRechargeResp{APIOrderID: resp.Data.PayOrderId, URL: resp.Data.PayData, Channel: uint32(values.AgroPay)}, nil
} else if s.Base.Opt == base.OPTWithdraw {
resp := s.Base.Resp.(*WithdrawResp)
if s.Base.Status == 0 && resp.Code != 2000 {
return nil, errors.New("withdraw fail")
}
return &pb.InnerWithdrawResp{APIOrderID: fmt.Sprintf("%v", resp.Data.TransferId), Channel: uint32(values.AgroPay)}, nil
}
return nil, fmt.Errorf("unknow opt")
}
func (s *Sub) PackPayReq() interface{} {
r := s.Base.PayReq
send := &PayReq{
MchNo: mid,
AppId: appID,
MchOrderNo: r.OrderID,
// Amount: fmt.Sprintf("%d", r.Amount),
Amount: float64(r.Amount),
Currency: "INR",
UserName: r.Name,
Phone: r.Phone,
Email: r.Email,
Subject: "shop",
Body: "shop buy",
NotifyUrl: values.GetPayCallback(values.AgroPay),
ReturnUrl: values.GetFrontCallback(),
RiskControlExtParam: fmt.Sprintf(`{"userId":%d}`, r.UID),
EncryptPhone: util.CalculateMD5(r.Phone),
EncryptEmail: util.CalculateMD5(r.Email),
}
send.Sign = s.Base.SignMD5(send)
return send
}
func (s *Sub) PackWithdrawReq() interface{} {
r := s.Base.WithdrawReq
send := &WithdrawReq{
MchNo: mid,
AppId: appID,
MchOrderNo: r.OrderID,
Amount: fmt.Sprintf("%d", r.Amount),
Currency: "INR",
// EntryType: ,
// AccountNo: ,
// IfscCode: ,
AccountName: r.Name,
Phone: r.Phone,
Email: r.Email,
Address: r.Address,
// BankName: ,
ClientIp: r.IP,
TransferDesc: "shop withdraw",
NotifyUrl: values.GetWithdrawCallback(values.AgroPay),
// ChannelExtra
// ExtParam: ,
EncryptPhone: util.CalculateMD5(r.Phone),
EncryptEmail: util.CalculateMD5(r.Email),
DeviceId: r.DeviceID,
}
if r.PayType == common.WithdrawTypeBank {
send.EntryType = "banks"
send.AccountNo = r.CardNo
send.IfscCode = r.PayCode
send.BankName = r.BankName
} else if r.PayType == common.WithdrawTypeUPI {
send.EntryType = "upi"
send.AccountNo = r.PayCode
} else {
return nil
}
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 == base.OPTPayCB {
req := s.Base.CallbackReq.(*PayCallbackReq)
log.Debug("checkSign pay:%+v", *req)
checkSign = req.Sign
s.Base.CallbackResp.OrderID = req.MchOrderNo
s.Base.CallbackResp.Success = req.State == 2
mySign = s.Base.SignMD5(req)
} else if s.Base.Opt == base.OPTWithdrawCB {
req := s.Base.CallbackReq.(*WithdrawCallbackReq)
log.Debug("checkSign withdraw:%+v", *req)
checkSign = req.Sign
s.Base.CallbackResp.OrderID = req.MchOrderNo
s.Base.CallbackResp.Success = req.State == 2
s.Base.CallbackResp.APIOrderID = req.TransferId
s.Base.CallbackResp.FailMessage = req.ErrMsg
mySign = s.Base.SignMD5(req)
}
return mySign == checkSign
}