97 lines
2.3 KiB
Go
97 lines
2.3 KiB
Go
package orders
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"backend/app/errorx"
|
|
"backend/app/http/posts"
|
|
"backend/app/http/tenants"
|
|
"backend/app/http/users"
|
|
"backend/database/fields"
|
|
"backend/database/models/qvyun_v2/public/model"
|
|
"backend/providers/jwt"
|
|
"backend/providers/pay"
|
|
|
|
"github.com/gofiber/fiber/v3"
|
|
"github.com/samber/lo"
|
|
log "github.com/sirupsen/logrus"
|
|
)
|
|
|
|
// @provider
|
|
type PayController struct {
|
|
svc *Service
|
|
pay *pay.Client
|
|
userSvc *users.Service
|
|
tenantSvc *tenants.Service
|
|
postSvc *posts.Service
|
|
log *log.Entry `inject:"false"`
|
|
}
|
|
|
|
func (c *PayController) Prepare() error {
|
|
c.log = log.WithField("module", "orders.Controller")
|
|
return nil
|
|
}
|
|
|
|
// JSPay
|
|
// @Router /api/v1/orders/pay/:orderID/:channel [get]
|
|
// @Bind claim local
|
|
// @Bind orderID path
|
|
// @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
|
|
}
|
|
|
|
if order.Status != fields.OrderStatusPending {
|
|
return nil, errorx.BadRequest.WithMsg("订单状态异常")
|
|
}
|
|
|
|
oauths, err := ctl.userSvc.GetUserOAuthChannels(ctx.Context(), claim.UserID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
oauth, ok := lo.Find(oauths, func(v model.UserOauths) bool {
|
|
return v.Channel == fields.AuthChannelWeChat
|
|
})
|
|
|
|
if !ok {
|
|
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,
|
|
order.OrderSerial,
|
|
order.Title,
|
|
order.Amount,
|
|
1,
|
|
ctl.getNotifyURL(ctx, "wechat"),
|
|
)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
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
|
|
}
|