Compare commits

...

2 Commits

Author SHA1 Message Date
陈俊宏 6adcce1480 回调接口 2024-08-06 11:19:13 +08:00
陈俊宏 74d1472a43 支付配置 2024-08-06 11:09:39 +08:00
3 changed files with 18 additions and 10 deletions

View File

@ -12,7 +12,6 @@ import (
"github.com/go-pay/gopay" "github.com/go-pay/gopay"
"github.com/go-pay/gopay/alipay" "github.com/go-pay/gopay/alipay"
"github.com/go-pay/gopay/wechat/v3" "github.com/go-pay/gopay/wechat/v3"
"github.com/go-pay/xlog"
"github.com/qit-team/snow-core/log/logger" "github.com/qit-team/snow-core/log/logger"
"strconv" "strconv"
@ -26,6 +25,7 @@ func WxCallback(c *gin.Context) {
logger.Info(c, "WxCallback-回调数据payChannelId", payChannelId) logger.Info(c, "WxCallback-回调数据payChannelId", payChannelId)
if payChannelId == "" { if payChannelId == "" {
c.String(http.StatusBadRequest, "%s", "fail") c.String(http.StatusBadRequest, "%s", "fail")
return
} }
// 查询应用下的支付配置 // 查询应用下的支付配置
@ -36,10 +36,12 @@ func WxCallback(c *gin.Context) {
if code == errorcode.PayChannelNotFound { if code == errorcode.PayChannelNotFound {
logger.Error(c, "AliCallback-回调数据未获取到支付配置404") logger.Error(c, "AliCallback-回调数据未获取到支付配置404")
c.String(http.StatusBadRequest, "%s", "fail") c.String(http.StatusBadRequest, "%s", "fail")
return
} }
if payChannelModel.ChannelType != common.PAY_CHANNEL_WECHAT_H5 { if payChannelModel.ChannelType != common.PAY_CHANNEL_WECHAT_H5 {
logger.Error(c, "WxCallback-回调数据解析支付配置错误,查询的数据不是当前渠道") logger.Error(c, "WxCallback-回调数据解析支付配置错误,查询的数据不是当前渠道")
c.String(http.StatusBadRequest, "%s", "fail") c.String(http.StatusBadRequest, "%s", "fail")
return
} }
var wxConfig paymentService.WxPay var wxConfig paymentService.WxPay
@ -47,22 +49,26 @@ func WxCallback(c *gin.Context) {
if err != nil { if err != nil {
logger.Error(c, "WxCallback-回调数据解析支付配置错误", fmt.Sprintf("错误原因:%s", err.Error())) logger.Error(c, "WxCallback-回调数据解析支付配置错误", fmt.Sprintf("错误原因:%s", err.Error()))
c.String(http.StatusBadRequest, "%s", "fail") c.String(http.StatusBadRequest, "%s", "fail")
return
} }
if wxConfig.ApiV3Key == "" || wxConfig.MchId == "" || wxConfig.PrivateKey == "" || wxConfig.SerialNo == "" { if wxConfig.ApiV3Key == "" || wxConfig.MchId == "" || wxConfig.PrivateKey == "" || wxConfig.SerialNo == "" {
logger.Error(c, "WxCallback-回调数据解析支付配置错误,解析出来的信息为空") logger.Error(c, "WxCallback-回调数据解析支付配置错误,解析出来的信息为空")
c.String(http.StatusBadRequest, "%s", "fail") c.String(http.StatusBadRequest, "%s", "fail")
return
} }
wxConfig.AppId = payChannelModel.AppId wxConfig.AppId = payChannelModel.AppId
notifyReq, err := wechat.V3ParseNotify(c.Request) notifyReq, err := wechat.V3ParseNotify(c.Request)
if err != nil { if err != nil {
logger.Error(c, "WxCallback-回调数据验签失败", err.Error()) logger.Error(c, "WxCallback-回调数据验签失败", err.Error())
c.String(http.StatusBadRequest, "%s", "fail")
return return
} }
err = paymentService.WxPayCallBack(notifyReq, wxConfig) err = paymentService.WxPayCallBack(notifyReq, wxConfig)
if err != nil { if err != nil {
logger.Error(c, "WxCallback-回调执行失败,失败原因:", err.Error()) logger.Error(c, "WxCallback-回调执行失败,失败原因:", err.Error())
c.String(http.StatusBadRequest, "%s", "fail")
return return
} }
@ -70,6 +76,7 @@ func WxCallback(c *gin.Context) {
// 退款通知http应答码为200且返回状态码为SUCCESS才会当做商户接收成功否则会重试。 // 退款通知http应答码为200且返回状态码为SUCCESS才会当做商户接收成功否则会重试。
// 注意:重试过多会导致微信支付端积压过多通知而堵塞,影响其他正常通知。 // 注意:重试过多会导致微信支付端积压过多通知而堵塞,影响其他正常通知。
c.JSON(http.StatusOK, &wechat.V3NotifyRsp{Code: gopay.SUCCESS, Message: "成功"}) c.JSON(http.StatusOK, &wechat.V3NotifyRsp{Code: gopay.SUCCESS, Message: "成功"})
return
} }
// AliCallback 支付宝支付回调 // AliCallback 支付宝支付回调
@ -79,6 +86,7 @@ func AliCallback(c *gin.Context) {
logger.Info(c, "AliCallback-回调数据APPID", payChannelId) logger.Info(c, "AliCallback-回调数据APPID", payChannelId)
if payChannelId == "" { if payChannelId == "" {
c.String(http.StatusBadRequest, "%s", "fail") c.String(http.StatusBadRequest, "%s", "fail")
return
} }
// 查询应用下的支付配置 // 查询应用下的支付配置
var payChannelModel paychannelmodel.PayChannel var payChannelModel paychannelmodel.PayChannel
@ -88,10 +96,12 @@ func AliCallback(c *gin.Context) {
if code == errorcode.PayChannelNotFound { if code == errorcode.PayChannelNotFound {
logger.Error(c, "AliCallback-回调数据未获取到支付配置404") logger.Error(c, "AliCallback-回调数据未获取到支付配置404")
c.String(http.StatusBadRequest, "%s", "fail") c.String(http.StatusBadRequest, "%s", "fail")
return
} }
if payChannelModel.ChannelType != common.PAY_CHANNEL_ALIPAY_WEB { if payChannelModel.ChannelType != common.PAY_CHANNEL_ALIPAY_WEB {
logger.Error(c, "AliCallback-回调数据解析支付配置错误,查询的数据不是当前渠道") logger.Error(c, "AliCallback-回调数据解析支付配置错误,查询的数据不是当前渠道")
c.String(http.StatusBadRequest, "%s", "fail") c.String(http.StatusBadRequest, "%s", "fail")
return
} }
var aliConfig paymentService.AliPay var aliConfig paymentService.AliPay
@ -105,10 +115,12 @@ func AliCallback(c *gin.Context) {
if err != nil { if err != nil {
logger.Error(c, "AliCallback-回调数据解析支付配置错误", fmt.Sprintf("错误原因:%s", err.Error())) logger.Error(c, "AliCallback-回调数据解析支付配置错误", fmt.Sprintf("错误原因:%s", err.Error()))
c.String(http.StatusBadRequest, "%s", "fail") c.String(http.StatusBadRequest, "%s", "fail")
return
} }
if aliConfigModel.AlipayPublicCert == "" || aliConfigModel.PrivateKey == "" || aliConfigModel.AppPublicCert == "" || aliConfigModel.AlipayRootCert == "" { if aliConfigModel.AlipayPublicCert == "" || aliConfigModel.PrivateKey == "" || aliConfigModel.AppPublicCert == "" || aliConfigModel.AlipayRootCert == "" {
logger.Error(c, "AliCallback-回调数据解析支付配置错误,解析出来的信息为空") logger.Error(c, "AliCallback-回调数据解析支付配置错误,解析出来的信息为空")
c.String(http.StatusBadRequest, "%s", "fail") c.String(http.StatusBadRequest, "%s", "fail")
return
} }
aliConfig.AppId = payChannelModel.AppId aliConfig.AppId = payChannelModel.AppId
aliConfig.PrivateKey = aliConfigModel.PrivateKey aliConfig.PrivateKey = aliConfigModel.PrivateKey
@ -118,14 +130,16 @@ func AliCallback(c *gin.Context) {
notifyReq, err := alipay.ParseNotifyToBodyMap(c.Request) // c.Request 是 gin 框架的写法 notifyReq, err := alipay.ParseNotifyToBodyMap(c.Request) // c.Request 是 gin 框架的写法
if err != nil { if err != nil {
xlog.Error(err) c.String(http.StatusBadRequest, "%s", "fail")
return return
} }
err = paymentService.ALiCallBack(notifyReq, aliConfig) err = paymentService.ALiCallBack(notifyReq, aliConfig)
if err != nil { if err != nil {
logger.Error(c, "AliCallback-回调执行失败,失败原因:", err.Error()) logger.Error(c, "AliCallback-回调执行失败,失败原因:", err.Error())
c.String(http.StatusBadRequest, "%s", "fail")
return return
} }
c.String(http.StatusOK, "%s", "success") c.String(http.StatusOK, "%s", "success")
return
} }

View File

@ -1,7 +1,6 @@
package paymentService package paymentService
import ( import (
"PaymentCenter/bootstrap"
"PaymentCenter/config" "PaymentCenter/config"
"context" "context"
"crypto/md5" "crypto/md5"
@ -28,12 +27,7 @@ func TestMarketSendRequest_Market(t *testing.T) {
//加载配置 //加载配置
opts.ConfFile = "../../../.env" opts.ConfFile = "../../../.env"
conf, err := config.Load(opts.ConfFile) _, err := config.Load(opts.ConfFile)
if err != nil {
return
}
//引导程序
err = bootstrap.Bootstrap(conf)
if err != nil { if err != nil {
return return
} }

View File

@ -93,7 +93,7 @@ type CronConfig struct {
} }
type PayService struct { type PayService struct {
Host string `toml:"TestHost"` Host string `toml:"Host"`
IsProd bool `toml:"IsProd"` IsProd bool `toml:"IsProd"`
} }