moip-assinaturas 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (38) hide show
  1. data/.gitignore +17 -0
  2. data/Gemfile +4 -0
  3. data/Guardfile +11 -0
  4. data/LICENSE.txt +22 -0
  5. data/README.md +180 -0
  6. data/Rakefile +1 -0
  7. data/lib/moip-assinaturas/client.rb +99 -0
  8. data/lib/moip-assinaturas/customer.rb +59 -0
  9. data/lib/moip-assinaturas/invoice.rb +38 -0
  10. data/lib/moip-assinaturas/payment.rb +38 -0
  11. data/lib/moip-assinaturas/plan.rb +59 -0
  12. data/lib/moip-assinaturas/subscription.rb +84 -0
  13. data/lib/moip-assinaturas/version.rb +5 -0
  14. data/lib/moip-assinaturas.rb +33 -0
  15. data/lib/rails/generators/moip_assinaturas/install_generator.rb +14 -0
  16. data/lib/rails/generators/templates/moip_assinaturas.rb +5 -0
  17. data/moip-assinaturas.gemspec +28 -0
  18. data/spec/fixtures/create_customer.json +3 -0
  19. data/spec/fixtures/create_plan.json +12 -0
  20. data/spec/fixtures/create_subscription.json +36 -0
  21. data/spec/fixtures/details_customer.json +34 -0
  22. data/spec/fixtures/details_invoice.json +36 -0
  23. data/spec/fixtures/details_payment.json +34 -0
  24. data/spec/fixtures/details_plan.json +14 -0
  25. data/spec/fixtures/details_subscription.json +32 -0
  26. data/spec/fixtures/list_customers.json +15 -0
  27. data/spec/fixtures/list_invoices.json +23 -0
  28. data/spec/fixtures/list_payments.json +32 -0
  29. data/spec/fixtures/list_plans.json +18 -0
  30. data/spec/fixtures/list_subscriptions.json +35 -0
  31. data/spec/moip-assinaturas/customer_spec.rb +76 -0
  32. data/spec/moip-assinaturas/invoice_spec.rb +35 -0
  33. data/spec/moip-assinaturas/payment_spec.rb +36 -0
  34. data/spec/moip-assinaturas/plan_spec.rb +61 -0
  35. data/spec/moip-assinaturas/subscription_spec.rb +83 -0
  36. data/spec/moip_assinaturas_spec.rb +15 -0
  37. data/spec/spec_helper.rb +18 -0
  38. metadata +230 -0
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in moip-assinaturas.gemspec
4
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,11 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard 'rspec', :notification => true do
5
+ watch(%r{^spec/.+_spec\.rb$})
6
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
7
+ watch(%r{^lib/moip-assinaturas/(.+)\.rb$}) { |m| "spec/moip-assinaturas/#{m[1]}_spec.rb" }
8
+ watch('lib/moip-assinaturas/client.rb') { "spec" }
9
+ watch('spec/spec_helper.rb') { "spec" }
10
+ end
11
+
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Warlley
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,180 @@
1
+ # Moip Assinaturas
2
+
3
+ Essa gem permite utilizar a API do Moip Assinaturas.
4
+
5
+ O Moip Assinaturas permite que você faça cobranças de forma automática, no valor e intervalo que escolher por meio da criação de planos.
6
+
7
+ [http://site.moip.com.br/assinaturas/](http://site.moip.com.br/assinaturas)
8
+
9
+ ## Instalação
10
+
11
+ Adicione a Gem no Gemfile da sua aplicação:
12
+
13
+ gem 'moip-assinaturas'
14
+
15
+ Então execute:
16
+
17
+ $ bundle
18
+
19
+ Ou instale você mesmo:
20
+
21
+ $ gem install moip-assinaturas
22
+
23
+ ## Configuração
24
+
25
+ Use o generator para gerar o arquivo de inicialização do Rails
26
+
27
+ $ rails g moip_assinaturas:install
28
+
29
+ Configure a sua conta
30
+
31
+ ```ruby
32
+ Moip::Assinaturas.config do |config|
33
+ config.sandbox = false
34
+ config.token = "SEU TOKEN"
35
+ config.key = "SUA KEY"
36
+ end
37
+ ```
38
+
39
+ ## Usando
40
+
41
+ Exemplo da criação de um novo plano usando a API do Moip
42
+
43
+ ```ruby
44
+ plan = {
45
+ code: "plano01",
46
+ name: "Plano Especial",
47
+ description: "Descrição do Plano Especial",
48
+ amount: 990,
49
+ setup_fee: 500,
50
+ max_qty: 1,
51
+ interval: {
52
+ length: 1,
53
+ unit: "MONTH"
54
+ },
55
+ billing_cycles: 12
56
+ }
57
+
58
+ request = Moip::Assinaturas::Plan.create(@plan)
59
+
60
+ if request[:success]
61
+ # O plano foi criado com sucesso
62
+ else
63
+ # Houve um erro ao gravar o plano, dê uma olhada em request[:errors]
64
+ end
65
+ ```
66
+
67
+ O uso é bem simples, basta seguir a API para obter os atributos específicos de cada método.
68
+ [http://moiplabs.github.io/assinaturas-docs/api.html](http://moiplabs.github.io/assinaturas-docs/api.html)
69
+
70
+ ## Planos
71
+
72
+ Criar um novo plano:
73
+
74
+ ```ruby
75
+ Moip::Assinaturas::Plan.create(plan_attributes)
76
+ ```
77
+
78
+ Listar todos planos:
79
+
80
+ ```ruby
81
+ Moip::Assinaturas::Plan.list
82
+ ```
83
+
84
+ Obter detalhes do plano:
85
+
86
+ ```ruby
87
+ Moip::Assinaturas::Plan.details(plan_code)
88
+ ```
89
+
90
+ ## Clientes
91
+
92
+ Criar um novo cliente:
93
+
94
+ ```ruby
95
+ Moip::Assinaturas::Customer.create(customer_attributes, new_valt = true)
96
+ ```
97
+
98
+ Listar todos clientes:
99
+
100
+ ```ruby
101
+ Moip::Assinaturas::Customer.list
102
+ ```
103
+
104
+ Obter detalhes do cliente:
105
+
106
+ ```ruby
107
+ Moip::Assinaturas::Customer.details(customer_code)
108
+ ```
109
+
110
+ ## Assinaturas
111
+
112
+ Criar uma nova Assinatura:
113
+
114
+ ```ruby
115
+ Moip::Assinaturas::Subscription.create(subscription_attributes, new_customer = false)
116
+ ```
117
+
118
+ Listar todas assinaturas:
119
+
120
+ ```ruby
121
+ Moip::Assinaturas::Subscription.list
122
+ ```
123
+
124
+ Obter detalhes da assinatura:
125
+
126
+ ```ruby
127
+ Moip::Assinaturas::Subscription.details(subscription_code)
128
+ ```
129
+
130
+ Suspender uma assinatura:
131
+
132
+ ```ruby
133
+ Moip::Assinaturas::Subscription.suspend(subscription_code)
134
+ ```
135
+
136
+ Ativar uma assinatura:
137
+
138
+ ```ruby
139
+ Moip::Assinaturas::Subscription.activate(subscription_code)
140
+ ```
141
+
142
+ ## Cobranças
143
+
144
+ Listar cobranças de uma assinatura:
145
+
146
+ ```ruby
147
+ Moip::Assinaturas::Invoice.list(subscription_code)
148
+ ```
149
+
150
+ Obter detalhes da cobrança
151
+
152
+ ```ruby
153
+ Moip::Assinaturas::Invoice.details(invoice_id)
154
+ ```
155
+
156
+ ## Pagamentos
157
+
158
+ Listar pagamentos de uma cobrança:
159
+
160
+ ```ruby
161
+ Moip::Assinaturas::Payment.list(invoice_id)
162
+ ```
163
+
164
+ Obter detalhes de um pagamento
165
+
166
+ ```ruby
167
+ Moip::Assinaturas::Invoice.details(payment_id)
168
+ ```
169
+
170
+ ## Contribuindo
171
+
172
+ 1. Fork it
173
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
174
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
175
+ 4. Push to the branch (`git push origin my-new-feature`)
176
+ 5. Create new Pull Request
177
+
178
+ ## Agradecimentos
179
+
180
+ Gem baseada no código da gem de pagamentos do **Guilherme Nascimento** - [https://github.com/guinascimento/moip-ruby](https://github.com/guinascimento/moip-ruby)
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,99 @@
1
+ # coding: utf-8
2
+
3
+ require 'httparty'
4
+ require 'json'
5
+
6
+ module Moip::Assinaturas
7
+
8
+ class WebServerResponseError < StandardError ; end
9
+ class MissingTokenError < StandardError ; end
10
+
11
+ class Client
12
+ include HTTParty
13
+
14
+ if Moip::Assinaturas.sandbox
15
+ base_uri "https://sandbox.moip.com.br/assinaturas/v1"
16
+ else
17
+ base_uri "https://api.moip.com.br/assinaturas/v1"
18
+ end
19
+
20
+ basic_auth Moip::Assinaturas.token, Moip::Assinaturas.key
21
+
22
+ class << self
23
+
24
+ def create_plan(plan)
25
+ peform_action!(:post, "/plans", { body: plan.to_json, headers: { 'Content-Type' => 'application/json' } })
26
+ end
27
+
28
+ def list_plans
29
+ peform_action!(:get, "/plans", { headers: { 'Content-Type' => 'application/json' } })
30
+ end
31
+
32
+ def details_plan(code)
33
+ peform_action!(:get, "/plans/#{code}", { headers: { 'Content-Type' => 'application/json' } })
34
+ end
35
+
36
+ def create_customer(customer, new_vault)
37
+ peform_action!(:post, "/customers?new_vault=#{new_vault}", { body: customer.to_json, headers: { 'Content-Type' => 'application/json' } })
38
+ end
39
+
40
+ def list_customers
41
+ peform_action!(:get, "/customers", { headers: { 'Content-Type' => 'application/json' } })
42
+ end
43
+
44
+ def details_customer(code)
45
+ peform_action!(:get, "/customers/#{code}", { headers: { 'Content-Type' => 'application/json' } })
46
+ end
47
+
48
+ def create_subscription(subscription, new_customer)
49
+ peform_action!(:post, "/subscriptions?new_customer=#{new_customer}", { body: subscription.to_json, headers: { 'Content-Type' => 'application/json' } })
50
+ end
51
+
52
+ def list_subscriptions
53
+ peform_action!(:get, "/subscriptions", { headers: { 'Content-Type' => 'application/json' } })
54
+ end
55
+
56
+ def details_subscription(code)
57
+ peform_action!(:get, "/subscriptions/#{code}", { headers: { 'Content-Type' => 'application/json' } })
58
+ end
59
+
60
+ def suspend_subscription(code)
61
+ peform_action!(:put, "/subscriptions/#{code}/suspend", { headers: { 'Content-Type' => 'application/json' } })
62
+ end
63
+
64
+ def activate_subscription(code)
65
+ peform_action!(:put, "/subscriptions/#{code}/activate", { headers: { 'Content-Type' => 'application/json' } })
66
+ end
67
+
68
+ def list_invoices(subscription_code)
69
+ peform_action!(:get, "/subscriptions/#{subscription_code}/invoices", { headers: { 'Content-Type' => 'application/json' } })
70
+ end
71
+
72
+ def details_invoice(id)
73
+ peform_action!(:get, "/invoices/#{id}", { headers: { 'Content-Type' => 'application/json' } })
74
+ end
75
+
76
+ def list_payments(invoice_id)
77
+ peform_action!(:get, "/invoices/#{invoice_id}/payments", { headers: { 'Content-Type' => 'application/json' } })
78
+ end
79
+
80
+ def details_payment(id)
81
+ peform_action!(:get, "/payments/#{id}", { headers: { 'Content-Type' => 'application/json' } })
82
+ end
83
+
84
+ private
85
+
86
+ def peform_action!(action_name, url, options = {})
87
+ if (Moip::Assinaturas.token.blank? or Moip::Assinaturas.key.blank?)
88
+ raise(MissingTokenError, "Informe o token e a key para realizar a autenticação no webservice")
89
+ end
90
+
91
+ response = self.send(action_name, url, options)
92
+ raise(WebServerResponseError, "Ocorreu um erro ao chamar o webservice") if response.nil?
93
+ response
94
+ end
95
+
96
+ end
97
+ end
98
+
99
+ end
@@ -0,0 +1,59 @@
1
+ module Moip::Assinaturas
2
+ class Customer
3
+
4
+ class << self
5
+
6
+ def create(customer, new_valt = true)
7
+ response = Moip::Assinaturas::Client.create_customer(customer, new_valt)
8
+
9
+ case response.code
10
+ when 201
11
+ hash = JSON.load(response.body).with_indifferent_access
12
+ return {
13
+ success: true,
14
+ message: hash['message']
15
+ }
16
+ when 400
17
+ return {
18
+ success: false,
19
+ message: hash['message'],
20
+ errors: hash['errors']
21
+ }
22
+ else
23
+ raise(WebServerResponseError, "Ocorreu um erro no retorno do webservice")
24
+ end
25
+ end
26
+
27
+ def list
28
+ response = Moip::Assinaturas::Client.list_customers
29
+
30
+ case response.code
31
+ when 200
32
+ hash = JSON.load(response.body).with_indifferent_access
33
+ return {
34
+ success: true,
35
+ customers: hash['customers']
36
+ }
37
+ else
38
+ raise(WebServerResponseError, "Ocorreu um erro no retorno do webservice")
39
+ end
40
+ end
41
+
42
+ def details(code)
43
+ response = Moip::Assinaturas::Client.details_customer(code)
44
+
45
+ case response.code
46
+ when 200
47
+ hash = JSON.load(response.body).with_indifferent_access
48
+ return {
49
+ success: true,
50
+ customer: hash
51
+ }
52
+ else
53
+ raise(WebServerResponseError, "Ocorreu um erro no retorno do webservice")
54
+ end
55
+ end
56
+
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,38 @@
1
+ module Moip::Assinaturas
2
+ class Invoice
3
+
4
+ class << self
5
+
6
+ def list(subscription_code)
7
+ response = Moip::Assinaturas::Client.list_invoices(subscription_code)
8
+
9
+ case response.code
10
+ when 200
11
+ hash = JSON.load(response.body).with_indifferent_access
12
+ return {
13
+ success: true,
14
+ invoices: hash[:invoices]
15
+ }
16
+ else
17
+ raise(WebServerResponseError, "Ocorreu um erro no retorno do webservice")
18
+ end
19
+ end
20
+
21
+ def details(id)
22
+ response = Moip::Assinaturas::Client.details_invoice(id)
23
+
24
+ case response.code
25
+ when 200
26
+ hash = JSON.load(response.body).with_indifferent_access
27
+ return {
28
+ success: true,
29
+ invoice: hash
30
+ }
31
+ else
32
+ raise(WebServerResponseError, "Ocorreu um erro no retorno do webservice")
33
+ end
34
+ end
35
+
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,38 @@
1
+ module Moip::Assinaturas
2
+ class Payment
3
+
4
+ class << self
5
+
6
+ def list(invoice_id)
7
+ response = Moip::Assinaturas::Client.list_payments(invoice_id)
8
+
9
+ case response.code
10
+ when 200
11
+ hash = JSON.load(response.body).with_indifferent_access
12
+ return {
13
+ success: true,
14
+ payments: hash[:payments]
15
+ }
16
+ else
17
+ raise(WebServerResponseError, "Ocorreu um erro no retorno do webservice")
18
+ end
19
+ end
20
+
21
+ def details(id)
22
+ response = Moip::Assinaturas::Client.details_payment(id)
23
+
24
+ case response.code
25
+ when 200
26
+ hash = JSON.load(response.body).with_indifferent_access
27
+ return {
28
+ success: true,
29
+ payment: hash
30
+ }
31
+ else
32
+ raise(WebServerResponseError, "Ocorreu um erro no retorno do webservice")
33
+ end
34
+ end
35
+
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,59 @@
1
+ module Moip::Assinaturas
2
+ class Plan
3
+
4
+ class << self
5
+
6
+ def create(plan)
7
+ response = Moip::Assinaturas::Client.create_plan(plan)
8
+
9
+ case response.code
10
+ when 201
11
+ hash = JSON.load(response.body).with_indifferent_access
12
+ return {
13
+ success: true,
14
+ plan: hash
15
+ }
16
+ when 400
17
+ return {
18
+ success: false,
19
+ message: hash['message'],
20
+ errors: hash['errors']
21
+ }
22
+ else
23
+ raise(WebServerResponseError, "Ocorreu um erro no retorno do webservice")
24
+ end
25
+ end
26
+
27
+ def list
28
+ response = Moip::Assinaturas::Client.list_plans
29
+
30
+ case response.code
31
+ when 200
32
+ hash = JSON.load(response.body).with_indifferent_access
33
+ return {
34
+ success: true,
35
+ plans: hash[:plans]
36
+ }
37
+ else
38
+ raise(WebServerResponseError, "Ocorreu um erro no retorno do webservice")
39
+ end
40
+ end
41
+
42
+ def details(code)
43
+ response = Moip::Assinaturas::Client.details_plan(code)
44
+
45
+ case response.code
46
+ when 200
47
+ hash = JSON.load(response.body).with_indifferent_access
48
+ return {
49
+ success: true,
50
+ plan: hash
51
+ }
52
+ else
53
+ raise(WebServerResponseError, "Ocorreu um erro no retorno do webservice")
54
+ end
55
+ end
56
+
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,84 @@
1
+ module Moip::Assinaturas
2
+ class Subscription
3
+
4
+ class << self
5
+
6
+ def create(subscription, new_customer = false)
7
+ response = Moip::Assinaturas::Client.create_subscription(subscription, new_customer)
8
+
9
+ case response.code
10
+ when 201
11
+ hash = JSON.load(response.body).with_indifferent_access
12
+ return {
13
+ success: true,
14
+ subscription: hash
15
+ }
16
+ when 400
17
+ return {
18
+ success: false,
19
+ message: hash[:message],
20
+ errors: hash[:errors]
21
+ }
22
+ else
23
+ raise(WebServerResponseError, "Ocorreu um erro no retorno do webservice")
24
+ end
25
+
26
+ end
27
+
28
+ def list
29
+ response = Moip::Assinaturas::Client.list_subscriptions
30
+
31
+ case response.code
32
+ when 200
33
+ hash = JSON.load(response.body).with_indifferent_access
34
+ return {
35
+ success: true,
36
+ subscriptions: hash[:subscriptions]
37
+ }
38
+ else
39
+ raise(WebServerResponseError, "Ocorreu um erro no retorno do webservice")
40
+ end
41
+
42
+ end
43
+
44
+ def details(code)
45
+ response = Moip::Assinaturas::Client.details_subscription(code)
46
+
47
+ case response.code
48
+ when 200
49
+ hash = JSON.load(response.body).with_indifferent_access
50
+ return {
51
+ success: true,
52
+ subscription: hash
53
+ }
54
+ else
55
+ raise(WebServerResponseError, "Ocorreu um erro no retorno do webservice")
56
+ end
57
+ end
58
+
59
+ def suspend(code)
60
+ response = Moip::Assinaturas::Client.suspend_subscription(code)
61
+
62
+ case response.code
63
+ when 201
64
+ return { success: true }
65
+ else
66
+ raise(WebServerResponseError, "Ocorreu um erro no retorno do webservice")
67
+ end
68
+ end
69
+
70
+ def activate(code)
71
+ response = Moip::Assinaturas::Client.activate_subscription(code)
72
+
73
+ case response.code
74
+ when 201
75
+ return { success: true }
76
+ else
77
+ raise(WebServerResponseError, "Ocorreu um erro no retorno do webservice")
78
+ end
79
+ end
80
+
81
+ end
82
+
83
+ end
84
+ end
@@ -0,0 +1,5 @@
1
+ module Moip
2
+ module Assinaturas
3
+ VERSION = "0.0.2"
4
+ end
5
+ end
@@ -0,0 +1,33 @@
1
+ require 'moip-assinaturas/version'
2
+ require 'active_support/core_ext/module/attribute_accessors'
3
+ require 'active_support/core_ext/object/blank'
4
+ require 'active_support/core_ext/hash/indifferent_access'
5
+ require 'active_support/deprecation'
6
+
7
+ module Moip
8
+ module Assinaturas
9
+
10
+ autoload :Plan, 'moip-assinaturas/plan'
11
+ autoload :Customer, 'moip-assinaturas/customer'
12
+ autoload :Subscription, 'moip-assinaturas/subscription'
13
+ autoload :Invoice, 'moip-assinaturas/invoice'
14
+ autoload :Payment, 'moip-assinaturas/payment'
15
+ autoload :Client, 'moip-assinaturas/client'
16
+
17
+ mattr_accessor :sandbox
18
+ @@sandbox = false
19
+
20
+ # Token de autenticação
21
+ mattr_accessor :token
22
+
23
+ # Chave de acesso ao serviço
24
+ mattr_accessor :key
25
+
26
+ class << self
27
+ def config
28
+ yield self
29
+ end
30
+ end
31
+
32
+ end
33
+ end
@@ -0,0 +1,14 @@
1
+ module MoipAssinaturas
2
+ module Generators
3
+
4
+ class InstallGenerator < Rails::Generators::Base
5
+ source_root File.expand_path("../../templates", __FILE__)
6
+ desc "Creates a Moip Assinaturas initializer"
7
+
8
+ def copy_initializer
9
+ template 'moip_assinaturas.rb', 'config/initializers/moip_assinaturas.rb'
10
+ end
11
+ end
12
+
13
+ end
14
+ end
@@ -0,0 +1,5 @@
1
+ Moip::Assinaturas.config do |config|
2
+ config.sandbox = false
3
+ config.token = "SEU TOKEN"
4
+ config.key = "SUA KEY"
5
+ end