feat: 增加支付宝小程序获取openid接口

This commit is contained in:
wolter 2025-06-20 16:59:50 +08:00
parent 9108c2bce9
commit 4aa22bf346
6 changed files with 99 additions and 0 deletions

View File

@ -0,0 +1,26 @@
package front
import (
"PaymentCenter/app/constants/errorcode"
"PaymentCenter/app/http/controllers"
"PaymentCenter/app/http/entities/front"
"PaymentCenter/app/services/thirdpay"
"PaymentCenter/app/utils"
"github.com/gin-gonic/gin"
)
// 支付宝小程序获取授权获取openId
func GetAliOauth(c *gin.Context) {
req, _ := controllers.GetRequest(c).(*front.GetAliOauthRequest)
rsp, err := thirdpay.GetAliOauth(c, *req)
if err != nil {
utils.Log(c, "GetAliOauth-获取支付宝授权失败", err.Error())
c.JSON(200, gin.H{
"code": errorcode.SystemError,
"message": err.Error(),
"data": "",
})
return
}
controllers.HandCodeRes(c, rsp, errorcode.Success)
}

View File

@ -0,0 +1,6 @@
package front
type GetAliOauthRequest struct {
Code string `json:"code" form:"code" validate:"required" `
PayChannelId string `json:"pay_channel_id" form:"pay_channel_id" validate:"required" `
}

View File

@ -34,6 +34,7 @@ var FrontRequestMapBeforeDecrypt = map[string]func() interface{}{
common.FRONT_V1 + "/payPage/list": func() interface{} { return new(front.PayChannelListRequest) },
common.FRONT_V1 + "/payPage/submit": func() interface{} { return new(front.GetPayLinkRequest) },
common.FRONT_V1 + "/ali/getOauth": func() interface{} { return new(front.GetAliOauthRequest) },
common.WXCodeRedirectAddress: func() interface{} { return new(front.WxJsApiPayRequest) },
}

View File

@ -66,6 +66,9 @@ func RegisterRoute(router *gin.Engine) {
v1.GET("/brokerWechatUrl", front.BrokerWechatUrl)
}
// 支付宝小程序获取授权获取openId
v1.POST("/ali/getOauth", middlewares.ValidateRequest(), front.GetAliOauth)
// 微信获取授权相关
router.LoadHTMLGlob("./front/templates/*")
wx := v1.Group("/wx", middlewares.ValidateRequest())

View File

@ -0,0 +1,42 @@
package thirdpay
import (
"PaymentCenter/app/data"
"PaymentCenter/app/http/entities/front"
"PaymentCenter/app/models/paychannelmodel"
"PaymentCenter/app/third/paymentService"
"context"
"fmt"
"github.com/go-pay/gopay/alipay"
"github.com/goccy/go-json"
"xorm.io/builder"
)
// 小程序支付 通过code换取网页授权access_tokenopenid,
func GetAliOauth(ctx context.Context, param front.GetAliOauthRequest) (result *alipay.OauthTokenInfo, err error) {
var (
has bool
payChannel = paychannelmodel.PayChannel{}
repo = data.NewPayChannelRepo(paychannelmodel.GetInstance().GetDb())
)
// 获取支付渠道的配置
has, err = repo.PayChannelGet(&payChannel, builder.Eq{"id": param.PayChannelId})
if err != nil {
return
} else if !has {
err = fmt.Errorf("获取支付渠道不存在")
return
}
// 配置支付的解析
aliConfig := paymentService.AliPay{}
err = json.Unmarshal([]byte(payChannel.ExtJson), &aliConfig)
if err != nil {
err = fmt.Errorf("解析支付渠道配置失败")
return
}
// 支付宝,获取小程序 授权信息 openid
result, err = paymentService.ALiGetOpenId(ctx, aliConfig, param.Code)
return
}

View File

@ -399,3 +399,24 @@ func ALiJsApiPayInfo(c context.Context, payOrderRequest PayOrderRequest) (string
return aliRsp.Response.TradeNo, nil
}
// 支付宝,获取小程序 授权信息 openid
func ALiGetOpenId(c context.Context, ali AliPay, authToken string) (result *alipay.OauthTokenInfo, err error) {
// 初始化支付宝客户端
aliClient, err := AliInitClient(ali)
if err != nil {
return
}
// 取得 user_id 和 token授权令牌
//请求参数
bm := make(gopay.BodyMap)
bm.Set("grant_type", "authorization_code")
bm.Set("code", authToken)
oauthRsp, err := aliClient.SystemOauthToken(c, bm)
if err != nil {
return
}
return oauthRsp.Response, nil
}