From cc93c10aa24417a4181da728c965142becc17c14 Mon Sep 17 00:00:00 2001 From: lwl0608 Date: Wed, 29 May 2024 10:56:52 +0800 Subject: [PATCH] =?UTF-8?q?=E9=80=80=E6=AC=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/mix/service/logic/vas.go | 20 +++++++++-- library/idgenerator/genid.go | 7 ++++ library/payclients/yeepaycli/client.go | 47 ++++++++++++++++++++++++++ 3 files changed, 72 insertions(+), 2 deletions(-) diff --git a/app/mix/service/logic/vas.go b/app/mix/service/logic/vas.go index 3b55d2fe..0248f431 100644 --- a/app/mix/service/logic/vas.go +++ b/app/mix/service/logic/vas.go @@ -320,7 +320,7 @@ func (v *Vas) CreateOrder(ctx *gin.Context, req *vasproto.CreateOrderReq) (data return } yeepayAlipayH5ParamStr = aggPayResp.PrePayTn - req.Oid3 = yeepayCli.AppId + req.Oid3 = yeepayCli.MerchantNo case vasproto.PayTypeYeepayWxpayH5: var aggPayResp *yeepaycli.AggPayResp yeepayCli := yeepaycli.GetDefaultYeepayClient() @@ -342,7 +342,7 @@ func (v *Vas) CreateOrder(ctx *gin.Context, req *vasproto.CreateOrderReq) (data return } yeepayWxpayH5ParamStr = aggPayResp.PrePayTn - req.Oid3 = yeepayCli.AppId + req.Oid3 = yeepayCli.MerchantNo } // 添加订单 @@ -3519,6 +3519,22 @@ func (v *Vas) payRefund(ctx *gin.Context, order *dbstruct.Order) error { logger.Error("wxpayCli.RefundOne fail, orderId: %v, resp: %v, err: %v", orderId, util.ToJson(resp), err) return err } + case vasproto.PayTypeYeepayAlipayH5, vasproto.PayTypeYeepayWxpayH5: + yeepayCli := yeepaycli.GetDefaultYeepayClient() + resp, err := yeepayCli.RefundOne(ctx, &yeepaycli.RefundOneParam{ + OutTradeNo: orderId, + RefundAmount: order.GetPayAmount(), + RefundReason: "用户退款", + }) + if err != nil { + logger.Error("wxpayCli.RefundOne fail, orderId: %v, resp: %v, err: %v", orderId, util.ToJson(resp), err) + return err + } + if resp.Status != "SUCCESS" { + err = fmt.Errorf("退款失败,请稍后再试") + logger.Error("wxpayCli.RefundOne fail, orderId: %v, resp: %v, err: %v", orderId, util.ToJson(resp), err) + return err + } } return nil } diff --git a/library/idgenerator/genid.go b/library/idgenerator/genid.go index 0043b581..f186b3f8 100644 --- a/library/idgenerator/genid.go +++ b/library/idgenerator/genid.go @@ -53,6 +53,7 @@ const ( NodeZoneSession // node 空间对话表 NodeZoneThirdPartner // node 空间代运营表 NodeDailyStatementZoneInfo // node 空间相关每日报表 + NodeYeepayRefund // node 易宝支付退款 ) func GenIdInt64(node int64) (int64, error) { @@ -252,3 +253,9 @@ func GenDailyStatementZoneInfoId() int64 { id, _ := GenIdInt64(NodeDailyStatementZoneInfo) return id } + +// yeepay refund +func GenYeepayRefundId() string { + id, _ := GenIdString(NodeYeepayRefund) + return id +} diff --git a/library/payclients/yeepaycli/client.go b/library/payclients/yeepaycli/client.go index d8d255b0..e5c9e4e1 100644 --- a/library/payclients/yeepaycli/client.go +++ b/library/payclients/yeepaycli/client.go @@ -3,6 +3,7 @@ package yeepaycli import ( "context" "encoding/json" + "fmt" "github.com/yop-platform/yop-go-sdk/yop/client" "github.com/yop-platform/yop-go-sdk/yop/constants" "github.com/yop-platform/yop-go-sdk/yop/request" @@ -10,6 +11,7 @@ import ( "net/http" "service/bizcommon/util" "service/library/configcenter" + "service/library/idgenerator" "service/library/logger" "time" ) @@ -149,3 +151,48 @@ func (c *YeepayClient) AggPay(ctx context.Context, param *AggPayParam) (resp *Ag logger.Info("yeepay AggPay param: %v, resp: %v", util.ToJson(req.Params), util.ToJson(resp)) return } + +// 退款 +type RefundOneParam struct { + OutTradeNo string // 商家订单id,我们自己的订单id + RefundAmount int64 // 退款金额,单位:分 + RefundReason string // 退款理由 +} + +type RefundOneResp struct { + Code string `json:"code"` + Message string `json:"message"` + ParentMerchantNo string `json:"parentMerchantNo"` + MerchantNo string `json:"merchantNo"` + OrderId string `json:"orderId"` + RefundRequestId string `json:"refundRequestId"` + UniqueRefundNo string `json:"uniqueRefundNo"` + Status string `json:"status"` + RefundAmount string `json:"refundAmount"` + RefundRequestDate string `json:"refundRequestDate"` + RefundMerchantFee string `json:"refundMerchantFee"` + RefundAccountDetail string `json:"refundAccountDetail"` + RefundCsFinishDate string `json:"refundCsFinishDate"` +} + +func (c *YeepayClient) RefundOne(ctx context.Context, param *RefundOneParam) (resp *RefundOneResp, err error) { + req := request.NewYopRequest(constants.POST_HTTP_METHOD, "/rest/v1.0/trade/refund") + req.AppId = c.AppId + req.IsvPriKey = request.IsvPriKey{Value: c.PrivateKey, CertType: request.RSA2048} + req.AddParam("parentMerchantNo", c.ParentMerchantNo) + req.AddParam("merchantNo", c.MerchantNo) + req.AddParam("orderId", param.OutTradeNo) + req.AddParam("refundRequestId", idgenerator.GenYeepayRefundId()) + req.AddParam("refundAmount", fmt.Sprintf("%.2f", float64(param.RefundAmount)/100.0)) + + respMeta, err := c.client.Request(req) + if err != nil { + logger.Error("yeepay RefundOne fail, param: %v, respMeta: %v, err: %v", util.ToJson(req.Params), util.ToJson(respMeta), err) + return + } + resp = new(RefundOneResp) + b, _ := json.Marshal(respMeta.Result) + _ = json.Unmarshal(b, resp) + logger.Info("yeepay RefundOne param: %v, resp: %v", util.ToJson(req.Params), util.ToJson(resp)) + return +}