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 1 commit
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)
can, err := client.Cancel(context.Background(), "{{DEVICE_ID}}", "{{PAYMENT_INTENT_ID}}")
if err != nil {
fmt.Println(err)
return
}

fmt.Println(can)
}
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.Request{
Amount: 1500,
Description: "your payment intent description",
AdditionalInfo: point.AdditionalInfo{
PrintOnTerminal: false,
ExternalReference: "4561ads-das4das4-das4754-das456",
},
Payment: point.Payment{
Installments: 1,
Type: "credit_card",
InstallmentsCost: "seller",
},
}

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

fmt.Println(ppi)
}
28 changes: 28 additions & 0 deletions examples/apis/point/getdevices/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)
dev, err := client.GetDevices(context.Background())
if err != nil {
fmt.Println(err)
return
}

fmt.Println(dev)
}
28 changes: 28 additions & 0 deletions examples/apis/point/getintentstatus/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)
ppi, err := client.GetIntentStatus(context.Background(), "{{PAYMENT_INTENT_ID}}")
if err != nil {
fmt.Println(err)
return
}

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

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

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

client := point.NewClient(cfg)
intents, err := client.ListIntents(context.Background(), "2024-01-01", "2024-01-02")
if err != nil {
fmt.Println(err)
return
}

for _, i := range intents.Intent {
fmt.Println(i)
}
}
28 changes: 28 additions & 0 deletions examples/apis/point/search/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)
ppi, err := client.Search(context.Background(), "{{PAYMENT_INTENT_ID}}")
if err != nil {
fmt.Println(err)
return
}

fmt.Println(ppi)
}
32 changes: 32 additions & 0 deletions examples/apis/point/updatedeviceopmode/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)
om, err := client.UpdateDeviceOperationMode(context.Background(), "{{DEVICE_ID}}", req)
if err != nil {
fmt.Println(err)
return
}

fmt.Println(om)
}
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"
paymentIntentUrl = urlBase + "/point/integration-api/devices/:device_id/payment-intents"
paymentIntentSearchUrl = urlBase + "/point/integration-api/payment-intents/:payment_intent_id"
paymentIntentCancelUrl = urlBase + "/point/integration-api/devices/:device_id/payment-intents/:payment_intent_id"
devicesUrl = urlBase + "/point/integration-api/devices"
deviceWithIDUrl = urlBase + "/point/integration-api/devices/: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.br/developers/pt/reference/integrations_api_paymentintent_mlb/_point_integration-api_devices_deviceid_payment-intents/post
Create(ctx context.Context, deviceID string, request Request) (*CreateResponse, error)

// Search a point payment intent.
// It is a search request to the endpoint: https://api.mercadopago.com/point/integration-api/payment-intents/{payment_intent_id}
// Reference: https://www.mercadopago.com.br/developers/pt/reference/integrations_api/_point_integration-api_payment-intents_paymentintentid/get
Search(ctx context.Context, paymentIntentID string) (*SearchResponse, 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.br/developers/pt/reference/integrations_api/_point_integration-api_devices_deviceid_payment-intents_paymentintentid/delete
Cancel(ctx context.Context, deviceID string, paymentIntentID string) (*CancelResponse, error)

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

// UpdateDeviceOperationMode getintentstatus device.
// It is a patch request to the endpoint: https://api.mercadopago.com/point/integration-api/devices/{device-id}
// Reference: https://www.mercadopago.com.br/developers/pt/reference/integrations_api/_point_integration-api_devices_device-id/patch
UpdateDeviceOperationMode(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 Request) (*CreateResponse, error) {
params := map[string]string{
"device_id": deviceID,
}

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

return res, nil
}

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

res, err := baseclient.Get[*SearchResponse](ctx, c.cfg, paymentIntentSearchUrl, 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, paymentIntentCancelUrl, nil, baseclient.WithPathParams(params))
if err != nil {
return nil, err
}

return res, nil
}

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

return res, nil
}

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

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

return res, nil
}
Loading
Loading