iugu-dev-wonder 1.0.9
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +56 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +57 -0
- data/Rakefile +1 -0
- data/iugu.gemspec +26 -0
- data/lib/iugu.rb +54 -0
- data/lib/iugu/api_child_resource.rb +29 -0
- data/lib/iugu/api_create.rb +18 -0
- data/lib/iugu/api_delete.rb +12 -0
- data/lib/iugu/api_fetch.rb +22 -0
- data/lib/iugu/api_request.rb +61 -0
- data/lib/iugu/api_resource.rb +42 -0
- data/lib/iugu/api_save.rb +12 -0
- data/lib/iugu/api_search.rb +13 -0
- data/lib/iugu/charge.rb +14 -0
- data/lib/iugu/customer.rb +21 -0
- data/lib/iugu/factory.rb +25 -0
- data/lib/iugu/invoice.rb +31 -0
- data/lib/iugu/object.rb +65 -0
- data/lib/iugu/payment_method.rb +15 -0
- data/lib/iugu/payment_token.rb +5 -0
- data/lib/iugu/plan.rb +13 -0
- data/lib/iugu/search_result.rb +16 -0
- data/lib/iugu/subscription.rb +58 -0
- data/lib/iugu/utils.rb +39 -0
- data/lib/iugu/version.rb +3 -0
- data/test/lib/iugu/customer_test.rb +76 -0
- data/test/lib/iugu/version_test.rb +7 -0
- data/test/test_helper.rb +3 -0
- metadata +133 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: bbb12186d82006d73d08e0986a3e9e6a9547fd25
|
4
|
+
data.tar.gz: a89824b98a9f56fba5baad537af2b43ae0cf81ac
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 85c5dbb9c3963dde745a31cba5f1cfb049b7a292e15fd1adf91aa7e889e1bba156d920b07a282dc1710fc156c66702de702b9ba9ed6a940f116583024360ce9b
|
7
|
+
data.tar.gz: 2930c1f6aae54e5f2eec60fa5bbeb9678297c56a29cd168aa79bd8cd5a9a7656f318bd170a85aa1f1af4fe01b697ae65e665b2da136c44c98e6251226cd365c6
|
data/.gitignore
ADDED
@@ -0,0 +1,56 @@
|
|
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
|
18
|
+
|
19
|
+
*.gem
|
20
|
+
.bundle
|
21
|
+
Gemfile.lock
|
22
|
+
pkg/*
|
23
|
+
.bundle
|
24
|
+
db/*.sqlite3
|
25
|
+
log/*.log
|
26
|
+
tmp/
|
27
|
+
.sass-cache/
|
28
|
+
coverage/
|
29
|
+
*~
|
30
|
+
*.lock
|
31
|
+
*.DS_Store
|
32
|
+
*.out
|
33
|
+
*.swp
|
34
|
+
.sass-cache/
|
35
|
+
.sass-cache/*
|
36
|
+
.sass-cache/**
|
37
|
+
tmp/
|
38
|
+
tmp/*/*
|
39
|
+
tmp/**
|
40
|
+
*/*.swp
|
41
|
+
db/schema.rb
|
42
|
+
public/system/*
|
43
|
+
public/system/**
|
44
|
+
log/*/*
|
45
|
+
log/**
|
46
|
+
**/log
|
47
|
+
*/*/log
|
48
|
+
*/*/*/log
|
49
|
+
spec/dummy/log/*/*
|
50
|
+
spec/dummy/log/**
|
51
|
+
spec/dummy/tmp/
|
52
|
+
spec/dummy/tmp/*/*
|
53
|
+
spec/dummy/tmp/**
|
54
|
+
*.sqlite3
|
55
|
+
public/assets/**
|
56
|
+
sandbox/public/assets/**
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 TODO: Write your name
|
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,57 @@
|
|
1
|
+
# Iugu
|
2
|
+
|
3
|
+
Gem para acesso a API da Iugu
|
4
|
+
|
5
|
+
## Instalação
|
6
|
+
|
7
|
+
Adicione essa linha ao Gemfile de sua aplicação:
|
8
|
+
|
9
|
+
gem 'iugu'
|
10
|
+
|
11
|
+
Depois execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Ou instale você mesmo com:
|
16
|
+
|
17
|
+
$ gem install iugu
|
18
|
+
|
19
|
+
## Exemplos de Uso
|
20
|
+
|
21
|
+
~~~
|
22
|
+
# Configure seu api key
|
23
|
+
Iugu.api_key = SEU_TOKEN_DE_API
|
24
|
+
|
25
|
+
# Exemplo de cobrança direta de CC em Ruby
|
26
|
+
Iugu::Charge.create(
|
27
|
+
{
|
28
|
+
"token"=> "TOKEN DO IUGU.JS ou LIB",
|
29
|
+
"email"=>"endereço do email do cliente",
|
30
|
+
"months"=>"quantidade de parcelas",
|
31
|
+
"items" =>
|
32
|
+
[
|
33
|
+
{
|
34
|
+
"description"=>"Item Teste",
|
35
|
+
"quantity"=>"1",
|
36
|
+
"price_cents"=>"1000"
|
37
|
+
}
|
38
|
+
]
|
39
|
+
}
|
40
|
+
)
|
41
|
+
|
42
|
+
# Exemplo de Gestão de Assinaturas em meia dúzia de linhas. Com direito a pagamento recorrente via Cartão ou Boleto. No caso de Cartão, recomenda-se vincular um token ao customer (Default Payment Method).
|
43
|
+
customer = Iugu::Customer.create({
|
44
|
+
"email"=>"EMAIL DO CLIENTE",
|
45
|
+
"name"=>"NOME DO CLIENTE"
|
46
|
+
})
|
47
|
+
|
48
|
+
subscription = Iugu::Subscription.create({
|
49
|
+
"plan_identifier" => "basic_plan", "customer_id" => customer.id
|
50
|
+
})
|
51
|
+
|
52
|
+
# Exemplo de Downgrade/Upgrade de Conta (Com cálculo automático de diferença de valores entre planos, créditos, etc)
|
53
|
+
subscription.change_plan( "novo_plano" );
|
54
|
+
|
55
|
+
# Histórico de Pagamentos do Cliente
|
56
|
+
customer.invoices
|
57
|
+
~~~
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/iugu.gemspec
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
lib = File.expand_path('../lib', __FILE__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
require 'iugu/version'
|
6
|
+
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = 'iugu-dev-wonder'
|
9
|
+
spec.version = Iugu::VERSION
|
10
|
+
spec.authors = ['Marcelo Paez Sequeira']
|
11
|
+
spec.email = ['marcelo@iugu.com']
|
12
|
+
spec.summary = 'Iugu API pointing to a development/test api'
|
13
|
+
spec.homepage = 'https://iugu.com'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ['lib']
|
20
|
+
|
21
|
+
spec.add_dependency 'rest-client'
|
22
|
+
spec.add_dependency 'dotenv-rails'
|
23
|
+
|
24
|
+
spec.add_development_dependency 'bundler', '~> 1.5'
|
25
|
+
spec.add_development_dependency 'rake'
|
26
|
+
end
|
data/lib/iugu.rb
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
require_relative 'iugu/version'
|
2
|
+
require_relative 'iugu/utils'
|
3
|
+
require_relative 'iugu/api_request'
|
4
|
+
require_relative 'iugu/object'
|
5
|
+
require_relative 'iugu/api_resource'
|
6
|
+
require_relative 'iugu/api_child_resource'
|
7
|
+
require_relative 'iugu/factory'
|
8
|
+
require_relative 'iugu/search_result'
|
9
|
+
require_relative 'iugu/api_fetch'
|
10
|
+
require_relative 'iugu/api_create'
|
11
|
+
require_relative 'iugu/api_save'
|
12
|
+
require_relative 'iugu/api_delete'
|
13
|
+
require_relative 'iugu/api_search'
|
14
|
+
require_relative 'iugu/customer'
|
15
|
+
require_relative 'iugu/payment_method'
|
16
|
+
require_relative 'iugu/payment_token'
|
17
|
+
require_relative 'iugu/invoice'
|
18
|
+
require_relative 'iugu/subscription'
|
19
|
+
require_relative 'iugu/charge'
|
20
|
+
require_relative 'iugu/plan'
|
21
|
+
|
22
|
+
module Iugu
|
23
|
+
class AuthenticationException < StandardError
|
24
|
+
end
|
25
|
+
|
26
|
+
class RequestFailed < StandardError
|
27
|
+
end
|
28
|
+
|
29
|
+
class ObjectNotFound < StandardError
|
30
|
+
end
|
31
|
+
|
32
|
+
class RequestWithErrors < StandardError
|
33
|
+
attr_accessor :errors
|
34
|
+
|
35
|
+
def initialize(errors)
|
36
|
+
@errors = errors
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
class << self
|
41
|
+
attr_accessor :api_key
|
42
|
+
end
|
43
|
+
|
44
|
+
@api_version = 'v1'
|
45
|
+
@endpoint = ENV.fetch('IUGU_API_URL', 'https://api.iugu.com')
|
46
|
+
|
47
|
+
def self.base_uri
|
48
|
+
"#{@endpoint}/#{@api_version}/"
|
49
|
+
end
|
50
|
+
|
51
|
+
def self.endpoint=(endpoint)
|
52
|
+
@endpoint = endpoint
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module Iugu
|
2
|
+
class APIChildResource
|
3
|
+
@parent_keys = {}
|
4
|
+
@fabricator = nil
|
5
|
+
|
6
|
+
def initialize(parent_keys = {}, fabricator)
|
7
|
+
@parent_keys = parent_keys
|
8
|
+
@fabricator = fabricator
|
9
|
+
end
|
10
|
+
|
11
|
+
def create(attributes = {})
|
12
|
+
@fabricator.send "create", merge_params(attributes)
|
13
|
+
end
|
14
|
+
|
15
|
+
def search(options = {})
|
16
|
+
@fabricator.send "search", merge_params(options)
|
17
|
+
end
|
18
|
+
|
19
|
+
def fetch(options = nil)
|
20
|
+
@fabricator.send "fetch", merge_params({ id: options })
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def merge_params(attributes)
|
26
|
+
@parent_keys.merge attributes
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module Iugu
|
2
|
+
module APICreate
|
3
|
+
module ClassMethods
|
4
|
+
def create(attributes = {})
|
5
|
+
Iugu::Factory.create_from_response self.object_type, APIRequest.request("POST", self.url(attributes), attributes)
|
6
|
+
rescue Iugu::RequestWithErrors => ex
|
7
|
+
obj = self.new
|
8
|
+
obj.set_attributes attributes, true
|
9
|
+
obj.errors = ex.errors
|
10
|
+
obj
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.included(base)
|
15
|
+
base.extend(ClassMethods)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module Iugu
|
2
|
+
module APIFetch
|
3
|
+
def refresh
|
4
|
+
copy Iugu::Factory.create_from_response(self.class.object_type, APIRequest.request("GET", self.class.url(self.id)))
|
5
|
+
self.errors = nil
|
6
|
+
true
|
7
|
+
rescue Iugu::RequestWithErrors => ex
|
8
|
+
self.errors = ex.errors
|
9
|
+
false
|
10
|
+
end
|
11
|
+
|
12
|
+
module ClassMethods
|
13
|
+
def fetch(options = nil)
|
14
|
+
Iugu::Factory.create_from_response self.object_type, APIRequest.request("GET", self.url(options))
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.included(base)
|
19
|
+
base.extend(ClassMethods)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
#encoding: UTF-8
|
2
|
+
|
3
|
+
require 'rest_client'
|
4
|
+
require "base64"
|
5
|
+
require "json"
|
6
|
+
|
7
|
+
module Iugu
|
8
|
+
class APIRequest
|
9
|
+
|
10
|
+
def self.request(method, url, data = {})
|
11
|
+
Iugu::Utils.auth_from_env if Iugu.api_key.nil?
|
12
|
+
raise Iugu::AuthenticationException, "Chave de API não configurada. Utilize Iugu.api_key = ... para configurar." if Iugu.api_key.nil?
|
13
|
+
handle_response self.send_request method, url, data
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def self.send_request(method, url, data)
|
19
|
+
RestClient::Request.execute build_request(method, url, data)
|
20
|
+
rescue RestClient::ResourceNotFound
|
21
|
+
raise ObjectNotFound
|
22
|
+
rescue RestClient::UnprocessableEntity => ex
|
23
|
+
raise RequestWithErrors.new JSON.parse(ex.response)['errors']
|
24
|
+
rescue RestClient::BadRequest => ex
|
25
|
+
raise RequestWithErrors.new JSON.parse(ex.response)['errors']
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.build_request(method, url, data)
|
29
|
+
{
|
30
|
+
verify_ssl: true,
|
31
|
+
headers: default_headers,
|
32
|
+
method: method,
|
33
|
+
payload: data.to_json,
|
34
|
+
url: url,
|
35
|
+
timeout: 30
|
36
|
+
}
|
37
|
+
end
|
38
|
+
|
39
|
+
def self.handle_response(response)
|
40
|
+
response_json = JSON.parse(response.body)
|
41
|
+
raise ObjectNotFound if response_json.is_a?(Hash) && response_json['errors'] == "Not Found"
|
42
|
+
raise RequestWithErrors, response_json['errors'] if response_json.is_a?(Hash) && response_json['errors'] && response_json['errors'].length > 0
|
43
|
+
response_json
|
44
|
+
rescue JSON::ParserError
|
45
|
+
raise RequestFailed
|
46
|
+
end
|
47
|
+
|
48
|
+
def self.default_headers
|
49
|
+
{
|
50
|
+
authorization: 'Basic ' + Base64.encode64(Iugu.api_key + ":"),
|
51
|
+
accept: 'application/json',
|
52
|
+
accept_charset: 'utf-8',
|
53
|
+
user_agent: 'Iugu RubyLibrary',
|
54
|
+
accept_language: 'pt-br;q=0.9,pt-BR',
|
55
|
+
#content_type: 'application/x-www-form-urlencoded; charset=utf-8'
|
56
|
+
content_type: 'application/json; charset=utf-8'
|
57
|
+
}
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
module Iugu
|
2
|
+
class APIResource < Iugu::Object
|
3
|
+
def self.url(options = nil)
|
4
|
+
endpoint_url + self.relative_url(options)
|
5
|
+
end
|
6
|
+
|
7
|
+
def is_new?
|
8
|
+
@attributes['id'].nil?
|
9
|
+
end
|
10
|
+
|
11
|
+
protected
|
12
|
+
|
13
|
+
def self.object_type
|
14
|
+
Iugu::Utils.underscore self.name.to_s.split('::')[-1]
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.endpoint_url
|
18
|
+
Iugu.base_uri + object_base_uri
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.relative_url(options = "")
|
22
|
+
if options.is_a?(Hash)
|
23
|
+
id = options[:id] || options["id"]
|
24
|
+
elsif options.is_a?(Iugu::APIResource)
|
25
|
+
id = options.id
|
26
|
+
else
|
27
|
+
id = options
|
28
|
+
end
|
29
|
+
id ? "/#{id}" : ""
|
30
|
+
end
|
31
|
+
|
32
|
+
def self.object_base_uri
|
33
|
+
pluralized_models = ["customer", "payment_method", "invoice", "subscription", "plan"]
|
34
|
+
if pluralized_models.include? self.object_type
|
35
|
+
object_type = self.object_type + "s"
|
36
|
+
else
|
37
|
+
object_type = self.object_type
|
38
|
+
end
|
39
|
+
object_type
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
module Iugu
|
2
|
+
module APISave
|
3
|
+
def save
|
4
|
+
copy Iugu::Factory.create_from_response(self.class.object_type, APIRequest.request(is_new? ? "POST" : "PUT", self.class.url(self.attributes), modified_attributes))
|
5
|
+
self.errors = nil
|
6
|
+
true
|
7
|
+
rescue Iugu::RequestWithErrors => ex
|
8
|
+
self.errors = ex.errors
|
9
|
+
false
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module Iugu
|
2
|
+
module APICreate
|
3
|
+
module ClassMethods
|
4
|
+
def search(options = {})
|
5
|
+
Iugu::Factory.create_from_response self.object_type, APIRequest.request("GET", self.url(options), options)
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.included(base)
|
10
|
+
base.extend(ClassMethods)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
data/lib/iugu/charge.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
module Iugu
|
2
|
+
class Customer < APIResource
|
3
|
+
include Iugu::APIFetch
|
4
|
+
include Iugu::APICreate
|
5
|
+
include Iugu::APISave
|
6
|
+
include Iugu::APIDelete
|
7
|
+
|
8
|
+
def payment_methods
|
9
|
+
APIChildResource.new({ customer_id: self.id }, Iugu::PaymentMethod)
|
10
|
+
end
|
11
|
+
|
12
|
+
def invoices
|
13
|
+
APIChildResource.new({ customer_id: self.id }, Iugu::Invoice)
|
14
|
+
end
|
15
|
+
|
16
|
+
def default_payment_method
|
17
|
+
return false unless @attributes['default_payment_method_id']
|
18
|
+
PaymentMethod.fetch({ id: @attributes['default_payment_method_id'], customer_id: self.id })
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
data/lib/iugu/factory.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
module Iugu
|
2
|
+
class Factory
|
3
|
+
def self.create_from_response(object_type, response, errors = nil)
|
4
|
+
if response.nil?
|
5
|
+
obj = Iugu.const_get(Iugu::Utils.camelize(object_type)).new
|
6
|
+
obj.errors = errors if errors
|
7
|
+
obj
|
8
|
+
elsif response.is_a?(Array)
|
9
|
+
results = []
|
10
|
+
response.each do |i|
|
11
|
+
results.push Iugu.const_get(Iugu::Utils.camelize(object_type)).new i
|
12
|
+
end
|
13
|
+
Iugu::SearchResult.new results, results.count
|
14
|
+
elsif response['items'] && response['totalItems']
|
15
|
+
results = []
|
16
|
+
response['items'].each do |v|
|
17
|
+
results.push self.create_from_response(object_type, v)
|
18
|
+
end
|
19
|
+
Iugu::SearchResult.new results, response['totalItems']
|
20
|
+
else
|
21
|
+
Iugu.const_get(Iugu::Utils.camelize(object_type)).new response
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
data/lib/iugu/invoice.rb
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
module Iugu
|
2
|
+
class Invoice < APIResource
|
3
|
+
include Iugu::APIFetch
|
4
|
+
include Iugu::APICreate
|
5
|
+
include Iugu::APISave
|
6
|
+
include Iugu::APIDelete
|
7
|
+
|
8
|
+
def customer
|
9
|
+
return false unless @attributes["customer_id"]
|
10
|
+
Customer.fetch @attributes["customer_id"]
|
11
|
+
end
|
12
|
+
|
13
|
+
def cancel
|
14
|
+
copy Iugu::Factory.create_from_response(self.class.object_type, APIRequest.request("PUT", "#{self.class.url(self.id)}/cancel"))
|
15
|
+
self.errors = nil
|
16
|
+
true
|
17
|
+
rescue Iugu::RequestWithErrors => ex
|
18
|
+
self.errors = ex.errors
|
19
|
+
false
|
20
|
+
end
|
21
|
+
|
22
|
+
def refund
|
23
|
+
copy Iugu::Factory.create_from_response(self.class.object_type, APIRequest.request("POST", "#{self.class.url(self.id)}/refund"))
|
24
|
+
self.errors = nil
|
25
|
+
true
|
26
|
+
rescue Iugu::RequestWithErrors => ex
|
27
|
+
self.errors = ex.errors
|
28
|
+
false
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
data/lib/iugu/object.rb
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
require 'set'
|
2
|
+
|
3
|
+
module Iugu
|
4
|
+
class Object
|
5
|
+
|
6
|
+
attr_accessor :errors
|
7
|
+
|
8
|
+
undef :id if method_defined?(:id)
|
9
|
+
|
10
|
+
def initialize(attributes = {})
|
11
|
+
@unsaved_attributes = Set.new
|
12
|
+
set_attributes attributes
|
13
|
+
end
|
14
|
+
|
15
|
+
def add_accessor(name)
|
16
|
+
singleton_class.class_eval do
|
17
|
+
define_method(name.to_s) { self.attributes[name.to_s] }
|
18
|
+
define_method(name.to_s + "=") do |value|
|
19
|
+
self.attributes[name.to_s] = value
|
20
|
+
self.unsaved_attributes.add name.to_s
|
21
|
+
end unless name.to_s == 'id'
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def method_missing(name, *args)
|
26
|
+
return super unless name.to_s.end_with? '='
|
27
|
+
return super if name.to_s.start_with? 'id'
|
28
|
+
add_accessor(name.to_s[0...-1])
|
29
|
+
return send(name, args[0])
|
30
|
+
end
|
31
|
+
|
32
|
+
def unsaved_attributes
|
33
|
+
@unsaved_attributes
|
34
|
+
end
|
35
|
+
|
36
|
+
def attributes
|
37
|
+
@attributes
|
38
|
+
end
|
39
|
+
|
40
|
+
def modified_attributes
|
41
|
+
Iugu::Utils.intersect @unsaved_attributes, @attributes
|
42
|
+
end
|
43
|
+
|
44
|
+
def copy(object)
|
45
|
+
@unsaved_attributes = Set.new
|
46
|
+
@attributes = {}
|
47
|
+
set_attributes object.attributes
|
48
|
+
end
|
49
|
+
|
50
|
+
def set_attributes(attributes, unsaved = false)
|
51
|
+
@attributes = Iugu::Utils.stringify_keys(attributes)
|
52
|
+
@attributes.each do |k, v|
|
53
|
+
add_accessor(k)
|
54
|
+
end
|
55
|
+
@unsaved_attributes = @attributes.keys.to_set if unsaved
|
56
|
+
end
|
57
|
+
|
58
|
+
protected
|
59
|
+
|
60
|
+
def metaclass
|
61
|
+
class << self; self; end
|
62
|
+
end
|
63
|
+
|
64
|
+
end
|
65
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module Iugu
|
2
|
+
class PaymentMethod < APIResource
|
3
|
+
include Iugu::APIFetch
|
4
|
+
include Iugu::APICreate
|
5
|
+
include Iugu::APISave
|
6
|
+
include Iugu::APIDelete
|
7
|
+
|
8
|
+
def self.url(options = {})
|
9
|
+
customer_id = options[:customer_id] || options["customer_id"]
|
10
|
+
id = options[:id] || options["id"]
|
11
|
+
raise StandardError, "Missing Customer ID" unless customer_id
|
12
|
+
"#{Customer.url customer_id}/#{self.object_base_uri}" + self.relative_url(id)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
data/lib/iugu/plan.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
module Iugu
|
2
|
+
class Plan < APIResource
|
3
|
+
include Iugu::APIFetch
|
4
|
+
include Iugu::APICreate
|
5
|
+
include Iugu::APISave
|
6
|
+
include Iugu::APIDelete
|
7
|
+
|
8
|
+
def self.fetch_by_identifier(identifier)
|
9
|
+
Iugu::Factory.create_from_response(object_type, APIRequest.request("GET", "#{url}/identifier/#{identifier}"))
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
module Iugu
|
2
|
+
class Subscription < APIResource
|
3
|
+
include Iugu::APIFetch
|
4
|
+
include Iugu::APICreate
|
5
|
+
include Iugu::APISave
|
6
|
+
include Iugu::APIDelete
|
7
|
+
|
8
|
+
def add_credits(quantity)
|
9
|
+
copy Iugu::Factory.create_from_response(self.class.object_type, APIRequest.request("PUT", "#{self.class.url(self.id)}/add_credits", { quantity: quantity }))
|
10
|
+
self.errors = nil
|
11
|
+
true
|
12
|
+
rescue Iugu::RequestWithErrors => ex
|
13
|
+
self.errors = ex.errors
|
14
|
+
false
|
15
|
+
end
|
16
|
+
|
17
|
+
def remove_credits(quantity)
|
18
|
+
copy Iugu::Factory.create_from_response(self.class.object_type, APIRequest.request("PUT", "#{self.class.url(self.id)}/remove_credits", { quantity: quantity }))
|
19
|
+
self.errors = nil
|
20
|
+
true
|
21
|
+
rescue Iugu::RequestWithErrors => ex
|
22
|
+
self.errors = ex.errors
|
23
|
+
false
|
24
|
+
end
|
25
|
+
|
26
|
+
def suspend
|
27
|
+
copy Iugu::Factory.create_from_response(self.class.object_type, APIRequest.request("POST", "#{self.class.url(self.id)}/suspend"))
|
28
|
+
self.errors = nil
|
29
|
+
true
|
30
|
+
rescue Iugu::RequestWithErrors => ex
|
31
|
+
self.errors = ex.errors
|
32
|
+
false
|
33
|
+
end
|
34
|
+
|
35
|
+
def activate
|
36
|
+
copy Iugu::Factory.create_from_response(self.class.object_type, APIRequest.request("POST", "#{self.class.url(self.id)}/activate"))
|
37
|
+
self.errors = nil
|
38
|
+
true
|
39
|
+
rescue Iugu::RequestWithErrors => ex
|
40
|
+
self.errors = ex.errors
|
41
|
+
false
|
42
|
+
end
|
43
|
+
|
44
|
+
def change_plan(plan_identifier)
|
45
|
+
copy Iugu::Factory.create_from_response(self.class.object_type, APIRequest.request("POST", "#{self.class.url(self.id)}/change_plan/#{plan_identifier}"))
|
46
|
+
self.errors = nil
|
47
|
+
true
|
48
|
+
rescue Iugu::RequestWithErrors => ex
|
49
|
+
self.errors = ex.errors
|
50
|
+
false
|
51
|
+
end
|
52
|
+
|
53
|
+
def customer
|
54
|
+
return false unless @attributes['customer_id']
|
55
|
+
Customer.fetch @attributes['customer_id']
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
data/lib/iugu/utils.rb
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
module Iugu
|
2
|
+
class Utils
|
3
|
+
def self.auth_from_env
|
4
|
+
api_key = ENV['IUGU_API_KEY']
|
5
|
+
Iugu.api_key = api_key if api_key
|
6
|
+
end
|
7
|
+
|
8
|
+
def self.intersect(array, another_hash)
|
9
|
+
keys_intersection = array & another_hash.keys
|
10
|
+
intersection = {}
|
11
|
+
keys_intersection.each {|k| intersection[k] = another_hash[k]}
|
12
|
+
intersection
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.underscore(string)
|
16
|
+
string.gsub(/::/, '/').
|
17
|
+
gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
|
18
|
+
gsub(/([a-z\d])([A-Z])/,'\1_\2').
|
19
|
+
tr("-", "_").
|
20
|
+
downcase
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.camelize(string)
|
24
|
+
string.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.stringify_keys(hash)
|
28
|
+
new_hash = {}
|
29
|
+
hash.each do |key, value|
|
30
|
+
if value.is_a? Hash
|
31
|
+
new_hash[key.to_s] = stringify_keys(value)
|
32
|
+
else
|
33
|
+
new_hash[key.to_s] = value
|
34
|
+
end
|
35
|
+
end
|
36
|
+
new_hash
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
data/lib/iugu/version.rb
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
require_relative '../../test_helper'
|
2
|
+
|
3
|
+
describe Iugu::Customer do
|
4
|
+
it "must be defined" do
|
5
|
+
Iugu::Customer.wont_be_nil
|
6
|
+
end
|
7
|
+
|
8
|
+
=begin
|
9
|
+
|
10
|
+
describe "fetching customers" do
|
11
|
+
describe "using a correct customer id" do
|
12
|
+
before do
|
13
|
+
@customer = Iugu::Customer.create({ email: "teste@teste.com", name: "Nome do Cliente" })
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should return a customer object" do
|
17
|
+
@customer.name = "NEW NAME"
|
18
|
+
assert @customer.save
|
19
|
+
end
|
20
|
+
|
21
|
+
after do
|
22
|
+
@customer.delete
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
describe "fetching customers" do
|
28
|
+
describe "using a correct customer id" do
|
29
|
+
before do
|
30
|
+
@customer = Iugu::Customer.create({ email: "teste@teste.com", name: "Nome do Cliente" })
|
31
|
+
@fetched_customer = Iugu::Customer.fetch @customer.id
|
32
|
+
end
|
33
|
+
|
34
|
+
it "should return a customer object" do
|
35
|
+
assert @fetched_customer.is_a? Iugu::Customer
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should have an id" do
|
39
|
+
assert @fetched_customer.id && @fetched_customer.id.length > 0
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
describe "using a wrong customer id" do
|
44
|
+
it "should raise not found exception" do
|
45
|
+
proc { @fetched_customer = Iugu::Customer.fetch "BLABLABLA" }.must_raise Iugu::ObjectNotFound
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
describe "creating customers" do
|
51
|
+
before do
|
52
|
+
@customer = Iugu::Customer.create({ email: "teste@teste.com", name: "Nome do Cliente" })
|
53
|
+
end
|
54
|
+
|
55
|
+
describe "using correct data" do
|
56
|
+
it "should return a customer object" do
|
57
|
+
assert @customer.is_a? Iugu::Customer
|
58
|
+
end
|
59
|
+
|
60
|
+
it "should have an id" do
|
61
|
+
assert @customer.id && @customer.id.length > 0
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
describe "deleting customers" do
|
67
|
+
before do
|
68
|
+
@customer = Iugu::Customer.create({ email: "teste@teste.com", name: "Nome do Cliente" })
|
69
|
+
end
|
70
|
+
|
71
|
+
it "should return true" do
|
72
|
+
assert @customer.delete
|
73
|
+
end
|
74
|
+
end
|
75
|
+
=end
|
76
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,133 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: iugu-dev-wonder
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.9
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Marcelo Paez Sequeira
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-06-30 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rest-client
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: dotenv-rails
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.5'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.5'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description:
|
70
|
+
email:
|
71
|
+
- marcelo@iugu.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- Gemfile
|
78
|
+
- LICENSE.txt
|
79
|
+
- README.md
|
80
|
+
- Rakefile
|
81
|
+
- iugu.gemspec
|
82
|
+
- lib/iugu.rb
|
83
|
+
- lib/iugu/api_child_resource.rb
|
84
|
+
- lib/iugu/api_create.rb
|
85
|
+
- lib/iugu/api_delete.rb
|
86
|
+
- lib/iugu/api_fetch.rb
|
87
|
+
- lib/iugu/api_request.rb
|
88
|
+
- lib/iugu/api_resource.rb
|
89
|
+
- lib/iugu/api_save.rb
|
90
|
+
- lib/iugu/api_search.rb
|
91
|
+
- lib/iugu/charge.rb
|
92
|
+
- lib/iugu/customer.rb
|
93
|
+
- lib/iugu/factory.rb
|
94
|
+
- lib/iugu/invoice.rb
|
95
|
+
- lib/iugu/object.rb
|
96
|
+
- lib/iugu/payment_method.rb
|
97
|
+
- lib/iugu/payment_token.rb
|
98
|
+
- lib/iugu/plan.rb
|
99
|
+
- lib/iugu/search_result.rb
|
100
|
+
- lib/iugu/subscription.rb
|
101
|
+
- lib/iugu/utils.rb
|
102
|
+
- lib/iugu/version.rb
|
103
|
+
- test/lib/iugu/customer_test.rb
|
104
|
+
- test/lib/iugu/version_test.rb
|
105
|
+
- test/test_helper.rb
|
106
|
+
homepage: https://iugu.com
|
107
|
+
licenses:
|
108
|
+
- MIT
|
109
|
+
metadata: {}
|
110
|
+
post_install_message:
|
111
|
+
rdoc_options: []
|
112
|
+
require_paths:
|
113
|
+
- lib
|
114
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
115
|
+
requirements:
|
116
|
+
- - ">="
|
117
|
+
- !ruby/object:Gem::Version
|
118
|
+
version: '0'
|
119
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
120
|
+
requirements:
|
121
|
+
- - ">="
|
122
|
+
- !ruby/object:Gem::Version
|
123
|
+
version: '0'
|
124
|
+
requirements: []
|
125
|
+
rubyforge_project:
|
126
|
+
rubygems_version: 2.6.12
|
127
|
+
signing_key:
|
128
|
+
specification_version: 4
|
129
|
+
summary: Iugu API pointing to a development/test api
|
130
|
+
test_files:
|
131
|
+
- test/lib/iugu/customer_test.rb
|
132
|
+
- test/lib/iugu/version_test.rb
|
133
|
+
- test/test_helper.rb
|