package handler import ( "net/http" "strconv" ) // UploadDocument handles KYC doc upload. func (h *Handler) UploadDocument(w http.ResponseWriter, r *http.Request) { usr, err := h.getUserFromContext(r.Context()) if err != nil { http.Error(w, "unauthorized", http.StatusUnauthorized) return } var req struct { Type string `json:"type"` URL string `json:"url"` } if err := jsonAPI.NewDecoder(r.Body).Decode(&req); err != nil { http.Error(w, err.Error(), http.StatusBadRequest) return } doc, err := h.svc.UploadDocument(r.Context(), usr.CompanyID, req.Type, req.URL) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } w.Header().Set("Content-Type", "application/json") jsonAPI.NewEncoder(w).Encode(doc) } // GetDocuments lists company KYC docs. func (h *Handler) GetDocuments(w http.ResponseWriter, r *http.Request) { usr, err := h.getUserFromContext(r.Context()) if err != nil { http.Error(w, "unauthorized", http.StatusUnauthorized) return } docs, err := h.svc.GetCompanyDocuments(r.Context(), usr.CompanyID) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } w.Header().Set("Content-Type", "application/json") jsonAPI.NewEncoder(w).Encode(docs) } // GetLedger returns financial history. func (h *Handler) GetLedger(w http.ResponseWriter, r *http.Request) { usr, err := h.getUserFromContext(r.Context()) if err != nil { http.Error(w, "unauthorized", http.StatusUnauthorized) return } page, _ := strconv.Atoi(r.URL.Query().Get("page")) pageSize, _ := strconv.Atoi(r.URL.Query().Get("page_size")) res, err := h.svc.GetFormattedLedger(r.Context(), usr.CompanyID, page, pageSize) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } w.Header().Set("Content-Type", "application/json") jsonAPI.NewEncoder(w).Encode(res) } // GetBalance returns current wallet balance. func (h *Handler) GetBalance(w http.ResponseWriter, r *http.Request) { usr, err := h.getUserFromContext(r.Context()) if err != nil { http.Error(w, "unauthorized", http.StatusUnauthorized) return } bal, err := h.svc.GetBalance(r.Context(), usr.CompanyID) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } w.Header().Set("Content-Type", "application/json") jsonAPI.NewEncoder(w).Encode(map[string]int64{"balance_cents": bal}) } // RequestWithdrawal initiates a payout. func (h *Handler) RequestWithdrawal(w http.ResponseWriter, r *http.Request) { usr, err := h.getUserFromContext(r.Context()) if err != nil { http.Error(w, "unauthorized", http.StatusUnauthorized) return } var req struct { AmountCents int64 `json:"amount_cents"` BankInfo string `json:"bank_info"` } if err := jsonAPI.NewDecoder(r.Body).Decode(&req); err != nil { http.Error(w, err.Error(), http.StatusBadRequest) return } wd, err := h.svc.RequestWithdrawal(r.Context(), usr.CompanyID, req.AmountCents, req.BankInfo) if err != nil { http.Error(w, err.Error(), http.StatusBadRequest) // User error mostly (insufficient funds) return } w.Header().Set("Content-Type", "application/json") jsonAPI.NewEncoder(w).Encode(wd) } // ListWithdrawals shows history of payouts. func (h *Handler) ListWithdrawals(w http.ResponseWriter, r *http.Request) { usr, err := h.getUserFromContext(r.Context()) if err != nil { http.Error(w, "unauthorized", http.StatusUnauthorized) return } wds, err := h.svc.ListWithdrawals(r.Context(), usr.CompanyID) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } w.Header().Set("Content-Type", "application/json") jsonAPI.NewEncoder(w).Encode(wds) }