fix: issues

This commit is contained in:
Rogee
2025-01-14 19:48:23 +08:00
parent b429f30916
commit 48a0a6c195
14 changed files with 1037 additions and 195 deletions

View File

@@ -61,7 +61,7 @@ func (c *OrderController) List(ctx fiber.Ctx, claim *jwt.Claims, pagination *req
// Create order
// @Router /api/v1/orders [post]
// @Bind claim local
// @Bind hash path
// @Bind hash
// @Bind tenantSlug cookie key(tenant)
func (c *OrderController) Create(ctx fiber.Ctx, claim *jwt.Claims, tenantSlug, hash string) (*UserOrder, error) {
user, err := c.userSvc.GetUserByID(ctx.Context(), claim.UserID)

View File

@@ -1,6 +1,8 @@
package orders
import (
"fmt"
"backend/app/errorx"
"backend/app/http/posts"
"backend/app/http/tenants"
@@ -10,7 +12,6 @@ import (
"backend/providers/jwt"
"backend/providers/pay"
"github.com/go-pay/gopay/wechat/v3"
"github.com/gofiber/fiber/v3"
"github.com/samber/lo"
log "github.com/sirupsen/logrus"
@@ -32,10 +33,11 @@ func (c *PayController) Prepare() error {
}
// JSPay
// @Router /api/v1/orders/pay/:orderID/js [get]
// @Router /api/v1/orders/pay/:orderID/:channel [get]
// @Bind claim local
// @Bind orderID path
func (ctl *PayController) JSPay(ctx fiber.Ctx, claim *jwt.Claims, orderID string) (*wechat.JSAPIPayParams, error) {
// @Bind channel path
func (ctl *PayController) Pay(ctx fiber.Ctx, claim *jwt.Claims, channel, orderID string) (any, error) {
order, err := ctl.svc.GetUserOrderByOrderID(ctx.Context(), orderID, claim.UserID)
if err != nil {
return nil, err
@@ -58,6 +60,15 @@ func (ctl *PayController) JSPay(ctx fiber.Ctx, claim *jwt.Claims, orderID string
return nil, errorx.BadRequest.WithMsg("未绑定微信")
}
switch channel {
case "wechat-js":
return ctl.payWechatJS(ctx, order, &oauth)
}
return nil, errorx.BadRequest.WithMsg("支付渠道错误")
}
func (ctl *PayController) payWechatJS(ctx fiber.Ctx, order *model.Orders, oauth *model.UserOauths) (any, error) {
params, err := ctl.pay.WeChat_JSApiPayRequest(
ctx.Context(),
oauth.OpenID,
@@ -65,7 +76,7 @@ func (ctl *PayController) JSPay(ctx fiber.Ctx, claim *jwt.Claims, orderID string
order.Title,
order.Amount,
1,
"/v1/orders/pay/wechat/notify",
ctl.getNotifyURL(ctx, "wechat"),
)
if err != nil {
return nil, err
@@ -73,3 +84,13 @@ func (ctl *PayController) JSPay(ctx fiber.Ctx, claim *jwt.Claims, orderID string
return params, nil
}
func (ctl *PayController) getNotifyURL(ctx fiber.Ctx, channel string) string {
return fmt.Sprintf("%s://%s/v1/orders/pay/notify/%s", ctx.Request().URI().Scheme(), ctx.Host(), channel)
}
// @Router /v1/orders/pay/notify/:channel [post]
// @Bind channel path
func (c *PayController) Notify(ctx fiber.Ctx, channel string) (any, error) {
return nil, nil
}

View File

@@ -46,9 +46,10 @@ func (r *Routes) Register(router fiber.Router) {
))
// 注册路由组: PayController
router.Get("/api/v1/orders/pay/:orderID/js", DataFunc2(
r.payController.JSPay,
router.Get("/api/v1/orders/pay/:orderID/:channel", DataFunc3(
r.payController.Pay,
Local[*jwt.Claims]("claim"),
PathParam[string]("channel"),
PathParam[string]("orderID"),
))