Organization + added logs

This commit is contained in:
Borna Rajković 2023-07-31 10:01:37 +02:00
parent fe6f3b6672
commit 7b2523e2b1
8 changed files with 33 additions and 15 deletions

26
main.go
View File

@ -14,11 +14,11 @@ import (
"os" "os"
"payment-poc/database" "payment-poc/database"
"payment-poc/migration" "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" "payment-poc/state"
stripe2 "payment-poc/stripe"
"payment-poc/viva"
"payment-poc/wspay"
"strconv" "strconv"
"strings" "strings"
"time" "time"
@ -85,16 +85,18 @@ func main() {
} }
mockHandlers(g.Group("mock"), entryProvider, &mockService) mockHandlers(g.Group("mock"), entryProvider, &mockService)
paymentGateways[state.GatewayMock] = &mockService paymentGateways[state.GatewayMock] = &mockService
log.Printf("Registered provider: %s", state.GatewayMock)
} }
if hasProfile(string(state.GatewayWsPay)) { if hasProfile(string(state.GatewayWsPay)) {
wspayService := wspay.Service{ wspayService := wspay2.Service{
ShopId: envMustExist("WSPAY_SHOP_ID"), ShopId: envMustExist("WSPAY_SHOP_ID"),
ShopSecret: envMustExist("WSPAY_SHOP_SECRET"), ShopSecret: envMustExist("WSPAY_SHOP_SECRET"),
BackendUrl: backendUrl, BackendUrl: backendUrl,
} }
wsPayHandlers(g.Group("wspay"), entryProvider, &wspayService) wsPayHandlers(g.Group("wspay"), entryProvider, &wspayService)
paymentGateways[state.GatewayWsPay] = &wspayService paymentGateways[state.GatewayWsPay] = &wspayService
log.Printf("Registered provider: %s", state.GatewayWsPay)
} }
if hasProfile(string(state.GatewayStripe)) { if hasProfile(string(state.GatewayStripe)) {
stripeService := stripe2.Service{ stripeService := stripe2.Service{
@ -104,6 +106,7 @@ func main() {
stripeHandlers(g.Group("stripe"), entryProvider, &stripeService) stripeHandlers(g.Group("stripe"), entryProvider, &stripeService)
paymentGateways[state.GatewayStripe] = &stripeService paymentGateways[state.GatewayStripe] = &stripeService
stripe.Key = envMustExist("STRIPE_KEY") stripe.Key = envMustExist("STRIPE_KEY")
log.Printf("Registered provider: %s", state.GatewayStripe)
} }
if hasProfile(string(state.GatewayVivaWallet)) { if hasProfile(string(state.GatewayVivaWallet)) {
vivaService := viva.Service{ vivaService := viva.Service{
@ -115,6 +118,7 @@ func main() {
} }
vivaHandlers(g.Group("viva"), entryProvider, &vivaService) vivaHandlers(g.Group("viva"), entryProvider, &vivaService)
paymentGateways[state.GatewayVivaWallet] = &vivaService paymentGateways[state.GatewayVivaWallet] = &vivaService
log.Printf("Registered provider: %s", state.GatewayVivaWallet)
} }
g.GET("/", func(c *gin.Context) { g.GET("/", func(c *gin.Context) {
@ -145,7 +149,9 @@ func main() {
State: state.StatePreinitialized, State: state.StatePreinitialized,
TotalAmount: amount, 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 { if entry, url, err := paymentGateway.CreatePaymentUrl(entry); err == nil {
log.Printf("[%s:%s] created redirect url", entry.Id, entry.State)
entryProvider.UpdateEntry(entry) entryProvider.UpdateEntry(entry)
c.Redirect(http.StatusSeeOther, url) c.Redirect(http.StatusSeeOther, url)
} else { } else {
@ -179,9 +185,11 @@ func main() {
c.AbortWithError(http.StatusBadRequest, err) c.AbortWithError(http.StatusBadRequest, err)
return return
} }
log.Printf("[%s:%s] completing payment with amount %f", id.String(), entry.State, float64(amount)/100.0)
entry, err = paymentGateway.CompleteTransaction(entry, amount) entry, err = paymentGateway.CompleteTransaction(entry, amount)
if err == nil { if err == nil {
entryProvider.UpdateEntry(entry) entryProvider.UpdateEntry(entry)
log.Printf("[%s:%s] completed payment", id.String(), entry.State)
c.Redirect(http.StatusSeeOther, "/entries/"+id.String()) c.Redirect(http.StatusSeeOther, "/entries/"+id.String())
} else { } else {
c.AbortWithError(http.StatusInternalServerError, err) c.AbortWithError(http.StatusInternalServerError, err)
@ -202,9 +210,11 @@ func main() {
return return
} }
if paymentGateway, ok := paymentGateways[entry.Gateway]; ok { if paymentGateway, ok := paymentGateways[entry.Gateway]; ok {
log.Printf("[%s:%s] canceling payment", id.String(), entry.State)
entry, err = paymentGateway.CancelTransaction(entry) entry, err = paymentGateway.CancelTransaction(entry)
if err == nil { if err == nil {
entryProvider.UpdateEntry(entry) entryProvider.UpdateEntry(entry)
log.Printf("[%s:%s] canceled payment", id.String(), entry.State)
c.Redirect(http.StatusSeeOther, "/entries/"+id.String()) c.Redirect(http.StatusSeeOther, "/entries/"+id.String())
} else { } else {
c.AbortWithError(http.StatusInternalServerError, err) c.AbortWithError(http.StatusInternalServerError, err)
@ -225,9 +235,11 @@ func main() {
return return
} }
if paymentGateway, ok := paymentGateways[entry.Gateway]; ok { if paymentGateway, ok := paymentGateways[entry.Gateway]; ok {
log.Printf("[%s:%s] fetching payment info", entry.Id.String(), entry.State)
entry, err = paymentGateway.UpdatePayment(entry) entry, err = paymentGateway.UpdatePayment(entry)
if err == nil { if err == nil {
entryProvider.UpdateEntry(entry) entryProvider.UpdateEntry(entry)
log.Printf("[%s:%s] fetched payment info", entry.Id.String(), entry.State)
} }
c.Redirect(http.StatusSeeOther, "/entries/"+id.String()) c.Redirect(http.StatusSeeOther, "/entries/"+id.String())
} else { } 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) { g.GET("/initialize/:id", func(c *gin.Context) {
entry, err := provider.FetchById(uuid.MustParse(c.Param("id"))) entry, err := provider.FetchById(uuid.MustParse(c.Param("id")))
if err != nil { if err != nil {
@ -371,7 +383,7 @@ func wsPayHandlers(g *gin.RouterGroup, provider *database.PaymentEntryProvider,
} }
form := wspayService.InitializePayment(entry) 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) { g.GET("success", func(c *gin.Context) {

View File

@ -3,6 +3,7 @@ package mock
import ( import (
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/google/uuid" "github.com/google/uuid"
"log"
"payment-poc/database" "payment-poc/database"
"payment-poc/state" "payment-poc/state"
) )
@ -38,5 +39,6 @@ func (s Service) HandleResponse(c *gin.Context, provider *database.PaymentEntryP
} }
entry.State = paymentState entry.State = paymentState
_, err = provider.UpdateEntry(entry) _, err = provider.UpdateEntry(entry)
log.Printf("[%s:%s] received authorization response", entry.Id.String(), entry.State)
return "/entries/" + id.String(), err return "/entries/" + id.String(), err
} }

View File

@ -24,7 +24,7 @@ func (s *Service) UpdatePayment(entry database.PaymentEntry) (updatedEntry datab
newState := determineState(pi.Status) newState := determineState(pi.Status)
if entry.State != newState && newState != "" { 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 { if pi.AmountReceived > 0 {
entry.Amount = &pi.AmountReceived entry.Amount = &pi.AmountReceived
} }
@ -135,6 +135,9 @@ func (s *Service) HandleResponse(c *gin.Context, provider *database.PaymentEntry
return "", err return "", err
} }
entry.State = paymentState 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 return "/entries/" + entry.Id.String(), nil
} }

View File

@ -49,7 +49,7 @@ func (s *Service) UpdatePayment(entry database.PaymentEntry) (updatedEntry datab
newState := determineStatus(response.StatusId) newState := determineStatus(response.StatusId)
if entry.State != newState && newState != "" { 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 entry.State = newState
} }
return entry, nil return entry, nil
@ -248,10 +248,10 @@ func (s *Service) HandleResponse(c *gin.Context, provider *database.PaymentEntry
eventId := c.Query("eventId") eventId := c.Query("eventId")
eci := c.Query("eci") 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) entry, err := provider.FetchByOrderId(orderId)
if err != nil { 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 return "", err
} }
@ -265,6 +265,7 @@ func (s *Service) HandleResponse(c *gin.Context, provider *database.PaymentEntry
return "", err 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 return "/entries/" + entry.Id.String(), nil
} }

View File

@ -20,8 +20,8 @@
<section class="container"> <section class="container">
<h2>Mock gateway {{.Entry.Id.String}}</h2> <h2>Mock gateway {{.Entry.Id.String}}</h2>
<p>{{formatCurrency .Entry.TotalAmount}}</p> <p>{{formatCurrency .Entry.TotalAmount}}</p>
<a href="/mock/success?id={{.Entry.Id.String}}" class="btn btn-success">Potvrdi plaćanje</a> <a href="/providers/mock/success?id={{.Entry.Id.String}}" class="btn btn-success">Potvrdi plaćanje</a>
<a href="/mock/error?id={{.Entry.Id.String}}" class="btn btn-danger">Otkaži plaćanje</a> <a href="/providers/mock/error?id={{.Entry.Id.String}}" class="btn btn-danger">Otkaži plaćanje</a>
</section> </section>
</body> </body>
</html> </html>