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

140 lines
3.7 KiB

package payplus
import (
"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.SignKey = key
b.HttpType = base.HttpTypeForm
b.ShouldSignUpper = true
b.Channel = values.PayPlus
if b.Opt == 1 {
b.Resp = new(PayResp)
b.ReqURL = payApi
} else if b.Opt == 2 {
b.Resp = new(WithdrawResp)
b.ReqURL = withdrawApi
} else if b.Opt == 3 {
b.SignPassStr = []string{"sign"}
b.CallbackResp.Msg = strings.ToLower(statusSuccess)
b.CallbackReq = new(PayCallbackReq)
} else if b.Opt == 4 {
b.SignPassStr = []string{"sign", "msg"}
b.CallbackResp.Msg = strings.ToLower(statusSuccess)
b.CallbackReq = new(WithdrawCallbackReq)
}
b.Sub = sub
}
type Sub struct {
Base *base.Base
}
func (s *Sub) PackHeader(_ 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.RetCode != statusSuccess || resp.PayUrl == "" {
return nil, errors.New("pay fail")
}
1 year ago
return &pb.InnerRechargeResp{APIOrderID: fmt.Sprintf("%v", resp.PlatOrder), URL: resp.PayUrl, Channel: uint32(values.PayPlus)}, nil
}
resp := s.Base.Resp.(*WithdrawResp)
if s.Base.Status == 0 && resp.RetCode != statusSuccess {
return nil, errors.New("withdraw fail")
}
return &pb.InnerWithdrawResp{APIOrderID: fmt.Sprintf("%v", resp.PlatOrder), Channel: uint32(values.PayPlus)}, nil
}
func (s *Sub) PackPayReq() interface{} {
r := s.Base.PayReq
send := &PayReq{
MchId: mchId,
OrderNo: r.OrderID,
Amount: util.Decimal(float64(r.Amount)/common.DecimalDigits, 2),
Product: indiaUpi,
BankCode: "all",
Goods: fmt.Sprintf("email:%s/name:%s/phone:%s/ip:127.0.0.1", r.Email, r.Name, r.Phone),
NotifyUrl: values.GetPayCallback(values.PayPlus),
ReturnUrl: values.GetPayCallback(values.PayPlus),
}
send.Sign = s.Base.SignMD5(send)
return send
}
func (s *Sub) PackWithdrawReq() interface{} {
r := s.Base.WithdrawReq
if r.PayType != int64(common.PayTypeBank) {
return nil
}
payType := ""
if common.PayType(r.PayType) == common.PayTypeBank {
payType = "bank"
} else if common.PayType(r.PayType) == common.PayTypeUPI {
payType = "upi"
}
send := &WithdrawReq{
Type: "api",
MchId: mchId,
MchTransNo: r.OrderID,
Amount: util.Decimal(float64(r.Amount)/common.DecimalDigits, 2),
NotifyUrl: values.GetWithdrawCallback(values.PayPlus),
AccountName: r.Name,
AccountNo: r.CardNo,
BankCode: r.PayCode,
RemarkInfo: fmt.Sprintf("email:%s/phone:%s/mode:%s", r.Email, r.Phone, payType),
}
send.Sign = s.Base.SignMD5(send)
return send
}
func (s *Sub) CheckSign(_ string) bool {
checkSign := ""
s.Base.CallbackResp.Msg = statusFail
mySign := ""
if s.Base.Opt == 3 {
req := s.Base.CallbackReq.(*PayCallbackReq)
log.Debug("checkSign pay:%+v", *req)
checkSign = req.Sign
s.Base.CallbackResp.OrderID = req.OrderNo
s.Base.CallbackResp.Success = req.Status == 2
mySign = s.Base.SignMD5(req)
} else if s.Base.Opt == 4 {
req := s.Base.CallbackReq.(*WithdrawCallbackReq)
log.Debug("checkSign withdraw:%+v", *req)
checkSign = req.Sign
s.Base.CallbackResp.OrderID = req.MchTransNo
s.Base.CallbackResp.Success = req.Status == 2
s.Base.CallbackResp.APIOrderID = req.MchTransNo
s.Base.CallbackResp.FailMessage = req.Msg
mySign = s.Base.SignMD5(req, s.Base.SignPassStr...)
}
return mySign == checkSign
}