123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- package billing
- import (
- "io"
- "net/http"
- "git.linuxforward.com/byom/byom-onboard/internal/common/models"
- "git.linuxforward.com/byom/byom-onboard/internal/platform/database"
- "github.com/gin-gonic/gin"
- )
- type Handler struct {
- client BillingClient
- db *database.Database
- }
- func NewHandler(client BillingClient, db *database.Database) *Handler {
- return &Handler{
- client: client,
- db: db,
- }
- }
- func (h *Handler) CreatePayment(c *gin.Context) {
- var req models.PaymentRequest
- if err := c.ShouldBindJSON(&req); err != nil {
- c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request format"})
- return
- }
- intent, err := h.client.CreatePaymentIntent(req.Amount, req.Currency, req.PaymentMethodID)
- if err != nil {
- c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
- return
- }
- c.JSON(http.StatusOK, intent)
- }
- func (h *Handler) HandleWebhook(c *gin.Context) {
- payload, err := io.ReadAll(c.Request.Body)
- if err != nil {
- c.JSON(http.StatusBadRequest, gin.H{"error": "Unable to read request body"})
- return
- }
- signature := c.GetHeader("Stripe-Signature")
- if err := h.client.HandleWebhook(payload, signature); err != nil {
- c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
- return
- }
- c.JSON(http.StatusOK, gin.H{"received": true})
- }
- func (h *Handler) InitiateSubscription(c *gin.Context) {
- var req struct {
- SessionToken string `json:"session_token" binding:"required"`
- PlanID string `json:"plan_id" binding:"required"`
- }
- if err := c.ShouldBindJSON(&req); err != nil {
- c.JSON(400, gin.H{"error": "Invalid request"})
- return
- }
- // Get plan details from DefaultPlans
- plan, exists := models.GetPlan(req.PlanID)
- if !exists {
- c.JSON(400, gin.H{"error": "Invalid plan"})
- return
- }
- // Create payment intent with plan price
- intent, err := h.client.CreatePaymentIntent(plan.Price, plan.Currency, "")
- if err != nil {
- c.JSON(500, gin.H{"error": "Failed to create payment"})
- return
- }
- c.JSON(200, intent)
- }
|