an updated and hopefully faster version of the ST Toolbox
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

48 lines
1.1 KiB

package api
import (
"encoding/json"
"fmt"
"io"
"regexp"
)
func (s *Session) GetInvoice(identifier string) (map[string]interface{}, error) {
var endpoint string
isInvoiceNumber, _ := regexp.MatchString(`^[A-Za-z]`, identifier)
if isInvoiceNumber {
endpoint = fmt.Sprintf("/invoice?invoiceNumber=%s", identifier)
} else {
endpoint = fmt.Sprintf("/invoice/%s", identifier)
}
resp, err := s.DoRequest("GET", endpoint, nil)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
if resp.StatusCode != 200 {
return nil, fmt.Errorf("failed to get invoice info: %s, response: %s", resp.Status, string(body))
}
var result map[string]interface{}
if err := json.Unmarshal(body, &result); err != nil {
return nil, fmt.Errorf("error unmarshalling response: %v", err)
}
if data, ok := result["data"].(map[string]interface{}); ok {
if invoices, ok := data["invoices"].([]interface{}); ok && len(invoices) > 0 {
if invoice, ok := invoices[0].(map[string]interface{}); ok {
return invoice, nil
}
} else {
return data, nil
}
}
return nil, fmt.Errorf("no invoice found in the response")
}