diff --git a/database/model.go b/database/model.go
index 906b7df..f53425a 100644
--- a/database/model.go
+++ b/database/model.go
@@ -31,7 +31,7 @@ type PaymentEntry struct {
PaymentIntentId *string `db:"payment_intent_id"`
// wspay field
- ShoppingCardID *string `db:"shopping_card_it"`
+ ShoppingCardID *string `db:"shopping_card_id"`
STAN *string `db:"stan"`
Success *int `db:"success"`
ApprovalCode *string `db:"approval_code"`
diff --git a/database/provider.go b/database/provider.go
index 0050ae7..ab0099c 100644
--- a/database/provider.go
+++ b/database/provider.go
@@ -16,7 +16,7 @@ func (p *PaymentEntryProvider) CreateEntry(entry PaymentEntry) (PaymentEntry, er
}
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")`,
+ _, 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 {
diff --git a/db/dev/v1_0.sql b/db/dev/v1_0.sql
index a6a8494..0d3ecce 100644
--- a/db/dev/v1_0.sql
+++ b/db/dev/v1_0.sql
@@ -1,65 +1,30 @@
-
-CREATE TABLE IF NOT EXISTS "wspay"
+CREATE TABLE IF NOT EXISTS "payment_entry"
(
"id" uuid NOT NULL,
- "shop_id" varchar(128) NOT NULL,
- "shopping_card_id" varchar(128) NOT NULL,
+ "created" timestamp NOT NULL,
+ "modified" timestamp DEFAULT NULL,
+
+ "gateway" varchar(255) NOT NULL,
+ "state" varchar(255) NOT NULL,
+
+ "lang" varchar(16) DEFAULT NULL,
+ "error" varchar(255) DEFAULT NULL,
+
+ "amount" int DEFAULT NULL,
"total_amount" int NOT NULL,
- "lang" varchar(128) DEFAULT '',
+ "eci" varchar(255) DEFAULT NULL,
- "customer_first_name" varchar(128) DEFAULT '',
- "customer_last_name" varchar(128) DEFAULT '',
- "customer_address" varchar(128) DEFAULT '',
- "customer_city" varchar(128) DEFAULT '',
- "customer_zip" varchar(128) DEFAULT '',
- "customer_country" varchar(128) DEFAULT '',
- "customer_phone" varchar(128) DEFAULT '',
+ "payment_intent_id" varchar(255) DEFAULT NULL,
- "payment_plan" varchar(128) DEFAULT '',
- "credit_card_name" varchar(128) DEFAULT '',
- "credit_card_number" varchar(128) DEFAULT '',
- "payment_method" varchar(128) DEFAULT '',
- "currency_code" int DEFAULT 0,
+ "shopping_card_id" varchar(255) DEFAULT NULL,
+ "stan" varchar(255) DEFAULT NULL,
+ "success" int DEFAULT NULL,
+ "approval_code" varchar(255) DEFAULT NULL,
- "date_time" timestamp DEFAULT current_timestamp,
-
- "eci" varchar(256) DEFAULT '',
- "stan" varchar(256) DEFAULT '',
-
- "success" int DEFAULT 0,
- "approval_code" varchar(256) DEFAULT '',
- "error_message" varchar(256) DEFAULT '',
- "error_codes" varchar(256) DEFAULT '',
-
- "payment_state" varchar(256) DEFAULT '',
-
- PRIMARY KEY (id),
- CONSTRAINT unique_id UNIQUE ("shopping_card_id")
-);
-
-
-CREATE TABLE IF NOT EXISTS "stripe"
-(
- "id" uuid NOT NULL,
- "total_amount" int NOT NULL,
- "lang" varchar(128) DEFAULT '',
- "payment_intent_id" varchar(256) DEFAULT '',
- "payment_state" varchar(256) DEFAULT '',
-
- PRIMARY KEY (id)
-);
-
-
-CREATE TABLE IF NOT EXISTS "viva"
-(
- "id" uuid NOT NULL,
- "order_id" varchar(24) DEFAULT '',
+ "order_id" varchar(255) DEFAULT NULL,
"transaction_id" uuid DEFAULT NULL,
- "total_amount" int NOT NULL,
- "event_id" varchar(128) DEFAULT '',
- "eci" varchar(128) DEFAULT '',
- "payment_state" varchar(256) DEFAULT '',
+ "event_id" varchar(255) DEFAULT NULL,
PRIMARY KEY (id)
);
diff --git a/db/prod/v1_0.sql b/db/prod/v1_0.sql
index a6a8494..e69de29 100644
--- a/db/prod/v1_0.sql
+++ b/db/prod/v1_0.sql
@@ -1,65 +0,0 @@
-
-CREATE TABLE IF NOT EXISTS "wspay"
-(
- "id" uuid NOT NULL,
- "shop_id" varchar(128) NOT NULL,
- "shopping_card_id" varchar(128) NOT NULL,
- "total_amount" int NOT NULL,
-
- "lang" varchar(128) DEFAULT '',
-
- "customer_first_name" varchar(128) DEFAULT '',
- "customer_last_name" varchar(128) DEFAULT '',
- "customer_address" varchar(128) DEFAULT '',
- "customer_city" varchar(128) DEFAULT '',
- "customer_zip" varchar(128) DEFAULT '',
- "customer_country" varchar(128) DEFAULT '',
- "customer_phone" varchar(128) DEFAULT '',
-
- "payment_plan" varchar(128) DEFAULT '',
- "credit_card_name" varchar(128) DEFAULT '',
- "credit_card_number" varchar(128) DEFAULT '',
- "payment_method" varchar(128) DEFAULT '',
- "currency_code" int DEFAULT 0,
-
- "date_time" timestamp DEFAULT current_timestamp,
-
- "eci" varchar(256) DEFAULT '',
- "stan" varchar(256) DEFAULT '',
-
- "success" int DEFAULT 0,
- "approval_code" varchar(256) DEFAULT '',
- "error_message" varchar(256) DEFAULT '',
- "error_codes" varchar(256) DEFAULT '',
-
- "payment_state" varchar(256) DEFAULT '',
-
- PRIMARY KEY (id),
- CONSTRAINT unique_id UNIQUE ("shopping_card_id")
-);
-
-
-CREATE TABLE IF NOT EXISTS "stripe"
-(
- "id" uuid NOT NULL,
- "total_amount" int NOT NULL,
- "lang" varchar(128) DEFAULT '',
- "payment_intent_id" varchar(256) DEFAULT '',
- "payment_state" varchar(256) DEFAULT '',
-
- PRIMARY KEY (id)
-);
-
-
-CREATE TABLE IF NOT EXISTS "viva"
-(
- "id" uuid NOT NULL,
- "order_id" varchar(24) DEFAULT '',
- "transaction_id" uuid DEFAULT NULL,
- "total_amount" int NOT NULL,
- "event_id" varchar(128) DEFAULT '',
- "eci" varchar(128) DEFAULT '',
- "payment_state" varchar(256) DEFAULT '',
-
- PRIMARY KEY (id)
-);
diff --git a/main.go b/main.go
index 1253bb0..6c6da23 100644
--- a/main.go
+++ b/main.go
@@ -18,6 +18,7 @@ import (
stripe2 "payment-poc/stripe"
"payment-poc/viva"
"payment-poc/wspay"
+ "sort"
"strconv"
"strings"
"time"
@@ -121,6 +122,9 @@ func main() {
for key := range paymentGateways {
gateways = append(gateways, key)
}
+ sort.Slice(gateways, func(i, j int) bool {
+ return string(gateways[i]) < string(gateways[j])
+ })
c.HTML(200, "methods.gohtml", gin.H{"Amount": amount, "Gateways": gateways})
})
g.GET("/:gateway", func(c *gin.Context) {
@@ -146,6 +150,15 @@ func main() {
return
}
})
+ g.GET("/entries/:id", func(c *gin.Context) {
+ id := uuid.MustParse(c.Param("id"))
+ entry, err := entryProvider.FetchById(id)
+ if err != nil {
+ c.AbortWithError(http.StatusBadRequest, err)
+ return
+ }
+ c.HTML(200, "info.gohtml", gin.H{"Entry": entry})
+ })
g.POST("/entries/:id/complete", func(c *gin.Context) {
id := uuid.MustParse(c.Param("id"))
entry, err := entryProvider.FetchById(id)
@@ -160,7 +173,7 @@ func main() {
return
}
entry, err = paymentGateway.CompleteTransaction(entry, amount)
- if err != nil {
+ if err == nil {
entryProvider.UpdateEntry(entry)
c.Redirect(http.StatusSeeOther, "/entries/"+id.String())
} else {
@@ -183,7 +196,7 @@ func main() {
}
if paymentGateway, ok := paymentGateways[entry.Gateway]; ok {
entry, err = paymentGateway.CancelTransaction(entry)
- if err != nil {
+ if err == nil {
entryProvider.UpdateEntry(entry)
c.Redirect(http.StatusSeeOther, "/entries/"+id.String())
} else {
@@ -218,14 +231,6 @@ func getAccounts() gin.Accounts {
return gin.Accounts{auth[0]: auth[1]}
}
-func parseDateTime(dateTime string) time.Time {
- t, err := time.Parse("20060102150405", dateTime)
- if err != nil {
- log.Printf("couldn't parse response time %s: %v", dateTime, err)
- }
- return t
-}
-
func fetchAmount(amount string) (int64, error) {
if amount, err := strconv.ParseFloat(amount, 64); err == nil {
return int64(amount * 100), nil
@@ -329,19 +334,21 @@ func hasProfile(profile string) bool {
func formatState(stt state.PaymentState) string {
switch stt {
case state.StateCanceled:
- return "Otkazano"
+ return "Otkazana"
case state.StateVoided:
- return "Otkazano sa strane administratora"
+ return "Otkazana sa strane administratora"
case state.StateAccepted:
- return "Prihvačeno"
+ return "Predautorizirana"
case state.StateError:
return "Greška"
+ case state.StatePreinitialized:
+ return "Predinicijalizirana"
case state.StateInitialized:
- return "Inicijalna izrada"
+ return "Inicijalizirana"
case state.StateCanceledInitialization:
- return "Otkazano tijekom izrade"
+ return "Otkazana tijekom izrade"
case state.StateCompleted:
- return "Završeno"
+ return "Autorizirana"
}
return "nepoznato stanje '" + string(stt) + "'"
}
diff --git a/state/model.go b/state/model.go
index b3ff9b4..b8c24b5 100644
--- a/state/model.go
+++ b/state/model.go
@@ -3,6 +3,8 @@ package state
type PaymentState string
const (
+ // initial state
+ StatePreinitialized PaymentState = "preinitialized"
// initial state
StateInitialized PaymentState = "initialized"
diff --git a/stripe/model.go b/stripe/model.go
deleted file mode 100644
index 05fee09..0000000
--- a/stripe/model.go
+++ /dev/null
@@ -1,16 +0,0 @@
-package stripe
-
-import (
- "github.com/google/uuid"
- "payment-poc/state"
-)
-
-type StripeDb struct {
- Id uuid.UUID `db:"id"`
- TotalAmount int64 `db:"total_amount"`
- Lang string `db:"lang"`
-
- PaymentIntentId string `db:"payment_intent_id"`
-
- State state.PaymentState `db:"payment_state"`
-}
diff --git a/stripe/service.go b/stripe/service.go
index 7c9aeb5..9d81214 100644
--- a/stripe/service.go
+++ b/stripe/service.go
@@ -19,7 +19,7 @@ type Service struct {
func (s *Service) CreatePaymentUrl(amount int64) (url string, err error) {
entry, err := s.Provider.CreateEntry(database.PaymentEntry{
- Gateway: state.GatewayVivaWallet,
+ Gateway: state.GatewayStripe,
State: state.StateInitialized,
TotalAmount: amount,
})
@@ -83,7 +83,7 @@ func (s *Service) CompleteTransaction(entry database.PaymentEntry, amount int64)
}
log.Printf("received state on completion: %v", pi.Status)
if pi.Status == stripe.PaymentIntentStatusSucceeded || pi.Status == stripe.PaymentIntentStatusProcessing {
- entry.TotalAmount = pi.Amount
+ entry.Amount = &pi.AmountReceived
entry.State = state.StateCompleted
}
return entry, nil
diff --git a/templates/iframe_handler.gohtml b/templates/iframe_handler.gohtml
deleted file mode 100644
index b8697dd..0000000
--- a/templates/iframe_handler.gohtml
+++ /dev/null
@@ -1,35 +0,0 @@
-
-
-
-
-
-
- Obrada odgovora
-
-
-
-
-
-
-Obrada odgovora...
-
-
-
-
-
-
\ No newline at end of file
diff --git a/templates/index.gohtml b/templates/index.gohtml
index 35794d3..4b9df32 100644
--- a/templates/index.gohtml
+++ b/templates/index.gohtml
@@ -10,10 +10,10 @@
- Novo plačanje
+ Novo plaćanje
-
WsPay
+
Entries
Id |
+ Gateway |
Vrijednost |
Stanje |
- {{range .WsPay}}
+ {{range .Entries}}
- {{.Id}} |
- {{formatCurrency .TotalAmount}} |
- {{formatState .State}} |
-
- {{end}}
-
-
-
-
Stripe
-
-
-
- Id |
- Vrijednost |
- Stanje |
-
-
-
- {{range .Stripe}}
-
- {{.Id}} |
- {{formatCurrency .TotalAmount}} |
- {{formatState .State}} |
-
- {{end}}
-
-
-
-
Viva
-
-
-
- Id |
- Vrijednost |
- Stanje |
-
-
-
- {{range .Viva}}
-
- {{.Id}} |
+ {{.Id}} |
+ {{.Gateway}} |
{{formatCurrency .TotalAmount}} |
{{formatState .State}} |
diff --git a/templates/info.gohtml b/templates/info.gohtml
new file mode 100644
index 0000000..30f7c54
--- /dev/null
+++ b/templates/info.gohtml
@@ -0,0 +1,65 @@
+
+
+
+
+
+
+ Index
+
+
+
+
+
+
+ Plaćanje {{.Entry.Id}}
+
+
+ Id: | {{.Entry.Id}} |
+ Datum izrade: | {{.Entry.Created.Format "Jan 02, 2006 15:04:05 UTC"}} |
+ Zadnja izmjena: | {{or (.Entry.Modified.Format "Jan 02, 2006 15:04:05 UTC") .Entry.Created.Format "Jan 02, 2006 15:04:05 UTC"}} |
+ Gateway: | {{.Entry.Gateway}} |
+ Naplaćena vrijednost: | {{or .Entry.Amount "-"}} |
+ Ukupna vrijednost: | {{formatCurrency .Entry.TotalAmount}} |
+ Jezik: | {{or .Entry.Lang "-"}} |
+ Greške: | {{or .Entry.Error "-"}} |
+ Stanje: | {{formatState .Entry.State}} |
+
+ {{if eq .Entry.Gateway "wspay"}}
+ WsPay | |
+ Shopping cart ID: | {{or .Entry.ShoppingCartID "-"}} |
+ Success: | {{or .Entry.Success "-"}} |
+ {{end}}
+
+ {{if eq .Entry.Gateway "stripe"}}
+ Stripe | |
+ Payment intent ID: | {{or .Entry.PaymentIntentId "-"}} |
+ {{end}}
+
+ {{if eq .Entry.Gateway "viva-wallet"}}
+ Viva wallet | |
+ Order ID: | {{or .Entry.OrderId "-"}} |
+ Transaction ID: | {{or .Entry.TransactionId "-"}} |
+ Event ID: | {{or .Entry.EventId "-"}} |
+ {{end}}
+
+
+ {{if eq .Entry.State "accepted"}}
+
+
+ {{end}}
+
+
\ No newline at end of file
diff --git a/templates/methods.gohtml b/templates/methods.gohtml
index 9982918..82b38a0 100644
--- a/templates/methods.gohtml
+++ b/templates/methods.gohtml
@@ -22,9 +22,9 @@
- Izaberi metodu plačanja
- WsPay
- Stripe
- Viva
+ Izaberi metodu plaćanja
+ {{range .Gateways}}
+ {{.}}
+ {{end}}