From cb14c35aa912facd0676d0fa9055e65c98d7e72c Mon Sep 17 00:00:00 2001 From: Rzy <465386466@qq.com> Date: Thu, 8 Aug 2024 10:48:25 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/console/command.go | 6 +-- app/http/controllers/front/api.go | 6 +-- app/http/entities/front/pay.go | 1 - app/services/thirdpay/api/api_res_data.go | 16 ------- app/services/thirdpay/do/pay.go | 8 +++- app/services/thirdpay/do/pay_check.go | 8 ++-- app/services/thirdpay/notify/notify.go | 55 +++++++++------------- app/services/thirdpay/pay.go | 52 +++++++++++++++++++- app/third/paymentService/ali_service.go | 4 +- app/third/paymentService/wechat_service.go | 4 +- 10 files changed, 92 insertions(+), 68 deletions(-) diff --git a/app/console/command.go b/app/console/command.go index 47025ab..462f41a 100644 --- a/app/console/command.go +++ b/app/console/command.go @@ -6,7 +6,7 @@ import ( "PaymentCenter/app/http/entities" "PaymentCenter/app/http/entities/backend" "PaymentCenter/app/models/ordersmodel" - "PaymentCenter/app/services/thirdpay/notify" + "PaymentCenter/app/services/thirdpay" "PaymentCenter/app/third/paymentService" "PaymentCenter/app/third/paymentService/payCommon" "PaymentCenter/app/utils" @@ -181,7 +181,7 @@ func queryOrder() { status = common.ORDER_STATUS_CLOSE } // 回调通知下游 todo - notifyResult := notify.NewOrderNotifyWithHandle(orderInfo.Id, status, int(result.Result.PayerTotal), "") + notifyResult := thirdpay.NewOrderNotifyWithHandle(orderInfo.Id, status, int(result.Result.PayerTotal), "") utils.Log(nil, "主动查询订单支付状态,回调下游", notifyResult) } }(orderInfo) @@ -278,7 +278,7 @@ func queryRefundOrder() { status = common.ORDER_STATUS_FAILED } // 回调通知下游 todo - notifyResult := notify.NewOrderNotifyWithHandle(orderInfo.Id, status, int(result.Result.RefundFee), "") + notifyResult := thirdpay.NewOrderNotifyWithHandle(orderInfo.Id, status, int(result.Result.RefundFee), "") utils.Log(nil, "主动查询退款订单状态,回调下游", notifyResult) } }(orderInfo) diff --git a/app/http/controllers/front/api.go b/app/http/controllers/front/api.go index 89c09b1..f60f770 100644 --- a/app/http/controllers/front/api.go +++ b/app/http/controllers/front/api.go @@ -26,12 +26,12 @@ func PayUrl(c *gin.Context) { controllers.ApiRes(c, nil, check.CheckCode) return } - pay := thirdpay.ThirdPayWeb(check) + pay := thirdpay.ThirdPayUrl(check) if pay.PayCode != errorcode.Success { controllers.ApiRes(c, nil, pay.PayCode) return } - data := api.NewOrdersResp(pay.Order) + data := thirdpay.NewOrdersResp(pay.Order) encryptData, errCode := api.EnCrypt(appCheckInfo.App, data) if errCode != errorcode.Success { @@ -62,7 +62,7 @@ func QueryOrder(c *gin.Context) { controllers.ApiRes(c, nil, code) return } - data := api.NewOrdersResp(&order) + data := thirdpay.NewOrdersResp(&order) encryptData, errCode := api.EnCrypt(appCheckInfo.App, data) if errCode != errorcode.Success { controllers.ApiRes(c, nil, errCode) diff --git a/app/http/entities/front/pay.go b/app/http/entities/front/pay.go index 4413d58..d73e193 100644 --- a/app/http/entities/front/pay.go +++ b/app/http/entities/front/pay.go @@ -16,7 +16,6 @@ type PayReqs struct { ApiCommonBody PayChannelId int64 `json:"pay_channel_id" validate:"required" label:"支付渠道"` OutTradeNo string `json:"out_trade_no" validate:"required" label:"外侧商户订单号"` - OrderType int `json:"order_type" validate:"required" label:"订单类型,支付,退款"` Amount int `json:"amount" validate:"required" label:"支付金额,单位分"` ExtJson string `json:"ext_json" label:"扩展参数"` Desc string `json:"desc" validate:"max=100" label:"商品描述"` diff --git a/app/services/thirdpay/api/api_res_data.go b/app/services/thirdpay/api/api_res_data.go index cf09148..d592269 100644 --- a/app/services/thirdpay/api/api_res_data.go +++ b/app/services/thirdpay/api/api_res_data.go @@ -1,9 +1,5 @@ package api -import ( - "PaymentCenter/app/models/ordersmodel" -) - type OrdersResp struct { OrderNo int64 `json:"order_no"` OrderType int `json:"order_type"` @@ -20,18 +16,6 @@ type OrdersWithUrl struct { Url string } -func NewOrdersResp(db *ordersmodel.Orders) *OrdersResp { - return &OrdersResp{ - OrderNo: db.Id, - OutTreadNo: db.OutTreadNo, - Status: db.Status, - OrderType: db.OrderType, - Amount: db.Amount, - Desc: db.Desc, - CreateTime: db.CreateTime.Format("2006-01-02 15:04:05"), - } -} - func (o *OrdersResp) WithUrl(url string) *OrdersWithUrl { return &OrdersWithUrl{ Order: o, diff --git a/app/services/thirdpay/do/pay.go b/app/services/thirdpay/do/pay.go index 72714d8..25f5f7e 100644 --- a/app/services/thirdpay/do/pay.go +++ b/app/services/thirdpay/do/pay.go @@ -25,13 +25,17 @@ func NewPay(paycheck *PayCheck) *Pay { } } -func (w *Pay) CreateOrder() { +func (w *Pay) CreateOrder(order_type int) { + if _, ok := common.OrderTypeList[order_type]; !ok { //判断是否是支持的支付渠道 + w.PayCode = errorcode.PayChannelNotFound + return + } w.Order, w.PayCode = services.OrderCreate(&ordersmodel.Orders{ MerchantId: w.paycheck.Merchant.Id, PayChannelId: w.paycheck.Channel.Id, AppId: w.paycheck.Channel.Id, OutTreadNo: w.paycheck.WebPayReqs.OutTradeNo, - OrderType: w.paycheck.WebPayReqs.OrderType, + OrderType: order_type, Amount: w.paycheck.WebPayReqs.Amount, ExtJson: w.paycheck.WebPayReqs.ExtJson, Desc: w.paycheck.WebPayReqs.Desc, diff --git a/app/services/thirdpay/do/pay_check.go b/app/services/thirdpay/do/pay_check.go index d690c07..daa9a57 100644 --- a/app/services/thirdpay/do/pay_check.go +++ b/app/services/thirdpay/do/pay_check.go @@ -38,10 +38,10 @@ func NewPayCheck(ctx *context.Context, reqs *front.PayReqs, appCheck *services.A } func (w *PayCheck) CheckForm() { - if _, ok := common.OrderTypeList[w.WebPayReqs.OrderType]; !ok { //判断是否是支持的支付渠道 - w.CheckCode = errorcode.PayChannelNotFound - return - } + //if _, ok := common.OrderTypeList[w.WebPayReqs.OrderType]; !ok { //判断是否是支持的支付渠道 + // w.CheckCode = errorcode.PayChannelNotFound + // return + //} } diff --git a/app/services/thirdpay/notify/notify.go b/app/services/thirdpay/notify/notify.go index 2a2b949..462e5a4 100644 --- a/app/services/thirdpay/notify/notify.go +++ b/app/services/thirdpay/notify/notify.go @@ -15,7 +15,7 @@ import ( type OrderNotify struct { order *ordersmodel.Orders - code int + Code int app *appmodel.App OrderId int64 @@ -46,17 +46,6 @@ type OrderNotifySendContent struct { MerchantId int64 `json:"merchant_id"` } -func NewOrderNotifyWithHandle(orderId int64, Status int, actualAmount int, msg string) *OrderNotifyResp { - orderNotify := &OrderNotify{ - OrderId: orderId, - Status: Status, - ActualAmount: actualAmount, - Msg: msg, - code: errorcode.Success, - } - return orderNotify.handle() -} - func (o *OrderNotify) NotifyRespFail(errorCode int) *OrderNotifyResp { return &OrderNotifyResp{ OrderId: o.OrderId, @@ -66,27 +55,27 @@ func (o *OrderNotify) NotifyRespFail(errorCode int) *OrderNotifyResp { } } -func (o *OrderNotify) handle() (res *OrderNotifyResp) { +func (o *OrderNotify) Handle() (res *OrderNotifyResp) { o.checkOrder() - if o.code != errorcode.Success { - return o.NotifyRespFail(o.code) + if o.Code != errorcode.Success { + return o.NotifyRespFail(o.Code) } o.checkApp() - if o.code != errorcode.Success { - return o.NotifyRespFail(o.code) + if o.Code != errorcode.Success { + return o.NotifyRespFail(o.Code) } o.updateOrder() - if o.code != errorcode.Success { - return o.NotifyRespFail(o.code) + if o.Code != errorcode.Success { + return o.NotifyRespFail(o.Code) } o.sendNotify(o.setBody()) - if o.code != errorcode.Success { - return o.NotifyRespFail(o.code) + if o.Code != errorcode.Success { + return o.NotifyRespFail(o.Code) } return &OrderNotifyResp{ OrderId: o.OrderId, Send: true, - ErrCode: o.code, + ErrCode: o.Code, Content: o, } } @@ -98,7 +87,7 @@ func (o *OrderNotify) sendNotify(body *OrderNotifySendContent) { headers["Content-Type"] = "application/json" resByte, err := httpclient.FastHttpPost(o.app.NotifyUrl, headers, bodyByte, 0) if err != nil || string(resByte) != "success" { - o.code = errorcode.NotifySendFail + o.Code = errorcode.NotifySendFail } return } @@ -120,25 +109,25 @@ func (o *OrderNotify) setBody() *OrderNotifySendContent { func (o *OrderNotify) updateOrder() { if _, ok := common.OrderStatusMap[o.Status]; !ok { - o.code = errorcode.OrderStatusErr + o.Code = errorcode.OrderStatusErr return } o.order.Status = o.Status - o.code = services.OrderUpdate(o.order, "status") + o.Code = services.OrderUpdate(o.order, "status") return } func (o *OrderNotify) checkApp() { - o.app, o.code = services.AppFindOne(entities.IdRequest{Id: o.order.AppId}) - if o.code != errorcode.Success { + o.app, o.Code = services.AppFindOne(entities.IdRequest{Id: o.order.AppId}) + if o.Code != errorcode.Success { return } if o.app.DeleteTime.IsZero() { - o.code = errorcode.AppDisabled + o.Code = errorcode.AppDisabled return } if o.app.NotifyUrl == "" { - o.code = errorcode.AppNotifyUrlNotFound + o.Code = errorcode.AppNotifyUrlNotFound return } @@ -149,16 +138,16 @@ func (o *OrderNotify) checkOrder() { cond := builder.NewCond() cond = cond.And(builder.Eq{"id": o.OrderId}) - o.order, o.code = services.OrderFindOne(&ordersmodel.Orders{}, cond) - if o.code != errorcode.Success { + o.order, o.Code = services.OrderFindOne(&ordersmodel.Orders{}, cond) + if o.Code != errorcode.Success { return } if o.order.DeleteTime.IsZero() { - o.code = errorcode.OrderIsDelete + o.Code = errorcode.OrderIsDelete return } if o.order.Status != common.ORDER_STATUS_PAYING { - o.code = errorcode.OrderStatusErr + o.Code = errorcode.OrderStatusErr return } return diff --git a/app/services/thirdpay/pay.go b/app/services/thirdpay/pay.go index 12973b6..9a968b4 100644 --- a/app/services/thirdpay/pay.go +++ b/app/services/thirdpay/pay.go @@ -1,18 +1,52 @@ package thirdpay import ( + "PaymentCenter/app/constants/common" "PaymentCenter/app/constants/errorcode" "PaymentCenter/app/http/entities/front" + "PaymentCenter/app/models/ordersmodel" "PaymentCenter/app/services" + "PaymentCenter/app/services/thirdpay/api" thirdpay "PaymentCenter/app/services/thirdpay/do" + "PaymentCenter/app/services/thirdpay/notify" "context" ) -func ThirdPayWeb(check *thirdpay.PayCheck) *thirdpay.Pay { +func ThirdPayUrl(check *thirdpay.PayCheck) *thirdpay.Pay { + pay := ThirdPay(check) + // 支付 + pay.PayUrl() + return pay +} + +func NewOrdersResp(db *ordersmodel.Orders) *api.OrdersResp { + return &api.OrdersResp{ + OrderNo: db.Id, + OutTreadNo: db.OutTreadNo, + Status: db.Status, + OrderType: db.OrderType, + Amount: db.Amount, + Desc: db.Desc, + CreateTime: db.CreateTime.Format("2006-01-02 15:04:05"), + } +} + +func NewOrderNotifyWithHandle(orderId int64, Status int, actualAmount int, msg string) *notify.OrderNotifyResp { + orderNotify := ¬ify.OrderNotify{ + OrderId: orderId, + Status: Status, + ActualAmount: actualAmount, + Msg: msg, + Code: errorcode.Success, + } + return orderNotify.Handle() +} + +func ThirdPayRefund(check *thirdpay.PayCheck) *thirdpay.Pay { pay := thirdpay.NewPay(check) // 创建订单 if &check.OldOrder != nil { - pay.CreateOrder() + pay.CreateOrder(common.ORDER_TYPE_PAY) if pay.PayCode != errorcode.Success { return pay } @@ -45,3 +79,17 @@ func ThirdPayInfoCheck(ctx context.Context, req *front.PayReqs, appCheck *servic return } + +func ThirdPay(check *thirdpay.PayCheck) (pay *thirdpay.Pay) { + pay = thirdpay.NewPay(check) + // 创建订单 + if &check.OldOrder != nil { + pay.CreateOrder(common.ORDER_TYPE_PAY) + if pay.PayCode != errorcode.Success { + return pay + } + } else { + pay.Order = check.OldOrder + } + return +} diff --git a/app/third/paymentService/ali_service.go b/app/third/paymentService/ali_service.go index bdee5d7..fbc448e 100644 --- a/app/third/paymentService/ali_service.go +++ b/app/third/paymentService/ali_service.go @@ -3,7 +3,7 @@ package paymentService import ( "PaymentCenter/app/constants/common" "PaymentCenter/app/constants/errorcode" - "PaymentCenter/app/services/thirdpay/notify" + "PaymentCenter/app/services/thirdpay" "PaymentCenter/app/third/paymentService/payCommon" "PaymentCenter/config" "context" @@ -126,7 +126,7 @@ func ALiCallBack(notifyReq gopay.BodyMap, aliConfig AliPay) error { return errors.New("订单状态异常,无法进行后续回调") } - res := notify.NewOrderNotifyWithHandle(int64(orderId), errCode, payerTotal, msg) + res := thirdpay.NewOrderNotifyWithHandle(int64(orderId), errCode, payerTotal, msg) merchantCallback, _ := json.Marshal(res) // 记录日志 go func() { diff --git a/app/third/paymentService/wechat_service.go b/app/third/paymentService/wechat_service.go index 3979b15..56fd8a9 100644 --- a/app/third/paymentService/wechat_service.go +++ b/app/third/paymentService/wechat_service.go @@ -3,7 +3,7 @@ package paymentService import ( "PaymentCenter/app/constants/common" "PaymentCenter/app/constants/errorcode" - "PaymentCenter/app/services/thirdpay/notify" + "PaymentCenter/app/services/thirdpay" "PaymentCenter/app/third/paymentService/payCommon" "PaymentCenter/config" "context" @@ -160,7 +160,7 @@ func WxPayCallBack(notifyReq *wechat.V3NotifyReq, wxConfig WxPay) error { // 触发下游回调的格式 orderId, _ := strconv.Atoi(CallBackInfo.OutTradeNo) - res := notify.NewOrderNotifyWithHandle(int64(orderId), errCode, int(CallBackInfo.Amount.PayerTotal), msg) + res := thirdpay.NewOrderNotifyWithHandle(int64(orderId), errCode, int(CallBackInfo.Amount.PayerTotal), msg) merchantCallback, _ := json.Marshal(res) // 记录日志 go func() {