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

153 lines
3.9 KiB

1 year ago
package grepay
import (
"errors"
"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
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 = "SC000000"
b.CallbackReq = new(PayCallbackReq)
} else if b.Opt == 4 {
b.SignPassStr = []string{"sign", "msg"}
b.CallbackResp.Msg = "SC000000"
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 != "000000" || resp.BusContent == "" {
return nil, errors.New("pay fail")
}
return &pb.InnerRechargeResp{APIOrderID: resp.PrdOrdNo, URL: resp.BusContent, Channel: uint32(values.Grepay)}, nil
}
resp := s.Base.Resp.(*WithdrawResp)
if s.Base.Status == 0 && (resp.Code != "000000" || resp.OrdStatus == "08" || resp.OrdStatus == "09") {
return nil, errors.New("withdraw fail")
}
return &pb.InnerWithdrawResp{APIOrderID: resp.CasOrdNo, Channel: uint32(values.Grepay)}, nil
}
func (s *Sub) PackPayReq() interface{} {
r := s.Base.PayReq
send := &PayReq{
Version: "2.1",
OrgNo: oid,
CustId: mid,
CustOrderNo: r.OrderID,
TranType: "0512",
ClearType: "01",
PayAmt: int(r.Amount) / 1e6,
BackUrl: values.GetPayCallback(values.Grepay),
FrontUrl: values.GetFrontCallback(),
GoodsName: "shopbuy",
OrderDesc: "pix",
BuyIp: r.IP,
UserName: r.Name,
UserEmail: r.Email,
UserPhone: r.Phone,
CountryCode: "BR",
Currency: "BRL",
}
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{
Version: "2.1",
OrgNo: oid,
CustId: mid,
CustOrdNo: r.OrderID,
CasType: "00",
Country: "BR",
Currency: "BRL",
CasAmt: r.Amount / 1e6,
DeductWay: "02",
CallBackUrl: values.GetWithdrawCallback(values.Grepay),
Account: "2406120000279502155",
PayoutType: "PIX",
AccountName: r.Name,
PanNum: r.Number,
WalletId: r.Number,
Phone: r.Phone,
Email: r.Email,
CasDesc: "pix",
}
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.CustOrderNo
s.Base.CallbackResp.Success = req.OrdStatus == "01"
} else if s.Base.Opt == 4 {
req := s.Base.CallbackReq.(*WithdrawCallbackReq)
log.Debug("checkSign withdraw:%+v", *req)
if req.OrdStatus == "00" || req.OrdStatus == "01" || req.OrdStatus == "06" {
return false
}
checkSign = req.Sign
s.Base.CallbackResp.OrderID = req.CustOrderNo
s.Base.CallbackResp.Success = req.OrdStatus == "07"
s.Base.CallbackResp.APIOrderID = req.PrdOrdNo
s.Base.CallbackResp.FailMessage = req.CasDesc
}
return strings.ToUpper(util.CalculateMD5(str)) == checkSign
}