58 lines
2.3 KiB
Go
58 lines
2.3 KiB
Go
|
package database
|
||
|
|
||
|
import (
|
||
|
"github.com/google/uuid"
|
||
|
"github.com/jmoiron/sqlx"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
type PaymentEntryProvider struct {
|
||
|
DB *sqlx.DB
|
||
|
}
|
||
|
|
||
|
func (p *PaymentEntryProvider) CreateEntry(entry PaymentEntry) (PaymentEntry, error) {
|
||
|
if entry.Id == uuid.Nil {
|
||
|
entry.Id = uuid.Must(uuid.NewRandom())
|
||
|
}
|
||
|
entry.Created = time.Now()
|
||
|
|
||
|
_, err := p.DB.Exec(`INSERT INTO "payment_entry" ("id", "created", "gateway", "state", "lang", "error", "amount", "total_amount", "eci", "payment_intent_id", "shopping_card_id", "stan", "success", "approval_code", "order_id", "transaction_id", "event_id")`,
|
||
|
&entry.Id, &entry.Created, &entry.Gateway, &entry.State, &entry.Lang, &entry.Error, &entry.Amount, &entry.TotalAmount, &entry.ECI, &entry.PaymentIntentId, &entry.ShoppingCardID, &entry.STAN, &entry.Success, &entry.ApprovalCode, &entry.OrderId, &entry.TransactionId, &entry.EventId,
|
||
|
)
|
||
|
if err != nil {
|
||
|
return PaymentEntry{}, err
|
||
|
}
|
||
|
return p.FetchById(entry.Id)
|
||
|
}
|
||
|
|
||
|
func (p *PaymentEntryProvider) UpdateEntry(entry PaymentEntry) (PaymentEntry, error) {
|
||
|
currentTime := time.Now()
|
||
|
entry.Modified = ¤tTime
|
||
|
|
||
|
_, err := p.DB.Exec(`UPDATE "payment_entry" SET "modified" = $2, "state" = $3, "lang" = $4, "error" = $5, "amount" = $6, "eci" = $7, "payment_intent_id" = $8, "shopping_card_id" = $9, "stan" = $10, "success" = $11, "approval_code" = $12, "order_id" = $13, "transaction_id" = $14, "event_id" = $15 WHERE "id" = $1`,
|
||
|
&entry.Id, &entry.Modified, &entry.State, &entry.Lang, &entry.Error, &entry.Amount, &entry.ECI, &entry.PaymentIntentId, &entry.ShoppingCardID, &entry.STAN, &entry.Success, &entry.ApprovalCode, &entry.OrderId, &entry.TransactionId, &entry.EventId,
|
||
|
)
|
||
|
if err != nil {
|
||
|
return PaymentEntry{}, err
|
||
|
}
|
||
|
return p.FetchById(entry.Id)
|
||
|
}
|
||
|
|
||
|
func (p *PaymentEntryProvider) FetchById(id uuid.UUID) (PaymentEntry, error) {
|
||
|
entry := PaymentEntry{}
|
||
|
err := p.DB.Get(&entry, `SELECT * FROM "payment_entry" WHERE "id" = $1`, id)
|
||
|
return entry, err
|
||
|
}
|
||
|
|
||
|
func (p *PaymentEntryProvider) FetchAll() ([]PaymentEntry, error) {
|
||
|
var entries []PaymentEntry
|
||
|
err := p.DB.Select(&entries, `SELECT * FROM "payment_entry"`)
|
||
|
return entries, err
|
||
|
}
|
||
|
|
||
|
func (p *PaymentEntryProvider) FetchByOrderId(orderId OrderId) (PaymentEntry, error) {
|
||
|
entry := PaymentEntry{}
|
||
|
err := p.DB.Get(&entry, `SELECT * FROM "payment_entry" WHERE "order_id" = $1`, orderId)
|
||
|
return entry, err
|
||
|
}
|