From 4480a183f004d5f1b4bbd1208a7c4ee1bd679db7 Mon Sep 17 00:00:00 2001 From: huangjiajun <582604932@qq.com> Date: Tue, 19 Nov 2024 15:09:23 +0800 Subject: [PATCH] 1 --- app/hdl/hdl_pay_callback.go | 28 +++++++++++++++++ app/router/router.go | 1 + app/svc/svc_pay.go | 63 +++++++++++++++++++++++++++++++++++++ 3 files changed, 92 insertions(+) create mode 100644 app/hdl/hdl_pay_callback.go diff --git a/app/hdl/hdl_pay_callback.go b/app/hdl/hdl_pay_callback.go new file mode 100644 index 0000000..72dbf98 --- /dev/null +++ b/app/hdl/hdl_pay_callback.go @@ -0,0 +1,28 @@ +package hdl + +import ( + "applet/app/e" + "applet/app/svc" + "applet/app/utils/logx" + "errors" + "github.com/gin-gonic/gin" +) + +// 整合所有支付回调 +func PayCallback(c *gin.Context) { + // 统一处理回调参数 + data, orderType, payMethod := svc.CheckAllCallbackParams(c) + if data == nil { + e.OutErr(c, e.ERR_INVALID_ARGS) + return + } + c.Set("callback", data) + payCallbackFunc, ok := svc.PayCallbackFuncList[orderType][payMethod] + if !ok || payCallbackFunc == nil { + _ = logx.Warn(errors.New("回调不存在")) + return + } + payCallbackFunc(c) + c.String(200, "success") + return +} diff --git a/app/router/router.go b/app/router/router.go index 53cb695..d0746c8 100644 --- a/app/router/router.go +++ b/app/router/router.go @@ -139,6 +139,7 @@ func routeCommunityTeamOwnStore(r *gin.RouterGroup) { r.POST("/notice/del", storeHdl.NoticeDel) } func routeCommunityTeam(r *gin.RouterGroup) { + r.Any("/pay/callback", hdl.PayCallback) r.Use(mw.DB) // 下面接口再根据mid 获取数据库名 r.Use(mw.CheckBody) //body参数转换 r.Use(mw.CheckSign) //签名校验 diff --git a/app/svc/svc_pay.go b/app/svc/svc_pay.go index 0c53c83..d7bbdf7 100644 --- a/app/svc/svc_pay.go +++ b/app/svc/svc_pay.go @@ -2,9 +2,72 @@ package svc import ( "applet/app/md" + "applet/app/utils" + "applet/app/utils/logx" + "encoding/json" + "encoding/xml" + "fmt" "github.com/gin-gonic/gin" + "github.com/iGoogle-ink/gopay" + "io/ioutil" + "net/url" ) +func CheckAllCallbackParams(c *gin.Context) (interface{}, string, string) { + body, _ := ioutil.ReadAll(c.Request.Body) + dataAlipay, _ := GetAlipayCallbackParams(body) + if dataAlipay != nil && dataAlipay.PayMethod == md.ALIPAY { + return dataAlipay, dataAlipay.OrderType, dataAlipay.PayMethod + } + dataWxPay, _ := GetWxPayCallbackParams(body) + if dataWxPay != nil && dataWxPay.PayMethod == md.WX_PAY { + return dataWxPay, dataWxPay.OrderType, dataWxPay.PayMethod + } + return nil, "", "" +} + +// 支付宝参数解析 +func GetAlipayCallbackParams(body []byte) (*md.AliPayCallback, error) { + //decodeArgs := "order_type=privilege_card&pay_method=alipay&master_id=123456&gmt_create=2021-01-06+15%3A33%3A18&charset=utf-8&seller_email=1666296478%40qq.com&subject=%E6%B5%8B%E8%AF%95&sign=ILbQtP7E51hcdKaroi%2FKlefltw%2BaSOeaqgvBhYIcIuRy5yy440OIOdxPEvhTITA%2BHnA1Lgf3STepzzHVmvvTFIGeKL%2FVvz%2FLSI7vXGpUBxcw2entVZCzhmblGAB8hiK4EOHBIfUAQiyo9ePWl63p%2B%2BvS4CQuZe8SFXrFaj2bTnRyBPemxekudU8TP8tZd630SzHx7kOHYqxTYZ7kHMrLK0fjIdSWBHtSD2cocm%2FxVQ3wYXUyyfZwwTpcXLW9ao97Uj1hZSnknuFRtMfvE57D5f4W3cB%2Bnp%2B39xpuvfbBlbXXllzKUfkWVBqV6zebYMnzaogwVN%2FDXgp74BQOzkJYvA%3D%3D&buyer_id=2088012352245491&invoice_amount=0.01¬ify_id=2021010600222153319045491449160553&fund_bill_list=%5B%7B%22amount%22%3A%220.01%22%2C%22fundChannel%22%3A%22PCREDIT%22%7D%5D¬ify_type=trade_status_sync&trade_status=TRADE_SUCCESS&receipt_amount=0.01&app_id=2016120103683451&buyer_pay_amount=0.01&sign_type=RSA2&seller_id=2088221294035253&gmt_payment=2021-01-06+15%3A33%3A18¬ify_time=2021-01-06+15%3A33%3A19&passback_params=706160991839039994&version=1.0&out_trade_no=791161468268877545&total_amount=0.01&trade_no=2021010622001445491401511921&auth_app_id=2016120103683451&buyer_logon_id=150****0420&point_amount=0.00" + decodeArgs, err := url.QueryUnescape(string(body)) + if err != nil { + _ = logx.Warn(err) + return nil, logx.Warn("回调参数解码错误") + } + data, err := url.ParseQuery(decodeArgs) + if err != nil { + return nil, err + } + dataMap := make(map[string]interface{}) + for k := range data { + dataMap[k] = data.Get(k) + } + callbackStr := utils.Serialize(dataMap) + fmt.Println("支付宝回调数据", string(callbackStr)) + var args md.AliPayCallback + if err := json.Unmarshal(callbackStr, &args); err != nil { + return nil, logx.Warn(err) + } + return &args, nil +} + +// 微信回调参数解析 +func GetWxPayCallbackParams(body []byte) (*md.WxPayCallback, error) { + dataMap := make(gopay.BodyMap) + if err := xml.Unmarshal(body, &dataMap); err != nil { + return nil, fmt.Errorf("xml.Unmarshal(%s):%w", string(body), err) + } + callbackStr := utils.Serialize(dataMap) + fmt.Println("微信回调数据", string(callbackStr)) + var args md.WxPayCallback + err := json.Unmarshal(callbackStr, &args) + if err != nil { + fmt.Println("err3") + return nil, logx.Warn(err) + } + return &args, nil +} + var PayFuncList = map[string]map[string]func(*gin.Context) (interface{}, error){ md.CommunityTeam: { md.BALANCE_PAY: BalanceCommunityTeam,