From 57d3cefff1ecbb9afa04199c0bc8190d55b3b8ae Mon Sep 17 00:00:00 2001 From: DengBiao <2319963317@qq.com> Date: Tue, 10 Oct 2023 17:29:35 +0800 Subject: [PATCH] 1 --- app/customer/hdl/hdl_call_back.go | 43 +++++++++++- .../hdl_central_kitchen_for_school_order.go | 5 +- app/customer/hdl/test/hdl_wx.go | 68 +++++++++++++++++++ app/customer/md/wxpay.go | 8 +-- .../svc_central_kitchen_for_school_order.go | 23 +++++-- app/router/customer_router.go | 7 ++ 6 files changed, 142 insertions(+), 12 deletions(-) create mode 100644 app/customer/hdl/test/hdl_wx.go diff --git a/app/customer/hdl/hdl_call_back.go b/app/customer/hdl/hdl_call_back.go index 2f873e3..a738445 100644 --- a/app/customer/hdl/hdl_call_back.go +++ b/app/customer/hdl/hdl_call_back.go @@ -42,7 +42,7 @@ func AlipayJsApiCallBack(c *gin.Context) { } func WxJsApiCallBack(c *gin.Context) { - var req md.WxPayCallback + var req md.WxCallback err := c.ShouldBindJSON(&req) if err != nil { err = validate.HandleValidateErr(err) @@ -60,8 +60,9 @@ func WxJsApiCallBack(c *gin.Context) { e.OutErr(c, e.ERR, "解密失败") return } + fmt.Println(">>>>>>>>>>", decryptString) - var resp md.WxPayDecryptAesCallback + var resp md.WxPayCallback err = json.Unmarshal([]byte(decryptString), &resp) if err != nil { e.OutErr(c, e.ERR, err.Error()) @@ -74,4 +75,42 @@ func WxJsApiCallBack(c *gin.Context) { return } e.OutSuc(c, "success", nil) + return +} + +func WxRefundApiCallBack(c *gin.Context) { + var req md.WxCallback + err := c.ShouldBindJSON(&req) + if err != nil { + err = validate.HandleValidateErr(err) + err1 := err.(e.E) + e.OutErr(c, err1.Code, err1.Error()) + return + } + sysCfgDb := db.SysCfgDb{} + sysCfgDb.Set() + sysCfg := sysCfgDb.SysCfgFindWithDb(enum.WxSpMchApiV3Key) + + //TODO::进行解密 + decryptString, err := utils.DecryptAES256GCM(sysCfg[enum.WxSpMchApiV3Key], req.Resource.AssociatedData, req.Resource.Nonce, req.Resource.Ciphertext) + if err != nil { + e.OutErr(c, e.ERR, "解密失败") + return + } + + fmt.Println(">>>>>>>>>>WxRefundApiCallBack<<<<<<<<<", decryptString) + //var resp md.WxPayCallback + //err = json.Unmarshal([]byte(decryptString), &resp) + //if err != nil { + // e.OutErr(c, e.ERR, err.Error()) + // return + //} + // + //_, err = svc.DealCentralKitchenForSchoolOrderCallBackForWx(resp.OutTradeNo, resp.TransactionId, resp.TradeState) + //if err != nil { + // e.OutErr(c, e.ERR, err.Error()) + // return + //} + e.OutSuc(c, "success", nil) + return } diff --git a/app/customer/hdl/hdl_central_kitchen_for_school_order.go b/app/customer/hdl/hdl_central_kitchen_for_school_order.go index 4e7f74b..52b806a 100644 --- a/app/customer/hdl/hdl_central_kitchen_for_school_order.go +++ b/app/customer/hdl/hdl_central_kitchen_for_school_order.go @@ -15,6 +15,7 @@ import ( ) func CentralKitchenForSchoolOrderList(c *gin.Context) { + platform := c.DefaultQuery("platform", "alipay") var req md.CentralKitchenForSchoolOrderListReq err := c.ShouldBindJSON(&req) if err != nil { @@ -23,12 +24,12 @@ func CentralKitchenForSchoolOrderList(c *gin.Context) { e.OutErr(c, err1.Code, err1.Error()) return } - list, total, err := svc.OrderList(req) + list, total, err := svc.OrderList(req, platform) if err != nil { e.OutErr(c, e.ERR, err.Error()) return } - sumTotal, err := svc.OrderStatistic(req) + sumTotal, err := svc.OrderStatistic(req, platform) if err != nil { e.OutErr(c, e.ERR, err.Error()) return diff --git a/app/customer/hdl/test/hdl_wx.go b/app/customer/hdl/test/hdl_wx.go new file mode 100644 index 0000000..cfae7d0 --- /dev/null +++ b/app/customer/hdl/test/hdl_wx.go @@ -0,0 +1,68 @@ +package test + +import ( + "applet/app/db" + "applet/app/e" + enum2 "applet/app/enum" + svc3 "applet/app/svc" + "applet/app/utils" + "github.com/gin-gonic/gin" + "github.com/wechatpay-apiv3/wechatpay-go/core" + "github.com/wechatpay-apiv3/wechatpay-go/services/refunddomestic" + "math" + "time" +) + +func WxRefundsTest(c *gin.Context) { + outTradeNo := c.DefaultQuery("out_trade_no", "") + refund := c.DefaultQuery("refund", "") + centralKitchenForSchoolPackageOrd := db.CentralKitchenForSchoolPackageOrd{} + centralKitchenForSchoolPackageOrd.Set(outTradeNo) + ord, err := centralKitchenForSchoolPackageOrd.GetCentralKitchenForSchoolPackageOrd() + if err != nil { + e.OutErr(c, e.ERR_DB_ORM, err.Error()) + return + } + if ord == nil { + e.OutErr(c, e.ERR_NO_DATA, "未查询到对应订单记录") + return + } + sysCfgDb := db.SysCfgDb{} + sysCfgDb.Set() + sysCfg := sysCfgDb.SysCfgFindWithDb(enum2.WxSpMchId) + client, err := svc3.NewWxPayClient(c) + if err != nil { + e.OutErr(c, e.ERR, err.Error()) + return + } + wxSvc := refunddomestic.RefundsApiService{Client: client} + outRequestNoSuffix := "_" + utils.Int64ToStr(time.Now().Unix()) + outRequestNo := "r_" + ord.OutTradeNo + outRequestNoSuffix + + resp, result, err := wxSvc.Create(c, + refunddomestic.CreateRequest{ + SubMchid: core.String(sysCfg[enum2.WxSpMchId]), + OutTradeNo: core.String(ord.OutTradeNo), + OutRefundNo: core.String(outRequestNo), + Reason: core.String("央厨订餐退款"), + NotifyUrl: core.String("https://weixin.qq.com"), + Amount: &refunddomestic.AmountReq{ + Currency: core.String("CNY"), + Refund: core.Int64(utils.StrToInt64(refund)), + Total: core.Int64(int64(math.Round(utils.StrToFloat64(ord.TotalPrice) * 100))), + }, + }, + ) + if err != nil { + // 处理错误 + println("call Create err:%s", err) + } else { + // 处理返回结果 + println("status=%d resp=%s", result.Response.StatusCode, resp) + } + e.OutSuc(c, map[string]interface{}{ + "resp": resp, + "result": result, + }, nil) + return +} diff --git a/app/customer/md/wxpay.go b/app/customer/md/wxpay.go index c6e288e..be3e83e 100644 --- a/app/customer/md/wxpay.go +++ b/app/customer/md/wxpay.go @@ -1,7 +1,7 @@ package md -// WxPayCallback 微信回调结构体 -type WxPayCallback struct { +// WxCallback 微信回调结构体 +type WxCallback struct { Id string `json:"id"` CreateTime string `json:"create_time"` ResourceType string `json:"resource_type"` @@ -16,8 +16,8 @@ type WxPayCallback struct { } `json:"resource"` } -// WxPayDecryptAesCallback 微信解密回调结构体 -type WxPayDecryptAesCallback struct { +// WxPayCallback 微信支付回调结构体 +type WxPayCallback struct { SpMchid string `json:"sp_mchid"` SubMchid string `json:"sub_mchid"` SpAppid string `json:"sp_appid"` diff --git a/app/customer/svc/order/svc_central_kitchen_for_school_order.go b/app/customer/svc/order/svc_central_kitchen_for_school_order.go index b35d833..0a7c25f 100644 --- a/app/customer/svc/order/svc_central_kitchen_for_school_order.go +++ b/app/customer/svc/order/svc_central_kitchen_for_school_order.go @@ -2,6 +2,7 @@ package svc import ( "applet/app/admin/svc/enterprise_manage" + enum2 "applet/app/customer/enum" "applet/app/customer/md" "applet/app/db" "applet/app/db/model" @@ -12,10 +13,16 @@ import ( "time" ) -func OrderList(req md.CentralKitchenForSchoolOrderListReq) (m []model.CentralKitchenForSchoolPackageOrd, total int64, err error) { +func OrderList(req md.CentralKitchenForSchoolOrderListReq, platform string) (m []model.CentralKitchenForSchoolPackageOrd, total int64, err error) { sess := db.Db.Desc("id").Where("user_identity_id =?", req.UserIdentityId). And("create_at >= ? And create_at <= ?", req.StartDate, req.EndDate).Limit(req.Limit, (req.Page-1)*req.Limit) sess.And("ord_state = ?", req.OrdState) + if platform == "alipay" { + sess.And("pay_way = ?", enum2.PayWayForAli) + } + if platform == "wx" { + sess.And("pay_way = ?", enum2.PayWayForWx) + } total, err = sess.FindAndCount(&m) if err != nil { return @@ -23,9 +30,17 @@ func OrderList(req md.CentralKitchenForSchoolOrderListReq) (m []model.CentralKit return } -func OrderStatistic(req md.CentralKitchenForSchoolOrderListReq) (total float64, err error) { - var sql = fmt.Sprintf("SELECT SUM(total_price) AS sum_total FROM central_kitchen_for_school_package_ord where "+ - "user_identity_id = '%d' and create_at >= '%s' And create_at <= '%s' and ord_state = '%d'", req.UserIdentityId, req.StartDate, req.EndDate, req.OrdState) +func OrderStatistic(req md.CentralKitchenForSchoolOrderListReq, platform string) (total float64, err error) { + var sql string + if platform == "alipay" { + sql = fmt.Sprintf("SELECT SUM(total_price) AS sum_total FROM central_kitchen_for_school_package_ord where "+ + "user_identity_id = '%d' and create_at >= '%s' And create_at <= '%s' and ord_state = '%d' and pay_way = '%d'", req.UserIdentityId, req.StartDate, req.EndDate, req.OrdState, enum2.PayWayForAli) + } + if platform == "wx" { + sql = fmt.Sprintf("SELECT SUM(total_price) AS sum_total FROM central_kitchen_for_school_package_ord where "+ + "user_identity_id = '%d' and create_at >= '%s' And create_at <= '%s' and ord_state = '%d' and pay_way = '%d'", req.UserIdentityId, req.StartDate, req.EndDate, req.OrdState, enum2.PayWayForWx) + } + mapArr1, err := db.QueryNativeString(db.Db, sql) if err != nil { return diff --git a/app/router/customer_router.go b/app/router/customer_router.go index df9e7fa..0cd8b1b 100644 --- a/app/router/customer_router.go +++ b/app/router/customer_router.go @@ -3,6 +3,7 @@ package router import ( "applet/app/customer/hdl" selfSupportForSchoolhdl "applet/app/customer/hdl/self_support_for_school" + "applet/app/customer/hdl/test" "applet/app/customer/mw" "github.com/gin-gonic/gin" ) @@ -14,6 +15,7 @@ func CustomerInit(r *gin.RouterGroup) { rSelfSupportForSchoolOrder(r.Group("/order/selfSupportForSchool")) rSelfSupportForSchool(r.Group("/selfSupportForSchool")) rUserIdentity(r.Group("/userIdentity")) + rTest(r.Group("/test")) r.GET("/sanHu", hdl.SanHu) r.POST("/test", hdl.Demo) @@ -21,6 +23,7 @@ func CustomerInit(r *gin.RouterGroup) { r.POST("/curlAlipayPlanetEcocampusApiRosterSignUpInfoTest", hdl.CurlAlipayPlanetEcocampusApiRosterSignUpInfo) r.POST("/alipayJsApiCallBack", hdl.AlipayJsApiCallBack) r.POST("/wxJsApiCallBack", hdl.WxJsApiCallBack) + r.POST("/wxRefundApiCallBack", hdl.WxRefundApiCallBack) r.POST("/login", hdl.Login) r.GET("/wxAppletLogin", hdl.WxAppletLogin) @@ -64,6 +67,10 @@ func CustomerInit(r *gin.RouterGroup) { } } +func rTest(r *gin.RouterGroup) { + r.POST("/wxRefunds", test.WxRefundsTest) // 测试-微信退款 +} + func rPay(r *gin.RouterGroup) { r.Use(mw.Auth) //检测登录状态 r.POST("/buyPackage", hdl.BuyPackage) // 购买套餐