Removed unnecessary redirect on WsPay initialization

This commit is contained in:
Borna Rajković 2023-08-04 08:13:11 +02:00
parent 5623e01f7f
commit e3d77d55b9
4 changed files with 51 additions and 87 deletions

15
main.go
View File

@ -371,21 +371,6 @@ func stripeHandlers(g *gin.RouterGroup, provider *database.PaymentEntryProvider,
} }
func wsPayHandlers(g *gin.RouterGroup, provider *database.PaymentEntryProvider, wspayService *wspay2.Service) { func wsPayHandlers(g *gin.RouterGroup, provider *database.PaymentEntryProvider, wspayService *wspay2.Service) {
g.GET("/initialize/:id", func(c *gin.Context) {
entry, err := provider.FetchById(uuid.MustParse(c.Param("id")))
if err != nil {
c.AbortWithError(http.StatusNotFound, err)
return
}
if entry.State != state.StatePreinitialized {
c.AbortWithError(http.StatusBadRequest, err)
return
}
form := wspayService.InitializePayment(entry)
c.HTML(200, "wspay.gohtml", gin.H{"Action": wspay2.AuthorisationForm, "Form": form})
})
g.GET("success", func(c *gin.Context) { g.GET("success", func(c *gin.Context) {
url, err := wspayService.HandleSuccessResponse(c, provider) url, err := wspayService.HandleSuccessResponse(c, provider)
if err != nil { if err != nil {

View File

@ -6,6 +6,7 @@ import (
"encoding/hex" "encoding/hex"
"encoding/json" "encoding/json"
"errors" "errors"
"fmt"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/google/uuid" "github.com/google/uuid"
"io" "io"
@ -77,7 +78,11 @@ func determineState(response StatusCheckResponse) state.PaymentState {
} }
func (s *Service) CreatePaymentUrl(entry database.PaymentEntry) (database.PaymentEntry, string, error) { func (s *Service) CreatePaymentUrl(entry database.PaymentEntry) (database.PaymentEntry, string, error) {
return entry, "/wspay/initialize/" + entry.Id.String(), nil entry, url, err := s.InitializePayment(entry)
if err != nil {
return entry, "", err
}
return entry, url, nil
} }
func (s *Service) CompleteTransaction(entry database.PaymentEntry, amount int64) (database.PaymentEntry, error) { func (s *Service) CompleteTransaction(entry database.PaymentEntry, amount int64) (database.PaymentEntry, error) {
@ -159,18 +164,42 @@ func (s *Service) CancelTransaction(entry database.PaymentEntry) (database.Payme
} }
} }
func (s *Service) InitializePayment(entry database.PaymentEntry) Form { func (s *Service) InitializePayment(entry database.PaymentEntry) (database.PaymentEntry, string, error) {
form := Form{ formattedAmount := fmt.Sprintf("%d,%02d", entry.TotalAmount/100, entry.TotalAmount%100)
var request = CreateTransaction{
ShopID: s.ShopId, ShopID: s.ShopId,
ShoppingCartID: entry.Id.String(), ShoppingCardID: entry.Id.String(),
Version: "2.0", Version: "2.0",
TotalAmount: entry.TotalAmount, TotalAmount: formattedAmount,
ReturnURL: s.BackendUrl + "/wspay/success", ReturnUrl: s.BackendUrl + "/wspay/success",
ReturnErrorURL: s.BackendUrl + "/wspay/error", ReturnErrorUrl: s.BackendUrl + "/wspay/error",
CancelURL: s.BackendUrl + "/wspay/cancel", CancelUrl: s.BackendUrl + "/wspay/cancel",
Signature: CalculateFormSignature(s.ShopId, s.ShopSecret, entry.Id.String(), entry.TotalAmount), Signature: CalculateFormSignature(s.ShopId, s.ShopSecret, entry.Id.String(), entry.TotalAmount),
} }
return form
httpResponse, err := createRequest(
"POST",
"https://formtest.wspay.biz/api/create-transaction",
map[string]string{"content-type": "application/json"},
toJson(request),
)
if err != nil {
return database.PaymentEntry{}, "", err
}
var response TransactionResponse
err = readResponse(httpResponse, &response)
if err != nil {
return database.PaymentEntry{}, "", err
}
if response.TransactionId == nil {
return database.PaymentEntry{}, "", errors.New("received bad response")
}
entry.State = state.StateInitialized
return entry, *response.PaymentFormUrl, nil
} }
func (s *Service) HandleSuccessResponse(c *gin.Context, provider *database.PaymentEntryProvider) (string, error) { func (s *Service) HandleSuccessResponse(c *gin.Context, provider *database.PaymentEntryProvider) (string, error) {

View File

@ -1,34 +1,19 @@
package wspay package wspay
const AuthorisationForm = "https://formtest.wspay.biz/authorization.aspx" type CreateTransaction struct {
ShopID string `json:"ShopID"`
ShoppingCardID string `json:"ShoppingCartID"`
Version string `json:"Version"`
TotalAmount string `json:"TotalAmount"`
ReturnUrl string `json:"ReturnURL"`
ReturnErrorUrl string `json:"ReturnErrorURL"`
CancelUrl string `json:"CancelURL"`
Signature string `json:"Signature"`
}
type Form struct { type TransactionResponse struct {
// required args TransactionId *string `json:"TransactionId"`
ShopID string PaymentFormUrl *string `json:"PaymentFormUrl"`
ShoppingCartID string
Version string
TotalAmount int64
ReturnURL string
ReturnErrorURL string
CancelURL string
Signature string
// optional args
Lang string
CustomerFirstName string
CustomerLastName string
CustomerAddress string
CustomerCity string
CustomerZIP string
CustomerCountry string
CustomerPhone string
PaymentPlan string
CreditCardName string
PaymentMethod string
IntAmount int64
IntCurrency string
ReturnMethod string
CurrencyCode int
} }
type FormReturn struct { type FormReturn struct {

View File

@ -1,35 +0,0 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Izradi planćanje</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-9ndCyUaIbzAi2FUVXJi0CjmCapSmO7SnpJef0486qhLnuZ2cdeRhO02iuK6FUUVM" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js" integrity="sha384-geWF76RCwLtnZ8qwWowPQNguL3RmwHVBC9FhGdlKrxdiJJigb/j/68SIy3Te4Bkz" crossorigin="anonymous"></script>
<style>
h2 {
margin-top: 16px;
}
</style>
</head>
<body class="container" style="margin-top: 32px">
<h2>Započni proces plaćanja</h2>
<form id="wspay-form" action="{{.Action}}" method="POST">
<input type="hidden" name="ShopID" value="{{.Form.ShopID}}">
<input type="hidden" name="ShoppingCartID" value="{{.Form.ShoppingCartID}}">
<input type="hidden" name="Version" value="{{.Form.Version}}">
<input type="hidden" name="TotalAmount" value="{{formatCurrency .Form.TotalAmount}}">
<input type="hidden" name="Signature" value="{{.Form.Signature}}">
<input type="hidden" name="ReturnURL" value="{{.Form.ReturnURL}}">
<input type="hidden" name="CancelURL" value="{{.Form.CancelURL}}">
<input type="hidden" name="ReturnErrorURL" value="{{.Form.ReturnErrorURL}}">
</form>
<script>
document.querySelector("#wspay-form").submit();
</script>
</body>
</html>