From 6357d6706dcb931b6dbb82fcbb3a3500222f963b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E4=BF=8A=E5=AE=8F?= <389838709@qq.com> Date: Thu, 8 Aug 2024 17:25:42 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AE=B0=E5=BD=95third=E8=A1=A8=E7=9A=84?= =?UTF-8?q?=E6=97=A5=E5=BF=97=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/constants/common/common.go | 4 + app/third/paymentService/ali_service.go | 3 +- app/third/paymentService/payment_service.go | 112 ++++++++++++-------- app/third/paymentService/wechat_service.go | 3 +- 4 files changed, 78 insertions(+), 44 deletions(-) diff --git a/app/constants/common/common.go b/app/constants/common/common.go index a3b7ca1..3d2e07c 100644 --- a/app/constants/common/common.go +++ b/app/constants/common/common.go @@ -45,6 +45,10 @@ const ( THIRD_ORDER_TYPE_REFUND_QUERY = 4 // 退款查询 THIRD_ORDER_TYPE_CLOSE = 5 // 关闭订单 THIRD_ORDER_TYPE_CALL_BACK = 6 // 支付回调 + + THIRD_ORDER_LOG_STATUS_COMMON = 0 // 第三方日志状态 未知 + THIRD_ORDER_LOG_STATUS_FAIL = 1 // 第三方日志状态 失败 + THIRD_ORDER_LOG_STATUS_SUCCESS = 2 // 第三方日志状态 成功 ) var PayChannelList = map[int]string{ diff --git a/app/third/paymentService/ali_service.go b/app/third/paymentService/ali_service.go index d0ed0b0..48a74ad 100644 --- a/app/third/paymentService/ali_service.go +++ b/app/third/paymentService/ali_service.go @@ -132,7 +132,8 @@ func ALiCallBack(notifyReq gopay.BodyMap, aliConfig AliPay) error { go func() { payCallback, _ := json.Marshal(notifyReq) payParam := "{}" - SaveLog(int64(orderId), common.THIRD_ORDER_TYPE_CALL_BACK, string(payCallback), payParam, string(merchantCallback)) + status := common.THIRD_ORDER_LOG_STATUS_SUCCESS + SaveLog(int64(orderId), common.THIRD_ORDER_TYPE_CALL_BACK, string(payCallback), payParam, "{}", status) }() if res.ErrCode != errorcode.Success { diff --git a/app/third/paymentService/payment_service.go b/app/third/paymentService/payment_service.go index ba850b1..dd297f4 100644 --- a/app/third/paymentService/payment_service.go +++ b/app/third/paymentService/payment_service.go @@ -59,6 +59,7 @@ func PaymentService(c context.Context, payOrderRequest PayOrderRequest) PayOrder logger.Info(c, "PaymentService 收到支付请求", payOrderRequest) var err error var info string + var payOrderResponse PayOrderResponse switch payOrderRequest.ChannelType { case payCommon.PAY_CHANNEL_WECHAT_H5: // 微信H5支付 @@ -67,29 +68,35 @@ func PaymentService(c context.Context, payOrderRequest PayOrderRequest) PayOrder // 支付宝H5支付 info, err = ALiH5PayInfo(c, payOrderRequest) default: - return PayOrderResponse{ + payOrderResponse = PayOrderResponse{ Code: payCommon.PAY_NOT_FOUND_CODE, ErrorMsg: "暂不支持该支付渠道,请后续再使用", } } + + logStatus := common.THIRD_ORDER_LOG_STATUS_FAIL if err != nil { - return PayOrderResponse{ + payOrderResponse = PayOrderResponse{ Code: payCommon.PAY_ERROR_CODE, ErrorMsg: err.Error(), } + logStatus = common.THIRD_ORDER_LOG_STATUS_FAIL + } else { + payOrderResponse = PayOrderResponse{ + Code: payCommon.PAY_SUCCESS_CODE, + ErrorMsg: "", + Result: info, + } + logStatus = common.THIRD_ORDER_LOG_STATUS_SUCCESS } - payOrderResponse := PayOrderResponse{ - Code: payCommon.PAY_SUCCESS_CODE, - ErrorMsg: "", - Result: info, - } + // 记录日志 go func() { orderId := payOrderRequest.OrderId payCallback := fmt.Sprintf("{%s}", info) payParam, _ := json.Marshal(payOrderRequest) merchantCallback, _ := json.Marshal(payOrderResponse) - SaveLog(orderId, common.THIRD_ORDER_TYPE_PAY, payCallback, string(payParam), string(merchantCallback)) + SaveLog(orderId, common.THIRD_ORDER_TYPE_PAY, payCallback, string(payParam), string(merchantCallback), logStatus) }() return payOrderResponse } @@ -122,6 +129,7 @@ type PayOrderQueryInfo struct { func PayOrderQuery(c context.Context, payOrderQueryRequest PayOrderQueryRequest) PayOrderQueryResponse { var err error var info PayOrderQueryInfo + var payOrderQueryResponse PayOrderQueryResponse switch payOrderQueryRequest.PayChannel { case payCommon.PAY_CHANNLE_TYPE_WECHAT: // 微信H5支付 @@ -129,21 +137,25 @@ func PayOrderQuery(c context.Context, payOrderQueryRequest PayOrderQueryRequest) case payCommon.PAY_CHANNLE_TYPE_ZFB: info, err = ALiOrderQuery(c, payOrderQueryRequest.Ali, strconv.FormatInt(payOrderQueryRequest.OrderId, 10)) default: - return PayOrderQueryResponse{ + payOrderQueryResponse = PayOrderQueryResponse{ Code: payCommon.PAY_ERROR_CODE, ErrorMsg: "暂不支持该支付渠道,请后续再使用", } } + + logStatus := common.THIRD_ORDER_LOG_STATUS_FAIL if err != nil { - return PayOrderQueryResponse{ + payOrderQueryResponse = PayOrderQueryResponse{ Code: payCommon.PAY_ERROR_CODE, ErrorMsg: err.Error(), } - } - - payOrderQueryResponse := PayOrderQueryResponse{ - Code: payCommon.PAY_SUCCESS_CODE, - Result: info, + logStatus = common.THIRD_ORDER_LOG_STATUS_FAIL + } else { + payOrderQueryResponse = PayOrderQueryResponse{ + Code: payCommon.PAY_SUCCESS_CODE, + Result: info, + } + logStatus = common.THIRD_ORDER_LOG_STATUS_SUCCESS } // 记录日志 @@ -152,7 +164,7 @@ func PayOrderQuery(c context.Context, payOrderQueryRequest PayOrderQueryRequest) payCallback, _ := json.Marshal(info) payParam, _ := json.Marshal(payOrderQueryRequest) merchantCallback, _ := json.Marshal(payOrderQueryResponse) - SaveLog(orderId, common.THIRD_ORDER_TYPE_ORDER_QUERY, string(payCallback), string(payParam), string(merchantCallback)) + SaveLog(orderId, common.THIRD_ORDER_TYPE_ORDER_QUERY, string(payCallback), string(payParam), string(merchantCallback), logStatus) }() return payOrderQueryResponse } @@ -187,6 +199,7 @@ func OrderRefund(c context.Context, orderRefundRequest OrderRefundRequest) Order logger.Info(c, "PaymentService 收到退款请求", orderRefundRequest) var err error var info OrderRefundInfo + var orderRefundResponse OrderRefundResponse switch orderRefundRequest.PayChannel { case payCommon.PAY_CHANNLE_TYPE_WECHAT: // 微信H5支付 @@ -194,31 +207,35 @@ func OrderRefund(c context.Context, orderRefundRequest OrderRefundRequest) Order case payCommon.PAY_CHANNLE_TYPE_ZFB: info, err = AliRefundOrder(c, orderRefundRequest) default: - return OrderRefundResponse{ + orderRefundResponse = OrderRefundResponse{ Code: payCommon.PAY_ERROR_CODE, ErrorMsg: "暂不支持该支付渠道,请后续再使用", } } + + logStatus := common.THIRD_ORDER_LOG_STATUS_FAIL if err != nil { - return OrderRefundResponse{ + orderRefundResponse = OrderRefundResponse{ Code: payCommon.PAY_ERROR_CODE, ErrorMsg: err.Error(), } + logStatus = common.THIRD_ORDER_LOG_STATUS_FAIL + } else { + orderRefundResponse = OrderRefundResponse{ + Code: payCommon.PAY_SUCCESS_CODE, + Result: info, + } + logStatus = common.THIRD_ORDER_LOG_STATUS_SUCCESS } - orderRefundResponse := OrderRefundResponse{ - Code: payCommon.PAY_SUCCESS_CODE, - Result: info, - } // 记录日志 go func() { orderId := orderRefundRequest.OrderId payCallback, _ := json.Marshal(info) payParam, _ := json.Marshal(orderRefundRequest) merchantCallback, _ := json.Marshal(orderRefundResponse) - SaveLog(orderId, common.THIRD_ORDER_TYPE_REFUND, string(payCallback), string(payParam), string(merchantCallback)) + SaveLog(orderId, common.THIRD_ORDER_TYPE_REFUND, string(payCallback), string(payParam), string(merchantCallback), logStatus) }() - return orderRefundResponse } @@ -239,6 +256,7 @@ type OrderRefundQueryResponse struct { func OrderRefundQuery(c context.Context, orderRefundQueryRequest OrderRefundQueryRequest) OrderRefundQueryResponse { var err error var info OrderRefundInfo + var orderRefundQueryResponse OrderRefundQueryResponse switch orderRefundQueryRequest.PayChannel { case payCommon.PAY_CHANNLE_TYPE_WECHAT: // 微信H5支付 @@ -246,20 +264,25 @@ func OrderRefundQuery(c context.Context, orderRefundQueryRequest OrderRefundQuer case payCommon.PAY_CHANNLE_TYPE_ZFB: info, err = AliRefundOrderQuery(c, orderRefundQueryRequest) default: - return OrderRefundQueryResponse{ + orderRefundQueryResponse = OrderRefundQueryResponse{ Code: payCommon.PAY_ERROR_CODE, ErrorMsg: "暂不支持该支付渠道,请后续再使用", } } + + logStatus := common.THIRD_ORDER_LOG_STATUS_FAIL if err != nil { - return OrderRefundQueryResponse{ + orderRefundQueryResponse = OrderRefundQueryResponse{ Code: payCommon.PAY_ERROR_CODE, ErrorMsg: err.Error(), } - } - orderRefundQueryResponse := OrderRefundQueryResponse{ - Code: payCommon.PAY_SUCCESS_CODE, - Result: info, + logStatus = common.THIRD_ORDER_LOG_STATUS_FAIL + } else { + orderRefundQueryResponse = OrderRefundQueryResponse{ + Code: payCommon.PAY_SUCCESS_CODE, + Result: info, + } + logStatus = common.THIRD_ORDER_LOG_STATUS_SUCCESS } // 记录日志 @@ -268,9 +291,8 @@ func OrderRefundQuery(c context.Context, orderRefundQueryRequest OrderRefundQuer payCallback, _ := json.Marshal(info) payParam, _ := json.Marshal(orderRefundQueryRequest) merchantCallback, _ := json.Marshal(orderRefundQueryResponse) - SaveLog(orderId, common.THIRD_ORDER_TYPE_REFUND_QUERY, string(payCallback), string(payParam), string(merchantCallback)) + SaveLog(orderId, common.THIRD_ORDER_TYPE_REFUND_QUERY, string(payCallback), string(payParam), string(merchantCallback), logStatus) }() - return orderRefundQueryResponse } @@ -296,6 +318,7 @@ func OrderClose(c context.Context, orderCloseRequest OrderCloseRequest) OrderClo logger.Info(c, "PaymentService 收到关闭订单请求", orderCloseRequest) var err error var info OrderCloseInfo + var orderCloseResponse OrderCloseResponse switch orderCloseRequest.PayChannel { case payCommon.PAY_CHANNLE_TYPE_WECHAT: // 微信H5支付 @@ -303,40 +326,45 @@ func OrderClose(c context.Context, orderCloseRequest OrderCloseRequest) OrderClo case payCommon.PAY_CHANNLE_TYPE_ZFB: info, err = AliCloseOrder(c, orderCloseRequest) default: - return OrderCloseResponse{ + orderCloseResponse = OrderCloseResponse{ Code: payCommon.PAY_ERROR_CODE, ErrorMsg: "暂不支持该支付渠道,请后续再使用", } } + + logStatus := common.THIRD_ORDER_LOG_STATUS_FAIL if err != nil { - return OrderCloseResponse{ + orderCloseResponse = OrderCloseResponse{ Code: payCommon.PAY_ERROR_CODE, ErrorMsg: err.Error(), } + logStatus = common.THIRD_ORDER_LOG_STATUS_FAIL + } else { + orderCloseResponse = OrderCloseResponse{ + Code: payCommon.PAY_SUCCESS_CODE, + Result: info, + } + logStatus = common.THIRD_ORDER_LOG_STATUS_SUCCESS } - orderCloseResponse := OrderCloseResponse{ - Code: payCommon.PAY_SUCCESS_CODE, - Result: info, - } + // 记录日志 go func() { orderId := orderCloseRequest.OrderId payCallback, _ := json.Marshal(info) payParam, _ := json.Marshal(orderCloseRequest) merchantCallback, _ := json.Marshal(orderCloseResponse) - SaveLog(orderId, common.THIRD_ORDER_TYPE_CLOSE, string(payCallback), string(payParam), string(merchantCallback)) + SaveLog(orderId, common.THIRD_ORDER_TYPE_CLOSE, string(payCallback), string(payParam), string(merchantCallback), logStatus) }() - return orderCloseResponse } // SaveLog 记录操作日志 -func SaveLog(orderId int64, OType int, payCallback string, PayParam string, MerchantCallback string) { +func SaveLog(orderId int64, OType int, payCallback string, PayParam string, MerchantCallback string, status int) { thirdRepo := data.NewOrderThirdPayLogRepo(paychannelmodel.GetInstance().GetDb()) log := orderthirdpaylogmodel.OrderThirdPayLog{ OrderId: orderId, PayCallback: payCallback, - Status: 0, + Status: status, PayParam: PayParam, MerchantCallback: MerchantCallback, Type: OType, diff --git a/app/third/paymentService/wechat_service.go b/app/third/paymentService/wechat_service.go index 6da53c2..8cb27d0 100644 --- a/app/third/paymentService/wechat_service.go +++ b/app/third/paymentService/wechat_service.go @@ -166,7 +166,8 @@ func WxPayCallBack(notifyReq *wechat.V3NotifyReq, wxConfig WxPay) error { go func() { payCallback, _ := json.Marshal(CallBackInfo) payParam := "{}" - SaveLog(int64(orderId), common.THIRD_ORDER_TYPE_CALL_BACK, string(payCallback), payParam, string(merchantCallback)) + status := common.THIRD_ORDER_LOG_STATUS_SUCCESS + SaveLog(int64(orderId), common.THIRD_ORDER_TYPE_CALL_BACK, string(payCallback), payParam, "{}", status) }() if res.ErrCode != errorcode.Success {