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/payments implementation #3

Merged
merged 32 commits into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
8b22954
Update client
gdeandradero Jan 25, 2024
331ffd8
Improve structs
gdeandradero Jan 25, 2024
e943ef4
Add more fields to response struct
gdeandradero Jan 25, 2024
cd5fac0
Adjust search
gdeandradero Jan 26, 2024
7617d60
Add some tests
gdeandradero Jan 26, 2024
ff73ca8
Merge branch 'feature/payment-methods' into feature/payments-implemen…
gdeandradero Jan 26, 2024
8419356
Merge branch 'feature/payment-methods' into feature/payments-implemen…
gdeandradero Jan 26, 2024
3a533a2
Refactor tests
gdeandradero Jan 26, 2024
508df48
Merge branch 'feature/payment-methods' into feature/payments-implemen…
gdeandradero Jan 26, 2024
0d77d35
Add payment examples
gdeandradero Jan 26, 2024
b1030c9
Search test
gdeandradero Jan 27, 2024
d15e653
Add get tests
gdeandradero Jan 27, 2024
4274609
Add cancel test
gdeandradero Jan 27, 2024
5433d64
Add capture test
gdeandradero Jan 27, 2024
fb2c9ae
Add capture amount test
gdeandradero Jan 27, 2024
71f6e48
Add integration tests
gdeandradero Jan 27, 2024
d908a89
Update test name
gdeandradero Jan 27, 2024
75af2dc
Merge branch 'feature/payment-methods' into feature/payments-implemen…
edmarSoaress Jan 31, 2024
a3b4825
merge main into feature payment implementation
edmarSoaress Jan 31, 2024
1c21392
adjust integration test
edmarSoaress Jan 31, 2024
7728ca7
adjust examples
edmarSoaress Jan 31, 2024
a22d5ff
adjust capture payment integration tests
edmarSoaress Feb 1, 2024
01fe142
adjust examples and integration test
edmarSoaress Feb 1, 2024
d44a2b1
adjust examples
edmarSoaress Feb 1, 2024
ba97a1d
Merge branch 'main' into feature/payments-implementation
edmarSoaress Feb 1, 2024
09341bb
rename variable names
edmarSoaress Feb 1, 2024
eec206b
remover pointer from response
edmarSoaress Feb 2, 2024
7c220ea
add integration with generic httpclient
edmarSoaress Feb 7, 2024
a3a2b47
Merge branch 'main' into feature/payments-implementation
edmarSoaress Feb 7, 2024
35c9f05
merge with develop and adjust test
edmarSoaress Feb 7, 2024
37b775b
add lint suggestion
edmarSoaress Feb 7, 2024
d4f6178
add lint suggestion
edmarSoaress Feb 7, 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
30 changes: 30 additions & 0 deletions examples/apis/payment/cancel/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package main

