huangjiajun 1 month ago
parent
commit
4480a183f0
3 changed files with 92 additions and 0 deletions
  1. +28
    -0
      app/hdl/hdl_pay_callback.go
  2. +1
    -0
      app/router/router.go
  3. +63
    -0
      app/svc/svc_pay.go

+ 28
- 0
app/hdl/hdl_pay_callback.go View File

@@ -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
}

+ 1
- 0
app/router/router.go View File

@@ -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) //签名校验


+ 63
- 0
app/svc/svc_pay.go View File

@@ -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&notify_id=2021010600222153319045491449160553&fund_bill_list=%5B%7B%22amount%22%3A%220.01%22%2C%22fundChannel%22%3A%22PCREDIT%22%7D%5D&notify_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&notify_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,


Loading…
Cancel
Save