-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add pre approval plan - implementation, unit test, integration tests, examples * adjustments to maintain standard * adjustments made to maintain standard * adjustments made to maintain standard * adjustment in the name of struct and pointers * adjustments requested in code review * remove return in a test * add a return in a test * add a return in a test * remove a return in a test * remove wrong comment
- Loading branch information
1 parent
17c7b45
commit 3251ce4
Showing
15 changed files
with
1,215 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
result, 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", | ||
}, | ||
} | ||
|
||
result, err = client.Update(context.Background(), req, result.ID) | ||
if err != nil { | ||
fmt.Println(err) | ||
return | ||
} | ||
|
||
fmt.Println(result) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/preapproval_plan/{id} | ||
// 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. | ||
// 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 | ||
} |
Oops, something went wrong.