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

100 lines
2.5 KiB
Go

package front
import (
"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"
"xorm.io/builder"
)
// 预支付接口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
)
orderId := c.Query("no")
if orderId != "" {
orderInfo := &ordersmodel.Orders{}
orderInfo, code = services.OrderFindOne(&ordersmodel.Orders{}, builder.Eq{"id": orderId})
if code == errorcode.Success {
amount = fmt.Sprintf("%.2f", float64(orderInfo.Amount)/100)
} else {
message = errorcode.GetMsg(code, "")
}
} else {
message = "页面不存在"
}
c.HTML(http.StatusOK, "payPage.html", gin.H{
"code": code,
"message": message,
"id": orderId,
"amount": amount,
})
}
// 收银台获取支付渠道列表
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: payChannel.PayName,
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 code == errorcode.Success {
c.HTML(http.StatusOK, "payTemplate.html", gin.H{
"payUrl": result,
})
} else {
c.String(http.StatusOK, errorcode.GetMsg(code, "")+message)
}
}