Skip to content

Commit

Permalink
Feature/switch result to resource (#46)
Browse files Browse the repository at this point in the history
* Switch result to resource

* Switch result to resource

* Change req to request

* Change pay to resource

* Change pref to resource

* Change refund to resource

* Sort clients instantiation

* Sort clients instantiation

* Change card and cards to resource and resources

* Change value name on for range statements

* Default for range names

* Change order to resource

* Change paymentMethods to resources

* Change canceledResponse to resource

* Change paymentIntent to resource

* Change devices to resources

* Change opMode to resource

* Change update to updateRequest

* Change usr to resource

* Change merchant order requests names

* Change cred to resource

* Switch res to resource
  • Loading branch information
gdeandradero authored Mar 20, 2024
1 parent ca46968 commit 2b30a3c
Show file tree
Hide file tree
Showing 83 changed files with 631 additions and 583 deletions.
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ func main() {
return
}

req := payment.Request{
client := payment.NewClient(cfg)

request := payment.Request{
TransactionAmount: 105.1,
Payer: &payment.PayerRequest{
Email: "{{EMAIL}}",
Expand All @@ -59,14 +61,13 @@ func main() {
Installments: 1,
}

client := payment.NewClient(cfg)
pay, err := client.Create(context.Background(), req)
resource, err := client.Create(context.Background(), request)
if err != nil {
fmt.Println(err)
return
}

fmt.Println(pay)
fmt.Println(resource)
}
```

Expand All @@ -86,14 +87,14 @@ To make requests to the Mercado Pago APIs, you can use the packages provided by

```go
client := paymentmethod.NewClient(cfg)
paymentMethods, err := client.List(context.Background())
resources, err := client.List(context.Background())
```

### Exception throwing handling

Every package methods returns two variables: response (type of the package) and error (type of the std lib), which will contain any error thrown. It is important to handle these errors in the best possible way.
```go
paymentMethods, err := client.List(context.Background())
resources, err := client.List(context.Background())
if err != nil {
// appropriate treatment
}
Expand Down
6 changes: 3 additions & 3 deletions examples/apis/cardtoken/create/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func main() {

client := cardtoken.NewClient(cfg)

var req = cardtoken.Request{
request := cardtoken.Request{
SiteID: "{{SITE_ID}}",
CardNumber: "{{CARD_NUMBER}}",
ExpirationMonth: "11",
Expand All @@ -34,10 +34,10 @@ func main() {
},
}

result, err := client.Create(context.Background(), req)
resource, err := client.Create(context.Background(), request)
if err != nil {
return
}

fmt.Println(result)
fmt.Println(resource)
}
9 changes: 5 additions & 4 deletions examples/apis/customer/create/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@ func main() {
return
}

req := customer.Request{Email: "{{EMAIL}}"}

client := customer.NewClient(cfg)
cus, err := client.Create(context.Background(), req)

request := customer.Request{Email: "{{EMAIL}}"}

resource, err := client.Create(context.Background(), request)
if err != nil {
fmt.Println(err)
return
}

fmt.Println(cus)
fmt.Println(resource)
}
7 changes: 4 additions & 3 deletions examples/apis/customer/get/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@ func main() {
return
}

client := customer.NewClient(cfg)

customerID := "{{CUSTOMER_ID}}"

client := customer.NewClient(cfg)
cus, err := client.Get(context.Background(), customerID)
resource, err := client.Get(context.Background(), customerID)
if err != nil {
fmt.Println(err)
return
}

fmt.Println(cus)
fmt.Println(resource)
}
9 changes: 5 additions & 4 deletions examples/apis/customer/search/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,19 @@ func main() {
return
}

req := customer.SearchRequest{
client := customer.NewClient(cfg)

request := customer.SearchRequest{
Filters: map[string]string{
"EMAIL": "{{EMAIL}}",
},
}

client := customer.NewClient(cfg)
cus, err := client.Search(context.Background(), req)
resource, err := client.Search(context.Background(), request)
if err != nil {
fmt.Println(err)
return
}

fmt.Println(cus)
fmt.Println(resource)
}
9 changes: 5 additions & 4 deletions examples/apis/customer/update/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,16 @@ func main() {
return
}

client := customer.NewClient(cfg)

customerID := "{{CUSTOMER_ID}}"
req := customer.Request{Description: "Description updated."}
request := customer.Request{Description: "Description updated."}

client := customer.NewClient(cfg)
cus, err := client.Update(context.Background(), customerID, req)
resource, err := client.Update(context.Background(), customerID, request)
if err != nil {
fmt.Println(err)
return
}

fmt.Println(cus)
fmt.Println(resource)
}
9 changes: 5 additions & 4 deletions examples/apis/customercard/create/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@ func main() {
return
}

req := customercard.Request{Token: "{{CARD_TOKEN}}"}

client := customercard.NewClient(cfg)
card, err := client.Create(context.Background(), "{{CUSTOMER_ID}}", req)

request := customercard.Request{Token: "{{CARD_TOKEN}}"}

resource, err := client.Create(context.Background(), "{{CUSTOMER_ID}}", request)
if err != nil {
fmt.Println(err)
return
}

fmt.Println(card)
fmt.Println(resource)
}
5 changes: 3 additions & 2 deletions examples/apis/customercard/delete/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ func main() {
}

