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

Add Point integrations #18

Merged
merged 7 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
28 changes: 28 additions & 0 deletions examples/apis/point/cancel/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/point"
)

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

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

client := point.NewClient(cfg)
canceledResponse, err := client.Cancel(context.Background(), "{{DEVICE_ID}}", "{{PAYMENT_INTENT_ID}}")
if err != nil {
fmt.Println(err)
return
}

fmt.Println(canceledResponse)
}
42 changes: 42 additions & 0 deletions examples/apis/point/create/main.go
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/point"
)

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

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

req := point.CreateRequest{
Amount: 1500,
Description: "your payment intent description",
AdditionalInfo: point.AdditionalInfo{
PrintOnTerminal: false,
ExternalReference: "4561ads-das4das4-das4754-das456",
},
Payment: point.PaymentRequest{
Installments: 1,
Type: "credit_card",
InstallmentsCost: "seller",
},
}

client := point.NewClient(cfg)
paymentIntent, err := client.Create(context.Background(), "{{DEVICE_ID}}", req)
if err != nil {
fmt.Println(err)
return
}

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

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

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

client := point.NewClient(cfg)
paymentIntent, err := client.Get(context.Background(), "{{PAYMENT_INTENT_ID}}")
if err != nil {
fmt.Println(err)
return
}

fmt.Println(paymentIntent)
}
28 changes: 28 additions & 0 deletions examples/apis/point/listdevices/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/point"
)

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

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

client := point.NewClient(cfg)
devices, err := client.ListDevices(context.Background())
if err != nil {
fmt.Println(err)
return
}

fmt.Println(devices)
}
32 changes: 32 additions & 0 deletions examples/apis/point/updatedeviceoperatingmode/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/point"
)

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

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

req := point.UpdateDeviceOperatingModeRequest{
OperatingMode: "PDV", // PDV or STANDALONE
}

client := point.NewClient(cfg)
opMode, err := client.UpdateDeviceOperatingMode(context.Background(), "{{DEVICE_ID}}", req)
if err != nil {
fmt.Println(err)
return
}

fmt.Println(opMode)
}
7 changes: 4 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ module github.com/mercadopago/sdk-go

go 1.21.0

