PaymentCenter/app/http/controllers/front/pay_page.go

105 lines
2.6 KiB
Go

package front
import (
"PaymentCenter/app/constants/common"
"PaymentCenter/app/constants/errorcode"
"PaymentCenter/app/http/controllers"
"PaymentCenter/app/http/entities/front"
"PaymentCenter/app/models/ordersmodel"
"PaymentCenter/app/models/paychannelmodel"
"PaymentCenter/app/services"
"PaymentCenter/app/services/thirdpay"
"fmt"
"github.com/ahmetb/go-linq/v3"
"github.com/gin-gonic/gin"
"net/http"
"strconv"
"strings"
)
// 预支付接口V2, 返回收银台页面地址
func PayUrlV2(c *gin.Context) {
var res front.ApiResponse
req := controllers.GetRequest(c).(*front.PayReqsV2)
// 更新请求日志需要的参数
c.Set("OutTradeNo", req.OutTradeNo)
appCheckInfo := controllers.GetAppCheckInfo(c).(*services.AppCheck)
result, code := thirdpay.NewPayUrl(*req).
WithApp(appCheckInfo).
WithClientIp(c.ClientIP()).
PayUrlV2Service()
if result.Order != nil {
res.Order = thirdpay.NewOrdersResp(result.Order)
res.Url = result.Url
}
controllers.ApiRes(c, res, code)
return
}
// 收银台页面
func PayPage(c *gin.Context) {
var (
code int
message string
amount string
orderInfo ordersmodel.Orders
desc string
)
orderId := strings.TrimSpace(c.Query("no"))
if orderId == "" {
message = "页面不存在"
} else {
orderInfo, code = thirdpay.PayPageCheckOrder(orderId)
if code != errorcode.Success {
message = errorcode.GetMsg(code, "")
} else {
amount = fmt.Sprintf("%.2f", float64(orderInfo.Amount)/100)
desc = orderInfo.Desc
}
}
c.HTML(http.StatusOK, "payPage.html", gin.H{
"code": code,
"message": message,
"id": orderId,
"amount": amount,
"desc": desc,
})
}
// 收银台获取支付渠道列表接口
func PayChannelList(c *gin.Context) {
req, _ := controllers.GetRequest(c).(*front.PayChannelListRequest)
req.UserAgent = c.Request.UserAgent()
data, code := services.PayPageChannelList(*req)
result := []front.PayChannelListResponse{}
linq.From(data).SelectT(func(payChannel paychannelmodel.PayChannel) front.PayChannelListResponse {
return front.PayChannelListResponse{
ChannelType: payChannel.ChannelType,
PayName: common.PayChannelName[payChannel.ChannelType],
PayChannelId: strconv.Itoa(int(payChannel.Id)),
}
}).ToSlice(&result)
controllers.HandCodeRes(c, result, code)
}
// 获取付款链接,返回付款模板
func GetPayLink(c *gin.Context) {
req, _ := controllers.GetRequest(c).(*front.GetPayLinkRequest)
result, message, code := thirdpay.GetPayLinkService(*req)
if message == "" {
message = errorcode.GetMsg(code, "")
}
c.HTML(http.StatusOK, "payTemplateDefault.html", gin.H{
"code": code,
"payUrl": result,
"message": message,
})
}