conexa 0.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.editorconfig +30 -0
- data/.rspec +3 -0
- data/.rubocop.yml +13 -0
- data/.solargraph.yml +2 -0
- data/.tool-versions +1 -0
- data/.vscode/launch.json +30 -0
- data/CHANGELOG.md +26 -0
- data/CODE_OF_CONDUCT.md +84 -0
- data/Gemfile +20 -0
- data/Gemfile.lock +191 -0
- data/LICENSE +21 -0
- data/README.md +640 -0
- data/README_pt-BR.md +640 -0
- data/Rakefile +12 -0
- data/bin/console +61 -0
- data/bin/setup +8 -0
- data/lib/conexa/authenticator.rb +112 -0
- data/lib/conexa/configuration.rb +10 -0
- data/lib/conexa/core_ext.rb +9 -0
- data/lib/conexa/errors.rb +69 -0
- data/lib/conexa/generators/install_generator.rb +42 -0
- data/lib/conexa/model.rb +97 -0
- data/lib/conexa/object.rb +139 -0
- data/lib/conexa/order_commom.rb +44 -0
- data/lib/conexa/request.rb +125 -0
- data/lib/conexa/resources/addres.rb +4 -0
- data/lib/conexa/resources/bill.rb +7 -0
- data/lib/conexa/resources/charge.rb +34 -0
- data/lib/conexa/resources/company.rb +5 -0
- data/lib/conexa/resources/contract.rb +21 -0
- data/lib/conexa/resources/credit_card.rb +13 -0
- data/lib/conexa/resources/customer.rb +5 -0
- data/lib/conexa/resources/invoicing_method.rb +13 -0
- data/lib/conexa/resources/legal_person.rb +4 -0
- data/lib/conexa/resources/pagination.rb +4 -0
- data/lib/conexa/resources/person.rb +18 -0
- data/lib/conexa/resources/plan.rb +8 -0
- data/lib/conexa/resources/product.rb +7 -0
- data/lib/conexa/resources/recurring_sale.rb +29 -0
- data/lib/conexa/resources/result.rb +52 -0
- data/lib/conexa/resources/sale.rb +4 -0
- data/lib/conexa/resources/supplier.rb +13 -0
- data/lib/conexa/token_manager.rb +134 -0
- data/lib/conexa/util.rb +94 -0
- data/lib/conexa/version.rb +5 -0
- data/lib/conexa.rb +34 -0
- metadata +204 -0
data/README_pt-BR.md
ADDED
|
@@ -0,0 +1,640 @@
|
|
|
1
|
+
# Conexa Ruby Gem
|
|
2
|
+
|
|
3
|
+
Biblioteca Ruby para integração com a API Conexa
|
|
4
|
+
|
|
5
|
+
**[English version](README.md)**
|
|
6
|
+
|
|
7
|
+
## Documentação
|
|
8
|
+
|
|
9
|
+
- [Site Conexa](https://conexa.app/)
|
|
10
|
+
- [Documentação da API](https://conexa.app/api-docs)
|
|
11
|
+
- [Coleção Postman da API Conexa](https://web.postman.co/workspace/8e1887b1-bef9-4e36-848f-2b6774a81022/collection/33452984-58f4d7ab-d280-4aac-8578-8366988ff7af)
|
|
12
|
+
|
|
13
|
+
## Primeiros Passos
|
|
14
|
+
|
|
15
|
+
### Instalação
|
|
16
|
+
|
|
17
|
+
```shell
|
|
18
|
+
gem install conexa-ruby
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
Ou adicione a seguinte linha ao seu `Gemfile`:
|
|
22
|
+
|
|
23
|
+
```ruby
|
|
24
|
+
gem 'conexa-ruby'
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
Depois execute `bundle install` no terminal.
|
|
28
|
+
|
|
29
|
+
### Configurar sua Chave de API
|
|
30
|
+
|
|
31
|
+
Você pode definir suas credenciais de API em Ruby:
|
|
32
|
+
|
|
33
|
+
```ruby
|
|
34
|
+
Conexa.configure do |config|
|
|
35
|
+
config.api_token = ENV['API_TOKEN']
|
|
36
|
+
config.api_host = ENV['API_HOST']
|
|
37
|
+
end
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
Certifique-se de que as variáveis de ambiente `API_TOKEN` e `API_HOST` estejam configuradas com seu token e URL da API Conexa.
|
|
41
|
+
|
|
42
|
+
## Uso
|
|
43
|
+
|
|
44
|
+
Após a configuração, você pode começar a usar a gem para interagir com os recursos da API Conexa.
|
|
45
|
+
|
|
46
|
+
### Recursos Disponíveis
|
|
47
|
+
|
|
48
|
+
- `Bill` (Faturas)
|
|
49
|
+
- `Charge` (Cobranças)
|
|
50
|
+
- `Company` (Empresas)
|
|
51
|
+
- `Contract` (Contratos)
|
|
52
|
+
- `CreditCard` (Cartão de Crédito)
|
|
53
|
+
- `Customer` (Clientes)
|
|
54
|
+
- `InvoicingMethod` (Meios de Faturamento)
|
|
55
|
+
- `LegalPerson` (Pessoa Jurídica)
|
|
56
|
+
- `Person` (Pessoa)
|
|
57
|
+
- `Plan` (Planos)
|
|
58
|
+
- `Product` (Produtos)
|
|
59
|
+
- `RecurringSale` (Vendas Recorrentes)
|
|
60
|
+
- `Sale` (Vendas)
|
|
61
|
+
- `Supplier` (Fornecedores)
|
|
62
|
+
|
|
63
|
+
### Padrões de Métodos
|
|
64
|
+
|
|
65
|
+
Cada recurso segue um conjunto consistente de padrões de métodos:
|
|
66
|
+
|
|
67
|
+
- `Conexa::<NomeRecurso>.new(params).create` - Criar um novo recurso.
|
|
68
|
+
- `Conexa::<NomeRecurso>.all(params)` - Buscar todas as entidades com os parâmetros.
|
|
69
|
+
- `Conexa::<NomeRecurso>.find(id)` - Recuperar um recurso por ID.
|
|
70
|
+
- `Conexa::<NomeRecurso>.find(hash_filtros, page, size)` - Recuperar recursos com filtros e paginação.
|
|
71
|
+
- `Conexa::<NomeRecurso>.destroy(id)` - Excluir um recurso por ID.
|
|
72
|
+
- `Conexa::<NomeRecurso>.find(id).destroy` - Buscar e excluir um recurso.
|
|
73
|
+
|
|
74
|
+
### Paginação
|
|
75
|
+
|
|
76
|
+
A API utiliza os parâmetros `page` e `size` para paginação com os seguintes valores padrão:
|
|
77
|
+
|
|
78
|
+
- **page**: Número da página atual (padrão: `1`)
|
|
79
|
+
- **size**: Quantidade de itens por página (padrão: `100`)
|
|
80
|
+
|
|
81
|
+
#### Chamando com Paginação
|
|
82
|
+
|
|
83
|
+
```ruby
|
|
84
|
+
# Paginação padrão (página 1, 100 itens)
|
|
85
|
+
Conexa::Customer.all
|
|
86
|
+
|
|
87
|
+
# Argumentos posicionais (página, tamanho)
|
|
88
|
+
Conexa::Customer.all(2, 50) # página 2, 50 itens por página
|
|
89
|
+
|
|
90
|
+
# Parâmetros nomeados
|
|
91
|
+
Conexa::Customer.all(page: 3, size: 25)
|
|
92
|
+
|
|
93
|
+
# Usando o alias `where`
|
|
94
|
+
Conexa::Customer.where(page: 2, size: 10)
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
### Objeto Result
|
|
98
|
+
|
|
99
|
+
Ao listar recursos, a API retorna um objeto `Conexa::Result` contendo:
|
|
100
|
+
|
|
101
|
+
- **data**: Array com os objetos do recurso
|
|
102
|
+
- **pagination**: Objeto `Conexa::Pagination` com metadados da paginação
|
|
103
|
+
|
|
104
|
+
```ruby
|
|
105
|
+
result = Conexa::Customer.all
|
|
106
|
+
|
|
107
|
+
# Acessar o array de dados
|
|
108
|
+
result.data # => Array de objetos Conexa::Customer
|
|
109
|
+
|
|
110
|
+
# Acessar informações de paginação
|
|
111
|
+
result.pagination # => Objeto Conexa::Pagination
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
#### Delegação de Métodos para Data
|
|
115
|
+
|
|
116
|
+
O objeto `Result` delega automaticamente métodos de Array para o atributo `data`. Isso significa que você pode chamar métodos como `first`, `second`, `last`, `length`, `count`, `each`, `map`, etc. diretamente no resultado:
|
|
117
|
+
|
|
118
|
+
```ruby
|
|
119
|
+
result = Conexa::Customer.all
|
|
120
|
+
|
|
121
|
+
# Estes métodos são delegados para result.data
|
|
122
|
+
result.first # => Primeiro Conexa::Customer (equivale a result.data.first)
|
|
123
|
+
result.second # => Segundo Conexa::Customer
|
|
124
|
+
result.last # => Último Conexa::Customer
|
|
125
|
+
result.length # => Quantidade de itens na página atual
|
|
126
|
+
result.count # => Quantidade de itens na página atual
|
|
127
|
+
result.empty? # => true se não houver resultados
|
|
128
|
+
|
|
129
|
+
# Iteração direta
|
|
130
|
+
result.each { |customer| puts customer.name }
|
|
131
|
+
|
|
132
|
+
# Transformações
|
|
133
|
+
result.map(&:name) # => Array com os nomes dos clientes
|
|
134
|
+
result.select { |c| c.active } # => Filtra clientes ativos
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
### Filtragem
|
|
138
|
+
|
|
139
|
+
Você pode filtrar resultados passando parâmetros junto com a paginação. Os filtros disponíveis variam por recurso.
|
|
140
|
+
|
|
141
|
+
#### Padrões Comuns de Filtro
|
|
142
|
+
|
|
143
|
+
```ruby
|
|
144
|
+
# Filtrar por nome
|
|
145
|
+
Conexa::Customer.all(name: "João", page: 1, size: 10)
|
|
146
|
+
|
|
147
|
+
# Filtrar por múltiplos IDs (sintaxe de array)
|
|
148
|
+
Conexa::Customer.all("id[]": [102, 103])
|
|
149
|
+
|
|
150
|
+
# Filtrar por empresa
|
|
151
|
+
Conexa::Customer.all("companyId[]": [3])
|
|
152
|
+
Conexa::Contract.all("companyId[]": [540], page: 2, size: 5)
|
|
153
|
+
|
|
154
|
+
# Múltiplos filtros combinados
|
|
155
|
+
Conexa::Company.all(
|
|
156
|
+
"id[]": [3, 4],
|
|
157
|
+
trade_name: "Acme",
|
|
158
|
+
legal_name: "Acme Ltda",
|
|
159
|
+
cnpj: "17.992.846/0001-58",
|
|
160
|
+
city: "São Paulo",
|
|
161
|
+
active: 1,
|
|
162
|
+
page: 1,
|
|
163
|
+
size: 5
|
|
164
|
+
)
|
|
165
|
+
|
|
166
|
+
# Filtrar faturas por status
|
|
167
|
+
Conexa::Bill.all(status: "pending", page: 1, size: 20)
|
|
168
|
+
|
|
169
|
+
# Filtrar vendas
|
|
170
|
+
Conexa::Sale.all(page: 2, size: 6)
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
#### Filtros Específicos por Recurso
|
|
174
|
+
|
|
175
|
+
| Recurso | Filtros Comuns |
|
|
176
|
+
|---------|---------------|
|
|
177
|
+
| Customer | `name`, `id[]`, `companyId[]` |
|
|
178
|
+
| Company | `id[]`, `tradeName`, `legalName`, `cnpj`, `city`, `active` |
|
|
179
|
+
| Contract | `companyId[]`, `active` |
|
|
180
|
+
| Bill | `status`, `companyId[]` |
|
|
181
|
+
| Plan | `id[]` |
|
|
182
|
+
| Sale | `date` |
|
|
183
|
+
| InvoicingMethod | `id[]`, `companyId[]`, `isActive`, `type` |
|
|
184
|
+
|
|
185
|
+
### Operações Específicas de Recursos (Sub-processos)
|
|
186
|
+
|
|
187
|
+
Alguns recursos possuem operações adicionais além do CRUD básico:
|
|
188
|
+
|
|
189
|
+
#### Operações de Cobrança (Charge)
|
|
190
|
+
|
|
191
|
+
```ruby
|
|
192
|
+
# Quitar uma cobrança
|
|
193
|
+
cobranca = Conexa::Charge.find(charge_id)
|
|
194
|
+
cobranca.settle(parametros_pagamento)
|
|
195
|
+
|
|
196
|
+
# Ou diretamente pelo ID
|
|
197
|
+
Conexa::Charge.settle(charge_id, parametros_pagamento)
|
|
198
|
+
|
|
199
|
+
# Obter QR Code PIX de uma cobrança
|
|
200
|
+
dados_pix = Conexa::Charge.pix(charge_id)
|
|
201
|
+
# Retorna dados do QR code para pagamento
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
#### Operações de Contrato (Contract)
|
|
205
|
+
|
|
206
|
+
```ruby
|
|
207
|
+
# Encerrar um contrato
|
|
208
|
+
contrato = Conexa::Contract.find(contract_id)
|
|
209
|
+
contrato.end_contract(end_date: "2024-12-31", reason: "Solicitação do cliente")
|
|
210
|
+
|
|
211
|
+
# Ou diretamente pelo ID
|
|
212
|
+
Conexa::Contract.end_contract(contract_id, end_date: "2024-12-31")
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
#### Operações de Venda Recorrente (RecurringSale)
|
|
216
|
+
|
|
217
|
+
```ruby
|
|
218
|
+
# Encerrar uma venda recorrente
|
|
219
|
+
venda_recorrente = Conexa::RecurringSale.find(recurring_sale_id)
|
|
220
|
+
venda_recorrente.end_recurring_sale(end_date: "2024-12-31")
|
|
221
|
+
|
|
222
|
+
# Ou diretamente pelo ID
|
|
223
|
+
Conexa::RecurringSale.end_recurring_sale(recurring_sale_id, end_date: "2024-12-31")
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
#### Meio de Faturamento (InvoicingMethod)
|
|
227
|
+
|
|
228
|
+
```ruby
|
|
229
|
+
# Listar meios de faturamento com filtros
|
|
230
|
+
Conexa::InvoicingMethod.all("companyId[]": [3], is_active: 1, type: "billet")
|
|
231
|
+
|
|
232
|
+
# Buscar por ID
|
|
233
|
+
Conexa::InvoicingMethod.find(invoicing_method_id)
|
|
234
|
+
```
|
|
235
|
+
|
|
236
|
+
#### Cartão de Crédito (CreditCard)
|
|
237
|
+
|
|
238
|
+
```ruby
|
|
239
|
+
# Cadastrar cartão de crédito para um cliente
|
|
240
|
+
Conexa::CreditCard.new(
|
|
241
|
+
customer_id: customer_id,
|
|
242
|
+
card_number: "4111111111111111",
|
|
243
|
+
card_holder_name: "João Silva",
|
|
244
|
+
expiration_date: "12/25",
|
|
245
|
+
cvv: "123"
|
|
246
|
+
).create
|
|
247
|
+
```
|
|
248
|
+
|
|
249
|
+
#### Fornecedor (Supplier)
|
|
250
|
+
|
|
251
|
+
```ruby
|
|
252
|
+
# Criar fornecedor (pessoa jurídica)
|
|
253
|
+
Conexa::Supplier.new(
|
|
254
|
+
company_id: company_id,
|
|
255
|
+
legal_name: "Fornecedor Ltda",
|
|
256
|
+
trade_name: "Fornecedor",
|
|
257
|
+
cnpj: "12.345.678/0001-90"
|
|
258
|
+
).create
|
|
259
|
+
|
|
260
|
+
# Criar fornecedor (pessoa física)
|
|
261
|
+
Conexa::Supplier.new(
|
|
262
|
+
company_id: company_id,
|
|
263
|
+
name: "João Fornecedor",
|
|
264
|
+
cpf: "123.456.789-00"
|
|
265
|
+
).create
|
|
266
|
+
```
|
|
267
|
+
|
|
268
|
+
### Navegando Entre Páginas
|
|
269
|
+
|
|
270
|
+
```ruby
|
|
271
|
+
# Buscar primeira página
|
|
272
|
+
pagina1 = Conexa::Customer.all(page: 1, size: 10)
|
|
273
|
+
puts "Página 1: #{pagina1.length} clientes"
|
|
274
|
+
|
|
275
|
+
# Buscar próxima página
|
|
276
|
+
pagina2 = Conexa::Customer.all(page: 2, size: 10)
|
|
277
|
+
puts "Página 2: #{pagina2.length} clientes"
|
|
278
|
+
|
|
279
|
+
# Verificar metadados de paginação
|
|
280
|
+
paginacao = pagina1.pagination
|
|
281
|
+
# paginacao contém total de registros, total de páginas, etc.
|
|
282
|
+
```
|
|
283
|
+
|
|
284
|
+
#### Iterando Por Todas as Páginas
|
|
285
|
+
|
|
286
|
+
```ruby
|
|
287
|
+
pagina = 1
|
|
288
|
+
tamanho = 100
|
|
289
|
+
|
|
290
|
+
loop do
|
|
291
|
+
resultado = Conexa::Customer.all(page: pagina, size: tamanho)
|
|
292
|
+
|
|
293
|
+
break if resultado.empty?
|
|
294
|
+
|
|
295
|
+
resultado.each do |cliente|
|
|
296
|
+
# Processar cada cliente
|
|
297
|
+
puts cliente.name
|
|
298
|
+
end
|
|
299
|
+
|
|
300
|
+
pagina += 1
|
|
301
|
+
end
|
|
302
|
+
```
|
|
303
|
+
|
|
304
|
+
### Exemplos
|
|
305
|
+
|
|
306
|
+
#### Clientes (Customers)
|
|
307
|
+
|
|
308
|
+
##### Criando um Cliente
|
|
309
|
+
|
|
310
|
+
```ruby
|
|
311
|
+
cliente = Conexa::Customer.new(
|
|
312
|
+
name: 'João Silva',
|
|
313
|
+
email: 'joao.silva@exemplo.com',
|
|
314
|
+
phone: '11-99999-1234'
|
|
315
|
+
).create
|
|
316
|
+
```
|
|
317
|
+
|
|
318
|
+
##### Recuperando um Cliente
|
|
319
|
+
|
|
320
|
+
```ruby
|
|
321
|
+
cliente = Conexa::Customer.find(customer_id)
|
|
322
|
+
```
|
|
323
|
+
|
|
324
|
+
##### Atualizando um Cliente
|
|
325
|
+
|
|
326
|
+
```ruby
|
|
327
|
+
cliente = Conexa::Customer.find(customer_id)
|
|
328
|
+
cliente.email = 'novo.email@exemplo.com'
|
|
329
|
+
cliente.save
|
|
330
|
+
```
|
|
331
|
+
|
|
332
|
+
##### Excluindo um Cliente
|
|
333
|
+
|
|
334
|
+
```ruby
|
|
335
|
+
Conexa::Customer.destroy(customer_id)
|
|
336
|
+
```
|
|
337
|
+
|
|
338
|
+
Ou:
|
|
339
|
+
|
|
340
|
+
```ruby
|
|
341
|
+
cliente = Conexa::Customer.find(customer_id)
|
|
342
|
+
cliente.destroy
|
|
343
|
+
```
|
|
344
|
+
|
|
345
|
+
##### Listando Clientes
|
|
346
|
+
|
|
347
|
+
```ruby
|
|
348
|
+
clientes = Conexa::Customer.find({ name: 'João Silva' }, 1, 20)
|
|
349
|
+
```
|
|
350
|
+
|
|
351
|
+
#### Faturas (Bills)
|
|
352
|
+
|
|
353
|
+
##### Criando uma Fatura
|
|
354
|
+
|
|
355
|
+
```ruby
|
|
356
|
+
fatura = Conexa::Bill.new(
|
|
357
|
+
customer_id: customer_id,
|
|
358
|
+
amount: 1000, # em centavos
|
|
359
|
+
due_date: '2024-12-31'
|
|
360
|
+
).create
|
|
361
|
+
```
|
|
362
|
+
|
|
363
|
+
##### Recuperando uma Fatura
|
|
364
|
+
|
|
365
|
+
```ruby
|
|
366
|
+
fatura = Conexa::Bill.find(bill_id)
|
|
367
|
+
```
|
|
368
|
+
|
|
369
|
+
##### Excluindo uma Fatura
|
|
370
|
+
|
|
371
|
+
```ruby
|
|
372
|
+
Conexa::Bill.destroy(bill_id)
|
|
373
|
+
```
|
|
374
|
+
|
|
375
|
+
##### Listando Faturas
|
|
376
|
+
|
|
377
|
+
```ruby
|
|
378
|
+
faturas = Conexa::Bill.find({ status: 'pending' }, 1, 20)
|
|
379
|
+
```
|
|
380
|
+
|
|
381
|
+
#### Cobranças (Charges)
|
|
382
|
+
|
|
383
|
+
##### Criando uma Cobrança
|
|
384
|
+
|
|
385
|
+
```ruby
|
|
386
|
+
cobranca = Conexa::Charge.new(
|
|
387
|
+
customer_id: customer_id,
|
|
388
|
+
amount: 1000, # em centavos
|
|
389
|
+
payment_method: 'credit_card',
|
|
390
|
+
card_number: '4111111111111111',
|
|
391
|
+
card_holder_name: 'João Silva',
|
|
392
|
+
card_expiration_date: '12/25',
|
|
393
|
+
card_cvv: '123'
|
|
394
|
+
).create
|
|
395
|
+
```
|
|
396
|
+
|
|
397
|
+
##### Recuperando uma Cobrança
|
|
398
|
+
|
|
399
|
+
```ruby
|
|
400
|
+
cobranca = Conexa::Charge.find(charge_id)
|
|
401
|
+
```
|
|
402
|
+
|
|
403
|
+
##### Excluindo uma Cobrança
|
|
404
|
+
|
|
405
|
+
```ruby
|
|
406
|
+
Conexa::Charge.destroy(charge_id)
|
|
407
|
+
```
|
|
408
|
+
|
|
409
|
+
##### Listando Cobranças
|
|
410
|
+
|
|
411
|
+
```ruby
|
|
412
|
+
cobrancas = Conexa::Charge.find({ status: 'paid' }, 1, 20)
|
|
413
|
+
```
|
|
414
|
+
|
|
415
|
+
#### Planos (Plans)
|
|
416
|
+
|
|
417
|
+
##### Criando um Plano
|
|
418
|
+
|
|
419
|
+
```ruby
|
|
420
|
+
plano = Conexa::Plan.new(
|
|
421
|
+
name: 'Plano Premium',
|
|
422
|
+
amount: 4990, # em centavos
|
|
423
|
+
billing_cycle: 'monthly'
|
|
424
|
+
).create
|
|
425
|
+
```
|
|
426
|
+
|
|
427
|
+
##### Recuperando um Plano
|
|
428
|
+
|
|
429
|
+
```ruby
|
|
430
|
+
plano = Conexa::Plan.find(plan_id)
|
|
431
|
+
```
|
|
432
|
+
|
|
433
|
+
##### Excluindo um Plano
|
|
434
|
+
|
|
435
|
+
```ruby
|
|
436
|
+
Conexa::Plan.destroy(plan_id)
|
|
437
|
+
```
|
|
438
|
+
|
|
439
|
+
##### Listando Planos
|
|
440
|
+
|
|
441
|
+
```ruby
|
|
442
|
+
planos = Conexa::Plan.find({}, 1, 20)
|
|
443
|
+
```
|
|
444
|
+
|
|
445
|
+
#### Contratos (Contracts)
|
|
446
|
+
|
|
447
|
+
##### Criando um Contrato
|
|
448
|
+
|
|
449
|
+
```ruby
|
|
450
|
+
contrato = Conexa::Contract.new(
|
|
451
|
+
customer_id: customer_id,
|
|
452
|
+
plan_id: plan_id,
|
|
453
|
+
start_date: '2024-01-01',
|
|
454
|
+
end_date: '2024-12-31'
|
|
455
|
+
).create
|
|
456
|
+
```
|
|
457
|
+
|
|
458
|
+
##### Recuperando um Contrato
|
|
459
|
+
|
|
460
|
+
```ruby
|
|
461
|
+
contrato = Conexa::Contract.find(contract_id)
|
|
462
|
+
```
|
|
463
|
+
|
|
464
|
+
##### Excluindo um Contrato
|
|
465
|
+
|
|
466
|
+
```ruby
|
|
467
|
+
Conexa::Contract.destroy(contract_id)
|
|
468
|
+
```
|
|
469
|
+
|
|
470
|
+
##### Listando Contratos
|
|
471
|
+
|
|
472
|
+
```ruby
|
|
473
|
+
contratos = Conexa::Contract.find({ active: true }, 1, 20)
|
|
474
|
+
```
|
|
475
|
+
|
|
476
|
+
#### Produtos (Products)
|
|
477
|
+
|
|
478
|
+
##### Criando um Produto
|
|
479
|
+
|
|
480
|
+
```ruby
|
|
481
|
+
produto = Conexa::Product.new(
|
|
482
|
+
name: 'Nome do Produto',
|
|
483
|
+
description: 'Descrição do Produto',
|
|
484
|
+
price: 2990 # em centavos
|
|
485
|
+
).create
|
|
486
|
+
```
|
|
487
|
+
|
|
488
|
+
##### Recuperando um Produto
|
|
489
|
+
|
|
490
|
+
```ruby
|
|
491
|
+
produto = Conexa::Product.find(product_id)
|
|
492
|
+
```
|
|
493
|
+
|
|
494
|
+
##### Excluindo um Produto
|
|
495
|
+
|
|
496
|
+
```ruby
|
|
497
|
+
Conexa::Product.destroy(product_id)
|
|
498
|
+
```
|
|
499
|
+
|
|
500
|
+
##### Listando Produtos
|
|
501
|
+
|
|
502
|
+
```ruby
|
|
503
|
+
produtos = Conexa::Product.find({ category: 'Eletrônicos' }, 1, 20)
|
|
504
|
+
```
|
|
505
|
+
|
|
506
|
+
#### Vendas (Sales)
|
|
507
|
+
|
|
508
|
+
##### Criando uma Venda
|
|
509
|
+
|
|
510
|
+
```ruby
|
|
511
|
+
venda = Conexa::Sale.new(
|
|
512
|
+
customer_id: customer_id,
|
|
513
|
+
product_id: product_id,
|
|
514
|
+
quantity: 2,
|
|
515
|
+
total_amount: 5980 # em centavos
|
|
516
|
+
).create
|
|
517
|
+
```
|
|
518
|
+
|
|
519
|
+
##### Recuperando uma Venda
|
|
520
|
+
|
|
521
|
+
```ruby
|
|
522
|
+
venda = Conexa::Sale.find(sale_id)
|
|
523
|
+
```
|
|
524
|
+
|
|
525
|
+
##### Excluindo uma Venda
|
|
526
|
+
|
|
527
|
+
```ruby
|
|
528
|
+
Conexa::Sale.destroy(sale_id)
|
|
529
|
+
```
|
|
530
|
+
|
|
531
|
+
##### Listando Vendas
|
|
532
|
+
|
|
533
|
+
```ruby
|
|
534
|
+
vendas = Conexa::Sale.find({ date: '2024-01-01' }, 1, 20)
|
|
535
|
+
```
|
|
536
|
+
|
|
537
|
+
#### Vendas Recorrentes (Recurring Sales)
|
|
538
|
+
|
|
539
|
+
##### Criando uma Venda Recorrente
|
|
540
|
+
|
|
541
|
+
```ruby
|
|
542
|
+
venda_recorrente = Conexa::RecurringSale.new(
|
|
543
|
+
customer_id: customer_id,
|
|
544
|
+
plan_id: plan_id,
|
|
545
|
+
start_date: '2024-01-01'
|
|
546
|
+
).create
|
|
547
|
+
```
|
|
548
|
+
|
|
549
|
+
##### Recuperando uma Venda Recorrente
|
|
550
|
+
|
|
551
|
+
```ruby
|
|
552
|
+
venda_recorrente = Conexa::RecurringSale.find(recurring_sale_id)
|
|
553
|
+
```
|
|
554
|
+
|
|
555
|
+
##### Excluindo uma Venda Recorrente
|
|
556
|
+
|
|
557
|
+
```ruby
|
|
558
|
+
Conexa::RecurringSale.destroy(recurring_sale_id)
|
|
559
|
+
```
|
|
560
|
+
|
|
561
|
+
##### Listando Vendas Recorrentes
|
|
562
|
+
|
|
563
|
+
```ruby
|
|
564
|
+
vendas_recorrentes = Conexa::RecurringSale.find({ active: true }, 1, 20)
|
|
565
|
+
```
|
|
566
|
+
|
|
567
|
+
### Métodos Comuns
|
|
568
|
+
|
|
569
|
+
Cada recurso fornece métodos comuns:
|
|
570
|
+
|
|
571
|
+
- `new(params).create` - Criar um novo registro.
|
|
572
|
+
- `find(id)` - Recuperar um registro específico por ID.
|
|
573
|
+
- `find(hash_filtros, page, size)` - Recuperar registros com filtros e paginação.
|
|
574
|
+
- `destroy(id)` - Excluir um registro por ID.
|
|
575
|
+
- `find(id).destroy` - Buscar e excluir um registro.
|
|
576
|
+
|
|
577
|
+
### Operações Específicas de Recursos
|
|
578
|
+
|
|
579
|
+
Alguns recursos podem oferecer métodos adicionais específicos de sua funcionalidade. Consulte a [Documentação da API Conexa](https://conexa.app/api-docs) para informações detalhadas.
|
|
580
|
+
|
|
581
|
+
## Validando Webhooks
|
|
582
|
+
|
|
583
|
+
Para garantir que todos os webhooks recebidos são enviados pela Conexa, você deve validar a assinatura fornecida no cabeçalho HTTP `X-Signature`. Você pode validá-la usando o payload bruto e a assinatura.
|
|
584
|
+
|
|
585
|
+
```ruby
|
|
586
|
+
valido = Conexa::Webhook.valid_request_signature?(raw_payload, signature)
|
|
587
|
+
```
|
|
588
|
+
|
|
589
|
+
### Exemplo com Rails
|
|
590
|
+
|
|
591
|
+
Se você está usando Rails, pode validar o webhook no seu controller:
|
|
592
|
+
|
|
593
|
+
```ruby
|
|
594
|
+
class WebhooksController < ApplicationController
|
|
595
|
+
skip_before_action :verify_authenticity_token
|
|
596
|
+
|
|
597
|
+
def receive
|
|
598
|
+
if webhook_valido?
|
|
599
|
+
# Processe seu código aqui
|
|
600
|
+
# O payload do webhook está em params
|
|
601
|
+
else
|
|
602
|
+
render_resposta_webhook_invalido
|
|
603
|
+
end
|
|
604
|
+
end
|
|
605
|
+
|
|
606
|
+
private
|
|
607
|
+
|
|
608
|
+
def webhook_valido?
|
|
609
|
+
raw_payload = request.raw_post
|
|
610
|
+
signature = request.headers['X-Signature']
|
|
611
|
+
Conexa::Webhook.valid_request_signature?(raw_payload, signature)
|
|
612
|
+
end
|
|
613
|
+
|
|
614
|
+
def render_resposta_webhook_invalido
|
|
615
|
+
render json: { error: 'Webhook inválido' }, status: 400
|
|
616
|
+
end
|
|
617
|
+
end
|
|
618
|
+
```
|
|
619
|
+
|
|
620
|
+
## Funcionalidades Não Documentadas
|
|
621
|
+
|
|
622
|
+
Esta gem é estável, mas está em desenvolvimento contínuo. Este README fornece uma visão geral rápida de suas principais funcionalidades.
|
|
623
|
+
|
|
624
|
+
Você pode explorar o código-fonte para ver todos os [recursos suportados](lib/conexa/resources). Continuaremos a documentar e adicionar suporte para todas as funcionalidades listadas na [Documentação da API Conexa](https://conexa.app/api-docs).
|
|
625
|
+
|
|
626
|
+
Sinta-se à vontade para contribuir enviando pull requests.
|
|
627
|
+
|
|
628
|
+
## Suporte
|
|
629
|
+
|
|
630
|
+
Se você tiver problemas ou sugestões, por favor abra uma issue [aqui](https://github.com/guilhermegazzinelli/conexa-ruby/issues).
|
|
631
|
+
|
|
632
|
+
## Licença
|
|
633
|
+
|
|
634
|
+
Este projeto está licenciado sob a [Licença MIT](LICENSE).
|
|
635
|
+
|
|
636
|
+
## Aviso Legal
|
|
637
|
+
|
|
638
|
+
Esta gem é baseada nas definições da API Conexa disponíveis na [coleção Postman oficial](https://web.postman.co/workspace/8e1887b1-bef9-4e36-848f-2b6774a81022/collection/33452984-58f4d7ab-d280-4aac-8578-8366988ff7af). Atualmente suporta apenas autenticação via chave de API.
|
|
639
|
+
|
|
640
|
+
Para mais informações sobre a API Conexa, visite a [documentação oficial](https://conexa.app/).
|
data/Rakefile
ADDED
data/bin/console
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
# frozen_string_literal: true
|
|
3
|
+
|
|
4
|
+
require "irb"
|
|
5
|
+
require "irb/completion"
|
|
6
|
+
require "bundler/setup"
|
|
7
|
+
require "conexa"
|
|
8
|
+
require "factory_bot"
|
|
9
|
+
require "faker"
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
|
13
|
+
# with your gem easier. You can also use a different console, if you like.
|
|
14
|
+
|
|
15
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
|
16
|
+
# require "pry"
|
|
17
|
+
# Pry.start
|
|
18
|
+
|
|
19
|
+
# Config IRB to enable --simple-prompt and auto indent
|
|
20
|
+
IRB.conf[:PROMPT_MODE] = :SIMPLE
|
|
21
|
+
IRB.conf[:AUTO_INDENT] = true
|
|
22
|
+
include FactoryBot::Syntax::Methods
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
FactoryBot.find_definitions
|
|
26
|
+
FactoryBot.reload
|
|
27
|
+
Faker::Config.locale = 'pt-BR'
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def reload!(print = true)
|
|
31
|
+
puts 'Reloading ...' if print
|
|
32
|
+
# Main project directory.
|
|
33
|
+
root_dir = File.expand_path('..', __dir__)
|
|
34
|
+
# Directories within the project that should be reloaded.
|
|
35
|
+
reload_dirs = %w{lib}
|
|
36
|
+
# Loop through and reload every file in all relevant project directories.
|
|
37
|
+
reload_dirs.each do |dir|
|
|
38
|
+
Dir.glob("#{root_dir}/#{dir}/**/*.rb").each { |f| load(f) }
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
FactoryBot.reload
|
|
42
|
+
|
|
43
|
+
# Return true when complete.
|
|
44
|
+
true
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def initialize_conexa
|
|
48
|
+
pp "Initilizing..."
|
|
49
|
+
|
|
50
|
+
Conexa.configure do |config|
|
|
51
|
+
config.api_token = ENV['API_TOKEN']
|
|
52
|
+
config.api_host = ENV['API_HOST']
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
pp Conexa.configuration
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
initialize_conexa
|
|
61
|
+
IRB.start
|