client := customercard.NewClient(cfg)
card, err := client.Delete(context.Background(), "{{CUSTOMER_ID}}", "{{CARD_ID}}")

resource, err := client.Delete(context.Background(), "{{CUSTOMER_ID}}", "{{CARD_ID}}")
if err != nil {
fmt.Println(err)
return
}

fmt.Println(card)
fmt.Println(resource)
}
5 changes: 3 additions & 2 deletions examples/apis/customercard/get/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ func main() {
}

client := customercard.NewClient(cfg)
card, err := client.Get(context.Background(), "{{CUSTOMER_ID}}", "{{CARD_ID}}")

resource, err := client.Get(context.Background(), "{{CUSTOMER_ID}}", "{{CARD_ID}}")
if err != nil {
fmt.Println(err)
return
}

fmt.Println(card)
fmt.Println(resource)
}
7 changes: 4 additions & 3 deletions examples/apis/customercard/list/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ func main() {
}

client := customercard.NewClient(cfg)
cards, err := client.List(context.Background(), "{{CUSTOMER_ID}}")

resources, err := client.List(context.Background(), "{{CUSTOMER_ID}}")
if err != nil {
fmt.Println(err)
return
}

for _, c := range cards {
fmt.Println(c)
for _, v := range resources {
fmt.Println(v)
}
}
9 changes: 5 additions & 4 deletions examples/apis/customercard/update/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@ func main() {
return
}

req := customercard.Request{Token: "{{CARD_TOKEN}}"}

client := customercard.NewClient(cfg)
card, err := client.Update(context.Background(), "{{CUSTOMER_ID}}", "{{CARD_ID}}", req)

request := customercard.Request{Token: "{{CARD_TOKEN}}"}

resource, err := client.Update(context.Background(), "{{CUSTOMER_ID}}", "{{CARD_ID}}", request)
if err != nil {
fmt.Println(err)
return
}

fmt.Println(card)
fmt.Println(resource)
}
5 changes: 3 additions & 2 deletions examples/apis/identificationtype/list/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ func main() {
}

client := identificationtype.NewClient(cfg)
identificationTypes, err := client.List(context.Background())

resources, err := client.List(context.Background())
if err != nil {
fmt.Println(err)
return
}

for _, v := range identificationTypes {
for _, v := range resources {
fmt.Println(v)
}
}
5 changes: 3 additions & 2 deletions examples/apis/invoice/get/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"context"
"fmt"

