Skip to content

guilhermegazzinelli/conexa-ruby

Repository files navigation

Conexa Ruby

Gem Version CI

Ruby client for the Conexa API - Billing and subscription management platform.

Versão em Português

Installation

Add to your Gemfile:

gem 'conexa'

Or install directly:

gem install conexa

Configuration

Conexa.configure do |config|
  config.subdomain = 'YOUR_SUBDOMAIN'  # your-company.conexa.app
  config.api_token = 'YOUR_API_TOKEN'  # Application Token from Conexa
end

Authentication Methods

  1. Application Token (recommended): Created in Conexa at Config > Integrações > API / Token
  2. Username/Password: Use the /auth endpoint to get a JWT token

Quick Start

require 'conexa'

Conexa.configure do |config|
  config.subdomain = 'mycompany'
  config.api_token = ENV['CONEXA_API_TOKEN']
end

# List customers
customers = Conexa::Customer.list
customers.data.each do |customer|
  puts "#{customer.customer_id}: #{customer.name}"
end

# Get a specific customer
customer = Conexa::Customer.retrieve(127)
puts customer.name
puts customer.address.city

Convention

This gem follows Ruby conventions:

  • Parameters: Use snake_case when calling methods - the gem automatically converts to camelCase for the API
  • Responses: API responses are converted from camelCase to snake_case
  • Backwards compatibility: camelCase methods are aliased (e.g., customer.customer_id and customer.customerId both work)

Resources

Customer

# Create a customer (Legal Person - PJ)
customer = Conexa::Customer.create(
  company_id: 3,
  name: 'Empresa ABC Ltda',
  trade_name: 'ABC',
  cell_number: '11999998888',
  has_login_access: false,
  legal_person: {
    cnpj: '99.557.155/0001-90',
    foundation_date: '2020-06-12'
  },
  address: {
    zip_code: '13058-111',
    state: 'SP',
    city: 'Campinas',
    street: 'Rua Principal',
    number: '100',
    neighborhood: 'Centro'
  },
  phones: ['(11) 3333-4444'],
  emails_message: ['contato@empresa.com'],
  emails_financial_messages: ['financeiro@empresa.com']
)
puts customer.id  # => 114

# Create a customer (Natural Person - PF)
customer = Conexa::Customer.create(
  company_id: 3,
  name: 'João Silva',
  natural_person: {
    cpf: '516.079.209-05',
    birth_date: '1990-05-15',
    profession: 'Developer'
  },
  has_login_access: true,
  login: 'joao.silva',
  password: 'SecurePass123!'
)

# Retrieve customer
customer = Conexa::Customer.retrieve(127)
customer.name           # => "Empresa ABC Ltda"
customer.company_id     # => 3
customer.is_active      # => true
customer.address.city   # => "Campinas"
customer.legal_person['cnpj']  # => "99.557.155/0001-90"

# Update customer
Conexa::Customer.update(127, name: 'New Name', cell_number: '11888887777')

# List customers with filters
customers = Conexa::Customer.list(
  company_id: [3],
  is_active: true,
  page: 1,
  size: 20
)

Contract

# Create a contract
contract = Conexa::Contract.create(
  customer_id: 127,
  plan_id: 5,
  start_date: '2024-01-01',
  payment_day: 10,
  invoicing_method_id: 1
)

# Create contract with custom items
contract = Conexa::Contract.create_with_products(
  customer_id: 127,
  start_date: '2024-01-01',
  payment_day: 10,
  items: [
    { product_id: 101, quantity: 1, amount: 299.90 },
    { product_id: 102, quantity: 2, amount: 49.90 }
  ]
)

# Retrieve contract
contract = Conexa::Contract.retrieve(456)

# Cancel contract
Conexa::Contract.cancel(456, cancel_date: '2024-12-31')

Sale (One-time)

# Create a one-time sale
sale = Conexa::Sale.create(
  customer_id: 450,
  requester_id: 458,
  product_id: 2521,
  quantity: 1,
  amount: 80.99,
  reference_date: '2024-09-24T17:24:00-03:00',
  notes: 'WhatsApp order'
)
puts sale.id  # => 188481

# Retrieve sale
sale = Conexa::Sale.retrieve(188510)
sale.status         # => "notBilled"
sale.amount         # => 80.99
sale.discount_value # => 69.21