require github.com/google/uuid v1.5.0
require (
github.com/google/uuid v1.5.0
github.com/stretchr/testify v1.8.4
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/objx v0.5.0 // indirect
github.com/stretchr/testify v1.8.4 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
9 changes: 1 addition & 8 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU=
github.com/google/uuid v1.5.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
6 changes: 6 additions & 0 deletions pkg/internal/baseclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ func Put[T any](ctx context.Context, cfg *config.Config, url string, body any, o
return make[T](ctx, cfg, url, http.MethodPut, body, opts...)
}

// Patch makes requests with the PATCH method
// Will return the struct specified in Generics
func Patch[T any](ctx context.Context, cfg *config.Config, url string, body any, opts ...Option) (T, error) {
return make[T](ctx, cfg, url, http.MethodPatch, body, opts...)
}

// Delete makes requests with the DELETE method
// Will return the struct specified in Generics
func Delete[T any](ctx context.Context, cfg *config.Config, url string, body any, opts ...Option) (T, error) {
Expand Down
118 changes: 118 additions & 0 deletions pkg/point/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package point

import (
"context"

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

const (
urlBase = "https://api.mercadopago.com/point"
urlDevices = urlBase + "/integration-api/devices/"
urlPaymentIntent = urlDevices + ":device_id/payment-intents"
urlPaymentIntentGet = urlBase + "/integration-api/payment-intents/:payment_intent_id"
urlPaymentIntentCancel = urlDevices + ":device_id/payment-intents/:payment_intent_id"
urlDevicesWithID = urlDevices + ":device_id"
)

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

// Client contains the methods to interact with the Point API.
type Client interface {

// Create a point payment intent.
// It is a post request to the endpoint: https://api.mercadopago.com/point/integration-api/devices/{device_id}/payment-intents
// Reference: https://www.mercadopago.com/developers/en/reference/integrations_api_paymentintent_mlb/_point_integration-api_devices_deviceid_payment-intents/post
Create(ctx context.Context, deviceID string, request CreateRequest) (*CreateResponse, error)

// Get a point payment intent.
// It is a get request to the endpoint: https://api.mercadopago.com/point/integration-api/payment-intents/{payment_intent_id}
// Reference: https://www.mercadopago.com/developers/en/reference/integrations_api/_point_integration-api_payment-intents_paymentintentid/get
Get(ctx context.Context, paymentIntentID string) (*GetResponse, error)

// Cancel a point payment intent.
// It is a cancel request to the endpoint: https://api.mercadopago.com/point/integration-api/devices/{device_id}/payment-intents/{payment_intent_id}
// Reference: https://www.mercadopago.com/developers/en/reference/integrations_api/_point_integration-api_devices_deviceid_payment-intents_paymentintentid/delete
Cancel(ctx context.Context, deviceID string, paymentIntentID string) (*CancelResponse, error)

// ListDevices retrieve devices.
// It is a get request to the endpoint: https://api.mercadopago.com/point/integration-api/devices
// Reference: https://www.mercadopago.com/developers/en/reference/integrations_api/_point_integration-api_devices/get
ListDevices(ctx context.Context) (*DevicesResponse, error)

// UpdateDeviceOperatingMode update operating mode from device.
// It is a patch request to the endpoint: https://api.mercadopago.com/point/integration-api/devices/{device-id}
// Reference: https://www.mercadopago.com/developers/en/reference/integrations_api/_point_integration-api_devices_device-id/patch
UpdateDeviceOperatingMode(ctx context.Context, deviceID string, request UpdateDeviceOperatingModeRequest) (*OperationModeResponse, error)
}

// NewClient returns a new Point Client.
func NewClient(c *config.Config) Client {
return &client{cfg: c}
}

func (c *client) Create(ctx context.Context, deviceID string, request CreateRequest) (*CreateResponse, error) {
params := map[string]string{
"device_id": deviceID,
}

res, err := baseclient.Post[*CreateResponse](ctx, c.cfg, urlPaymentIntent, request, baseclient.WithPathParams(params))
if err != nil {
return nil, err
}

return res, nil
}

func (c *client) Get(ctx context.Context, paymentIntentID string) (*GetResponse, error) {
params := map[string]string{
"payment_intent_id": paymentIntentID,
}

res, err := baseclient.Get[*GetResponse](ctx, c.cfg, urlPaymentIntentGet, baseclient.WithPathParams(params))
if err != nil {
return nil, err
}

return res, nil
}

func (c *client) Cancel(ctx context.Context, deviceID string, paymentIntentID string) (*CancelResponse, error) {
params := map[string]string{
"device_id": deviceID,
"payment_intent_id": paymentIntentID,
}

res, err := baseclient.Delete[*CancelResponse](ctx, c.cfg, urlPaymentIntentCancel, nil, baseclient.WithPathParams(params))
if err != nil {
return nil, err
}

return res, nil
}

func (c *client) ListDevices(ctx context.Context) (*DevicesResponse, error) {
res, err := baseclient.Get[*DevicesResponse](ctx, c.cfg, urlDevices)
if err != nil {
return nil, err
}

return res, nil
}

func (c *client) UpdateDeviceOperatingMode(ctx context.Context, deviceID string, request UpdateDeviceOperatingModeRequest) (*OperationModeResponse, error) {
params := map[string]string{
"device_id": deviceID,
}

res, err := baseclient.Patch[*OperationModeResponse](ctx, c.cfg, urlDevicesWithID, request, baseclient.WithPathParams(params))
if err != nil {
return nil, err
}

return res, nil
}
Loading
Loading