commit 59a9da1f7b8f877d8fa74f15006c0d84e56b5813 Author: elf <360197197@qq.com> Date: Sun Dec 10 22:27:04 2023 +0800 initial diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/jypay.iml b/.idea/jypay.iml new file mode 100644 index 0000000..5e764c4 --- /dev/null +++ b/.idea/jypay.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..f721125 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/api/index.go b/api/index.go new file mode 100644 index 0000000..c128ece --- /dev/null +++ b/api/index.go @@ -0,0 +1,189 @@ +package api + +import ( + "bytes" + "encoding/json" + "fmt" + "github.com/gin-gonic/gin" + "github.com/smartwalle/alipay/v3" + "io" + "jypay/errors" + "jypay/global" + "jypay/model" + "jypay/request" + "jypay/utils" + "log" + "net/http" + "strconv" + "time" +) + +func Index(c *gin.Context) (utils.Data, error) { + return utils.Data{"hello": "elf"}, nil +} + +func Error(c *gin.Context) (utils.Data, error) { + return nil, errors.NewBusinessError("测试", "1122") +} + +func Pay(c *gin.Context) (utils.Data, error) { + req := request.OrderRequest{} + err := c.BindJSON(&req) + if err != nil { + fmt.Printf("bind json err:%v", err) + return nil, errors.NewBusinessError("参数错误") + } + + order, err := saveOrder(req) + if err != nil { + return nil, err + } + + var param = alipay.TradeWapPay{} + param.OutTradeNo = order.OrderNo + param.TotalAmount = strconv.FormatFloat(order.PayAmount, 'E', -1, 64) + param.Subject = order.Subject + param.NotifyURL = global.Config.Server.Domain + "/notify" + param.ReturnURL = global.Config.Server.Domain + "/return" + client, err := alipay.New(global.Config.Alipay.AppId, global.Config.Alipay.PrivateKey, true) + if err != nil { + return nil, errors.NewBusinessError("初始化支付宝失败") + } + result, err := client.TradeWapPay(param) + if err != nil { + return nil, errors.NewBusinessError("请求支付宝失败:" + err.Error()) + } + fmt.Println(result.String()) + data := utils.Data{ + "pay_url": result.String(), + } + return data, nil +} + +func saveOrder(req request.OrderRequest) (*model.Order, error) { + if req.OrderNo == "" { + return nil, errors.NewBusinessError("订单号[orderNo]不能为空") + } + + order := model.Order{} + global.DB.Where("order_no = ?", req.OrderNo).First(&order) + + order.OrderNo = req.OrderNo + order.UserId = req.UserId + order.UserAccount = req.UserAccount + order.GameId = req.GameId + order.GameName = req.GameName + order.ServerId = req.ServerId + order.ServerName = req.ServerName + order.Subject = req.Subject + order.RoleId = req.RoleId + order.RoleName = req.RoleName + order.PayAmount = float64(req.PayAmount) / 100 + order.NotifyUrl = req.NotifyUrl + order.ReturnUrl = req.ReturnUrl + order.PromoteId = req.PromoteId + order.PromoteAccount = req.PromoteAccount + order.PayStatus = 0 + + result := global.DB.Create(&order) + if result.Error != nil { + return nil, errors.NewBusinessError("数据异常") + } + + return &order, nil +} + +func Notify(c *gin.Context) { + err := c.Request.ParseForm() + if err != nil { + log.Printf("参数错误:%v", err) + return + } + client, err := alipay.New(global.Config.Alipay.AppId, global.Config.Alipay.PrivateKey, true) + if err != nil { + log.Printf("初始化支付宝失败:%v", err) + return + } + notification, err := client.DecodeNotification(c.Request.Form) + if err != nil { + log.Printf("解析异步通知发生错误:%v", err) + return + } + order := model.Order{} + tx := global.DB.Where("order_no = ?", notification.OutTradeNo).First(&order) + if tx.Error != nil { + log.Printf("订单不存在:%s", notification.OutTradeNo) + return + } + now := time.Now() + if notification.TradeStatus == "TRADE_SUCCESS" || notification.TradeStatus == "TRADE_FINISHED" { + order.PayStatus = 1 + order.PayTime = &now + tx := global.DB.Save(order) + if tx.Error != nil { + log.Printf("更新订单失败:%s", notification.OutTradeNo) + return + } + } + notifyData := make(map[string]interface{}) + notifyData["trade_no"] = notification.TradeNo + notifyData["out_trade_no"] = notification.OutTradeNo + notifyData["trade_status"] = notification.TradeStatus + notifyData["total_amount"] = notification.TotalAmount + notifyResult := postToSdk(order.NotifyUrl, notifyData) + if notifyResult == "success" { + client.ACKNotification(c.Writer) + } +} + +func postToSdk(url string, data map[string]interface{}) string { + content, err := json.Marshal(data) + if err != nil { + log.Fatalf("无法编码JSON数据:%v", err) + return "fail" + } + + client := &http.Client{} + + req, err := http.NewRequest("POST", url, bytes.NewBuffer(content)) + if err != nil { + log.Fatalf("无法创建新的请求:%v", err) + return "fail" + } + + req.Header.Set("Content-Type", "application/json") + + resp, err := client.Do(req) + if err != nil { + log.Fatalf("无法发送POST请求:%v", err) + return "fail" + } + defer resp.Body.Close() + + body, err := io.ReadAll(resp.Body) + if err != nil { + log.Fatalf("无法读取响应体内容:%v", err) + return "fail" + } + + return string(body) +} + +func Return(c *gin.Context) { + var req request.ReturnRequest + // 将路由参数绑定到结构体中 + if err := c.ShouldBindUri(&req); err != nil { + c.Writer.WriteHeader(http.StatusOK) + c.Writer.Write([]byte("参数错误")) + return + } + order := model.Order{} + tx := global.DB.Where("order_no = ?", req.OrderNo).First(&order) + if tx.Error != nil { + c.Writer.WriteHeader(http.StatusOK) + c.Writer.Write([]byte("订单不存在")) + return + } + + c.Redirect(http.StatusFound, order.ReturnUrl) +} diff --git a/config.yaml b/config.yaml new file mode 100644 index 0000000..19f7ddf --- /dev/null +++ b/config.yaml @@ -0,0 +1,12 @@ +server: + port: 9090 + domain: "" +alipay: + app-id: "2021003176607480" + private-key: "MIIEpAIBAAKCAQEApYCmDarjoizjH++KYs0QztdXSnBMSAKwPbCI98ma/soren7rBBEqSS7E6pHxGAoH9FgjugaYJQLT5D+a/qwko7E/swf59RZM7VjbAfQVaSPPPnl2rRzVC8hAVGsTXRZB3dgeZlXgS4QecjxtiqoejtLf8ipXJHGbwu3avvIJ9FnF6BwoZrbiS7OeijuNmiu0kjRecCq28tRMsVZL/d1eYKV38GiWEsIKl533JkdAOTkec/4hmBeyZRCBzwAwHwvEZmXEf82i9vlLlSD6A2LMkKYWOlI+3CVZ52YcYWmgyad5FA61fHt9mqqtkC71Aot0uzHCwaeljpp5tgjx/j7xfQIDAQABAoIBAA7AHYygS3N7zq5c+hd+MV5OAQCoa5QLcUu+PSbgeAj3WdKsFyQgp4UoPvKtGTDMTBMx+9Urm0nJK7tvE9BY5VcViBjbwCJg33BT+Aj/iRz6zRDDYJljNXW8Xkdn16z3O92a0nLKepggaVpDGnw+V0ThdIYv0d50pa5E5rcX+V0mseR2kjUw44tHtJc7odcrz2CSog6+1Zpfa+RN1FpVtY7GM1rx+Y3DbdXaAr0RGUvp26j0k/OB3oBUuTZF+nhAqSpC6x2cUowYEwz7P0QUJ8YgxoXZGYRqn+INNtrC0UF8sku5w9zly/HBlbOHpQsCHuoSRXCGyCRJjJIJ5Pr3br0CgYEA9T3hG32WhA2KtsZkM08cmpZb7usJLqsLPWzwwV7CI/hrgPpcRuk2jCyR37gLgfMq5hwG0fqmz2yf83kDVAakP6M6f6fZAcBTITJ6heTmcGAHeLkP0/6llkzxBDdn29mDoSAf4bXrTAhlUpkoPBom8325pBMoX73UPGp72p91bUMCgYEArMNHr/Yu19CI5QGsRDZjLwEl8J7+itA4omkBXY8bO5z2VQ2+n7rV+bG/rFrpjLQZ5aXtA/bFQV4RNMBZp7XquL71NltrtCw8DSZJLTDgCB18iEPI/2BGpJZVUrgzHnctMD99awqOOnG6LlfIl76/q7N9fQIy+4Ya1XwBMO2a2j8CgYEAxMoz0ah0WGC5d7M5AocwD8gp200VIDK2oULC6phMNysHsQa1d116nP7Cy3/XFB75xI6m1mZI5tdmfqwThKCTulEUBw28MBCRZGkqMjkcxP354OC3l8iWPUZefKCtIZiobGnny2n8dxWhoKcvxxLBQNAZYFTTKAievyA/ZI5iXxsCgYAJAfGF6icNF5Fbp1etAszvEXdB8XhCEpMxXJo2n5SC6i8pxhqvR1WKvErLGL5emLFU+O6/qykjvFzXTEQga2q+kS5F1ERsU3YaFWnxUkAMDEWG8UnUjteAE1qFgpcfuoT/o//NCfLadz2G1wrdZpOlxyj+0JOJ67cQdHO3clwFjwKBgQDB0IQrdEScRiVx9lLSAj85o6gwzrTUUjYTf7rpQqjMTF72co9aBxemKxkbGfdFwTUnsZXBbP0TbV2xH/8XrhFjZO0aCagww4S5GkdFpaWZcjL1h2fiH2NmGh5NyXub8gewaJ9n1ZMStijgson72wVHVLryoyBF/TSGYhcbDluyTg==" +database: + host: "47.98.244.18" + port: "3306" + username: "liaojinling" + password: "Ljl@3115316" + dbname: "juyou" \ No newline at end of file diff --git a/config/alipay.go b/config/alipay.go new file mode 100644 index 0000000..f70d7d6 --- /dev/null +++ b/config/alipay.go @@ -0,0 +1,6 @@ +package config + +type Alipay struct { + AppId string `yaml:"app-id"` + PrivateKey string `yaml:"private-key"` +} diff --git a/config/config.go b/config/config.go new file mode 100644 index 0000000..2be0245 --- /dev/null +++ b/config/config.go @@ -0,0 +1,7 @@ +package config + +type Config struct { + Server Server + Database Database + Alipay Alipay +} diff --git a/config/database.go b/config/database.go new file mode 100644 index 0000000..a4f5225 --- /dev/null +++ b/config/database.go @@ -0,0 +1,16 @@ +package config + +type Database struct { + Host string + Port string + Username string + Password string + Dbname string +} + +func (d *Database) GetDsn() string { + if d.Port == "" { + d.Port = "3306" + } + return d.Username + ":" + d.Password + "@tcp(" + d.Host + ":" + d.Port + ")/" + d.Dbname + "?charset=utf8mb4&parseTime=True&loc=Local" +} diff --git a/config/server.go b/config/server.go new file mode 100644 index 0000000..a2fdfc8 --- /dev/null +++ b/config/server.go @@ -0,0 +1,6 @@ +package config + +type Server struct { + Port string + Domain string +} diff --git a/errors/business_error.go b/errors/business_error.go new file mode 100644 index 0000000..632a024 --- /dev/null +++ b/errors/business_error.go @@ -0,0 +1,25 @@ +package errors + +type BusinessError struct { + Code string + Message string +} + +func (e *BusinessError) Error() string { + return e.Message +} + +func NewBusinessError(message ...string) *BusinessError { + err := &BusinessError{ + "1000", + "业务错误", + } + if len(message) == 0 { + return err + } + err.Message = message[0] + if len(message) > 1 { + err.Code = message[1] + } + return err +} diff --git a/global/global.go b/global/global.go new file mode 100644 index 0000000..801349b --- /dev/null +++ b/global/global.go @@ -0,0 +1,9 @@ +package global + +import ( + "gorm.io/gorm" + "jypay/config" +) + +var Config *config.Config +var DB *gorm.DB diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..b2d67a8 --- /dev/null +++ b/go.mod @@ -0,0 +1,42 @@ +module jypay + +go 1.21.1 + +require ( + github.com/bytedance/sonic v1.10.2 // indirect + github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect + github.com/chenzhuoyu/iasm v0.9.1 // indirect + github.com/gabriel-vasile/mimetype v1.4.3 // indirect + github.com/gin-contrib/sse v0.1.0 // indirect + github.com/gin-gonic/gin v1.9.1 // indirect + github.com/go-playground/locales v0.14.1 // indirect + github.com/go-playground/universal-translator v0.18.1 // indirect + github.com/go-playground/validator/v10 v10.16.0 // indirect + github.com/go-sql-driver/mysql v1.7.0 // indirect + github.com/goccy/go-json v0.10.2 // indirect + github.com/jinzhu/inflection v1.0.0 // indirect + github.com/jinzhu/now v1.1.5 // indirect + github.com/json-iterator/go v1.1.12 // indirect + github.com/klauspost/cpuid/v2 v2.2.6 // indirect + github.com/leodido/go-urn v1.2.4 // indirect + github.com/mattn/go-isatty v0.0.20 // indirect + github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect + github.com/modern-go/reflect2 v1.0.2 // indirect + github.com/pelletier/go-toml/v2 v2.1.0 // indirect + github.com/smartwalle/alipay/v3 v3.2.20 // indirect + github.com/smartwalle/ncrypto v1.0.4 // indirect + github.com/smartwalle/ngx v1.0.9 // indirect + github.com/smartwalle/nsign v1.0.9 // indirect + github.com/twitchyliquid64/golang-asm v0.15.1 // indirect + github.com/ugorji/go/codec v1.2.12 // indirect + golang.org/x/arch v0.6.0 // indirect + golang.org/x/crypto v0.16.0 // indirect + golang.org/x/net v0.19.0 // indirect + golang.org/x/sys v0.15.0 // indirect + golang.org/x/text v0.14.0 // indirect + google.golang.org/protobuf v1.31.0 // indirect + gopkg.in/yaml.v2 v2.4.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect + gorm.io/driver/mysql v1.5.2 // indirect + gorm.io/gorm v1.25.5 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..342a826 --- /dev/null +++ b/go.sum @@ -0,0 +1,106 @@ +github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM= +github.com/bytedance/sonic v1.10.0-rc/go.mod h1:ElCzW+ufi8qKqNW0FY314xriJhyJhuoJ3gFZdAHF7NM= +github.com/bytedance/sonic v1.10.2 h1:GQebETVBxYB7JGWJtLBi07OVzWwt+8dWA00gEVW2ZFE= +github.com/bytedance/sonic v1.10.2/go.mod h1:iZcSUejdk5aukTND/Eu/ivjQuEL0Cu9/rf50Hi0u/g4= +github.com/chenzhuoyu/base64x v0.0.0-20211019084208-fb5309c8db06/go.mod h1:DH46F32mSOjUmXrMHnKwZdA8wcEefY7UVqBKYGjpdQY= +github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311/go.mod h1:b583jCggY9gE99b6G5LEC39OIiVsWj+R97kbl5odCEk= +github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d h1:77cEq6EriyTZ0g/qfRdp61a3Uu/AWrgIq2s0ClJV1g0= +github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d/go.mod h1:8EPpVsBuRksnlj1mLy4AWzRNQYxauNi62uWcE3to6eA= +github.com/chenzhuoyu/iasm v0.9.0/go.mod h1:Xjy2NpN3h7aUqeqM+woSuuvxmIe6+DDsiNLIrkAmYog= +github.com/chenzhuoyu/iasm v0.9.1 h1:tUHQJXo3NhBqw6s33wkGn9SP3bvrWLdlVIJ3hQBL7P0= +github.com/chenzhuoyu/iasm v0.9.1/go.mod h1:Xjy2NpN3h7aUqeqM+woSuuvxmIe6+DDsiNLIrkAmYog= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0= +github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk= +github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= +github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= +github.com/gin-gonic/gin v1.9.1 h1:4idEAncQnU5cB7BeOkPtxjfCSye0AAm1R0RVIqJ+Jmg= +github.com/gin-gonic/gin v1.9.1/go.mod h1:hPrL7YrpYKXt5YId3A/Tnip5kqbEAP+KLuI3SUcPTeU= +github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA= +github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY= +github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY= +github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY= +github.com/go-playground/validator/v10 v10.16.0 h1:x+plE831WK4vaKHO/jpgUGsvLKIqRRkz6M78GuJAfGE= +github.com/go-playground/validator/v10 v10.16.0/go.mod h1:9iXMNT7sEkjXb0I+enO7QXmzG6QCsPWY4zveKFVRSyU= +github.com/go-sql-driver/mysql v1.7.0 h1:ueSltNNllEqE3qcWBTD0iQd3IpL/6U+mJxLkazJ7YPc= +github.com/go-sql-driver/mysql v1.7.0/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI= +github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU= +github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= +github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= +github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= +github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= +github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ= +github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= +github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= +github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= +github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= +github.com/klauspost/cpuid/v2 v2.2.6 h1:ndNyv040zDGIDh8thGkXYjnFtiN02M1PVVF+JE/48xc= +github.com/klauspost/cpuid/v2 v2.2.6/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws= +github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M= +github.com/leodido/go-urn v1.2.4 h1:XlAE/cm/ms7TE/VMVoduSpNBoyc2dOxHs5MZSwAN63Q= +github.com/leodido/go-urn v1.2.4/go.mod h1:7ZrI8mTSeBSHl/UaRyKQW1qZeMgak41ANeCNaVckg+4= +github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= +github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M= +github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= +github.com/pelletier/go-toml/v2 v2.1.0 h1:FnwAJ4oYMvbT/34k9zzHuZNrhlz48GB3/s6at6/MHO4= +github.com/pelletier/go-toml/v2 v2.1.0/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/smartwalle/alipay/v3 v3.2.20 h1:IjpG3YYgUgzCfS0z/EHlUbbr0OlrmOBHUst/3FzToYE= +github.com/smartwalle/alipay/v3 v3.2.20/go.mod h1:KWg91KsY+eIOf26ZfZeH7bed1bWulGpGrL1ErHF3jWo= +github.com/smartwalle/ncrypto v1.0.4 h1:P2rqQxDepJwgeO5ShoC+wGcK2wNJDmcdBOWAksuIgx8= +github.com/smartwalle/ncrypto v1.0.4/go.mod h1:Dwlp6sfeNaPMnOxMNayMTacvC5JGEVln3CVdiVDgbBk= +github.com/smartwalle/ngx v1.0.9 h1:pUXDvWRZJIHVrCKA1uZ15YwNti+5P4GuJGbpJ4WvpMw= +github.com/smartwalle/ngx v1.0.9/go.mod h1:mx/nz2Pk5j+RBs7t6u6k22MPiBG/8CtOMpCnALIG8Y0= +github.com/smartwalle/nsign v1.0.9 h1:8poAgG7zBd8HkZy9RQDwasC6XZvJpDGQWSjzL2FZL6E= +github.com/smartwalle/nsign v1.0.9/go.mod h1:eY6I4CJlyNdVMP+t6z1H6Jpd4m5/V+8xi44ufSTxXgc= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI= +github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08= +github.com/ugorji/go/codec v1.2.12 h1:9LC83zGrHhuUA9l16C9AHXAqEV/2wBQ4nkvumAE65EE= +github.com/ugorji/go/codec v1.2.12/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg= +golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8= +golang.org/x/arch v0.6.0 h1:S0JTfE48HbRj80+4tbvZDYsJ3tGv6BUU3XxyZ7CirAc= +golang.org/x/arch v0.6.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys= +golang.org/x/crypto v0.16.0 h1:mMMrFzRSCF0GvB7Ne27XVtVAaXLrPmgPC7/v0tkwHaY= +golang.org/x/crypto v0.16.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4= +golang.org/x/net v0.19.0 h1:zTwKpTd2XuCqf8huc7Fo2iSy+4RHPd10s4KzeTnVr1c= +golang.org/x/net v0.19.0/go.mod h1:CfAk/cbD4CthTvqiEl8NpboMuiuOYsAr/7NOjZJtv1U= +golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc= +golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= +golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= +google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8= +google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= +gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gorm.io/driver/mysql v1.5.2 h1:QC2HRskSE75wBuOxe0+iCkyJZ+RqpudsQtqkp+IMuXs= +gorm.io/driver/mysql v1.5.2/go.mod h1:pQLhh1Ut/WUAySdTHwBpBv6+JKcj+ua4ZFx1QQTBzb8= +gorm.io/gorm v1.25.2-0.20230530020048-26663ab9bf55/go.mod h1:L4uxeKpfBml98NYqVqwAdmV1a2nBtAec/cf3fpucW/k= +gorm.io/gorm v1.25.5 h1:zR9lOiiYf09VNh5Q1gphfyia1JpiClIWG9hQaxB/mls= +gorm.io/gorm v1.25.5/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8= +nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50= +rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4= diff --git a/initialize/initialize.go b/initialize/initialize.go new file mode 100644 index 0000000..1ccb078 --- /dev/null +++ b/initialize/initialize.go @@ -0,0 +1,35 @@ +package initialize + +import ( + "fmt" + "gopkg.in/yaml.v3" + "gorm.io/driver/mysql" + "gorm.io/gorm" + "jypay/config" + "os" +) + +const FilePath = "./config.yaml" + +func InitConfig() *config.Config { + content, err := os.ReadFile(FilePath) + if err != nil { + fmt.Printf("err: %v\n", err) + panic(err) + } + var conf config.Config + if err := yaml.Unmarshal(content, &conf); err != nil { + fmt.Printf("err: %v\n", err) + panic(err) + } + + return &conf +} + +func InitDB(conf *config.Config) *gorm.DB { + db, err := gorm.Open(mysql.Open(conf.Database.GetDsn()), &gorm.Config{}) + if err != nil { + panic(err) + } + return db +} diff --git a/main.go b/main.go new file mode 100644 index 0000000..167dccd --- /dev/null +++ b/main.go @@ -0,0 +1,24 @@ +package main + +import ( + "fmt" + "github.com/gin-gonic/gin" + "jypay/api" + "jypay/global" + "jypay/initialize" + "jypay/utils" +) + +func main() { + global.Config = initialize.InitConfig() + global.DB = initialize.InitDB(global.Config) + fmt.Println(global.Config) + r := gin.Default() + r.GET("/ping", utils.BuildJsonHandler(api.Index)) + r.GET("/error", utils.BuildJsonHandler(api.Error)) + r.POST("/pay", utils.BuildJsonHandler(api.Pay)) + r.POST("/notify", utils.BuildHandler(api.Notify)) + r.GET("/return/:order_no", utils.BuildHandler(api.Return)) + err := r.Run() // listen and serve on 0.0.0.0:8080 + panic(err) +} diff --git a/model/order.go b/model/order.go new file mode 100644 index 0000000..361de3e --- /dev/null +++ b/model/order.go @@ -0,0 +1,33 @@ +package model + +import ( + "time" +) + +type Order struct { + ID uint + OrderNo string + UserId int + UserAccount string + RoleName string + RoleId string + GameId int + GameName string + ServerId string + ServerName string + PayAmount float64 + PayStatus int8 + PayUrl string + NotifyUrl string + ReturnUrl string + Subject string + PromoteId int + PromoteAccount string + PayTime *time.Time + CreatedAt time.Time + UpdatedAt time.Time +} + +func (Order) TableName() string { + return "outer_orders" +} diff --git a/request/OrderRequest.go b/request/OrderRequest.go new file mode 100644 index 0000000..e0ac450 --- /dev/null +++ b/request/OrderRequest.go @@ -0,0 +1,19 @@ +package request + +type OrderRequest struct { + OrderNo string `json:"order_no"` + UserId int `json:"user_id"` + UserAccount string `json:"user_account"` + GameId int `json:"game_id"` + GameName string `json:"game_name"` + ServerId string `json:"server_id"` + ServerName string `json:"server_name"` + Subject string `json:"subject"` + RoleId string `json:"role_id"` + RoleName string `json:"role_name"` + PayAmount int `json:"pay_amount"` + NotifyUrl string `json:"notify_url"` + ReturnUrl string `json:"return_url"` + PromoteId int `json:"promote_id"` + PromoteAccount string `json:"promote_account"` +} diff --git a/request/ReturnRequest.go b/request/ReturnRequest.go new file mode 100644 index 0000000..82614fa --- /dev/null +++ b/request/ReturnRequest.go @@ -0,0 +1,5 @@ +package request + +type ReturnRequest struct { + OrderNo string `url:"order_no"` +} diff --git a/system/application.go b/system/application.go new file mode 100644 index 0000000..9b140a3 --- /dev/null +++ b/system/application.go @@ -0,0 +1 @@ +package system diff --git a/utils/result.go b/utils/result.go new file mode 100644 index 0000000..47b6285 --- /dev/null +++ b/utils/result.go @@ -0,0 +1,50 @@ +package utils + +import ( + syserrors "errors" + "github.com/gin-gonic/gin" + "jypay/errors" +) + +type Result struct { + Code string `json:"code"` + Message string `json:"message"` + Data Data `json:"data"` +} + +type Data map[string]interface{} + +func BuildJsonHandler(api func(c *gin.Context) (Data, error)) func(c *gin.Context) { + return func(c *gin.Context) { + data, err := api(c) + result := Result{} + if err != nil { + result = buildErrorResult(err) + result.Data = data + } else { + result.Code = "0000" + result.Message = "成功" + result.Data = data + } + c.JSON(200, result) + } +} + +func BuildHandler(api func(c *gin.Context)) func(c *gin.Context) { + return api +} + +func buildErrorResult(err error) Result { + result := Result{} + switch err.(type) { + case *errors.BusinessError: + var businessError *errors.BusinessError + syserrors.As(err, &businessError) + result.Code = businessError.Code + result.Message = businessError.Message + default: + result.Code = "1000" + result.Message = "系统异常" + } + return result +}