package wspay import ( "crypto/sha512" "encoding/hex" "errors" "github.com/google/uuid" "github.com/jmoiron/sqlx" "payment-poc/state" "strconv" ) type Service struct { DB *sqlx.DB } func (s *Service) CreateEntry(shopId string, totalAmount int64) (WsPayDb, error) { id := uuid.Must(uuid.NewRandom()) entry := WsPayDb{ Id: id, ShopID: shopId, ShoppingCartID: id.String(), TotalAmount: totalAmount, State: state.StateInitialized, } _, err := s.DB.Exec(`INSERT INTO "wspay" ("id", "shop_id", "shopping_card_id", "total_amount", "payment_state") VALUES ($1, $2, $3, $4, $5)`, &entry.Id, &entry.ShopID, &entry.ShoppingCartID, &entry.TotalAmount, &entry.State, ) if err != nil { return WsPayDb{}, err } return s.FetchById(id) } func (s *Service) FetchAll() ([]WsPayDb, error) { var entries []WsPayDb err := s.DB.Select(&entries, `SELECT * FROM "wspay"`) return entries, err } func (s *Service) FetchById(id uuid.UUID) (WsPayDb, error) { entry := WsPayDb{} err := s.DB.Get(&entry, `SELECT * FROM "wspay" WHERE "id" = $1`, id) return entry, err } func (s *Service) FetchByShoppingCartID(id string) (WsPayDb, error) { entry := WsPayDb{} err := s.DB.Get(&entry, `SELECT * FROM "wspay" WHERE "shopping_card_id" = $1`, id) return entry, err } func (s *Service) Update(entry WsPayDb) error { _, err := s.DB.Exec(`UPDATE "wspay" set "lang" = $2, "customer_first_name" = $3, "customer_last_name" = $4, "customer_address" = $5, "customer_city" = $6, "customer_zip" = $7, "customer_country" = $8, "customer_phone" = $9, "payment_plan" = $10, "credit_card_name" = $11, "credit_card_number" = $12, "payment_method" = $13, "currency_code" = $14, "date_time" = $15, "eci" = $16, "stan" = $17, "success" = $18, "approval_code" = $19, "error_message" = $20, "error_codes" = $21, "payment_state" = $22 WHERE "id" = $1`, &entry.Id, &entry.Lang, &entry.CustomerFirstName, &entry.CustomerLastName, &entry.CustomerAddress, &entry.CustomerCity, &entry.CustomerZIP, &entry.CustomerCountry, &entry.CustomerPhone, &entry.PaymentPlan, &entry.CreditCardName, &entry.CreditCardNumber, &entry.PaymentMethod, &entry.CurrencyCode, &entry.DateTime, &entry.ECI, &entry.STAN, &entry.Success, &entry.ApprovalCode, &entry.ErrorMessage, &entry.ErrorCodes, &entry.State, ) return err } func CalculateFormSignature(shopId string, secret string, cartId string, amount int64) string { /** Represents a signature created from string formatted from following values in a following order using SHA512 algorithm: ShopID SecretKey ShoppingCartID SecretKey TotalAmount SecretKey */ signature := shopId + secret + cartId + secret + strconv.FormatInt(amount, 10) + secret hash := sha512.New() hash.Write([]byte(signature)) return hex.EncodeToString(hash.Sum(nil)) } func CompareFormReturnSignature(signature string, shopId string, secret string, cartId string, success int, approvalCode string) error { /** Represents a signature created from string formatted from following values in a following order using SHA512 algorithm: ShopID SecretKey ShoppingCartID SecretKey Success SecretKey ApprovalCode SecretKey Merchant should validate this signature to make sure that the request is originating from WSPayForm. */ calculatedSignature := shopId + secret + cartId + secret + strconv.FormatInt(int64(success), 10) + secret + approvalCode + secret hash := sha512.New() hash.Write([]byte(calculatedSignature)) if hex.EncodeToString(hash.Sum(nil)) == signature { return nil } else { return errors.New("signature mismatch") } }