Reorganized code + migration to go1.22
This commit is contained in:
1 parent
e3d77d55b9
commit
7369777098
25 files changed
+825
-605
No files matched your search
@@ -0,0 +1,55 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"github.com/google/uuid"
|
||||
"payment-poc/domain/state"
|
||||
"time"
|
||||
)
|
||||
|
||||
type PaymentEntry struct {
|
||||
Id uuid.UUID `db:"id"`
|
||||
|
||||
Created time.Time `db:"created"`
|
||||
Modified *time.Time `db:"modified"`
|
||||
|
||||
Gateway state.PaymentGateway `db:"gateway"`
|
||||
State state.PaymentState `db:"state"`
|
||||
|
||||
Lang *string `db:"lang"`
|
||||
|
||||
Error *string `db:"error"`
|
||||
|
||||
// paid amount
|
||||
Amount *int64 `db:"amount"`
|
||||
// preauthorized amount
|
||||
TotalAmount int64 `db:"total_amount"`
|
||||
|
||||
// used for wspay and viva
|
||||
ECI *string `db:"eci"`
|
||||
|
||||
// stripe field
|
||||
PaymentIntentId *string `db:"payment_intent_id"`
|
||||
|
||||
// wspay field
|
||||
WsPayOrderId string `db:"ws_pay_order_id"`
|
||||
ShoppingCardID *string `db:"shopping_card_id"`
|
||||
STAN *string `db:"stan"`
|
||||
Success *int `db:"success"`
|
||||
ApprovalCode *string `db:"approval_code"`
|
||||
|
||||
// viva field
|
||||
OrderId *OrderId `db:"order_id"`
|
||||
TransactionId *uuid.UUID `db:"transaction_id"`
|
||||
EventId *string `db:"event_id"`
|
||||
}
|
||||
|
||||
type OrderId string
|
||||
|
||||
func (o OrderId) MarshalJSON() ([]byte, error) {
|
||||
return []byte(o), nil
|
||||
}
|
||||
|
||||
func (o *OrderId) UnmarshalJSON(value []byte) error {
|
||||
*o = OrderId(value)
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
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") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17)`,
|
||||
&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, "ws_pay_order_id" = $16 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, &entry.WsPayOrderId,
|
||||
)
|
||||
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
|
||||
}
|
||||
Reference in new issue
Block a user