mymoip 0.6.1 → 0.6.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 401ae2832ae5de6b706bf0581872a9554bd06f7e
4
+ data.tar.gz: d8ec3a961608600adae70bd42211e07ea9aae631
5
+ SHA512:
6
+ metadata.gz: e5b9b24159c3122829f406ee1e09e19db8f087f3d4e63474461b57cab065a6095aec3d7f984b6b7f2516108f94bf1e21a55800d30c7467747c7f6aa320b6e80a
7
+ data.tar.gz: f3bfdf0541cab984fbf4f535fe01725a83670e4429120407d6d535b9d04ef7e7e9689ca39cebbaff0320106998898c7bfc394657f04b9a3ee20022847d7d2c46
data/.gitignore ADDED
@@ -0,0 +1,15 @@
1
+ # rdoc generated
2
+ rdoc
3
+
4
+ # yard generated
5
+ doc
6
+ .yardoc
7
+
8
+ # bundler
9
+ .bundle
10
+
11
+ # jeweler generated
12
+ pkg
13
+
14
+ # Bundler
15
+ Gemfile.lock
data/CHANGELOG.md CHANGED
@@ -1,10 +1,28 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 0.6.2
4
+
5
+ * Removed development dependency of jeweler. Gems are now managed
6
+ directly in .gemspec file.
7
+ * Offer a easier way to manage purchase implementations that don't have
8
+ many customizations over passing some attribute list, making the
9
+ checkout and getting a successful (or not) response. Through
10
+ MyMoip::Purchase.
11
+ * Accept string keys in initializers of CreditCard and Payer classes.
12
+
13
+ ## 0.6.1
14
+
15
+ * Send 2 decimal place numbers in fixed and percentage values nodes of
16
+ Comission's XML. Percentage values are required to be in a 0 to 100 range.
17
+ Reported by @zangrandi.
18
+
3
19
  ## 0.6.0
20
+
4
21
  * Add support for Ruby 2.0.
5
22
  * Improved installments option for Instructions.
6
23
 
7
24
  ## 0.5.0
25
+
8
26
  * Breaking backward compatibility with exceptions raised. ArgumentError
9
27
  is not used anymore. New MyMoip::InvalidComission, MyMoip::InvalidCreditCard,
10
28
  MyMoip::InvalidInstruction and MyMoip::InvalidPayer exceptions inherited from
data/README.md CHANGED
@@ -50,29 +50,29 @@ purchase_id = 'UNIQUE_PURCHASE_ID'
50
50
  transaction_price = 100.0
51
51
 
52
52
  card_attrs = {
53
- logo: 'visa',
54
- card_number: '4916654211627608',
55
- expiration_date: '06/15',
56
- security_code: '000',
57
- owner_name: 'Juquinha da Rocha',
58
- owner_birthday: '03/11/1980',
59
- owner_phone: '5130405060',
60
- owner_cpf: '52211670695'
53
+ 'logo' => 'visa',
54
+ 'card_number' => '4916654211627608',
55
+ 'expiration_date' => '06/15',
56
+ 'security_code' => '000',
57
+ 'owner_name' => 'Juquinha da Rocha',
58
+ 'owner_birthday' => '03/11/1980',
59
+ 'owner_phone' => '5130405060',
60
+ 'owner_cpf' => '52211670695'
61
61
  }
62
62
 
63
63
  payer_attrs = {
64
- id: 'payer_id_defined_by_you',
65
- name: 'Juquinha da Rocha',
66
- email: 'juquinha@rocha.com',
67
- address_street: 'Felipe Neri',
68
- address_street_number: '406',
69
- address_street_extra: 'Sala 501',
70
- address_neighbourhood: 'Auxiliadora',
71
- address_city: 'Porto Alegre',
72
- address_state: 'RS',
73
- address_country: 'BRA',
74
- address_cep: '90440150',
75
- address_phone: '5130405060'
64
+ 'id' => 'payer_id_defined_by_you',
65
+ 'name' => 'Juquinha da Rocha',
66
+ 'email' => 'juquinha@rocha.com',
67
+ 'address_street' => 'Felipe Neri',
68
+ 'address_street_number' => '406',
69
+ 'address_street_extra' => 'Sala 501',
70
+ 'address_neighbourhood' => 'Auxiliadora',
71
+ 'address_city' => 'Porto Alegre',
72
+ 'address_state' => 'RS',
73
+ 'address_country' => 'BRA',
74
+ 'address_cep' => '90440150',
75
+ 'address_phone' => '5130405060'
76
76
  }
