Skip to content

Commit

Permalink
adjust tests and examples
Browse files Browse the repository at this point in the history
  • Loading branch information
edmarSoaress committed Feb 20, 2024
1 parent 1cb7b29 commit f700a24
Show file tree
Hide file tree
Showing 11 changed files with 342 additions and 178 deletions.
49 changes: 35 additions & 14 deletions examples/apis/merchantorder/create/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import (
"context"
"fmt"

"github.com/google/uuid"
"github.com/mercadopago/sdk-go/pkg/config"
"github.com/mercadopago/sdk-go/pkg/merchantorder"
"github.com/mercadopago/sdk-go/pkg/preference"
)

func main() {
Expand All @@ -17,27 +19,46 @@ func main() {
return
}

// Create payment.
// Create preference.
prefReq := preference.Request{
ExternalReference: uuid.New().String(),
Items: []preference.PreferenceItemRequest{
{
ID: "123",
Title: "Title",
UnitPrice: 100,
Quantity: 1,
Description: "Description",
},
},
}

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

// Create merchant order.
req := merchantorder.Request{
ExternalReference: "default",
PreferenceID: "123456789",
ExternalReference: pref.ExternalReference,
PreferenceID: pref.ID,
Collector: &merchantorder.CollectorRequest{
ID: 123456789,
ID: pref.CollectorID,
},
SiteID: "MLB",
SiteID: pref.SiteID,
Items: []merchantorder.ItemRequest{
{
CategoryID: "MLB123456789",
CurrencyID: "BRL",
Description: "description",
PictureURL: "https://http2.mlstatic.com/D_NQ_NP_652451-MLB74602308021_022024-F.jpg",
Title: "title",
Quantity: 1,
UnitPrice: 1,
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,
},
},
ApplicationID: "123456789",
Version: 1,
}

client := merchantorder.NewClient(cfg)
Expand Down
8 changes: 4 additions & 4 deletions examples/apis/merchantorder/get/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

func main() {
accessToken := "APP_USR-4849723703374061-053108-80867bffcb4a85cda0cd797f6c40cf28-1340175910"
accessToken := "{{ACCESS_TOKEN}}"

cfg, err := config.New(accessToken)
if err != nil {
Expand All @@ -19,13 +19,13 @@ func main() {

client := merchantorder.NewClient(cfg)

var merchantOrderID int64 = 8416510703
var merchantOrderID int64 = 1234566788

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

fmt.Println(merchant)
fmt.Println(order)
}
9 changes: 3 additions & 6 deletions examples/apis/merchantorder/search/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,21 @@ import (
)

func main() {
accessToken := "APP_USR-4849723703374061-053108-80867bffcb4a85cda0cd797f6c40cf28-1340175910"
accessToken := "{{ACCESS_TOKEN}}"

cfg, err := config.New(accessToken)
if err != nil {
fmt.Println(err)
return
}

client := merchantorder.NewClient(cfg)

filter := make(map[string]string)
filter["preference_id"] = "1340175910-f2694bdc-7562-499e-a373-057cce3a027b"
filter["preference_id"] = "{{PREFERENCE_ID}}"
req := merchantorder.SearchRequest{
Limit: "10",
Offset: "0",
Filters: filter,
}

client := merchantorder.NewClient(cfg)
search, err := client.Search(context.Background(), req)
if err != nil {
fmt.Println(err)
Expand Down
71 changes: 54 additions & 17 deletions examples/apis/merchantorder/update/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import (
"context"
"fmt"

"github.com/google/uuid"
"github.com/mercadopago/sdk-go/pkg/config"
"github.com/mercadopago/sdk-go/pkg/merchantorder"
"github.com/mercadopago/sdk-go/pkg/preference"
)

func main() {
Expand All @@ -17,34 +19,69 @@ func main() {
return
}

// Create payment.
req := merchantorder.UpdateRequest{
ExternalReference: "default",
PreferenceID: "123456789",
// Create preference.
prefReq := preference.Request{
ExternalReference: uuid.New().String(),
Items: []preference.PreferenceItemRequest{
{
ID: uuid.New().String(),
Title: "Title",
UnitPrice: 100,
Quantity: 1,
Description: "Description",
},
},
}

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

// Create merchant order.
createReq := merchantorder.Request{
ExternalReference: pref.ExternalReference,
PreferenceID: pref.ID,
Collector: &merchantorder.CollectorRequest{
ID: 123456789,
ID: pref.CollectorID,
},
SiteID: "MLB",
SiteID: pref.SiteID,
Items: []merchantorder.ItemRequest{
{
CategoryID: "MLB123456789",
CurrencyID: "BRL",
Description: "description",
PictureURL: "https://http2.mlstatic.com/D_NQ_NP_652451-MLB74602308021_022024-F.jpg",
Title: "title",
Quantity: 1,
UnitPrice: 1,
ID: pref.Items[0].ID,
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,
},
},
ApplicationID: "123456789",
Version: 1,
}

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

var merchantOrderID int64 = 123456789
// Update merchant order.
req := merchantorder.UpdateRequest{
PreferenceID: pref.ID,
SiteID: pref.SiteID,
Items: []merchantorder.ItemRequest{
{
ID: order.Items[0].ID,
Quantity: 2,
},
},
}

order, err := client.Update(context.Background(), req, merchantOrderID)
order, err = client.Update(context.Background(), req, order.ID)
if err != nil {
fmt.Println(err)
return
Expand Down
15 changes: 8 additions & 7 deletions pkg/merchantorder/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,22 @@ const (
type Client interface {
// Get a specific merchant order by id.
// It is a get request to the endpoint: https://api.mercadopago.com/merchant_orders/{id}
// Reference: https://www.mercadopago.com.br/developers/en/reference/merchant_orders/_merchant_orders_id/get
// Reference: https://www.mercadopago.com/developers/en/reference/merchant_orders/_merchant_orders_id/get
Get(ctx context.Context, id int64) (*Response, error)

// Search for merchant orders.
// It is a get request to the endpoint: https://api.mercadopago.com/merchant_orders/search
// Reference: https://www.mercadopago.com.br/developers/en/reference/merchant_orders/_merchant_orders_search/get
// Reference: https://www.mercadopago.com/developers/en/reference/merchant_orders/_merchant_orders_search/get
Search(ctx context.Context, request SearchRequest) (*SearchResponse, error)

// Update a merchant order.
// It is a put request to the endpoint: https://api.mercadopago.com/merchant_orders/{id}
// Reference: https://www.mercadopago.com.br/developers/en/reference/merchant_orders/_merchant_orders_id/put
Update(ctx context.Context, request UpdateRequest, id int64) ([]Response, error)
// Reference: https://www.mercadopago.com/developers/en/reference/merchant_orders/_merchant_orders_id/put
Update(ctx context.Context, request UpdateRequest, id int64) (*Response, error)

// Create a merchant order.
// It is a post request to the endpoint: https://api.mercadopago.com/merchant_orders
// Reference: https://www.mercadopago.com.br/developers/en/reference/merchant_orders/_merchant_orders/post
// Reference: https://www.mercadopago.com/developers/en/reference/merchant_orders/_merchant_orders/post
Create(ctx context.Context, request Request) (*Response, error)
}

Expand Down Expand Up @@ -75,19 +75,20 @@ func (c *client) Search(ctx context.Context, request SearchRequest) (*SearchResp
url.RawQuery = params

res, err := baseclient.Get[*SearchResponse](ctx, c.cfg, url.String())

if err != nil {
return nil, err
}

return res, nil
}

func (c *client) Update(ctx context.Context, request UpdateRequest, id int64) ([]Response, error) {
func (c *client) Update(ctx context.Context, request UpdateRequest, id int64) (*Response, error) {
param := map[string]string{
"id": strconv.Itoa(int(id)),
}

res, err := baseclient.Put[[]Response](ctx, c.cfg, urlWithID, request, baseclient.WithPathParams(param))
res, err := baseclient.Put[*Response](ctx, c.cfg, urlWithID, request, baseclient.WithPathParams(param))
if err != nil {
return nil, err
}
Expand Down
6 changes: 2 additions & 4 deletions pkg/merchantorder/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func TestUpdate(t *testing.T) {
c := &client{
cfg: tt.fields.config,
}
got, err := c.Update(tt.args.ctx, Request{}, 1622029222)
got, err := c.Update(tt.args.ctx, UpdateRequest{}, 1622029222)
gotErr := ""
if err != nil {
gotErr = err.Error()
Expand Down Expand Up @@ -328,7 +328,7 @@ func buildResponseMock() *Response {
dateCreated, _ := time.Parse(time.RFC3339, "2023-03-28T13:50:19.143-04:00")
lastUpdate, _ := time.Parse(time.RFC3339, "2023-03-28T13:50:19.143-04:00")

res := &Response{
return &Response{
ID: 1234545454543,
Status: "opened",
PreferenceID: "1340175910-f2694bdc-7562-499e-a373-057cce3a027b",
Expand All @@ -352,8 +352,6 @@ func buildResponseMock() *Response {
},
OrderStatus: "payment_required",
}

return res
}

func buildSearchResponseMock() *SearchResponse {
Expand Down
46 changes: 23 additions & 23 deletions pkg/merchantorder/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,40 @@ package merchantorder

// Request represents merchant order request.
type Request struct {
PreferenceID string `json:"preference_id"`
ApplicationID string `json:"application_id"`
SiteID string `json:"site_id"`
SponsorID string `json:"sponsor_id"`
NotificationURL string `json:"notification_url"`
AdditionalInfo string `json:"additional_info"`
ExternalReference string `json:"external_reference"`
Marketplace string `json:"marketplace"`
Version int64 `json:"version"`
PreferenceID string `json:"preference_id,omitempty"`
ApplicationID string `json:"application_id,omitempty"`
SiteID string `json:"site_id,omitempty"`
SponsorID string `json:"sponsor_id,omitempty"`
NotificationURL string `json:"notification_url,omitempty"`
AdditionalInfo string `json:"additional_info,omitempty"`
ExternalReference string `json:"external_reference,omitempty"`
Marketplace string `json:"marketplace,omitempty"`
Version int64 `json:"version,omitempty"`

Collector *CollectorRequest `json:"collector"`
Payer *PayerRequest `json:"payer"`
Items []ItemRequest `json:"items"`
Collector *CollectorRequest `json:"collector,omitempty"`
Payer *PayerRequest `json:"payer,omitempty"`
Items []ItemRequest `json:"items,omitempty"`
}

// PayerRequest represents payer information.
type PayerRequest struct {
Nickname string `json:"nickname"`
ID int64 `json:"id"`
Nickname string `json:"nickname,omitempty"`
ID int64 `json:"id,omitempty"`
}

// ItemRequest represents item information.
type ItemRequest struct {
ID string `json:"id"`
Title string `json:"title"`
Description string `json:"description"`
PictureURL string `json:"picture_url"`
CategoryID string `json:"category_id"`
CurrencyID string `json:"currency_id"`
Quantity int `json:"quantity"`
UnitPrice float64 `json:"unit_price"`
ID string `json:"id,omitempty"`
Title string `json:"title,omitempty"`
Description string `json:"description,omitempty"`
PictureURL string `json:"picture_url,omitempty"`
CategoryID string `json:"category_id,omitempty"`
CurrencyID string `json:"currency_id,omitempty"`
Quantity int `json:"quantity,omitempty"`
UnitPrice float64 `json:"unit_price,omitempty"`
}

// CollectorRequest represents seller information.
type CollectorRequest struct {
ID int64 `json:"id"`
ID int64 `json:"id,omitempty"`
}
2 changes: 1 addition & 1 deletion pkg/merchantorder/search_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

// SearchRequest is the request to search services.
// Filters field can receive a lot of paramaters. For details, see:
// https://www.mercadopago.com.br/developers/pt/reference/payments/_payments_search/get.
// https://www.mercadopago.com/developers/en/reference/merchant_orders/_merchant_orders_search/get.
type SearchRequest struct {
Limit string
Offset string
Expand Down
11 changes: 2 additions & 9 deletions pkg/merchantorder/search_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,6 @@ package merchantorder
// SearchResponse represents the response from the search endpoint.
type SearchResponse struct {
Elements []Response `json:"elements"`
// Paging PagingResponse `json:"paging"`
Total int64 `json:"total"`
Offset int64 `json:"next_offset"`
Total int64 `json:"total"`
Offset int64 `json:"next_offset"`
}

// // PagingResponse represents the paging information within SearchResponse.
// type PagingResponse struct {
// Total int64 `json:"total"`
// Offset int64 `json:"next_offset"`
// }
Loading

0 comments on commit f700a24

Please sign in to comment.