From b19576089cfc44e9ae8f57089150ad2fe09ab668 Mon Sep 17 00:00:00 2001 From: wolter Date: Tue, 6 Aug 2024 17:48:17 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AE=A2=E5=8D=95=E6=9F=A5=E8=AF=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/console/command.go | 14 ++++++++++---- app/constants/common/common.go | 10 ++++++---- app/http/controllers/front/api.go | 24 ++++++++++++++++++++++++ app/http/entities/front/pay.go | 31 +++++++++++++++++++++++++++++++ app/http/routes/route.go | 1 + 5 files changed, 72 insertions(+), 8 deletions(-) diff --git a/app/console/command.go b/app/console/command.go index 86c3659..de6eed2 100644 --- a/app/console/command.go +++ b/app/console/command.go @@ -13,6 +13,7 @@ import ( "PaymentCenter/config" "context" "encoding/json" + "fmt" "github.com/qit-team/snow-core/command" "strconv" "sync" @@ -23,6 +24,7 @@ import ( func RegisterCommand(c *command.Command) { c.AddFunc("test", test) c.AddFunc("closeOrder", closeOrder) + c.AddFunc("queryOrder", queryOrder) } // 关闭长时间支付中的订单 @@ -106,6 +108,7 @@ func queryOrder() { // 拼接条件 cond := builder.NewCond() cond = cond.And(builder.Eq{"status": common.ORDER_STATUS_PAYING}, builder.Gt{"orders.create_time": time.Now().Add(-time.Second * time.Duration(config.GetConf().CronConfig.QueryOrderTime))}) + cond = cond.And(builder.Eq{"order_type": common.ORDER_TYPE_PAY}) order := make([]ordersmodel.OrdersLeftPayChannelList, 0) err := repo.OrdersLeftPayChannelList(cond, entities.PageRequest{}, &order) if err != nil { @@ -170,6 +173,7 @@ func queryOrder() { status = common.ORDER_STATUS_PAYED case "REFUND": // 退款 + status = common.ORDER_STATUS_REFUND case "NOTPAY": // 未支付 @@ -183,8 +187,9 @@ func queryOrder() { // 更新订单状态 todo orderUpdate := ordersmodel.Orders{ - Id: orderInfo.Id, - Status: status, + Id: orderInfo.Id, + Status: status, + PayerTotal: int(result.Result.PayerTotal), } session := ordersmodel.GetInstance().GetDb().NewSession() @@ -216,8 +221,9 @@ func queryOrder() { OrderId: orderInfo.Id, PayCallback: string(body), Status: 0, - PayParam: "", - MerchantCallback: "", + PayParam: fmt.Sprintf(`{"pay_channel_id":%d}`, orderInfo.PayChannelId), + Type: common.THIRD_ORDER_TYPE_ORDER_QUERY, + MerchantCallback: "{}", } _, err = orderLogRepo.OrderThirdPayLogInsertOne(&log) if err != nil { diff --git a/app/constants/common/common.go b/app/constants/common/common.go index a738cc4..f560214 100644 --- a/app/constants/common/common.go +++ b/app/constants/common/common.go @@ -22,19 +22,21 @@ const ( ADMIN_USER_NAME = "User-Name" ADMIN_USER_INCLUDEUSERS = "Include-Users" - // '订单状态,1待支付、2支付中、3支付成功、4支付失败、5订单关闭', + // '订单状态: 1待支付、2支付中、3支付成功、4支付失败、5订单关闭 6支付完成后退款, ORDER_STATUS_WAITPAY = 1 ORDER_STATUS_PAYING = 2 ORDER_STATUS_PAYED = 3 ORDER_STATUS_FAILED = 4 ORDER_STATUS_CLOSE = 5 + ORDER_STATUS_REFUND = 6 + + // 订单类型,1支付,2退款 + ORDER_TYPE_PAY = 1 + ORDER_TYPE_REFUND = 2 STATUS_ENABLE = 1 STATUS_DISABLED = 2 - ORDER_TYPE_PAY = 1 - ORDER_TYPE_REFUND = 2 - PAY_CHANNLE_TYPE_WECHAT = 1 // 支付类型: 微信 PAY_CHANNLE_TYPE_ZFB = 2 // 支付类型:支付宝 diff --git a/app/http/controllers/front/api.go b/app/http/controllers/front/api.go index fd7f2da..006abbb 100644 --- a/app/http/controllers/front/api.go +++ b/app/http/controllers/front/api.go @@ -4,9 +4,11 @@ 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 Pay(c *gin.Context) { @@ -30,3 +32,25 @@ func Pay(c *gin.Context) { controllers.ApiRes(c, thirdpay.PayCallBack(payInfo.Order, true), errorcode.Success) return } + +// 查询订单 +func QueryOrder(c *gin.Context) { + req := controllers.GetRequest(c).(*front.QueryReqs) + appCheckInfo := controllers.GetAppCheckInfo(c).(*services.AppCheck) + // 查询订单表 + order := ordersmodel.Orders{ + OutTreadNo: req.OutTradeNo, + AppId: req.AppId, + MerchantId: appCheckInfo.App.MerchantId, + } + cond := builder.NewCond() + cond = cond.And(builder.Eq{"out_tread_no": order.OutTreadNo}, builder.Eq{"app_id": order.AppId}) + _, code := services.OrderFindOne(&order, cond) + if code != errorcode.OrdersExist { + controllers.ApiRes(c, order, code) + return + } + data := front.OrdersResponse{} + data.ResponseFromDb(order) + controllers.ApiRes(c, data, errorcode.Success) +} diff --git a/app/http/entities/front/pay.go b/app/http/entities/front/pay.go index 413970a..cdaba6c 100644 --- a/app/http/entities/front/pay.go +++ b/app/http/entities/front/pay.go @@ -1,5 +1,7 @@ package front +import "PaymentCenter/app/models/ordersmodel" + type ApiCommonBody struct { AppId int64 `json:"app_id" validate:"required"` Timestamp int64 `json:"timestamp" validate:"required"` @@ -26,3 +28,32 @@ type OrderApiResp struct { Msg string Data interface{} } + +type QueryReqs struct { + ApiCommonBody + OutTradeNo string `json:"out_trade_no" validate:"required" label:"外侧商户订单号"` +} + +type OrdersResponse struct { + Id int64 `json:"id"` + PayChannelId int64 `json:"pay_channel_id"` + AppId int64 `json:"app_id"` + OutTreadNo string `json:"out_tread_no"` + Status int `json:"status"` + OrderType int `json:"order_type"` + Amount int `json:"amount"` + PayerTotal int `json:"payer_total"` + CreateTime string `json:"create_time"` +} + +func (o *OrdersResponse) ResponseFromDb(db ordersmodel.Orders) { + o.Id = db.Id + o.PayChannelId = db.PayChannelId + o.AppId = db.AppId + o.OutTreadNo = db.OutTreadNo + o.Status = db.Status + o.OrderType = db.OrderType + o.Amount = db.Amount + o.PayerTotal = db.PayerTotal + o.CreateTime = db.CreateTime.Format("2006-01-02 15:04:05") +} diff --git a/app/http/routes/route.go b/app/http/routes/route.go index 65deec4..8e684af 100644 --- a/app/http/routes/route.go +++ b/app/http/routes/route.go @@ -59,6 +59,7 @@ func RegisterRoute(router *gin.Engine) { pay := v1.Group("/pay", middlewares.ValidateRequest(), middlewares.ValidatePayRequest()) { pay.POST("/do", front.Pay) + pay.POST("/query", front.QueryOrder) //查询订单 } }