分离小米支付与taptap支付到库里面

This commit is contained in:
ayflying
2025-07-08 16:30:44 +08:00
parent ed9b0dd248
commit ee9e399a81
5 changed files with 224 additions and 100 deletions

View File

@@ -1,60 +1,62 @@
package taptap
import (
"bytes"
"context"
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"fmt"
"github.com/gogf/gf/v2/os/gtime"
"github.com/gogf/gf/v2/util/grand"
"io"
"net/http"
"sort"
"strconv"
"strings"
)
type pTapTap struct {
Secret string `json:"secret" dc:"秘钥"`
OrderId string `json:"order_id" dc:"订单唯一 ID"`
Secret string `json:"secret" dc:"秘钥"`
//OrderId string `json:"order_id" dc:"订单唯一 ID"`
ClientId string `json:"client_id" dc:"应用的 Client ID"`
}
func New(orderId string) *pTapTap {
func New(clientId string, secret string) *pTapTap {
return &pTapTap{
Secret: "5AFEWnadBA0NgJK2mxeBLQEde0qyIefxLSc4XKHsx9AwkQRhxzkQ9DixsOkK6gcV",
ClientId: "mox88lbz43edfukdgk",
OrderId: orderId,
Secret: secret,
ClientId: clientId,
//OrderId: orderId,
}
}
func (p *pTapTap) Sign(url string, body []byte) (sign string, ts int64, nonce string, err error) {
//nolint:gosec
secret := p.Secret
//body := gjson.MustEncode(g.Map{})
//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"}}`)
//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
// Sign signs the request.
func (p *pTapTap) Sign(req *http.Request, secret string) (string, error) {
//获取请求参数
//req := g.RequestFromCtx(ctx).Request
return Sign(req, secret)
}
//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.
func Sign(req *http.Request, secret string) (string, error) {
methodPart := req.Method