diff --git a/app/http/controllers/front/payment_controller.go b/app/http/controllers/front/payment_controller.go index f22a12f..a21e05d 100644 --- a/app/http/controllers/front/payment_controller.go +++ b/app/http/controllers/front/payment_controller.go @@ -1,28 +1,53 @@ package front import ( + "PaymentCenter/app/constants/common" + "PaymentCenter/app/constants/errorcode" + "PaymentCenter/app/models/paychannelmodel" + "PaymentCenter/app/services" "PaymentCenter/app/third/paymentService" + "encoding/json" + "fmt" "github.com/gin-gonic/gin" "github.com/go-pay/gopay" "github.com/go-pay/gopay/alipay" "github.com/go-pay/gopay/wechat/v3" "github.com/go-pay/xlog" "github.com/qit-team/snow-core/log/logger" + "strconv" "net/http" ) // WxCallback 微信支付回调 func WxCallback(c *gin.Context) { - appId := c.Param("appId") - logger.Info(c, "WxCallback-回调数据APPID", appId) - if appId == "" { + payChannelId := c.Param("payChannelId") + logger.Info(c, "WxCallback-回调数据payChannelId", payChannelId) + if payChannelId == "" { c.String(http.StatusBadRequest, "%s", "fail") } - // todo 查询应用下的支付配置 + // 查询应用下的支付配置 + var payChannelModel paychannelmodel.PayChannel + payChannelIdInt, _ := strconv.Atoi(payChannelId) + payChannelModel.Id = int64(payChannelIdInt) + services.PayChannelGet(&payChannelModel) + if payChannelModel.ChannelType != common.PAY_CHANNEL_WECHAT_H5 { + logger.Error(c, "WxCallback-回调数据解析支付配置错误,查询的数据不是当前渠道") + c.String(http.StatusBadRequest, "%s", "fail") + } var wxConfig paymentService.WxPay + err := json.Unmarshal([]byte(payChannelModel.ExtJson), &wxConfig) + if err != nil { + logger.Error(c, "WxCallback-回调数据解析支付配置错误", fmt.Sprintf("错误原因:%s", err.Error())) + c.String(http.StatusBadRequest, "%s", "fail") + } + if wxConfig.ApiV3Key == "" || wxConfig.MchId == "" || wxConfig.PrivateKey == "" || wxConfig.SerialNo == "" { + logger.Error(c, "WxCallback-回调数据解析支付配置错误,解析出来的信息为空") + c.String(http.StatusBadRequest, "%s", "fail") + } + wxConfig.AppId = payChannelModel.AppId logger.Info(c, "WxCallback-回调数据", c.Request) notifyReq, err := wechat.V3ParseNotify(c.Request) @@ -45,12 +70,46 @@ func WxCallback(c *gin.Context) { // AliCallback 支付宝支付回调 func AliCallback(c *gin.Context) { - appId := c.Param("appId") - logger.Info(c, "AliCallback-回调数据APPID", appId) - if appId == "" { + payChannelId := c.Param("payChannelId") + logger.Info(c, "AliCallback-回调数据APPID", payChannelId) + if payChannelId == "" { c.String(http.StatusBadRequest, "%s", "fail") } - // todo 查询应用下的支付配置 + // 查询应用下的支付配置 + var payChannelModel paychannelmodel.PayChannel + payChannelIdInt, _ := strconv.Atoi(payChannelId) + payChannelModel.Id = int64(payChannelIdInt) + code := services.PayChannelGet(&payChannelModel) + if payChannelModel.ChannelType != common.PAY_CHANNEL_ALIPAY_WEB { + logger.Error(c, "AliCallback-回调数据解析支付配置错误,查询的数据不是当前渠道") + c.String(http.StatusBadRequest, "%s", "fail") + } + if code == errorcode.PayChannelNotFound { + logger.Error(c, "AliCallback-回调数据未获取到支付配置,404") + c.String(http.StatusBadRequest, "%s", "fail") + } + + var aliConfig paymentService.AliPay + var aliConfigModel struct { + PrivateKey string `json:"private_key"` // 应用私钥 + AppPublicCert string `json:"app_public_cert"` // 应用公钥 + AlipayRootCert string `json:"alipay_root_cert"` // 支付宝根证书 + AlipayPublicCert string `json:"alipay_public_cert"` // 支付宝公钥 + } + err := json.Unmarshal([]byte(payChannelModel.ExtJson), &aliConfigModel) + if err != nil { + logger.Error(c, "AliCallback-回调数据解析支付配置错误", fmt.Sprintf("错误原因:%s", err.Error())) + c.String(http.StatusBadRequest, "%s", "fail") + } + if aliConfigModel.AlipayPublicCert == "" || aliConfigModel.PrivateKey == "" || aliConfigModel.AppPublicCert == "" || aliConfigModel.AlipayRootCert == "" { + logger.Error(c, "AliCallback-回调数据解析支付配置错误,解析出来的信息为空") + c.String(http.StatusBadRequest, "%s", "fail") + } + aliConfig.AppId = payChannelModel.AppId + aliConfig.PrivateKey = aliConfigModel.PrivateKey + aliConfig.AppPublicCert = []byte(aliConfigModel.AppPublicCert) + aliConfig.AlipayRootCert = []byte(aliConfigModel.AlipayRootCert) + aliConfig.AlipayPublicCert = []byte(aliConfigModel.AlipayPublicCert) notifyReq, err := alipay.ParseNotifyToBodyMap(c.Request) // c.Request 是 gin 框架的写法 logger.Info(c, "AliCallback-回调数据", c.Request) @@ -58,7 +117,6 @@ func AliCallback(c *gin.Context) { xlog.Error(err) return } - var aliConfig paymentService.AliPay err = paymentService.ALiCallBack(notifyReq, aliConfig) if err != nil { logger.Error(c, "AliCallback-回调执行失败,失败原因:", err.Error()) diff --git a/app/http/routes/route.go b/app/http/routes/route.go index a3fe510..8c1f59e 100644 --- a/app/http/routes/route.go +++ b/app/http/routes/route.go @@ -55,8 +55,8 @@ func RegisterRoute(router *gin.Engine) { //回调处理 notify := v1.Group("/notify") { - notify.POST("/wx/:appId", front.WxCallback) - notify.POST("/ali/:appId", front.AliCallback) + notify.POST("/wx/:payChannelId", front.WxCallback) + notify.POST("/ali/:payChannelId", front.AliCallback) } } diff --git a/app/third/paymentService/ali_service.go b/app/third/paymentService/ali_service.go index 8fdf7f8..df3e6a7 100644 --- a/app/third/paymentService/ali_service.go +++ b/app/third/paymentService/ali_service.go @@ -76,8 +76,8 @@ func ALiH5PayInfo(c context.Context, payOrderRequest PayOrderRequest) (string, e bm.Set("out_trade_no", payOrderRequest.OrderId). Set("total_amount", amount). Set("subject", payOrderRequest.Description). - //Set("notify_url", payCommon.ALI_NOTIFY_URL_TEST+payOrderRequest.Ali.AppId). - Set("notify_url", payCommon.ALI_NOTIFY_URL_PROD+payOrderRequest.Ali.AppId) + //Set("notify_url", fmt.Sprintf(payCommon.ALI_NOTIFY_URL_TEST+"%d", payOrderRequest.PayChannelId)). + Set("notify_url", fmt.Sprintf(payCommon.ALI_NOTIFY_URL_PROD+"%d", payOrderRequest.PayChannelId)) aliRsp, err := aliClient.TradeWapPay(c, bm) if err != nil { diff --git a/app/third/paymentService/payCommon/common.go b/app/third/paymentService/payCommon/common.go index 7c376df..bf2c8c5 100644 --- a/app/third/paymentService/payCommon/common.go +++ b/app/third/paymentService/payCommon/common.go @@ -21,10 +21,10 @@ const ( WX_SUCCESS_CODE = 200 // 微信状态码返回成功 - WX_NOTIFY_URL_TEST = TEST_URL + "/v1/notify/wx/appId" // 微信支付回调地址 - WX_NOTIFY_URL_PROD = PROD_URL + "/v1/notify/wx/appId" // 微信支付回调地址 - ALI_NOTIFY_URL_TEST = TEST_URL + "/v1/notify/ali/appId" // 支付宝支付回调地址 - ALI_NOTIFY_URL_PROD = PROD_URL + "/v1/notify/ali/appId" // 支付宝支付回调地址 + WX_NOTIFY_URL_TEST = TEST_URL + "/v1/notify/wx/" // 微信支付回调地址 + WX_NOTIFY_URL_PROD = PROD_URL + "/v1/notify/wx/" // 微信支付回调地址 + ALI_NOTIFY_URL_TEST = TEST_URL + "/v1/notify/ali/" // 支付宝支付回调地址 + ALI_NOTIFY_URL_PROD = PROD_URL + "/v1/notify/ali/" // 支付宝支付回调地址 ORDER_NO_TYPE_ORDER_NO = 2 diff --git a/app/third/paymentService/payment_service.go b/app/third/paymentService/payment_service.go index cf9a369..6f5e00b 100644 --- a/app/third/paymentService/payment_service.go +++ b/app/third/paymentService/payment_service.go @@ -8,6 +8,7 @@ import ( ) type PayOrderRequest struct { + PayChannelId int64 `json:"pay_channel_id"` // 支付方式ID OrderId int64 `json:"order_id"` // 平台订单号 ChannelType int `json:"channel_type"` // 支付方式 Description string `json:"description"` // 商品描述 diff --git a/app/third/paymentService/wechat_service.go b/app/third/paymentService/wechat_service.go index 24c629b..f7c9b03 100644 --- a/app/third/paymentService/wechat_service.go +++ b/app/third/paymentService/wechat_service.go @@ -73,8 +73,8 @@ func WxH5PayInfo(c context.Context, payOrderRequest PayOrderRequest) (string, er Set("description", payOrderRequest.Description). Set("out_trade_no", payOrderRequest.OrderId). Set("time_expire", expire). - //Set("notify_url", payCommon.WX_NOTIFY_URL_TEST+payOrderRequest.Wx.AppId). - Set("notify_url", payCommon.WX_NOTIFY_URL_PROD+payOrderRequest.Wx.AppId). + //Set("notify_url", fmt.Sprintf(payCommon.WX_NOTIFY_URL_TEST+"%d", payOrderRequest.PayChannelId)). + Set("notify_url", fmt.Sprintf(payCommon.WX_NOTIFY_URL_PROD+"%d", payOrderRequest.PayChannelId)). SetBodyMap("amount", func(bm gopay.BodyMap) { bm.Set("total", payOrderRequest.Amount). Set("currency", "CNY")