Skip to content

Feature/refund implementation #9

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

Merged
merged 13 commits into from
Feb 14, 2024
Merged
4 changes: 2 additions & 2 deletions examples/apis/payment/cancel/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ func main() {
client := payment.NewClient(cfg)
var paymentID int64 = 123

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

fmt.Println(result)
fmt.Println(payment)
}
6 changes: 3 additions & 3 deletions examples/apis/payment/capture/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,18 @@ func main() {
}

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

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

fmt.Println(pay)
fmt.Println(payment)
}
6 changes: 3 additions & 3 deletions examples/apis/payment/capture_amount/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,18 @@ func main() {
}

client := payment.NewClient(cfg)
pay, err := client.Create(context.Background(), paymentRequest)
payment, 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)
payment, err = client.CaptureAmount(context.Background(), payment.ID, 100.1)
if err != nil {
fmt.Println(err)
return
}

fmt.Println(pay)
fmt.Println(payment)
}
4 changes: 2 additions & 2 deletions examples/apis/payment/create/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ func main() {
}

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

fmt.Println(pay)
fmt.Println(payment)
}
7 changes: 3 additions & 4 deletions examples/apis/payment/get/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,19 @@ func main() {
Email: "{{EMAIL}}",
},
}

client := payment.NewClient(cfg)

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

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

fmt.Println(pay)
fmt.Println(payment)
}
4 changes: 2 additions & 2 deletions examples/apis/payment/search/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ func main() {
}

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

fmt.Println(pay)
fmt.Println(payment)
}
4 changes: 2 additions & 2 deletions examples/apis/paymentmethod/list/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ func main() {
return
}

client := paymentmethod.NewClient(cfg)
paymentMethods, err := client.List(context.Background())
paymentMethodClient := paymentmethod.NewClient(cfg)
paymentMethods, err := paymentMethodClient.List(context.Background())
if err != nil {
fmt.Println(err)
return
Expand Down
32 changes: 32 additions & 0 deletions examples/apis/refund/get/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package main

import (
"context"
"fmt"

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

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

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

refundClient := refund.NewClient(cfg)

var paymentID int64 = 12344555
var refundID int64 = 12344555

refund, err := refundClient.Get(context.Background(), paymentID, refundID)
if err != nil {
fmt.Println(err)
return
}

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

import (
"context"
"fmt"

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

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

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

refundClient := refund.NewClient(cfg)

var paymentID int64 = 12233344

refunds, err := refundClient.List(context.Background(), paymentID)
if err != nil {
fmt.Println(err)
return
}
for _, v := range refunds {
fmt.Println(v)
}
}
50 changes: 50 additions & 0 deletions examples/apis/refund/partial_refund/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package main

import (
"context"
"fmt"

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

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: "master",
Payer: &payment.PayerRequest{
Email: "{{EMAIL}}",
},
Token: "{{TOKEN}}",
Installments: 1,
Capture: false,
}

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

partialAmount := payment.TransactionAmount - 10.0

refundClient := refund.NewClient(cfg)
refund, err := refundClient.CreatePartialRefund(context.Background(), partialAmount, payment.ID)
if err != nil {
fmt.Println(err)
return
}

fmt.Println(refund)
}
48 changes: 48 additions & 0 deletions examples/apis/refund/total_refund/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/payment"
"github.com/mercadopago/sdk-go/pkg/refund"
)

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: "master",
Payer: &payment.PayerRequest{
Email: "{{EMAIL}}",
},
Token: "{{TOKEN}}",
Installments: 1,
Capture: false,
}

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

refundClient := refund.NewClient(cfg)
refund, err := refundClient.Create(context.Background(), payment.ID)
if err != nil {
fmt.Println(err)
return
}

fmt.Println(refund)
}
4 changes: 2 additions & 2 deletions examples/apis/user/get/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ func main() {
return
}

client := user.NewClient(cfg)
user, err := client.Get(context.Background())
userClient := user.NewClient(cfg)
user, err := userClient.Get(context.Background())
if err != nil {
fmt.Println(err)
return
Expand Down
20 changes: 9 additions & 11 deletions pkg/payment/payment.go → pkg/payment/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@ import (
)

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

// Client contains the methods to interact with the Payments API.
Expand Down Expand Up @@ -62,7 +60,7 @@ func NewClient(c *config.Config) Client {
}

func (c *client) Create(ctx context.Context, request Request) (*Response, error) {
res, err := httpclient.Post[Response](ctx, c.cfg, postURL, request)
res, err := httpclient.Post[Response](ctx, c.cfg, urlBase, request)
if err != nil {
return nil, err
}
Expand All @@ -73,7 +71,7 @@ func (c *client) Create(ctx context.Context, request Request) (*Response, error)
func (c *client) Search(ctx context.Context, dto SearchRequest) (*SearchResponse, error) {
params := dto.Parameters()

url, err := url.Parse(searchURL)
url, err := url.Parse(urlSearch)
if err != nil {
return nil, fmt.Errorf("error parsing url: %w", err)
}
Expand All @@ -90,7 +88,7 @@ func (c *client) Search(ctx context.Context, dto SearchRequest) (*SearchResponse
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))
res, err := httpclient.Get[Response](ctx, c.cfg, strings.Replace(urlWithID, "{id}", conv, 1))
if err != nil {
return nil, err
}
Expand All @@ -102,7 +100,7 @@ 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)
res, err := httpclient.Put[Response](ctx, c.cfg, strings.Replace(urlWithID, "{id}", conv, 1), dto)
if err != nil {
return nil, err
}
Expand All @@ -114,7 +112,7 @@ 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)
res, err := httpclient.Put[Response](ctx, c.cfg, strings.Replace(urlWithID, "{id}", conv, 1), dto)
if err != nil {
return nil, err
}
Expand All @@ -126,7 +124,7 @@ func (c *client) CaptureAmount(ctx context.Context, id int64, amount float64) (*
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)
res, err := httpclient.Put[Response](ctx, c.cfg, strings.Replace(urlWithID, "{id}", conv, 1), dto)
if err != nil {
return nil, err
}
Expand Down
Loading