-
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.
* implementation, unit test, integration test and example * refactoring to adapt to merge * refactor type at integration test * edit comment * remove blank line * add omitempy to FreeTrialRequest * refactor field name * remove omitempty * rename variable name * rename variable name * rename variable name * refactor comment * refactor preapproval search IT * refactor searchResponse
- Loading branch information
1 parent
f361d01
commit 07f1fb1
Showing
16 changed files
with
1,212 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,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) | ||
} |
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,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) | ||
} |
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,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) | ||
} | ||
} |
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,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) | ||
} |
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,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 | ||
} |
Oops, something went wrong.