47 lines
1.1 KiB
Go
47 lines
1.1 KiB
Go
|
package stripe
|
||
|
|
||
|
import (
|
||
|
"github.com/google/uuid"
|
||
|
"github.com/jmoiron/sqlx"
|
||
|
"payment-poc/state"
|
||
|
)
|
||
|
|
||
|
type Service struct {
|
||
|
DB *sqlx.DB
|
||
|
}
|
||
|
|
||
|
func (s *Service) CreateEntry(totalAmount int64) (StripeDb, error) {
|
||
|
id := uuid.Must(uuid.NewRandom())
|
||
|
entry := StripeDb{
|
||
|
Id: id,
|
||
|
TotalAmount: totalAmount,
|
||
|
State: state.StateInitialized,
|
||
|
}
|
||
|
_, err := s.DB.Exec(`INSERT INTO "stripe" ("id", "total_amount", "payment_state") VALUES ($1, $2, $3)`,
|
||
|
&entry.Id, &entry.TotalAmount, &entry.State,
|
||
|
)
|
||
|
if err != nil {
|
||
|
return StripeDb{}, err
|
||
|
}
|
||
|
return s.FetchById(id)
|
||
|
}
|
||
|
|
||
|
func (s *Service) FetchAll() ([]StripeDb, error) {
|
||
|
var entries []StripeDb
|
||
|
err := s.DB.Select(&entries, `SELECT * FROM "stripe"`)
|
||
|
return entries, err
|
||
|
}
|
||
|
|
||
|
func (s *Service) FetchById(id uuid.UUID) (StripeDb, error) {
|
||
|
entry := StripeDb{}
|
||
|
err := s.DB.Get(&entry, `SELECT * FROM "stripe" WHERE "id" = $1`, id)
|
||
|
return entry, err
|
||
|
}
|
||
|
|
||
|
func (s *Service) Update(entry StripeDb) error {
|
||
|
_, err := s.DB.Exec(`UPDATE "stripe" set "payment_intent_id" = $2, "payment_state" = $3 WHERE "id" = $1`,
|
||
|
&entry.Id, &entry.PaymentIntentId, &entry.State,
|
||
|
)
|
||
|
return err
|
||
|
}
|