feat: 付款链接接口

This commit is contained in:
wolter 2024-12-21 17:26:26 +08:00
parent 0ae425e4bb
commit 64973f683d
5 changed files with 114 additions and 13 deletions

View File

@ -61,6 +61,7 @@ const (
PayChannelNotFound = 1300 PayChannelNotFound = 1300
PayChannelNotBuild = 1301 PayChannelNotBuild = 1301
PayChannelExtJsonError = 1302 PayChannelExtJsonError = 1302
PayChannelExpired = 1303
//订单 //订单
OrdersNotFound = 1401 OrdersNotFound = 1401

View File

@ -9,6 +9,7 @@ import (
"github.com/ahmetb/go-linq/v3" "github.com/ahmetb/go-linq/v3"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"net/http" "net/http"
"strconv"
) )
// 预支付接口V2, 返回收银台页面 // 预支付接口V2, 返回收银台页面
@ -50,8 +51,17 @@ func PayChannelList(c *gin.Context) {
return front.PayChannelListResponse{ return front.PayChannelListResponse{
ChannelType: payChannel.ChannelType, ChannelType: payChannel.ChannelType,
PayName: payChannel.PayName, PayName: payChannel.PayName,
PayChannelId: strconv.Itoa(int(payChannel.Id)),
} }
}).ToSlice(&result) }).ToSlice(&result)
controllers.HandCodeRes(c, result, code) controllers.HandCodeRes(c, result, code)
} }
// 获取付款链接
func GetPayLink(c *gin.Context) {
req, _ := controllers.GetRequest(c).(*front.GetPayLinkRequest)
result, code := services.GetPayLinkService(*req)
controllers.HandCodeRes(c, result, code)
}

View File

