moip2 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (66) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +18 -0
  3. data/.rspec +4 -0
  4. data/.ruby-gemset +1 -0
  5. data/.ruby-version +1 -0
  6. data/Gemfile +4 -0
  7. data/Guardfile +9 -0
  8. data/LICENSE.txt +22 -0
  9. data/README.md +129 -0
  10. data/Rakefile +1 -0
  11. data/lib/moip2/api.rb +24 -0
  12. data/lib/moip2/auth/basic.rb +19 -0
  13. data/lib/moip2/auth/oauth.rb +18 -0
  14. data/lib/moip2/client.rb +107 -0
  15. data/lib/moip2/customer_api.rb +24 -0
  16. data/lib/moip2/exceptions/invalid_enviroment_error.rb +6 -0
  17. data/lib/moip2/invoice_api.rb +32 -0
  18. data/lib/moip2/keys_api.rb +17 -0
  19. data/lib/moip2/multi_order_api.rb +23 -0
  20. data/lib/moip2/multi_payment_api.rb +17 -0
  21. data/lib/moip2/order_api.rb +25 -0
  22. data/lib/moip2/payment_api.rb +15 -0
  23. data/lib/moip2/resource/customer.rb +16 -0
  24. data/lib/moip2/resource/invoice.rb +14 -0
  25. data/lib/moip2/resource/keys.rb +14 -0
  26. data/lib/moip2/resource/multi_order.rb +25 -0
  27. data/lib/moip2/resource/multi_payment.rb +11 -0
  28. data/lib/moip2/resource/order.rb +25 -0
  29. data/lib/moip2/resource/payment.rb +13 -0
  30. data/lib/moip2/response.rb +20 -0
  31. data/lib/moip2/version.rb +3 -0
  32. data/lib/moip2.rb +74 -0
  33. data/moip2.gemspec +30 -0
  34. data/spec/moip2/api_spec.rb +25 -0
  35. data/spec/moip2/auth/basic_spec.rb +9 -0
  36. data/spec/moip2/auth/oauth_spec.rb +9 -0
  37. data/spec/moip2/client_spec.rb +133 -0
  38. data/spec/moip2/customer_api_spec.rb +152 -0
  39. data/spec/moip2/invoice_spec.rb +95 -0
  40. data/spec/moip2/keys_spec.rb +19 -0
  41. data/spec/moip2/multi_order_api_spec.rb +198 -0
  42. data/spec/moip2/order_api_spec.rb +138 -0
  43. data/spec/moip2/payment_api_spec.rb +43 -0
  44. data/spec/moip2/resource/order_spec.rb +47 -0
  45. data/spec/moip2/response_spec.rb +51 -0
  46. data/spec/moip2_spec.rb +50 -0
  47. data/spec/spec_helper.rb +113 -0
  48. data/vcr_cassettes/create_customer.yml +43 -0
  49. data/vcr_cassettes/create_customer_with_funding_instrument.yml +45 -0
  50. data/vcr_cassettes/create_invoice.yml +42 -0
  51. data/vcr_cassettes/create_mulit_order_success.yml +142 -0
  52. data/vcr_cassettes/create_multi_order_fail.yml +43 -0
  53. data/vcr_cassettes/create_order_fail.yml +43 -0
  54. data/vcr_cassettes/create_order_success.yml +47 -0
  55. data/vcr_cassettes/create_payment_success.yml +39 -0
  56. data/vcr_cassettes/get_customer.yml +42 -0
  57. data/vcr_cassettes/get_invoice.yml +41 -0
  58. data/vcr_cassettes/get_keys.yml +42 -0
  59. data/vcr_cassettes/list_invoices.yml +43 -0
  60. data/vcr_cassettes/show_multi_order.yml +132 -0
  61. data/vcr_cassettes/show_multi_order_not_found.yml +40 -0
  62. data/vcr_cassettes/show_order.yml +43 -0
  63. data/vcr_cassettes/show_order_not_found.yml +38 -0
  64. data/vcr_cassettes/update_invoice.yml +41 -0
  65. data/wercker.yml +48 -0
  66. metadata +238 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ea24ded7f8ad4ce61b397ed6fa301330d4c0e9ee
