Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preference #4

Merged
merged 30 commits into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
ff45c18
add get preference by id and create preference
brunacamposxx Jan 23, 2024
971f7c9
correcting pointers
brunacamposxx Jan 23, 2024
8beb301
add update and search
brunacamposxx Jan 24, 2024
165f8dc
Merge branch 'feature/payment-methods' into feature/preference
brunacamposxx Jan 24, 2024
5b1e985
add get, create and update unit tests
brunacamposxx Jan 26, 2024
11203ed
rename path to json response list
brunacamposxx Jan 26, 2024
39933ff
Merge branch 'feature/payment-methods' into feature/preference
brunacamposxx Jan 26, 2024
41a11aa
add search test
brunacamposxx Jan 29, 2024
553b6d9
Merge branch 'feature/payment-methods' into feature/preference
brunacamposxx Jan 29, 2024
375a5c0
adapt changes generated in payment methods branch to functions and tests
brunacamposxx Jan 29, 2024
4a8160c
add integration test to preference
brunacamposxx Jan 29, 2024
19953ab
move tests to a single test file
brunacamposxx Jan 31, 2024
7443f27
Merge branch 'main' into feature/preference
brunacamposxx Jan 31, 2024
b9ce992
adjustments made to adapt to main changes
brunacamposxx Feb 1, 2024
5e5d3fb
formatting documents
brunacamposxx Feb 2, 2024
573a0f0
change urls format
brunacamposxx Feb 2, 2024
e989e7b
Merge branch 'feature/payments-implementation' into feature/preference
edmarSoaress Feb 8, 2024
bdcd6a9
add call to generic httpClient and adjust test
edmarSoaress Feb 8, 2024
62e4b0e
Merge branch 'main' into feature/preference
edmarSoaress Feb 9, 2024
b837d01
adjust url and add pointer
edmarSoaress Feb 9, 2024
88d9a40
adjust variable name
edmarSoaress Feb 9, 2024
66fc10c
Merge branch 'main' into feature/preference
edmarSoaress Feb 9, 2024
137ee22
adjust urls
edmarSoaress Feb 9, 2024
bfa588a
Merge branch 'main' into feature/preference
edmarSoaress Feb 14, 2024
f060764
rename response models, rename variable names
edmarSoaress Feb 15, 2024
eec6088
removed tests and adjust variables
edmarSoaress Feb 16, 2024
5e848ae
adjust examples
edmarSoaress Feb 16, 2024
767707e
adjust examples
edmarSoaress Feb 16, 2024
2b853aa
adjust examples
edmarSoaress Feb 16, 2024
3c3219d
remove pointer of struct
edmarSoaress Feb 19, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions examples/apis/preference/create/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package main

