Просмотр исходного кода

add the deposit qrcode dinamic

gdias 3 недель назад
Сommit
181b37844d
6 измененных файлов с 174 добавлено и 0 удалено
  1. 2 0
      .env
  2. 10 0
      README.md
  3. 8 0
      go.mod
  4. 4 0
      go.sum
  5. 150 0
      main.go
  6. BIN
      woovi

+ 2 - 0
.env

@@ -0,0 +1,2 @@
+WOOVI_APPID=Q2xpZW50X0lkX2U3MmU0YmJkLTMzYWQtNGYzNC05Y2M3LTg0ZmJkMGQzMDI1MzpDbGllbnRfU2VjcmV0X1J5WXJCSkZRSDlKK0V3NWJ2Z1dCM2xJWElIWjNuTUVrY0txcTBqNFZUdHc9
+WOOVI_BASE_URL=https://api.woovi.com

+ 10 - 0
README.md

@@ -0,0 +1,10 @@
+Para executar o programa, use o comando:
+
+```
+./woovi -value=100 -expires=600 -correlation=9134e286-6f71-427a-bf00-241681624586
+```
+retorna:
+
+```
+https://api.woovi.com/api/v1/charge/9134e286-6f71-427a-bf00-241681624586
+```

+ 8 - 0
go.mod

@@ -0,0 +1,8 @@
+module woovi-cli
+
+go 1.25.0
+
+require (
+	github.com/google/uuid v1.6.0
+	github.com/joho/godotenv v1.5.1
+)

+ 4 - 0
go.sum

@@ -0,0 +1,4 @@
+github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
+github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
+github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
+github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=

+ 150 - 0
main.go

@@ -0,0 +1,150 @@
+package main
+
+import (
+	"bytes"
+	"encoding/json"
+	"flag"
+	"fmt"
+	"io"
+	"net/http"
+	"os"
+
+	"github.com/google/uuid"
+	"github.com/joho/godotenv"
+)
+
+/*
+======================
+ Models
+======================
+*/
+
+type CreateChargeRequest struct {
+	CorrelationID string `json:"correlationID"`
+	Value         int    `json:"value"`
+	ExpiresIn     int    `json:"expiresIn"`
+	Comment       string `json:"comment,omitempty"`
+}
+
+type CreateChargeResponse struct {
+	Charge struct {
+		BRCode string `json:"brCode"`
+	} `json:"charge"`
+}
+
+func main() {
+	/*
+	======================
+	 Load .env
+	======================
+	*/
+	_ = godotenv.Load()
+
+	appID := os.Getenv("WOOVI_APPID")
+	baseURL := os.Getenv("WOOVI_BASE_URL")
+
+	if appID == "" || baseURL == "" {
+		fmt.Fprintln(os.Stderr, "Erro: WOOVI_APPID ou WOOVI_BASE_URL não definidos")
+		os.Exit(1)
+	}
+
+	/*
+	======================
+	 CLI flags
+	======================
+	*/
+	value := flag.Int("value", 0, "Valor em centavos")
+	expires := flag.Int("expires", 900, "Tempo de expiração em segundos")
+	correlation := flag.String("correlation", "", "CorrelationID (opcional)")
+	comment := flag.String("comment", "", "Comentário (opcional)")
+	flag.Parse()
+
+	if *value <= 0 {
+		fmt.Fprintln(os.Stderr, "Erro: -value é obrigatório e deve ser > 0")
+		os.Exit(1)
+	}
+
+	/*
+	======================
+	 CorrelationID
+	======================
+	*/
+	corrID := *correlation
+	if corrID == "" {
+		corrID = uuid.New().String()
+	}
+
+	/*
+	======================
+	 Payload
+	======================
+	*/
+	payload := CreateChargeRequest{
+		CorrelationID: corrID,
+		Value:         *value,
+		ExpiresIn:     *expires,
+		Comment:       *comment,
+	}
+
+	bodyJSON, err := json.Marshal(payload)
+	if err != nil {
+		fmt.Fprintln(os.Stderr, "Erro ao gerar JSON:", err)
+		os.Exit(1)
+	}
+
+	/*
+	======================
+	 HTTP Request
+	======================
+	*/
+	req, err := http.NewRequest(
+		"POST",
+		baseURL+"/api/v1/charge",
+		bytes.NewBuffer(bodyJSON),
+	)
+	if err != nil {
+		fmt.Fprintln(os.Stderr, "Erro ao criar request:", err)
+		os.Exit(1)
+	}
+
+	req.Header.Set("Authorization", appID)
+	req.Header.Set("Content-Type", "application/json")
+
+	client := &http.Client{}
+	res, err := client.Do(req)
+	if err != nil {
+		fmt.Fprintln(os.Stderr, "Erro ao chamar API:", err)
+		os.Exit(1)
+	}
+	defer res.Body.Close()
+
+	respBody, _ := io.ReadAll(res.Body)
+
+	if res.StatusCode < 200 || res.StatusCode >= 300 {
+		fmt.Fprintln(os.Stderr, string(respBody))
+		os.Exit(1)
+	}
+
+	/*
+	======================
+	 Parse response
+	======================
+	*/
+	var result CreateChargeResponse
+	if err := json.Unmarshal(respBody, &result); err != nil {
+		fmt.Fprintln(os.Stderr, "Erro ao parsear resposta:", err)
+		os.Exit(1)
+	}
+
+	if result.Charge.BRCode == "" {
+		fmt.Fprintln(os.Stderr, "Erro: BRCode não retornado")
+		os.Exit(1)
+	}
+
+	/*
+	======================
+	 Output CLI (ONLY URL)
+	======================
+	*/
+	fmt.Println(result.Charge.BRCode)
+}