207 lines
5.8 KiB
Go
207 lines
5.8 KiB
Go
package rchgutil
|
|
|
|
import (
|
|
"bytes"
|
|
"crypto/md5"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
|
|
"91porn-server/app/appg"
|
|
"91porn-server/common"
|
|
"91porn-server/common/httputil"
|
|
"91porn-server/common/log"
|
|
"91porn-server/web/webg"
|
|
)
|
|
|
|
var gpc = config{
|
|
MercID: "10147",
|
|
AppSecret: "62b1e8b914de0647133a0648b17ed480",
|
|
ExeUrl: "https://sord.jmsnmall.com/api/djy/topay",
|
|
QueryUrl: "https://sord.jmsnmall.com/api/djy/order/queryOrder",
|
|
NotifyUrl: "/3rd/defray/callback/goldfishPlus",
|
|
}
|
|
|
|
// GoldfishPlus 充值订单请求体
|
|
type GoldfishPlus struct {
|
|
MercID string `json:"mercId"` //商户编号
|
|
Type string `json:"type"` // 订单充值类型
|
|
Money string `json:"money"` // 充值⾦额
|
|
TradeNo string `json:"tradeNo"` //商户订单号
|
|
NotifyURL string `json:"notifyUrl"` // 回调地址
|
|
Info GoldfishPlusInfo `json:"info"` // 用户信息
|
|
Time string `json:"time"` //UTC时间戳(13位)
|
|
Sign string `json:"sign"` // 签名
|
|
PayMethod string
|
|
}
|
|
|
|
// GoldfishPlusInfo 玩家信息
|
|
type GoldfishPlusInfo struct {
|
|
PlayerID string `json:"playerId"` // 玩家ID
|
|
PlayerIP string `json:"playerIp"` // 玩家IP
|
|
DeviceID string `json:"deviceId"` // 玩家设备ID
|
|
DeviceType string `json:"deviceType"` // 玩家设备类型 ios pc android
|
|
Name string `json:"name"` // 玩家姓名
|
|
Tel string `json:"tel"` // 玩家⼿机号
|
|
PayAct string `json:"alipayAct"` // 玩家支付宝账号
|
|
}
|
|
|
|
// GoldfishPlusRes 回调函数参数model
|
|
type GoldfishPlusRes struct {
|
|
Code int `json:"code" binding:"required"`
|
|
MercID string `json:"mercId" binding:"required"` //商户编号
|
|
OID string `json:"oid" binding:"required"` //支付平台订单号
|
|
PayMoney string `json:"payMoney" binding:"required"` //订单到账实际金额
|
|
TradeNo string `json:"tradeNo" binding:"required"` //商户订单号
|
|
Sign string `json:"sign" binding:"required"` //签名
|
|
}
|
|
|
|
// ToPay 支付发起
|
|
func (g *GoldfishPlus) ToPay() (payUrl string, oid string, err error) {
|
|
var errlog string
|
|
g.Fill()
|
|
msgModel := MsgModel{}
|
|
code, err := httputil.DefaultClientPostWithRespWithProxy(appg.ProxyCfg, &msgModel, g.GetURL(), g.GetHeader(), g.GetBody())
|
|
if err != nil {
|
|
errlog = fmt.Sprintf("GoldfishPlus ToPay Http.PostJson fail error:%+v/,data:%+v;", err, g)
|
|
log.Error(errlog)
|
|
err = errors.New(errlog)
|
|
return
|
|
}
|
|
if code != http.StatusOK {
|
|
errlog = fmt.Sprintf("GoldfishPlus ToPay Http.PostJson fail error:%+v/,data:%+v;", err, code)
|
|
err = errors.New(errlog)
|
|
log.Error(errlog)
|
|
return
|
|
}
|
|
if msgModel.Code != 200 {
|
|
errlog = fmt.Sprintf("GoldfishPlus topay response err :%+v:", msgModel)
|
|
err = errors.New(errlog)
|
|
log.Error(errlog)
|
|
return
|
|
}
|
|
msg := msgModel.Msg
|
|
var buf bytes.Buffer
|
|
buf.WriteString(msg["oid"])
|
|
buf.WriteString(msg["payUrl"])
|
|
buf.WriteString(g.GetAppSecret())
|
|
if !VerifySign(msg["sign"], buf.String()) {
|
|
errlog = fmt.Sprintf("http GoldfishPlus VerifySign fail err :%+v/data:%+v", err, msgModel)
|
|
err = errors.New(errlog)
|
|
log.Error(errlog)
|
|
return
|
|
}
|
|
payUrl = msg["payUrl"]
|
|
oid = msg["oid"]
|
|
return
|
|
}
|
|
|
|
// QueryOrder 订单状态查询发起
|
|
func (g *GoldfishPlus) QueryOrder() (msg QueryMSG, err error) {
|
|
var res struct {
|
|
Code int `json:"code"`
|
|
MSG QueryMSG `json:"msg"`
|
|
}
|
|
body := make(map[string]string)
|
|
body["mercId"] = g.GetMercID()
|
|
body["tradeNo"] = g.TradeNo
|
|
code, err := httputil.DefaultClientPostWithRespWithProxy(webg.ProxyCfg, &res, g.GetQueryURL(), nil, body)
|
|
if err != nil || code != 200 {
|
|
log.Error(fmt.Sprintf("GoldfishPlus QueryOrder http.Get fail error:%+v:", err))
|
|
return
|
|
}
|
|
if res.Code != 200 {
|
|
err = errors.New("GoldfishPlus QueryOrder http query fail")
|
|
return
|
|
}
|
|
msg = res.MSG
|
|
return
|
|
}
|
|
|
|
// Fill 补全配置信息和签名信息
|
|
func (g *GoldfishPlus) Fill() {
|
|
g.Type = g.PayMethod
|
|
g.MercID = g.GetMercID()
|
|
g.NotifyURL = g.GetCallBackURL()
|
|
if g.Info.Name == "" {
|
|
g.Info.Name = GetFullName()
|
|
}
|
|
if g.Info.PlayerIP == "" {
|
|
g.Info.PlayerIP = genIpaddr()
|
|
}
|
|
if g.Info.DeviceID == "" {
|
|
g.Info.DeviceID = common.UUID()
|
|
}
|
|
if g.Info.DeviceType == "" {
|
|
g.Info.DeviceType = "ios"
|
|
}
|
|
g.Sign = g.sign()
|
|
}
|
|
|
|
// sign 签名
|
|
func (g *GoldfishPlus) sign() (sign string) {
|
|
var buf bytes.Buffer
|
|
buf.WriteString(g.GetMercID())
|
|
buf.WriteString(g.Money)
|
|
buf.WriteString(g.GetCallBackURL())
|
|
buf.WriteString(g.TradeNo)
|
|
buf.WriteString(g.Type)
|
|
buf.WriteString(g.GetAppSecret())
|
|
md5Ctx := md5.New()
|
|
md5Ctx.Write(buf.Bytes())
|
|
cipherStr := md5Ctx.Sum(nil)
|
|
return hex.EncodeToString(cipherStr)
|
|
}
|
|
|
|
// GetBody 获取请求body
|
|
func (g *GoldfishPlus) GetBody() interface{} {
|
|
jsonStr, err := json.Marshal(g)
|
|
if err != nil {
|
|
log.Info(fmt.Sprintf("GoldfishPlus GetBody json.Marshal is fail error:%+v/data:%+v", err, g))
|
|
}
|
|
sstr := string(jsonStr)
|
|
log.Info(fmt.Sprintf("==================%+v", sstr))
|
|
return jsonStr
|
|
}
|
|
|
|
// GetHeader 获取请求头信息
|
|
func (g *GoldfishPlus) GetHeader() map[string]string {
|
|
header := make(map[string]string, 0)
|
|
return header
|
|
}
|
|
|
|
// GetURL 获取请求地址
|
|
func (*GoldfishPlus) GetURL() string {
|
|
return gpc.ExeUrl
|
|
}
|
|
|
|
// GetQueryURL 获取请求地址
|
|
func (*GoldfishPlus) GetQueryURL() string {
|
|
return gpc.QueryUrl
|
|
}
|
|
|
|
// GetLowAndUpperURL 获取请求地址
|
|
func (*GoldfishPlus) GetLowAndUpperURL() string {
|
|
return gpc.LowAndUpperURL
|
|
}
|
|
|
|
// GetCallBackURL 获取回掉函数
|
|
func (*GoldfishPlus) GetCallBackURL() string {
|
|
if appg.Conf != nil {
|
|
return appg.Conf.URL.LocalWebUrl + gpc.NotifyUrl
|
|
}
|
|
return webg.Conf.URL.LocalAppUrl + gpc.NotifyUrl
|
|
}
|
|
|
|
// GetMercID 获取商户编号
|
|
func (*GoldfishPlus) GetMercID() string {
|
|
return gpc.MercID
|
|
}
|
|
|
|
// GetAppSecret 获取app签名密钥
|
|
func (*GoldfishPlus) GetAppSecret() string {
|
|
return gpc.AppSecret
|
|
}
|