saveinmed/backend/internal/http/handler/seller_payment_handler.go
Gabbriiel 90467db1ec refactor: substitui backend Medusa por backend Go e corrige testes do marketplace
- Remove backend Medusa.js (TypeScript) e substitui pelo backend Go (saveinmed-performance-core)
- Corrige testes auth.test.ts: alinha paths de API (v1/ sem barra inicial) e campo access_token
- Corrige GroupedProductCard.test.tsx: ajusta distância formatada (toFixed) e troca userEvent por fireEvent com fakeTimers
- Corrige AuthContext.test.tsx: usa vi.hoisted() para mocks e corrige parênteses no waitFor

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-17 04:56:37 -06:00

76 lines
1.9 KiB
Go

package handler
import (
stdjson "encoding/json"
"net/http"
"github.com/gofrs/uuid/v5"
)
// GetSellerPaymentConfig returns the seller's payment configuration
func (h *Handler) GetSellerPaymentConfig(w http.ResponseWriter, r *http.Request) {
idStr := r.PathValue("id")
sellerID, err := uuid.FromString(idStr)
if err != nil {
http.Error(w, "invalid seller id", http.StatusBadRequest)
return
}
// Verify permissions (only seller or admin)
usr, err := h.getUserFromContext(r.Context())
if err != nil {
http.Error(w, "unauthorized", http.StatusUnauthorized)
return
}
if usr.CompanyID != sellerID && usr.Role != "Admin" {
http.Error(w, "forbidden", http.StatusForbidden)
return
}
acc, err := h.svc.GetSellerPaymentAccount(r.Context(), sellerID)
if err != nil {
// return empty if not found? or 404?
// for UX, empty object is often better
stdjson.NewEncoder(w).Encode(map[string]any{})
return
}
w.Header().Set("Content-Type", "application/json")
stdjson.NewEncoder(w).Encode(acc)
}
// OnboardSeller initiates the onboarding flow (e.g. Stripe Connect)
func (h *Handler) OnboardSeller(w http.ResponseWriter, r *http.Request) {
idStr := r.PathValue("id")
sellerID, err := uuid.FromString(idStr)
if err != nil {
http.Error(w, "invalid seller id", http.StatusBadRequest)
return
}
usr, err := h.getUserFromContext(r.Context())
if err != nil {
http.Error(w, "unauthorized", http.StatusUnauthorized)
return
}
if usr.CompanyID != sellerID && usr.Role != "Admin" {
http.Error(w, "forbidden", http.StatusForbidden)
return
}
var req struct {
Gateway string `json:"gateway"`
}
if err := stdjson.NewDecoder(r.Body).Decode(&req); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
link, err := h.svc.OnboardSeller(r.Context(), sellerID, req.Gateway)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
stdjson.NewEncoder(w).Encode(map[string]string{"onboarding_url": link})
}