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

+ - {{range .WsPay}} + {{range .Entries}} - - - - - {{end}} - -
IdGateway Vrijednost Stanje
{{.Id}}{{formatCurrency .TotalAmount}}{{formatState .State}}
- -

Stripe

- - - - - - - - - - {{range .Stripe}} - - - - - - {{end}} - -
IdVrijednostStanje
{{.Id}}{{formatCurrency .TotalAmount}}{{formatState .State}}
- -

Viva

- - - - - - - - - - {{range .Viva}} - - + + 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}}

+ +
IdVrijednostStanje
{{.Id}}{{.Id}}{{.Gateway}} {{formatCurrency .TotalAmount}} {{formatState .State}}
+ + + + + + + + + + + {{if eq .Entry.Gateway "wspay"}} + + + + {{end}} + + {{if eq .Entry.Gateway "stripe"}} + + + {{end}} + + {{if eq .Entry.Gateway "viva-wallet"}} + + + + + {{end}} + +
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}}
WsPay
Shopping cart ID: {{or .Entry.ShoppingCartID "-"}}
Success: {{or .Entry.Success "-"}}
Stripe
Payment intent ID: {{or .Entry.PaymentIntentId "-"}}
Viva wallet
Order ID: {{or .Entry.OrderId "-"}}
Transaction ID: {{or .Entry.TransactionId "-"}}
Event ID: {{or .Entry.EventId "-"}}
+ {{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}} \ No newline at end of file diff --git a/templates/stripe_info.gohtml b/templates/stripe_info.gohtml deleted file mode 100644 index 68e08d3..0000000 --- a/templates/stripe_info.gohtml +++ /dev/null @@ -1,42 +0,0 @@ - - - - - - - Info - - - - - -

Plačanje {{.Entry.Id}}

- - - - - - -
Id: {{.Entry.Id}}
Ukupna vrijednost: {{formatCurrency .Entry.TotalAmount}}
Jezik: {{omitempty .Entry.Lang}}
Stanje: {{formatState .Entry.State}}
- - - {{if eq .Entry.State "accepted"}} -
-
- - -
- -
-
- -
- {{end}} - - \ No newline at end of file diff --git a/templates/viva_info.gohtml b/templates/viva_info.gohtml deleted file mode 100644 index 090f134..0000000 --- a/templates/viva_info.gohtml +++ /dev/null @@ -1,44 +0,0 @@ - - - - - - - Info - - - - - -

Plačanje {{.Entry.Id}}

- - - - - - - - - -
Id: {{.Entry.Id}}
Order id: {{.Entry.OrderId}}
Transaction id: {{.Entry.TransactionId.String}}
Ukupna vrijednost: {{formatCurrency .Entry.TotalAmount}}
Jezik: {{omitempty .Entry.Lang}}
Događaj: {{.Entry.EventId}}
Stanje: {{formatState .Entry.State}}
- - {{if eq .Entry.State "accepted"}} -
-
- - -
- -
-
- -
- {{end}} - - \ No newline at end of file diff --git a/templates/wspay.gohtml b/templates/wspay.gohtml index 5aa865c..5f88d07 100644 --- a/templates/wspay.gohtml +++ b/templates/wspay.gohtml @@ -17,7 +17,7 @@

Započni proces plačanja

-
+ @@ -26,36 +26,10 @@ -
-

Započni normalni proces u iframe-u

- -
- - - - - - - - - - - - -
- - \ No newline at end of file diff --git a/templates/wspay_info.gohtml b/templates/wspay_info.gohtml deleted file mode 100644 index 8f01791..0000000 --- a/templates/wspay_info.gohtml +++ /dev/null @@ -1,62 +0,0 @@ - - - - - - - Info - - - - - -

Plačanje {{.Entry.Id}}

- - - - - - - - - - - - - - - - - - - - - - - - - - - -
CartId: {{.Entry.ShoppingCartID}}
Ukupna vrijednost: {{formatCurrency .Entry.TotalAmount}}
Jezik: {{omitempty .Entry.Lang}}
Ime: {{omitempty .Entry.CustomerFirstName}}
Prezime: {{omitempty .Entry.CustomerLastName}}
Adresa: {{omitempty .Entry.CustomerAddress}}
Grad: {{omitempty .Entry.CustomerCity}}
ZIP: {{omitempty .Entry.CustomerZIP}}
Zemlja: {{omitempty .Entry.CustomerCountry}}
Broj telefona: {{omitempty .Entry.CustomerPhone}}
Plan plačanja: {{omitempty .Entry.PaymentPlan}}
Ime kartice: {{omitempty .Entry.CreditCardName}}
Broj kartice: {{omitempty .Entry.CreditCardNumber}}
Metoda plačanja: {{omitempty .Entry.PaymentMethod}}
Oznaka valute: {{.Entry.CurrencyCode}}
Datum i vrijeme: {{.Entry.DateTime.Format "Jan 02, 2006 15:04:05 UTC"}}
Uspjeh: {{.Entry.Success}}
Kod: {{omitempty .Entry.ApprovalCode}}
Poruka greške: {{omitempty .Entry.ErrorMessage}}
Kodovi greške: {{omitempty .Entry.ErrorCodes}}
Stanje: {{formatState .Entry.State}}
- - {{if eq .Entry.State "accepted"}} -
-
- - -
- -
-
- -
- {{end}} - - \ No newline at end of file diff --git a/wspay/model.go b/wspay/model.go deleted file mode 100644 index a941a86..0000000 --- a/wspay/model.go +++ /dev/null @@ -1,41 +0,0 @@ -package wspay - -import ( - "github.com/google/uuid" - "payment-poc/state" - "time" -) - -type WsPayDb struct { - Id uuid.UUID `db:"id"` - ShopID string `db:"shop_id"` - ShoppingCartID string `db:"shopping_card_id"` - TotalAmount int64 `db:"total_amount"` - Lang string `db:"lang"` - - CustomerFirstName string `db:"customer_first_name"` - CustomerLastName string `db:"customer_last_name"` - CustomerAddress string `db:"customer_address"` - CustomerCity string `db:"customer_city"` - CustomerZIP string `db:"customer_zip"` - CustomerCountry string `db:"customer_country"` - CustomerPhone string `db:"customer_phone"` - - PaymentPlan string `db:"payment_plan"` - CreditCardName string `db:"credit_card_name"` - CreditCardNumber string `db:"credit_card_number"` - PaymentMethod string `db:"payment_method"` - CurrencyCode int `db:"currency_code"` - - DateTime time.Time `db:"date_time"` - - ECI string `db:"eci"` - STAN string `db:"stan"` - - Success int `db:"success"` - ApprovalCode string `db:"approval_code"` - ErrorMessage string `db:"error_message"` - ErrorCodes string `db:"error_codes"` - - State state.PaymentState `db:"payment_state"` -} diff --git a/wspay/service.go b/wspay/service.go index 8582e55..544a479 100644 --- a/wspay/service.go +++ b/wspay/service.go @@ -24,7 +24,7 @@ type Service struct { func (s *Service) CreatePaymentUrl(amount int64) (string, error) { entry, err := s.Provider.CreateEntry(database.PaymentEntry{ - Gateway: state.GatewayVivaWallet, + Gateway: state.GatewayWsPay, State: state.StateInitialized, TotalAmount: amount, })