Files
telecom-platform/apps/api-server/internal/payment/controller_queries.go
T
nutcas3 cb1d7171e0 feat: Add PaymentController with modular handlers for payment operations, refunds, and webhook processing
- Add PaymentController struct with paymentService/db dependencies and NewPaymentController constructor
- Add SetupRoutes with /payments endpoints (POST /top-up, POST/DELETE/GET /payment-methods, GET /transactions/:id, POST /transactions/:id/refund) and /webhooks/stripe endpoint
- Add ProcessTopUp handler with amount/paymentMethodId validation, subscriberID authentication, and PaymentService.
2026-04-24 08:53:34 +03:00

62 lines
1.9 KiB
Go

package payment
import (
"net/http"
"github.com/gin-gonic/gin"
)
// ListPaymentMethods lists payment methods for the authenticated subscriber.
func (pc *PaymentController) ListPaymentMethods(c *gin.Context) {
subscriberID := c.GetInt("subscriberID")
if subscriberID == 0 {
c.JSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"})
return
}
paymentMethods, err := pc.db.ListPaymentMethods(c.Request.Context(), uint(subscriberID))
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"paymentMethods": paymentMethods})
}
// ListTransactions lists transactions for the authenticated subscriber.
func (pc *PaymentController) ListTransactions(c *gin.Context) {
subscriberID := c.GetInt("subscriberID")
if subscriberID == 0 {
c.JSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"})
return
}
transactions, err := pc.db.ListTransactions(c.Request.Context(), uint(subscriberID))
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"transactions": transactions})
}
// GetTransaction returns a specific transaction, enforcing ownership.
func (pc *PaymentController) GetTransaction(c *gin.Context) {
transactionID := c.Param("id")
if transactionID == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "Transaction ID required"})
return
}
subscriberID := c.GetInt("subscriberID")
if subscriberID == 0 {
c.JSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"})
return
}
transaction, err := pc.db.GetTransaction(c.Request.Context(), transactionID)
if err != nil {
c.JSON(http.StatusNotFound, gin.H{"error": "Transaction not found"})
return
}
if transaction.SubscriberID != uint(subscriberID) {
c.JSON(http.StatusForbidden, gin.H{"error": "Transaction does not belong to subscriber"})
return
}
c.JSON(http.StatusOK, gin.H{"transaction": transaction})
}