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

102 lines
2.8 KiB
Go

package front
import (
"PaymentCenter/app/constants/errorcode"
"PaymentCenter/app/http/controllers"
"PaymentCenter/app/http/entities/front"
"PaymentCenter/app/models/ordersmodel"
"PaymentCenter/app/services"
"PaymentCenter/app/services/thirdpay"
"github.com/gin-gonic/gin"
"xorm.io/builder"
)
func PayUrl(c *gin.Context) {
var res front.ApiResponse
req := controllers.GetRequest(c).(*front.PayReqs)
c.Set("OutTradeNo", req.OutTradeNo)
appCheckInfo := controllers.GetAppCheckInfo(c).(*services.AppCheck)
check := thirdpay.ThirdPayInfoCheck(c.Request.Context(), req, appCheckInfo, c.ClientIP())
if check.CheckCode != errorcode.Success {
controllers.ApiRes(c, nil, check.CheckCode)
return
}
check.CheckOrderPay()
if check.CheckCode != errorcode.Success {
controllers.ApiRes(c, nil, check.CheckCode)
return
}
pay := thirdpay.ThirdPayUrl(check)
if pay.PayCode != errorcode.Success {
controllers.ApiRes(c, nil, pay.PayCode, pay.ThirdMsg)
return
}
res.Order = thirdpay.NewOrdersResp(pay.Order)
res.Url = pay.Url
controllers.ApiRes(c, res, pay.PayCode)
return
}
func Refund(c *gin.Context) {
req := controllers.GetRequest(c).(*front.RefundReqs)
c.Set("OutTradeNo", req.OutTradeNo)
appCheckInfo := controllers.GetAppCheckInfo(c).(*services.AppCheck)
check, errCode := thirdpay.ThirdPayRefundCheck(c.Request.Context(), req, appCheckInfo, c.ClientIP())
if errCode != errorcode.Success {
controllers.ApiRes(c, nil, errCode)
return
}
refund, errCode := thirdpay.ThirdPayRefund(check)
if errCode != errorcode.Success {
controllers.ApiRes(c, nil, errCode, refund.ThirdMsg)
return
}
data := thirdpay.NewOrdersResp(refund.Order)
var res front.ApiResponse
res.Order = data
controllers.ApiRes(c, res, refund.PayCode)
return
}
// 查询订单
func QueryOrder(c *gin.Context) {
req := controllers.GetRequest(c).(*front.QueryReqs)
c.Set("OutTradeNo", req.OutTradeNo)
appCheckInfo := controllers.GetAppCheckInfo(c).(*services.AppCheck)
// 查询订单表
order := ordersmodel.Orders{
OutTradeNo: req.OutTradeNo,
AppId: req.AppId,
MerchantId: appCheckInfo.App.MerchantId,
}
cond := builder.NewCond()
cond = cond.And(builder.Eq{"out_trade_no": order.OutTradeNo}, builder.Eq{"app_id": order.AppId})
_, code := services.OrderFindOne(&order, cond)
if code != errorcode.Success {
controllers.ApiRes(c, nil, code)
return
}
data := thirdpay.NewOrdersResp(&order)
var res front.ApiResponse
res.Order = data
controllers.ApiRes(c, res, errorcode.Success)
}
// 关闭订单
func CloseOrder(c *gin.Context) {
req := controllers.GetRequest(c).(*front.CloseReqs)
c.Set("OutTradeNo", req.OutTradeNo)
appCheckInfo := controllers.GetAppCheckInfo(c).(*services.AppCheck)
code, message := thirdpay.ThirdCloseOrder(c.Request.Context(), *req, appCheckInfo, c.ClientIP())
var res front.ApiResponse
res.Order = message
controllers.ApiRes(c, res, code)
}