晚上oppo支付回调与登录回调

This commit is contained in:
ayflying
2025-07-23 15:43:15 +08:00
parent 50fe34e1c1
commit 58bea0c09a
4 changed files with 124 additions and 95 deletions

97
package/pay/oppo/login.go Normal file
View File

@@ -0,0 +1,97 @@
package oppo
import (
"context"
"crypto/hmac"
"crypto/sha1"
"encoding/base64"
"github.com/gogf/gf/v2/encoding/gjson"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gtime"
"github.com/gogf/gf/v2/util/grand"
"io"
"net/url"
"strings"
)
const host = "https://iopen.game.heytapmobi.com"
// oppo参数类型
type LoginType struct {
Token string `json:"token"`
Ssoid string `json:"ssoid"`
Channel int `json:"channel"`
AdId string `json:"adId"`
}
//登录回复
type LoginResType struct {
ResultCode string `json:"resultCode" dc:"响应码,成功为 200"`
ResultMsg string `json:"resultMsg" dc:"响应信息"`
LoginToken string `json:"loginToken" dc:"透传的token"`
Ssoid string `json:"ssoid" dc:"透传的ssoid"`
//AppKey string `json:"appKey" dc:"秘钥key,因隐私安全规范,该字段目前已不返回信息"`
UserName string `json:"userName" dc:"用户ssoid绑定的账户昵称"`
//Email string `json:"email" dc:"因隐私安全规范,该字段目前已不返回信息"`
//MobileNumber string `json:"mobileNumber" dc:"因隐私安全规范,该字段目前已不返回信息"`
//CreateTime string `json:"createTime" dc:"因隐私安全规范,该字段目前已不返回信息"`
UserStatus string `json:"userStatus" dc:"用户状态NORMAL 表示正常"`
}
func (p *OppoType) FileIdInfo(ctx context.Context, oauthToken string, ssoid string) (res *LoginResType, err error) {
url := host + "/sdkopen/user/fileIdInfo"
header := p.GetHeader(oauthToken)
getHtml, err := g.Client().Header(header).Get(ctx, url, g.Map{
"token": oauthToken,
"fileId": ssoid,
})
getRes := getHtml.ReadAllString()
gjson.DecodeTo(getRes, &res)
g.Log().Debugf(ctx, "当前登陆请求的:%v", res)
return
}
func (p *OppoType) GenParam(oauthToken, oauthTimestamp, oauthNonce string) string {
// 注意:拼接的顺序不能有改变,不然会导致联运方验签失败
params := []string{
"oauthConsumerKey=" + url.QueryEscape(p.AppKey),
"oauthToken=" + url.QueryEscape(oauthToken),
"oauthSignatureMethod=" + url.QueryEscape("HMAC-SHA1"),
"oauthTimestamp=" + url.QueryEscape(oauthTimestamp),
"oauthNonce=" + url.QueryEscape(oauthNonce),
"oauthVersion=" + url.QueryEscape("1.0"),
}
return strings.Join(params, "&") + "&"
}
// 生成签名
func (p *OppoType) GenOauthSignature(param string) string {
oauthSignatureKey := p.AppSecret + "&"
mac := hmac.New(sha1.New, []byte(oauthSignatureKey))
io.WriteString(mac, param)
signature := base64.StdEncoding.EncodeToString(mac.Sum(nil))
return url.QueryEscape(signature)
}
func (p *OppoType) GetHeader(oauthToken string) (headers map[string]string) {
// 没有做过 urlEncode 的 token由游戏客户端调用 OPPO SDK 直接获取
//oauthToken := "TICKET_Ajnxxxxx"
oauthTimestamp := gtime.Now().TimestampStr()
oauthNonce := grand.S(5)
// 生成请求头参数和签名
param := p.GenParam(oauthToken, oauthTimestamp, oauthNonce)
oauthSignature := p.GenOauthSignature(param)
// 封装请求头
headers = map[string]string{
"param": param,
"oauthSignature": oauthSignature,
}
//fmt.Println("游戏服务端登录鉴权请求头为:", headers)
return
}

View File

@@ -2,118 +2,40 @@ 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"
"github.com/haxqer/xthird/oppo"
)
// 跟充值平台通信的加密key
//const PUBLIC_KEY = `dfsdfs`
type OppoType struct {
AppId string `json:"app_id"`
AppKey string `json:"app_key"`
AppSecret string `json:"app_secret"`
PublicKey string `json:"public_key"`
}
func New(PublicKey string) *OppoType {
func New(cfg *OppoType) *OppoType {
return &OppoType{
PublicKey: PublicKey,
AppKey: cfg.AppKey,
AppSecret: cfg.AppSecret,
PublicKey: cfg.PublicKey,
}
}
func (p *OppoType) Verify(ctx context.Context, data map[string]string) error {
func (p *OppoType) Verify(ctx context.Context) (err error) {
// OPPO公钥. 在官方给的 demo 中. 无需修改,改了就验证不过
oppoPublicKey := "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCmreYIkPwVovKR8rLHWlFVw7YDfm9uQOJKL89Smt6ypXGVdrAKKl0wNYc3/jecAoPi2ylChfa2iRu5gunJyNmpWZzlCNRIau55fxGW0XEu553IiprOZcaw5OuYGlf60ga8QT6qToP0/dpiL/ZbmNUO9kUhosIjEu22uFgR+5cYyQIDAQAB"
//oppoPublicKey := p.PublicKey
// 解析请求参数
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)
bodyMap, err := oppo.ParseNotifyToBodyMap(g.RequestFromCtx(ctx).Request)
if err != nil {
//http.Error(w, "Verification error: "+err.Error(), http.StatusInternalServerError)
g.Log().Errorf(ctx, "Verification error: %v", err.Error())
return err
// 解析失败, 处理错误逻辑
return
}
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
err = oppo.VerifySign(oppoPublicKey, bodyMap)
return
}