From 3e7df62375b55356422e5933fecc984a3412ac96 Mon Sep 17 00:00:00 2001 From: Rzy <465386466@qq.com> Date: Mon, 5 Aug 2024 16:20:01 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BD=91=E9=A1=B5=E6=94=AF=E4=BB=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/data/app.go | 1 - app/data/merchat.go | 9 +++++++ app/http/controllers/base.go | 8 ++++++ app/http/controllers/front/api.go | 9 +++++-- app/http/entities/front/pay.go | 9 ++++--- app/http/middlewares/base.go | 25 +++++++------------ app/http/requestmapping/front.go | 2 +- app/services/api_request_valid.go | 6 +++-- app/services/app.go | 22 ++++++++++++++++- app/services/merchant.go | 26 ++++++++++++++++++++ app/services/web_pay.go | 41 +++++++++++++++++++++++++++++++ 11 files changed, 132 insertions(+), 26 deletions(-) create mode 100644 app/services/web_pay.go diff --git a/app/data/app.go b/app/data/app.go index f355d0f..475d0ce 100644 --- a/app/data/app.go +++ b/app/data/app.go @@ -45,5 +45,4 @@ func (m *AppRepo) AppFindOne(app *appmodel.App, conn builder.Cond, columns ...st return nil, sql.ErrNoRows } return app, err - } diff --git a/app/data/merchat.go b/app/data/merchat.go index 763758c..3b85a0b 100644 --- a/app/data/merchat.go +++ b/app/data/merchat.go @@ -3,6 +3,7 @@ package data import ( "PaymentCenter/app/http/entities" "PaymentCenter/app/models/merchantmodel" + "database/sql" "xorm.io/builder" "xorm.io/xorm" ) @@ -41,3 +42,11 @@ func (m *MerchantRepo) MerchantUpdate(merchant *merchantmodel.Merchant, conn bui func (m *MerchantRepo) MerchantDetail(merchant *merchantmodel.Merchant, conn builder.Cond) (bool, error) { return m.repo.Where(conn).Get(merchant) } + +func (m *MerchantRepo) MerchantFindOne(merchant *merchantmodel.Merchant, conn builder.Cond, columns ...string) (*merchantmodel.Merchant, error) { + has, err := m.repo.Where(conn).Get(merchant) + if !has { + return nil, sql.ErrNoRows + } + return merchant, err +} diff --git a/app/http/controllers/base.go b/app/http/controllers/base.go index 92fd2d6..03afeee 100644 --- a/app/http/controllers/base.go +++ b/app/http/controllers/base.go @@ -192,6 +192,14 @@ func GetRequest(c *gin.Context) interface{} { return request } +func GetAppCheckInfo(c *gin.Context) interface{} { + request, exists := c.Get("appCheckInfo") + if !exists { + return nil + } + return request +} + func HandRes(c *gin.Context, data interface{}, err error) { if err == nil { Success(c, data, "") diff --git a/app/http/controllers/front/api.go b/app/http/controllers/front/api.go index 15942d1..48f709d 100644 --- a/app/http/controllers/front/api.go +++ b/app/http/controllers/front/api.go @@ -1,10 +1,15 @@ package front import ( - "fmt" + "PaymentCenter/app/http/controllers" + "PaymentCenter/app/http/entities/front" + "PaymentCenter/app/services" "github.com/gin-gonic/gin" ) func WebPay(c *gin.Context) { - fmt.Println("312312312321") + req := controllers.GetRequest(c).(*front.PayWebReqs) + appCheckInfo := controllers.GetAppCheckInfo(c).(*services.AppCheck) + services.NewWebPay(req, appCheckInfo) + } diff --git a/app/http/entities/front/pay.go b/app/http/entities/front/pay.go index 67daf3b..bf63994 100644 --- a/app/http/entities/front/pay.go +++ b/app/http/entities/front/pay.go @@ -11,8 +11,11 @@ type RequestBody struct { Data string `json:"data" validate:"required"` } -type PayWeb struct { +type PayWebReqs struct { ApiCommonBody - PayChannel int64 `json:"private_key_path" validate:"required" label:"支付渠道"` - Delay int64 `json:"delay"` //延时时间 + PayChannelId int64 `json:"pay_channel_id" validate:"required" label:"支付渠道"` + OutTradeNo string `json:"out_trade_no" validate:"required" label:"外侧商户订单号"` + OrderType int32 `json:"order_type" validate:"required" label:"订单类型,支付,退款"` + Amount int64 `json:"amount" validate:"required" label:"支付金额,单位分"` + ExtJson string `json:"ext_json" validate:"required" label:"扩展参数"` } diff --git a/app/http/middlewares/base.go b/app/http/middlewares/base.go index c31ed96..220a59a 100644 --- a/app/http/middlewares/base.go +++ b/app/http/middlewares/base.go @@ -4,7 +4,6 @@ import ( "PaymentCenter/app/constants/common" "PaymentCenter/app/constants/errorcode" "PaymentCenter/app/http/controllers" - "PaymentCenter/app/http/entities" "PaymentCenter/app/http/entities/front" "PaymentCenter/app/http/requestmapping" "PaymentCenter/app/services" @@ -145,20 +144,13 @@ func ValidatePayRequest() gin.HandlerFunc { // return //} //获取app信息 - app, errCode := services.AppFindOne(entities.IdRequest{Id: requestDataStruct.AppId}) - if errCode != errorcode.Success { - controllers.ErrWithCode(c, errCode) - return - } - //检查app可用性 - appCheck := services.NewAppCheck(app).Check() - if appCheck.GetCode() != errorcode.Success { - controllers.ErrWithCode(c, appCheck.GetCode()) - return - } - //检查白名单 - if !appCheck.IpCheck(c.ClientIP()) { - controllers.ErrWithCode(c, appCheck.GetCode()) + appCheck := services.AppGetAndCheck(&services.AppCheck{ + AppId: requestDataStruct.AppId, + Ip: c.ClientIP(), + Code: errorcode.Success, + }) + if appCheck.Code != errorcode.Success { + controllers.ErrWithCode(c, appCheck.Code) return } //解密 @@ -166,7 +158,7 @@ func ValidatePayRequest() gin.HandlerFunc { if cryptFunc == nil { controllers.ErrWithCode(c, appCheck.GetCode()) } - dataByte, errCode := cryptFunc(app).Decrypt(requestDataStruct.Data) + dataByte, errCode := cryptFunc(appCheck.App).Decrypt(requestDataStruct.Data) if errCode != apicrypt.CryptNotError { controllers.ErrWithCode(c, errCode) return @@ -187,6 +179,7 @@ func ValidatePayRequest() gin.HandlerFunc { c.Abort() } c.Set("request", dataByte) + c.Set("appCheckInfo", appCheck) c.Next() } } diff --git a/app/http/requestmapping/front.go b/app/http/requestmapping/front.go index 9aa8e13..79e6f2c 100644 --- a/app/http/requestmapping/front.go +++ b/app/http/requestmapping/front.go @@ -6,7 +6,7 @@ import ( ) var FrontRequestMap = map[string]func() interface{}{ - common.FRONT_V1 + "/pay/web": func() interface{} { return new(front.PayWeb) }, + common.FRONT_V1 + "/pay/web": func() interface{} { return new(front.PayWebResp) }, } func FrontRequest() func() interface{} { diff --git a/app/services/api_request_valid.go b/app/services/api_request_valid.go index 0c6ed47..4a8a251 100644 --- a/app/services/api_request_valid.go +++ b/app/services/api_request_valid.go @@ -12,8 +12,10 @@ import ( ) type AppCheck struct { - App *appmodel.App - Code int + AppId int64 + Ip string + App *appmodel.App + Code int } func NewAppCheck(app *appmodel.App) *AppCheck { diff --git a/app/services/app.go b/app/services/app.go index a161494..88ce3d3 100644 --- a/app/services/app.go +++ b/app/services/app.go @@ -65,7 +65,7 @@ func AppUpdate(app *appmodel.App) (code int) { } else { _, err = repo.AppUpdate(app, conn) } - + code = handErr(err) return } @@ -99,3 +99,23 @@ func AppFindOne(req entities.IdRequest, col ...string) (row *appmodel.App, code } return row, errorcode.Success } + +func AppGetAndCheck(appCheckIn *AppCheck) (appCheckOut *AppCheck) { + errCode := errorcode.Success + appCheckOut.App, errCode = AppFindOne(entities.IdRequest{Id: appCheckIn.AppId}) + if errCode != errorcode.Success { + appCheckOut.Code = errCode + return + } + //检查app可用性 + appCheckOut = appCheckIn.Check() + if appCheckOut.GetCode() != errorcode.Success { + return + } + //检查白名单 + if appCheckIn.Ip != "" && !appCheckOut.IpCheck(appCheckIn.Ip) { + return + } + + return +} diff --git a/app/services/merchant.go b/app/services/merchant.go index 99d80f1..d8f98c3 100644 --- a/app/services/merchant.go +++ b/app/services/merchant.go @@ -1,10 +1,12 @@ package services import ( + "PaymentCenter/app/constants/errorcode" "PaymentCenter/app/data" "PaymentCenter/app/http/entities" "PaymentCenter/app/http/entities/backend" "PaymentCenter/app/models/merchantmodel" + "database/sql" "strings" "xorm.io/builder" ) @@ -65,3 +67,27 @@ func MerchantDelete(req entities.IdRequest) (code int) { code = handErr(err) return } + +func MerchantFindOne(merchant *merchantmodel.Merchant, conn builder.Cond, col ...string) (merchantInfo *merchantmodel.Merchant, code int) { + repo := data.NewMerchantRepo(merchantmodel.GetInstance().GetDb()) + // 拼接查询条件 + merchantInfo, err := repo.MerchantFindOne(merchant, conn, col...) + if err != nil { + if err == sql.ErrNoRows { + return nil, errorcode.MerchantNotFound + } + return nil, errorcode.SystemError + } + return merchantInfo, errorcode.Success +} + +func GetAndCheckMerchant(merchant *merchantmodel.Merchant, conn builder.Cond, col ...string) (code int) { + merchantInfo, code := MerchantFindOne(merchant, conn, col...) + if code != errorcode.Success { + return code + } + if merchantInfo.DeleteTime.Location() != nil { + return errorcode.MerchantNotFound + } + return +} diff --git a/app/services/web_pay.go b/app/services/web_pay.go new file mode 100644 index 0000000..ab1c9d7 --- /dev/null +++ b/app/services/web_pay.go @@ -0,0 +1,41 @@ +package services + +import ( + "PaymentCenter/app/constants/errorcode" + "PaymentCenter/app/data" + "PaymentCenter/app/http/entities/front" + "PaymentCenter/app/models/merchantmodel" +) + +type WebPay struct { + WebPayReqs *front.PayWebReqs + AppCheck *AppCheck + Order *data.OrderRepo + PayCode int +} + +func NewWebPay(resp *front.PayWebReqs, appCheck *AppCheck) *WebPay { + if appCheck == nil { + appCheck = AppGetAndCheck(&AppCheck{ + AppId: resp.AppId, + Code: errorcode.Success, + }) + } + return &WebPay{ + WebPayReqs: resp, + AppCheck: appCheck, + PayCode: appCheck.Code, + } +} + +func (w *WebPay) AddPayLog() { + w.PayCode = GetAndCheckMerchant(&merchantmodel.Merchant{Id: w.AppCheck.App.MerchantId}, nil) +} + +func (w *WebPay) CheckMerchant() { + w.PayCode = GetAndCheckMerchant(&merchantmodel.Merchant{Id: w.AppCheck.App.MerchantId}, nil) +} + +func (w *WebPay) CheckPayChannel() { + w.PayCode = GetAndCheckMerchant(&merchantmodel.Merchant{Id: w.AppCheck.App.MerchantId}, nil) +}