Skip to content

Commit

Permalink
Add client card
Browse files Browse the repository at this point in the history
  • Loading branch information
eltinMeli committed Feb 5, 2024
1 parent da89d1a commit 198b679
Show file tree
Hide file tree
Showing 5 changed files with 224 additions and 1 deletion.
166 changes: 166 additions & 0 deletions pkg/customer/customercard/card.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
package customercard

import (
"context"
"encoding/json"
"fmt"
"net/http"
"strconv"
"strings"

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

const (
baseURL = "https://api.mercadopago.com/v1/customers/{customer_id}"
cardsURL = baseURL + "/cards"
cardsByIDURL = baseURL + cardsURL + "/{card_id}"
)

// Client contains the methods to interact with the Payment Methods API.
type Client interface {
// Create a new customer card.
// It is a post request to the endpoint: https://api.mercadopago.com/v1/customer/{customer_id}/cards
// Reference: https://www.mercadopago.com/developers/en/reference/cards/_customers_customer_id_cards/post
Create(ctx context.Context, customerID string, request Request) (*Response, error)

// Get a customer card by ID.
// It is a get request to the endpoint: https://api.mercadopago.com/v1/customer/{customer_id}/cards/{card_id}
// Reference: https://www.mercadopago.com/developers/en/reference/cards/_customers_customer_id_cards_id/get
Get(ctx context.Context, customerID, cardID string) (*Response, error)

// Update a customer card by ID.
// It is a put request to the endpoint: https://api.mercadopago.com/v1/customer/{customer_id}/cards/{card_id}
// Reference: https://www.mercadopago.com/developers/en/reference/cards/_customers_customer_id_cards_id/put
Update(ctx context.Context, customerID, cardID string) (*Response, error)

// Delete deletes a customer card by ID.
// It is a delete request to the endpoint: https://api.mercadopago.com/v1/customer/{customer_id}/cards/{card_id}
// Reference: https://www.mercadopago.com/developers/en/reference/cards/_customers_customer_id_cards_id/delete
Delete(ctx context.Context, customerID, cardID string) (*Response, error)

// List all customers.
// It is a get request to the endpoint: https://api.mercadopago.com/v1/customer/{customer_id}/cards
// Reference: https://www.mercadopago.com/developers/en/reference/cards/_customers_customer_id_cards/get
List(ctx context.Context, customerID string) ([]Response, error)
}

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

// NewClient returns a new Payment Methods API Client.
func NewClient(c *config.Config) Client {
return &client{
config: c,
}
}

func (c *client) Create(ctx context.Context, customerID string, request Request) (*Response, error) {
body, err := json.Marshal(&request)
if err != nil {
return nil, fmt.Errorf("error marshaling request body: %w", err)
}

req, err := http.NewRequestWithContext(ctx, http.MethodPost, cardsURL, strings.NewReader(string(body)))
if err != nil {
return nil, fmt.Errorf("error creating request: %w", err)
}

res, err := httpclient.Send(ctx, c.config, req)
if err != nil {
return nil, err
}

formatted := &Response{}
if err := json.Unmarshal(res, &formatted); err != nil {
return nil, fmt.Errorf("error unmarshaling response: %w", err)
}

return formatted, nil
}

func (c *client) Get(ctx context.Context, customerID, cardID string) (*Response, error) {
url := strings.Replace(cardsByIDURL, "{customer_id}", customerID, 1)
url = strings.Replace(cardsByIDURL, "{card_id}", cardID, 1)

req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return nil, fmt.Errorf("error creating request: %w", err)
}

res, err := httpclient.Send(ctx, c.config, req)
if err != nil {
return nil, err
}

formatted := &Response{}
if err := json.Unmarshal(res, &formatted); err != nil {
return nil, fmt.Errorf("error unmarshaling response: %w", err)
}

return formatted, nil
}

