diff --git a/payment/.gitignore b/payment/.gitignore new file mode 100644 index 00000000..723ef36f --- /dev/null +++ b/payment/.gitignore @@ -0,0 +1 @@ +.idea \ No newline at end of file diff --git a/payment/api/api.go b/payment/api/api.go new file mode 100644 index 00000000..6b966238 --- /dev/null +++ b/payment/api/api.go @@ -0,0 +1,8 @@ +package api + +import "gorm.io/gorm" + +// API contains the data needed to operate the endpoints +type API struct { + Database *gorm.DB +} diff --git a/payment/api/idpay.go b/payment/api/idpay.go new file mode 100644 index 00000000..778f64ec --- /dev/null +++ b/payment/api/idpay.go @@ -0,0 +1 @@ +package api diff --git a/payment/api/miscellaneous.go b/payment/api/miscellaneous.go new file mode 100644 index 00000000..bf646fea --- /dev/null +++ b/payment/api/miscellaneous.go @@ -0,0 +1,11 @@ +package api + +import ( + "github.com/gin-gonic/gin" + "net/http" +) + +// HealthCheck returns an empty json array +func HealthCheck(c *gin.Context) { + c.JSON(http.StatusOK, gin.H{}) +} diff --git a/payment/cmd/payment/main.go b/payment/cmd/payment/main.go new file mode 100644 index 00000000..a2668fc6 --- /dev/null +++ b/payment/cmd/payment/main.go @@ -0,0 +1,80 @@ +package main + +import ( + "context" + "github.com/gin-gonic/gin" + "github.com/go-faster/errors" + log "github.com/sirupsen/logrus" + "gorm.io/gorm" + "net" + "net/http" + "os" + "os/signal" + "syscall" + "wss-payment/api" + db "wss-payment/internal/database" +) + +func main() { + // Create the data needed + endpointApi := new(api.API) + endpointApi.Database = setupDatabase() + defer closeDatabase(endpointApi.Database) + // Setup endpoints + r := gin.New() + r.Use(gin.Recovery()) + r.POST("/create") + r.GET("/status") + r.GET("/health", api.HealthCheck) + // Listen + srv := &http.Server{ + Handler: r, + } + go func() { + if err := srv.Serve(getListener()); err != nil && !errors.Is(err, http.ErrServerClosed) { + log.Fatalf("listen: %s", err) + } + }() + quit := make(chan os.Signal, 1) + signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM) + <-quit + log.Println("Shutting down server...") + _ = srv.Shutdown(context.Background()) +} + +// Setup the database according to the DATABASE_URL environment variable +func setupDatabase() *gorm.DB { + // Check DB url + dbURL := os.Getenv("DATABASE_URL") + if dbURL == "" { + log.Fatal("please set DATABASE_URL environment variable") + } + // Get the database url and connect to it + database, err := db.NewPostgres(dbURL) + if err != nil { + log.Fatalf("cannot connect to database: %s\n", err) + } + return database +} + +// Closes a gorm database +func closeDatabase(database *gorm.DB) { + if database, err := database.DB(); err == nil { + _ = database.Close() + } +} + +// getListener will start a listener based on environment variables +func getListener() net.Listener { + // Get protocol + protocol := "tcp" + if envProtocol := os.Getenv("LISTEN_PROTOCOL"); envProtocol != "" { + protocol = envProtocol + } + // Listen + listener, err := net.Listen(protocol, os.Getenv("LISTEN_ADDRESS")) + if err != nil { + log.Fatalf("cannot listen: %s", err) + } + return listener +} diff --git a/payment/go.mod b/payment/go.mod new file mode 100644 index 00000000..8b71b523 --- /dev/null +++ b/payment/go.mod @@ -0,0 +1,49 @@ +module wss-payment + +go 1.21 + +require ( + github.com/gin-gonic/gin v1.9.1 + github.com/go-faster/errors v0.7.1 + github.com/google/uuid v1.5.0 + gorm.io/driver/postgres v1.5.4 + gorm.io/gorm v1.25.5 +) + +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/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/goccy/go-json v0.10.2 // indirect + github.com/jackc/pgpassfile v1.0.0 // indirect + github.com/jackc/pgservicefile v0.0.0-20231201235250-de7065d80cb9 // indirect + github.com/jackc/pgx/v5 v5.5.1 // indirect + github.com/jackc/puddle/v2 v2.2.1 // 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/kr/text v0.2.0 // 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.1 // indirect + github.com/rogpeppe/go-internal v1.12.0 // indirect + github.com/sirupsen/logrus v1.9.3 // 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.17.0 // indirect + golang.org/x/net v0.19.0 // indirect + golang.org/x/sync v0.5.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.v3 v3.0.1 // indirect +) diff --git a/payment/go.sum b/payment/go.sum new file mode 100644 index 00000000..898b752b --- /dev/null +++ b/payment/go.sum @@ -0,0 +1,126 @@ +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/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= +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/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-faster/errors v0.7.1 h1:MkJTnDoEdi9pDabt1dpWf7AA8/BaSYZqibYyhZ20AYg= +github.com/go-faster/errors v0.7.1/go.mod h1:5ySTjWFiphBs07IKuiL69nxdfd5+fzh1u7FPGZP2quo= +github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s= +github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4= +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/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 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= +github.com/google/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU= +github.com/google/uuid v1.5.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM= +github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg= +github.com/jackc/pgservicefile v0.0.0-20231201235250-de7065d80cb9 h1:L0QtFUgDarD7Fpv9jeVMgy/+Ec0mtnmYuImjTz6dtDA= +github.com/jackc/pgservicefile v0.0.0-20231201235250-de7065d80cb9/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM= +github.com/jackc/pgx/v5 v5.5.1 h1:5I9etrGkLrN+2XPCsi6XLlV5DITbSL/xBZdmAxFcXPI= +github.com/jackc/pgx/v5 v5.5.1/go.mod h1:Ig06C2Vu0t5qXC60W8sqIthScaEnFvojjj9dSljmHRA= +github.com/jackc/puddle/v2 v2.2.1 h1:RhxXJtFG022u4ibrCSMSiu5aOq1i77R3OHKNJj77OAk= +github.com/jackc/puddle/v2 v2.2.1/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4= +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/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0= +github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk= +github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= +github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= +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.1 h1:LWAJwfNvjQZCFIDKWYQaM62NcYeYViCmWIwmOStowAI= +github.com/pelletier/go-toml/v2 v2.1.1/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc= +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/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8= +github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4= +github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ= +github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= +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 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= +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.17.0 h1:r8bRNjWL3GshPW3gkd+RpvzWrZAwPS49OmTGZ/uhM4k= +golang.org/x/crypto v0.17.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/sync v0.5.0 h1:60k92dhOjHxJkrqnwsfl8KuaHbn/5dl0lUPUklKo3qE= +golang.org/x/sync v0.5.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= +golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +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 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= +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/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= +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/postgres v1.5.4 h1:Iyrp9Meh3GmbSuyIAGyjkN+n9K+GHX9b9MqsTL4EJCo= +gorm.io/driver/postgres v1.5.4/go.mod h1:Bgo89+h0CRcdA33Y6frlaHHVuTdOf87pmyzwW9C/BH0= +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/payment/internal/database/pg.go b/payment/internal/database/pg.go new file mode 100644 index 00000000..568fc68b --- /dev/null +++ b/payment/internal/database/pg.go @@ -0,0 +1,23 @@ +package database + +import ( + "github.com/go-faster/errors" + "gorm.io/driver/postgres" + "gorm.io/gorm" +) + +// NewPostgres opens a postgres database and returns the connection +func NewPostgres(dsn string) (*gorm.DB, error) { + // Create database + db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{}) + if err != nil { + return nil, errors.Wrap(err, "cannot open database") + } + // Gorm automatically pings the database thus we can just migrate tables + err = db.AutoMigrate(&Payment{}) + if err != nil { + return nil, errors.Wrap(err, "cannot migrate database") + } + // GTG + return db, nil +} diff --git a/payment/internal/database/types.go b/payment/internal/database/types.go new file mode 100644 index 00000000..a702ee95 --- /dev/null +++ b/payment/internal/database/types.go @@ -0,0 +1,46 @@ +package database + +import ( + "github.com/google/uuid" + "time" +) + +type PayedGood uint8 + +const ( + PayedGoodOnlineAttendance PayedGood = 0 + PayedGoodInPersonAttendance PayedGood = 1 +) + +type PaymentStatus uint8 + +const ( + PaymentStatusInitiated PaymentStatus = 0 + PaymentStatusFailed PaymentStatus = 1 + PaymentStatusTimeout PaymentStatus = 2 + PaymentStatusSuccess PaymentStatus = 3 +) + +// Payment represents a payment made by a user +type Payment struct { + // The order ID which is sent to pay.ir + OrderID uuid.UUID `gorm:"primaryKey;type:uuid;default:gen_random_uuid()"` + // Who has made this payment? + UserID uint64 `gorm:"index;not null"` + // What is the amount that the user should pay? + ToPayAmount uint64 `gorm:"not null"` + // An optional description about this payment + Description string + // The track ID which idpay returns to us after verification + TrackID string + // The payment track ID which idpay returns to us after verification + PaymentTrackID string + // What product this user is buying + GoodType PayedGood `gorm:"not null"` + // What is the status of this payment? + PaymentStatus PaymentStatus `gorm:"not null"` + // When was this payment created? + CreatedAt time.Time `gorm:"not null"` + // When was it verified? (could be null) + VerifiedAt time.Time +} diff --git a/payment/pkg/idpay/debug.go b/payment/pkg/idpay/debug.go new file mode 100644 index 00000000..ea4b4e6e --- /dev/null +++ b/payment/pkg/idpay/debug.go @@ -0,0 +1,13 @@ +package idpay + +import "os" + +var debugMode = os.Getenv("IDPAY_DEBUG") == "TRUE" + +func getSandboxHeader() string { + if debugMode { + return "1" + } else { + return "0" + } +} diff --git a/payment/pkg/idpay/idpay.go b/payment/pkg/idpay/idpay.go new file mode 100644 index 00000000..6be90128 --- /dev/null +++ b/payment/pkg/idpay/idpay.go @@ -0,0 +1,90 @@ +package idpay + +import ( + "bytes" + "context" + "encoding/json" + "github.com/go-faster/errors" + log "github.com/sirupsen/logrus" + "net/http" + "os" +) + +// apiKey is the API key used for ID Pay +var apiKey = os.Getenv("IDPAY_KEY") + +// CreateTransaction will create a new transaction in ID pay and return its result (id and link) +func CreateTransaction(ctx context.Context, reqBody TransactionCreationRequest) (TransactionCreationResult, error) { + // Create the request + payload, _ := json.Marshal(reqBody) + req, _ := http.NewRequestWithContext(ctx, "POST", "https://api.idpay.ir/v1.1/payment", bytes.NewReader(payload)) + req.Header.Set("Content-Type", "application/json") + req.Header.Set("X-API-KEY", apiKey) + req.Header.Set("X-SANDBOX", getSandboxHeader()) + // Send the request + resp, err := http.DefaultClient.Do(req) + if err != nil { + return TransactionCreationResult{}, errors.Wrap(err, "cannot send request") + } + defer resp.Body.Close() + // Check results + if resp.StatusCode/100 == 2 { // 2xx, ok + var body TransactionCreationResult + err = json.NewDecoder(resp.Body).Decode(&body) + if err != nil { + return TransactionCreationResult{}, errors.Wrap(err, "cannot parse body") + } + return body, nil + } else { // fuckup + var body ErrorMessage + err = json.NewDecoder(resp.Body).Decode(&body) + if err != nil { + return TransactionCreationResult{}, errors.Wrap(err, "cannot parse body") + } + return TransactionCreationResult{}, errors.Errorf("not 2xx status code: %d with error message %s", resp.StatusCode, body.ErrorMessage) + } +} + +// VerifyTransaction will verify a previously made transaction and report errors if there was a problem with it +func VerifyTransaction(ctx context.Context, reqBody TransactionVerificationRequest) (TransactionVerificationResult, error) { + // Create the request + payload, _ := json.Marshal(reqBody) + req, _ := http.NewRequestWithContext(ctx, "POST", "https://api.idpay.ir/v1.1/payment/verify", bytes.NewReader(payload)) + req.Header.Set("Content-Type", "application/json") + req.Header.Set("X-API-KEY", apiKey) + req.Header.Set("X-SANDBOX", getSandboxHeader()) + // Send the request + resp, err := http.DefaultClient.Do(req) + if err != nil { + return TransactionVerificationResult{}, errors.Wrap(err, "cannot send request") + } + defer resp.Body.Close() + // Check results + if resp.StatusCode/100 == 2 { // 2xx, ok + var body TransactionVerificationResponse + err = json.NewDecoder(resp.Body).Decode(&body) + if err != nil { + return TransactionVerificationResult{}, errors.Wrap(err, "cannot parse body") + } + return TransactionVerificationResult{ + PaymentOK: isPaymentOk(body), + TrackID: body.TrackId, + PaymentTrackID: body.Payment.TrackId, + }, nil + } else { // fuckup + var body ErrorMessage + err = json.NewDecoder(resp.Body).Decode(&body) + if err != nil { + return TransactionVerificationResult{}, errors.Wrap(err, "cannot parse body") + } + return TransactionVerificationResult{}, errors.Errorf("not 2xx status code: %d with error message %s", resp.StatusCode, body.ErrorMessage) + } +} + +// isPaymentOk checks if a payment was ok based on the result of verification endpoint or not +func isPaymentOk(resp TransactionVerificationResponse) bool { + if resp.Status == "101" { + log.WithField("resp", resp).Warn("double verification") + } + return resp.Status == "100" || resp.Status == "101" || resp.Status == "200" +} diff --git a/payment/pkg/idpay/types.go b/payment/pkg/idpay/types.go new file mode 100644 index 00000000..5d9836b4 --- /dev/null +++ b/payment/pkg/idpay/types.go @@ -0,0 +1,44 @@ +package idpay + +type ErrorMessage struct { + ErrorMessage string `json:"error_message"` + ErrorCode int `json:"error_code"` +} + +type TransactionCreationRequest struct { + // This must be a randomly generated ID + OrderID string `json:"order_id"` + Name string `json:"name"` + Phone string `json:"phone"` + Mail string `json:"mail"` + Description string `json:"desc"` + Callback string `json:"callback"` + Amount uint64 `json:"amount"` +} + +type TransactionCreationResult struct { + ID string `json:"id"` + Link string `json:"link"` +} + +type TransactionVerificationRequest struct { + OrderID string `json:"order_id"` + ID string `json:"id"` +} + +type TransactionVerificationResponse struct { + Status string `json:"status"` + TrackId string `json:"track_id"` + Payment struct { + TrackId string `json:"track_id"` + } `json:"payment"` +} + +type TransactionVerificationResult struct { + // The track ID which idpay returns to us after verification + TrackID string + // The payment track ID which idpay returns to us after verification + PaymentTrackID string + // Was the payment OK? + PaymentOK bool +}