feat: add wepay

This commit is contained in:
yanghao05
2025-04-11 16:49:57 +08:00
parent 3a04c0b39d
commit 7e17cd8164
7 changed files with 223 additions and 1 deletions

View File

@@ -0,0 +1 @@
package http

View File

@@ -1,12 +1,16 @@
package http
import (
"time"
"quyun/app/models"
"quyun/app/requests"
"quyun/database/schemas/public/model"
"quyun/providers/wepay"
"github.com/gofiber/fiber/v3"
"github.com/gofiber/fiber/v3/log"
"github.com/pkg/errors"
)
type ListQuery struct {
@@ -14,7 +18,9 @@ type ListQuery struct {
}
// @provider
type posts struct{}
type posts struct {
wepay *wepay.Client
}
// List posts
// @Router /posts [get]
@@ -40,3 +46,40 @@ func (ctl *posts) Mine(ctx fiber.Ctx, pagination *requests.Pagination, query *Li
log.Infof("Fetching posts for user with pagination: %+v and keyword: %v", pagination, query.Keyword)
return models.Users.PostList(ctx.Context(), 1, pagination, query.Keyword)
}
// Buy
// @Router /buy/:id [get]
// @Bind id path
func (ctl *posts) Buy(ctx fiber.Ctx, id int64) (*string, error) {
var userId int64 = 1
user, err := models.Users.GetByID(ctx.Context(), userId)
if err != nil {
return nil, errors.Wrapf(err, " failed to get user: %d", userId)
}
post, err := models.Posts.GetByID(ctx.Context(), id)
if err != nil {
return nil, errors.Wrapf(err, " failed to get post: %d", id)
}
// create order
order, err := models.Orders.Create(ctx.Context(), userId, post.ID)
if err != nil {
return nil, errors.Wrap(err, "订单创建失败")
}
body := ctl.wepay.
BodyMap().
Expire(30 * time.Minute).
Description(post.Title).
OutTradeNo(order.OrderNo).
Payer(user.OpenID)
prePayResp, err := ctl.wepay.V3TransactionJsapi(ctx.Context(), body)
if err != nil {
log.Errorf("wepay.V3TransactionJsapi err: %v", err)
return nil, errors.Wrap(err, "微信支付失败")
}
return &prePayResp.Response.PrepayId, nil
}

View File

@@ -17,6 +17,7 @@ import (
"quyun/providers/job"
"quyun/providers/jwt"
"quyun/providers/postgres"
"quyun/providers/wepay"
"go.ipao.vip/atom"
"go.ipao.vip/atom/container"
@@ -31,6 +32,7 @@ import (
func defaultProviders() container.Providers {
return service.Default(container.Providers{
ali.DefaultProvider(),
wepay.DefaultProvider(),
http.DefaultProvider(),
postgres.DefaultProvider(),
jwt.DefaultProvider(),