"github.com/mercadopago/sdk-go/pkg/config"
"github.com/mercadopago/sdk-go/pkg/invoice"
)
Expand All @@ -18,11 +19,11 @@ func main() {

invoiceID := "123"

result, err := client.Get(context.Background(), invoiceID)
resource, err := client.Get(context.Background(), invoiceID)
if err != nil {
fmt.Println(err)
return
}

fmt.Println(result)
fmt.Println(resource)
}
8 changes: 4 additions & 4 deletions examples/apis/invoice/search/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,21 @@ func main() {

client := invoice.NewClient(cfg)

req := invoice.SearchRequest{
request := invoice.SearchRequest{
Limit: 10,
Offset: 10,
Filters: map[string]string{
"preapproval_id": "preapproval_id",
},
}

result, err := client.Search(context.Background(), req)
resource, err := client.Search(context.Background(), request)
if err != nil {
fmt.Println(err)
return
}

for _, inv := range result.Results {
fmt.Println(inv)
for _, v := range resource.Results {
fmt.Println(v)
}
}
37 changes: 19 additions & 18 deletions examples/apis/merchantorder/create/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@ func main() {
return
}

preferenceClient := preference.NewClient(cfg)
merchantOrderClient := merchantorder.NewClient(cfg)

// Create preference.
prefReq := preference.Request{
preferenceRequest := preference.Request{
ExternalReference: uuid.New().String(),
Items: []preference.PreferenceItemRequest{
{
Expand All @@ -33,40 +36,38 @@ func main() {
},
}

preferenceClient := preference.NewClient(cfg)
pref, err := preferenceClient.Create(context.Background(), prefReq)
preferenceResource, err := preferenceClient.Create(context.Background(), preferenceRequest)
if err != nil {
fmt.Println(err)
return
}

// Create merchant order.
req := merchantorder.Request{
ExternalReference: pref.ExternalReference,
PreferenceID: pref.ID,
merchantOrderRequest := merchantorder.Request{
ExternalReference: preferenceResource.ExternalReference,
PreferenceID: preferenceResource.ID,
Collector: &merchantorder.CollectorRequest{
ID: pref.CollectorID,
ID: preferenceResource.CollectorID,
},
SiteID: pref.SiteID,
SiteID: preferenceResource.SiteID,
Items: []merchantorder.ItemRequest{
{
CategoryID: pref.Items[0].CategoryID,
CurrencyID: pref.Items[0].CurrencyID,
Description: pref.Items[0].Description,
PictureURL: pref.Items[0].PictureURL,
Title: pref.Items[0].Title,
Quantity: pref.Items[0].Quantity,
UnitPrice: pref.Items[0].UnitPrice,
CategoryID: preferenceResource.Items[0].CategoryID,
CurrencyID: preferenceResource.Items[0].CurrencyID,
Description: preferenceResource.Items[0].Description,
PictureURL: preferenceResource.Items[0].PictureURL,
Title: preferenceResource.Items[0].Title,
Quantity: preferenceResource.Items[0].Quantity,
UnitPrice: preferenceResource.Items[0].UnitPrice,
},
},
}

client := merchantorder.NewClient(cfg)
order, err := client.Create(context.Background(), req)
merchantOrderResource, err := merchantOrderClient.Create(context.Background(), merchantOrderRequest)
if err != nil {
fmt.Println(err)
return
}

fmt.Println(order)
fmt.Println(merchantOrderResource)
}
4 changes: 2 additions & 2 deletions examples/apis/merchantorder/get/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ func main() {

merchantOrderID := 1234566788

order, err := client.Get(context.Background(), merchantOrderID)
resource, err := client.Get(context.Background(), merchantOrderID)
if err != nil {
fmt.Println(err)
return
}

fmt.Println(order)
fmt.Println(resource)
}
Loading

0 comments on commit 2b30a3c

Please sign in to comment.