Sha256: 70b65f91d249d02d1dee718613a1ce85b1d72bcf8fe69445994e9985b4124c6e

Contents?: true

Size: 1.77 KB

Versions: 2

Compression:

Stored size: 1.77 KB

Contents

module MpApi
  class Client < Ac::Base
    BASE_URL = "https://api.mercadopago.com/v1/"
    MAX_RETRIES = 1
    SAVE_RESPONSES = true

    attr_reader :access_token
    def initialize(access_token = MpApi.configuration.access_token)
      @headers = {
        "Content-Type" => "application/json",
        "x-idempotency-key" => SecureRandom.uuid
      }
      super(access_token)
    end

    def create_payment(body)
      post("/payments", body: body, headers: @headers) { |response| validate_response(response, "id") }
    end

    def get_payment(payment_id)
      get("/payments/#{payment_id}") { |response| validate_response(response, "id") }
    end

    def update_payment(payment_id, body)
      put("/payments/#{payment_id}", body: body, headers: @headers) { |response| validate_response(response, "id") }
    end

    def create_token(card_token_data)
      post("/card_tokens", body: card_token_data, headers: @headers) { |response| validate_response(response, "id") }
    end

    def search_payment_methods(query)
      get("/payment_methods/search", params: query) { |response| validate_response(response, ["results", 0]) }
    end

    def get_payment_methods
      get("/payment_methods") { |response| validate_response(response, "id") }
    end

    def create_customer(body)
      post("/customers", body: body, headers: @headers) { |response| validate_response(response, "id") }
    end

    def create_card(customer_id, body)
      post("/customers/#{customer_id}/cards", body: body, headers: @headers) { |response| validate_response(response, "id") }
    end

    private

    def validate_response(response, required_response_key)
      puts response.json unless response.success?
      ![500, 429].include?(response.code) || response.json.dig(*required_response_key)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
mp_api-1.2.2 lib/mp_api/client.rb
mp_api-1.2.1 lib/mp_api/client.rb