Compare commits

...

3 Commits

Author SHA1 Message Date
Borna Rajković e3d77d55b9 Removed unnecessary redirect on WsPay initialization 2023-08-04 08:13:11 +02:00
Borna Rajković 5623e01f7f Typo 2023-07-31 11:43:34 +02:00
Borna Rajković b3637805bc Updated makefile 2023-07-31 11:32:46 +02:00
5 changed files with 55 additions and 91 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) {
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 {

View File

@ -10,10 +10,10 @@ setup:
go get
docker-dev:
docker image build -t registry.bbr-dev.info/payment-poc/backend:$(VERSION)-dev .
docker tag registry.bbr-dev.info/payment-poc/backend:$(VERSION)-dev registry.bbr-dev.info/payment-poc/backend:latest-dev
docker image push registry.bbr-dev.info/payment-poc/backend:$(VERSION)-dev
docker image push registry.bbr-dev.info/payment-poc/backend:latest-dev
docker image build -t registry.s2internal.com/opgdirekt/payment-poc/backend:$(VERSION)-dev .
docker tag registry.s2internal.com/opgdirekt/payment-poc/backend:$(VERSION)-dev registry.s2internal.com/opgdirekt/payment-poc/backend:latest-dev
docker image push registry.s2internal.com/opgdirekt/payment-poc/backend:$(VERSION)-dev
docker image push registry.s2internal.com/opgdirekt/payment-poc/backend:latest-dev
docker-prod:

View File

@ -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) {

View File

@ -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 {

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>