mymoip 0.3.1 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.md CHANGED
@@ -1,6 +1,15 @@
1
1
  CHANGELOG
2
2
  =========
3
3
 
4
+ 0.4.0
5
+ -----
6
+
7
+ * Accept multiple receivers for each instruction.
8
+ * Can set a fixed value (e.g. R$ 50,00).
9
+ * Can set a percentage value (e.g. 10%).
10
+ * Define which one will take the fees.
11
+ * Accept payments to any MoIP users, even those without API keys.
12
+
4
13
  0.3.1
5
14
  -----
6
15
 
data/Gemfile CHANGED
@@ -17,5 +17,7 @@ group :development do
17
17
  gem "turn"
18
18
  gem "mocha", require: false
19
19
  gem "vcr"
20
- gem "webmock"
20
+ # Version requirement caused by VCR's warning.
21
+ # Can be removed after merge of https://github.com/vcr/vcr/commit/f75353b75e1ac1e4309faa9323e7c01d8ce28e46
22
+ gem "webmock", "< 1.9.0"
21
23
  end
data/Gemfile.lock CHANGED
@@ -1,18 +1,18 @@
1
1
  GEM
2
2
  remote: http://rubygems.org/
3
3
  specs:
4
- activemodel (3.2.8)
5
- activesupport (= 3.2.8)
4
+ activemodel (3.2.9)
5
+ activesupport (= 3.2.9)
6
6
  builder (~> 3.0.0)
7
- activesupport (3.2.8)
7
+ activesupport (3.2.9)
8
8
  i18n (~> 0.6)
9
9
  multi_json (~> 1.0)
10
10
  addressable (2.3.2)
11
11
  ansi (1.4.3)
12
- builder (3.0.0)
12
+ builder (3.0.4)
13
13
  crack (0.3.1)
14
14
  git (1.2.5)
15
- httparty (0.8.3)
15
+ httparty (0.9.0)
16
16
  multi_json (~> 1.0)
17
17
  multi_xml
18
18
  i18n (0.6.1)
@@ -21,18 +21,18 @@ GEM
21
21
  git (>= 1.2.5)
22
22
  rake
23
23
  rdoc
24
- json (1.7.4)
24
+ json (1.7.5)
25
25
  metaclass (0.0.1)
26
- mocha (0.12.3)
26
+ mocha (0.13.0)
27
27
  metaclass (~> 0.0.1)
28
- multi_json (1.3.6)
28
+ multi_json (1.4.0)
29
29
  multi_xml (0.5.1)
30
- rake (0.9.2.2)
30
+ rake (10.0.2)
31
31
  rdoc (3.12)
32
32
  json (~> 1.4)
33
33
  turn (0.9.6)
34
34
  ansi
35
- vcr (2.2.5)
35
+ vcr (2.3.0)
36
36
  webmock (1.8.11)
37
37
  addressable (>= 2.2.7)
38
38
  crack (>= 0.1.7)
@@ -50,4 +50,4 @@ DEPENDENCIES
50
50
  rdoc (~> 3.12)
51
51
  turn
52
52
  vcr
53
- webmock
53
+ webmock (< 1.9.0)
data/README.md CHANGED
@@ -85,6 +85,39 @@ payment_request.api_call(credit_card_payment, token: transparent_request.token)
85
85
  payment_request.success?