77
77
 
78
78
  purchase = MyMoip::Purchase.new(
@@ -41,8 +41,12 @@ module MyMoip
41
41
  root.Comissionamento do |n1|
42
42
  n1.Razao(reason)
43
43
  n1.Comissionado {|n2| n2.LoginMoIP(receiver_login)}
44
- n1.ValorFixo(fixed_value) if fixed_value
45
- n1.ValorPercentual(percentage_value) if percentage_value
44
+ if fixed_value
45
+ n1.ValorFixo(sprintf('%.2f', fixed_value))
46
+ end
47
+ if percentage_value
48
+ n1.ValorPercentual(sprintf('%.2f', percentage_value * 100))
49
+ end
46
50
  end
47
51
 
48
52
  xml
@@ -16,17 +16,9 @@ module MyMoip
16
16
  validates_inclusion_of :logo, in: AVAILABLE_LOGOS
17
17
 
18
18
  def initialize(attrs)
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
-
28
- # Deprecated attributes
29
- self.owner_rg = attrs[:owner_rg] if attrs.has_key?(:owner_rg)
19
+ attrs.each do |attr, value|
20
+ public_send(:"#{attr}=", value)
21
+ end
30
22
  end
31
23
 
32
24
  def logo=(value)
data/lib/mymoip/payer.rb CHANGED
@@ -17,18 +17,9 @@ module MyMoip
17
17
  validates_length_of :address_phone, within: 10..11
18
18
 
19
19
  def initialize(attrs)
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]
20
+ attrs.each do |attr, value|
21
+ public_send(:"#{attr}=", value)
22
+ end
32
23
  end
33
24
 
34
25
  def address_cep=(value)
@@ -0,0 +1,40 @@
1
+ module MyMoip
2
+ class Purchase
3
+ attr_accessor :id, :price, :credit_card, :payer, :reason
4
+ attr_reader :code
5
+
6
+ def initialize(attrs)
7
+ @id = attrs.fetch(:id) { rand }
8
+ @price = attrs.fetch(:price)
9
+ @credit_card = MyMoip::CreditCard.new(attrs.fetch(:credit_card))
10
+ @payer = MyMoip::Payer.new(attrs.fetch(:payer))
11
+ @reason = attrs.fetch(:reason)
12
+ end
13
+
14
+ def checkout!
15
+ authorization = get_authorization!
16
+ payment = MyMoip::CreditCardPayment.new(@credit_card,
17
+ installments: 1)
18
+ request = MyMoip::PaymentRequest.new(@id)
19
+ request.api_call(payment, token: authorization.token)
20
+
21
+ @code = request.code
22
+ request.success?
23
+ end
24
+
25
+ private
26
+
27
+ def get_authorization!
28
+ instruction = MyMoip::Instruction.new(
29
+ id: @id,
30
+ payment_reason: @reason,
31
+ values: [@price],
32
+ payer: @payer
33
+ )
34
+
35
+ request = MyMoip::TransparentRequest.new(@id)
36
+ request.api_call(instruction)
37
+ request
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,3 @@
1
+ module MyMoip
2
+ VERSION = '0.6.2'
3
+ end
data/mymoip.gemspec CHANGED
@@ -1,91 +1,30 @@
1
- # Generated by jeweler
2
- # DO NOT EDIT THIS FILE DIRECTLY
3
- # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
- # -*- encoding: utf-8 -*-
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'mymoip/version'
5
5
 