import (
"context"
"fmt"

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

func main() {
cfg, err := config.New("{{ACCESS_TOKEN}}")
if err != nil {
fmt.Println(err)
return
}

req := preference.Request{
Items: []preference.PreferenceItemRequest{
{
ID: "123",
Title: "Title",
UnitPrice: 100,
Quantity: 1,
Description: "Description",
},
},
}

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

fmt.Println(pref)
}
29 changes: 29 additions & 0 deletions examples/apis/preference/get/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package main

import (
"context"
"fmt"

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

func main() {
cfg, err := config.New("{{ACCESS_TOKEN}}")
if err != nil {
fmt.Println(err)
return
}

client := preference.NewClient(cfg)

preferenceID := "123"

pref, err := client.Get(context.Background(), preferenceID)
if err != nil {
fmt.Println(err)
return
}

fmt.Println(pref)
}
37 changes: 37 additions & 0 deletions examples/apis/preference/search/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package main

import (
"context"
"fmt"

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

func main() {
cfg, err := config.New("{{ACCESS_TOKEN}}")
if err != nil {
fmt.Println(err)
return
}

client := preference.NewClient(cfg)

filter := make(map[string]string)
filter["external_reference"] = "wee3rffee23"
filters := preference.SearchRequest{
Limit: "10",
Offset: "10",
Filters: filter,
}

search, err := client.Search(context.Background(), filters)
if err != nil {
fmt.Println(err)
return
}

for _, pref := range search.Elements {
fmt.Println(pref)
}
}
57 changes: 57 additions & 0 deletions examples/apis/preference/update/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package main

import (
"context"
"fmt"

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

func main() {
cfg, err := config.New("{{ACCESS_TOKEN}}")
if err != nil {
fmt.Println(err)
return
}

req := preference.Request{
Items: []preference.PreferenceItemRequest{
{
ID: "123",
Title: "Title",
UnitPrice: 100,
Quantity: 1,
Description: "Description",
},
},
}

client := preference.NewClient(cfg)

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

req = preference.Request{
Items: []preference.PreferenceItemRequest{
{
ID: "123",
Title: "Title",
UnitPrice: 100,
Quantity: 4,
Description: "Description",
},
},
}

pref, err = client.Update(context.Background(), req, pref.ID)
if err != nil {
fmt.Println(err)
return
}

fmt.Println(pref)
}
103 changes: 103 additions & 0 deletions pkg/preference/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package preference

import (
"context"
"fmt"
"net/url"

"github.com/mercadopago/sdk-go/pkg/config"
"github.com/mercadopago/sdk-go/pkg/internal/baseclient"
)

const (
urlBase = "https://api.mercadopago.com/checkout/preferences"
urlSearch = urlBase + "/search"
urlWithID = urlBase + "/:id"
)

// Client contains the methods to interact with the Preference API.
type Client interface {
// Create a preference with information about a product or service and obtain the URL needed to start the payment flow.
// It is a post request to the endpoint: https://api.mercadopago.com/checkout/preferences
// Reference: https://www.mercadopago.com/developers/en/reference/preferences/_checkout_preferences/post
Create(ctx context.Context, request Request) (*Response, error)

// Get finds a preference by ID.
// It is a get request to the endpoint: https://api.mercadopago.com/checkout/preferences/{id}
// Reference: https://www.mercadopago.com/developers/en/reference/preferences/_checkout_preferences_id/get
Get(ctx context.Context, id string) (*Response, error)

// Update updates details for a payment preference.
// It is a put request to the endpoint: https://api.mercadopago.com/checkout/preferences/{id}
// Reference: https://www.mercadopago.com/developers/en/reference/preferences/_checkout_preferences_id/put
Update(ctx context.Context, request Request, id string) (*Response, error)

// Search finds all preference information generated through specific filters
// It is a get request to the endpoint: https://api.mercadopago.com/checkout/preferences/search
// Reference: https://www.mercadopago.com/developers/en/reference/preferences/_checkout_preferences_search/get
Search(ctx context.Context, request SearchRequest) (*SearchResponsePage, error)
}

// client is the implementation of Client.
type client struct {
cfg *config.Config
}

// NewClient returns a new Preference API Client.
func NewClient(c *config.Config) Client {
return &client{
cfg: c,
}
}

func (c *client) Create(ctx context.Context, request Request) (*Response, error) {
res, err := baseclient.Post[*Response](ctx, c.cfg, urlBase, request)
if err != nil {
return nil, err
}

return res, nil
}

func (c *client) Get(ctx context.Context, id string) (*Response, error) {
params := map[string]string{
"id": id,
}

res, err := baseclient.Get[*Response](ctx, c.cfg, urlWithID, baseclient.WithPathParams(params))
if err != nil {
return nil, err
}

return res, nil
}

func (c *client) Update(ctx context.Context, request Request, id string) (*Response, error) {
params := map[string]string{
"id": id,
}

res, err := baseclient.Put[*Response](ctx, c.cfg, urlWithID, request, baseclient.WithPathParams(params))
if err != nil {
return nil, err
}

return res, nil
}

func (c *client) Search(ctx context.Context, request SearchRequest) (*SearchResponsePage, error) {
params := request.Parameters()

url, err := url.Parse(urlSearch)
if err != nil {
return nil, fmt.Errorf("error parsing url: %w", err)
}
url.RawQuery = params

res, err := baseclient.Get[*SearchResponsePage](ctx, c.cfg, url.String())
if err != nil {
return nil, err
}

return res, nil
}
Loading
Loading