diff --git a/main.go b/main.go index 17fe5e8..9897dbd 100644 --- a/main.go +++ b/main.go @@ -14,11 +14,11 @@ import ( "os" "payment-poc/database" "payment-poc/migration" - "payment-poc/mock" + "payment-poc/providers/mock" + stripe2 "payment-poc/providers/stripe" + "payment-poc/providers/viva" + wspay2 "payment-poc/providers/wspay" "payment-poc/state" - stripe2 "payment-poc/stripe" - "payment-poc/viva" - "payment-poc/wspay" "strconv" "strings" "time" @@ -85,16 +85,18 @@ func main() { } mockHandlers(g.Group("mock"), entryProvider, &mockService) paymentGateways[state.GatewayMock] = &mockService + log.Printf("Registered provider: %s", state.GatewayMock) } if hasProfile(string(state.GatewayWsPay)) { - wspayService := wspay.Service{ + wspayService := wspay2.Service{ ShopId: envMustExist("WSPAY_SHOP_ID"), ShopSecret: envMustExist("WSPAY_SHOP_SECRET"), BackendUrl: backendUrl, } wsPayHandlers(g.Group("wspay"), entryProvider, &wspayService) paymentGateways[state.GatewayWsPay] = &wspayService + log.Printf("Registered provider: %s", state.GatewayWsPay) } if hasProfile(string(state.GatewayStripe)) { stripeService := stripe2.Service{ @@ -104,6 +106,7 @@ func main() { stripeHandlers(g.Group("stripe"), entryProvider, &stripeService) paymentGateways[state.GatewayStripe] = &stripeService stripe.Key = envMustExist("STRIPE_KEY") + log.Printf("Registered provider: %s", state.GatewayStripe) } if hasProfile(string(state.GatewayVivaWallet)) { vivaService := viva.Service{ @@ -115,6 +118,7 @@ func main() { } vivaHandlers(g.Group("viva"), entryProvider, &vivaService) paymentGateways[state.GatewayVivaWallet] = &vivaService + log.Printf("Registered provider: %s", state.GatewayVivaWallet) } g.GET("/", func(c *gin.Context) { @@ -145,7 +149,9 @@ func main() { State: state.StatePreinitialized, TotalAmount: amount, }) + log.Printf("[%s:%s] creating payment with gateway '%s' for '%f'", entry.Id.String(), entry.State, gateway, float64(amount)/100.0) if entry, url, err := paymentGateway.CreatePaymentUrl(entry); err == nil { + log.Printf("[%s:%s] created redirect url", entry.Id, entry.State) entryProvider.UpdateEntry(entry) c.Redirect(http.StatusSeeOther, url) } else { @@ -179,9 +185,11 @@ func main() { c.AbortWithError(http.StatusBadRequest, err) return } + log.Printf("[%s:%s] completing payment with amount %f", id.String(), entry.State, float64(amount)/100.0) entry, err = paymentGateway.CompleteTransaction(entry, amount) if err == nil { entryProvider.UpdateEntry(entry) + log.Printf("[%s:%s] completed payment", id.String(), entry.State) c.Redirect(http.StatusSeeOther, "/entries/"+id.String()) } else { c.AbortWithError(http.StatusInternalServerError, err) @@ -202,9 +210,11 @@ func main() { return } if paymentGateway, ok := paymentGateways[entry.Gateway]; ok { + log.Printf("[%s:%s] canceling payment", id.String(), entry.State) entry, err = paymentGateway.CancelTransaction(entry) if err == nil { entryProvider.UpdateEntry(entry) + log.Printf("[%s:%s] canceled payment", id.String(), entry.State) c.Redirect(http.StatusSeeOther, "/entries/"+id.String()) } else { c.AbortWithError(http.StatusInternalServerError, err) @@ -225,9 +235,11 @@ func main() { return } if paymentGateway, ok := paymentGateways[entry.Gateway]; ok { + log.Printf("[%s:%s] fetching payment info", entry.Id.String(), entry.State) entry, err = paymentGateway.UpdatePayment(entry) if err == nil { entryProvider.UpdateEntry(entry) + log.Printf("[%s:%s] fetched payment info", entry.Id.String(), entry.State) } c.Redirect(http.StatusSeeOther, "/entries/"+id.String()) } else { @@ -358,7 +370,7 @@ func stripeHandlers(g *gin.RouterGroup, provider *database.PaymentEntryProvider, }) } -func wsPayHandlers(g *gin.RouterGroup, provider *database.PaymentEntryProvider, wspayService *wspay.Service) { +func wsPayHandlers(g *gin.RouterGroup, provider *database.PaymentEntryProvider, wspayService *wspay2.Service) { g.GET("/initialize/:id", func(c *gin.Context) { entry, err := provider.FetchById(uuid.MustParse(c.Param("id"))) if err != nil { @@ -371,7 +383,7 @@ func wsPayHandlers(g *gin.RouterGroup, provider *database.PaymentEntryProvider, } form := wspayService.InitializePayment(entry) - c.HTML(200, "wspay.gohtml", gin.H{"Action": wspay.AuthorisationForm, "Form": form}) + c.HTML(200, "wspay.gohtml", gin.H{"Action": wspay2.AuthorisationForm, "Form": form}) }) g.GET("success", func(c *gin.Context) { diff --git a/mock/service.go b/providers/mock/service.go similarity index 92% rename from mock/service.go rename to providers/mock/service.go index 8c9e555..b68ca31 100644 --- a/mock/service.go +++ b/providers/mock/service.go @@ -3,6 +3,7 @@ package mock import ( "github.com/gin-gonic/gin" "github.com/google/uuid" + "log" "payment-poc/database" "payment-poc/state" ) @@ -38,5 +39,6 @@ func (s Service) HandleResponse(c *gin.Context, provider *database.PaymentEntryP } entry.State = paymentState _, err = provider.UpdateEntry(entry) + log.Printf("[%s:%s] received authorization response", entry.Id.String(), entry.State) return "/entries/" + id.String(), err } diff --git a/stripe/service.go b/providers/stripe/service.go similarity index 94% rename from stripe/service.go rename to providers/stripe/service.go index 259ae47..04e68e7 100644 --- a/stripe/service.go +++ b/providers/stripe/service.go @@ -24,7 +24,7 @@ func (s *Service) UpdatePayment(entry database.PaymentEntry) (updatedEntry datab newState := determineState(pi.Status) if entry.State != newState && newState != "" { - log.Printf("Updated state for %s: %s -> %s", entry.Id.String(), entry.State, newState) + log.Printf("[%s] updated state for %s -> %s", entry.Id.String(), entry.State, newState) if pi.AmountReceived > 0 { entry.Amount = &pi.AmountReceived } @@ -135,6 +135,9 @@ func (s *Service) HandleResponse(c *gin.Context, provider *database.PaymentEntry return "", err } entry.State = paymentState - provider.UpdateEntry(entry) + if _, err := provider.UpdateEntry(entry); err != nil { + return "", err + } + log.Printf("[%s:%s] received authorization response", entry.Id.String(), entry.State) return "/entries/" + entry.Id.String(), nil } diff --git a/viva/service.go b/providers/viva/service.go similarity index 95% rename from viva/service.go rename to providers/viva/service.go index fc3b710..baaa5b4 100644 --- a/viva/service.go +++ b/providers/viva/service.go @@ -49,7 +49,7 @@ func (s *Service) UpdatePayment(entry database.PaymentEntry) (updatedEntry datab newState := determineStatus(response.StatusId) if entry.State != newState && newState != "" { - log.Printf("Updated state for %s: %s -> %s", entry.Id.String(), entry.State, newState) + log.Printf("[%s:%s] updated state %s -> %s", entry.Id.String(), entry.State, entry.State, newState) entry.State = newState } return entry, nil @@ -248,10 +248,10 @@ func (s *Service) HandleResponse(c *gin.Context, provider *database.PaymentEntry eventId := c.Query("eventId") eci := c.Query("eci") - log.Printf("Received error response for viva payment %s", orderId) + log.Printf("[%s] received error response for viva payment", orderId) entry, err := provider.FetchByOrderId(orderId) if err != nil { - log.Printf("Couldn't find payment info for viva payment %s", orderId) + log.Printf("[%s] couldn't find payment info for viva payment", orderId) return "", err } @@ -265,6 +265,7 @@ func (s *Service) HandleResponse(c *gin.Context, provider *database.PaymentEntry return "", err } - log.Printf("Viva payment %s received correctly, returning redirect", orderId) + log.Printf("[%s:%s] received authorization response", entry.Id.String(), entry.State) + return "/entries/" + entry.Id.String(), nil } diff --git a/viva/viva.go b/providers/viva/viva.go similarity index 100% rename from viva/viva.go rename to providers/viva/viva.go diff --git a/wspay/service.go b/providers/wspay/service.go similarity index 100% rename from wspay/service.go rename to providers/wspay/service.go diff --git a/wspay/wspay.go b/providers/wspay/wspay.go similarity index 100% rename from wspay/wspay.go rename to providers/wspay/wspay.go diff --git a/templates/mock_gateway.gohtml b/templates/mock_gateway.gohtml index 6b9b436..5308e31 100644 --- a/templates/mock_gateway.gohtml +++ b/templates/mock_gateway.gohtml @@ -20,8 +20,8 @@

Mock gateway {{.Entry.Id.String}}

{{formatCurrency .Entry.TotalAmount}}

- Potvrdi plaćanje - Otkaži plaćanje + Potvrdi plaćanje + Otkaži plaćanje
\ No newline at end of file