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 #32

Merged
merged 15 commits into from
Mar 12, 2024
38 changes: 38 additions & 0 deletions examples/apis/preapproval/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/preapproval"
)

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

req := preapproval.Request{
AutoRecurring: &preapproval.AutoRecurringRequest{
Frequency: 1,
FrequencyType: "months",
TransactionAmount: 100,
CurrencyID: "BRL",
},
BackURL: "https://www.yoursite.com",
ExternalReference: "Ref-123",
PayerEmail: "[email protected]",
Reason: "Yoga Class",
}

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

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

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

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

client := preapproval.NewClient(cfg)

preApprovalID := "123"

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

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

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

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

client := preapproval.NewClient(cfg)

filters := preapproval.SearchRequest{
Limit: "10",
Offset: "10",
Filters: map[string]string{
"payer_id": "123123123",
},
}

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

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

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

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

req := preapproval.Request{
AutoRecurring: &preapproval.AutoRecurringRequest{
Frequency: 1,
FrequencyType: "months",
TransactionAmount: 100,
CurrencyID: "BRL",
},
BackURL: "https://www.yoursite.com",
ExternalReference: "Ref-123",
PayerEmail: "[email protected]",
Reason: "Yoga Class",
}

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

update := preapproval.UpdateRequest{
AutoRecurring: &preapproval.AutoRecurringRequest{
Frequency: 1,
FrequencyType: "months",
TransactionAmount: 100,
CurrencyID: "BRL",
},
BackURL: "https://www.yoursite.com",
ExternalReference: "Ref-123",
Reason: "Yoga Class",
}

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

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

import (
"context"
"github.com/mercadopago/sdk-go/pkg/config"
"github.com/mercadopago/sdk-go/pkg/internal/httpclient"
"net/http"
)

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

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

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

// Update updates details a pre-approval by ID.
// It is a put request to the endpoint: https://api.mercadopago.com/preapproval/{id}
// Reference: https://www.mercadopago.com/developers/en/reference/subscriptions/_preapproval_id/put
Update(ctx context.Context, request UpdateRequest, id string) (*Response, error)

// Search finds all pre-approval information generated through specific filters.
// It is a get request to the endpoint: https://api.mercadopago.com/preapproval/search
// Reference: https://www.mercadopago.com/developers/en/reference/subscriptions/_preapproval_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 API Client.
func NewClient(c *config.Config) Client {
return &client{
cfg: c,
}
}

func (c *client) Create(ctx context.Context, request Request) (*Response, error) {
requestData := httpclient.RequestData{
Body: request,
Method: http.MethodPost,
URL: urlBase,
}
result, err := httpclient.DoRequest[*Response](ctx, c.cfg, requestData)
if err != nil {
return nil, err
}

return result, nil
}

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

requestData := httpclient.RequestData{
PathParams: pathParams,
Method: http.MethodGet,
URL: urlWithID,
}
result, err := httpclient.DoRequest[*Response](ctx, c.cfg, requestData)
if err != nil {
return nil, err
}

return result, nil
}

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

requestData := httpclient.RequestData{
Body: request,
PathParams: pathParams,
Method: http.MethodPut,
URL: urlWithID,
}
result, err := httpclient.DoRequest[*Response](ctx, c.cfg, requestData)
if err != nil {
return nil, err
}

return result, nil
}

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

requestData := httpclient.RequestData{
QueryParams: queryParams,
Method: http.MethodGet,
URL: urlSearch,
}
result, err := httpclient.DoRequest[*SearchResponse](ctx, c.cfg, requestData)
if err != nil {
return nil, err
}

return result, nil
}
Loading
Loading