From 1630304f3dc95160ebfe8c86f2eb663bd1b4c832 Mon Sep 17 00:00:00 2001 From: Rzy <465386466@qq.com> Date: Fri, 9 Aug 2024 10:43:00 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E9=99=A4pay=E5=92=8Cpaycheck?= =?UTF-8?q?=E4=B9=8B=E9=97=B4=E7=9A=84=E5=BC=BA=E5=85=B3=E8=81=94=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/services/thirdpay/do/pay.go | 84 +++++++++++++++++---------- app/services/thirdpay/do/pay_check.go | 2 +- app/services/thirdpay/pay.go | 4 +- 3 files changed, 56 insertions(+), 34 deletions(-) diff --git a/app/services/thirdpay/do/pay.go b/app/services/thirdpay/do/pay.go index d87a09f..1942d65 100644 --- a/app/services/thirdpay/do/pay.go +++ b/app/services/thirdpay/do/pay.go @@ -7,25 +7,47 @@ import ( "PaymentCenter/app/models/paychannelmodel" "PaymentCenter/app/services" "PaymentCenter/app/third/paymentService/payCommon" + "context" "PaymentCenter/app/models/ordersmodel" "PaymentCenter/app/third/paymentService" ) -type Pay struct { - Merchant *merchantmodel.Merchant - Channel *paychannelmodel.PayChannel - - paycheck *PayCheck - Order *ordersmodel.Orders - PayCode int - Url string +type PayParam struct { + Merchant *merchantmodel.Merchant + Channel *paychannelmodel.PayChannel + App_id int64 + OutTradeNo string + Amount int + ExtJson string + Desc string + ClientIp string } -func NewPay(paycheck *PayCheck) *Pay { +type Pay struct { + ctx *context.Context + PayParam *PayParam + RelationOrder *ordersmodel.Orders + Order *ordersmodel.Orders + PayCode int + Url string +} + +func NewPayWithPayCheck(paycheck *PayCheck) *Pay { return &Pay{ - paycheck: paycheck, - PayCode: errorcode.Success, + ctx: paycheck.ctx, + PayParam: &PayParam{ + Merchant: paycheck.Merchant, + Channel: paycheck.Channel, + App_id: paycheck.Reqs.AppId, + OutTradeNo: paycheck.Reqs.OutTradeNo, + Amount: paycheck.Reqs.Amount, + ExtJson: paycheck.Reqs.ExtJson, + Desc: paycheck.Reqs.Desc, + ClientIp: paycheck.AppCheck.Ip, + }, + RelationOrder: paycheck.OldOrder, + PayCode: errorcode.Success, } } @@ -35,14 +57,14 @@ func (w *Pay) CreateOrder(order_type int) { return } w.Order, w.PayCode = services.OrderCreate(&ordersmodel.Orders{ - MerchantId: w.paycheck.Merchant.Id, - PayChannelId: w.paycheck.Channel.Id, - AppId: w.paycheck.Reqs.AppId, - OutTreadNo: w.paycheck.Reqs.OutTradeNo, + MerchantId: w.PayParam.Merchant.Id, + PayChannelId: w.PayParam.Channel.Id, + AppId: w.PayParam.App_id, + OutTreadNo: w.PayParam.OutTradeNo, OrderType: order_type, - Amount: w.paycheck.Reqs.Amount, - ExtJson: w.paycheck.Reqs.ExtJson, - Desc: w.paycheck.Reqs.Desc, + Amount: w.PayParam.Amount, + ExtJson: w.PayParam.ExtJson, + Desc: w.PayParam.Desc, Status: common.ORDER_STATUS_WAITPAY, }, ) @@ -54,23 +76,23 @@ func (w *Pay) PayUrl() (url string) { ok bool ) thirdPay := &paymentService.PayOrderRequest{ - PayChannelId: w.paycheck.Reqs.PayChannelId, + PayChannelId: w.PayParam.Channel.Id, OrderId: w.Order.Id, - ChannelType: w.paycheck.Channel.ChannelType, + ChannelType: w.PayParam.Channel.ChannelType, Description: w.Order.Desc, Amount: w.Order.Amount, - PayerClientIp: w.paycheck.AppCheck.Ip, + PayerClientIp: w.PayParam.ClientIp, } - if payFunc, ok = PayWayList[w.paycheck.Channel.ChannelType]; !ok { + if payFunc, ok = PayWayList[w.PayParam.Channel.ChannelType]; !ok { w.PayCode = errorcode.PayChannelNotBuild return } - err := payFunc(thirdPay, w.paycheck.Channel) + err := payFunc(thirdPay, w.PayParam.Channel) if err != nil { w.PayCode = errorcode.PayChannelExtJsonError return } - res := paymentService.PaymentService(*w.paycheck.ctx, *thirdPay) + res := paymentService.PaymentService(*w.ctx, *thirdPay) if res.Code == payCommon.PAY_SUCCESS_CODE { w.Order.Status = common.ORDER_STATUS_PAYING @@ -93,21 +115,21 @@ func (w *Pay) Refund() { ) thirdPayRefund := &paymentService.OrderRefundRequest{ OrderId: w.Order.Id, - RefundOrderId: w.paycheck.OldOrder.Id, - RefundReason: w.paycheck.Reqs.Desc, - RefundAmount: int64(w.paycheck.Reqs.Amount), - PayChannel: w.paycheck.Channel.ChannelType, + RefundOrderId: w.RelationOrder.Id, + RefundReason: w.PayParam.Desc, + RefundAmount: int64(w.PayParam.Amount), + PayChannel: w.PayParam.Channel.ChannelType, } - if refundFunc, ok = RefundWayList[w.paycheck.Channel.ChannelType]; !ok { + if refundFunc, ok = RefundWayList[w.PayParam.Channel.ChannelType]; !ok { w.PayCode = errorcode.PayChannelNotBuild return } - err := refundFunc(thirdPayRefund, w.paycheck.Channel) + err := refundFunc(thirdPayRefund, w.PayParam.Channel) if err != nil { w.PayCode = errorcode.PayChannelExtJsonError return } - res := paymentService.OrderRefund(*w.paycheck.ctx, *thirdPayRefund) + res := paymentService.OrderRefund(*w.ctx, *thirdPayRefund) if res.Code == payCommon.PAY_SUCCESS_CODE { w.Order.Status = common.ORDER_STATUS_PAYING code := services.OrderUpdate(w.Order, "status") diff --git a/app/services/thirdpay/do/pay_check.go b/app/services/thirdpay/do/pay_check.go index 52a0170..35a31d5 100644 --- a/app/services/thirdpay/do/pay_check.go +++ b/app/services/thirdpay/do/pay_check.go @@ -104,7 +104,7 @@ func (w *PayCheck) GetOrder() { w.CheckCode = code return } - if code == errorcode.OrdersExist { + if code == errorcode.Success { w.OldOrder = order } diff --git a/app/services/thirdpay/pay.go b/app/services/thirdpay/pay.go index 4b1d521..5932e67 100644 --- a/app/services/thirdpay/pay.go +++ b/app/services/thirdpay/pay.go @@ -58,14 +58,14 @@ func ThirdPayRefund(ctx context.Context, refundReq *front.RefundReqs, appCheck * if check.CheckCode != errorcode.Success { return } - refund = thirdpay.NewPay(check) + refund = thirdpay.NewPayWithPayCheck(check) refund.CreateOrder(common.ORDER_TYPE_REFUND) refund.Refund() return } func ThirdPay(check *thirdpay.PayCheck) (pay *thirdpay.Pay) { - pay = thirdpay.NewPay(check) + pay = thirdpay.NewPayWithPayCheck(check) // 创建订单 if &check.OldOrder != nil { pay.CreateOrder(common.ORDER_TYPE_PAY)