From 7a45794fbc9d988617f0014937b5b4be7d4aacd6 Mon Sep 17 00:00:00 2001 From: Rzy <465386466@qq.com> Date: Mon, 12 Aug 2024 14:09:11 +0800 Subject: [PATCH] =?UTF-8?q?=E9=80=80=E6=AC=BE=E5=8D=95=E5=88=A4?= =?UTF-8?q?=E9=87=8D=E5=92=8C=E7=AC=AC=E4=B8=89=E6=96=B9=E6=B6=88=E6=81=AF?= =?UTF-8?q?=E5=A4=84=E7=90=86=E8=BF=94=E5=9B=9E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/constants/errorcode/error_code.go | 10 +++++++ app/http/controllers/api.go | 5 ++-- app/http/controllers/front/api.go | 4 +-- app/http/middlewares/base.go | 1 - app/services/order.go | 2 +- app/services/thirdpay/do/pay.go | 22 +++++++++++++-- app/services/thirdpay/do/pay_check.go | 39 ++++++++++++++++++--------- app/services/thirdpay/pay.go | 22 ++++++++++----- 8 files changed, 77 insertions(+), 28 deletions(-) diff --git a/app/constants/errorcode/error_code.go b/app/constants/errorcode/error_code.go index 96da1d0..2f7170e 100644 --- a/app/constants/errorcode/error_code.go +++ b/app/constants/errorcode/error_code.go @@ -70,6 +70,9 @@ const ( OrderPayed = 1409 RefundOrderNotFound = 1410 OrderStatusRefundNotSupport = 1411 + OrderAppidNotEqRefundAppid = 1412 + OrderNotSupportRefundPart = 1413 + OrderRefundAmountError = 1414 //请求日志 RequestLogErrors = 1500 @@ -82,6 +85,8 @@ const ( //订单结果 PrePayFail = 1701 PreRefundFail = 1702 + + ThirdRefundFail = 1710 ) var MsgEN = map[int]string{ @@ -150,8 +155,13 @@ var MsgZH = map[int]string{ NotifySendFail: "回调发送失败", RefundOrderNotFound: "退款订单未找到", OrderStatusRefundNotSupport: "订单状态不支持退款", + OrderAppidNotEqRefundAppid: "订单app_id与退款订单app_id不一致", PrePayFail: "预支付失败", PreRefundFail: "创建退款单失败", + OrderNotSupportRefundPart: "订单暂不支持部分退款", + OrderRefundAmountError: "退款金额错误", + + ThirdRefundFail: "第三方退款失败", } var MsgMap map[string]map[int]string = map[string]map[int]string{"en": MsgZH} diff --git a/app/http/controllers/api.go b/app/http/controllers/api.go index 8aa0ac2..7ce5732 100644 --- a/app/http/controllers/api.go +++ b/app/http/controllers/api.go @@ -31,10 +31,9 @@ func ApiRes(c *gin.Context, data interface{}, code int, msg ...string) { data = struct{}{} } // 获取错误信息 + message = errorcode.GetMsg(code, "") if len(msg) > 0 { - message = msg[0] - } else { - message = errorcode.GetMsg(code, "") + message = fmt.Sprintf("%s:%s", message, msg[0]) } // 获取日志ID log, exists := GetApiLogId(c) diff --git a/app/http/controllers/front/api.go b/app/http/controllers/front/api.go index 5be2abc..6f9fafb 100644 --- a/app/http/controllers/front/api.go +++ b/app/http/controllers/front/api.go @@ -29,7 +29,7 @@ func PayUrl(c *gin.Context) { } pay := thirdpay.ThirdPayUrl(check) if pay.PayCode != errorcode.Success { - controllers.ApiRes(c, nil, pay.PayCode) + controllers.ApiRes(c, nil, pay.PayCode, pay.ThirdMsg) return } @@ -46,7 +46,7 @@ func Refund(c *gin.Context) { appCheckInfo := controllers.GetAppCheckInfo(c).(*services.AppCheck) refund, errCode := thirdpay.ThirdPayRefund(c.Request.Context(), req, appCheckInfo, c.ClientIP()) if errCode != errorcode.Success { - controllers.ApiRes(c, nil, errCode) + controllers.ApiRes(c, nil, errCode, refund.ThirdMsg) return } data := thirdpay.NewOrdersResp(refund.Order) diff --git a/app/http/middlewares/base.go b/app/http/middlewares/base.go index ab5dcda..b4c9eed 100644 --- a/app/http/middlewares/base.go +++ b/app/http/middlewares/base.go @@ -167,7 +167,6 @@ func ValidatePayRequest() gin.HandlerFunc { c.Set("appCheckInfo", appCheck) //解密 if appCheck.App.KeyType != pojo.NO_CRYPT { - postBody, errCode = api.DeCrypt(appCheck.App, requestData.Data, requestData.Key) if errCode != errorcode.Success { controllers.ApiRes(c, nil, errCode) diff --git a/app/services/order.go b/app/services/order.go index 777bbb2..af61fe3 100644 --- a/app/services/order.go +++ b/app/services/order.go @@ -129,7 +129,7 @@ func OrderUpdate(order *ordersmodel.Orders, col ...string) (code int) { return } -func OrderFindOne(order *ordersmodel.Orders, conn builder.Cond, col ...string) (merchantInfo *ordersmodel.Orders, code int) { +func OrderFindOne(order *ordersmodel.Orders, conn builder.Cond, col ...string) (orderInfo *ordersmodel.Orders, code int) { repo := data.NewOrderRepo(ordersmodel.GetInstance().GetDb()) // 拼接查询条件 orderInfo, err := repo.OrderFindOne(order, conn, col...) diff --git a/app/services/thirdpay/do/pay.go b/app/services/thirdpay/do/pay.go index 44e9e4a..308112e 100644 --- a/app/services/thirdpay/do/pay.go +++ b/app/services/thirdpay/do/pay.go @@ -8,6 +8,7 @@ import ( "PaymentCenter/app/services" "PaymentCenter/app/third/paymentService/payCommon" "context" + "xorm.io/builder" "PaymentCenter/app/models/ordersmodel" "PaymentCenter/app/third/paymentService" @@ -31,6 +32,7 @@ type Pay struct { Order *ordersmodel.Orders PayCode int Url string + ThirdMsg string } func NewPayWithPayCheck(paycheck *PayCheck) *Pay { @@ -51,11 +53,26 @@ func NewPayWithPayCheck(paycheck *PayCheck) *Pay { } } +func (w *Pay) CheckRefundOrder() bool { + cond := builder.NewCond() + cond = cond.And(builder.Eq{"app_id": w.PayParam.App_id}, builder.Eq{"out_trade_no": w.PayParam.OutTradeNo}) + order, code := services.OrderFindOne(&ordersmodel.Orders{}, cond) + if code != errorcode.Success { + return true + } + if order.Status == common.ORDER_STATUS_FAILED { + return true + } + w.Order = order + return false +} + func (w *Pay) CreateOrder(order_type int) { if _, ok := common.OrderTypeList[order_type]; !ok { //判断是否是支持的支付渠道 w.PayCode = errorcode.PayChannelNotFound return } + order := &ordersmodel.Orders{ MerchantId: w.PayParam.Merchant.Id, PayChannelId: w.PayParam.Channel.Id, @@ -106,6 +123,7 @@ func (w *Pay) PayUrl() (url string) { w.Url = res.Result } else { w.PayCode = errorcode.PrePayFail + w.ThirdMsg = res.ErrorMsg } return @@ -140,7 +158,7 @@ func (w *Pay) Refund() { return } } else { - w.PayCode = errorcode.PreRefundFail + w.PayCode = errorcode.ThirdRefundFail + w.ThirdMsg = res.ErrorMsg } - return } diff --git a/app/services/thirdpay/do/pay_check.go b/app/services/thirdpay/do/pay_check.go index 7640c22..6f5142e 100644 --- a/app/services/thirdpay/do/pay_check.go +++ b/app/services/thirdpay/do/pay_check.go @@ -61,7 +61,7 @@ func (w *PayCheck) CheckMerchant() { } func (w *PayCheck) CheckOrderPay() { - w.GetOrder(&types.OrderFindOne{ + w.GetOldOrder(&types.OrderFindOne{ OutTradeNo: w.Reqs.OutTradeNo, }) if w.OldOrder != nil { @@ -79,8 +79,8 @@ func (w *PayCheck) CheckOrderPay() { return } -func (w *PayCheck) CheckOrderRefund() { - w.GetOrder(&types.OrderFindOne{ +func (w *PayCheck) CheckOrderRefund() (order *ordersmodel.Orders) { + w.GetOldOrder(&types.OrderFindOne{ OutTradeNo: w.Reqs.RefundOutTradeNo, OrderId: w.Reqs.RefundOrderId, }) @@ -91,10 +91,31 @@ func (w *PayCheck) CheckOrderRefund() { if w.OldOrder.Status != common.ORDER_STATUS_PAYED { w.CheckCode = errorcode.OrderStatusRefundNotSupport } + + if w.OldOrder.Amount < w.Reqs.Amount { + w.CheckCode = errorcode.OrderRefundAmountError + } + //暂不支持部分退款 + if w.OldOrder.Amount > w.Reqs.Amount { + w.CheckCode = errorcode.OrderNotSupportRefundPart + } + return } -func (w *PayCheck) GetOrder(orderCol *types.OrderFindOne) { +func (w *PayCheck) GetOldOrder(orderCol *types.OrderFindOne) { + order, code := w.GetOrder(orderCol) + if code == errorcode.SystemError { + w.CheckCode = code + return + } + if code == errorcode.Success { + w.OldOrder = order + } + return +} + +func (w *PayCheck) GetOrder(orderCol *types.OrderFindOne) (order *ordersmodel.Orders, code int) { cond := builder.NewCond() cond = cond.And(builder.Eq{"app_id": w.AppCheck.AppId}) if orderCol.OrderId == "" && orderCol.OutTradeNo == "" { @@ -107,13 +128,7 @@ func (w *PayCheck) GetOrder(orderCol *types.OrderFindOne) { if orderCol.OutTradeNo != "" { cond = cond.And(builder.Eq{"out_trade_no": orderCol.OutTradeNo}) } - order, code := services.OrderFindOne(&ordersmodel.Orders{}, cond) - if code == errorcode.SystemError { - w.CheckCode = code - return - } - if code == errorcode.Success { - w.OldOrder = order - } + order, code = services.OrderFindOne(&ordersmodel.Orders{}, cond) + return } diff --git a/app/services/thirdpay/pay.go b/app/services/thirdpay/pay.go index 01acc83..1de7650 100644 --- a/app/services/thirdpay/pay.go +++ b/app/services/thirdpay/pay.go @@ -59,13 +59,8 @@ func ThirdPayRefund(ctx context.Context, refundReq *front.RefundReqs, appCheck * if check.CheckCode != errorcode.Success { return nil, check.CheckCode } - refund = thirdpay.NewPayWithPayCheck(check) - refund.CreateOrder(common.ORDER_TYPE_REFUND) - if refund.PayCode != errorcode.Success { - return refund, refund.PayCode - } - refund.Refund() - return refund, errorcode.Success + refund, errorCode = ThirdRefund(check) + return } func ThirdPay(check *thirdpay.PayCheck) (pay *thirdpay.Pay) { @@ -81,3 +76,16 @@ func ThirdPay(check *thirdpay.PayCheck) (pay *thirdpay.Pay) { } return } + +func ThirdRefund(check *thirdpay.PayCheck) (refund *thirdpay.Pay, errCode int) { + refund = thirdpay.NewPayWithPayCheck(check) + if refund.CheckRefundOrder() { + refund.CreateOrder(common.ORDER_TYPE_REFUND) + if refund.PayCode != errorcode.Success { + return refund, refund.PayCode + } + } + + refund.Refund() + return refund, refund.PayCode +}