<feat>网页支付

This commit is contained in:
Rzy 2024-08-05 16:20:01 +08:00
parent b1a1a4cd50
commit 3e7df62375
11 changed files with 132 additions and 26 deletions

View File

@ -45,5 +45,4 @@ func (m *AppRepo) AppFindOne(app *appmodel.App, conn builder.Cond, columns ...st
return nil, sql.ErrNoRows return nil, sql.ErrNoRows
} }
return app, err return app, err
} }

View File

@ -3,6 +3,7 @@ package data
import ( import (
"PaymentCenter/app/http/entities" "PaymentCenter/app/http/entities"
"PaymentCenter/app/models/merchantmodel" "PaymentCenter/app/models/merchantmodel"
"database/sql"
"xorm.io/builder" "xorm.io/builder"
"xorm.io/xorm" "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) { func (m *MerchantRepo) MerchantDetail(merchant *merchantmodel.Merchant, conn builder.Cond) (bool, error) {
return m.repo.Where(conn).Get(merchant) 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
}

View File

@ -192,6 +192,14 @@ func GetRequest(c *gin.Context) interface{} {
return request 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) { func HandRes(c *gin.Context, data interface{}, err error) {
if err == nil { if err == nil {
Success(c, data, "") Success(c, data, "")

View File

@ -1,10 +1,15 @@
package front package front
import ( import (
"fmt" "PaymentCenter/app/http/controllers"
"PaymentCenter/app/http/entities/front"
"PaymentCenter/app/services"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
func WebPay(c *gin.Context) { func WebPay(c *gin.Context) {
fmt.Println("312312312321") req := controllers.GetRequest(c).(*front.PayWebReqs)
appCheckInfo := controllers.GetAppCheckInfo(c).(*services.AppCheck)
services.NewWebPay(req, appCheckInfo)
} }

View File

@ -11,8 +11,11 @@ type RequestBody struct {
Data string `json:"data" validate:"required"` Data string `json:"data" validate:"required"`
} }
type PayWeb struct { type PayWebReqs struct {
ApiCommonBody ApiCommonBody
PayChannel int64 `json:"private_key_path" validate:"required" label:"支付渠道"` PayChannelId int64 `json:"pay_channel_id" validate:"required" label:"支付渠道"`
Delay int64 `json:"delay"` //延时时间 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:"扩展参数"`
} }

View File

@ -4,7 +4,6 @@ import (
"PaymentCenter/app/constants/common" "PaymentCenter/app/constants/common"
"PaymentCenter/app/constants/errorcode" "PaymentCenter/app/constants/errorcode"
"PaymentCenter/app/http/controllers" "PaymentCenter/app/http/controllers"
"PaymentCenter/app/http/entities"
"PaymentCenter/app/http/entities/front" "PaymentCenter/app/http/entities/front"
"PaymentCenter/app/http/requestmapping" "PaymentCenter/app/http/requestmapping"
"PaymentCenter/app/services" "PaymentCenter/app/services"
@ -145,20 +144,13 @@ func ValidatePayRequest() gin.HandlerFunc {
// return // return
//} //}
//获取app信息 //获取app信息
app, errCode := services.AppFindOne(entities.IdRequest{Id: requestDataStruct.AppId}) appCheck := services.AppGetAndCheck(&services.AppCheck{
if errCode != errorcode.Success { AppId: requestDataStruct.AppId,
controllers.ErrWithCode(c, errCode) Ip: c.ClientIP(),
return Code: errorcode.Success,
} })
//检查app可用性 if appCheck.Code != errorcode.Success {
appCheck := services.NewAppCheck(app).Check() controllers.ErrWithCode(c, appCheck.Code)
if appCheck.GetCode() != errorcode.Success {
controllers.ErrWithCode(c, appCheck.GetCode())
return
}
//检查白名单
if !appCheck.IpCheck(c.ClientIP()) {
controllers.ErrWithCode(c, appCheck.GetCode())
return return
} }
//解密 //解密
@ -166,7 +158,7 @@ func ValidatePayRequest() gin.HandlerFunc {
if cryptFunc == nil { if cryptFunc == nil {
controllers.ErrWithCode(c, appCheck.GetCode()) controllers.ErrWithCode(c, appCheck.GetCode())
} }
dataByte, errCode := cryptFunc(app).Decrypt(requestDataStruct.Data) dataByte, errCode := cryptFunc(appCheck.App).Decrypt(requestDataStruct.Data)
if errCode != apicrypt.CryptNotError { if errCode != apicrypt.CryptNotError {
controllers.ErrWithCode(c, errCode) controllers.ErrWithCode(c, errCode)
return return
@ -187,6 +179,7 @@ func ValidatePayRequest() gin.HandlerFunc {
c.Abort() c.Abort()
} }
c.Set("request", dataByte) c.Set("request", dataByte)
c.Set("appCheckInfo", appCheck)
c.Next() c.Next()
} }
} }

View File

@ -6,7 +6,7 @@ import (
) )
var FrontRequestMap = map[string]func() interface{}{ 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{} { func FrontRequest() func() interface{} {

View File

@ -12,8 +12,10 @@ import (
) )
type AppCheck struct { type AppCheck struct {
App *appmodel.App AppId int64
Code int Ip string
App *appmodel.App
Code int
} }
func NewAppCheck(app *appmodel.App) *AppCheck { func NewAppCheck(app *appmodel.App) *AppCheck {

View File

@ -65,7 +65,7 @@ func AppUpdate(app *appmodel.App) (code int) {
} else { } else {
_, err = repo.AppUpdate(app, conn) _, err = repo.AppUpdate(app, conn)
} }
code = handErr(err) code = handErr(err)
return return
} }
@ -99,3 +99,23 @@ func AppFindOne(req entities.IdRequest, col ...string) (row *appmodel.App, code
} }
return row, errorcode.Success 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
}

View File

@ -1,10 +1,12 @@
package services package services
import ( import (
"PaymentCenter/app/constants/errorcode"
"PaymentCenter/app/data" "PaymentCenter/app/data"
"PaymentCenter/app/http/entities" "PaymentCenter/app/http/entities"
"PaymentCenter/app/http/entities/backend" "PaymentCenter/app/http/entities/backend"
"PaymentCenter/app/models/merchantmodel" "PaymentCenter/app/models/merchantmodel"
"database/sql"
"strings" "strings"
"xorm.io/builder" "xorm.io/builder"
) )
@ -65,3 +67,27 @@ func MerchantDelete(req entities.IdRequest) (code int) {
code = handErr(err) code = handErr(err)
return 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
}

41
app/services/web_pay.go Normal file
View File

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