Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e5e6068337 | ||
|
|
1355634c22 |
@@ -1,5 +1,6 @@
|
|||||||
package honor
|
package honor
|
||||||
|
|
||||||
const (
|
const (
|
||||||
Host = "https://iap-api.cloud.honor.com"
|
Host = "https://iap-api-drcn.cloud.honor.com"
|
||||||
|
TokenHost = "https://hnoauth-login-drcn.cloud.honor.com"
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -9,19 +9,50 @@ import (
|
|||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"encoding/pem"
|
"encoding/pem"
|
||||||
"errors"
|
"errors"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/ayflying/utility_go/package/pay/common"
|
"github.com/ayflying/utility_go/package/pay/common"
|
||||||
|
"github.com/ayflying/utility_go/pkg"
|
||||||
|
"github.com/gogf/gf/v2/frame/g"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Pay struct {
|
type Pay struct {
|
||||||
PubKey string `json:"pubKey"`
|
PubKey string `json:"pubKey"`
|
||||||
AppId string `json:"appId"`
|
AppId string `json:"appId"`
|
||||||
|
ClientSecret string `json:"client_secret"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(pay *Pay) *Pay {
|
func New(pay *Pay) *Pay {
|
||||||
return &Pay{
|
return pay
|
||||||
AppId: pay.AppId,
|
}
|
||||||
PubKey: pay.PubKey,
|
|
||||||
|
func (p *Pay) GetToken(ctx context.Context) (accessToken string, err error) {
|
||||||
|
type TokenResp struct {
|
||||||
|
AccessToken string `json:"access_token"`
|
||||||
|
ExpiresIn int `json:"expires_in"`
|
||||||
|
TokenType string `json:"token_type"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
get, err := pkg.Cache("redis", "cache").GetOrSetFunc(ctx, "pay:honor:Sign:token", func(ctx context.Context) (value interface{}, err error) {
|
||||||
|
|
||||||
|
url := TokenHost + "/oauth2/v3/token"
|
||||||
|
get, err := g.Client().Post(ctx, url, g.Map{
|
||||||
|
"client_id": p.AppId,
|
||||||
|
"client_secret": p.ClientSecret,
|
||||||
|
"grant_type": "client_credentials",
|
||||||
|
})
|
||||||
|
|
||||||
|
//var res *TokenResp
|
||||||
|
//gjson.DecodeTo(get, &res)
|
||||||
|
value = get.ReadAllString()
|
||||||
|
return
|
||||||
|
}, time.Hour)
|
||||||
|
|
||||||
|
var res *TokenResp
|
||||||
|
err = get.Scan(&res)
|
||||||
|
accessToken = res.AccessToken
|
||||||
|
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// VerifyRSASignature 验证RSA数字签名
|
// VerifyRSASignature 验证RSA数字签名
|
||||||
@@ -29,13 +60,13 @@ func New(pay *Pay) *Pay {
|
|||||||
// sign: 签名的Base64编码字符串
|
// sign: 签名的Base64编码字符串
|
||||||
// pubKey: PEM格式的公钥字符串
|
// pubKey: PEM格式的公钥字符串
|
||||||
// 返回验证结果和可能的错误
|
// 返回验证结果和可能的错误
|
||||||
func (p *Pay) VerifyRSASignature(ctx context.Context, data []byte, sign string) (bool, error) {
|
func (p *Pay) VerifyRSASignature(ctx context.Context, data []byte, signature string) (bool, error) {
|
||||||
//req := g.RequestFromCtx(ctx).Request
|
//req := g.RequestFromCtx(ctx).Request
|
||||||
//post, err := common.ParseNotifyToBodyMap(req)
|
//post, err := common.ParseNotifyToBodyMap(req)
|
||||||
//var data = gjson.MustEncode(post)
|
//var data = gjson.MustEncode(post)
|
||||||
|
|
||||||
// 解码Base64格式的签名
|
// 解码Base64格式的签名
|
||||||
signBytes, err := base64.StdEncoding.DecodeString(sign)
|
signBytes, err := base64.StdEncoding.DecodeString(signature)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, errors.New("签名解码失败: " + err.Error())
|
return false, errors.New("签名解码失败: " + err.Error())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,11 +1,30 @@
|
|||||||
package honor
|
package honor
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
|
|
||||||
|
"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/os/gctx"
|
"github.com/gogf/gf/v2/os/gctx"
|
||||||
|
"github.com/gogf/gf/v2/util/grand"
|
||||||
|
|
||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// 响应结果结构体
|
||||||
|
type Response struct {
|
||||||
|
Code int `json:"code"` // 结果码 0: 成功,其他: 失败
|
||||||
|
Message string `json:"message"` // 错误信息
|
||||||
|
Data *DataContent `json:"data"` // 包含购买信息的结构体
|
||||||
|
}
|
||||||
|
|
||||||
|
// 数据内容结构体,对应data字段
|
||||||
|
type DataContent struct {
|
||||||
|
PurchaseProductInfo string `json:"purchaseProductInfo"` // 消耗结果数据的JSON字符串
|
||||||
|
DataSig string `json:"dataSig"` // purchaseProductInfo的签名
|
||||||
|
SigAlgorithm string `json:"sigAlgorithm"` // 签名算法,云侧加密算法为"RSA"
|
||||||
|
}
|
||||||
|
|
||||||
func (p *Pay) Notification(r *http.Request) {
|
func (p *Pay) Notification(r *http.Request) {
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -13,14 +32,30 @@ func (p *Pay) Notification(r *http.Request) {
|
|||||||
// ConsumeProduct 商品消耗
|
// ConsumeProduct 商品消耗
|
||||||
func (p *Pay) ConsumeProduct(purchaseToken string) (err error) {
|
func (p *Pay) ConsumeProduct(purchaseToken string) (err error) {
|
||||||
url := Host + "/iap/server/consumeProduct"
|
url := Host + "/iap/server/consumeProduct"
|
||||||
_, err = g.Client().ContentJson().Post(gctx.New(), url, g.Map{
|
//获取token
|
||||||
|
token, err := p.GetToken(gctx.New())
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
get, err := g.Client().ContentJson().Header(g.MapStrStr{
|
||||||
|
"access-token": token,
|
||||||
|
"x-iap-appid": p.AppId,
|
||||||
|
"purchaseToken": purchaseToken,
|
||||||
|
}).Post(gctx.New(), url, g.Map{
|
||||||
"purchaseToken": purchaseToken,
|
"purchaseToken": purchaseToken,
|
||||||
"developerChallenge": "",
|
"developerChallenge": grand.S(16),
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var res *Response
|
||||||
|
gjson.DecodeTo(get.ReadAllString(), &res)
|
||||||
|
if res.Code != 0 {
|
||||||
|
g.Log().Error(gctx.New(), "商品消耗失败: "+res.Message)
|
||||||
|
return errors.New(res.Message)
|
||||||
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user