func (c *client) Update(ctx context.Context, customerID, cardID string) (*Response, error) {
conv := strconv.Itoa(int(id))

req, err := http.NewRequestWithContext(ctx, http.MethodGet, strings.Replace(getURL, "{id}", conv, 1), nil)
if err != nil {
return nil, fmt.Errorf("error creating request: %w", err)
}

res, err := httpclient.Send(ctx, c.config, req)
if err != nil {
return nil, err
}

formatted := &Response{}
if err := json.Unmarshal(res, &formatted); err != nil {
return nil, fmt.Errorf("error unmarshaling response: %w", err)
}

return formatted, nil
}

func (c *client) Delete(ctx context.Context, customerID, cardID string) (*Response, error) {
conv := strconv.Itoa(int(id))

req, err := http.NewRequestWithContext(ctx, http.MethodGet, strings.Replace(getURL, "{id}", conv, 1), nil)
if err != nil {
return nil, fmt.Errorf("error creating request: %w", err)
}

res, err := httpclient.Send(ctx, c.config, req)
if err != nil {
return nil, err
}

formatted := &Response{}
if err := json.Unmarshal(res, &formatted); err != nil {
return nil, fmt.Errorf("error unmarshaling response: %w", err)
}

return formatted, nil
}

func (c *client) List(ctx context.Context, customerID string) ([]Response, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return nil, fmt.Errorf("error creating request: %w", err)
}

res, err := httpclient.Send(ctx, c.config, req)
if err != nil {
return nil, err
}

var formatted []Response
if err := json.Unmarshal(res, &formatted); err != nil {
return nil, err
}

return formatted, nil
}
5 changes: 5 additions & 0 deletions pkg/customer/customercard/request.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package customercard

type Request struct {
Token string `json:"token"`
}
52 changes: 52 additions & 0 deletions pkg/customer/customercard/response.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package customercard

type Response struct {
ID string `json:"id"`
Name string `json:"name"`
PaymentTypeID string `json:"payment_type_id"`
Status string `json:"status"`
SecureThumbnail string `json:"secure_thumbnail"`
Thumbnail string `json:"thumbnail"`
DeferredCapture string `json:"deferred_capture"`
AdditionalInfoNeeded []string `json:"additional_info_needed"`
ProcessingModes []string `json:"processing_modes"`
AccreditationTime int64 `json:"accreditation_time"`
MinAllowedAmount float64 `json:"min_allowed_amount"`
MaxAllowedAmount float64 `json:"max_allowed_amount"`

Settings []SettingsResponse `json:"settings"`
FinancialInstitutions []FinancialInstitutionResponse `json:"financial_institutions"`
}

// SettingsResponse represents payment method settings.
type SettingsResponse struct {
Bin *SettingsBinResponse `json:"bin"`
CardNumber *SettingsCardNumberResponse `json:"card_number"`
SecurityCode *SettingsSecurityCodeResponse `json:"security_code"`
}

// SettingsBinResponse represents BIN (Bank Identification Number) settings.
type SettingsBinResponse struct {
Pattern string `json:"pattern"`
ExclusionPattern string `json:"exclusion_pattern"`
InstallmentsPattern string `json:"installments_pattern"`
}

// SettingsCardNumberResponse represents customer number settings.
type SettingsCardNumberResponse struct {
Length int `json:"length"`
Validation string `json:"validation"`
}

// SettingsSecurityCodeResponse represents security code settings.
type SettingsSecurityCodeResponse struct {
Mode string `json:"mode"`
Length int `json:"length"`
CardLocation string `json:"card_location"`
}

// FinancialInstitutionResponse represents financial institution settings.
type FinancialInstitutionResponse struct {
ID string `json:"id"`
Description string `json:"description"`
}
File renamed without changes.
2 changes: 1 addition & 1 deletion pkg/paymentmethod/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type SettingsBinResponse struct {
InstallmentsPattern string `json:"installments_pattern"`
}

// SettingsCardNumberResponse represents card number settings.
// SettingsCardNumberResponse represents customer number settings.
type SettingsCardNumberResponse struct {
Length int `json:"length"`
Validation string `json:"validation"`
Expand Down

0 comments on commit 198b679

Please sign in to comment.