Files
telecom-platform/apps/cli/internal/api/billing.go
T
nutcas3 bd3226edf0 feat: Refactor CLI API client by splitting billing, carrier, and charging modules into separate files
- Extract Invoice, Payment, Subscriber types and GetInvoices, GetPayments, GenerateInvoice methods to billing.go
- Extract CarrierInfo, ConnectivityStatus, ProfileInfo types and carrier connector methods to carrier.go
- Extract CreditCheckRequest/Response, CreditBalanceResponse types and charging engine methods to charging.go
- Add PacketStats, CreditInfo types and GetPacketStats, GetCreditInfo
2026-04-25 05:33:41 +03:00

156 lines
4.3 KiB
Go

package api
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"time"
)
// Invoice represents an invoice from the API
type Invoice struct {
ID string `json:"id"`
SubscriberID string `json:"subscriber_id"`
Amount float64 `json:"amount"`
Status string `json:"status"`
CreatedAt time.Time `json:"created_at"`
DueDate time.Time `json:"due_date"`
PaidAt *time.Time `json:"paid_at,omitempty"`
Subscriber Subscriber `json:"subscriber"`
}
// Payment represents a payment from the API
type Payment struct {
ID string `json:"id"`
InvoiceID string `json:"invoice_id"`
Amount float64 `json:"amount"`
Method string `json:"method"`
Status string `json:"status"`
CreatedAt time.Time `json:"created_at"`
ProcessedAt *time.Time `json:"processed_at,omitempty"`
}
// GenerateInvoiceRequest represents a request to generate an invoice
type GenerateInvoiceRequest struct {
SubscriberID string `json:"subscriber_id"`
}
// GenerateInvoiceResponse represents a response from generating an invoice
type GenerateInvoiceResponse struct {
ID string `json:"id"`
Amount float64 `json:"amount"`
Status string `json:"status"`
CreatedAt time.Time `json:"created_at"`
DueDate time.Time `json:"due_date"`
}
// GetInvoices retrieves invoices from the API
func (c *Client) GetInvoices() ([]Invoice, error) {
url := fmt.Sprintf("%s/api/v1/invoices", c.baseURL)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, fmt.Errorf("failed to create request: %w", err)
}
req.Header.Set("Authorization", "Bearer "+c.apiKey)
req.Header.Set("Content-Type", "application/json")
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, fmt.Errorf("failed to make request: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
body, _ := io.ReadAll(resp.Body)
return nil, fmt.Errorf("API request failed with status %d: %s", resp.StatusCode, string(body))
}
var response struct {
Data []Invoice `json:"data"`
}
if err := json.NewDecoder(resp.Body).Decode(&response); err != nil {
return nil, fmt.Errorf("failed to decode response: %w", err)
}
return response.Data, nil
}
// GetPayments retrieves payments from the API
func (c *Client) GetPayments() ([]Payment, error) {
url := fmt.Sprintf("%s/api/v1/payments", c.baseURL)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, fmt.Errorf("failed to create request: %w", err)
}
req.Header.Set("Authorization", "Bearer "+c.apiKey)
req.Header.Set("Content-Type", "application/json")
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, fmt.Errorf("failed to make request: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
body, _ := io.ReadAll(resp.Body)
return nil, fmt.Errorf("API request failed with status %d: %s", resp.StatusCode, string(body))
}
var response struct {
Data []Payment `json:"data"`
}
if err := json.NewDecoder(resp.Body).Decode(&response); err != nil {
return nil, fmt.Errorf("failed to decode response: %w", err)
}
return response.Data, nil
}
// GenerateInvoice generates a new invoice for a subscriber
func (c *Client) GenerateInvoice(subscriberID string) (*GenerateInvoiceResponse, error) {
url := fmt.Sprintf("%s/api/v1/invoices/generate", c.baseURL)
request := GenerateInvoiceRequest{
SubscriberID: subscriberID,
}
jsonData, err := json.Marshal(request)
if err != nil {
return nil, fmt.Errorf("failed to marshal request: %w", err)
}
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData))
if err != nil {
return nil, fmt.Errorf("failed to create request: %w", err)
}
req.Header.Set("Authorization", "Bearer "+c.apiKey)
req.Header.Set("Content-Type", "application/json")
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, fmt.Errorf("failed to make request: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusCreated {
body, _ := io.ReadAll(resp.Body)
return nil, fmt.Errorf("API request failed with status %d: %s", resp.StatusCode, string(body))
}
var response GenerateInvoiceResponse
if err := json.NewDecoder(resp.Body).Decode(&response); err != nil {
return nil, fmt.Errorf("failed to decode response: %w", err)
}
return &response, nil
}