diff --git a/mock/service.go b/mock/service.go new file mode 100644 index 0000000..8c9e555 --- /dev/null +++ b/mock/service.go @@ -0,0 +1,42 @@ +package mock + +import ( + "github.com/gin-gonic/gin" + "github.com/google/uuid" + "payment-poc/database" + "payment-poc/state" +) + +type Service struct { + BackendUrl string +} + +func (s Service) UpdatePayment(entry database.PaymentEntry) (updatedEntry database.PaymentEntry, err error) { + return entry, nil +} + +func (s Service) CreatePaymentUrl(entry database.PaymentEntry) (updateEntry database.PaymentEntry, url string, err error) { + return entry, "/mock/gateway/" + entry.Id.String(), nil +} + +func (s Service) CompleteTransaction(entry database.PaymentEntry, amount int64) (database.PaymentEntry, error) { + entry.Amount = &amount + entry.State = state.StateCompleted + return entry, nil +} + +func (s Service) CancelTransaction(entry database.PaymentEntry) (database.PaymentEntry, error) { + entry.State = state.StateVoided + return entry, nil +} + +func (s Service) HandleResponse(c *gin.Context, provider *database.PaymentEntryProvider, paymentState state.PaymentState) (string, error) { + id := uuid.MustParse(c.Query("id")) + entry, err := provider.FetchById(id) + if err != nil { + return "", err + } + entry.State = paymentState + _, err = provider.UpdateEntry(entry) + return "/entries/" + id.String(), err +}