@ -69,6 +69,7 @@ type PayChannelListRequest struct {
type PayChannelListResponse struct { type PayChannelListResponse struct {
PayName string `json:"pay_name"` PayName string `json:"pay_name"`
ChannelType int `json:"channel_type"` ChannelType int `json:"channel_type"`
PayChannelId string `json:"pay_channel_id"`
} }
type PayReqsV2 struct { type PayReqsV2 struct {
@ -90,3 +91,8 @@ type PayReqsV2Response struct {
Url string Url string
ThirdMsg string ThirdMsg string
} }
type GetPayLinkRequest struct {
PayChannelId string `json:"pay_channel_id"`
OrderId string `json:"id"`
}

View File

@ -6,11 +6,14 @@ import (
"PaymentCenter/app/http/entities" "PaymentCenter/app/http/entities"
"PaymentCenter/app/http/entities/backend" "PaymentCenter/app/http/entities/backend"
"PaymentCenter/app/http/entities/front" "PaymentCenter/app/http/entities/front"
"PaymentCenter/app/models/orderrequestlogmodel"
"PaymentCenter/app/models/ordersmodel" "PaymentCenter/app/models/ordersmodel"
"PaymentCenter/app/models/paychannelmodel" "PaymentCenter/app/models/paychannelmodel"
"PaymentCenter/app/utils" "PaymentCenter/app/utils"
"PaymentCenter/config" "PaymentCenter/config"
"encoding/json"
"fmt" "fmt"
"net/url"
"strconv" "strconv"
"time" "time"
"xorm.io/builder" "xorm.io/builder"
@ -106,20 +109,21 @@ func (this *payUrl) WithClientIp(ip string) *payUrl {
return this return this
} }
// 订单存在 // 订单是否是支付状态
func (this *payUrl) orderExist() { func orderStatusCheck(order ordersmodel.Orders) (code int) {
switch this.result.Order.Status { switch order.Status {
case common.ORDER_STATUS_CLOSE: case common.ORDER_STATUS_CLOSE:
this.result.PayCode = errorcode.OrderClosed code = errorcode.OrderClosed
case common.ORDER_STATUS_PAYED: case common.ORDER_STATUS_PAYED:
this.result.PayCode = errorcode.OrderPayed code = errorcode.OrderPayed
case common.ORDER_STATUS_FAILED: case common.ORDER_STATUS_FAILED:
this.result.PayCode = errorcode.OrderFailed code = errorcode.OrderFailed
case common.ORDER_STATUS_WAITPAY, common.ORDER_STATUS_PAYING: case common.ORDER_STATUS_WAITPAY, common.ORDER_STATUS_PAYING:
this.result.PayCode = errorcode.Success code = errorcode.Success
default: default:
this.result.PayCode = errorcode.OrderStatusErr code = errorcode.OrderStatusErr
} }
return code
} }
func (this *payUrl) saveOrder() { func (this *payUrl) saveOrder() {
@ -173,7 +177,7 @@ func (this *payUrl) PayUrlV2Service() (result front.PayReqsV2Response, code int)
if code == errorcode.Success { if code == errorcode.Success {
// 订单存在 // 订单存在
this.result.Order = order this.result.Order = order
this.orderExist() this.result.PayCode = orderStatusCheck(*order)
} else if code == errorcode.OrdersNotFound { } else if code == errorcode.OrdersNotFound {
// 订单不存在 // 订单不存在
this.saveOrder() this.saveOrder()
@ -187,3 +191,80 @@ func (this *payUrl) PayUrlV2Service() (result front.PayReqsV2Response, code int)
return this.result, code return this.result, code
} }
// 拼接微信授权链接 appid:微信的公众号appid
func GetWxAuthUrl(appid string, orderId int64) (targetUrl string, code int) {
var (
// 重定向地址
redirectUri = config.GetConf().PayService.Host + common.WXCodeRedirectAddress
)
baseUrl := "https://open.weixin.qq.com/connect/oauth2/authorize"
redirectUri = url.QueryEscape(redirectUri)
responseType := "code"
scope := "snsapi_base"
order := strconv.Itoa(int(orderId))
targetUrl = baseUrl + "?appid=" + appid + "&redirect_uri=" + redirectUri + "&response_type=" + responseType + "&scope=" + scope + "&state=" + order + "#wechat_redirect"
code = errorcode.Success
return
}
// 收银台获取付款链接
func GetPayLinkService(req front.GetPayLinkRequest) (result string, code int) {
var (
payChannel = &paychannelmodel.PayChannel{}
order = &ordersmodel.Orders{}
orderPayRequest front.PayReqsV2
)
// 支付方式校验
payChannel, code = PayChannelFindOne(payChannel, builder.Eq{"id": req.PayChannelId})
if code != errorcode.Success {
return
}
if payChannel.ExpireTime.Unix() < time.Now().Unix() {
code = errorcode.PayChannelExpired
return
}
// 订单校验
order, code = OrderFindOne(order, builder.Eq{"id": req.OrderId})
if code != errorcode.Success {
return
}
// 订单状态校验
code = orderStatusCheck(*order)
if code != errorcode.Success {
return
}
// 获取订单的请求参数
orderRequestLog := orderrequestlogmodel.OrderRequestLog{
OutTradeNo: order.OutTradeNo,
AppId: order.AppId,
}
has, err := OrderRequestLogs(&orderRequestLog)
if err != nil {
code = handErr(err)
return
}
if !has {
code = errorcode.OrderPayRequestLogNotExist
return
}
err = json.Unmarshal([]byte(orderRequestLog.MerchantRequest), &orderPayRequest)
if err != nil {
code = handErr(err)
return
}
// 支付方式需要用户授权
if payChannel.ChannelType == common.PAY_CHANNEL_WECHAT_JSAPI && orderPayRequest.OpenId == "" {
result, code = GetWxAuthUrl(payChannel.AppId, order.Id)
return
}
// 请求第三方支付获取付款链接
//payParam := paymentService.PayOrderRequest{
// OrderId: order.Id,
//}
//res := paymentService.PaymentService(context.Background(), payParam)
//result = res.Result
//code = res.Code
return
}

View File

@ -28,6 +28,9 @@ func Bootstrap(conf *config.Config) (err error) {
err = db.Pr.Register(db.SingletonMain, conf.Db) err = db.Pr.Register(db.SingletonMain, conf.Db)
//cacher := caches.NewLRUCacher2(caches.NewMemoryStore(), time.Hour, 1000) //cacher := caches.NewLRUCacher2(caches.NewMemoryStore(), time.Hour, 1000)
//db.GetDb().SetDefaultCacher(cacher) //db.GetDb().SetDefaultCacher(cacher)
if conf.Debug {
db.GetDb().ShowSQL(true)
}
if err != nil { if err != nil {
return return