6
- Gem::Specification.new do |s|
7
- s.name = "mymoip"
8
- s.version = "0.6.1"
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "mymoip"
8
+ spec.version = MyMoip::VERSION
9
+ spec.authors = ["Irio Irineu Musskopf Junior"]
10
+ spec.email = ["iirineu@gmail.com"]
11
+ spec.description = %q{The easier way to use Moip's transparent checkout.}
12
+ spec.summary = %q{MoIP transactions in a gem to call your own.}
13
+ spec.homepage = "https://github.com/Irio/mymoip"
14
+ spec.license = "MIT"
9
15
 
10
- s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
- s.authors = ["Irio Irineu Musskopf Junior"]
12
- s.date = "2013-06-13"
13
- s.description = "Provides a implementation of MoIP's transparent checkout."
14
- s.email = "irio.musskopf@caixadeideias.com.br"
15
- s.extra_rdoc_files = [
16
- "LICENSE.txt",
17
- "README.md"
18
- ]
19
- s.files = [
20
- ".document",
21
- ".travis.yml",
22
- "CHANGELOG.md",
23
- "Gemfile",
24
- "LICENSE.txt",
25
- "README.md",
26
- "Rakefile",
27
- "lib/mymoip.rb",
28
- "lib/mymoip/commission.rb",
29
- "lib/mymoip/credit_card.rb",
30
- "lib/mymoip/credit_card_payment.rb",
31
- "lib/mymoip/exceptions.rb",
32
- "lib/mymoip/formatter.rb",
33
- "lib/mymoip/instruction.rb",
34
- "lib/mymoip/json_parser.rb",
35
- "lib/mymoip/payer.rb",
36
- "lib/mymoip/request.rb",
37
- "lib/mymoip/requests/payment_request.rb",
38
- "lib/mymoip/requests/transparent_request.rb",
39
- "mymoip.gemspec",
40
- "test/fixtures/fixture.rb",
41
- "test/fixtures/vcr_cassettes/payment_request.yml",
42
- "test/fixtures/vcr_cassettes/transparent_request.yml",
43
- "test/fixtures/vcr_cassettes/transparent_request_with_commissions.yml",
44
- "test/live_test.rb"
45
- ]
46
- s.homepage = "http://github.com/Irio/mymoip"
47
- s.licenses = ["MIT"]
48
- s.require_paths = ["lib"]
49
- s.rubygems_version = "1.8.23"
50
- s.summary = "MoIP transactions in a gem to call your own."
16
+ spec.files = `git ls-files`.split($/)
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"]
51
20
 
52
- if s.respond_to? :specification_version then
53
- s.specification_version = 3
54
-
55
- if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
56
- s.add_runtime_dependency(%q<builder>, [">= 0"])
57
- s.add_runtime_dependency(%q<httparty>, [">= 0"])
58
- s.add_runtime_dependency(%q<activemodel>, [">= 0"])
59
- s.add_development_dependency(%q<rdoc>, ["~> 3.12"])
60
- s.add_development_dependency(%q<bundler>, [">= 0"])
61
- s.add_development_dependency(%q<jeweler>, ["~> 1.8.4"])
62
- s.add_development_dependency(%q<turn>, [">= 0"])
63
- s.add_development_dependency(%q<mocha>, [">= 0"])
64
- s.add_development_dependency(%q<vcr>, [">= 0"])
65
- s.add_development_dependency(%q<webmock>, [">= 0"])
66
- else
67
- s.add_dependency(%q<builder>, [">= 0"])
68
- s.add_dependency(%q<httparty>, [">= 0"])
69
- s.add_dependency(%q<activemodel>, [">= 0"])
70
- s.add_dependency(%q<rdoc>, ["~> 3.12"])
71
- s.add_dependency(%q<bundler>, [">= 0"])
72
- s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
73
- s.add_dependency(%q<turn>, [">= 0"])
74
- s.add_dependency(%q<mocha>, [">= 0"])
75
- s.add_dependency(%q<vcr>, [">= 0"])
76
- s.add_dependency(%q<webmock>, [">= 0"])
77
- end
78
- else
79
- s.add_dependency(%q<builder>, [">= 0"])
80
- s.add_dependency(%q<httparty>, [">= 0"])
81
- s.add_dependency(%q<activemodel>, [">= 0"])
82
- s.add_dependency(%q<rdoc>, ["~> 3.12"])
83
- s.add_dependency(%q<bundler>, [">= 0"])
84
- s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
85
- s.add_dependency(%q<turn>, [">= 0"])
86
- s.add_dependency(%q<mocha>, [">= 0"])
87
- s.add_dependency(%q<vcr>, [">= 0"])
88
- s.add_dependency(%q<webmock>, [">= 0"])
89
- end
21
+ spec.add_dependency "activemodel"
22
+ spec.add_dependency "builder"
23
+ spec.add_dependency "httparty"
24
+ spec.add_development_dependency "bundler", "~> 1.3"
25
+ spec.add_development_dependency "mocha"
26
+ spec.add_development_dependency "rake"
27
+ spec.add_development_dependency "rdoc", "~> 3.12"
28
+ spec.add_development_dependency "vcr"
29
+ spec.add_development_dependency "webmock"
90
30
  end
91
-
@@ -0,0 +1,121 @@
1
+ require_relative '../test_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 MyMoip::InvalidComission 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.00</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>15.00</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 MyMoip::InvalidComission do
118
+ subject.to_xml
119
+ end
120
+ end
121
+ end
@@ -0,0 +1,114 @@
1
+ require_relative '../test_helper'
2
+
3
+ class TestCreditCardPayment < Test::Unit::TestCase
4
+ def test_initialization_and_getters
5
+ credit_card = Fixture.credit_card
6
+ subject = MyMoip::CreditCardPayment.new(credit_card, 1)
7
+ assert_equal credit_card, subject.credit_card
8
+ assert_equal 1, subject.installments
9
+ end
10
+
11
+ def test_allow_initialization_with_a_hash_of_options
12
+ credit_card = Fixture.credit_card
13
+ subject = MyMoip::CreditCardPayment.new(credit_card, installments: 2)
14
+ assert_equal credit_card, subject.credit_card
15
+ assert_equal 2, subject.installments
16
+ end
17
+
18
+ def test_cash_method_with_one_tranch
19
+ credit_card = Fixture.credit_card
20
+ subject = MyMoip::CreditCardPayment.new(credit_card, 1)
21
+ assert_equal true, subject.cash?
22
+ end
23
+
24
+ def test_cash_method_with_more_than_one_installments
25
+ credit_card = Fixture.credit_card
26
+ subject = MyMoip::CreditCardPayment.new(credit_card, 3)
27
+ assert_equal false, subject.cash?
28
+ end
29
+
30
+ def test_default_initialization_with_one_tranch
31
+ credit_card = Fixture.credit_card
32
+ subject = MyMoip::CreditCardPayment.new(credit_card)
33
+ assert_equal 1, subject.installments
34
+ end
35
+
36
+ def test_json_format
37
+ payment = MyMoip::CreditCardPayment.new(Fixture.credit_card)
38
+ assert_equal "CartaoCredito", payment.to_json[:Forma]
39
+ assert payment.to_json[:Parcelas].kind_of?(Integer), "'Parcelas' must be a kind of integer."
40
+ end
41
+
42
+ def test_json_credit_card_format
43
+ payment = MyMoip::CreditCardPayment.new(Fixture.credit_card)
44
+ assert_match /\A\d+\z/, payment.to_json[:CartaoCredito][:Numero]
45
+ assert_match /\A((0[1-9])|(1[02]))\/\d{2}\z/, payment.to_json[:CartaoCredito][:Expiracao]
46
+ assert_match /\A\d{3}\z/, payment.to_json[:CartaoCredito][:CodigoSeguranca]
47
+ original_date = Date.new(1980, 11, 3)
48
+ MyMoip::Formatter.stubs(:date).returns('03/11/1980')
49
+ assert_equal '03/11/1980', payment.to_json[:CartaoCredito][:Portador][:DataNascimento]
50
+ assert_match /\A\(\d{2}\)\d{4,5}-\d{4}/, payment.to_json[:CartaoCredito][:Portador][:Telefone]
51
+ assert_match /\A\d{3}\.\d{3}\.\d{3}\-\d{2}\z/, payment.to_json[:CartaoCredito][:Portador][:Identidade]
52
+ end
53
+
54
+ def test_to_json_should_accept_any_creditcard_from_available_logos_constant
55
+ MyMoip::CreditCard::AVAILABLE_LOGOS.each do |logo|
56
+ payment = MyMoip::CreditCardPayment.new(Fixture.credit_card(logo: logo))
57
+ assert_nothing_raised(KeyError) { payment.to_json }
58
+ end
59
+ end
60
+
61
+ def test_to_json_method_uses_the_formatted_version_of_the_credit_cards_owner_birthday
62
+ date = Date.new(2040, 10, 30)
63
+ subject = MyMoip::CreditCardPayment.new(Fixture.credit_card(owner_birthday: date))
64
+ formatter = stub_everything('formatter')
65
+ formatter.expects(:date).with(date)
66
+ subject.to_json(formatter)
67
+ end
68
+
69
+ def test_to_json_method_skip_formatting_when_credit_cards_owner_birthday_is_nil
70
+ subject = MyMoip::CreditCardPayment.new(Fixture.credit_card(owner_birthday: nil))
71
+ formatter = stub_everything('formatter')
72
+ formatter.stubs(:date).with(nil).returns('should not be here')
73
+ json = subject.to_json(formatter)
74
+ assert_nil json[:CartaoCredito][:Portador][:DataNascimento]
75
+ end
76
+
77
+ def test_to_json_method_uses_the_formatted_version_of_the_credit_cards_owner_phone
78
+ subject = MyMoip::CreditCardPayment.new(Fixture.credit_card(owner_phone: '5130405060'))
79
+ formatter = stub_everything('formatter')
80
+ formatter.expects(:phone).with('5130405060')
81
+ subject.to_json(formatter)
82
+ end
83
+
84
+ def test_to_json_method_skip_formatting_when_credit_cards_owner_phone_is_nil
85
+ subject = MyMoip::CreditCardPayment.new(Fixture.credit_card(owner_phone: nil))
86
+ formatter = stub_everything('formatter')
87
+ formatter.stubs(:phone).with(nil).returns('should not be here')
88
+ json = subject.to_json(formatter)
89
+ assert_nil json[:CartaoCredito][:Portador][:Telefone]
90
+ end
91
+
92
+ def test_to_json_method_raises_an_exception_when_called_without_a_credit_card
93
+ subject = MyMoip::CreditCardPayment.new(nil)
94
+ assert_raise MyMoip::InvalidCreditCard do
95
+ subject.to_json
96
+ end
97
+ end
98
+
99
+ def test_to_json_method_skip_formatting_when_credit_cards_owner_cpf_is_nil
100
+ subject = MyMoip::CreditCardPayment.new(Fixture.credit_card(owner_cpf: nil))
101
+ formatter = stub_everything('formatter')
102
+ formatter.stubs(:cpf).with(nil).returns('should not be here')
103
+ json = subject.to_json(formatter)
104
+ assert_nil json[:CartaoCredito][:Portador][:Identidade]
105
+ end
106
+
107
+ def test_to_json_method_raises_an_exception_when_called_with_a_invalid_credit_card
108
+ subject = MyMoip::CreditCardPayment.new(Fixture.credit_card)
109
+ MyMoip::CreditCard.any_instance.stubs(:invalid?).returns(true)
110
+ assert_raise MyMoip::InvalidCreditCard do
111
+ subject.to_json
112
+ end
113
+ end
114
+ end