diff --git a/main.go b/main.go index 9897dbd..85eeae2 100644 --- a/main.go +++ b/main.go @@ -371,21 +371,6 @@ func stripeHandlers(g *gin.RouterGroup, provider *database.PaymentEntryProvider, } 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) { url, err := wspayService.HandleSuccessResponse(c, provider) if err != nil { diff --git a/providers/wspay/service.go b/providers/wspay/service.go index 5552f32..cbbaa79 100644 --- a/providers/wspay/service.go +++ b/providers/wspay/service.go @@ -6,6 +6,7 @@ import ( "encoding/hex" "encoding/json" "errors" + "fmt" "github.com/gin-gonic/gin" "github.com/google/uuid" "io" @@ -77,7 +78,11 @@ func determineState(response StatusCheckResponse) state.PaymentState { } 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) { @@ -159,18 +164,42 @@ func (s *Service) CancelTransaction(entry database.PaymentEntry) (database.Payme } } -func (s *Service) InitializePayment(entry database.PaymentEntry) Form { - form := Form{ +func (s *Service) InitializePayment(entry database.PaymentEntry) (database.PaymentEntry, string, error) { + formattedAmount := fmt.Sprintf("%d,%02d", entry.TotalAmount/100, entry.TotalAmount%100) + + var request = CreateTransaction{ ShopID: s.ShopId, - ShoppingCartID: entry.Id.String(), + ShoppingCardID: entry.Id.String(), Version: "2.0", - TotalAmount: entry.TotalAmount, - ReturnURL: s.BackendUrl + "/wspay/success", - ReturnErrorURL: s.BackendUrl + "/wspay/error", - CancelURL: s.BackendUrl + "/wspay/cancel", + TotalAmount: formattedAmount, + ReturnUrl: s.BackendUrl + "/wspay/success", + ReturnErrorUrl: s.BackendUrl + "/wspay/error", + CancelUrl: s.BackendUrl + "/wspay/cancel", 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) { diff --git a/providers/wspay/wspay.go b/providers/wspay/wspay.go index 94bad71..db4932c 100644 --- a/providers/wspay/wspay.go +++ b/providers/wspay/wspay.go @@ -1,34 +1,19 @@ 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 { - // required args - ShopID string - 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 TransactionResponse struct { + TransactionId *string `json:"TransactionId"` + PaymentFormUrl *string `json:"PaymentFormUrl"` } type FormReturn struct { diff --git a/templates/wspay.gohtml b/templates/wspay.gohtml deleted file mode 100644 index 1eb536c..0000000 --- a/templates/wspay.gohtml +++ /dev/null @@ -1,35 +0,0 @@ - - -
- - - -