4
+ data.tar.gz: 055511449cfd2ab0e7b1a62c2bc0559121d23032
5
+ SHA512:
6
+ metadata.gz: abf681efbb30e56584aa53835abf971b42f5507d8a520473a843de679855a510db40dbf3bf0ace212b669c06b22cbd82358121b6318ab25173e34ce681fb614e
7
+ data.tar.gz: e877e1686a986ee1de201c3680b8730370cfcaccf0b9ff0a2b03953b7d3b796da6a08f7c750137f06bbc2c44615e150f5877417f81459be774c7ebe95348ed92
data/.gitignore ADDED
@@ -0,0 +1,18 @@
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
+ .DS_Store
data/.rspec ADDED
@@ -0,0 +1,4 @@
1
+ --color
2
+ --warnings
3
+ --require spec_helper
4
+ --format d
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ moip2
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ ruby-2.2.0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in moip2.gemspec
4
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,9 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard :rspec do
5
+ watch(%r{^spec/.+_spec\.rb$})
6
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
7
+ watch('spec/spec_helper.rb') { "spec" }
8
+ end
9
+
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,129 @@
1
+ # Moip v2 Ruby SDK
2
+
3
+ [![Circle CI](https://circleci.com/gh/moip/moip-sdk-ruby.svg?style=svg)](https://circleci.com/gh/moip/moip-sdk-ruby)
4
+
5
+ O jeito mais simples e rápido de integrar o Moip a sua aplicação Ruby
6
+
7
+ ## Instalação
8
+
9
+ Adicione a seguinte linha no seu Gemfile:
10
+
11
+ ```ruby
12
+ gem "moip2"
13
+ ```
14
+
15
+ ## Configurando sua autenticação
16
+ - Autenticando por BasicAuth
17
+ ```ruby
18
+ auth = Moip2::Auth::Basic.new("TOKEN", "SECRET")
19
+ ```
20
+ - Autenticando por OAuth
21
+ ```ruby
22
+ auth = Moip2::Auth::OAuth.new("TOKEN_OAUTH")
23
+ ```
24
+
25
+ Após deifinir o tipo de autenticação, é necessário gerar o client, informando em qual environment você quer executar suas ações:
26
+ ```ruby
27
+ client = Moip2::Client.new(:sandbox/:production, auth)
28
+ ```
29
+
30
+ ## Criando um Pedido
31
+
32
+ Agora basta criar o pedido:
33
+
34
+ ```ruby
35
+ order = Moip2::OrderApi.new(client).create(
36
+ {
37
+ own_id: "ruby_sdk_1",
38
+ items: [
39
+ {
40
+ product: "Nome do produto",
41
+ quantity: 1,
42
+ detail: "Mais info...",
43
+ price: 1000
44
+ }
45
+ ],
46
+ customer: {
47
+ own_id: "ruby_sdk_customer_1",
48
+ fullname: "Jose da Silva",
49
+ email: "sandbox_v2_1401147277@email.com",
50
+ birthdate: "1988-12-30",
51
+ tax_document: { number: "33333333333", type: "CPF" },
52
+ phone: { country_code: "55", area_code: "11", number: "66778899" },
53
+ shipping_address:
54
+ {
55
+ street: "Avenida Faria Lima",
56
+ street_number: 2927,
57
+ complement: 8,
58
+ district: "Itaim",
59
+ city: "Sao Paulo",
60
+ state: "SP",
61
+ country: "BRA",
62
+ zip_code: "01234000"
63
+ }
64
+ }
65
+ }
66
+ )
67
+ ```
68
+
69
+ ## Criando um pagamento
70
+
71
+ ### Cartão de crédito
72
+
73
+ ```ruby
74
+ Moip2::PaymentApi.new(client).create(order.id,
75
+ {
76
+ installment_count: 1,
77
+ funding_instrument: {
78
+ method: "CREDIT_CARD",
79
+ credit_card: {
80
+ expiration_month: 04,
81
+ expiration_year: 18,
82
+ number: "4012001038443335",
83
+ cvc: "123",
84
+ holder: {
85
+ fullname: "Jose Portador da Silva",
86
+ birthdate: "1988-10-10",
87
+ tax_document: {
88
+ type: "CPF",
89
+ number: "22222222222"
90
+ },
91
+ phone: {
92
+ country_code: "55",
93
+ area_code: "11",
94
+ number: "55667788"
95
+ }
96
+ }
97
+ }
98
+ }
99
+ }
100
+ )
101
+ ```
102
+
103
+ ### Boleto
104
+
105
+ ```ruby
106
+ Moip2::PaymentApi.new(client).create(
107
+ {
108
+ funding_instrument: {
109
+ method: "BOLETO",
110
+ boleto: {
111
+ expiration_date: "2015-09-30",
112
+ instruction_lines: {
113
+ first: "Primeira linha do boleto",
114
+ second: "Segunda linha do boleto",
115
+ third: "Terceira linha do boleto"
116
+ },
117
+ logo_uri: "https://"
118
+ }
119
+ }
120
+ }
121
+ )
122
+ ```
123
+ ## Documentação
124
+
125
+ [Documentação oficial](https://moip.com.br/referencia-api/)
126
+
127
+ ## Licença
128
+
129
+ [The MIT License](https://github.com/moip/moip-sdk-ruby/blob/master/LICENSE.txt)
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/lib/moip2/api.rb ADDED
@@ -0,0 +1,24 @@
1
+ module Moip2
2
+
3
+ class Api
4
+ attr_reader :client
5
+
6
+ def initialize(client)
7
+ @client = client
8
+ end
9
+
10
+ def order
11
+ Moip2::OrderApi.new(client)
12
+ end
13
+
14
+ def payment
15
+ Moip2::PaymentApi.new(client)
16
+ end
17
+
18
+ def invoice
19
+ Moip2::InvoiceApi.new client
20
+ end
21
+
22
+ end
23
+
24
+ end
@@ -0,0 +1,19 @@
1
+ require "base64"
2
+
3
+ module Moip2
4
+ module Auth
5
+
6
+ class Basic
7
+
8
+ def initialize(token, secret)
9
+ @token, @secret = token, secret
10
+ end
11
+
12
+ def header
13
+ %(Basic #{Base64.strict_encode64("#{@token}:#{@secret}")})
14
+ end
15
+
16
+ end
17
+
18
+ end
19
+ end
@@ -0,0 +1,18 @@
1
+ module Moip2
2
+ module Auth
3
+
4
+ class OAuth
5
+ def initialize(oauth)
6
+ @oauth = oauth
7
+ end
8
+
9
+ def header
10
+ return @oauth if @oauth.start_with? "OAuth"
11
+
12
+ %(OAuth #{@oauth})
13
+ end
14
+
15
+ end
16
+
17
+ end
18
+ end
@@ -0,0 +1,107 @@
1
+ module Moip2
2
+
3
+ class Client
4
+ include HTTParty
5
+
6
+ attr_reader :env, :auth, :uri
7
+
8
+ def initialize(env = :sandbox, auth = nil, opts = {})
9
+ @env, @auth, @opts = env.to_sym, auth, opts
10
+
11
+ @uri = get_base_uri
12
+ self.class.base_uri @uri
13
+ end
14
+
15
+ def sandbox?
16
+ env == :sandbox
17
+ end
18
+
19
+ def production?
20
+ env == :production
21
+ end
22
+
23
+ def opts
24
+ opts = @opts
25
+ opts[:headers] ||= {}
26
+
27
+ opts[:headers].merge!(
28
+ {
29
+ "Content-Type" => "application/json",
30
+ "Authorization" => auth.header
31
+ }
32
+ )
33
+
34
+ opts
35
+ end
36
+
37
+ def post(path, resource)
38
+ options = opts().merge(body: convert_hash_keys_to(:camel_case, resource).to_json)
39
+ resp = self.class.post path, options
40
+
41
+ create_response resp
42
+ end
43
+
44
+ def put(path, resource)
45
+ options = opts().merge(body: convert_hash_keys_to(:camel_case, resource).to_json)
46
+ resp = self.class.put path, options
47
+
48
+ create_response resp
49
+ end
50
+
51
+ def get(path)
52
+ resp = self.class.get path, opts()
53
+
54
+ create_response resp
55
+ end
56
+
57
+ private
58
+
59
+ def get_base_uri
60
+ return ENV["base_uri"] if ENV["base_uri"]
61
+
62
+ if production?
63
+ "https://api.moip.com.br"
64
+ else
65
+ "https://test.moip.com.br"
66
+ end
67
+
68
+ end
69
+
70
+ def create_response(resp)
71
+ raise NotFoundError, "Resource not found" if resp.code == 404
72
+
73
+ Response.new resp, convert_hash_keys_to(:snake_case, resp.parsed_response)
74
+ end
75
+
76
+ def basic_auth
77
+ { username: @auth[:token], password: @auth[:secret]}
78
+ end
79
+
80
+ def convert_hash_keys_to(conversion, value)
81
+ case value
82
+ when Array
83
+ value.map { |v| convert_hash_keys_to(conversion, v) }
84
+ when Hash
85
+ Hash[value.map { |k, v| [send(conversion, k).to_sym, convert_hash_keys_to(conversion, v)] }]
86
+ else
87
+ value
88
+ end
89
+ end
90
+
91
+ def camel_case(str)
92
+ return str.to_s if str.to_s !~ /_/ && str.to_s =~ /[A-Z]+.*/
93
+ words = str.to_s.split('_')
94
+ (words[0..0] << words[1..-1].map{|e| e.capitalize}).join
95
+ end
96
+
97
+ def snake_case(str)
98
+ str.gsub(/::/, '/').
99
+ gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
100
+ gsub(/([a-z\d])([A-Z])/,'\1_\2').
101
+ tr("-", "_").
102
+ downcase
103
+ end
104
+
105
+ end
106
+
107
+ end
@@ -0,0 +1,24 @@
1
+ module Moip2
2
+
3
+ class CustomerApi
4
+ attr_reader :client
5
+
6
+ def initialize(client)
7
+ @client = client
8
+ end
9
+
10
+ def base_path
11
+ "/v2/customers"
12
+ end
13
+
14
+ def show(customer_external_id)
15
+ Resource::Customer.new client, client.get("#{base_path}/#{customer_external_id}")
16
+ end
17
+
18
+ def create(customer)
19
+ Resource::Customer.new client, client.post(base_path, customer)
20
+ end
21
+
22
+ end
23
+
24
+ end
@@ -0,0 +1,6 @@
1
+ module Moip2
2
+
3
+ class InvalidEnviromentError < RuntimeError
4
+ end
5
+
6
+ end
@@ -0,0 +1,32 @@
1
+ module Moip2
2
+
3
+ class InvoiceApi
4
+ attr_reader :client
5
+
6
+ def initialize(client)
7
+ @client = client
8
+ end
9
+
10
+ def base_path
11
+ "/v2/invoices"
12
+ end
13
+
14
+ def show(invoice_external_id)
15
+ Resource::Invoice.new client, client.get("#{base_path}/#{invoice_external_id}")
16
+ end
17
+
18
+ def create(invoice)
19
+ Resource::Invoice.new client, client.post(base_path, invoice)
20
+ end
21
+
22
+ def update(invoice_external_id, invoice)
23
+ Resource::Invoice.new client, client.put("#{base_path}/#{invoice_external_id}", invoice)
24
+ end
25
+
26
+ def list(begin_date, end_date)
27
+ Resource::Invoice.new client, client.get("#{base_path}?begin=#{begin_date}&end=#{end_date}")
28
+ end
29
+
30
+ end
31
+
32
+ end
@@ -0,0 +1,17 @@
1
+ module Moip2
2
+ class KeysApi
3
+ attr_reader :client
4
+
5
+ def initialize(client)
6
+ @client = client
7
+ end
8
+
9
+ def base_path
10
+ "/v2/keys"
11
+ end
12
+
13
+ def show
14
+ Resource::Keys.new client, client.get("#{base_path}")
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,23 @@
1
+ module Moip2
2
+ class MultiOrderApi
3
+
4
+ attr_reader :client
5
+
6
+ def initialize(client)
7
+ @client = client
8
+ end
9
+
10
+ def base_path
11
+ "/v2/multiorders"
12
+ end
13
+
14
+ def create(multi_order)
15
+ Resource::MultiOrder.new client, client.post(base_path, multi_order)
16
+ end
17
+
18
+ def show(multi_order_id)
19
+ Resource::MultiOrder.new client, client.get("#{base_path}/#{multi_order_id}")
20
+ end
21
+
22
+ end
23
+ end
@@ -0,0 +1,17 @@
1
+ module Moip2
2
+ class MultiPaymentApi
3
+ attr_reader :client
4
+
5
+ def initialize(client)
6
+ @client = client
7
+ end
8
+
9
+ def base_path(multi_order_id)
10
+ "/v2/multiorders/#{multi_order_id}/multipayments"
11
+ end
12
+
13
+ def create(multi_order_id, payment)
14
+ Resource::Payment.new client.post(base_path(multi_order_id), payment)
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,25 @@
1
+ module Moip2
2
+
3
+ class OrderApi
4
+
5
+ attr_reader :client
6
+
7
+ def initialize(client)
8
+ @client = client
9
+ end
10
+
11
+ def base_path
12
+ "/v2/orders"
13
+ end
14
+
15
+ def create(order)
16
+ Resource::Order.new client, client.post(base_path, order)
17
+ end
18
+
19
+ def show(id)
20
+ Resource::Order.new client, client.get("#{base_path}/#{id}")
21
+ end
22
+
23
+ end
24
+
25
+ end
@@ -0,0 +1,15 @@
1
+ module Moip2
2
+ class PaymentApi
3
+
4
+ attr_reader :client
5
+
6
+ def initialize(client)
7
+ @client = client
8
+ end
9
+
10
+ def create(order_id, payment)
11
+ Resource::Payment.new client.post("/v2/orders/#{order_id}/payments", payment)
12
+ end
13
+
14
+ end
15
+ end
@@ -0,0 +1,16 @@
1
+ module Moip2
2
+ module Resource
3
+
4
+ class Customer < SimpleDelegator
5
+
6
+ attr_reader :client
7
+
8
+ def initialize(client, response)
9
+ super(response)
10
+ @client = client
11
+ end
12
+
13
+ end
14
+
15
+ end
16
+ end
@@ -0,0 +1,14 @@
1
+ module Moip2
2
+ module Resource
3
+
4
+ class Invoice < SimpleDelegator
5
+ attr_reader :client
6
+
7
+ def initialize(client, response)
8
+ super(response)
9
+ @client = client
10
+ end
11
+ end
12
+
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+ module Moip2
2
+ module Resource
3
+
4
+ class Keys < SimpleDelegator
5
+ attr_reader :client
6
+
7
+ def initialize(client, response)
8
+ super(response)
9
+ @client = client
10
+ end
11
+ end
12
+
13
+ end
14
+ end
@@ -0,0 +1,25 @@
1
+ module Moip2
2
+
3
+ module Resource
4
+ class MultiOrder < SimpleDelegator
5
+
6
+ attr_reader :client, :multi_payment_api, :external_id
7
+
8
+ def initialize(client, response)
9
+ super(response)
10
+ @client = client
11
+
12
+ if response.respond_to?(:external_id)
13
+ @multi_payment_api = MultiPaymentApi.new(client)
14
+ @external_id = response.external_id
15
+ end
16
+ end
17
+
18
+ def create_multi_payment(payment)
19
+ multi_payment_api.create
20
+ end
21
+
22
+ end
23
+ end
24
+
25
+ end
@@ -0,0 +1,11 @@
1
+ module Moip2
2
+ module Resource
3
+
4
+ class MultiPayment < SimpleDelegator
5
+ def intialize(response)
6
+ super(response)
7
+ end
8
+ end
9
+
10
+ end
11
+ end
@@ -0,0 +1,25 @@
1
+ module Moip2
2
+ module Resource
3
+
4
+ class Order < SimpleDelegator
5
+
6
+ attr_reader :client, :payment_api, :external_id
7
+
8
+ def initialize(client, response)
9
+ super(response)
10
+ @client = client
11
+
12
+ if response.respond_to?(:external_id)
13
+ @payment_api = PaymentApi.new(client)
14
+ @external_id = response.external_id
15
+ end
16
+ end
17
+
18
+ def create_payment(payment)
19
+ payment_api.create(external_id, payment)
20
+ end
21
+
22
+ end
23
+
24
+ end
25
+ end
@@ -0,0 +1,13 @@
1
+ module Moip2
2
+ module Resource
3
+
4
+ class Payment < SimpleDelegator
5
+
6
+ def intialize(response)
7
+ super(response)
8
+ end
9
+
10
+ end
11
+
12
+ end
13
+ end