import (
"context"
"fmt"

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

func main() {
accessToken := "{{ACCESS_TOKEN}}"

cfg, err := config.New(accessToken)
if err != nil {
fmt.Println(err)
return
}

client := payment.NewClient(cfg)
var paymentID int64 = 123

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

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

import (
"context"
"fmt"

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

func main() {
accessToken := "{{ACCESS_TOKEN}}"

cfg, err := config.New(accessToken)
if err != nil {
fmt.Println(err)
return
}

// Create payment.
paymentRequest := payment.Request{
TransactionAmount: 105.1,
PaymentMethodID: "visa",
Payer: &payment.PayerRequest{
Email: "{{EMAIL}}",
},
Token: "{{CARD_TOKEN}}",
Installments: 1,
Capture: false,
}

client := payment.NewClient(cfg)
pay, err := client.Create(context.Background(), paymentRequest)
if err != nil {
fmt.Println(err)
return
}

// Capture.
pay, err = client.Capture(context.Background(), pay.ID)
if err != nil {
fmt.Println(err)
return
}

fmt.Println(pay)
}
47 changes: 47 additions & 0 deletions examples/apis/payment/capture_amount/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package main

import (
"context"
"fmt"

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

func main() {
accessToken := "{{ACCESS_TOKEN}}"

cfg, err := config.New(accessToken)
if err != nil {
fmt.Println(err)
return
}

// Create payment.
paymentRequest := payment.Request{
TransactionAmount: 105.1,
PaymentMethodID: "visa",
Payer: &payment.PayerRequest{
Email: "{{EMAIL}}",
},
Token: "{{CARD_TOKEN}}",
Installments: 1,
Capture: false,
}

client := payment.NewClient(cfg)
pay, err := client.Create(context.Background(), paymentRequest)
if err != nil {
fmt.Println(err)
return
}

// Capture amount.
pay, err = client.CaptureAmount(context.Background(), pay.ID, 100.1)
if err != nil {
fmt.Println(err)
return
}

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

func main() {
accessToken := "{{ACCESS_TOKEN}}"

cfg, err := config.New(accessToken)
if err != nil {
fmt.Println(err)
return
}

paymentRequest := payment.Request{
TransactionAmount: 105.1,
PaymentMethodID: "visa",
Payer: &payment.PayerRequest{
Email: "{{EMAIL}}",
},
Token: "{{CARD_TOKEN}}",
Installments: 1,
}

client := payment.NewClient(cfg)
pay, err := client.Create(context.Background(), paymentRequest)
if err != nil {
fmt.Println(err)
return
}

fmt.Println(pay)
}
42 changes: 42 additions & 0 deletions examples/apis/payment/get/main.go
edmarSoaress marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package main

import (
"context"
"fmt"

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

func main() {
accessToken := "{{ACCESS_TOKEN}}"

cfg, err := config.New(accessToken)
if err != nil {
fmt.Println(err)
return
}
paymentRequest := payment.Request{
TransactionAmount: 105.1,
PaymentMethodID: "pix",
Payer: &payment.PayerRequest{
Email: "{{EMAIL}}",
},
}

client := payment.NewClient(cfg)

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

pay, err = client.Get(context.Background(), pay.ID)
if err != nil {
fmt.Println(err)
return
}

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

import (
"context"
"fmt"

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

func main() {
accessToken := "{{ACCESS_TOKEN}}"

cfg, err := config.New(accessToken)
if err != nil {
fmt.Println(err)
return
}

paymentRequest := payment.SearchRequest{
Filters: map[string]string{
"external_reference": "abc_def_ghi_123_456123",
},
}

client := payment.NewClient(cfg)
pay, err := client.Search(context.Background(), paymentRequest)
if err != nil {
fmt.Println(err)
return
}

fmt.Println(pay)
}
135 changes: 135 additions & 0 deletions pkg/payment/payment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
package payment

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

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

const (
baseURL = "https://api.mercadopago.com/v1/"
postURL = baseURL + "payments"
searchURL = baseURL + "payments/search"
getURL = baseURL + "payments/{id}"
putURL = baseURL + "payments/{id}"
)

// Client contains the methods to interact with the Payments API.
type Client interface {
// Create creates a new payment.
// It is a post request to the endpoint: https://api.mercadopago.com/v1/payments
// Reference: https://www.mercadopago.com/developers/en/reference/payments/_payments/post/
Create(ctx context.Context, request Request) (*Response, error)

// Search searches for payments.
// It is a get request to the endpoint: https://api.mercadopago.com/v1/payments/search
// Reference: https://www.mercadopago.com/developers/en/reference/payments/_payments_search/get/
Search(ctx context.Context, request SearchRequest) (*SearchResponse, error)

// Get gets a payment by its ID.
// It is a get request to the endpoint: https://api.mercadopago.com/v1/payments/{id}
// Reference: https://www.mercadopago.com/developers/en/reference/payments/_payments_id/get/
Get(ctx context.Context, id int64) (*Response, error)

// Cancel cancels a payment by its ID.
// It is a put request to the endpoint: https://api.mercadopago.com/v1/payments/{id}
Cancel(ctx context.Context, id int64) (*Response, error)

// Capture captures a payment by its ID.
// It is a put request to the endpoint: https://api.mercadopago.com/v1/payments/{id}
Capture(ctx context.Context, id int64) (*Response, error)

// CaptureAmount captures amount of a payment by its ID.
// It is a put request to the endpoint: https://api.mercadopago.com/v1/payments/{id}
CaptureAmount(ctx context.Context, id int64, amount float64) (*Response, error)
}

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

// NewClient returns a new Payments 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 := httpclient.Post[Response](ctx, c.cfg, postURL, request)
if err != nil {
return nil, err
}

return res, nil
}

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

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

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

return res, nil
}

func (c *client) Get(ctx context.Context, id int64) (*Response, error) {
conv := strconv.Itoa(int(id))

res, err := httpclient.Get[Response](ctx, c.cfg, strings.Replace(getURL, "{id}", conv, 1))
if err != nil {
return nil, err
}

return res, nil
}

func (c *client) Cancel(ctx context.Context, id int64) (*Response, error) {
dto := &CancelRequest{Status: "cancelled"}
conv := strconv.Itoa(int(id))

res, err := httpclient.Put[Response](ctx, c.cfg, strings.Replace(putURL, "{id}", conv, 1), dto)
if err != nil {
return nil, err
}

return res, nil
}

func (c *client) Capture(ctx context.Context, id int64) (*Response, error) {
dto := &CaptureRequest{Capture: true}
conv := strconv.Itoa(int(id))

res, err := httpclient.Put[Response](ctx, c.cfg, strings.Replace(putURL, "{id}", conv, 1), dto)
if err != nil {
return nil, err
}

return res, nil
}

func (c *client) CaptureAmount(ctx context.Context, id int64, amount float64) (*Response, error) {
dto := &CaptureRequest{TransactionAmount: amount, Capture: true}
conv := strconv.Itoa(int(id))

res, err := httpclient.Put[Response](ctx, c.cfg, strings.Replace(putURL, "{id}", conv, 1), dto)
if err != nil {
return nil, err
}

return res, nil
}
Loading
Loading