Files
rootandClaude Opus 5 8679200f41 Initial commit
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-15 13:57:10 +08:00

191 lines
5.7 KiB
Go

package rchgutil
import (
"bytes"
"crypto/md5"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"net/http"
"strings"
"91porn-server/app/appg"
"91porn-server/common"
"91porn-server/common/httputil"
"91porn-server/common/log"
"91porn-server/web/webg"
)
var gec = config{
MercID: "10393",
AppSecret: "488b521efcfb8780f1e7731b8c1fc3ec",
ExeUrl: "https://zrpu.xtdfin.com/api/exchg/order/apply",
NotifyUrl: "/3rd/defray/callback/echg/goldfish",
QueryUrl: "https://inter.jxjssm.com/api/rechg/order/queryOrder",
}
// GoldFishExchange 充值订单请求体
type GoldFishExchange struct {
MercID string `json:"mercId"` //商户编号
Type string `json:"type"` // 订单充值类型
Money string `json:"money"` // 充值⾦额
TradeNo string `json:"tradeNo"` //商户订单号
NotifyURL string `json:"notifyUrl"` // 回调地址
ReceCardNo string `json:"receCardNo"` //兑换人银行卡号
ReceAlipayAct string `json:"receAlipayAct"` //兑换人支付宝账号
ReceiptName string `json:"receiptName"` //兑款人姓名
Info GoldFishExchangeInfo `json:"info"` // 用户信息
Time string `json:"time"` //UTC时间戳(13位)
Sign string `json:"sign"` // 签名
}
// GoldFishExchangeInfo 玩家信息
type GoldFishExchangeInfo 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"` // 玩家⼿机号
AlipayAct string `json:"alipayAct"` // 玩家支付宝账号
}
// GoldFishExchangeRes 回调函数参数model
type GoldFishExchangeRes struct {
Code int `json:"code" binding:"required"` //交易状态值
MercID string `json:"mercId" binding:"required"` //商户编号
OID string `json:"oid" binding:"required"` //支付平台订单号
Money string `json:"money" binding:"required"` //兑换金额
TradeNo string `json:"tradeNo" binding:"required"` //商户订单号
Status string `json:"status"`
Mark string `json:"mark"` //拒绝兑换理由
Sign string `json:"sign" binding:"required"` //签名
}
// ToExchg 支付发起
func (g *GoldFishExchange) ToExchg() (msg ExchgMsg) {
g.Fill()
code, err := httputil.DefaultClientPostWithRespWithProxy(webg.ProxyCfg, &msg, g.GetURL(), g.GetHeader(), g.GetBody())
if err != nil {
msg.Code = UNKNOWError
msg.Err = fmt.Sprintf("GoldFishExchange ToPay Http.PostJson fail error:%+v/,data:%+v;", err, g)
log.Error(msg.Err)
return
}
if code != http.StatusOK {
msg.Code = UNKNOWError
msg.Err = fmt.Sprintf("GoldFishExchange ToPay Http.PostJsonhttp code err:%+v/,data:%+v;", err, code)
log.Error(msg.Err)
return
}
if msg.Code != 200 {
log.Error(fmt.Sprintf("GoldFishExchange http request err :%+v:", msg))
return
}
return
}
// QueryOrder 订单状态查询发起
func (g *GoldFishExchangeRes) QueryOrder() (msg QueryMSG, err error) {
var res struct {
Code int `json:"code"`
MSG QueryMSG `json:"msg"`
}
body := make(map[string]string)
body["mercId"] = g.MercID
body["tradeNo"] = g.TradeNo
code, err := httputil.DefaultClientPostWithRespWithProxy(webg.ProxyCfg, &res, gec.QueryUrl, nil, body)
if err != nil || code != 200 {
log.Error(fmt.Sprintf("GoldFishExchange QueryOrder http.Get fail error:%+v:", err))
return
}
if res.Code != 200 {
err = errors.New("GoldFishExchange QueryOrder http query fail")
return
}
msg = res.MSG
return
}
// Fill 补全配置信息和签名信息
func (g *GoldFishExchange) Fill() {
g.MercID = g.GetMercID()
g.NotifyURL = g.GetCallBackURL()
g.Sign = g.sign()
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 strings.Contains(g.Info.DeviceType, "ios") {
g.Info.DeviceType = "ios"
} else {
g.Info.DeviceType = "android"
}
}
// sign 签名
func (g *GoldFishExchange) sign() (sign string) {
var buf bytes.Buffer
buf.WriteString(g.GetMercID())
buf.WriteString(g.Money)
buf.WriteString(g.GetCallBackURL())
switch g.Type {
case "alipay":
buf.WriteString(g.ReceAlipayAct)
case "bankcard":
buf.WriteString(g.ReceCardNo)
}
buf.WriteString(g.ReceiptName)
buf.WriteString(g.TradeNo)
buf.WriteString(g.GetAppSecret())
md5Ctx := md5.New()
md5Ctx.Write(buf.Bytes())
cipherStr := md5Ctx.Sum(nil)
return hex.EncodeToString(cipherStr)
}
// GetBody 获取请求body
func (e *GoldFishExchange) GetBody() interface{} {
jsonStr, err := json.Marshal(e)
if err != nil {
log.Info(fmt.Sprintf("GoldFishExchange GetBody json.Marshal is fail error:%+v/data:%+v", err, e))
}
log.Info(fmt.Sprintf("=====================:%+v", string(jsonStr)))
return jsonStr
}
// GetHeader 获取请求头信息
func (g *GoldFishExchange) GetHeader() map[string]string {
header := make(map[string]string, 0)
return header
}
// GetURL 获取请求地址
func (*GoldFishExchange) GetURL() string {
return gec.ExeUrl
}
// GetCallBackURL 获取回掉函数
func (*GoldFishExchange) GetCallBackURL() string {
if appg.Conf != nil {
return appg.Conf.URL.LocalWebUrl + gec.NotifyUrl
}
return webg.Conf.URL.LocalAppUrl + gec.NotifyUrl
}
// GetMercID 获取商户编号
func (*GoldFishExchange) GetMercID() string {
return gec.MercID
}
// GetAppSecret 获取app签名密钥
func (*GoldFishExchange) GetAppSecret() string {
return gec.AppSecret
}