晚上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

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
}