api-moip-assinaturas 0.0.6 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/api_moip_assinaturas.gemspec +2 -2
- data/lib/moip.rb +9 -8
- data/lib/moip/configuration.rb +1 -2
- data/lib/moip/header.rb +4 -2
- data/lib/moip/model.rb +29 -6
- data/lib/moip/models/address.rb +26 -0
- data/lib/moip/models/billing_info.rb +30 -0
- data/lib/moip/models/costumer.rb +95 -10
- data/lib/moip/models/invoice.rb +49 -8
- data/lib/moip/models/payment.rb +35 -9
- data/lib/moip/models/plan.rb +66 -18
- data/lib/moip/models/subscription.rb +119 -9
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
MmJmZjI1NGUxYzM2MThmYzExNTI4ODgzNDE0Njg4MjFiODljOGQwNQ==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
N2Q4YTBlZTMzOGFjMjA0MGNmNWE1MWVkNzc2Y2YxMmQyZmUzYzVhMw==
|
7
7
|
!binary "U0hBNTEy":
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
MmUxZjYyYzdjZmNlNjM4MTY4MDU2ODFlNjY2M2VhOGJkY2VmYjcyNTZmMTU0
|
10
|
+
NmEwZDI5YmVmMzgwMGIwNTU1MjhiY2Y2Njg4MGE3YjBhZjNlYTUwMDMyZDA1
|
11
|
+
MTY3YTk5YWM3MmE4OGQ5MmM5M2RlNTE2MTUyZmNjNDYzYzhmYzU=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
NmZmNmQwOTYxZDRmODBlMGRiMTJmOGU3NTYyZWNlYzdkNDY3ZDA3NmEwMTdl
|
14
|
+
MWQyNDQ4YzY0ODUyYmJjMWM3ODYwNThkY2Y2ZjBmZGI4YmIxY2E5MTI3ZGY2
|
15
|
+
MDQ1NDJkYzUzOGQwOTdjYzZiNWVmODhmMzVhNGQ4Nzc4ZGJmMjk=
|
@@ -1,7 +1,7 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = 'api-moip-assinaturas'
|
3
|
-
s.version = '0.0
|
4
|
-
s.date = '2013-06-
|
3
|
+
s.version = '0.1.0'
|
4
|
+
s.date = '2013-06-10'
|
5
5
|
s.summary = "Moip Assinaturas by Pixxel"
|
6
6
|
s.description = "Gem desenvolvida para atender aos requisitos do moip api de assinaturas"
|
7
7
|
s.authors = ["Douglas Rossignolli"]
|
data/lib/moip.rb
CHANGED
@@ -1,14 +1,15 @@
|
|
1
|
+
require "httparty"
|
2
|
+
require "json"
|
3
|
+
require "active_model"
|
4
|
+
|
1
5
|
module Moip
|
2
6
|
end
|
3
7
|
|
4
8
|
# imports para o statup
|
5
|
-
require
|
6
|
-
require "json"
|
7
|
-
require "active_model"
|
8
|
-
require "moip/configuration"
|
9
|
-
require "moip/header"
|
10
|
-
require "moip/model"
|
11
|
-
require "moip/webhooks"
|
9
|
+
Dir["#{Dir.pwd}/lib/moip/**.rb"].each { |file| require file }
|
12
10
|
|
13
11
|
# require all models
|
14
|
-
Dir["#{Dir.pwd}/lib/moip/models/**"].each { |file| require file }
|
12
|
+
Dir["#{Dir.pwd}/lib/moip/models/**"].each { |file| require file }
|
13
|
+
|
14
|
+
Moip.configure {|config| config.token = "3JECRRW49I3HHKHI8VC517EDMS2SS1S9"; config.acount_key = "AZQDHSQ8MSL3F57JBERR59HKBVXVDMSDXQ8V1S6V"}
|
15
|
+
|
data/lib/moip/configuration.rb
CHANGED
data/lib/moip/header.rb
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
module Moip
|
2
2
|
module Header
|
3
|
-
def default_header
|
3
|
+
def default_header json = nil
|
4
4
|
header = {}
|
5
5
|
header.merge! :basic_auth => auth
|
6
6
|
header.merge! :headers => { 'Content-Type' => 'application/json', 'Accept' => 'application/json' }
|
7
|
-
header.merge! :body =>
|
7
|
+
header.merge! :body => json if json
|
8
8
|
|
9
9
|
header
|
10
10
|
end
|
@@ -17,7 +17,9 @@ module Moip
|
|
17
17
|
url = "https://sandbox.moip.com.br/assinaturas/v1/#{model.to_s}"
|
18
18
|
url << "/#{options[:code]}" if options[:code]
|
19
19
|
url << "/#{options[:status]}" if options[:status]
|
20
|
+
url << "?#{options[:params]}" if options[:params]
|
20
21
|
url
|
21
22
|
end
|
23
|
+
|
22
24
|
end
|
23
25
|
end
|
data/lib/moip/model.rb
CHANGED
@@ -1,12 +1,35 @@
|
|
1
|
+
# encoding: utf-8
|
1
2
|
module Moip
|
2
|
-
|
3
|
+
class Model
|
3
4
|
include ActiveModel::Validations
|
4
5
|
include ActiveModel::Serialization
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
6
|
+
|
7
|
+
def to_json
|
8
|
+
hash = self.serializable_hash.delete_if {|key, value| value.nil? }
|
9
|
+
hash.to_json
|
10
|
+
end
|
11
|
+
|
12
|
+
def set_parameters params
|
13
|
+
params.each do |key, value|
|
14
|
+
self.send("#{key}=".to_sym, value)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
class << self
|
19
|
+
# metodo que cria um objeto e seta os seus parametros se responder pela chave
|
20
|
+
# recebe um Hash como parametro
|
21
|
+
def build params
|
22
|
+
if params.is_a? Hash
|
23
|
+
object = new
|
24
|
+
|
25
|
+
params.each do |key, value|
|
26
|
+
object.send("#{key}=".to_sym, value) if object.respond_to? key.to_sym
|
27
|
+
end
|
28
|
+
|
29
|
+
object
|
30
|
+
else
|
31
|
+
raise Exception.new "unexpected parameter, expected Hash, received #{params.class}"
|
32
|
+
end
|
10
33
|
end
|
11
34
|
end
|
12
35
|
|
@@ -0,0 +1,26 @@
|
|
1
|
+
class Moip::Address < Moip::Model
|
2
|
+
|
3
|
+
attr_accessor :street, :number, :complement,
|
4
|
+
:district, :city, :state, :country, :zipcode
|
5
|
+
|
6
|
+
validates :street, :number,
|
7
|
+
:district, :city, :state, :country,
|
8
|
+
:zipcode, :presence => true
|
9
|
+
|
10
|
+
validates_format_of :number, :with => /[0-9]{1,6}/
|
11
|
+
validates_format_of :zipcode, :with => /[0-9]{8}/
|
12
|
+
|
13
|
+
def attributes
|
14
|
+
{
|
15
|
+
"street" => street,
|
16
|
+
"number" => number,
|
17
|
+
"complement" => complement,
|
18
|
+
"district" => district,
|
19
|
+
"city" => city,
|
20
|
+
"state" => state,
|
21
|
+
"country" => country,
|
22
|
+
"zipcode" => zipcode
|
23
|
+
}
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
class Moip::BillingInfo < Moip::Model
|
2
|
+
|
3
|
+
attr_accessor :holder_name, :number, :expiration_month, :expiration_year
|
4
|
+
|
5
|
+
validates :holder_name, :number, :expiration_month, :expiration_year, :presence => true
|
6
|
+
|
7
|
+
validates_format_of :expiration_month, :with => /[0-9]{2}/
|
8
|
+
validates_format_of :expiration_year, :with => /[0-9]{2}/
|
9
|
+
validates_format_of :number, :with => /[0-9]{16}/
|
10
|
+
|
11
|
+
|
12
|
+
def attributes
|
13
|
+
{
|
14
|
+
"holder_name" => holder_name,
|
15
|
+
"number" => number,
|
16
|
+
"expiration_month" => expiration_month,
|
17
|
+
"expiration_year" => expiration_year
|
18
|
+
}
|
19
|
+
end
|
20
|
+
|
21
|
+
def to_json
|
22
|
+
hash = { :credit_card => self.serializable_hash }
|
23
|
+
hash.to_json
|
24
|
+
end
|
25
|
+
|
26
|
+
def to_hash
|
27
|
+
{ :credit_card => self.serializable_hash }
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
data/lib/moip/models/costumer.rb
CHANGED
@@ -1,21 +1,106 @@
|
|
1
|
-
|
1
|
+
# encoding: utf-8
|
2
|
+
class Moip::Costumer < Moip::Model
|
2
3
|
include HTTParty
|
3
4
|
include Moip::Header
|
4
|
-
|
5
|
-
|
6
|
-
|
5
|
+
|
6
|
+
# see http://moiplabs.github.io/assinaturas-docs/api.html#criar_cliente
|
7
|
+
attr_accessor :code, :email, :fullname, :cpf, :phone_area_code,
|
8
|
+
:phone_number, :birthdate_day, :birthdate_month,
|
9
|
+
:birthdate_year, :address, :billing_info, :costumers
|
10
|
+
|
11
|
+
validates :code, :email, :fullname, :cpf, :phone_area_code,
|
12
|
+
:phone_number, :birthdate_day, :birthdate_month,
|
13
|
+
:birthdate_year, :presence => true
|
14
|
+
|
15
|
+
validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i, :message => "Invalid email"
|
16
|
+
|
17
|
+
validate :validates_presence_of_address, :validates_presence_of_billing_info
|
18
|
+
|
19
|
+
def attributes
|
20
|
+
{
|
21
|
+
"code" => code,
|
22
|
+
"email" => email,
|
23
|
+
"fullname" => fullname,
|
24
|
+
"cpf" => cpf,
|
25
|
+
"phone_area_code" => phone_area_code,
|
26
|
+
"phone_number" => phone_number,
|
27
|
+
"birthdate_day" => birthdate_day,
|
28
|
+
"birthdate_month" => birthdate_month,
|
29
|
+
"birthdate_year" => birthdate_year,
|
30
|
+
"address" => address,
|
31
|
+
"billing_info" => billing_info
|
32
|
+
}
|
7
33
|
end
|
8
34
|
|
9
|
-
def
|
10
|
-
|
35
|
+
def address= options = {}
|
36
|
+
@address = Moip::Address.build options
|
11
37
|
end
|
12
38
|
|
13
|
-
def
|
14
|
-
|
39
|
+
def address
|
40
|
+
@address.serializable_hash.delete_if {|key, value| value.nil? }
|
15
41
|
end
|
16
42
|
|
17
|
-
def
|
18
|
-
|
43
|
+
def billing_info= options
|
44
|
+
if options.is_a? Hash
|
45
|
+
@billing_info = Moip::BillingInfo.build options
|
46
|
+
elsif options.is_a? Moip::BillingInfo
|
47
|
+
@billing_info = Moip::BillingInfo
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def billing_info
|
52
|
+
@billing_info.to_hash
|
53
|
+
end
|
54
|
+
|
55
|
+
def validates_presence_of_address
|
56
|
+
self.errors.add :address, "can't be blank" and return if @address.nil?
|
57
|
+
|
58
|
+
if @address.valid?
|
59
|
+
true
|
60
|
+
else
|
61
|
+
self.errors.add :adress, @address.errors.full_messages.first
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
def validates_presence_of_billing_info
|
66
|
+
self.errors.add :billing_info, "can't be blank" and return if @billing_info.nil?
|
67
|
+
|
68
|
+
if @billing_info.valid?
|
69
|
+
true
|
70
|
+
else
|
71
|
+
self.errors.add :adress, @billing_info.errors.full_messages.first
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
def costumers= hash
|
76
|
+
@costumers = []
|
77
|
+
hash.each do |e|
|
78
|
+
costumer = self.class.new
|
79
|
+
costumer.set_parameters e
|
80
|
+
@costumers << costumer
|
81
|
+
end
|
82
|
+
@costumers
|
83
|
+
end
|
84
|
+
|
85
|
+
|
86
|
+
def load
|
87
|
+
list = self.class.get(base_url(:customers), default_header).parsed_response
|
88
|
+
self.costumers = list["costumers"]
|
89
|
+
end
|
90
|
+
|
91
|
+
# metodo que envia as informações para a API do moip e cria um novo cliente
|
92
|
+
# see http://moiplabs.github.io/assinaturas-docs/api.html#criar_cliente
|
93
|
+
def create
|
94
|
+
if self.valid?
|
95
|
+
self.class.post(base_url(:customers, :params => "new_vault=true"), default_header(self.to_json)).parsed_response
|
96
|
+
else
|
97
|
+
raise Exception.new "#{self.errors.first[0]} #{self.errors.first[1]}"
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
def find code
|
102
|
+
response = self.class.get(base_url(:customers, :code => code), default_header).parsed_response
|
103
|
+
self.set_parameters response unless response.nil?
|
19
104
|
end
|
20
105
|
|
21
106
|
end
|
data/lib/moip/models/invoice.rb
CHANGED
@@ -1,21 +1,62 @@
|
|
1
|
-
|
1
|
+
#encoding: utf-8
|
2
|
+
class Moip::Invoice < Moip::Model
|
2
3
|
include HTTParty
|
3
4
|
include Moip::Header
|
5
|
+
|
6
|
+
attr_accessor :id, :amount, :subscription_code, :occurrence,
|
7
|
+
:status, :items, :plan, :customer, :creation_date, :invoices
|
8
|
+
|
9
|
+
def attributes
|
10
|
+
{
|
11
|
+
"id" => id,
|
12
|
+
"amount" => amount,
|
13
|
+
"subscription_code" => subscription_code,
|
14
|
+
"occurrence" => occurrence,
|
15
|
+
"status" => status,
|
16
|
+
"items" => items,
|
17
|
+
"plan" => plan,
|
18
|
+
"customer" => customer,
|
19
|
+
"creation_date" => creation_date
|
20
|
+
}
|
21
|
+
end
|
4
22
|
|
5
|
-
def
|
6
|
-
|
23
|
+
def invoices= hash
|
24
|
+
@invoices = []
|
25
|
+
hash.each do |e|
|
26
|
+
invoice = self.class.new
|
27
|
+
invoice.set_parameters e
|
28
|
+
@invoices << invoice
|
29
|
+
end
|
30
|
+
@invoices
|
31
|
+
end
|
32
|
+
|
33
|
+
def full_status
|
34
|
+
status = case self.status
|
35
|
+
when 1 then "Em aberto" # A fatura foi gerada mas ainda não foi paga pelo assinante.
|
36
|
+
when 2 then "Aguardando Confirmação" # A fatura foi paga pelo assinante, mas o pagamento está em processo de análise de risco.
|
37
|
+
when 3 then "Pago" # A fatura foi paga pelo assinante e confirmada.
|
38
|
+
when 4 then "Não Pago" # O assinante tentou pagar a fatura, mas o pagamento foi negado pelo banco emissor do cartão de crédito ou a análise de risco detectou algum problema. Veja os motivos possíveis.
|
39
|
+
when 5 then "Atrasada" # O pagamento da fatura foi cancelado e serão feitas novas tentativas de cobrança de acordo com a configuração de retentativa automática.
|
40
|
+
end
|
41
|
+
status
|
42
|
+
end
|
43
|
+
|
44
|
+
def invoices
|
45
|
+
@invoices ||= self.invoices = self.load
|
7
46
|
end
|
8
47
|
|
9
|
-
def
|
10
|
-
|
48
|
+
def load
|
49
|
+
list = self.class.get(base_url(:subscriptions, :code => self.subscription_code, :status => "invoices"), default_header).parsed_response
|
50
|
+
self.invoices = list["invoices"]
|
11
51
|
end
|
12
52
|
|
13
53
|
def find
|
14
|
-
|
54
|
+
response = self.class.get(base_url(:invoices, :code => self.code), default_header).parsed_response
|
55
|
+
self.set_parameters response unless response.nil?
|
15
56
|
end
|
16
57
|
|
17
|
-
def
|
18
|
-
|
58
|
+
def payments
|
59
|
+
|
19
60
|
end
|
20
61
|
|
21
62
|
end
|
data/lib/moip/models/payment.rb
CHANGED
@@ -1,21 +1,47 @@
|
|
1
|
-
class Moip::Payment
|
1
|
+
class Moip::Payment < Moip::Model
|
2
2
|
include HTTParty
|
3
3
|
include Moip::Header
|
4
4
|
|
5
|
-
|
6
|
-
|
5
|
+
attr_accessor :id, :moip_id, :status, :subscription_code,
|
6
|
+
:customer_code, :invoice, :payment_method,
|
7
|
+
:creation_date
|
8
|
+
|
9
|
+
def attributes
|
10
|
+
{
|
11
|
+
"id": id,
|
12
|
+
"moip_id": moip_id,
|
13
|
+
"status": status,
|
14
|
+
"subscription_code": subscription_code,
|
15
|
+
"customer_code": customer_code,
|
16
|
+
"invoice": invoice,
|
17
|
+
"payment_method": payment_method,
|
18
|
+
"creation_date": creation_date
|
19
|
+
}
|
7
20
|
end
|
8
21
|
|
9
|
-
def
|
10
|
-
|
22
|
+
def payments= hash
|
23
|
+
@payments = []
|
24
|
+
hash.each do |e|
|
25
|
+
payment = self.class.new
|
26
|
+
payment.set_parameters e
|
27
|
+
@payments << payment
|
28
|
+
end
|
29
|
+
@payments
|
11
30
|
end
|
12
31
|
|
13
|
-
def
|
14
|
-
|
32
|
+
def payments
|
33
|
+
@payments ||= self.payments = self.load
|
34
|
+
end
|
35
|
+
|
36
|
+
def load
|
37
|
+
self.class.get(base_url(:invoices, :code => self.subscription_code, :status => "payments"), default_header).parsed_response
|
38
|
+
self.payments = list["payments"]
|
15
39
|
end
|
16
40
|
|
17
|
-
def
|
18
|
-
|
41
|
+
def find
|
42
|
+
response = self.class.get(base_url(:invoices, :code => self.id), default_header).parsed_response
|
43
|
+
self.set_parameters response unless response.nil?
|
19
44
|
end
|
45
|
+
|
20
46
|
|
21
47
|
end
|
data/lib/moip/models/plan.rb
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
# encoding: utf-8
|
2
|
-
class Plan
|
2
|
+
class Moip::Plan < Moip::Model
|
3
3
|
include HTTParty
|
4
4
|
include Moip::Header
|
5
5
|
|
6
6
|
# see http://moiplabs.github.io/assinaturas-docs/api.html#criar_plano
|
7
7
|
attr_accessor :code, :name, :description, :amount,
|
8
8
|
:setup_fee, :interval, :billing_cycles,
|
9
|
-
:status, :max_qty
|
9
|
+
:status, :max_qty, :plans
|
10
10
|
|
11
|
-
|
11
|
+
validates :code, :name, :amount, :presence => true
|
12
12
|
|
13
13
|
def attributes
|
14
14
|
{
|
@@ -24,34 +24,82 @@ class Plan
|
|
24
24
|
}
|
25
25
|
end
|
26
26
|
|
27
|
-
|
28
|
-
|
29
|
-
|
27
|
+
def interval= options = {}
|
28
|
+
@interval = {}
|
29
|
+
@interval.merge! :length => options[:length]
|
30
|
+
@interval.merge! :unit => options[:unit]
|
31
|
+
end
|
32
|
+
|
33
|
+
def interval
|
34
|
+
return nil if @interval.nil?
|
35
|
+
|
36
|
+
@interval.delete_if {|key, value| value.nil? }
|
37
|
+
|
38
|
+
if @interval.size > 0
|
39
|
+
@interval
|
40
|
+
else
|
41
|
+
nil
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def plans= hash
|
46
|
+
@plans = []
|
47
|
+
hash.each do |e|
|
48
|
+
plan = self.class.new
|
49
|
+
plan.set_parameters e
|
50
|
+
@plans << plan
|
51
|
+
end
|
52
|
+
@plans
|
30
53
|
end
|
31
54
|
|
32
55
|
# see http://moiplabs.github.io/assinaturas-docs/api.html#listar_plano
|
33
|
-
def
|
34
|
-
self.class.get(base_url(:plans), default_header).parsed_response
|
56
|
+
def load
|
57
|
+
list = self.class.get(base_url(:plans), default_header).parsed_response
|
58
|
+
self.plans = list["plans"]
|
35
59
|
end
|
36
60
|
|
37
61
|
# metodo que envia as informações para a API do moip e cria um novo plano
|
38
62
|
# see http://moiplabs.github.io/assinaturas-docs/api.html#criar_plano
|
39
|
-
def create
|
40
|
-
|
63
|
+
def create
|
64
|
+
if self.valid?
|
65
|
+
self.class.post(base_url(:plans), default_header(self.to_json)).parsed_response
|
66
|
+
else
|
67
|
+
raise Exception.new "#{self.errors.first[0]} #{self.errors.first[1]}"
|
68
|
+
end
|
41
69
|
end
|
42
70
|
|
43
|
-
|
44
|
-
|
71
|
+
# see http://moiplabs.github.io/assinaturas-docs/api.html#consultar_plano
|
72
|
+
def find code
|
73
|
+
response = self.class.get(base_url(:plans, :code => code), default_header).parsed_response
|
74
|
+
self.set_parameters response unless response.nil?
|
45
75
|
end
|
46
76
|
|
47
|
-
|
48
|
-
|
77
|
+
# see http://moiplabs.github.io/assinaturas-docs/api.html#alterar_plano
|
78
|
+
def update
|
79
|
+
if self.valid?
|
80
|
+
self.class.put(base_url(:plans, :code => self.code), default_header(self)).parsed_response
|
81
|
+
true
|
82
|
+
else
|
83
|
+
raise Exception.new "#{self.errors.first[0]} #{self.errors.first[1]}"
|
84
|
+
end
|
49
85
|
end
|
50
86
|
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
87
|
+
# see http://moiplabs.github.io/assinaturas-docs/api.html#ativar_desativar_plano
|
88
|
+
def activate
|
89
|
+
if self.status != "activate"
|
90
|
+
self.status = "activate"
|
91
|
+
self.class.put(base_url(:plans, :code => self.code, :status => "activate"), default_header).parsed_response
|
92
|
+
true
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
# see http://moiplabs.github.io/assinaturas-docs/api.html#ativar_desativar_plano
|
97
|
+
def inactivate
|
98
|
+
if self.status != "inactivate"
|
99
|
+
self.status = "inactivate"
|
100
|
+
self.class.put(base_url(:plans, :code => self.code, :status => "inactivate"), default_header).parsed_response
|
101
|
+
true
|
102
|
+
end
|
55
103
|
end
|
56
104
|
|
57
105
|
end
|
@@ -1,21 +1,131 @@
|
|
1
|
-
|
1
|
+
# encoding: utf-8
|
2
|
+
class Moip::Subscription < Moip::Model
|
2
3
|
include HTTParty
|
3
4
|
include Moip::Header
|
4
|
-
|
5
|
-
|
6
|
-
|
5
|
+
|
6
|
+
attr_accessor :code, :amount, :plan, :customer,
|
7
|
+
:next_invoice_date, :status, :message,
|
8
|
+
:errors, :alerts, :invoice, :creation_date,
|
9
|
+
:expiration_date, :subscriptions
|
10
|
+
|
11
|
+
validates :code, :amount, :plan, :customer, :presence => true
|
12
|
+
|
13
|
+
def attributes
|
14
|
+
{
|
15
|
+
"code" => code,
|
16
|
+
"amount" => amount,
|
17
|
+
"plan" => plan,
|
18
|
+
"customer" => customer
|
19
|
+
}
|
20
|
+
end
|
21
|
+
|
22
|
+
def plan
|
23
|
+
if @plan.is_a? Hash
|
24
|
+
@plan
|
25
|
+
else
|
26
|
+
{ :code => @plan }
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def customer
|
31
|
+
if @customer.is_a? Hash
|
32
|
+
@customer
|
33
|
+
else
|
34
|
+
{ :code => @customer }
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def subscriptions= hash
|
39
|
+
@subscriptions = []
|
40
|
+
hash.each do |e|
|
41
|
+
subscription = self.class.new
|
42
|
+
subscription.set_parameters e
|
43
|
+
@subscriptions << subscription
|
44
|
+
end
|
45
|
+
@subscriptions
|
7
46
|
end
|
8
47
|
|
48
|
+
def subscriptions
|
49
|
+
@subscriptions
|
50
|
+
end
|
51
|
+
|
52
|
+
def invoices
|
53
|
+
@invoices ||= Moip::Invoice.build(:subscription_code => self.code).invoices
|
54
|
+
end
|
55
|
+
|
56
|
+
# see http://moiplabs.github.io/assinaturas-docs/api.html#listar_assinaturas
|
57
|
+
def load
|
58
|
+
list = self.class.get(base_url(:subscriptions), default_header).parsed_response
|
59
|
+
self.subscriptions = list["subscriptions"]
|
60
|
+
end
|
61
|
+
|
62
|
+
# see http://moiplabs.github.io/assinaturas-docs/api.html#criar_assinatura
|
9
63
|
def create
|
10
|
-
|
64
|
+
self.class.post(base_url(:subscriptions), default_header(self.to_json)).parsed_response
|
65
|
+
end
|
66
|
+
|
67
|
+
# To Do
|
68
|
+
def create_with_costumer
|
69
|
+
# To Do: will not be developed this feature right now, if you
|
70
|
+
# want to fork and develop this feature, be welcome.
|
11
71
|
end
|
12
72
|
|
13
|
-
|
14
|
-
|
73
|
+
# see http://moiplabs.github.io/assinaturas-docs/api.html#consultar_assinatura
|
74
|
+
def find code
|
75
|
+
response = self.class.get(base_url(:subscriptions, :code => code), default_header).parsed_response
|
76
|
+
self.set_parameters response unless response.nil?
|
15
77
|
end
|
16
78
|
|
17
|
-
def
|
18
|
-
|
79
|
+
def suspend
|
80
|
+
if self.status != "suspend"
|
81
|
+
self.status = "suspend"
|
82
|
+
self.class.put(base_url(:subscriptions, :code => self.code, :status => "suspend"), default_header).parsed_response
|
83
|
+
true
|
84
|
+
end
|
19
85
|
end
|
86
|
+
|
87
|
+
def activate
|
88
|
+
if self.status != "activate"
|
89
|
+
self.status = "activate"
|
90
|
+
self.class.put(base_url(:subscriptions, :code => self.code, :status => "activate"), default_header).parsed_response
|
91
|
+
true
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
# see http://moiplabs.github.io/assinaturas-docs/api.html#alterar_assinatura
|
96
|
+
def update_plan new_plan
|
97
|
+
hash = {}
|
98
|
+
hash[:plan] = { :code => new_plan }
|
99
|
+
|
100
|
+
self.class.put(base_url(:subscriptions, :code => self.code), default_header(hash.to_json)).parsed_response
|
101
|
+
end
|
102
|
+
|
103
|
+
# see http://moiplabs.github.io/assinaturas-docs/api.html#alterar_assinatura
|
104
|
+
def update_next_invoice plan, next_invoice_date
|
105
|
+
hash = {}
|
106
|
+
hash[:plan] = { :code => plan }
|
107
|
+
hash[:next_invoice_date] = next_invoice_date
|
108
|
+
|
109
|
+
self.class.put(base_url(:subscriptions, :code => self.code), default_header(hash.to_json)).parsed_response
|
110
|
+
end
|
111
|
+
|
112
|
+
def update_amount plan, new_amount
|
113
|
+
hash = {}
|
114
|
+
|
115
|
+
self.find self.code
|
116
|
+
|
117
|
+
hash[:plan] = { :code => plan }
|
118
|
+
hash[:amount] = new_amount
|
119
|
+
|
120
|
+
self.class.put(base_url(:subscriptions, :code => self.code), default_header(hash.to_json)).parsed_response
|
121
|
+
end
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
|
128
|
+
|
129
|
+
|
20
130
|
|
21
131
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: api-moip-assinaturas
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Douglas Rossignolli
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-06-
|
11
|
+
date: 2013-06-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: httparty
|
@@ -79,6 +79,8 @@ files:
|
|
79
79
|
- lib/moip/configuration.rb
|
80
80
|
- lib/moip/header.rb
|
81
81
|
- lib/moip/model.rb
|
82
|
+
- lib/moip/models/address.rb
|
83
|
+
- lib/moip/models/billing_info.rb
|
82
84
|
- lib/moip/models/costumer.rb
|
83
85
|
- lib/moip/models/invoice.rb
|
84
86
|
- lib/moip/models/payment.rb
|