diff --git a/app/http/controllers/front/ali.go b/app/http/controllers/front/ali.go new file mode 100644 index 0000000..20f6bd4 --- /dev/null +++ b/app/http/controllers/front/ali.go @@ -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) +} diff --git a/app/http/entities/front/ali.go b/app/http/entities/front/ali.go new file mode 100644 index 0000000..8de4930 --- /dev/null +++ b/app/http/entities/front/ali.go @@ -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" ` +} diff --git a/app/http/requestmapping/front.go b/app/http/requestmapping/front.go index 14b37aa..c4e5b17 100644 --- a/app/http/requestmapping/front.go +++ b/app/http/requestmapping/front.go @@ -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) }, } diff --git a/app/http/routes/route.go b/app/http/routes/route.go index 7b75ef4..8208bf8 100644 --- a/app/http/routes/route.go +++ b/app/http/routes/route.go @@ -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()) diff --git a/app/services/thirdpay/ali.go b/app/services/thirdpay/ali.go new file mode 100644 index 0000000..cb2631e --- /dev/null +++ b/app/services/thirdpay/ali.go @@ -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_token,openid, +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 +} diff --git a/app/third/paymentService/ali_service.go b/app/third/paymentService/ali_service.go index 80446c3..a3742b4 100644 --- a/app/third/paymentService/ali_service.go +++ b/app/third/paymentService/ali_service.go @@ -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 +}