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

178 lines
4.6 KiB

package grepay
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.HttpType = base.HttpTypeForm
b.Channel = values.GrePay
// b.ShouldSignUpper = true
b.SignPassStr = []string{"sign"}
b.SignKey = config.GetConfig().Pay.GrePay.Key
if b.Opt == base.OPTPay {
b.Resp = new(PayResp)
b.ReqURL = payURL
} else if b.Opt == base.OPTWithdraw {
b.Resp = new(WithdrawResp)
b.ReqURL = withdrawURL
} else if b.Opt == base.OPTPayCB {
b.CallbackReq = new(PayCallbackReq)
b.CallbackResp.Msg = "success"
} else if b.Opt == base.OPTWithdrawCB {
b.CallbackReq = new(WithdrawCallbackReq)
b.CallbackResp.Msg = "success"
// } 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.BusContent == "" {
return nil, errors.New("pay fail")
}
return &pb.InnerRechargeResp{APIOrderID: resp.CustOrderNo, URL: resp.BusContent, Channel: uint32(values.GrePay)}, nil
} else if s.Base.Opt == base.OPTWithdraw {
resp := s.Base.Resp.(*WithdrawResp)
if s.Base.Status == 0 && resp.OrdStatus == "08" || (resp.Code != "000000" && (resp.OrdStatus == "" || resp.OrdStatus == "null")) {
return nil, errors.New("withdraw fail")
}
return &pb.InnerWithdrawResp{APIOrderID: resp.CasOrdNo, Channel: uint32(values.GrePay)}, nil
}
return nil, errors.New("unknown opt")
}
func (s *Sub) PackPayReq() interface{} {
r := s.Base.PayReq
tranType := "0412"
if r.Amount > config.GetConfig().Pay.GrePay.MIDAmount {
tranType = config.GetConfig().Pay.GrePay.BigChannel
}
send := &PayReq{
Version: "2.1",
OrgNo: orgNo,
CustId: mid,
CustOrderNo: r.OrderID,
TranType: tranType,
ClearType: "01",
PayAmt: r.Amount / 100,
BackUrl: values.GetPayCallback(values.GrePay),
FrontUrl: values.GetFrontCallback(),
GoodsName: "shopbuy",
OrderDesc: "payorder",
BuyIp: r.IP,
UserName: r.Name,
UserEmail: r.Email,
UserPhone: r.Phone,
UserCitizenID: fmt.Sprintf("%v", r.UID),
CountryCode: "IN",
Currency: "INR",
}
send.Sign = s.Base.SignMD5(send)
return send
}
func (s *Sub) PackWithdrawReq() interface{} {
r := s.Base.WithdrawReq
send := &WithdrawReq{
Version: "2.1",
OrgNo: orgNo,
CustId: mid,
CustOrderNo: r.OrderID,
CasType: "00",
Country: "IN",
Currency: "INR",
CasAmt: r.Amount / 100,
DeductWay: "02",
CallBackUrl: values.GetWithdrawCallback(values.GrePay),
Account: config.GetConfig().Pay.GrePay.WithdrawAccount,
// PayoutType: ,
AccountName: r.Name,
// PayeeBankCode: ,
// CardType: ,
// CnapsCode: ,
// CardNo: ,
// UpiId: ,
Phone: r.Phone,
Email: r.Email,
}
if r.PayType == int64(common.PayTypeUPI) {
send.PayoutType = "UPI"
send.UpiId = r.PayCode
} else if r.PayType == int64(common.PayTypeBank) {
send.PayoutType = "Card"
send.PayeeBankCode = r.PayCode
send.CardType = "IMPS"
send.CnapsCode = r.PayCode
send.CardNo = r.CardNo
} 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)
if s.Base.Opt == base.OPTPayCB {
req := s.Base.CallbackReq.(*PayCallbackReq)
s.Base.CallbackResp.OrderID = req.CustOrderNo
s.Base.CallbackResp.APIOrderID = req.PrdOrdNo
// if req.Status == 1 {
// return false
// }
s.Base.CallbackResp.Success = req.OrdStatus == "01"
return strings.ToUpper(util.CalculateMD5(str+"&key="+s.Base.SignKey)) == req.Sign
} else if s.Base.Opt == base.OPTWithdrawCB {
req := s.Base.CallbackReq.(*WithdrawCallbackReq)
s.Base.CallbackResp.OrderID = req.CustOrderNo
// if req.Status == 1 {
// return false
// }
s.Base.CallbackResp.Success = req.OrdStatus == "07"
return strings.ToUpper(util.CalculateMD5(str+"&key="+s.Base.SignKey)) == req.Sign
}
return false
}