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

148 lines
3.9 KiB

1 year ago
package igeekpay
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"
)
func NewSub(b *base.Base) {
sub := &Sub{
Base: b,
}
b.SignKey = config.GetConfig().Pay.IGeek.Key
b.HttpType = base.HttpTypeJson
b.ShouldSignUpper = true
if b.Opt == 1 {
b.Resp = new(PayResp)
b.ReqURL = config.GetConfig().Pay.IGeek.APIURL + payURL
} else if b.Opt == 2 {
b.Resp = new(WithdrawResp)
b.ReqURL = config.GetConfig().Pay.IGeek.APIURL + withdrawURL
} else if b.Opt == 3 {
b.CallbackResp.Msg = "success"
b.CallbackReq = new(PayCallbackReq)
} else if b.Opt == 4 {
b.CallbackResp.Msg = "success"
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.Status != 200 {
return nil, errors.New("pay fail")
}
return &pb.InnerRechargeResp{APIOrderID: resp.Data.TradeNo, URL: resp.Data.CashierUrl, Channel: uint32(values.IGeekPay)}, nil
}
resp := s.Base.Resp.(*WithdrawResp)
if s.Base.Status == 0 && resp.Status != 200 {
return nil, errors.New("withdraw fail")
}
return &pb.InnerWithdrawResp{APIOrderID: resp.Data.TradeNo, Channel: uint32(values.IGeekPay)}, nil
}
func (s *Sub) PackPayReq() interface{} {
r := s.Base.PayReq
send := &PayReq{
Version: "V1",
BizType: "LOCAL",
AppId: config.GetConfig().Pay.IGeek.APPID,
OrderNo: r.OrderID,
ProductCode: "CASHIER",
Currency: "BRL",
Amount: util.Decimal(float64(r.Amount)/common.DecimalDigits, 2),
PaymentDetail: PaymentDetail{
PayMode: "PIX",
},
ProductName: fmt.Sprintf("goods%v", r.Amount),
UserDetail: UserDetail{
ID: fmt.Sprintf("%v", r.UID),
Name: r.Name,
Mobile: r.Phone,
Email: r.Email,
CpfNumber: util.CheckCPF(r.Number),
},
FrontCallUrl: values.GetFrontCallback(),
BackCallUrl: values.GetPayCallback(values.IGeekPay),
}
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, ".", "")
}
send := &WithdrawReq{
Version: "V1",
BizType: "LOCAL",
AppId: config.GetConfig().Pay.IGeek.APPID,
OrderNo: r.OrderID,
ProductCode: "PAYOUT",
Currency: "BRL",
Amount: util.Decimal(float64(r.Amount)/common.DecimalDigits, 2),
PayMode: "PIX",
PayeeName: r.Name,
IDType: common.PayType(r.PayType).String(),
IDNumber: r.Number,
PayeeMobile: "55" + r.Phone,
UserId: fmt.Sprintf("%v", r.UID),
PayeeEmail: r.Email,
CpfNumber: r.Number,
PayeeAddress: r.Address,
BackCallUrl: values.GetWithdrawCallback(values.IGeekPay),
}
send.Sign = s.Base.SignMD5(send)
return send
}
func (s *Sub) CheckSign(str string) bool {
str = values.GetSignStrNull(str, "sign")
str += "&key=" + config.GetConfig().Pay.IGeek.Key
checkSign := ""
s.Base.CallbackResp.Msg = "success"
if s.Base.Opt == 3 {
req := s.Base.CallbackReq.(*PayCallbackReq)
checkSign = req.Sign
s.Base.CallbackResp.OrderID = req.OrderNo
s.Base.CallbackResp.Success = req.PayStatus == "Success"
} else if s.Base.Opt == 4 {
req := s.Base.CallbackReq.(*WithdrawCallbackReq)
checkSign = req.Sign
s.Base.CallbackResp.OrderID = req.OrderNo
s.Base.CallbackResp.Success = req.PayStatus == "Success"
s.Base.CallbackResp.APIOrderID = req.TradeNo
s.Base.CallbackResp.FailMessage = req.FailMessage
}
return strings.ToUpper(util.CalculateMD5(str)) == checkSign
}