Skip to content

Commit

Permalink
Fixed bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
HirbodBehnam committed Dec 30, 2023
1 parent 6b1c393 commit e153959
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 9 deletions.
20 changes: 16 additions & 4 deletions payment/api/idpay.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package api
import (
"github.com/gin-gonic/gin"
"github.com/go-faster/errors"
"github.com/google/uuid"
log "github.com/sirupsen/logrus"
"gorm.io/gorm"
"net/http"
"wss-payment/internal/database"
"wss-payment/pkg/idpay"
Expand Down Expand Up @@ -84,18 +86,28 @@ func (api *API) CreateTransaction(c *gin.Context) {
func (api *API) GetTransaction(c *gin.Context) {
// At first parse the body
var body getTransactionRequest
err := c.BindJSON(&body)
err := c.Bind(&body)
if err != nil {
c.JSON(http.StatusBadRequest, "cannot parse body: "+err.Error())
return
}
orderUUID, err := uuid.Parse(body.OrderID)
if err != nil {
c.JSON(http.StatusBadRequest, "invalid uuid")
return
}
logger := log.WithField("OrderID", body.OrderID)
// Now get the transaction from database
payment := database.Payment{OrderID: body.OrderID}
payment := database.Payment{OrderID: orderUUID}
err = api.Database.GetPayment(&payment)
if err != nil {
logger.WithError(err).Error("cannot get order from database")
c.JSON(http.StatusInternalServerError, "cannot parse body: "+err.Error())
if errors.Is(err, gorm.ErrRecordNotFound) {
logger.Warn("payment not found")
c.JSON(http.StatusBadRequest, "payment not found")
} else {
logger.WithError(err).Error("cannot get payment from database")
c.JSON(http.StatusInternalServerError, "cannot parse body: "+err.Error())
}
return
}
// Check if it's verified and verify it
Expand Down
2 changes: 1 addition & 1 deletion payment/api/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ type createGoodResponse struct {

// getTransactionRequest is the request body of the get transaction endpoint
type getTransactionRequest struct {
OrderID uuid.UUID `json:"order_id" binding:"required"`
OrderID string `form:"order_id" binding:"required"`
}

// getTransactionResponse is the response to the get transaction result.
Expand Down
6 changes: 3 additions & 3 deletions payment/cmd/payment/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ func main() {
endpointApi.PaymentService = idpay.Mock{ // TODO: remove
FailCreation: false,
FailVerification: false,
PaymentVerificationOk: true,
PaymentVerificationOk: false,
}
defer endpointApi.Database.Close()
// Setup endpoints
r := gin.New()
r.Use(gin.Recovery())
r.GET("/health", api.HealthCheck)
r.POST("/create", endpointApi.CreateTransaction)
r.GET("/status")
r.POST("/transaction", endpointApi.CreateTransaction)
r.GET("/transaction", endpointApi.GetTransaction)
r.GET("/goods", endpointApi.GetGoods)
r.POST("/goods", endpointApi.AddGood)
// Listen
Expand Down
2 changes: 1 addition & 1 deletion payment/internal/database/payment.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (db PaymentDatabase) MarkAsFailed(orderID uuid.UUID) {

// GetPayment will get a payment from database based on its primary key
func (db PaymentDatabase) GetPayment(payment *Payment) error {
return db.db.Find(payment).Error
return db.db.Model(&Payment{}).Preload("BoughtGoods").First(payment).Error
}

// MarkPaymentAsOK will mark a payment as successful and then updates its track ID, payment track ID and verified at time.
Expand Down
16 changes: 16 additions & 0 deletions payment/internal/database/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package database

import (
"database/sql"
"github.com/go-faster/errors"
"github.com/google/uuid"
"strconv"
"time"
)

Expand All @@ -15,6 +17,20 @@ const (
PaymentStatusSuccess PaymentStatus = 3
)

func (paymentStatus PaymentStatus) MarshalText() ([]byte, error) {
switch paymentStatus {
case PaymentStatusInitiated:
return []byte("initiated"), nil
case PaymentStatusFailed:
return []byte("failed"), nil
case PaymentStatusTimeout:
return []byte("timeout"), nil
case PaymentStatusSuccess:
return []byte("success"), nil
}
return nil, errors.New("invalid payment status: " + strconv.Itoa(int(paymentStatus)))
}

// Payment represents a payment made by a user
type Payment struct {
// The order ID which is sent to pay.ir
Expand Down

0 comments on commit e153959

Please sign in to comment.