Skip to content

Commit

Permalink
rename variable names
Browse files Browse the repository at this point in the history
  • Loading branch information
edmarSoaress authored and eltinMeli committed Feb 9, 2024
1 parent d65833a commit 42299e0
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 37 deletions.
3 changes: 2 additions & 1 deletion examples/apis/payment/cancel/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions examples/apis/payment/capture/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions examples/apis/payment/capture_amount/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions examples/apis/payment/create/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
8 changes: 4 additions & 4 deletions examples/apis/payment/get/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func main() {
fmt.Println(err)
return
}
dto := payment.Request{
paymentRequest := payment.Request{
TransactionAmount: 105.1,
PaymentMethodID: "pix",
Payer: &payment.PayerRequest{
Expand All @@ -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)
}
4 changes: 2 additions & 2 deletions examples/apis/payment/search/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
8 changes: 4 additions & 4 deletions pkg/payment/payment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down Expand Up @@ -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)
}
Expand Down
19 changes: 5 additions & 14 deletions pkg/payment/search_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package payment

import (
"net/url"
"strings"
)

// SearchRequest is the request to search services.
Expand All @@ -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)
}
Expand Down
12 changes: 6 additions & 6 deletions test/integration/payment/payment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit 42299e0

Please sign in to comment.