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

Feature/preapproval plan #28

Merged
merged 11 commits into from
Mar 6, 2024
48 changes: 48 additions & 0 deletions examples/apis/preapprovalplan/create/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package main

import (
"context"
"fmt"
"github.com/mercadopago/sdk-go/pkg/config"
"github.com/mercadopago/sdk-go/pkg/preapprovalplan"
)

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

req := preapprovalplan.Request{
AutoRecurring: preapprovalplan.AutoRecurringRequest{
Frequency: 1,
FrequencyType: "days",
TransactionAmount: 5,
CurrencyID: "BRL",
},
BackURL: "https://www.yoursite.com",
PaymentMethodsAllowed: preapprovalplan.PaymentMethodsAllowedRequest{
PaymentTypes: []preapprovalplan.PaymentTypeRequest{
{
ID: "credit_card",
},
},
PaymentMethods: []preapprovalplan.PaymentMethodRequest{
{
ID: "bolbradesco",
},
},
},
Reason: "Yoga classes",
}

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

fmt.Println(result)
}
29 changes: 29 additions & 0 deletions examples/apis/preapprovalplan/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/preapprovalplan"

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

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

client := preapprovalplan.NewClient(cfg)

preApprovalPlanID := "123"

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

fmt.Println(result)
}
37 changes: 37 additions & 0 deletions examples/apis/preapprovalplan/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/preapprovalplan"

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

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

client := preapprovalplan.NewClient(cfg)

filters := preapprovalplan.SearchRequest{
Limit: "10",
Offset: "10",
Filters: map[string]string{
"status": "active",
},
}

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

for _, plan := range result.Results {
fmt.Println(plan)
}
}
67 changes: 67 additions & 0 deletions examples/apis/preapprovalplan/update/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package main

import (
"context"
"fmt"
"github.com/mercadopago/sdk-go/pkg/preapprovalplan"

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

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

req := preapprovalplan.Request{
AutoRecurring: preapprovalplan.AutoRecurringRequest{
Frequency: 1,
FrequencyType: "days",
TransactionAmount: 5,
CurrencyID: "BRL",
},
BackURL: "https://www.yoursite.com",
PaymentMethodsAllowed: preapprovalplan.PaymentMethodsAllowedRequest{
PaymentTypes: []preapprovalplan.PaymentTypeRequest{
{
ID: "credit_card",
},
},
PaymentMethods: []preapprovalplan.PaymentMethodRequest{
{
ID: "bolbradesco",
},
},
},
Reason: "Yoga classes",
}

client := preapprovalplan.NewClient(cfg)

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

req = preapprovalplan.Request{
AutoRecurring: preapprovalplan.AutoRecurringRequest{
Frequency: 1,
FrequencyType: "months",
TransactionAmount: 10,
BillingDay: 10,
Repetitions: 12,
CurrencyID: "BRL",
},
}

pref, err = client.Update(context.Background(), req, pref.ID)
brunacamposxx marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
fmt.Println(err)
return
}

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

import (
"context"
"fmt"
"github.com/mercadopago/sdk-go/pkg/config"
"github.com/mercadopago/sdk-go/pkg/internal/baseclient"
"net/url"
"strings"
)

const (
urlBase = "https://api.mercadopago.com/preapproval_plan"
urlWithID = urlBase + "/{id}"
urlSearch = urlBase + "/search"
)

// Client contains the methods to interact with the Pre Approval Plan API.
type Client interface {
// Create creates a new pre-approval plan.
// It is a post request to the endpoint: https://api.mercadopago.com/preapproval_plan
// Reference: https://www.mercadopago.com/developers/en/reference/subscriptions/_preapproval_plan/post/
Create(ctx context.Context, request Request) (*Response, error)

// Get finds a pre-approval plan by ID.
// It is a get request to the endpoint: https://api.mercadopago.com/preapproval_plan/{id}
// Reference: https://www.mercadopago.com/developers/en/reference/subscriptions/_preapproval_plan_id/get
Get(ctx context.Context, id string) (*Response, error)

// Update updates details a pre-approval plan by ID.
// It is a put request to the endpoint: https://api.mercadopago.com/checkout/preferences/{id}
brunacamposxx marked this conversation as resolved.
Show resolved Hide resolved
// Reference: https://www.mercadopago.com/developers/en/reference/subscriptions/_preapproval_plan_id/put
Update(ctx context.Context, request Request, id string) (*Response, error)

// Search finds all pre-approval plan information generated through specific filters
brunacamposxx marked this conversation as resolved.
Show resolved Hide resolved
// It is a get request to the endpoint: https://api.mercadopago.com/preapproval_plan/search
// Reference: https://www.mercadopago.com/developers/en/reference/subscriptions/_preapproval_plan_search/get
Search(ctx context.Context, request SearchRequest) (*SearchResponse, error)
}

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

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

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

return result, nil
}

func (c *client) Get(ctx context.Context, id string) (*Response, error) {
result, err := baseclient.Get[*Response](ctx, c.cfg, strings.Replace(urlWithID, "{id}", id, 1))
if err != nil {
return nil, err
}

return result, nil
}

func (c *client) Update(ctx context.Context, request Request, id string) (*Response, error) {
result, err := baseclient.Put[*Response](ctx, c.cfg, strings.Replace(urlWithID, "{id}", id, 1), request)
if err != nil {
return nil, err
}

return result, nil
}

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

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

result, err := baseclient.Get[*SearchResponse](ctx, c.cfg, parsedURL.String())
if err != nil {
return nil, err
}

return result, nil
}
Loading
Loading