payment-poc/api/routes.go

85 lines
3.3 KiB
Go
Raw Normal View History

2024-04-01 18:29:24 +00:00
package api
import (
"github.com/gin-gonic/gin"
"github.com/jmoiron/sqlx"
"log/slog"
"payment-poc/domain/database"
"payment-poc/domain/providers"
"payment-poc/domain/providers/mock"
stripe2 "payment-poc/domain/providers/stripe"
"payment-poc/domain/providers/viva"
wspay2 "payment-poc/domain/providers/wspay"
"payment-poc/domain/state"
)
func RegisterRoutes(server *gin.Engine, db *sqlx.DB) {
backendUrl := envMustExist("BACKEND_URL")
paymentGateways := map[state.PaymentGateway]providers.PaymentProvider{}
entryProvider := &database.PaymentEntryProvider{DB: db}
server.GET("/", GetIndex(entryProvider))
server.GET("/methods", GetGateways(paymentGateways))
server.GET("/methods/:gateway", InitializePayment(entryProvider, paymentGateways))
server.GET("/entries/:id", GetEntry(entryProvider))
server.POST("/entries/:id/complete", CompletePayment(entryProvider, paymentGateways))
server.POST("/entries/:id/cancel", CancelPayment(entryProvider, paymentGateways))
server.POST("/entries/:id/refresh", RefreshPayment(entryProvider, paymentGateways))
if hasProfile(string(state.GatewayMock)) {
mockService := mock.Service{
BackendUrl: backendUrl,
}
paymentGateways[state.GatewayMock] = &mockService
mockGroup := server.Group("mock")
mockGroup.GET("/gateway/:id", MockOpenGateway(entryProvider))
mockGroup.GET("success", MockOnSuccess(mockService, entryProvider))
mockGroup.GET("error", MockOnFailure(mockService, entryProvider))
slog.Info("Registered provider", slog.Any("provider", state.GatewayMock))
}
if hasProfile(string(state.GatewayWsPay)) {
wspayService := wspay2.Service{
ShopId: envMustExist("WSPAY_SHOP_ID"),
ShopSecret: envMustExist("WSPAY_SHOP_SECRET"),
BackendUrl: backendUrl,
}
paymentGateways[state.GatewayWsPay] = &wspayService
wspayGroup := server.Group("wspay")
wspayGroup.GET("success", WsPayOnSuccess(wspayService, entryProvider))
wspayGroup.GET("error", WsPayOnFailure(wspayService, entryProvider, state.StateError))
wspayGroup.GET("cancel", WsPayOnFailure(wspayService, entryProvider, state.StateCanceled))
slog.Info("Registered provider", slog.Any("provider", state.GatewayWsPay))
}
if hasProfile(string(state.GatewayStripe)) {
stripeService := stripe2.Service{
ApiKey: envMustExist("STRIPE_KEY"),
BackendUrl: backendUrl,
}
paymentGateways[state.GatewayStripe] = &stripeService
stripeGroup := server.Group("stripe")
stripeGroup.GET("success", StripeOnSuccess(stripeService, entryProvider))
stripeGroup.GET("error", StripeOnFailure(stripeService, entryProvider))
slog.Info("Registered provider", slog.Any("provider", state.GatewayStripe))
}
if hasProfile(string(state.GatewayVivaWallet)) {
vivaService := viva.Service{
ClientId: envMustExist("VIVA_WALLET_CLIENT_ID"),
ClientSecret: envMustExist("VIVA_WALLET_CLIENT_SECRET"),
SourceCode: envMustExist("VIVA_WALLET_SOURCE_CODE"),
MerchantId: envMustExist("VIVA_WALLET_MERCHANT_ID"),
ApiKey: envMustExist("VIVA_WALLET_API_KEY"),
}
paymentGateways[state.GatewayVivaWallet] = &vivaService
vivaGroup := server.Group("viva")
vivaGroup.GET("success", VivaOnSuccess(vivaService, entryProvider))
vivaGroup.GET("error", VivaOnFailure(vivaService, entryProvider))
slog.Info("Registered provider", slog.Any("provider", state.GatewayVivaWallet))
}
}