# List sales
sales = Conexa::Sale.list(
  customer_id: [450, 216],
  status: 'notBilled',
  date_from: '2024-01-01',
  date_to: '2024-12-31',
  page: 1,
  size: 20
)

# Update sale
Conexa::Sale.update(188510, quantity: 2, amount: 150.00)

# Delete sale (only if not billed)
Conexa::Sale.delete(188510)

Recurring Sale

# Create recurring sale
recurring = Conexa::RecurringSale.create(
  customer_id: 127,
  product_id: 101,
  quantity: 1,
  start_date: '2024-01-01'
)

# List recurring sales for a contract
Conexa::RecurringSale.list(contract_id: 456)

Charge

# Retrieve charge
charge = Conexa::Charge.retrieve(789)
charge.status     # => "paid"
charge.amount     # => 299.90
charge.due_date   # => "2024-02-10"

# List charges
charges = Conexa::Charge.list(
  customer_id: [127],
  status: 'pending',
  due_date_from: '2024-01-01',
  due_date_to: '2024-12-31'
)

# Cancel charge
Conexa::Charge.cancel(789)

# Send charge by email
Conexa::Charge.send_email(789)

Bill (Invoice)

# Retrieve bill
bill = Conexa::Bill.retrieve(101)

# List bills
bills = Conexa::Bill.list(
  customer_id: [127],
  page: 1,
  size: 50
)

Plan

# List plans
plans = Conexa::Plan.list(company_id: [3])

# Retrieve plan
plan = Conexa::Plan.retrieve(5)
plan.name   # => "Plano Básico"
plan.price  # => 99.90

Product

# List products
products = Conexa::Product.list(company_id: [3])

# Retrieve product
product = Conexa::Product.retrieve(101)

Credit Card

# Add credit card to customer
card = Conexa::CreditCard.create(
  customer_id: 127,
  card_number: '4111111111111111',
  cardholder_name: 'JOAO SILVA',
  expiration_month: '12',
  expiration_year: '2025',
  cvv: '123'
)

# List customer's cards
cards = Conexa::CreditCard.list(customer_id: 127)

# Delete card
Conexa::CreditCard.delete(card_id)

Company (Unit)

# List companies/units
companies = Conexa::Company.list

# Retrieve company
company = Conexa::Company.retrieve(3)
company.name      # => "Matriz"
company.document  # => "12.345.678/0001-90"

Pagination

All list endpoints return paginated results:

result = Conexa::Customer.list(page: 1, size: 20)

result.data                       # Array of customers
result.pagination.current_page    # => 1
result.pagination.total_pages     # => 10
result.pagination.total_items     # => 195
result.pagination.item_per_page   # => 20

# Iterate through pages
loop do
  result.data.each { |customer| process(customer) }
  break if result.pagination.current_page >= result.pagination.total_pages
  result = Conexa::Customer.list(page: result.pagination.current_page + 1)
end

Error Handling

begin
  customer = Conexa::Customer.create(name: '')
rescue Conexa::ValidationError => e
  # Field validation errors (400)
  e.errors.each do |error|
    puts "#{error['field']}: #{error['messages'].join(', ')}"
  end
rescue Conexa::AuthenticationError => e
  # Authentication required (401)
  puts e.message
rescue Conexa::AuthorizationError => e
  # Not authorized (403)
  puts e.message
rescue Conexa::NotFoundError => e
  # Resource not found (404)
  puts e.message
rescue Conexa::UnprocessableError => e
  # Business logic error (422)
  e.errors.each do |error|
    puts "#{error['code']}: #{error['message']}"
  end
rescue Conexa::RateLimitError => e
  # Too many requests (429)
  puts "Rate limit exceeded. Retry after #{e.retry_after} seconds"
rescue Conexa::ApiError => e
  # Generic API error
  puts "Error #{e.status}: #{e.message}"
end

Rate Limiting

The Conexa API has a limit of 100 requests per minute. Response headers include:

  • X-Rate-Limit-Limit: Maximum requests in 60s
  • X-Rate-Limit-Remaining: Remaining requests in 60s
  • X-Rate-Limit-Reset: Seconds until reset

Documentation

Development

# Install dependencies
bundle install

# Run tests
bundle exec rspec

# Run linter
bundle exec rubocop

Contributing

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -am 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Create a Pull Request

License

MIT License. See LICENSE for details.

Changelog

See CHANGELOG.md for release history.

About

Biblioteca para interação com a api da Conexa

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

 
 
 

Contributors