diff --git a/api/h5/index.go b/api/h5/index.go index deb0985..7a08493 100644 --- a/api/h5/index.go +++ b/api/h5/index.go @@ -9,6 +9,7 @@ import ( "gold-shop/service" "gold-shop/utils/result" "gold-shop/utils/tlpay" + "gold-shop/utils/tlpay/param" "gold-shop/utils/weixin" "net/http" "strconv" @@ -160,7 +161,14 @@ func TransferPayNotify(c *gin.Context) { func PaymentNotify(c *gin.Context) { var req request.PaymentNotifyRequest - err := c.ShouldBindQuery(&req) + err := c.ShouldBind(&req) + if err != nil { + fmt.Println("error: " + err.Error()) + c.String(http.StatusOK, "fail") + return + } + fmt.Println("has-data") + fmt.Println(req) if err != nil { c.String(http.StatusOK, "fail") return @@ -170,21 +178,30 @@ func PaymentNotify(c *gin.Context) { return } +func TlBindingApply(c *gin.Context) (result.Data, error) { + var req request.TlAgreeApplyRequest + err := c.ShouldBind(&req) + if err != nil { + return nil, err + } + err = service.UserService.TlAgreeApply(req, user(c)) + return nil, err +} + func TLPay(c *gin.Context) { - param := tlpay.PayParam{} - param.PayType = "B2B" - param.OrderID = "12344565667" - param.CusID = "990581007426001" - param.AppID = "00000051" - param.TrxAmt = "0.01" - param.RetUrl = "https://www.baidu.com" - param.NotifyUrl = "https://www.baidu.com" - param.GoodsID = "123122" - param.GoodsInf = "slsdfxxx" - param.RandomStr = "sdfssss" - param.SignType = "RSA" - //c.String(http.StatusOK, "
nihao
") + p := param.GatewayPayParam{} + p.OrderID = "22222222223323232" + p.PayType = "B2B" + p.TrxAmt = "1" + p.RetUrl = "https://www.baidu.com" + p.NotifyUrl = "https://www.baidu.com" + p.GoodsID = "1" + p.GoodsInf = "充值" + p.Charset = "UTF-8" + + str := tlpay.TLPay.GatewayPay(p) + fmt.Println(str) c.Writer.Header().Set("content-type", "text/html") - c.Writer.WriteString("
nihao
") + c.Writer.WriteString(str) return } diff --git a/config.yaml b/config.yaml index de80883..1e73e7d 100644 --- a/config.yaml +++ b/config.yaml @@ -26,6 +26,15 @@ payment: auth-redirect-uri: "http://mall.wrtcjt.com/pages/wx/callback" sub-app-id: "wx939c26e4e09ce9ee" sub-app-secret: "0d365a525ec9bf2c055d735ced98a6c1" +tl-pay: + base-url: "https://vsp.allinpay.com" + app-id: "00307595" + cus-id: "6603910521126XB" + pri-pem-file: "./pri.pem" +# base-url: "https://syb-test.allinpay.com" +# app-id: "00000051" +# cus-id: "990581007426001" +# pri-pem-file: "./pri_test.pem" jwt: key: "I2js2oElEo82NmRru8v73Nwm" issuer: "gold-shop" \ No newline at end of file diff --git a/config/config.go b/config/config.go index 55797eb..6bb9088 100644 --- a/config/config.go +++ b/config/config.go @@ -6,4 +6,5 @@ type Config struct { Jwt Jwt Redis Redis Payment Payment + TlPay TlPay `yaml:"tl-pay"` } diff --git a/config/tl_pay.go b/config/tl_pay.go new file mode 100644 index 0000000..3ef9bb1 --- /dev/null +++ b/config/tl_pay.go @@ -0,0 +1,10 @@ +package config + +type TlPay struct { + BaseUrl string `yaml:"base-url"` + CusID string `yaml:"cus-id"` + AppId string `yaml:"app-id"` + PriPemFile string `yaml:"pri-pem-file"` + PubPemFile string `yaml:"pub-pem-file"` + TlPubPemFile string `yaml:"tl-pub-pem-file"` +} diff --git a/go.mod b/go.mod index 4eba5a7..55489f6 100644 --- a/go.mod +++ b/go.mod @@ -47,6 +47,7 @@ require ( 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/tjfoc/gmsm v1.4.1 // indirect github.com/twitchyliquid64/golang-asm v0.15.1 // indirect github.com/ugorji/go/codec v1.2.12 // indirect github.com/xuri/efp v0.0.0-20231025114914-d1ff6096ae53 // indirect diff --git a/go.sum b/go.sum index e35bcf8..758c323 100644 --- a/go.sum +++ b/go.sum @@ -1,9 +1,12 @@ +cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= +github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= 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/bytedance/sonic v1.11.3 h1:jRN+yEjakWh8aK5FzrciUHG8OFXK+4/KrAX/ysEtHAA= github.com/bytedance/sonic v1.11.3/go.mod h1:iZcSUejdk5aukTND/Eu/ivjQuEL0Cu9/rf50Hi0u/g4= +github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44= github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/chenzhuoyu/base64x v0.0.0-20211019084208-fb5309c8db06/go.mod h1:DH46F32mSOjUmXrMHnKwZdA8wcEefY7UVqBKYGjpdQY= @@ -13,11 +16,16 @@ github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d/go.mod h1:8EPpV 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/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= +github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78= github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc= +github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= +github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98= +github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= github.com/fvbock/endless v0.0.0-20170109170031-447134032cb6 h1:6VSn3hB5U5GeA6kQw4TwWIWbOhtvR2hmbBJnTOtqTWc= github.com/fvbock/endless v0.0.0-20170109170031-447134032cb6/go.mod h1:YxOVT5+yHzKvwhsiSIWmbAYM3Dr9AEEbER2dVayfBkg= github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0= @@ -54,7 +62,22 @@ 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-jwt/jwt/v5 v5.2.1 h1:OuVbFODueb089Lh128TAcimifWaLhJwVflnrgM17wHk= github.com/golang-jwt/jwt/v5 v5.2.1/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk= +github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= +github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= +github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw= +github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8= +github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA= +github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs= +github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w= +github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0= +github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= +github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= +github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU= 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= @@ -89,6 +112,7 @@ github.com/pelletier/go-toml/v2 v2.2.0 h1:QLgLl2yMN7N+ruc31VynXs1vhMZa7CeHHejIeB github.com/pelletier/go-toml/v2 v2.2.0/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= github.com/redis/go-redis/v9 v9.5.1 h1:H1X4D3yHPaYrkL5X06Wh6xNVM/pX0Ft4RV0vMGvLBh8= github.com/redis/go-redis/v9 v9.5.1/go.mod h1:hdY0cQFCN4fnSYT6TkisLufl/4W5UIXyv0b/CLO2V2M= github.com/richardlehane/mscfb v1.0.4 h1:WULscsljNPConisD5hR0+OyZjwK46Pfyr6mPu5ZawpM= @@ -118,6 +142,8 @@ github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcU github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +github.com/tjfoc/gmsm v1.4.1 h1:aMe1GlZb+0bLjn+cKTPEvvn9oUEBlJitaZiiBwsbgho= +github.com/tjfoc/gmsm v1.4.1/go.mod h1:j4INPkHWMrhJb38G+J6W4Tw0AbuN8Thu3PbdVYhVcTE= 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= @@ -133,16 +159,37 @@ golang.org/x/arch v0.6.0 h1:S0JTfE48HbRj80+4tbvZDYsJ3tGv6BUU3XxyZ7CirAc= golang.org/x/arch v0.6.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys= golang.org/x/arch v0.7.0 h1:pskyeJh/3AmoQ8CPE95vxHLqp1G1GfGNXTmcl9NEKTc= golang.org/x/arch v0.7.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/crypto v0.0.0-20201012173705-84dcc777aaee/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.16.0 h1:mMMrFzRSCF0GvB7Ne27XVtVAaXLrPmgPC7/v0tkwHaY= golang.org/x/crypto v0.16.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4= golang.org/x/crypto v0.18.0 h1:PGVlW0xEltQnzFZ55hkuX5+KLyrMYhHld1YHO4AKcdc= golang.org/x/crypto v0.18.0/go.mod h1:R0j02AL6hcrfOiy9T4ZYp/rcWeMxM3L6QYxlOuEG1mg= golang.org/x/crypto v0.21.0 h1:X31++rzVUdKhX5sWmSOFZxx8UW/ldWx55cbf08iNAMA= golang.org/x/crypto v0.21.0/go.mod h1:0BP7YvVV9gBbVKyeTG0Gyn+gZm94bibOW5BjDEYAOMs= +golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= +golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= +golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU= +golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= +golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20201010224723-4f7140c49acb/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= 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/net v0.22.0 h1:9sGLhx7iRIHEiX0oAJ3MRZMUCElJgy7Br1nO+AMN3Tc= golang.org/x/net v0.22.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg= +golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= +golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 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= @@ -151,10 +198,31 @@ golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU= golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4= golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= 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/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY= +golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= +golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= +google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= +google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= +google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc= +google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= +google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg= +google.golang.org/grpc v1.25.1/go.mod h1:c3i+UQWmh7LiEpx4sFZnkU36qjEYZ0imhYfXVyQciAY= +google.golang.org/grpc v1.31.0/go.mod h1:N36X2cJ7JwdamYAgDz+s+rVMFjt3numwzf/HckM8pak= +google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8= +google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0= +google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM= +google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE= +google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo= +google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= 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= @@ -171,5 +239,7 @@ gorm.io/driver/mysql v1.5.2/go.mod h1:pQLhh1Ut/WUAySdTHwBpBv6+JKcj+ua4ZFx1QQTBzb 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= +honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= +honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= 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/goldshop b/goldshop index d44cb74..4b9729a 100755 Binary files a/goldshop and b/goldshop differ diff --git a/main.go b/main.go index 6218293..c8ded2a 100644 --- a/main.go +++ b/main.go @@ -11,7 +11,6 @@ import ( "gold-shop/request" "gold-shop/route" "gold-shop/utils" - "gold-shop/utils/tlpay" "net/http" "net/url" "time" @@ -120,21 +119,3 @@ func payOrder3() { fmt.Println(string(body)) utils.Post2("https://api.paypal.com/v2/checkout/orders", string(body), token) } - -func testPay() { - param := tlpay.PayParam{} - param.PayType = "B2B" - param.OrderID = "12344565667" - param.CusID = "990581007426001" - param.AppID = "00000051" - param.TrxAmt = "0.01" - param.RetUrl = "https://www.baidu.com" - param.NotifyUrl = "https://www.baidu.com" - param.GoodsID = "123122" - param.GoodsInf = "slsdfxxx" - param.RandomStr = "sdfssss" - param.SignType = "RSA" - //param.GateID = "" - //param.ValidTime = "" - tlpay.TLPay.Pay(param) -} diff --git a/model/payment.go b/model/payment.go index 040ec13..7dc14d9 100644 --- a/model/payment.go +++ b/model/payment.go @@ -12,6 +12,7 @@ const PaymentStatusWait = 0 type Payment struct { ID int PaymentNo string + PayOrderID string UserID int Amount float64 User User `gorm:"foreignKey:UserID;references:ID"` diff --git a/pri.pem b/pri.pem new file mode 100644 index 0000000..89d22ca --- /dev/null +++ b/pri.pem @@ -0,0 +1,27 @@ +-----BEGIN RSA PRIVATE KEY----- +MIIEogIBAAKCAQEAsjn9SQoGMlKPQ+H+AnoeSLjBrqCt8FGG8MGgiAyOz7nsUzCU +Kig3sWZRZFzGEWBlICpzfqcTDPK1dukHGROmaPNF+aC5u1KzpjOx0R5cqnHR1iag +aeIqoivmgJdUciuc4G+9SmeeSJEn0LsgqDAIpY9wsuMtBjf41itvtK1x5EUo7Apy +YjoFAv+Iaeo5DQCHIVQZJ7hKDFnKXN6EzWaBIGRHsWYa1iBcSjsMMPXVXfiq7bdb +SpPjUJhaVxrq3GknKo+i0hqMFCBFodI6Ms+4xQm35ghCnOi0vyLcNzxNkQXnm56F +e1rskqu8oxUrjOCQLLvX0x+rADJE6dxv/0/CMQIDAQABAoIBACSIvxtLDwCfTGdn +qtxioJUbLHbJ0MYPzEfLhUcGPfsme2OFNIYcxufaV8ppOx/iQPCSSykWAhF4Kksl +ExOoo8blQX2dnR0hY5qpA6UgonG9Ebzin6T82+zw71gCTWj2+DpIpYacTYXGHaGL +znKzn/4iNIGzEG5zKFLsxgIFQPDABWfGyvSADUmw0tGhsjxUHtHWaBuOedxTJLFT +2NoLP/jS/Ais1GpvPX9ZIIe2aBKFSrVGaUTHg2Hy7DrHGLQtO7NC3wBkZ0d/ch+/ +TPhBnR9zaoa1RhCYACCH9171jJSvZDcvE0OdDvJ4lOr+Zd15HYA29m3164hCbfzk +CEoEmH0CgYEA8Ftdk9Ll/13hbPYRr5H4vZF/6njpP/IZW+TpF5Ro3S8MV3rMDJJ7 +vQeOrpcTwz86VjHa4YDStI7IJ5VoCByAQLm5UADMrHfDUkF09XkYl8nlaOlEU/zA +8KUQOKzClm0rwhOegrMmN8vWndGgAGWzysrJ6dQeWGd0888RoRL3oQ0CgYEAvdN1 +C7xLgN6JCKP8NkN4puBZdZ+Yi9oyFQpYKSSoBcOeuUSHhbIth5PDRKo4SH9DeCSY +IGm0EbZIkfcI0DZzwJxDkeunxnzV9kiUU3MgXKN+exvPn1ZKBiroJD2BhdG6IBIB +hGttPRxTXysIaRKMIaFL8Q5X2auItDF99SwFdLUCgYBMVdgccKiXN+u2PVhUmFmV +azZi80+w/WTMaKEW2g2L4btbtSZE/+jImjPYgybFpbkTUvYfFFjW+1naKHtHyDV7 +WQDfxcE7LTYdU6ifpXwICRkQ/rSyr91fEC1cspGCUN3K34xCGj+TDOv2Z3MXbTVy +vIDwAGw6y3C7VhJVQ77dqQKBgHar2pGCnsUq2gpv0j3uQmA5A1RYeNp9hy/GLsVR +6ETqPbFfGUZI1NPcWYf0zojsHrbeUch5rJwiFA5EnNrmH8k7J7by1wlsg6qbRzdA +Uuv3Phwij9Lx9AcVDdmSJgYNzkk8WHx7WGiK68gkM/bUJIzltH83003+QdE825zJ +b4O9AoGAe4C156UaZ1PqDFwDu0TeEknvHBzBCQ3WyqwiGUBWjV07ST14uAvqhyLg +ryY3nFo0/H6YFPi9EWaEwshAUuJIsFCLgWbu4IHvULUkdvZbFBK6o6mQyBiz2c/R +rHMso4Dyv1e5VS2BckH7fUdrz+7PgFPKWELiM70SDsMTCsvzeuQ= +-----END RSA PRIVATE KEY----- \ No newline at end of file diff --git a/pri_test.pem b/pri_test.pem new file mode 100644 index 0000000..7a69d23 --- /dev/null +++ b/pri_test.pem @@ -0,0 +1,28 @@ +-----BEGIN PRIVATE KEY----- +MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCKtoln2ZolnGUd +bTkOd0eOZ/Qk9EMwt7H4rVdbkRahVNuTbzXyg1KzHANG6ODjB/65INgdhPSW7TsH +UJLqjwmJTmpqfsH5vQVWb9h0J3opY3azyI2k/8G0ta+DuWJ6ykJgwlfw8jZM1mN+ +xKmyhTBJiDpN53s+QtZFX+xaR1XLUR3cMlwWaRcwOzHX5iYNtQEXm461GS1Mc7C3 +d4gdGlR7QUkd3apcapIgM4lBDAPC9wbOBMnBVYQyCjKem/nB57qgVznYRHClq888 +tv0OhkveAs1/l0KLPtqB3+dlef0ATMDyCt0S7KUMubw2hnx1hx5jpQYfAmluLHaK +THSYmY3HAgMBAAECggEAQ/FRz1S13KtEmcvIcS36liWT38/m40NuwFwDWH3yBDbA +0G/eEDnDtahIcKCbmohte3c7dAYOGaj8S0Jrm8lRBfirRhttXqp3pVVmQ8qcWw51 +c20N+B4v3phJbm9M7dEI/0Th90pkT3GJTWv9pMv5EWNRD5yM8LujIVrxNW0QeGO7 +7dO4Kl8ABG1EKoAOU/oepOFfM9wyb0nagDnYDMWNfhLC6gZACHwvCTlq91WmbGaD +CoVcAIg4/KW/C+JoGML4XWiSdk7HbRWPGwUb8p35wrfA3neS0k7KB1G8WSK0I+MD +/iT6D4fnU6IzQP8IsVMeJxwb1H1Akxxd2ZvHuATtyQKBgQC9aa61xkrtH6tFmleA +2O7bqCjYUNc92Qp0eJ+tfFqsy2At29E2ykjtZZVqvJfhoulSd0Ul6AXgZzbXNYlK +1m/cQZSFKi4+MChrH1mc2VfBTORTgI1eL8xEA71E2xzBax1SKNFE5ntDpBoVXxUy +y0IqGMllD9iJ/KbjTQxui3e/PwKBgQC7ehjloR9B/XnSz2jMLfKnSqCq37sHWAnq +l+F7SE47rmTaUdVwlA4+hc/WaOtQHMSzwqqdJ56OSfLRFHBszlvvcxG9FSUl9Zpj +qt/l7QyIdsQFGwf71BV3M74pQBzrpy76qVVYe0iX/zftJ7leLIB5u0RNBr/UUMPf +L1g/uk6XeQKBgDr5TalRDQEZbCUmsfnvESeZCuaW/P1Ht2aeK/FDF6RXSiHdIB20 +C9LGXyg1ggCBza2cbw4nWLUOzddh1tZTP87PKGPxxyYbrmnz/93DGfyYaTrKermJ +nkrK80IeCqz6Ic3z4XxXWqerz2c6DIqdGIOs5f+uWFhWLsv0sFs211jXAoGAFZnA +ypxg3JqW8boiCfx1S98mWenDQ9pZ8iSjvy+ai/fQ+PKa1AjLB1slkj+udSGLbGbO +f6Lz+tL8Xiirt22jXBh2/8Ey8Ye1zSv/9h705bhBZ5Ewv0w1WPGmRFx1uJUfrZPb +GIHD4HdweyuriPU5rEzuVq2jZpQTrWBoG5ecvIkCgYAFicbthDb4XLPSkAVaSoYG +MZEfqBNH+CsX47nsR2/4VnS+pNj9Y5vEJ+e4jEnMYWyDu8fxtLf/96PeqCxzRnWW +Y+ME6WJuuCZ/cgOtMEAC95lerQ8H9pHGYDQfpfC833hSMJSahnm4rtc0E6KOifri +jIfMQvadsQaNGKgTmgDlwA== +-----END PRIVATE KEY----- \ No newline at end of file diff --git a/request/tl_agree_apply_request.go b/request/tl_agree_apply_request.go new file mode 100644 index 0000000..444d9f1 --- /dev/null +++ b/request/tl_agree_apply_request.go @@ -0,0 +1,10 @@ +package request + +type TlAgreeApplyRequest struct { + IdNo string `json:"idNo" form:"idNo"` + AcctName string `json:"acctName" form:"acctName"` + AcctNo string `json:"acctNo" form:"acctNo"` + Mobile string `json:"mobile" form:"mobile"` + ValidDate string `json:"validDate" form:"validDate"` + Cvv2 string `json:"cvv2" form:"cvv2"` +} diff --git a/route/h5.go b/route/h5.go index d9cb055..5933f65 100644 --- a/route/h5.go +++ b/route/h5.go @@ -32,5 +32,6 @@ func h5RouteInit(r *gin.Engine) { authGroup.Match([]string{"GET", "OPTIONS"}, "/get-payments", result.Json(h5.GetPayments)) authGroup.Match([]string{"GET", "OPTIONS"}, "/get-user-data", result.Json(h5.GetUserData)) authGroup.Match([]string{"POST", "OPTIONS"}, "/save-alipay-account", result.Json(h5.SaveAlipayAccount)) + authGroup.Match([]string{"POST", "OPTIONS"}, "/tl-binding-apply", result.Json(h5.TlBindingApply)) } } diff --git a/service/payment_service.go b/service/payment_service.go index 9d7e283..709fcfc 100644 --- a/service/payment_service.go +++ b/service/payment_service.go @@ -39,7 +39,7 @@ func (s paymentService) Payment(userID int, paymentRequest request.PaymentReques data := result.Data{} if paymentRequest.PayType == "wxgzh" { - data, err = s.wxPayment(paymentRequest, p) + data, err = s.wxPayment(paymentRequest, p.PaymentNo) } else if paymentRequest.PayType == "transfer" { data, err = s.transferPayment(paymentRequest, p) } else { @@ -52,19 +52,19 @@ func (s paymentService) Payment(userID int, paymentRequest request.PaymentReques return nil, err } - p.Status = 1 + p.PayOrderID = data["orderId"].(string) global.DB.Save(p) fmt.Println(data) return data, nil } -func (s paymentService) wxPayment(paymentRequest request.PaymentRequest, p *model.Payment) (result.Data, error) { +func (s paymentService) wxPayment(paymentRequest request.PaymentRequest, orderID string) (result.Data, error) { param := payment.WxPayParam{} param.Remark = "payment" - param.OrderId = utils.GenerateNo("payment") + param.OrderId = orderID param.OpenId = paymentRequest.OpenID param.Amount = paymentRequest.Amount - param.NotifyUrl = "http://api.wrtcjt.com/payment-notify" + param.NotifyUrl = "http://api.wrtcjt.com/h5/payment-notify" res, err := payment.PayApi.WxPay(param) if err != nil { return nil, err @@ -75,6 +75,7 @@ func (s paymentService) wxPayment(paymentRequest request.PaymentRequest, p *mode } data := result.Data{ + "orderId": res.OrderID, "paySign": res.PaySign, "package": res.Package, "signType": res.PaySignType, @@ -190,15 +191,19 @@ func (paymentService) buildPaymentsExcel(payments []model.Payment, sheet string, func (s paymentService) AfterTransferPayNotify(req request.TransferPayNotifyRequest) error { pm := &model.Payment{} - err := global.DB.Where("payment_no", req.OutOrderID).First(&pm).Error + err := global.DB.Where("payment_no", req.OutOrderID).Where("status", 0).First(&pm).Error if err != nil && e.Is(err, gorm.ErrRecordNotFound) { return errors.NewBusinessError("记录不存在") } + user := model.User{} + global.DB.Model(&model.User{}).Where("id", pm.UserID).Find(&user) + if req.PayStatus == "1" { pm.Status = 1 pm.PayerCardNo = req.PayerCardNo pm.PayerAccountName = req.PayerAccountName + global.DB.Save(&user) } else { pm.Status = 2 } @@ -210,13 +215,17 @@ func (s paymentService) AfterTransferPayNotify(req request.TransferPayNotifyRequ func (s paymentService) AfterPaymentNotify(req request.PaymentNotifyRequest) error { pm := &model.Payment{} - err := global.DB.Where("payment_no", req.MchOrderID).First(&pm).Error + err := global.DB.Where("payment_no", req.MchOrderID).Where("status", 0).First(&pm).Error if err != nil && e.Is(err, gorm.ErrRecordNotFound) { return errors.NewBusinessError("记录不存在") } + user := model.User{} + global.DB.Model(&model.User{}).Where("id", pm.UserID).Find(&user) + if req.PayStatus == 1 { pm.Status = 1 + global.DB.Model(&user).UpdateColumn("balance", gorm.Expr("balance + ?", pm.Amount)) } else { pm.Status = 2 } diff --git a/service/user_service.go b/service/user_service.go index e17bbfa..45eecef 100644 --- a/service/user_service.go +++ b/service/user_service.go @@ -2,15 +2,20 @@ package service import ( e "errors" + "fmt" "github.com/xuri/excelize/v2" "gold-shop/errors" "gold-shop/global" "gold-shop/model" + "gold-shop/request" "gold-shop/request/manage" "gold-shop/utils" "gold-shop/utils/excel" "gold-shop/utils/result" + "gold-shop/utils/tlpay" + "gold-shop/utils/tlpay/param" "gorm.io/gorm" + "strconv" "time" ) @@ -128,3 +133,23 @@ func (userService) buildUserListExcel(users []model.User, sheet string, f *excel row++ } } + +func (userService) TlAgreeApply(req request.TlAgreeApplyRequest, user *model.User) error { + p := param.AgreeApplyParam{} + p.Mobile = req.Mobile + p.IdNo = req.IdNo + p.Cvv2 = req.Cvv2 + p.AcctNo = req.AcctNo + p.AcctName = req.AcctName + p.ValidDate = req.ValidDate + p.MerUserID = strconv.Itoa(user.ID) + p.IdType = "0" + p.AcctType = "00" + + r, err := tlpay.TLPay.AgreeApply(p) + fmt.Println(r) + if err != nil { + return err + } + return nil +} diff --git a/utils/encrypt.go b/utils/encrypt.go index 1ead4f1..3ff6a2d 100644 --- a/utils/encrypt.go +++ b/utils/encrypt.go @@ -120,17 +120,20 @@ func Post2(url string, params string, token string) { func RSASign(data []byte, filename string) (string, error) { // 1、选择hash算法,对需要签名的数据进行hash运算 - myHash := crypto.SHA256 + myHash := crypto.SHA1 hashInstance := myHash.New() hashInstance.Write(data) hashed := hashInstance.Sum(nil) + // 2、读取私钥文件,解析出私钥对象 privateKey, err := ReadParsePrivateKey(filename) + fmt.Println("signErr1", err) if err != nil { return "", err } // 3、RSA数字签名(参数是随机数、私钥对象、哈希类型、签名文件的哈希串,生成bash64编码) bytes, err := rsa.SignPKCS1v15(rand.Reader, privateKey, myHash, hashed) + fmt.Println("signErr2", err) if err != nil { return "", err } @@ -144,7 +147,7 @@ func RSAVerify(data []byte, base64Sig, filename string) error { return err } // 2、选择hash算法,对需要签名的数据进行hash运算 - myHash := crypto.SHA256 + myHash := crypto.SHA1 hashInstance := myHash.New() hashInstance.Write(data) hashed := hashInstance.Sum(nil) @@ -183,18 +186,29 @@ func ReadParsePublicKey(filename string) (*rsa.PublicKey, error) { func ReadParsePrivateKey(filename string) (*rsa.PrivateKey, error) { // 1、读取私钥文件,获取私钥字节 privateKeyBytes, err := os.ReadFile(filename) + fmt.Println(string(privateKeyBytes)) + if err != nil { return nil, err } // 2、解码私钥字节,生成加密对象 block, _ := pem.Decode(privateKeyBytes) + if block == nil { return nil, errors.New("私钥信息错误!") } + fmt.Println("blockType", block.Type) // 3、解析DER编码的私钥,生成私钥对象 - privateKey, err := x509.ParsePKCS1PrivateKey(block.Bytes) - if err != nil { - return nil, err + if block.Type == "RSA PRIVATE KEY" { + return x509.ParsePKCS1PrivateKey(block.Bytes) + } + + privateKey, err := x509.ParsePKCS8PrivateKey(block.Bytes) + rsaKey, ok := privateKey.(*rsa.PrivateKey) + + if ok { + return rsaKey, nil + } else { + return nil, errors.New("私钥错误") } - return privateKey, nil } diff --git a/utils/payment/pay_api.go b/utils/payment/pay_api.go index 6bca358..d3c455a 100644 --- a/utils/payment/pay_api.go +++ b/utils/payment/pay_api.go @@ -33,6 +33,9 @@ func (api *payApi) WxPay(param WxPayParam) (*WxPayResult, error) { sign := api.sign(data, payment.SignKey) data.Set("sign", sign) fmt.Println(data.Encode()) + + data.Set("notify_url", url.QueryEscape(param.NotifyUrl)) + res, err := api.post(payment.BaseUrl+"/payapi/mini/wxpay", data) if err != nil { return nil, err diff --git a/utils/sm.go b/utils/sm.go new file mode 100644 index 0000000..2ca250b --- /dev/null +++ b/utils/sm.go @@ -0,0 +1,104 @@ +package utils + +import ( + "crypto/rand" + "encoding/base64" + "fmt" + "github.com/tjfoc/gmsm/sm2" + "github.com/tjfoc/gmsm/x509" + "os" +) + +type SM struct { + PrivateFile string + PublicFile string +} + +func (s *SM) CreateSm2Sig(msg []byte) ([]byte, *sm2.PublicKey, error) { + //读取密钥对 + pri, err := s.readPemCxt(s.PrivateFile) + privateKey, _ := x509.ReadPrivateKeyFromPem(pri, nil) + c := sm2.P256Sm2() //椭圆曲线 + priKey := new(sm2.PrivateKey) + priKey.PublicKey.Curve = c + priKey.D = privateKey.D + priKey.PublicKey.X = privateKey.X + priKey.PublicKey.Y = privateKey.Y + + sign, err := priKey.Sign(rand.Reader, msg, nil) //sm2签名 + if err != nil { + return nil, nil, err + } + return sign, &priKey.PublicKey, err +} + +func (s *SM) readPemCxt(path string) ([]byte, error) { + file, err := os.Open(path) + if err != nil { + return []byte{}, err + } + defer func(file *os.File) { + err := file.Close() + if err != nil { + fmt.Println(err) + } + }(file) + fileInfo, err := file.Stat() + if err != nil { + return []byte{}, err + } + buf := make([]byte, fileInfo.Size()) + _, err = file.Read(buf) + if err != nil { + return []byte{}, err + } + return buf, err +} + +func (s *SM) Decrypt(data string) (string, error) { + pri, err := s.readPemCxt(s.PrivateFile) + if err != nil { + return "", err + } + + privateKeyFromPem, err := x509.ReadPrivateKeyFromPem(pri, nil) + if err != nil { + return "", err + } + + cipherTxt, err := base64.StdEncoding.DecodeString(data) + if err != nil { + return "", err + } + plainTxt, err := privateKeyFromPem.DecryptAsn1(cipherTxt) + if err != nil { + return "", err + } + return string(plainTxt), nil +} + +func (s *SM) Encrypt(data string) (string, error) { + pub, err := s.readPemCxt(s.PublicFile) + if err != nil { + return "", err + } + //read public key + publicKeyFromPem, err := x509.ReadPublicKeyFromPem(pub) + if err != nil { + fmt.Println(err) + return "", err + } + cipherTxt, err := publicKeyFromPem.EncryptAsn1([]byte(data), rand.Reader) + if err != nil { + return "", err + } + return base64.StdEncoding.EncodeToString(cipherTxt), nil +} + +func (s *SM) VerSm2Sig(pub *sm2.PublicKey, msg []byte, sign []byte) bool { + ok := pub.Verify(msg, sign) + if !ok { + return false + } + return true +} diff --git a/utils/tlpay/api.go b/utils/tlpay/api.go index fbe7667..f061bdf 100644 --- a/utils/tlpay/api.go +++ b/utils/tlpay/api.go @@ -1,14 +1,18 @@ package tlpay import ( + "encoding/json" "fmt" "gold-shop/errors" "gold-shop/global" "gold-shop/utils" + "gold-shop/utils/tlpay/param" + "gold-shop/utils/tlpay/result" "io" "net/http" "net/url" "strings" + "time" ) var TLPay tlPay = tlPay{} @@ -16,27 +20,63 @@ var TLPay tlPay = tlPay{} type tlPay struct { } -func (api *tlPay) Pay(param PayParam) (string, error) { - payment := global.Config.Payment - data, err := utils.StructToURLValues(param) +func (api *tlPay) AgreeApply(p param.AgreeApplyParam) (*result.AgreeApplyResult, error) { + payConfig := global.Config.TlPay + data, err := utils.StructToURLValues(p) + data = api.SetCommonParams(data, true) sign, err := api.sign(data) + if err != nil { + return nil, err + } + fmt.Println(sign) data.Set("sign", sign) - res, err := api.post(payment.BaseUrl+"/apiweb/gateway/pay", data) + res, err := api.post(payConfig.BaseUrl+"/apiweb/qpay/agreeapply", data) if err != nil { - return "", err + return nil, err + } + r := result.AgreeApplyResult{} + err = json.Unmarshal(res, &r) + if err != nil { + return nil, err } - return string(res), nil + return &r, nil +} + +func (api *tlPay) SetCommonParams(data url.Values, isNeedReqTime bool) url.Values { + payConfig := global.Config.TlPay + data.Set("cusid", payConfig.CusID) + data.Set("appid", payConfig.AppId) + data.Set("randomstr", utils.GenerateRandomString(32)) + if isNeedReqTime { + data.Set("reqtime", time.Now().Format("20060102150405")) + } + data.Set("signtype", "RSA") + return data } func (api *tlPay) sign(params url.Values) (string, error) { + payConfig := global.Config.TlPay params.Del("sign") - fmt.Println(params.Encode()) - return utils.RSASign([]byte(params.Encode()), "") + for key, value := range params { + if len(value) == 0 || value[0] == "" { + params.Del(key) + } + } + signStr, err := url.QueryUnescape(params.Encode()) + if err != nil { + return "", err + } + + fmt.Println("signStr: " + signStr) + return utils.RSASign([]byte(signStr), payConfig.PriPemFile) } func (api *tlPay) post(url string, data url.Values) ([]byte, error) { client := &http.Client{} body := strings.NewReader(data.Encode()) + + fmt.Println("url: " + url) + fmt.Println("data: " + data.Encode()) request, err := http.NewRequest("POST", url, body) if err != nil { @@ -62,15 +102,24 @@ func (api *tlPay) post(url string, data url.Values) ([]byte, error) { return bodyBytes, nil } -func (api *tlPay) buildHtml(param PayParam) string { +func (api *tlPay) GatewayPay(p param.GatewayPayParam) string { + data, _ := utils.StructToURLValues(p) + data = api.SetCommonParams(data, false) + sign, _ := api.sign(data) + data.Set("sign", sign) + return api.BuildForm(data) +} + +func (api *tlPay) BuildForm(data url.Values) string { htmlBegin := ` 充值 + @@ -80,15 +129,18 @@ func (api *tlPay) buildHtml(param PayParam) string { ` ` - return htmlBegin + api.buildFormHtml(param) + htmlEnd + return htmlBegin + api.buildFormHtml(data) + htmlEnd } -func (api *tlPay) buildFormHtml(param PayParam) string { - data, _ := utils.StructToURLValues(param) - formHtml := "
" +func (api *tlPay) buildFormHtml(data url.Values) string { + payConfig := global.Config.TlPay + formUrl := payConfig.BaseUrl + "/apiweb/gateway/pay" + formHtml := "" for key, value := range data { formHtml += "" } + + formHtml += "" formHtml += "
" return formHtml } diff --git a/utils/tlpay/param/agree_apply_param.go b/utils/tlpay/param/agree_apply_param.go new file mode 100644 index 0000000..98fa87e --- /dev/null +++ b/utils/tlpay/param/agree_apply_param.go @@ -0,0 +1,20 @@ +package param + +type AgreeApplyParam struct { + CusID string `json:"cusid" form:"cusid"` + AppID string `json:"appid" form:"appid"` + ReqTime string `json:"reqtime" form:"reqtime"` + RandomStr string `json:"randomstr" form:"randomstr"` + SignType string `json:"signtype" form:"signtype"` + Sign string `json:"sign" form:"sign"` + + MerUserID string `json:"meruserid" form:"meruserid"` + AcctType string `json:"accttype" form:"accttype"` + AcctNo string `json:"acctno" form:"acctno"` + IdType string `json:"idtype" form:"idtype"` + IdNo string `json:"idno" form:"idno"` + AcctName string `json:"acctname" form:"acctname"` + Mobile string `json:"mobile" form:"mobile"` + ValidDate string `json:"validdate" form:"validdate"` + Cvv2 string `json:"cvv2" form:"cvv2"` +} diff --git a/utils/tlpay/param/agree_confirm_param.go b/utils/tlpay/param/agree_confirm_param.go new file mode 100644 index 0000000..c09aee9 --- /dev/null +++ b/utils/tlpay/param/agree_confirm_param.go @@ -0,0 +1,21 @@ +package param + +type AgreeConfirmParam struct { + CusID string `json:"cusid" form:"cusid"` + AppID string `json:"appid" form:"appid"` + ReqTime string `json:"reqtime" form:"reqtime"` + RandomStr string `json:"randomstr" form:"randomstr"` + SignType string `json:"signtype" form:"signtype"` + Sign string `json:"sign" form:"sign"` + + MerUserID string `json:"meruserid" form:"meruserid"` + AcctType string `json:"accttype" form:"accttype"` + AcctNo string `json:"acctno" form:"acctno"` + IdType string `json:"idtype" form:"idtype"` + IdNo string `json:"idno" form:"idno"` + AcctName string `json:"acctname" form:"acctname"` + Mobile string `json:"mobile" form:"mobile"` + ValidDate string `json:"validdate" form:"validdate"` + SmsCode string `json:"smscode" form:"smscode"` + Cvv2 string `json:"cvv2" form:"cvv2"` +} diff --git a/utils/tlpay/pay_param.go b/utils/tlpay/param/gateway_pay_param.go similarity index 89% rename from utils/tlpay/pay_param.go rename to utils/tlpay/param/gateway_pay_param.go index 5f837df..31bbc29 100644 --- a/utils/tlpay/pay_param.go +++ b/utils/tlpay/param/gateway_pay_param.go @@ -1,20 +1,22 @@ -package tlpay +package param -type PayParam struct { +type GatewayPayParam struct { OrgID string `json:"orgid" form:"orgid"` CusID string `json:"cusid" form:"cusid"` AppID string `json:"appid" form:"appid"` + Charset string `json:"charset" form:"charset"` + RandomStr string `json:"randomstr" form:"randomstr"` + SignType string `json:"signtype" form:"signtype"` + Sign string `json:"sign" form:"sign"` + RetUrl string `json:"returl" form:"returl"` NotifyUrl string `json:"notifyurl" form:"notifyurl"` GoodsID string `json:"goodsid" form:"goodsid"` GoodsInf string `json:"goodsinf" form:"goodsinf"` TrxAmt string `json:"trxamt" form:"trxamt"` OrderID string `json:"orderid" form:"orderid"` - RandomStr string `json:"randomstr" form:"randomstr"` GateID string `json:"gateid" form:"gateid"` PayType string `json:"paytype" form:"paytype"` LimitPay string `json:"limitpay" form:"limitpay"` ValidTime string `json:"validtime" form:"validtime"` - SignType string `json:"signtype" form:"signtype"` - Sign string `json:"sign" form:"sign"` } diff --git a/utils/tlpay/param/pay_agree_confirm_param.go b/utils/tlpay/param/pay_agree_confirm_param.go new file mode 100644 index 0000000..f3acb8d --- /dev/null +++ b/utils/tlpay/param/pay_agree_confirm_param.go @@ -0,0 +1,15 @@ +package param + +type PayAgreeConfirmParam struct { + CusID string `json:"cusid" form:"cusid"` + AppID string `json:"appid" form:"appid"` + ReqTime string `json:"reqtime" form:"reqtime"` + RandomStr string `json:"randomstr" form:"randomstr"` + SignType string `json:"signtype" form:"signtype"` + Sign string `json:"sign" form:"sign"` + + ReqSn string `json:"reqsn" form:"reqsn"` + AgreeID string `json:"agreeid" form:"agreeid"` + SmsCode string `json:"smscode" form:"smscode"` + ThpInfo string `json:"thpinfo" form:"thpinfo"` +} diff --git a/utils/tlpay/param/pay_apply_agree_param.go b/utils/tlpay/param/pay_apply_agree_param.go new file mode 100644 index 0000000..65bd787 --- /dev/null +++ b/utils/tlpay/param/pay_apply_agree_param.go @@ -0,0 +1,19 @@ +package param + +type PayApplyAgreeParam struct { + CusID string `json:"cusid" form:"cusid"` + AppID string `json:"appid" form:"appid"` + ReqTime string `json:"reqtime" form:"reqtime"` + RandomStr string `json:"randomstr" form:"randomstr"` + SignType string `json:"signtype" form:"signtype"` + Sign string `json:"sign" form:"sign"` + + ReqSn string `json:"reqsn" form:"reqsn"` + AgreeID string `json:"agreeid" form:"agreeid"` + Amount string `json:"amount" form:"amount"` + Currency string `json:"currency" form:"currency"` + Subject string `json:"subject" form:"subject"` + TrxReserve string `json:"trxreserve" form:"trxreserve"` + NotifyUrl string `json:"notifyurl" form:"notifyurl"` + AsInfo string `json:"asinfo" form:"asinfo"` +} diff --git a/utils/tlpay/param/pay_sms_agree_param.go b/utils/tlpay/param/pay_sms_agree_param.go new file mode 100644 index 0000000..ffd9222 --- /dev/null +++ b/utils/tlpay/param/pay_sms_agree_param.go @@ -0,0 +1,14 @@ +package param + +type PaySmsAgreeParam struct { + CusID string `json:"cusid" form:"cusid"` + AppID string `json:"appid" form:"appid"` + ReqTime string `json:"reqtime" form:"reqtime"` + RandomStr string `json:"randomstr" form:"randomstr"` + SignType string `json:"signtype" form:"signtype"` + Sign string `json:"sign" form:"sign"` + + OrderID string `json:"orderid" form:"orderid"` + AgreeID string `json:"agreeid" form:"agreeid"` + ThpInfo string `json:"thpinfo" form:"thpinfo"` +} diff --git a/utils/tlpay/result/agree_apply_result.go b/utils/tlpay/result/agree_apply_result.go new file mode 100644 index 0000000..1049ad4 --- /dev/null +++ b/utils/tlpay/result/agree_apply_result.go @@ -0,0 +1,14 @@ +package result + +type AgreeApplyResult struct { + RetCode string `json:"retcode" form:"retcode"` + RetMsg string `json:"retmsg" form:"retmsg"` + RandomStr string `json:"randomstr" form:"randomstr"` + Sign string `json:"sign" form:"sign"` + + TrxStatus string `json:"trxstatus" form:"trxstatus"` + ErrMsg string `json:"errmsg" form:"errmsg"` + AgreeID string `json:"agreeid" form:"agreeid"` + BankCode string `json:"bankcode" form:"bankcode"` + BankName string `json:"bankname" form:"bankname"` +}