diff --git a/examples/apis/payment/cancel/main.go b/examples/apis/payment/cancel/main.go index 522a9378..b74eab66 100644 --- a/examples/apis/payment/cancel/main.go +++ b/examples/apis/payment/cancel/main.go @@ -18,7 +18,8 @@ func main() { } client := payment.NewClient(cfg) - paymentID := int64(123) + var paymentID int64 = 123 + result, err := client.Cancel(context.Background(), paymentID) if err != nil { fmt.Println(err) diff --git a/examples/apis/payment/capture/main.go b/examples/apis/payment/capture/main.go index 39957409..c617ccd9 100644 --- a/examples/apis/payment/capture/main.go +++ b/examples/apis/payment/capture/main.go @@ -30,14 +30,14 @@ func main() { } client := payment.NewClient(cfg) - result, err := client.Create(context.Background(), paymentRequest) + pay, err := client.Create(context.Background(), paymentRequest) if err != nil { fmt.Println(err) return } // Capture. - result, err = client.Capture(context.Background(), result.ID) + result, err := client.Capture(context.Background(), pay.ID) if err != nil { fmt.Println(err) return diff --git a/examples/apis/payment/capture_amount/main.go b/examples/apis/payment/capture_amount/main.go index 420d4357..972f82ac 100644 --- a/examples/apis/payment/capture_amount/main.go +++ b/examples/apis/payment/capture_amount/main.go @@ -30,14 +30,14 @@ func main() { } client := payment.NewClient(cfg) - result, err := client.Create(context.Background(), paymentRequest) + pay, err := client.Create(context.Background(), paymentRequest) if err != nil { fmt.Println(err) return } // Capture amount. - result, err = client.CaptureAmount(context.Background(), result.ID, 100.1) + result, err := client.CaptureAmount(context.Background(), pay.ID, 100.1) if err != nil { fmt.Println(err) return diff --git a/examples/apis/payment/create/main.go b/examples/apis/payment/create/main.go index 79679f6f..57cb2695 100644 --- a/examples/apis/payment/create/main.go +++ b/examples/apis/payment/create/main.go @@ -28,11 +28,11 @@ func main() { } client := payment.NewClient(cfg) - result, err := client.Create(context.Background(), paymentRequest) + pay, err := client.Create(context.Background(), paymentRequest) if err != nil { fmt.Println(err) return } - fmt.Println(result) + fmt.Println(pay) } diff --git a/examples/apis/payment/get/main.go b/examples/apis/payment/get/main.go index 91ed5c5b..1c6478f5 100644 --- a/examples/apis/payment/get/main.go +++ b/examples/apis/payment/get/main.go @@ -16,7 +16,7 @@ func main() { fmt.Println(err) return } - dto := payment.Request{ + paymentRequest := payment.Request{ TransactionAmount: 105.1, PaymentMethodID: "pix", Payer: &payment.PayerRequest{ @@ -26,17 +26,17 @@ func main() { client := payment.NewClient(cfg) - result, err := client.Create(context.Background(), dto) + pay, err := client.Create(context.Background(), paymentRequest) if err != nil { fmt.Println(err) return } - result, err = client.Get(context.Background(), result.ID) + pay, err = client.Get(context.Background(), pay.ID) if err != nil { fmt.Println(err) return } - fmt.Println(result) + fmt.Println(pay) } diff --git a/examples/apis/payment/search/main.go b/examples/apis/payment/search/main.go index 209c1d85..c62549b7 100644 --- a/examples/apis/payment/search/main.go +++ b/examples/apis/payment/search/main.go @@ -24,11 +24,11 @@ func main() { } client := payment.NewClient(cfg) - result, err := client.Search(context.Background(), paymentRequest) + pay, err := client.Search(context.Background(), paymentRequest) if err != nil { fmt.Println(err) return } - fmt.Println(result) + fmt.Println(pay) } diff --git a/pkg/payment/payment.go b/pkg/payment/payment.go index 0de4e2db..f1baafa5 100644 --- a/pkg/payment/payment.go +++ b/pkg/payment/payment.go @@ -24,12 +24,12 @@ type Client interface { // Create creates a new payment. // It is a post request to the endpoint: https://api.mercadopago.com/v1/payments // Reference: https://www.mercadopago.com.br/developers/pt/reference/payments/_payments/post/ - Create(ctx context.Context, dto Request) (*Response, error) + Create(ctx context.Context, request Request) (*Response, error) // Search searches for payments. // It is a get request to the endpoint: https://api.mercadopago.com/v1/payments/search // Reference: https://www.mercadopago.com.br/developers/pt/reference/payments/_payments_search/get/ - Search(ctx context.Context, dto SearchRequest) (*SearchResponse, error) + Search(ctx context.Context, request SearchRequest) (*SearchResponse, error) // Get gets a payment by its ID. // It is a get request to the endpoint: https://api.mercadopago.com/v1/payments/{id} @@ -61,8 +61,8 @@ func NewClient(c *config.Config) Client { } } -func (c *client) Create(ctx context.Context, dto Request) (*Response, error) { - body, err := json.Marshal(&dto) +func (c *client) Create(ctx context.Context, request Request) (*Response, error) { + body, err := json.Marshal(&request) if err != nil { return nil, fmt.Errorf("error marshaling request body: %w", err) } diff --git a/pkg/payment/search_request.go b/pkg/payment/search_request.go index 4a054052..9d8d5801 100644 --- a/pkg/payment/search_request.go +++ b/pkg/payment/search_request.go @@ -2,7 +2,6 @@ package payment import ( "net/url" - "strings" ) // SearchRequest is the request to search services. @@ -19,30 +18,22 @@ type SearchRequest struct { func (s SearchRequest) Parameters() string { params := url.Values{} - var limitKey, offsetKey bool for k, v := range s.Filters { params.Add(k, v) - - if strings.EqualFold(k, "limit") { - limitKey = true - continue - } - if strings.EqualFold(k, "offset") { - offsetKey = true - } } - if !limitKey { + if _, ok := s.Filters["limit"]; !ok { limit := "30" if s.Limit != "" { limit = s.Limit } params.Add("limit", limit) } - if !offsetKey { + + if _, ok := s.Filters["offset"]; !ok { offset := "0" - if s.Limit != "" { - offset = s.Limit + if s.Offset != "" { + offset = s.Offset } params.Add("offset", offset) } diff --git a/test/integration/payment/payment_test.go b/test/integration/payment/payment_test.go index 40c37aec..d984bc78 100644 --- a/test/integration/payment/payment_test.go +++ b/test/integration/payment/payment_test.go @@ -13,7 +13,7 @@ import ( func TestPayment(t *testing.T) { t.Run("should_create_payment", func(t *testing.T) { - c, err := config.New(os.Getenv("at")) + c, err := config.New(os.Getenv("ACCESS_TOKEN")) if err != nil { t.Fatal(err) } @@ -41,7 +41,7 @@ func TestPayment(t *testing.T) { }) t.Run("should_search_payment", func(t *testing.T) { - c, err := config.New(os.Getenv("at")) + c, err := config.New(os.Getenv("ACCESS_TOKEN")) if err != nil { t.Fatal(err) } @@ -63,7 +63,7 @@ func TestPayment(t *testing.T) { }) t.Run("should_get_payment", func(t *testing.T) { - c, err := config.New(os.Getenv("at")) + c, err := config.New(os.Getenv("ACCESS_TOKEN")) if err != nil { t.Fatal(err) } @@ -98,7 +98,7 @@ func TestPayment(t *testing.T) { }) t.Run("should_cancel_payment", func(t *testing.T) { - c, err := config.New(os.Getenv("at")) + c, err := config.New(os.Getenv("ACCESS_TOKEN")) if err != nil { t.Fatal(err) } @@ -134,7 +134,7 @@ func TestPayment(t *testing.T) { // We should validate how to test capture and capture amount. t.Run("should_capture_payment", func(t *testing.T) { - c, err := config.New(os.Getenv("at")) + c, err := config.New(os.Getenv("ACCESS_TOKEN")) if err != nil { t.Fatal(err) } @@ -172,7 +172,7 @@ func TestPayment(t *testing.T) { }) t.Run("should_capture_amount_payment", func(t *testing.T) { - c, err := config.New(os.Getenv("at")) + c, err := config.New(os.Getenv("ACCESS_TOKEN")) if err != nil { t.Fatal(err) }