package atlas import ( "bytes" "context" "crypto/sha1" "encoding/hex" "encoding/json" "fmt" "io" "net/http" "time" "github.com/SamuelTariku/FortuneBet-Backend/internal/config" "github.com/SamuelTariku/FortuneBet-Backend/internal/services/wallet" ) type Client struct { http *http.Client BaseURL string PrivateKey string CasinoID string PartnerID string walletSvc *wallet.Service } func NewClient(cfg *config.Config, walletSvc *wallet.Service) *Client { return &Client{ http: &http.Client{Timeout: 10 * time.Second}, BaseURL: cfg.Atlas.BaseURL, PrivateKey: cfg.Atlas.SecretKey, // PRIVATE_KEY from Atlas CasinoID: cfg.Atlas.CasinoID, // provided by Atlas PartnerID: cfg.Atlas.PartnerID, // aggregator/casino partner_id walletSvc: walletSvc, } } // Generate timestamp in ms func nowTimestamp() string { return fmt.Sprintf("%d", time.Now().UnixMilli()) } // Signature generator: sha1 hex of (REQUEST_BODY + PRIVATE_KEY + timestamp) func (c *Client) generateHash(body []byte, timestamp string) string { plain := string(body) + c.PrivateKey + timestamp h := sha1.New() h.Write([]byte(plain)) return hex.EncodeToString(h.Sum(nil)) } // POST helper func (c *Client) post(ctx context.Context, path string, body map[string]any, result any) error { // Add timestamp first timestamp := nowTimestamp() // Marshal without hash first tmp, _ := json.Marshal(body) // Generate hash using original body hash := c.generateHash(tmp, timestamp) body["hash"] = hash body["timestamp"] = timestamp fmt.Printf("atlasPost: %v \n", body) // Marshal final body data, _ := json.Marshal(body) req, _ := http.NewRequestWithContext(ctx, "POST", c.BaseURL+path, bytes.NewReader(data)) req.Header.Set("Content-Type", "application/json") // Debug fmt.Println("Request URL:", c.BaseURL+path) fmt.Println("Request Body:", string(data)) // Send request res, err := c.http.Do(req) if err != nil { return err } defer res.Body.Close() b, _ := io.ReadAll(res.Body) if res.StatusCode >= 400 { return fmt.Errorf("error: %s", string(b)) } if result != nil { return json.Unmarshal(b, result) } return nil }