86
86
  ```
87
87
 
88
+ Sending payments to multiple receivers
89
+ --------------------------------------
90
+
91
+ Choosing between commission with fixed or percentage value.
92
+
93
+ ```ruby
94
+ commissions = [MyMoip::Commission.new(
95
+ reason: 'System maintenance',
96
+ receiver_login: 'commissioned_moip_login',
97
+ fixed_value: 15.0
98
+ )]
99
+
100
+ # OR
101
+
102
+ commissions = [MyMoip::Commission.new(
103
+ reason: 'Shipping',
104
+ receiver_login: 'commissioned_moip_login',
105
+ percentage_value: 0.15
106
+ )]
107
+ ```
108
+
109
+ ```ruby
110
+ instruction = MyMoip::Instruction.new(
111
+ id: "instruction_id_defined_by_you",
112
+ payment_reason: "Order in Buy Everything Store",
113
+ values: [100.0],
114
+ payer: payer,
115
+ commissions: commissions
116
+ )
117
+ ```
118
+
119
+ [Wiki](https://github.com/Irio/mymoip/wiki/Sending-payments-to-multiple-receivers) will be helpful here.
120
+
88
121
  Documentation
89
122
  -------------
90
123
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.1
1
+ 0.4.0
@@ -0,0 +1,51 @@
1
+ module MyMoip
2
+ class Commission
3
+ include ActiveModel::Validations
4
+
5
+ attr_accessor :reason, :receiver_login, :fixed_value, :percentage_value
6
+
7
+ validates_presence_of :reason, :receiver_login
8
+ validates_presence_of :fixed_value, if: -> { percentage_value.nil? }
9
+ validates_presence_of :percentage_value, if: -> { fixed_value.nil? }
10
+ validates_numericality_of :fixed_value, greater_than_or_equal_to: 0,
11
+ allow_nil: true
12
+ validates_numericality_of :percentage_value, greater_than_or_equal_to: 0,
13
+ less_than_or_equal_to: 1,
14
+ allow_nil: true
15
+
16
+ def initialize(attrs)
17
+ self.reason = attrs[:reason]
18
+ self.receiver_login = attrs[:receiver_login]
19
+ self.fixed_value = attrs[:fixed_value]
20
+ self.percentage_value = attrs[:percentage_value]
21
+ end
22
+
23
+ def gross_amount(instruction)
24
+ if fixed_value
25
+ fixed_value
26
+ elsif percentage_value
27
+ percentage_value * instruction.gross_amount
28
+ else
29
+ raise ArgumentError, 'Cannot give gross_amount without fixed_value or percentage_value'
30
+ end
31
+ end
32
+
33
+ def to_xml(root = nil)
34
+ raise ArgumentError, "Invalid params for Commission" if invalid?
35
+
36
+ if root.nil?
37
+ xml = ""
38
+ root ||= Builder::XmlMarkup.new(target: xml)
39
+ end
40
+
41
+ root.Comissionamento do |n1|
42
+ n1.Razao(reason)
43
+ n1.Comissionado {|n2| n2.LoginMoIP(receiver_login)}
44
+ n1.ValorFixo(fixed_value) if fixed_value
45
+ n1.ValorPercentual(percentage_value) if percentage_value
46
+ end
47
+
48
+ xml
49
+ end
50
+ end
51
+ end
@@ -16,17 +16,17 @@ module MyMoip
16
16
  validates_inclusion_of :logo, in: AVAILABLE_LOGOS
17
17
 
18
18
  def initialize(attrs)
19
- self.logo = attrs[:logo] if attrs.has_key?(:logo)
20
- self.card_number = attrs[:card_number] if attrs.has_key?(:card_number)
21
- self.expiration_date = attrs[:expiration_date] if attrs.has_key?(:expiration_date)
22
- self.security_code = attrs[:security_code] if attrs.has_key?(:security_code)
23
- self.owner_name = attrs[:owner_name] if attrs.has_key?(:owner_name)
24
- self.owner_birthday = attrs[:owner_birthday] if attrs.has_key?(:owner_birthday)
25
- self.owner_phone = attrs[:owner_phone] if attrs.has_key?(:owner_phone)
26
- self.owner_cpf = attrs[:owner_cpf] if attrs.has_key?(:owner_cpf)
19
+ self.logo = attrs[:logo]
20
+ self.card_number = attrs[:card_number]
21
+ self.expiration_date = attrs[:expiration_date]
22
+ self.security_code = attrs[:security_code]
23
+ self.owner_name = attrs[:owner_name]
24
+ self.owner_birthday = attrs[:owner_birthday]
25
+ self.owner_phone = attrs[:owner_phone]
26
+ self.owner_cpf = attrs[:owner_cpf]
27
27
 
28
28
  # Deprecated attributes
29
- self.owner_rg = attrs[:owner_rg] if attrs.has_key?(:owner_rg)
29
+ self.owner_rg = attrs[:owner_rg] if attrs.has_key?(:owner_rg)
30
30
  end
31
31
 
32
32
  def logo=(value)
@@ -2,20 +2,31 @@ module MyMoip
2
2
  class Instruction
3
3
  include ActiveModel::Validations
4
4
 
5
- attr_accessor :id, :payment_reason, :values, :payer
5
+ attr_accessor :id, :payment_reason, :values, :payer,
6
+ :commissions, :fee_payer_login, :payment_receiver_login,
7
+ :payment_receiver_name
6
8
 
7
9
  validates_presence_of :id, :payment_reason, :values, :payer
10
+ validate :commissions_value_must_be_lesser_than_values
11
+ validate :payment_receiver_presence_in_commissions
8
12
 
9
13
  def initialize(attrs)
10
- self.id = attrs[:id] if attrs.has_key?(:id)
11
- self.payment_reason = attrs[:payment_reason] if attrs.has_key?(:payment_reason)
12
- self.values = attrs[:values] if attrs.has_key?(:values)
13
- self.payer = attrs[:payer] if attrs.has_key?(:payer)
14
+ self.id = attrs[:id]
15
+ self.payment_reason = attrs[:payment_reason]
16
+ self.values = attrs[:values]
17
+ self.payer = attrs[:payer]
18
+ self.commissions = attrs[:commissions] || []
19
+ self.fee_payer_login = attrs[:fee_payer_login]
20
+ self.payment_receiver_login = attrs[:payment_receiver_login]
21
+ self.payment_receiver_name = attrs[:payment_receiver_name]
14
22
  end
15
23
 
16
24
  def to_xml(root = nil)
17
25
  raise ArgumentError, 'Invalid payer' if payer.invalid?
18
26
  raise ArgumentError, 'Invalid params for instruction' if self.invalid?
27
+ if invalid_commission = commissions.detect { |c| c.invalid? }
28
+ raise ArgumentError, "Invalid commission: #{invalid_commission}"
29
+ end
19
30
 
20
31
  xml = ""
21
32
  root = Builder::XmlMarkup.new(target: xml)
@@ -27,11 +38,53 @@ module MyMoip
27
38
  @values.each { |v| n3.Valor("%.2f" % v, moeda: "BRL") }
28
39
  end
29
40
  n2.IdProprio(@id)
41
+
42
+ commissions_to_xml n2 if !commissions.empty?
43
+ payment_receiver_to_xml n2 if payment_receiver_login
44
+
30
45
  n2.Pagador { |n3| @payer.to_xml(n3) }
31
46
  end
32
47
  end
33
48
 
34
49
  xml
35
50
  end
51
+
52
+ def commissions_sum
53
+ commissions.inject(0) do |sum, commission|
54
+ sum + commission.gross_amount(self)
55
+ end
56
+ end
57
+
58
+ def gross_amount
59
+ values ? values.reduce(0) { |sum, value| sum + value } : 0
60
+ end
61
+
62
+ protected
63
+
64
+ def commissions_value_must_be_lesser_than_values
65
+ if commissions_sum > gross_amount
66
+ errors.add(:commissions, "Commissions value sum is greater than instruction value sum")
67
+ end
68
+ end
69
+
70
+ def payment_receiver_presence_in_commissions
71
+ if commissions.find { |c| c.receiver_login == payment_receiver_login }
72
+ errors.add(:payment_receiver_login, "Payment receiver can't be commissioned")
73
+ end
74
+ end
75
+
76
+ def commissions_to_xml(node)
77
+ node.Comissoes do |n|
78
+ commissions.each { |c| c.to_xml(n) }
79
+ n.PagadorTaxa { |pt| pt.LoginMoIP(fee_payer_login) } if fee_payer_login
80
+ end
81
+ end
82
+
83
+ def payment_receiver_to_xml(node)
84
+ node.Recebedor do |n|
85
+ n.LoginMoIP(payment_receiver_login)
86
+ n.Apelido(payment_receiver_name)
87
+ end
88
+ end
36
89
  end
37
90
  end
@@ -1,6 +1,5 @@
1
1
  module MyMoip
2
2
  class JsonParser
3
-
4
3
  def self.call(body, format)
5
4
  if format == :json
6
5
  JSON.parse body.match(/\?\((?<valid_json>.+)\)/)[:valid_json]
@@ -8,6 +7,5 @@ module MyMoip
8
7
  body
9
8
  end
10
9
  end
11
-
12
10
  end
13
11
  end
data/lib/mymoip/payer.rb CHANGED
@@ -17,18 +17,18 @@ module MyMoip
17
17
  validates_length_of :address_phone, within: 10..11
18
18
 
19
19
  def initialize(attrs)
20
- self.id = attrs[:id] if attrs.has_key?(:id)
21
- self.name = attrs[:name] if attrs.has_key?(:name)
22
- self.email = attrs[:email] if attrs.has_key?(:email)
23
- self.address_street = attrs[:address_street] if attrs.has_key?(:address_street)
24
- self.address_street_number = attrs[:address_street_number] if attrs.has_key?(:address_street_number)
25
- self.address_street_extra = attrs[:address_street_extra] if attrs.has_key?(:address_street_extra)
26
- self.address_neighbourhood = attrs[:address_neighbourhood] if attrs.has_key?(:address_neighbourhood)
27
- self.address_city = attrs[:address_city] if attrs.has_key?(:address_city)
28
- self.address_state = attrs[:address_state] if attrs.has_key?(:address_state)
29
- self.address_country = attrs[:address_country] if attrs.has_key?(:address_country)
30
- self.address_cep = attrs[:address_cep] if attrs.has_key?(:address_cep)
31
- self.address_phone = attrs[:address_phone] if attrs.has_key?(:address_phone)
20
+ self.id = attrs[:id]
21
+ self.name = attrs[:name]
22
+ self.email = attrs[:email]
23
+ self.address_street = attrs[:address_street]
24
+ self.address_street_number = attrs[:address_street_number]
25
+ self.address_street_extra = attrs[:address_street_extra]
26
+ self.address_neighbourhood = attrs[:address_neighbourhood]
27
+ self.address_city = attrs[:address_city]
28
+ self.address_state = attrs[:address_state]
29
+ self.address_country = attrs[:address_country]
30
+ self.address_cep = attrs[:address_cep]
31
+ self.address_phone = attrs[:address_phone]
32
32
  end
33
33
 
34
34
  def address_cep=(value)
data/mymoip.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "mymoip"
8
- s.version = "0.3.1"
8
+ s.version = "0.4.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Irio Irineu Musskopf Junior"]
12
- s.date = "2012-11-17"
12
+ s.date = "2012-12-01"
13
13
  s.description = "Provides a implementation of MoIP's transparent checkout."
14
14
  s.email = "irio.musskopf@caixadeideias.com.br"
15
15
  s.extra_rdoc_files = [
@@ -26,6 +26,7 @@ Gem::Specification.new do |s|
26
26
  "Rakefile",
27
27
  "VERSION",
28
28
  "lib/mymoip.rb",
29
+ "lib/mymoip/commission.rb",
29
30
  "lib/mymoip/credit_card.rb",
30
31
  "lib/mymoip/credit_card_payment.rb",
31
32
  "lib/mymoip/formatter.rb",
@@ -39,8 +40,10 @@ Gem::Specification.new do |s|
39
40
  "test/fixtures/fixture.rb",
40
41
  "test/fixtures/vcr_cassettes/payment_request.yml",
41
42
  "test/fixtures/vcr_cassettes/transparent_request.yml",
43
+ "test/fixtures/vcr_cassettes/transparent_request_with_commissions.yml",
42
44
  "test/helper.rb",
43
45
  "test/live_test.rb",
46
+ "test/test_commission.rb",
44
47
  "test/test_credit_card_payment.rb",
45
48
  "test/test_creditcard.rb",
46
49
  "test/test_formatter.rb",
@@ -70,7 +73,7 @@ Gem::Specification.new do |s|
70
73
  s.add_development_dependency(%q<turn>, [">= 0"])
71
74
  s.add_development_dependency(%q<mocha>, [">= 0"])
72
75
  s.add_development_dependency(%q<vcr>, [">= 0"])
73
- s.add_development_dependency(%q<webmock>, [">= 0"])
76
+ s.add_development_dependency(%q<webmock>, ["< 1.9.0"])
74
77
  else
75
78
  s.add_dependency(%q<builder>, [">= 0"])
76
79
  s.add_dependency(%q<httparty>, [">= 0"])
@@ -81,7 +84,7 @@ Gem::Specification.new do |s|
81
84
  s.add_dependency(%q<turn>, [">= 0"])
82
85
  s.add_dependency(%q<mocha>, [">= 0"])
83
86
  s.add_dependency(%q<vcr>, [">= 0"])
84
- s.add_dependency(%q<webmock>, [">= 0"])
87
+ s.add_dependency(%q<webmock>, ["< 1.9.0"])
85
88
  end
86
89
  else
87
90
  s.add_dependency(%q<builder>, [">= 0"])
@@ -93,7 +96,7 @@ Gem::Specification.new do |s|
93
96
  s.add_dependency(%q<turn>, [">= 0"])
94
97
  s.add_dependency(%q<mocha>, [">= 0"])
95
98
  s.add_dependency(%q<vcr>, [">= 0"])
96
- s.add_dependency(%q<webmock>, [">= 0"])
99
+ s.add_dependency(%q<webmock>, ["< 1.9.0"])
97
100
  end
98
101
  end
99
102
 
@@ -41,4 +41,13 @@ class Fixture
41
41
  MyMoip::CreditCard.new(params)
42
42
  end
43
43
 
44
+ def self.commission(params = {})
45
+ params = {
46
+ reason: 'Because we can',
47
+ receiver_login: 'commissioned_indentifier',
48
+ fixed_value: 23.4
49
+ }.merge(params)
50
+ MyMoip::Commission.new(params)
51
+ end
52
+
44
53
  end
@@ -0,0 +1,35 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: post
5
+ uri: https://YOUR_MOIP_TOKEN:YOUR_MOIP_KEY@desenvolvedor.moip.com.br/sandbox/ws/alpha/EnviarInstrucao/Unica
6
+ body:
7
+ encoding: US-ASCII
8
+ string: <EnviarInstrucao><InstrucaoUnica TipoValidacao="Transparente"><Razao>some
9
+ payment_reason</Razao><Valores><Valor moeda="BRL">100.00</Valor><Valor moeda="BRL">200.00</Valor></Valores><IdProprio>your_own_instruction_id</IdProprio><Comissoes><Comissionamento><Razao>Because
10
+ we can</Razao><Comissionado><LoginMoIP>commissioned_id</LoginMoIP></Comissionado><ValorFixo>5</ValorFixo></Comissionamento></Comissoes><Pagador><Nome>Juquinha
11
+ da Rocha</Nome><Email>juquinha@rocha.com</Email><IdPagador>your_own_payer_id</IdPagador><EnderecoCobranca><Logradouro>Felipe
12
+ Neri</Logradouro><Numero>406</Numero><Complemento>Sala 501</Complemento><Bairro>Auxiliadora</Bairro><Cidade>Porto
13
+ Alegre</Cidade><Estado>RS</Estado><Pais>BRA</Pais><CEP>90440-150</CEP><TelefoneFixo>(51)3040-5060</TelefoneFixo></EnderecoCobranca></Pagador></InstrucaoUnica></EnviarInstrucao>
14
+ headers: {}
15
+ response:
16
+ status:
17
+ code: 200
18
+ message: OK
19
+ headers:
20
+ Date:
21
+ - Wed, 28 Nov 2012 14:20:49 GMT
22
+ Server:
23
+ - Apache/2.2.23 (CentOS)
24
+ Content-Length:
25
+ - '273'
26
+ Vary:
27
+ - Accept-Encoding
28
+ Content-Type:
29
+ - text/xml;charset=UTF-8
30
+ body:
31
+ encoding: US-ASCII
32
+ string: <ns1:EnviarInstrucaoUnicaResponse xmlns:ns1="http://www.moip.com.br/ws/alpha/"><Resposta><ID>YOUR_REQUEST_ID</ID><Status>Sucesso</Status><Token>YOUR_MOIP_TOKEN</Token></Resposta></ns1:EnviarInstrucaoUnicaResponse>
33
+ http_version:
34
+ recorded_at: Wed, 28 Nov 2012 14:17:23 GMT
35
+ recorded_with: VCR 2.2.5
data/test/helper.rb CHANGED
@@ -9,7 +9,7 @@ rescue Bundler::BundlerError => e
9
9
  end
10
10
  require 'test/unit'
11
11
  require 'turn/autorun'
12
- require 'mocha'
12
+ require 'mocha/setup'
13
13
  require 'vcr'
14
14
 
15
15
  VCR.configure do |c|
@@ -0,0 +1,121 @@
1
+ require 'helper'
2
+
3
+ class TestCommission < Test::Unit::TestCase
4
+ def test_initialization_and_setters
5
+ params = {
6
+ reason: 'Because we can',
7
+ receiver_login: 'comissioned_indentifier',
8
+ fixed_value: 23.5,
9
+ percentage_value: 0.15
10
+ }
11
+ subject = MyMoip::Commission.new(params)
12
+ assert_equal params[:reason], subject.reason
13
+ assert_equal params[:receiver_login], subject.receiver_login
14
+ assert_equal params[:fixed_value], subject.fixed_value
15
+ assert_equal params[:percentage_value], subject.percentage_value
16
+ end
17
+
18
+ def test_validate_presence_of_reason
19
+ subject = Fixture.commission(reason: nil)
20
+ assert subject.invalid? && subject.errors[:reason].present?,
21
+ "should be invalid without a reason"
22
+ end
23
+
24
+ def test_validate_presence_of_receiver_login
25
+ subject = Fixture.commission(receiver_login: nil)
26
+ assert subject.invalid? && subject.errors[:receiver_login].present?,
27
+ "should be invalid without a receiver_login"
28
+ end
29
+
30
+ def test_validate_presence_of_fixed_value_or_percentage_value
31
+ subject = Fixture.commission(fixed_value: nil, percentage_value: nil)
32
+
33
+ assert subject.invalid? && subject.errors[:fixed_value].present?,
34
+ "should be invalid without a fixed value"
35
+
36
+ assert subject.invalid? && subject.errors[:percentage_value].present?,
37
+ "should be invalid without a percentage value"
38
+
39
+ subject.fixed_value = 2
40
+ assert subject.valid?, "should be valid with only fixed value set"
41
+
42
+ subject.fixed_value = nil
43
+ subject.percentage_value = 0.15
44
+ assert subject.valid?, "should be valid with only percentage value set"
45
+
46
+ subject.fixed_value = subject.percentage_value
47
+ assert subject.valid?, "should be valid with both values set"
48
+ end
49
+
50
+
51
+ def test_validate_numericality_of_fixed_value
52
+ subject = Fixture.commission(fixed_value: "I'm not a number")
53
+ assert subject.invalid? && subject.errors[:fixed_value].present?,
54
+ "should be invalid with a non number"
55
+ end
56
+
57
+ def test_validate_numericality_of_percentage_value
58
+ subject = Fixture.commission(percentage_value: "I'm not a number", fixed_value: nil)
59
+ assert subject.invalid? && subject.errors[:percentage_value].present?,
60
+ "should be invalid with a non number"
61
+ end
62
+
63
+ def test_validate_positive_number_of_fixed_value
64
+ subject = Fixture.commission(fixed_value: -0.1)
65
+ assert subject.invalid? && subject.errors[:fixed_value].present?,
66
+ "should be invalid with negative number"
67
+ end
68
+
69
+ def test_validate_percentage_number_of_percentage_value
70
+ subject = Fixture.commission(percentage_value: -0.1, fixed_value: nil)
71
+ assert subject.invalid? && subject.errors[:percentage_value].present?,
72
+ "should be invalid if lesser than 0"
73
+ subject.percentage_value = 1.01
74
+ assert subject.invalid? && subject.errors[:percentage_value].present?,
75
+ "should be invalid if greater than 1"
76
+ end
77
+
78
+ def test_gross_amount_is_equal_to_fixed_value_when_this_is_present
79
+ instruction = Fixture.instruction
80
+ subject = Fixture.commission(fixed_value: 5, percentage_value: nil)
81
+ assert_equal 5, subject.gross_amount(instruction)
82
+ end
83
+
84
+ def test_gross_amount_with_percentage_is_equal_to_a_percentage_of_instructions_values
85
+ instruction = stub(gross_amount: 200)
86
+ subject = Fixture.commission(fixed_value: nil, percentage_value: 0.2)
87
+ assert_equal 40, subject.gross_amount(instruction)
88
+ end
89
+
90
+ def test_cannot_give_gross_amount_without_fixed_or_percentage_value_set
91
+ instruction = stub(gross_amount: 200)
92
+ subject = Fixture.commission(fixed_value: nil, percentage_value: nil)
93
+ assert_raise ArgumentError do
94
+ subject.gross_amount(instruction)
95
+ end
96
+ end
97
+
98
+ def test_xml_format_with_fixed_value
99
+ subject = Fixture.commission(fixed_value: 5)
100
+ expected_format = <<XML
101
+ <Comissionamento><Razao>Because we can</Razao><Comissionado><LoginMoIP>commissioned_indentifier</LoginMoIP></Comissionado><ValorFixo>5</ValorFixo></Comissionamento>
102
+ XML
103
+ assert_equal expected_format.rstrip, subject.to_xml
104
+ end
105
+
106
+ def test_xml_format_with_percentage_value
107
+ subject = Fixture.commission(percentage_value: 0.15, fixed_value: nil)
108
+ expected_format = <<XML
109
+ <Comissionamento><Razao>Because we can</Razao><Comissionado><LoginMoIP>commissioned_indentifier</LoginMoIP></Comissionado><ValorPercentual>0.15</ValorPercentual></Comissionamento>
110
+ XML
111
+ assert_equal expected_format.rstrip, subject.to_xml
112
+ end
113
+
114
+ def test_xml_method_raises_exception_when_called_with_invalid_params
115
+ subject = Fixture.commission
116
+ subject.stubs(:invalid?).returns(true)
117
+ assert_raise ArgumentError do
118
+ subject.to_xml
119
+ end
120
+ end
121
+ end
@@ -3,17 +3,26 @@ require 'helper'
3
3
  class TestInstruction < Test::Unit::TestCase
4
4
  def test_getters_for_attributes
5
5
  payer = Fixture.payer
6
+ commissions = [Fixture.commission]
6
7
  instruction = MyMoip::Instruction.new(
7
8
  id: "some id",
8
9
  payment_reason: "some payment_reason",
9
10
  values: [100.0, 200.0],
10
- payer: payer
11
+ payer: payer,
12
+ commissions: commissions,
13
+ fee_payer_login: "fee_payer_login",
14
+ payment_receiver_login: "payment_receiver_login",
15
+ payment_receiver_name: "nick_fury"
11
16
  )
12
17
 
13
18
  assert_equal "some id", instruction.id
14
19
  assert_equal "some payment_reason", instruction.payment_reason
15
20
  assert_equal [100.0, 200.0], instruction.values
16
21
  assert_equal payer, instruction.payer
22
+ assert_equal commissions, instruction.commissions
23
+ assert_equal "fee_payer_login", instruction.fee_payer_login
24
+ assert_equal "payment_receiver_login", instruction.payment_receiver_login
25
+ assert_equal "nick_fury", instruction.payment_receiver_name
17
26
  end
18
27
 
19
28
  def test_should_generate_a_string_when_converting_to_xml
@@ -32,6 +41,60 @@ XML
32
41
  assert_equal expected_format.rstrip, instruction.to_xml
33
42
  end
34
43
 
44
+ def test_xml_format_with_commissions
45
+ commissions = [Fixture.commission(fixed_value: 5), Fixture.commission(percentage_value: 0.15, fixed_value: nil)]
46
+ payer = Fixture.payer
47
+ instruction = Fixture.instruction(payer: payer, commissions: commissions)
48
+ expected_format = <<XML
49
+ <EnviarInstrucao><InstrucaoUnica TipoValidacao=\"Transparente\"><Razao>some payment_reason</Razao><Valores><Valor moeda=\"BRL\">100.00</Valor><Valor moeda=\"BRL\">200.00</Valor></Valores><IdProprio>your_own_instruction_id</IdProprio><Comissoes><Comissionamento><Razao>Because we can</Razao><Comissionado><LoginMoIP>commissioned_indentifier</LoginMoIP></Comissionado><ValorFixo>5</ValorFixo></Comissionamento><Comissionamento><Razao>Because we can</Razao><Comissionado><LoginMoIP>commissioned_indentifier</LoginMoIP></Comissionado><ValorPercentual>0.15</ValorPercentual></Comissionamento></Comissoes><Pagador><Nome>Juquinha da Rocha</Nome><Email>juquinha@rocha.com</Email><IdPagador>your_own_payer_id</IdPagador><EnderecoCobranca><Logradouro>Felipe Neri</Logradouro><Numero>406</Numero><Complemento>Sala 501</Complemento><Bairro>Auxiliadora</Bairro><Cidade>Porto Alegre</Cidade><Estado>RS</Estado><Pais>BRA</Pais><CEP>90440-150</CEP><TelefoneFixo>(51)3040-5060</TelefoneFixo></EnderecoCobranca></Pagador></InstrucaoUnica></EnviarInstrucao>
50
+ XML
51
+ assert_equal expected_format.rstrip, instruction.to_xml
52
+ end
53
+
54
+ def test_xml_format_with_commissions_and_fee_payer
55
+ commissions = [Fixture.commission(fixed_value: 5), Fixture.commission(percentage_value: 0.15,fixed_value: nil)]
56
+ payer = Fixture.payer
57
+ instruction = Fixture.instruction(payer: payer, commissions: commissions, fee_payer_login: 'fee_payer_indentifier')
58
+ expected_format = <<XML
59
+ <EnviarInstrucao><InstrucaoUnica TipoValidacao=\"Transparente\"><Razao>some payment_reason</Razao><Valores><Valor moeda=\"BRL\">100.00</Valor><Valor moeda=\"BRL\">200.00</Valor></Valores><IdProprio>your_own_instruction_id</IdProprio><Comissoes><Comissionamento><Razao>Because we can</Razao><Comissionado><LoginMoIP>commissioned_indentifier</LoginMoIP></Comissionado><ValorFixo>5</ValorFixo></Comissionamento><Comissionamento><Razao>Because we can</Razao><Comissionado><LoginMoIP>commissioned_indentifier</LoginMoIP></Comissionado><ValorPercentual>0.15</ValorPercentual></Comissionamento><PagadorTaxa><LoginMoIP>fee_payer_indentifier</LoginMoIP></PagadorTaxa></Comissoes><Pagador><Nome>Juquinha da Rocha</Nome><Email>juquinha@rocha.com</Email><IdPagador>your_own_payer_id</IdPagador><EnderecoCobranca><Logradouro>Felipe Neri</Logradouro><Numero>406</Numero><Complemento>Sala 501</Complemento><Bairro>Auxiliadora</Bairro><Cidade>Porto Alegre</Cidade><Estado>RS</Estado><Pais>BRA</Pais><CEP>90440-150</CEP><TelefoneFixo>(51)3040-5060</TelefoneFixo></EnderecoCobranca></Pagador></InstrucaoUnica></EnviarInstrucao>
60
+ XML
61
+ assert_equal expected_format.rstrip, instruction.to_xml
62
+ end
63
+
64
+ def test_xml_format_with_commissions_and_payment_receiver
65
+ commissions = [Fixture.commission(fixed_value: 5), Fixture.commission(percentage_value: 0.15, fixed_value: nil)]
66
+ payer = Fixture.payer
67
+ instruction = Fixture.instruction(payer: payer, commissions: commissions,
68
+ payment_receiver_login:'payment_receiver_indentifier',
69
+ payment_receiver_name: 'nick_fury' )
70
+ expected_format = <<XML
71
+ <EnviarInstrucao><InstrucaoUnica TipoValidacao=\"Transparente\"><Razao>some payment_reason</Razao><Valores><Valor moeda=\"BRL\">100.00</Valor><Valor moeda=\"BRL\">200.00</Valor></Valores><IdProprio>your_own_instruction_id</IdProprio><Comissoes><Comissionamento><Razao>Because we can</Razao><Comissionado><LoginMoIP>commissioned_indentifier</LoginMoIP></Comissionado><ValorFixo>5</ValorFixo></Comissionamento><Comissionamento><Razao>Because we can</Razao><Comissionado><LoginMoIP>commissioned_indentifier</LoginMoIP></Comissionado><ValorPercentual>0.15</ValorPercentual></Comissionamento></Comissoes><Recebedor><LoginMoIP>payment_receiver_indentifier</LoginMoIP><Apelido>nick_fury</Apelido></Recebedor><Pagador><Nome>Juquinha da Rocha</Nome><Email>juquinha@rocha.com</Email><IdPagador>your_own_payer_id</IdPagador><EnderecoCobranca><Logradouro>Felipe Neri</Logradouro><Numero>406</Numero><Complemento>Sala 501</Complemento><Bairro>Auxiliadora</Bairro><Cidade>Porto Alegre</Cidade><Estado>RS</Estado><Pais>BRA</Pais><CEP>90440-150</CEP><TelefoneFixo>(51)3040-5060</TelefoneFixo></EnderecoCobranca></Pagador></InstrucaoUnica></EnviarInstrucao>
72
+ XML
73
+ assert_equal expected_format.rstrip, instruction.to_xml
74
+ end
75
+
76
+ def test_xml_format_with_commissions_and_payment_receiver_and_fee_payer
77
+ commissions = [Fixture.commission(fixed_value: 5), Fixture.commission(percentage_value: 0.15, fixed_value: nil)]
78
+ payer = Fixture.payer
79
+ instruction = Fixture.instruction(payer: payer, commissions: commissions,
80
+ payment_receiver_login:'payment_receiver_indentifier',
81
+ payment_receiver_name: 'nick_fury',
82
+ fee_payer_login: 'fee_payer_indentifier')
83
+ expected_format = <<XML
84
+ <EnviarInstrucao><InstrucaoUnica TipoValidacao=\"Transparente\"><Razao>some payment_reason</Razao><Valores><Valor moeda=\"BRL\">100.00</Valor><Valor moeda=\"BRL\">200.00</Valor></Valores><IdProprio>your_own_instruction_id</IdProprio><Comissoes><Comissionamento><Razao>Because we can</Razao><Comissionado><LoginMoIP>commissioned_indentifier</LoginMoIP></Comissionado><ValorFixo>5</ValorFixo></Comissionamento><Comissionamento><Razao>Because we can</Razao><Comissionado><LoginMoIP>commissioned_indentifier</LoginMoIP></Comissionado><ValorPercentual>0.15</ValorPercentual></Comissionamento><PagadorTaxa><LoginMoIP>fee_payer_indentifier</LoginMoIP></PagadorTaxa></Comissoes><Recebedor><LoginMoIP>payment_receiver_indentifier</LoginMoIP><Apelido>nick_fury</Apelido></Recebedor><Pagador><Nome>Juquinha da Rocha</Nome><Email>juquinha@rocha.com</Email><IdPagador>your_own_payer_id</IdPagador><EnderecoCobranca><Logradouro>Felipe Neri</Logradouro><Numero>406</Numero><Complemento>Sala 501</Complemento><Bairro>Auxiliadora</Bairro><Cidade>Porto Alegre</Cidade><Estado>RS</Estado><Pais>BRA</Pais><CEP>90440-150</CEP><TelefoneFixo>(51)3040-5060</TelefoneFixo></EnderecoCobranca></Pagador></InstrucaoUnica></EnviarInstrucao>
85
+ XML
86
+ assert_equal expected_format.rstrip, instruction.to_xml
87
+ end
88
+
89
+ def test_to_xml_method_raises_exception_when_called_with_some_invalid_comission
90
+ invalid_commission = Fixture.commission
91
+ invalid_commission.stubs(:invalid?).returns(true)
92
+ subject = Fixture.instruction(commissions: [Fixture.commission, invalid_commission])
93
+ assert_raise ArgumentError do
94
+ subject.to_xml
95
+ end
96
+ end
97
+
35
98
  def test_to_xml_method_raises_exception_when_called_with_invalid_payer
36
99
  subject = Fixture.instruction
37
100
  MyMoip::Payer.any_instance.stubs(:invalid?).returns(true)
@@ -64,6 +127,31 @@ XML
64
127
  end
65
128
  end
66
129
 
130
+ def test_validate_no_presence_of_payment_receiver_in_commissions
131
+ commissions = [Fixture.commission(receiver_login: 'payment_receiver_id')]
132
+ subject = Fixture.instruction(payment_receiver_login: 'payment_receiver_id', commissions: commissions)
133
+ assert subject.invalid? && subject.errors[:payment_receiver_login].present?,
134
+ "should be invalid with receiver present on commissions"
135
+ end
136
+
137
+ def test_gross_amount
138
+ subject = Fixture.instruction(values: [6, 5])
139
+ assert_equal 11, subject.gross_amount
140
+ end
141
+
142
+ def test_commissions_sum
143
+ commissions = [Fixture.commission(fixed_value: 5), Fixture.commission(percentage_value: 0.1, fixed_value: nil)]
144
+ subject = Fixture.instruction(commissions: commissions, values: [10])
145
+ assert_equal 6, subject.commissions_sum
146
+ end
147
+
148
+ def test_validate_commissions_sum
149
+ commissions = [Fixture.commission(fixed_value: 5), Fixture.commission(fixed_value: 5)]
150
+ subject = Fixture.instruction(commissions: commissions, values: [6])
151
+ assert subject.invalid? && subject.errors[:commissions].present?,
152
+ "should be invalid with commissions sum greater than values sum"
153
+ end
154
+
67
155
  def test_validate_presence_of_id_attribute
68
156
  subject = Fixture.instruction
69
157
  subject.id = nil
@@ -61,4 +61,12 @@ class TestTransparentRequest < Test::Unit::TestCase
61
61
  end
62
62
  assert_equal "201210171118501100000001102691", request.id
63
63
  end
64
+
65
+ def test_should_provide_the_transaction_id_get_by_the_request_with_commissions_feature
66
+ request = MyMoip::TransparentRequest.new("some_id")
67
+ VCR.use_cassette('transparent_request_with_commissions') do
68
+ request.api_call Fixture.instruction(commissions: [Fixture.commission])
69
+ end
70
+ assert_equal "YOUR_REQUEST_ID", request.id
71
+ end
64
72
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mymoip
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.4.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-11-17 00:00:00.000000000 Z
12
+ date: 2012-12-01 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: builder
@@ -160,17 +160,17 @@ dependencies:
160
160
  requirement: !ruby/object:Gem::Requirement
161
161
  none: false
162
162
  requirements:
163
- - - ! '>='
163
+ - - <
164
164
  - !ruby/object:Gem::Version
165
- version: '0'
165
+ version: 1.9.0
166
166
  type: :development
167
167
  prerelease: false
168
168
  version_requirements: !ruby/object:Gem::Requirement
169
169
  none: false
170
170
  requirements:
171
- - - ! '>='
171
+ - - <
172
172
  - !ruby/object:Gem::Version
173
- version: '0'
173
+ version: 1.9.0
174
174
  description: Provides a implementation of MoIP's transparent checkout.
175
175
  email: irio.musskopf@caixadeideias.com.br
176
176
  executables: []
@@ -188,6 +188,7 @@ files:
188
188
  - Rakefile
189
189
  - VERSION
190
190
  - lib/mymoip.rb
191
+ - lib/mymoip/commission.rb
191
192
  - lib/mymoip/credit_card.rb
192
193
  - lib/mymoip/credit_card_payment.rb
193
194
  - lib/mymoip/formatter.rb
@@ -201,8 +202,10 @@ files:
201
202
  - test/fixtures/fixture.rb
202
203
  - test/fixtures/vcr_cassettes/payment_request.yml
203
204
  - test/fixtures/vcr_cassettes/transparent_request.yml
205
+ - test/fixtures/vcr_cassettes/transparent_request_with_commissions.yml
204
206
  - test/helper.rb
205
207
  - test/live_test.rb
208
+ - test/test_commission.rb
206
209
  - test/test_credit_card_payment.rb
207
210
  - test/test_creditcard.rb
208
211
  - test/test_formatter.rb
@@ -227,7 +230,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
227
230
  version: '0'
228
231
  segments:
229
232
  - 0
230
- hash: 3042837852117747253
233
+ hash: -3065354461794399746
231
234
  required_rubygems_version: !ruby/object:Gem::Requirement
232
235
  none: false
233
236
  requirements: