From f700a24e93b82dbd1fe07e93a9c78c439c164470 Mon Sep 17 00:00:00 2001 From: edmarSoaress Date: Tue, 20 Feb 2024 17:05:45 -0300 Subject: [PATCH] adjust tests and examples --- examples/apis/merchantorder/create/main.go | 49 +++-- examples/apis/merchantorder/get/main.go | 8 +- examples/apis/merchantorder/search/main.go | 9 +- examples/apis/merchantorder/update/main.go | 71 +++++-- pkg/merchantorder/client.go | 15 +- pkg/merchantorder/client_test.go | 6 +- pkg/merchantorder/request.go | 46 ++--- pkg/merchantorder/search_request.go | 2 +- pkg/merchantorder/search_response.go | 11 +- pkg/merchantorder/update_request.go | 124 ++++++------ .../merchantorder/merchantorder_test.go | 179 +++++++++++++++--- 11 files changed, 342 insertions(+), 178 deletions(-) diff --git a/examples/apis/merchantorder/create/main.go b/examples/apis/merchantorder/create/main.go index 2e1e4696..f0e94440 100644 --- a/examples/apis/merchantorder/create/main.go +++ b/examples/apis/merchantorder/create/main.go @@ -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() { @@ -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) diff --git a/examples/apis/merchantorder/get/main.go b/examples/apis/merchantorder/get/main.go index f48b7955..6c41d5a6 100644 --- a/examples/apis/merchantorder/get/main.go +++ b/examples/apis/merchantorder/get/main.go @@ -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 { @@ -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) } diff --git a/examples/apis/merchantorder/search/main.go b/examples/apis/merchantorder/search/main.go index 8bb60fe8..e98a3c18 100644 --- a/examples/apis/merchantorder/search/main.go +++ b/examples/apis/merchantorder/search/main.go @@ -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 { @@ -17,16 +17,13 @@ func main() { 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) diff --git a/examples/apis/merchantorder/update/main.go b/examples/apis/merchantorder/update/main.go index 57a8e102..899e8d10 100644 --- a/examples/apis/merchantorder/update/main.go +++ b/examples/apis/merchantorder/update/main.go @@ -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() { @@ -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 diff --git a/pkg/merchantorder/client.go b/pkg/merchantorder/client.go index cce11714..4456d558 100644 --- a/pkg/merchantorder/client.go +++ b/pkg/merchantorder/client.go @@ -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) } @@ -75,6 +75,7 @@ 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 } @@ -82,12 +83,12 @@ func (c *client) Search(ctx context.Context, request SearchRequest) (*SearchResp 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 } diff --git a/pkg/merchantorder/client_test.go b/pkg/merchantorder/client_test.go index 1211f0f3..763df377 100644 --- a/pkg/merchantorder/client_test.go +++ b/pkg/merchantorder/client_test.go @@ -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() @@ -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", @@ -352,8 +352,6 @@ func buildResponseMock() *Response { }, OrderStatus: "payment_required", } - - return res } func buildSearchResponseMock() *SearchResponse { diff --git a/pkg/merchantorder/request.go b/pkg/merchantorder/request.go index 32c65e4a..79048f07 100644 --- a/pkg/merchantorder/request.go +++ b/pkg/merchantorder/request.go @@ -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"` } diff --git a/pkg/merchantorder/search_request.go b/pkg/merchantorder/search_request.go index b492d9ed..7fc24ab0 100644 --- a/pkg/merchantorder/search_request.go +++ b/pkg/merchantorder/search_request.go @@ -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 diff --git a/pkg/merchantorder/search_response.go b/pkg/merchantorder/search_response.go index 895b78bc..c20cee4b 100644 --- a/pkg/merchantorder/search_response.go +++ b/pkg/merchantorder/search_response.go @@ -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"` -// } diff --git a/pkg/merchantorder/update_request.go b/pkg/merchantorder/update_request.go index 72fbce91..69073866 100644 --- a/pkg/merchantorder/update_request.go +++ b/pkg/merchantorder/update_request.go @@ -4,103 +4,103 @@ import "time" // UpdateRequest represents merchant order update. type UpdateRequest 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"` - Shipments []ShipmentRequest `json:"shipments"` + Collector *CollectorRequest `json:"collector,omitempty"` + Payer *PayerRequest `json:"payer,omitempty"` + Items []ItemRequest `json:"items,omitempty"` + Shipments []ShipmentRequest `json:"shipments,omitempty"` } // ShipmentRequest represents shipment information. type ShipmentRequest struct { - ShippingType string `json:"shipping_type"` - ShippingMode string `json:"shipping_mode"` - PickingType string `json:"picking_type"` - Status string `json:"status"` - ShippingSubstatus string `json:"shipping_substatus"` - ServiceID string `json:"service_id"` - ID int64 `json:"id"` - SenderID int64 `json:"sender_id"` - ReceiverID int64 `json:"receiver_id"` - Items []map[string]interface{} `json:"items"` + ShippingType string `json:"shipping_type,omitempty"` + ShippingMode string `json:"shipping_mode,omitempty"` + PickingType string `json:"picking_type,omitempty"` + Status string `json:"status,omitempty"` + ShippingSubstatus string `json:"shipping_substatus,omitempty"` + ServiceID string `json:"service_id,omitempty"` + ID int64 `json:"id,omitempty"` + SenderID int64 `json:"sender_id,omitempty"` + ReceiverID int64 `json:"receiver_id,omitempty"` + Items []map[string]interface{} `json:"items,omitempty"` - DateCreated *time.Time `json:"date_created"` - LastModified *time.Time `json:"last_modified"` - DateFirstPrinted *time.Time `json:"date_first_printed"` - ReceiverAddress *ReceiverAddressRequest `json:"receiver_address"` - ShippingOption *ShippingOptionRequest `json:"shipping_option"` + DateCreated *time.Time `json:"date_created,omitempty"` + LastModified *time.Time `json:"last_modified,omitempty"` + DateFirstPrinted *time.Time `json:"date_first_printed,omitempty"` + ReceiverAddress *ReceiverAddressRequest `json:"receiver_address,omitempty"` + ShippingOption *ShippingOptionRequest `json:"shipping_option,omitempty"` } // ReceiverAddressRequest represents receiver address information. type ReceiverAddressRequest struct { - AddressLine string `json:"address_line"` - Apartment string `json:"apartment"` - Comment string `json:"comment"` - Contact string `json:"contact"` - ZipCode string `json:"zip_code"` - StreetName string `json:"street_name"` - StreetNumber string `json:"street_number"` - Floor string `json:"floor"` - Phone string `json:"phone"` - Latitude string `json:"latitude"` - Longitude string `json:"longitude"` - ID int64 `json:"id"` + AddressLine string `json:"address_line,omitempty"` + Apartment string `json:"apartment,omitempty"` + Comment string `json:"comment,omitempty"` + Contact string `json:"contact,omitempty"` + ZipCode string `json:"zip_code,omitempty"` + StreetName string `json:"street_name,omitempty"` + StreetNumber string `json:"street_number,omitempty"` + Floor string `json:"floor,omitempty"` + Phone string `json:"phone,omitempty"` + Latitude string `json:"latitude,omitempty"` + Longitude string `json:"longitude,omitempty"` + ID int64 `json:"id,omitempty"` - City *ReceiverAddressCityRequest `json:"city"` - State *ReceiverAddressStateRequest `json:"state"` - Country *ReceiverAddressCountryRequest `json:"country"` + City *ReceiverAddressCityRequest `json:"city,omitempty"` + State *ReceiverAddressStateRequest `json:"state,omitempty"` + Country *ReceiverAddressCountryRequest `json:"country,omitempty"` } // ShippingOptionRequest represents shipping option information. type ShippingOptionRequest struct { - Name string `json:"name"` - CurrencyID string `json:"currency_id"` - ShippingMethodID int64 `json:"shipping_method_id"` - ID int64 `json:"id"` - Cost float64 `json:"cost"` - ListCost float64 `json:"list_cost"` + Name string `json:"name,omitempty"` + CurrencyID string `json:"currency_id,omitempty"` + ShippingMethodID int64 `json:"shipping_method_id,omitempty"` + ID int64 `json:"id,omitempty"` + Cost float64 `json:"cost,omitempty"` + ListCost float64 `json:"list_cost,omitempty"` - EstimatedDelivery *ShippingEstimatedDeliveryRequest `json:"estimated_delivery"` - Speed *ShippingSpeedRequest `json:"speed"` + EstimatedDelivery *ShippingEstimatedDeliveryRequest `json:"estimated_delivery,omitempty"` + Speed *ShippingSpeedRequest `json:"speed,omitempty"` } // ReceiverAddressCityRequest represents city information. type ReceiverAddressCityRequest struct { - ID string `json:"id"` - Name string `json:"name"` + ID string `json:"id,omitempty"` + Name string `json:"name,omitempty"` } // ReceiverAddressStateRequest represents state information. type ReceiverAddressStateRequest struct { - ID string `json:"id"` - Name string `json:"name"` + ID string `json:"id,omitempty"` + Name string `json:"name,omitempty"` } // ReceiverAddressCountryRequest represents country information. type ReceiverAddressCountryRequest struct { - ID string `json:"id"` - Name string `json:"name"` + ID string `json:"id,omitempty"` + Name string `json:"name,omitempty"` } // ShippingEstimatedDeliveryRequest represents estimated delivery information. type ShippingEstimatedDeliveryRequest struct { - TimeFrom string `json:"time_from"` - TimeTo string `json:"time_to"` + TimeFrom string `json:"time_from,omitempty"` + TimeTo string `json:"time_to,omitempty"` - Date *time.Time `json:"date"` + Date *time.Time `json:"date,omitempty"` } // ShippingSpeedRequest represents shipping speed information. type ShippingSpeedRequest struct { - Handling int64 `json:"handling"` - Shipping int64 `json:"shipping"` + Handling int64 `json:"handling,omitempty"` + Shipping int64 `json:"shipping,omitempty"` } diff --git a/test/integration/merchantorder/merchantorder_test.go b/test/integration/merchantorder/merchantorder_test.go index 1b1f2200..e5657641 100644 --- a/test/integration/merchantorder/merchantorder_test.go +++ b/test/integration/merchantorder/merchantorder_test.go @@ -2,11 +2,13 @@ package integration import ( "context" + "fmt" "os" "testing" "github.com/mercadopago/sdk-go/pkg/config" "github.com/mercadopago/sdk-go/pkg/merchantorder" + "github.com/mercadopago/sdk-go/pkg/preference" ) func TestMerchantOrder(t *testing.T) { @@ -16,26 +18,47 @@ func TestMerchantOrder(t *testing.T) { t.Fatal(err) } + prefReq := preference.Request{ + 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 pref == nil { + t.Error("preference can't be nil") + return + } + if err != nil { + t.Errorf(err.Error()) + } + + // 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) @@ -50,35 +73,57 @@ func TestMerchantOrder(t *testing.T) { }) t.Run("should_update_merchant_order", func(t *testing.T) { - cfg, err := config.New(os.Getenv("ACCESS_TOKEN")) + cfg, err := config.New("TEST-4849723703374061-053108-98d6fdf742a963513320c567195b5cd6-1340175910") if err != nil { t.Fatal(err) } - req := merchantorder.UpdateRequest{ - ExternalReference: "default", - PreferenceID: "123456789", + prefReq := preference.Request{ + 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 pref == nil { + t.Error("preference can't be nil") + return + } + if err != nil { + t.Errorf(err.Error()) + } + + // 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.Update(context.Background(), req, 123456789) + order, err := client.Create(context.Background(), createReq) if order == nil { t.Error("merchant order can't be nil") return @@ -86,6 +131,28 @@ func TestMerchantOrder(t *testing.T) { if err != nil { t.Errorf(err.Error()) } + + // 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, order.ID) + if order == nil { + fmt.Println(err) + t.Error("merchant order can't be nil") + return + } + if err != nil { + t.Errorf(err.Error()) + } }) t.Run("should_get_merchant_order", func(t *testing.T) { @@ -94,8 +161,51 @@ func TestMerchantOrder(t *testing.T) { t.Fatal(err) } + prefReq := preference.Request{ + 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 pref == nil { + t.Error("preference can't be nil") + return + } + if err != nil { + t.Errorf(err.Error()) + } + + // Create merchant order. + req := merchantorder.Request{ + ExternalReference: pref.ExternalReference, + PreferenceID: pref.ID, + Collector: &merchantorder.CollectorRequest{ + ID: pref.CollectorID, + }, + SiteID: pref.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, + }, + }, + } + client := merchantorder.NewClient(cfg) - order, err := client.Get(context.Background(), 123456789) + order, err := client.Create(context.Background(), req) if order == nil { t.Error("merchant order can't be nil") return @@ -104,6 +214,14 @@ func TestMerchantOrder(t *testing.T) { t.Errorf(err.Error()) } + order, err = client.Get(context.Background(), order.ID) + if order == nil { + t.Error("merchant order can't be nil") + return + } + if err != nil { + t.Errorf(err.Error()) + } if order.ID == 0 { t.Error("id can't be nil") } @@ -120,7 +238,6 @@ func TestMerchantOrder(t *testing.T) { } client := merchantorder.NewClient(cfg) - order, err := client.Search(context.Background(), req) if order == nil { t.Error("merchant order can't be nil")