feat: 支付闭环
This commit is contained in:
@@ -8,6 +8,7 @@ type ApiGroup struct {
|
||||
ProgramApi
|
||||
SubscriptionApi
|
||||
InteractionApi
|
||||
PayApi
|
||||
}
|
||||
|
||||
var ApiGroupApp = new(ApiGroup)
|
||||
@@ -18,4 +19,5 @@ var (
|
||||
programService = service.GroupApp.RadioServiceGroup.ProgramService
|
||||
subscriptionService = service.GroupApp.RadioServiceGroup.SubscriptionService
|
||||
interactionService = service.GroupApp.RadioServiceGroup.InteractionService
|
||||
payService = service.GroupApp.RadioServiceGroup.PayService
|
||||
)
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
package radio
|
||||
|
||||
import (
|
||||
"sundynix-go/global"
|
||||
"sundynix-go/model/commom/response"
|
||||
"sundynix-go/utils/auth"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
type PayApi struct{}
|
||||
|
||||
// PrePay 预支付
|
||||
// @Tags 微信支付
|
||||
// @Summary 支付
|
||||
// @Security ApiKeyAuth
|
||||
// @Accept application/json
|
||||
// @Produce application/json
|
||||
// @Param orderId query string true "支付"
|
||||
// @Success 200 {string} string "{"success":true,"data":{},"msg":"支付成功"}"
|
||||
// @Router /pay/prePay [get]
|
||||
func (a *PayApi) PrePay(c *gin.Context) {
|
||||
orderId := c.Query("orderId")
|
||||
userId := auth.GetUserId(c)
|
||||
res, err := payService.PrePay(orderId, userId)
|
||||
if err != nil {
|
||||
global.Logger.Error("支付失败", zap.Error(err))
|
||||
response.FailWithMsg("支付失败:"+err.Error(), c)
|
||||
return
|
||||
}
|
||||
response.OkWithData(res, c)
|
||||
}
|
||||
|
||||
// QueryPay 查询支付
|
||||
// @Tags 微信支付
|
||||
// @Summary 支付
|
||||
// @Security BasicAuth
|
||||
// @Produce application/json
|
||||
// @Param orderId query string true "支付"
|
||||
// @Success 200 {string} string "{"success":true,"data":{},"msg":"支付成功"}"
|
||||
// @Router /pay/query [get]
|
||||
func (a *PayApi) QueryPay(c *gin.Context) {
|
||||
outTradeNo := c.Query("outTradeNo")
|
||||
userId := auth.GetUserId(c)
|
||||
res, err := payService.QueryPay(outTradeNo, userId)
|
||||
if err != nil {
|
||||
global.Logger.Error("支付失败", zap.Error(err))
|
||||
response.FailWithMsg("支付失败:"+err.Error(), c)
|
||||
return
|
||||
}
|
||||
response.OkWithData(res, c)
|
||||
}
|
||||
|
||||
// PayCallback 支付回调
|
||||
// @Tags 微信支付
|
||||
// @Summary 支付回调
|
||||
func (a *PayApi) PayCallback(c *gin.Context) {
|
||||
err := payService.PayCallback(c)
|
||||
if err != nil {
|
||||
global.Logger.Error("支付回调失败", zap.Error(err))
|
||||
response.FailWithMsg("支付回调失败:"+err.Error(), c)
|
||||
return
|
||||
}
|
||||
response.OkWithMsg("支付回调成功", c)
|
||||
}
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
common "sundynix-go/model/commom/request"
|
||||
"sundynix-go/model/commom/response"
|
||||
"sundynix-go/model/radio/request"
|
||||
radioRes "sundynix-go/model/radio/response"
|
||||
"sundynix-go/utils/auth"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
@@ -29,7 +30,6 @@ func (a *SubscriptionApi) GetSubscriptionList(c *gin.Context) {
|
||||
response.FailWithMsg("参数错误: "+err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
list, total, err := subscriptionService.GetUserSubscription(userId, req)
|
||||
if err != nil {
|
||||
global.Logger.Error("获取订阅列表失败!", zap.Error(err))
|
||||
@@ -44,91 +44,30 @@ func (a *SubscriptionApi) GetSubscriptionList(c *gin.Context) {
|
||||
}, c)
|
||||
}
|
||||
|
||||
// CanSubscribe 检查是否可以订阅
|
||||
// UnlockChannel 解锁频道(拉起支付)
|
||||
// @Tags 订阅管理
|
||||
// @Summary 检查是否可以订阅
|
||||
// @Summary 解锁频道
|
||||
// @Param id query string true "id"
|
||||
// @Param eventType query string true "eventType"
|
||||
// @Produce application/json
|
||||
// @Param data body request.SubscribeChannel true "频道ID"
|
||||
// @Success 200 {object} response.Response
|
||||
// @Router /radio/subscription/can-subscribe [post]
|
||||
func (a *SubscriptionApi) CanSubscribe(c *gin.Context) {
|
||||
userId := auth.GetUserId(c)
|
||||
var req request.SubscribeChannel
|
||||
// @Router /radio/subscription/unlock [post]
|
||||
func (a *SubscriptionApi) UnlockChannel(c *gin.Context) {
|
||||
var req request.UnlockChannel
|
||||
err := c.ShouldBindJSON(&req)
|
||||
if err != nil {
|
||||
response.FailWithMsg("参数错误: "+err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
can, _, err := subscriptionService.CanSubscribe(userId, req.ChannelId)
|
||||
if err != nil {
|
||||
global.Logger.Error("检查订阅权限失败!", zap.Error(err))
|
||||
response.FailWithMsg(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
response.OkWithData(can, c)
|
||||
}
|
||||
|
||||
// Subscribe 订阅频道
|
||||
// @Tags 订阅管理
|
||||
// @Summary 订阅频道
|
||||
// @Produce application/json
|
||||
// @Param data body request.SubscribeChannel true "频道ID"
|
||||
// @Success 200 {object} response.Response
|
||||
// @Router /radio/subscription/subscribe [post]
|
||||
func (a *SubscriptionApi) Subscribe(c *gin.Context) {
|
||||
userId := auth.GetUserId(c)
|
||||
var req request.SubscribeChannel
|
||||
err := c.ShouldBindJSON(&req)
|
||||
res, no, err := subscriptionService.UnlockChannel(userId, req)
|
||||
if err != nil {
|
||||
response.FailWithMsg("参数错误: "+err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
can, reason, err := subscriptionService.CanSubscribe(userId, req.ChannelId)
|
||||
if err != nil {
|
||||
global.Logger.Error("订阅失败!", zap.Error(err))
|
||||
global.Logger.Error("解锁频道失败!", zap.Error(err))
|
||||
response.FailWithMsg(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
if !can {
|
||||
response.FailWithMsg(reason, c)
|
||||
return
|
||||
}
|
||||
|
||||
// 订阅类型 1:免费
|
||||
if err := subscriptionService.Subscribe(userId, req.ChannelId, 1); err != nil {
|
||||
global.Logger.Error("订阅失败!", zap.Error(err))
|
||||
response.FailWithMsg(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
response.OkWithMsg("订阅成功", c)
|
||||
}
|
||||
|
||||
// Unsubscribe 退订频道
|
||||
// @Tags 订阅管理
|
||||
// @Summary 退订频道
|
||||
// @Produce application/json
|
||||
// @Param data body request.UnsubscribeChannel true "频道ID"
|
||||
// @Success 200 {object} response.Response
|
||||
// @Router /radio/subscription/unsubscribe [post]
|
||||
func (a *SubscriptionApi) Unsubscribe(c *gin.Context) {
|
||||
userId := auth.GetUserId(c)
|
||||
var req request.UnsubscribeChannel
|
||||
err := c.ShouldBindJSON(&req)
|
||||
if err != nil {
|
||||
response.FailWithMsg("参数错误: "+err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
if err := subscriptionService.Unsubscribe(userId, req.ChannelId); err != nil {
|
||||
global.Logger.Error("退订失败!", zap.Error(err))
|
||||
response.FailWithMsg(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
response.OkWithMsg("退订成功", c)
|
||||
response.OkWithData(radioRes.PrePayResult{
|
||||
Payments: res,
|
||||
OutTradeNo: no,
|
||||
}, c)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user