Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
25c00d5072 | ||
|
|
ee32c8b83d | ||
|
|
12a193fdee | ||
|
|
ca55880beb | ||
|
|
ee9e399a81 | ||
|
|
ed9b0dd248 | ||
|
|
02edbdae8d | ||
|
|
5e75094c16 |
@@ -1,7 +1,7 @@
|
|||||||
package config
|
package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/ayflying/utility_go"
|
"github.com/ayflying/utility_go/pkg"
|
||||||
"github.com/gogf/gf/v2/util/gutil"
|
"github.com/gogf/gf/v2/util/gutil"
|
||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
@@ -23,7 +23,7 @@ func (c *{mod}Mod) Load(_cfg ...string) {
|
|||||||
c.lock.Lock()
|
c.lock.Lock()
|
||||||
defer c.lock.Unlock()
|
defer c.lock.Unlock()
|
||||||
c.cfgArr = make([]*{cfg}Cfg, 0)
|
c.cfgArr = make([]*{cfg}Cfg, 0)
|
||||||
data, err := utility_go.Config.GetFile("{file}")
|
data, err := pkg.Config().GetFile("{file}")
|
||||||
err = data.Scan(&c.cfgArr)
|
err = data.Scan(&c.cfgArr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
|
|||||||
@@ -124,9 +124,9 @@ var (
|
|||||||
FileUrl: url[v.S3],
|
FileUrl: url[v.S3],
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
Proxy := g.Cfg().MustGet(ctx, "update_proxy", "http://192.168.50.170:10808").String()
|
||||||
g.Log().Debugf(ctx, "切换代理进行上传:err=%v", err)
|
g.Log().Debugf(ctx, "切换代理进行上传:err=%v", err)
|
||||||
get, err = client.Proxy("http://192.168.50.114:10808").
|
get, err = client.Proxy(Proxy).Post(ctx, address+"/callback/update", &UpdateReq{
|
||||||
Post(ctx, address+"/callback/update", &UpdateReq{
|
|
||||||
FileUrl: url[v.S3],
|
FileUrl: url[v.S3],
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -134,7 +134,8 @@ func (s *sGameAct) Save(ctx context.Context, actId int) (err error) {
|
|||||||
|
|
||||||
//循环获取缓存数据
|
//循环获取缓存数据
|
||||||
err = tools.Redis.RedisScanV2(cacheKey, func(keys []string) (err error) {
|
err = tools.Redis.RedisScanV2(cacheKey, func(keys []string) (err error) {
|
||||||
var add []interface{}
|
var add = make([]*entity.GameAct, 0)
|
||||||
|
var update = make([]*entity.GameAct, 0)
|
||||||
var delKey []string
|
var delKey []string
|
||||||
for _, cacheKey = range keys {
|
for _, cacheKey = range keys {
|
||||||
result := strings.Split(cacheKey, ":")
|
result := strings.Split(cacheKey, ":")
|
||||||
@@ -176,25 +177,51 @@ func (s *sGameAct) Save(ctx context.Context, actId int) (err error) {
|
|||||||
}
|
}
|
||||||
actionData := cacheGet.String()
|
actionData := cacheGet.String()
|
||||||
if data == nil {
|
if data == nil {
|
||||||
//data =
|
add = append(add, &entity.GameAct{
|
||||||
add = append(add, &do.GameAct{
|
|
||||||
ActId: actId,
|
ActId: actId,
|
||||||
Uid: uid,
|
Uid: uid,
|
||||||
Action: actionData,
|
Action: actionData,
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
//覆盖数据
|
//覆盖数据
|
||||||
|
data.ActId = actId
|
||||||
|
data.Uid = uid
|
||||||
data.Action = actionData
|
data.Action = actionData
|
||||||
add = append(add, data)
|
update = append(update, data)
|
||||||
}
|
}
|
||||||
//最后删除key
|
//最后删除key
|
||||||
delKey = append(delKey, cacheKey)
|
delKey = append(delKey, cacheKey)
|
||||||
}
|
}
|
||||||
|
|
||||||
//批量写入数据库
|
//批量写入数据库
|
||||||
if len(add) > 0 {
|
if len(delKey) > 0 {
|
||||||
dbRes, err2 := g.Model(Name).Batch(30).Data(add).Save()
|
for _, v := range update {
|
||||||
add = make([]interface{}, 0)
|
v.UpdatedAt = gtime.Now()
|
||||||
|
_, err2 := g.Model(Name).Where(do.GameAct{
|
||||||
|
Uid: v.Uid,
|
||||||
|
ActId: v.ActId,
|
||||||
|
UpdatedAt: v.UpdatedAt,
|
||||||
|
}).Data(v).Update()
|
||||||
|
if err2 != nil {
|
||||||
|
g.Log().Error(ctx, err2)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
////获取多少个数据,删除不是当前修改的数据
|
||||||
|
//count, _ := g.Model(Name).Where(do.GameAct{
|
||||||
|
// Uid: v.Uid,
|
||||||
|
// ActId: v.ActId,
|
||||||
|
//}).Count()
|
||||||
|
//if count > 1 {
|
||||||
|
// g.Model(Name).Where(do.GameAct{
|
||||||
|
// Uid: v.Uid,
|
||||||
|
// ActId: v.ActId,
|
||||||
|
// }).WhereNot("updated_at", v.UpdatedAt).Delete()
|
||||||
|
//}
|
||||||
|
}
|
||||||
|
//dbRes, err2 := g.Model(Name).Batch(50).Data(add).Update()
|
||||||
|
update = make([]*entity.GameAct, 0)
|
||||||
|
dbRes, err2 := g.Model(Name).Batch(50).Data(add).Save()
|
||||||
|
add = make([]*entity.GameAct, 0)
|
||||||
if err2 != nil {
|
if err2 != nil {
|
||||||
g.Log().Error(ctx, err2)
|
g.Log().Error(ctx, err2)
|
||||||
return
|
return
|
||||||
|
|||||||
13
package/pay/oppo/model.go
Normal file
13
package/pay/oppo/model.go
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
package oppo
|
||||||
|
|
||||||
|
// OPPO支付回调参数结构体
|
||||||
|
type PayCallback struct {
|
||||||
|
NotifyId string `json:"notifyId" dc:"回调通知单号,以GC开头,必填,示例:GC20230314657000"`
|
||||||
|
PartnerOrder string `json:"partnerOrder" dc:"开发者订单号,必填,示例:123456"`
|
||||||
|
ProductName string `json:"productName" dc:"商品名称,必填,示例:10元宝"`
|
||||||
|
ProductDesc string `json:"productDesc" dc:"商品描述,必填,示例:10元宝等于1元"`
|
||||||
|
Price int64 `json:"price" dc:"商品价格,单位为分,需要游戏服务端做验证,必填,示例:100"`
|
||||||
|
Count int `json:"count" dc:"商品数量(一般为1),必填,示例:1"`
|
||||||
|
Attach string `json:"attach" dc:"请求支付时上传的附加参数,可能为空,选填"`
|
||||||
|
Sign string `json:"sign" dc:"OPPO服务端签名,需要游戏服务端做验证,必填"`
|
||||||
|
}
|
||||||
119
package/pay/oppo/oppo.go
Normal file
119
package/pay/oppo/oppo.go
Normal file
@@ -0,0 +1,119 @@
|
|||||||
|
package oppo
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"crypto"
|
||||||
|
"crypto/rsa"
|
||||||
|
"crypto/sha1"
|
||||||
|
"crypto/x509"
|
||||||
|
"encoding/base64"
|
||||||
|
"encoding/pem"
|
||||||
|
"fmt"
|
||||||
|
"github.com/gogf/gf/v2/errors/gerror"
|
||||||
|
"github.com/gogf/gf/v2/frame/g"
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// 跟充值平台通信的加密key
|
||||||
|
//const PUBLIC_KEY = `dfsdfs`
|
||||||
|
|
||||||
|
type OppoType struct {
|
||||||
|
PublicKey string `json:"public_key"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func New(PublicKey string) *OppoType {
|
||||||
|
|
||||||
|
return &OppoType{
|
||||||
|
PublicKey: PublicKey,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *OppoType) Verify(ctx context.Context, data map[string]string) error {
|
||||||
|
// 解析请求参数
|
||||||
|
for k, v := range data {
|
||||||
|
if v == "" || v == "0" {
|
||||||
|
delete(data, k)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//data["notifyId"] = getParam(r, "notifyId")
|
||||||
|
//data["partnerOrder"] = getParam(r, "partnerOrder")
|
||||||
|
//data["productName"] = getParam(r, "productName")
|
||||||
|
//data["productDesc"] = getParam(r, "productDesc")
|
||||||
|
//data["price"] = getParam(r, "price")
|
||||||
|
//data["count"] = getParam(r, "count")
|
||||||
|
//data["attach"] = getParam(r, "attach")
|
||||||
|
//data["sign"] = getParam(r, "sign")
|
||||||
|
|
||||||
|
// 验证签名
|
||||||
|
result, err := p.rsaVerify(data)
|
||||||
|
if err != nil {
|
||||||
|
//http.Error(w, "Verification error: "+err.Error(), http.StatusInternalServerError)
|
||||||
|
g.Log().Errorf(ctx, "Verification error: %v", err.Error())
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if result {
|
||||||
|
// TODO::验证成功,处理后续逻辑
|
||||||
|
//fmt.Fprint(w, "Verification successful")
|
||||||
|
//g.Log().Errorf(ctx, "Verification error: %v", err.Error())
|
||||||
|
} else {
|
||||||
|
// TODO::验证失败,处理后续逻辑
|
||||||
|
//http.Error(w, "Verification failed", http.StatusBadRequest)
|
||||||
|
g.Log().Error(ctx, "Verification failed")
|
||||||
|
err = gerror.New("Verification failed")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *OppoType) getParam(r *http.Request, paramName string) string {
|
||||||
|
r.ParseForm()
|
||||||
|
if value := r.FormValue(paramName); value != "" {
|
||||||
|
return strings.TrimSpace(value)
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *OppoType) rsaVerify(contents map[string]string) (bool, error) {
|
||||||
|
// 构建待签名字符串
|
||||||
|
strContents := fmt.Sprintf("notifyId=%s&partnerOrder=%s&productName=%s&productDesc=%s&price=%s&count=%s&attach=%s",
|
||||||
|
contents["notifyId"], contents["partnerOrder"], contents["productName"],
|
||||||
|
contents["productDesc"], contents["price"], contents["count"], contents["attach"])
|
||||||
|
|
||||||
|
// 解析公钥
|
||||||
|
publicKey := p.PublicKey
|
||||||
|
pemData := []byte("-----BEGIN PUBLIC KEY-----\n" +
|
||||||
|
strings.ReplaceAll(publicKey, " ", "\n") +
|
||||||
|
"\n-----END PUBLIC KEY-----")
|
||||||
|
|
||||||
|
block, _ := pem.Decode(pemData)
|
||||||
|
if block == nil {
|
||||||
|
return false, fmt.Errorf("failed to decode PEM block")
|
||||||
|
}
|
||||||
|
|
||||||
|
pubInterface, err := x509.ParsePKIXPublicKey(block.Bytes)
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
|
||||||
|
pubKey, ok := pubInterface.(*rsa.PublicKey)
|
||||||
|
if !ok {
|
||||||
|
return false, fmt.Errorf("public key is not an RSA public key")
|
||||||
|
}
|
||||||
|
|
||||||
|
// 解码签名
|
||||||
|
signature, err := base64.StdEncoding.DecodeString(contents["sign"])
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// 计算内容的哈希值
|
||||||
|
hash := sha1.New()
|
||||||
|
hash.Write([]byte(strContents))
|
||||||
|
hashed := hash.Sum(nil)
|
||||||
|
|
||||||
|
// 验证签名
|
||||||
|
err = rsa.VerifyPKCS1v15(pubKey, crypto.SHA1, hashed, signature)
|
||||||
|
return err == nil, err
|
||||||
|
}
|
||||||
@@ -1,60 +1,62 @@
|
|||||||
package taptap
|
package taptap
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"context"
|
|
||||||
"crypto/hmac"
|
"crypto/hmac"
|
||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/gogf/gf/v2/os/gtime"
|
|
||||||
"github.com/gogf/gf/v2/util/grand"
|
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"sort"
|
"sort"
|
||||||
"strconv"
|
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
type pTapTap struct {
|
type pTapTap struct {
|
||||||
Secret string `json:"secret" dc:"秘钥"`
|
Secret string `json:"secret" dc:"秘钥"`
|
||||||
OrderId string `json:"order_id" dc:"订单唯一 ID"`
|
//OrderId string `json:"order_id" dc:"订单唯一 ID"`
|
||||||
ClientId string `json:"client_id" dc:"应用的 Client ID"`
|
ClientId string `json:"client_id" dc:"应用的 Client ID"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(orderId string) *pTapTap {
|
func New(clientId string, secret string) *pTapTap {
|
||||||
return &pTapTap{
|
return &pTapTap{
|
||||||
Secret: "5AFEWnadBA0NgJK2mxeBLQEde0qyIefxLSc4XKHsx9AwkQRhxzkQ9DixsOkK6gcV",
|
Secret: secret,
|
||||||
ClientId: "mox88lbz43edfukdgk",
|
ClientId: clientId,
|
||||||
OrderId: orderId,
|
//OrderId: orderId,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *pTapTap) Sign(url string, body []byte) (sign string, ts int64, nonce string, err error) {
|
// Sign signs the request.
|
||||||
//nolint:gosec
|
func (p *pTapTap) Sign(req *http.Request, secret string) (string, error) {
|
||||||
secret := p.Secret
|
//获取请求参数
|
||||||
//body := gjson.MustEncode(g.Map{})
|
//req := g.RequestFromCtx(ctx).Request
|
||||||
//body := []byte(`{"event_type":"charge.succeeded","order":{"order_id":"1790288650833465345","purchase_token":"rT2Et9p0cfzq4fwjrTsGSacq0jQExFDqf5gTy1alp+Y=","client_id":"o6nD4iNavjQj75zPQk","open_id":"4+Axcl2RFgXbt6MZwdh++w==","user_region":"US","goods_open_id":"com.goods.open_id","goods_name":"TestGoodsName","status":"charge.succeeded","amount":"19000000000","currency":"USD","create_time":"1716168000","pay_time":"1716168000","extra":"1111111111111111111"}}`)
|
return Sign(req, secret)
|
||||||
//url := "https://example.com/my-service/v1/my-method"
|
|
||||||
ts = gtime.Now().Unix()
|
|
||||||
nonce = grand.S(5)
|
|
||||||
method := "POST"
|
|
||||||
header := http.Header{
|
|
||||||
"Content-Type": {"Content-Type: application/json; charset=utf-8"},
|
|
||||||
"X-Tap-Ts": {strconv.FormatInt(ts, 10)},
|
|
||||||
"X-Tap-Nonce": {nonce},
|
|
||||||
}
|
|
||||||
ctx := context.Background()
|
|
||||||
req, err := http.NewRequestWithContext(ctx, method, url, bytes.NewBuffer(body))
|
|
||||||
req.Header = header
|
|
||||||
sign, err = Sign(req, secret)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
req.Header.Set("X-Tap-Sign", sign)
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//func (p *pTapTap) SignOld(ctx context.Context, method, url string, token string, data any) (sign string, ts int64, nonce string, err error) {
|
||||||
|
// //secret := p.Secret
|
||||||
|
// //ts = gtime.Now().Unix()
|
||||||
|
// //nonce = grand.S(5)
|
||||||
|
// //header := http.Header{
|
||||||
|
// // "Content-Type": {"Content-Type: application/json; charset=utf-8"},
|
||||||
|
// // "X-Tap-Ts": {strconv.FormatInt(ts, 10)},
|
||||||
|
// // "X-Tap-Nonce": {nonce},
|
||||||
|
// //}
|
||||||
|
// //if method == "POST" {
|
||||||
|
// // header.Set("Content-Type", "application/json; charset=utf-8")
|
||||||
|
// //}
|
||||||
|
// ////ctx := context.Background()
|
||||||
|
// //request := g.RequestFromCtx(ctx).Request
|
||||||
|
// //body, _ := json.Marshal(data)
|
||||||
|
// ////req, err := http.NewRequestWithContext(ctx, method, url, strings.NewReader(string(body)))
|
||||||
|
// //req.Header = header
|
||||||
|
// //sign, err = Sign(req, secret)
|
||||||
|
// //if err != nil {
|
||||||
|
// // panic(err)
|
||||||
|
// //}
|
||||||
|
// //req.Header.Set("X-Tap-Sign", sign)
|
||||||
|
// //return
|
||||||
|
//}
|
||||||
|
|
||||||
// Sign signs the request.
|
// Sign signs the request.
|
||||||
func Sign(req *http.Request, secret string) (string, error) {
|
func Sign(req *http.Request, secret string) (string, error) {
|
||||||
methodPart := req.Method
|
methodPart := req.Method
|
||||||
|
|||||||
@@ -1,59 +1,129 @@
|
|||||||
package taptap
|
package taptap
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/gogf/gf/v2/encoding/gjson"
|
||||||
"github.com/gogf/gf/v2/frame/g"
|
"github.com/gogf/gf/v2/frame/g"
|
||||||
"github.com/gogf/gf/v2/net/gclient"
|
"github.com/gogf/gf/v2/net/gclient"
|
||||||
"github.com/gogf/gf/v2/os/gctx"
|
"github.com/gogf/gf/v2/os/gctx"
|
||||||
"strconv"
|
"github.com/gogf/gf/v2/os/gtime"
|
||||||
|
"github.com/gogf/gf/v2/util/grand"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
type GetPayData struct {
|
type GetPayData struct {
|
||||||
Data struct {
|
Data struct {
|
||||||
Order struct {
|
Order struct {
|
||||||
|
Order
|
||||||
} `json:"order"`
|
} `json:"order"`
|
||||||
} `json:"data"`
|
} `json:"data"`
|
||||||
Success bool `json:"success"`
|
Success bool `json:"success"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Order struct {
|
||||||
|
OrderID string `json:"order_id"` // 订单唯一 ID
|
||||||
|
PurchaseToken string `json:"purchase_token"` // 用于订单核销的 token
|
||||||
|
ClientID string `json:"client_id"` // 应用的 Client ID
|
||||||
|
OpenID string `json:"open_id"` // 用户的开放平台 ID
|
||||||
|
UserRegion string `json:"user_region"` // 用户地区
|
||||||
|
GoodsOpenID string `json:"goods_open_id"` // 商品唯一 ID
|
||||||
|
GoodsName string `json:"goods_name"` // 商品名称
|
||||||
|
Status PaymentStatus `json:"status"` // 订单状态
|
||||||
|
Amount string `json:"amount"` // 金额(本币金额 x 1,000,000)
|
||||||
|
Currency string `json:"currency"` // 币种
|
||||||
|
CreateTime string `json:"create_time"` // 创建时间
|
||||||
|
PayTime string `json:"pay_time"` // 支付时间
|
||||||
|
Extra string `json:"extra"` // 商户自定义数据,如角色信息等,长度不超过 255 UTF-8 字符
|
||||||
|
}
|
||||||
|
type PaymentStatus string
|
||||||
|
|
||||||
|
const (
|
||||||
|
ChargePending PaymentStatus = "charge.pending" // 待支付
|
||||||
|
ChargeSucceeded PaymentStatus = "charge.succeeded" //支付成功
|
||||||
|
ChargeConfirmed PaymentStatus = "charge.confirmed" //已核销
|
||||||
|
ChargeOverdue PaymentStatus = "charge.overdue" //支付超时关闭
|
||||||
|
RefundPending PaymentStatus = "refund.pending" //退款中
|
||||||
|
RefundSucceeded PaymentStatus = "refund.succeeded" //退款成功
|
||||||
|
RefundFailed PaymentStatus = "refund.failed" //退款失败
|
||||||
|
RefundRejected PaymentStatus = "refund.rejected" //退款被拒绝
|
||||||
|
)
|
||||||
|
|
||||||
// 查询订单信息
|
// 查询订单信息
|
||||||
func (p *pTapTap) Info(orderId string, clientId string, token []byte) (res string, err error) {
|
func (p *pTapTap) Info(ctx context.Context, order string) (getPayData *GetPayData, err error) {
|
||||||
url := fmt.Sprintf("https://cloud-payment.tapapis.cn/order/v1/info?client_id=%v&order_id=%v", orderId, clientId)
|
url := fmt.Sprintf("https://cloud-payment.tapapis.cn/order/v1/info?client_id=%v&order_id=%v", p.ClientId, order)
|
||||||
res, err = p.get(url, token)
|
getPayData, err = p.get(ctx, url)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// 验证并核销订单
|
// 验证并核销订单
|
||||||
func (p *pTapTap) Verify(orderId string, clientId string, token []byte) (res string, err error) {
|
func (p *pTapTap) Verify(ctx context.Context, req any) (getPayData *GetPayData, err error) {
|
||||||
url := fmt.Sprintf("https://cloud-payment.tapapis.cn/order/v1/verify?client_id=%v", clientId)
|
url := fmt.Sprintf("https://cloud-payment.tapapis.cn/order/v1/verify?client_id=%v", p.ClientId)
|
||||||
res, err = p.get(url, token)
|
getPayData, err = p.get(ctx, url, req)
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *pTapTap) get(url string, token []byte, _data ...any) (res string, err error) {
|
|
||||||
sign, ts, nonce, err := p.Sign(url, token)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *pTapTap) get(ctx context.Context, url string, _data ...any) (getPayData *GetPayData, err error) {
|
||||||
|
|
||||||
var _get *gclient.Response
|
var _get *gclient.Response
|
||||||
if len(_data) == 0 {
|
|
||||||
_get, err = g.Client().Header(map[string]string{
|
|
||||||
"X-Tap-Sign": sign,
|
|
||||||
"X-Tap-Nonce": nonce,
|
|
||||||
"X-Tap-Ts": strconv.FormatInt(ts, 10),
|
|
||||||
}).Get(gctx.New(), url)
|
|
||||||
|
|
||||||
|
var header = map[string]string{
|
||||||
|
"Content-Type": "Content-Type: application/json; charset=utf-8",
|
||||||
|
"X-Tap-Nonce": grand.S(6),
|
||||||
|
"X-Tap-Ts": gtime.Now().TimestampStr(),
|
||||||
|
}
|
||||||
|
ctx2 := context.Background()
|
||||||
|
var method = "GET"
|
||||||
|
if len(_data) > 0 {
|
||||||
|
method = "POST"
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
//temp := []byte(`{"event_type":"charge.succeeded","order":{"order_id":"1790288650833465345","purchase_token":"rT2Et9p0cfzq4fwjrTsGSacq0jQExFDqf5gTy1alp+Y=","client_id":"o6nD4iNavjQj75zPQk","open_id":"4+Axcl2RFgXbt6MZwdh++w==","user_region":"US","goods_open_id":"com.goods.open_id","goods_name":"TestGoodsName","status":"charge.succeeded","amount":"19000000000","currency":"USD","create_time":"1716168000","pay_time":"1716168000","extra":"1111111111111111111"}}`)
|
||||||
|
var body io.Reader
|
||||||
|
if len(_data) > 0 {
|
||||||
|
body = bytes.NewBuffer(gjson.MustEncode(_data[0]))
|
||||||
} else {
|
} else {
|
||||||
_get, err = g.Client().Header(map[string]string{
|
body = bytes.NewBuffer([]byte{})
|
||||||
"X-Tap-Sign": sign,
|
}
|
||||||
"X-Tap-Nonce": nonce,
|
req, _ := http.NewRequestWithContext(ctx2, method, url, body)
|
||||||
"X-Tap-Ts": strconv.FormatInt(ts, 10),
|
for k, v := range header {
|
||||||
}).Post(gctx.New(), url, _data[0])
|
req.Header.Set(k, v)
|
||||||
|
}
|
||||||
|
sign, err2 := p.Sign(req, p.Secret)
|
||||||
|
if err2 != nil {
|
||||||
|
err = err2
|
||||||
|
return
|
||||||
|
}
|
||||||
|
req.Header.Set("X-Tap-Sign", sign)
|
||||||
|
header["X-Tap-Sign"] = sign
|
||||||
|
if len(_data) == 0 {
|
||||||
|
_get, err = g.Client().Header(header).ContentJson().Get(gctx.New(), url)
|
||||||
|
} else {
|
||||||
|
_get, err = g.Client().Header(header).ContentJson().Post(gctx.New(), url, _data[0])
|
||||||
}
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
res = _get.ReadAllString()
|
getPayData = &GetPayData{}
|
||||||
|
resData := _get.ReadAll()
|
||||||
|
g.Dump(resData)
|
||||||
|
if err = json.Unmarshal(resData, &getPayData); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !getPayData.Success {
|
||||||
|
err = errors.New(string(resData))
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,40 +0,0 @@
|
|||||||
package taptap
|
|
||||||
|
|
||||||
import "github.com/gogf/gf/v2/encoding/gjson"
|
|
||||||
|
|
||||||
type WebhookData struct {
|
|
||||||
Order *Order `json:"order"`
|
|
||||||
EventType string `json:"event_type"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// Order 订单信息结构体
|
|
||||||
type Order struct {
|
|
||||||
OrderID string `json:"order_id" dc:"订单唯一ID"`
|
|
||||||
PurchaseToken string `json:"purchase_token" dc:"用于订单核销的token"`
|
|
||||||
ClientID string `json:"client_id" dc:"应用的Client ID"`
|
|
||||||
OpenID string `json:"open_id" dc:"用户的开放平台ID"`
|
|
||||||
UserRegion string `json:"user_region" dc:"用户地区"`
|
|
||||||
GoodsOpenID string `json:"goods_open_id" dc:"商品唯一ID"`
|
|
||||||
GoodsName string `json:"goods_name" dc:"商品名称"`
|
|
||||||
Status string `json:"status" dc:"订单状态"`
|
|
||||||
Amount string `json:"amount" dc:"金额(本币金额x1,000,000)"`
|
|
||||||
Currency string `json:"currency" dc:"币种"`
|
|
||||||
CreateTime string `json:"create_time" dc:"创建时间"`
|
|
||||||
PayTime string `json:"pay_time" dc:"支付时间"`
|
|
||||||
Extra string `json:"extra" dc:"商户自定义数据,如角色信息等,长度不超过255 UTF-8字符"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *pTapTap) Webhook(body []byte) (res string, err error) {
|
|
||||||
var data *WebhookData
|
|
||||||
gjson.DecodeTo(body, &data)
|
|
||||||
|
|
||||||
switch data.EventType {
|
|
||||||
case "charge.succeeded": //充值成功
|
|
||||||
//todo 处理订单信息
|
|
||||||
|
|
||||||
case "refund.succeeded": //退款成功
|
|
||||||
case "refund.failed": //退款失败
|
|
||||||
}
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
|
||||||
63
package/pay/xiaomi/helper.go
Normal file
63
package/pay/xiaomi/helper.go
Normal file
@@ -0,0 +1,63 @@
|
|||||||
|
package xiaomi
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/hmac"
|
||||||
|
"crypto/sha1"
|
||||||
|
"encoding/hex"
|
||||||
|
"fmt"
|
||||||
|
"sort"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// SignatureHelper 签名辅助类
|
||||||
|
type SignatureHelper struct{}
|
||||||
|
|
||||||
|
// hmacSHA1 计算HMAC-SHA1哈希值
|
||||||
|
func hmacSHA1(data, key string) string {
|
||||||
|
h := hmac.New(sha1.New, []byte(key))
|
||||||
|
h.Write([]byte(data))
|
||||||
|
return hex.EncodeToString(h.Sum(nil))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Sign 计算hmac-sha1签名
|
||||||
|
func (m *MiPay) Sign(params map[string]string, secretKey string) string {
|
||||||
|
if _, ok := params["signature"]; ok {
|
||||||
|
delete(params, "signature")
|
||||||
|
}
|
||||||
|
for k, v := range params {
|
||||||
|
if v == "" || v == "0" {
|
||||||
|
delete(params, k)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
sortString := m.buildSortString(params)
|
||||||
|
signature := hmacSHA1(sortString, secretKey)
|
||||||
|
return signature
|
||||||
|
}
|
||||||
|
|
||||||
|
// VerifySignature 验证签名
|
||||||
|
func (m *MiPay) VerifySignature(params map[string]string, signature, secretKey string) bool {
|
||||||
|
tmpSign := m.Sign(params, secretKey)
|
||||||
|
return tmpSign == signature
|
||||||
|
}
|
||||||
|
|
||||||
|
// buildSortString 构造排序字符串
|
||||||
|
func (m *MiPay) buildSortString(params map[string]string) string {
|
||||||
|
if len(params) == 0 {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
// 按键排序
|
||||||
|
keys := make([]string, 0, len(params))
|
||||||
|
for k := range params {
|
||||||
|
keys = append(keys, k)
|
||||||
|
}
|
||||||
|
sort.Strings(keys)
|
||||||
|
|
||||||
|
// 构建排序字符串
|
||||||
|
var fields []string
|
||||||
|
for _, k := range keys {
|
||||||
|
fields = append(fields, fmt.Sprintf("%s=%s", k, params[k]))
|
||||||
|
}
|
||||||
|
|
||||||
|
return strings.Join(fields, "&")
|
||||||
|
}
|
||||||
20
package/pay/xiaomi/model.go
Normal file
20
package/pay/xiaomi/model.go
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
package xiaomi
|
||||||
|
|
||||||
|
import "github.com/gogf/gf/v2/os/gtime"
|
||||||
|
|
||||||
|
type PayCallback struct {
|
||||||
|
AppID string `json:"appId" dc:"游戏ID" required:"true"`
|
||||||
|
CPOrderID string `json:"cpOrderId" dc:"开发商订单ID" required:"true"`
|
||||||
|
CPUserInfo string `json:"cpUserInfo" dc:"开发商透传信息" required:"false"`
|
||||||
|
OrderConsumeType int `json:"orderConsumeType" dc:"订单类型:10:普通订单 11:直充直消订单" required:"false"`
|
||||||
|
OrderID string `json:"orderId" dc:"游戏平台订单ID" required:"true"`
|
||||||
|
OrderStatus string `json:"orderStatus" dc:"订单状态,TRADE_SUCCESS代表成功" required:"true"`
|
||||||
|
PayFee int `json:"payFee" dc:"支付金额,单位为分,即0.01米币。(请务必使用payFee字段值与游戏发起订单金额做校验,确保订单金额一致性)" required:"true"`
|
||||||
|
PayTime *gtime.Time `json:"payTime" dc:"支付时间,格式yyyy-MM-dd HH:mm:ss" required:"true"`
|
||||||
|
ProductCode string `json:"productCode" dc:"商品代码" required:"true"`
|
||||||
|
ProductCount int `json:"productCount" dc:"商品数量" required:"true"`
|
||||||
|
ProductName string `json:"productName" dc:"商品名称" required:"true"`
|
||||||
|
UID string `json:"uid" dc:"用户ID" required:"true"`
|
||||||
|
PartnerGiftConsume int64 `json:"partnerGiftConsume" dc:"使用游戏券金额(如果订单使用游戏券则有,long型),如果有则参与签名" required:"false"`
|
||||||
|
Signature string `json:"signature" dc:"签名,签名方法见后面说明" required:"true"`
|
||||||
|
}
|
||||||
29
package/pay/xiaomi/xiaomi.go
Normal file
29
package/pay/xiaomi/xiaomi.go
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
package xiaomi
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/gogf/gf/v2/frame/g"
|
||||||
|
"github.com/gogf/gf/v2/os/gctx"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Config 小米支付配置信息
|
||||||
|
type Config struct {
|
||||||
|
AppID string `json:"app_id"` // 应用ID
|
||||||
|
AppSecret string `json:"app_secret"` // 应用密钥
|
||||||
|
//PrivateKey string // 商户私钥(如需证书)
|
||||||
|
//MIAPIURL string // 小米支付API基础地址
|
||||||
|
//IsSandbox bool // 是否沙箱环境
|
||||||
|
}
|
||||||
|
|
||||||
|
// Miipay 小米支付客户端
|
||||||
|
type MiPay struct {
|
||||||
|
config *Config
|
||||||
|
}
|
||||||
|
|
||||||
|
func New() *MiPay {
|
||||||
|
_cfg, _ := g.Cfg().Get(gctx.New(), "pay.xiaomi")
|
||||||
|
var cfg *Config
|
||||||
|
_cfg.Scan(&cfg)
|
||||||
|
return &MiPay{
|
||||||
|
config: cfg,
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -12,6 +12,7 @@ import (
|
|||||||
"github.com/elastic/go-elasticsearch/v8/typedapi/types"
|
"github.com/elastic/go-elasticsearch/v8/typedapi/types"
|
||||||
"github.com/gogf/gf/v2/frame/g"
|
"github.com/gogf/gf/v2/frame/g"
|
||||||
"github.com/gogf/gf/v2/os/gctx"
|
"github.com/gogf/gf/v2/os/gctx"
|
||||||
|
"github.com/gogf/gf/v2/util/gconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -23,6 +24,13 @@ type Elastic struct {
|
|||||||
name string
|
name string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type elkBulk struct {
|
||||||
|
Index struct {
|
||||||
|
Index string `json:"_index"`
|
||||||
|
Id string `json:"_id"`
|
||||||
|
} `json:"index"`
|
||||||
|
}
|
||||||
|
|
||||||
func NewV1(name string) *Elastic {
|
func NewV1(name string) *Elastic {
|
||||||
var cfg elasticsearch.Config
|
var cfg elasticsearch.Config
|
||||||
_cfg := g.Cfg().MustGetWithEnv(gctx.New(), "elasticsearch")
|
_cfg := g.Cfg().MustGetWithEnv(gctx.New(), "elasticsearch")
|
||||||
@@ -52,7 +60,7 @@ func NewV1(name string) *Elastic {
|
|||||||
// fmt.Printf("index:%#v\n", resp.Index)
|
// fmt.Printf("index:%#v\n", resp.Index)
|
||||||
//}
|
//}
|
||||||
|
|
||||||
// Set 索引文档
|
// Set 添加文档索引文档
|
||||||
func (s *Elastic) Set(ctx context.Context, key string, data interface{}) (err error) {
|
func (s *Elastic) Set(ctx context.Context, key string, data interface{}) (err error) {
|
||||||
// 添加文档
|
// 添加文档
|
||||||
_, err = s.client.Index(s.name).Id(key).Document(data).Do(ctx)
|
_, err = s.client.Index(s.name).Id(key).Document(data).Do(ctx)
|
||||||
@@ -61,11 +69,20 @@ func (s *Elastic) Set(ctx context.Context, key string, data interface{}) (err er
|
|||||||
|
|
||||||
// SetBulk 批量添加文档
|
// SetBulk 批量添加文档
|
||||||
func (s *Elastic) SetBulk(ctx context.Context, data []any) (err error) {
|
func (s *Elastic) SetBulk(ctx context.Context, data []any) (err error) {
|
||||||
var save *bulk.Request
|
var save bulk.Request
|
||||||
save = &bulk.Request{
|
save = make(bulk.Request, 0)
|
||||||
data,
|
for _, v := range data {
|
||||||
|
val := gconv.Map(v)
|
||||||
|
var saveIndex = elkBulk{}
|
||||||
|
saveIndex.Index.Index = s.name
|
||||||
|
if _, ok := val["uuid"]; ok {
|
||||||
|
saveIndex.Index.Id = val["uuid"].(string)
|
||||||
}
|
}
|
||||||
s.client.Bulk().Index(s.name).Request(save).Do(ctx)
|
save = append(save, saveIndex)
|
||||||
|
save = append(save, v)
|
||||||
|
}
|
||||||
|
//save = data
|
||||||
|
_, err = s.client.Bulk().Index(s.name).Request(&save).Do(ctx)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
19
pkg/s3/s3.go
19
pkg/s3/s3.go
@@ -65,7 +65,7 @@ func New(_name ...string) *Mod {
|
|||||||
&minio.Options{
|
&minio.Options{
|
||||||
Creds: credentials.NewStaticV4(cfg.AccessKey, cfg.SecretKey, ""),
|
Creds: credentials.NewStaticV4(cfg.AccessKey, cfg.SecretKey, ""),
|
||||||
Secure: cfg.Ssl,
|
Secure: cfg.Ssl,
|
||||||
BucketLookup: minio.BucketLookupPath,
|
//BucketLookup: minio.BucketLookupPath,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -135,9 +135,9 @@ func (s *Mod) ListBuckets() []minio.BucketInfo {
|
|||||||
func (s *Mod) PutObject(f io.Reader, name string, bucketName string, _size ...int64) (res minio.UploadInfo, err error) {
|
func (s *Mod) PutObject(f io.Reader, name string, bucketName string, _size ...int64) (res minio.UploadInfo, err error) {
|
||||||
// 初始化文件大小为 -1,表示将读取文件至结束
|
// 初始化文件大小为 -1,表示将读取文件至结束
|
||||||
var size = int64(-1)
|
var size = int64(-1)
|
||||||
if len(_size) > 0 {
|
//if len(_size) > 0 {
|
||||||
size = _size[0]
|
// size = _size[0]
|
||||||
}
|
//}
|
||||||
// 调用 S3 客户端上传文件,设置内容类型为 "application/octet-stream"
|
// 调用 S3 客户端上传文件,设置内容类型为 "application/octet-stream"
|
||||||
res, err = s.client.PutObject(ctx, bucketName, name, f, size, minio.PutObjectOptions{
|
res, err = s.client.PutObject(ctx, bucketName, name, f, size, minio.PutObjectOptions{
|
||||||
//ContentType: "application/octet-stream",
|
//ContentType: "application/octet-stream",
|
||||||
@@ -192,12 +192,13 @@ func (s *Mod) GetUrl(filePath string, defaultFile ...string) (url string) {
|
|||||||
filePath = defaultFile[0]
|
filePath = defaultFile[0]
|
||||||
}
|
}
|
||||||
|
|
||||||
switch s.cfg.Provider {
|
//switch s.cfg.Provider {
|
||||||
case "qiniu":
|
//case "qiniu":
|
||||||
url = get + path.Join(bucketName, filePath)
|
// url = get + path.Join(bucketName, filePath)
|
||||||
default:
|
//default:
|
||||||
|
// url = get + filePath
|
||||||
|
//}
|
||||||
url = get + filePath
|
url = get + filePath
|
||||||
}
|
|
||||||
|
|
||||||
if !s.cfg.Ssl {
|
if !s.cfg.Ssl {
|
||||||
url = get + path.Join(bucketName, filePath)
|
url = get + path.Join(bucketName, filePath)
|
||||||
|
|||||||
Reference in New Issue
Block a user