72 lines
1.4 KiB
Go
72 lines
1.4 KiB
Go
package mdtrns
|
|
|
|
import (
|
|
"enaklo-pos-be/internal/handlers/request"
|
|
"enaklo-pos-be/internal/handlers/response"
|
|
"enaklo-pos-be/internal/services"
|
|
"github.com/gin-gonic/gin"
|
|
"net/http"
|
|
)
|
|
|
|
type Handler struct {
|
|
service services.Order
|
|
}
|
|
|
|
func (h *Handler) Route(group *gin.RouterGroup, jwt gin.HandlerFunc) {
|
|
route := group.Group("/midtrans")
|
|
|
|
route.POST("/callback", h.Callback)
|
|
}
|
|
|
|
func NewHandler(service services.Order) *Handler {
|
|
return &Handler{
|
|
service: service,
|
|
}
|
|
}
|
|
|
|
func (h *Handler) Callback(c *gin.Context) {
|
|
var callbackData request.MidtransCallbackRequest
|
|
if err := c.ShouldBindJSON(&callbackData); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
validStatuses := []string{"settlement", "expire", "deny", "cancel", "capture", "failure"}
|
|
|
|
isValidStatus := false
|
|
for _, status := range validStatuses {
|
|
if callbackData.TransactionStatus == status {
|
|
isValidStatus = true
|
|
break
|
|
}
|
|
}
|
|
|
|
if !isValidStatus {
|
|
c.JSON(http.StatusOK, response.BaseResponse{
|
|
Success: true,
|
|
Status: http.StatusOK,
|
|
Message: "",
|
|
})
|
|
return
|
|
}
|
|
|
|
err := h.service.ProcessCallback(c, callbackData.ToEntity())
|
|
|
|
if err != nil {
|
|
c.JSON(http.StatusUnauthorized, response.BaseResponse{
|
|
Success: false,
|
|
Status: http.StatusBadRequest,
|
|
Message: err.Error(),
|
|
Data: nil,
|
|
})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, response.BaseResponse{
|
|
Success: true,
|
|
Status: http.StatusOK,
|
|
Message: "order",
|
|
})
|
|
|
|
}
|