getnet_api 1.0.2 → 1.1.0

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.
Files changed (41) hide show
  1. checksums.yaml +5 -5
  2. data/.github/workflows/ruby.yml +40 -0
  3. data/.gitignore +2 -0
  4. data/.rspec +1 -0
  5. data/Gemfile +11 -1
  6. data/Gemfile.lock +63 -10
  7. data/README.md +31 -6
  8. data/lib/getnet_api/base.rb +14 -18
  9. data/lib/getnet_api/boleto.rb +0 -1
  10. data/lib/getnet_api/configure.rb +1 -1
  11. data/lib/getnet_api/customer.rb +5 -5
  12. data/lib/getnet_api/payment.rb +23 -29
  13. data/lib/getnet_api/payment_cancel.rb +1 -1
  14. data/lib/getnet_api/pix.rb +35 -0
  15. data/lib/getnet_api/version.rb +2 -2
  16. data/lib/getnet_api.rb +5 -5
  17. data/spec/lib/getnet_api/address_spec.rb +77 -0
  18. data/spec/lib/getnet_api/base_spec.rb +63 -0
  19. data/spec/lib/getnet_api/boleto_spec.rb +49 -0
  20. data/spec/lib/getnet_api/card_spec.rb +69 -0
  21. data/spec/lib/getnet_api/card_token_spec.rb +15 -0
  22. data/spec/lib/getnet_api/card_verification_spec.rb +30 -0
  23. data/spec/lib/getnet_api/configure_spec.rb +70 -0
  24. data/spec/lib/getnet_api/credit_spec.rb +68 -0
  25. data/spec/lib/getnet_api/customer_spec.rb +130 -0
  26. data/spec/lib/getnet_api/order_spec.rb +30 -0
  27. data/spec/lib/getnet_api/payment_cancel_spec.rb +121 -0
  28. data/spec/lib/getnet_api/payment_spec.rb +163 -0
  29. data/spec/spec_helper.rb +121 -0
  30. data/spec/vcr_cassettes/GetnetApi_Base/_build_request/builds_and_executes_request_based_on.yml +113 -0
  31. data/spec/vcr_cassettes/GetnetApi_CardVerification/_verify/performs_the_request_and_returns_the_verification_result.yml +110 -0
  32. data/spec/vcr_cassettes/GetnetApi_Customer/_create/performs_the_request.yml +64 -0
  33. data/spec/vcr_cassettes/GetnetApi_Customer/_create/performs_the_request_and_returns_the_number_token.yml +112 -0
  34. data/spec/vcr_cassettes/GetnetApi_Payment/_create/test/correctly_requests_for_cancelling.yml +67 -0
  35. data/spec/vcr_cassettes/GetnetApi_Payment/_create/when_cancelling_a_payment/correctly_execute_the_request.yml +118 -0
  36. data/spec/vcr_cassettes/GetnetApi_Payment/_create/when_paying_by_boleto/correctly_requests_for_boleto_payment.yml +60 -0
  37. data/spec/vcr_cassettes/GetnetApi_Payment/_create/when_paying_by_credit/correctly_requests_for_credit_payment.yml +63 -0
  38. data/spec/vcr_cassettes/getnet_api/base/valid_bearer.yml +58 -0
  39. data/spec/vcr_cassettes/getnet_api/cardtoken/get.yml +55 -0
  40. data/spec/vcr_cassettes/getnet_api/payment_cancel/create.yml +62 -0
  41. metadata +58 -8
@@ -0,0 +1,63 @@
1
+ # frozen_string_literal: true
2
+
3
+ describe GetnetApi::Base do
4
+ describe '.build_request', :vcr do
5
+ it 'builds and executes request based on' do
6
+ GetnetApi.ambiente = :sandbox
7
+ response = GetnetApi::Base.build_request 'some_endpoint', 'get'
8
+ expect(response).to be_a Net::HTTPNotFound
9
+ expected_uri = 'https://api-sandbox.getnet.com.br/v1/some_endpoint'
10
+ expect(WebMock).to have_requested(:get, expected_uri)
11
+ end
12
+ end
13
+
14
+ describe '.default_headers' do
15
+ let(:dummy_url) { 'someurl.com' }
16
+ let(:dummy_request) { Net::HTTP::Get.new(dummy_url) }
17
+ it 'creates headers for request' do
18
+ VCR.use_cassette 'getnet_api/base/valid_bearer' do
19
+ request = GetnetApi::Base.default_headers dummy_request
20
+ expected_auth = "Bearer #{GetnetApi::Base.valid_bearer}"
21
+ expect(request['authorization']).to eq expected_auth
22
+ expect(request['Content-Type']).to eq 'application/json'
23
+ expect(request['seller_id']).to eq GetnetApi.seller_id
24
+ end
25
+ end
26
+ end
27
+
28
+ describe '.valid_bearer' do
29
+ context 'when token is expired' do
30
+ it 'requests for new access token' do
31
+ VCR.use_cassette 'getnet_api/base/valid_bearer' do
32
+ GetnetApi.expires_in = nil
33
+ expect(GetnetApi::Base.valid_bearer).to be_present
34
+ expect(GetnetApi.expires_in).to be_present
35
+ expect(GetnetApi.expires_in > DateTime.current).to be true
36
+ end
37
+ end
38
+ end
39
+
40
+ context 'when token is not expired' do
41
+ it 'returnes current token' do
42
+ GetnetApi.access_token = 'some_token'
43
+ GetnetApi.expires_in = DateTime.current + 1.day
44
+ expect(GetnetApi::Base.valid_bearer).to be_present
45
+ expect(GetnetApi::Base.valid_bearer).to eq 'some_token'
46
+ end
47
+ end
48
+ end
49
+
50
+ describe '.get_token_de_bearer' do
51
+ context 'when token is expired' do
52
+ it 'creates requests for new access token' do
53
+ VCR.use_cassette 'getnet_api/base/valid_bearer' do
54
+ GetnetApi.expires_in = nil
55
+ GetnetApi::Base.get_token_de_bearer
56
+ expect(GetnetApi::Base.valid_bearer).to be_present
57
+ expect(GetnetApi.expires_in).to be_present
58
+ expect(GetnetApi.expires_in).to be > DateTime.current
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ describe GetnetApi::Boleto do
4
+ let(:boleto) do
5
+ GetnetApi::Boleto.new(
6
+ our_number: '123321123',
7
+ document_number: '12345678',
8
+ instructions: 'Não receber após o vencimento',
9
+ provider: 'santander'
10
+ )
11
+ end
12
+
13
+ it 'is valid with valid attributes' do
14
+ expect(boleto).to be_valid
15
+ end
16
+
17
+ it 'is not valid with our number length > 12' do
18
+ boleto.our_number = '1' * 13
19
+ expect(boleto).not_to be_valid
20
+ expect(boleto.errors.messages[:our_number]).to be_present
21
+ end
22
+
23
+ it 'is not valid with document number length > 15' do
24
+ boleto.document_number = '1' * 16
25
+ expect(boleto).not_to be_valid
26
+ expect(boleto.errors.messages[:document_number]).to be_present
27
+ end
28
+
29
+ it 'is not valid with instructions length > 1000' do
30
+ boleto.instructions = 'A' * 1001
31
+ expect(boleto).not_to be_valid
32
+ expect(boleto.errors.messages[:instructions]).to be_present
33
+ end
34
+
35
+ it 'is not valid with provider length > 40' do
36
+ boleto.provider = 'A' * 41
37
+ expect(boleto).not_to be_valid
38
+ expect(boleto.errors.messages[:provider]).to be_present
39
+ end
40
+
41
+ context '#to_request' do
42
+ it 'returns boleto object as a hash' do
43
+ boleto_hash = boleto.to_request
44
+ boleto_hash.keys.each do |key|
45
+ expect(boleto_hash[key]).to eq boleto.send(key)
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,69 @@
1
+ # frozen_string_literal: true
2
+
3
+ describe GetnetApi::Card do
4
+ let(:card) do
5
+ GetnetApi::Card.new(
6
+ number_token: 'dfe05208b105578c070f806c80abd3af09e246827d29b866cf4ce16c205849977c9496cbf0d0234f42339937f327747075f68763537b90b31389e01231d4d13c',
7
+ cardholder_name: 'JOAO DA SILVA',
8
+ security_code: '123',
9
+ brand: 'Mastercard',
10
+ expiration_month: '12',
11
+ expiration_year: '20'
12
+ )
13
+ end
14
+
15
+ it 'is valid with valid attributes' do
16
+ expect(card).to be_valid
17
+ end
18
+
19
+ it 'is not valid with number token length > 128' do
20
+ card.number_token = '1' * 129
21
+ expect(card).not_to be_valid
22
+ expect(card.errors.messages[:number_token]).to be_present
23
+ end
24
+
25
+ it 'is not valid with cardholder name length > 26' do
26
+ card.cardholder_name = '1' * 27
27
+ expect(card).not_to be_valid
28
+ expect(card.errors.messages[:cardholder_name]).to be_present
29
+ end
30
+
31
+ it 'is not valid with security_code length > 4' do
32
+ card.security_code = 'A' * 5
33
+ expect(card).not_to be_valid
34
+ expect(card.errors.messages[:security_code]).to be_present
35
+ end
36
+
37
+ it 'is not valid with security_code length < 3' do
38
+ card.security_code = 'AA'
39
+ expect(card).not_to be_valid
40
+ expect(card.errors.messages[:security_code]).to be_present
41
+ end
42
+
43
+ it 'is not valid with brand length > 50' do
44
+ card.brand = 'A' * 51
45
+ expect(card).not_to be_valid
46
+ expect(card.errors.messages[:brand]).to be_present
47
+ end
48
+
49
+ it 'is not valid with expiration month length > 2' do
50
+ card.expiration_month = '123'
51
+ expect(card).not_to be_valid
52
+ expect(card.errors.messages[:expiration_month]).to be_present
53
+ end
54
+
55
+ it 'is not valid with expiration year length > 2' do
56
+ card.expiration_year = '123'
57
+ expect(card).not_to be_valid
58
+ expect(card.errors.messages[:expiration_year]).to be_present
59
+ end
60
+
61
+ context '#to_request' do
62
+ it 'returns card object as a hash' do
63
+ card_hash = card.to_request
64
+ card_hash.keys.each do |key|
65
+ expect(card_hash[key]).to eq card.send(key)
66
+ end
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ describe GetnetApi::CardToken do
4
+ describe '.get' do
5
+ it 'performs the request and returns the number_token' do
6
+ VCR.use_cassette 'getnet_api/cardtoken/get' do
7
+ card_number = '5155901222280001' # see https://developers.getnet.com.br/api#section/Cartoes-para-Teste
8
+ response = GetnetApi::CardToken.get card_number
9
+ expected_uri = 'https://api-sandbox.getnet.com.br/v1/tokens/card'
10
+ expect(WebMock).to have_requested(:post, expected_uri)
11
+ expect(response).to have_key("number_token")
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ describe GetnetApi::CardVerification do
4
+ describe '.verify', :vcr do
5
+ let(:card_token) do
6
+ card_number = '5155901222280001' # see https://developers.getnet.com.br/api#section/Cartoes-para-Teste
7
+ GetnetApi::CardToken.get(card_number)['number_token']
8
+ end
9
+
10
+ let(:card) do
11
+ GetnetApi::Card.new(
12
+ number_token: card_token,
13
+ cardholder_name: 'JOAO DA SILVA',
14
+ security_code: '123',
15
+ brand: 'Mastercard',
16
+ expiration_month: '12',
17
+ expiration_year: '20'
18
+ )
19
+ end
20
+
21
+ it 'performs the request and returns the verification result' do
22
+ response = GetnetApi::CardVerification.verify card
23
+ expected_uri = 'https://api-sandbox.getnet.com.br/v1/cards/verification'
24
+ expect(WebMock).to have_requested(:post, expected_uri)
25
+ expect(response['status']).to eq 'VERIFIED'
26
+ expect(response).to have_key 'verification_id'
27
+ expect(response).to have_key 'authorization_code'
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,70 @@
1
+ # frozen_string_literal: true
2
+
3
+ describe GetnetApi::Configure do
4
+ describe '#configure' do
5
+ before do
6
+ GetnetApi.configure do |config|
7
+ config.ambiente = :sandbox
8
+ config.seller_id = 'seller_id'
9
+ config.client_id = 'client_id'
10
+ config.client_secret = 'client_secret'
11
+ end
12
+ end
13
+
14
+ after do
15
+ GetnetApi.configure do |config|
16
+ config.ambiente = :sandbox
17
+ config.seller_id = '67be6e90-00c1-410d-83f5-6d75621effc8'
18
+ config.client_id = '74438877-f00b-4502-8454-de3d7166dc2a'
19
+ config.client_secret = '4eac6920-1b57-4cd1-8799-d877173bfa37'
20
+ end
21
+ end
22
+
23
+ it 'sets attributes to GetnetApi' do
24
+ expect(GetnetApi.seller_id).to eq 'seller_id'
25
+ expect(GetnetApi.client_id).to eq 'client_id'
26
+ expect(GetnetApi.client_secret).to eq 'client_secret'
27
+ end
28
+ end
29
+
30
+ describe '#endpoint' do
31
+ it 'sets and returns endpoint based on ambiente' do
32
+ ambiente = GetnetApi.ambiente
33
+ expected_endpoint = GetnetApi::Configure::URL[ambiente]
34
+ expect(GetnetApi.endpoint).to eq expected_endpoint
35
+ end
36
+ end
37
+
38
+ describe '#set_endpoint' do
39
+ context 'when ambiente is producao' do
40
+ it 'returns endpoint for producao' do
41
+ GetnetApi.ambiente = :producao
42
+ expected_endpoint = GetnetApi::Configure::URL[:producao]
43
+ expect(GetnetApi.set_endpoint).to eq expected_endpoint
44
+ end
45
+ end
46
+
47
+ context 'when ambiente is homologacao' do
48
+ it 'returns endpoint for homologacao' do
49
+ GetnetApi.ambiente = :homologacao
50
+ expected_endpoint = GetnetApi::Configure::URL[:homologacao]
51
+ expect(GetnetApi.set_endpoint).to eq expected_endpoint
52
+ end
53
+ end
54
+
55
+ context 'when ambiente is sandbox' do
56
+ it 'returns endpoint for sandbox' do
57
+ GetnetApi.ambiente = :sandbox
58
+ expected_endpoint = GetnetApi::Configure::URL[:sandbox]
59
+ expect(GetnetApi.set_endpoint).to eq expected_endpoint
60
+ end
61
+ end
62
+ end
63
+
64
+ describe '#base_uri' do
65
+ it 'returnes uri based on endpoint and api version' do
66
+ expected_endpoint = "#{GetnetApi.endpoint}/#{GetnetApi.api_version}"
67
+ expect(GetnetApi.base_uri).to eq expected_endpoint
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,68 @@
1
+ # frozen_string_literal: true
2
+
3
+ describe GetnetApi::Credit do
4
+ let(:card) do
5
+ GetnetApi::Card.new(
6
+ number_token: 'dfe05208b105578c070f806c80abd3af09e246827d29b866cf4ce16c205849977c9496cbf0d0234f42339937f327747075f68763537b90b31389e01231d4d13c',
7
+ cardholder_name: 'JOAO DA SILVA',
8
+ security_code: '123',
9
+ brand: 'Mastercard',
10
+ expiration_month: '12',
11
+ expiration_year: '20'
12
+ )
13
+ end
14
+
15
+ let(:credit) do
16
+ GetnetApi::Credit.new(
17
+ delayed: false,
18
+ authenticated: false,
19
+ pre_authorization: false,
20
+ save_card_data: false,
21
+ transaction_type: 'FULL',
22
+ number_installments: 1,
23
+ soft_descriptor: 'Descrição para fatura',
24
+ dynamic_mcc: 1799,
25
+ card: card
26
+ )
27
+ end
28
+
29
+ it 'is valid with valid attributes' do
30
+ expect(credit).to be_valid
31
+ end
32
+
33
+ it 'is not valid with transaction type length > 22' do
34
+ credit.transaction_type = 'A' * 23
35
+ expect(credit).not_to be_valid
36
+ expect(credit.errors.messages[:transaction_type]).to be_present
37
+ end
38
+
39
+ it 'is not valid without number installments' do
40
+ credit.number_installments = nil
41
+ expect(credit).not_to be_valid
42
+ expect(credit.errors.messages[:number_installments]).to be_present
43
+ end
44
+
45
+ it 'is not valid with soft descriptor length > 22' do
46
+ credit.soft_descriptor = 'A' * 23
47
+ expect(credit).not_to be_valid
48
+ expect(credit.errors.messages[:soft_descriptor]).to be_present
49
+ end
50
+
51
+ it 'is not valid with dynamic mcc length > 10' do
52
+ credit.dynamic_mcc = 'A' * 11
53
+ expect(credit).not_to be_valid
54
+ expect(credit.errors.messages[:dynamic_mcc]).to be_present
55
+ end
56
+
57
+ context '#to_request' do
58
+ it 'returns credit object as a hash' do
59
+ credit_hash = credit.to_request
60
+ hash_keys = credit_hash.keys.reject { |k| k == :card }
61
+ hash_keys.each do |key|
62
+ expect(credit_hash[key]).to eq credit.send(key)
63
+ end
64
+
65
+ expect(credit_hash[:card]).to eq credit.card.to_request
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,130 @@
1
+ # frozen_string_literal: true
2
+
3
+ describe GetnetApi::Customer do
4
+ let(:address) do
5
+ GetnetApi::Address.new(
6
+ street: 'Nome da Rua',
7
+ number: '123',
8
+ complement: 'Complemento',
9
+ district: 'Nome do Bairro',
10
+ city: 'São Paulo',
11
+ state: 'SP',
12
+ country: 'Brasil',
13
+ postal_code: '01010010'
14
+ )
15
+ end
16
+
17
+ let(:customer) do
18
+ GetnetApi::Customer.new(
19
+ customer_id: '123',
20
+ first_name: 'João',
21
+ last_name: 'da Silva',
22
+ name: 'João da Silva',
23
+ email: 'joao@email.com',
24
+ document_type: :pessoa_fisica,
25
+ document_number: '12332112340',
26
+ address: address,
27
+ phone_number: '5551999887766',
28
+ celphone_number: '5551999887766',
29
+ observation: 'O cliente tem interesse no plano x.'
30
+ )
31
+ end
32
+
33
+ it 'is valid with valid attributes' do
34
+ expect(customer).to be_valid
35
+ end
36
+
37
+ it 'is not valid with customer id length > 100' do
38
+ customer.customer_id = '1' * 101
39
+ expect(customer).not_to be_valid
40
+ expect(customer.errors.messages[:customer_id]).to be_present
41
+ end
42
+
43
+ it 'is not valid with first name length > 40' do
44
+ customer.first_name = 'A' * 41
45
+ expect(customer).not_to be_valid
46
+ expect(customer.errors.messages[:first_name]).to be_present
47
+ end
48
+
49
+ it 'is not valid with last name length > 80' do
50
+ customer.last_name = 'A' * 81
51
+ expect(customer).not_to be_valid
52
+ expect(customer.errors.messages[:last_name]).to be_present
53
+ end
54
+
55
+ it 'is not valid with name length > 100' do
56
+ customer.name = 'A' * 101
57
+ expect(customer).not_to be_valid
58
+ expect(customer.errors.messages[:name]).to be_present
59
+ end
60
+
61
+ it 'is not valid with email length > 100' do
62
+ customer.email = 'A' * 101
63
+ expect(customer).not_to be_valid
64
+ expect(customer.errors.messages[:email]).to be_present
65
+ end
66
+
67
+ it 'is not valid with document number length > 15' do
68
+ customer.document_number = '1' * 16
69
+ expect(customer).not_to be_valid
70
+ expect(customer.errors.messages[:document_number]).to be_present
71
+ end
72
+
73
+ it 'is not valid with document number length < 11' do
74
+ customer.document_number = '1' * 10
75
+ expect(customer).not_to be_valid
76
+ expect(customer.errors.messages[:document_number]).to be_present
77
+ end
78
+
79
+ it 'is not valid with phone number length > 15' do
80
+ customer.phone_number = '1' * 16
81
+ expect(customer).not_to be_valid
82
+ expect(customer.errors.messages[:phone_number]).to be_present
83
+ end
84
+
85
+ it 'is not valid with observation length > 140' do
86
+ customer.observation = '1' * 141
87
+ expect(customer).not_to be_valid
88
+ expect(customer.errors.messages[:observation]).to be_present
89
+ end
90
+
91
+ it 'is not valid with address not being the correct object' do
92
+ customer.address = Object.new
93
+ expect(customer).not_to be_valid
94
+ expect(customer.errors.messages[:address]).to be_present
95
+ end
96
+
97
+ it 'is not valid with invalid address' do
98
+ customer.address.street = 'A' * 61
99
+ expect(customer).not_to be_valid
100
+ expect(customer.errors.messages[:address]).to be_present
101
+ end
102
+
103
+ it 'is not valid with document type with invalid key' do
104
+ customer.document_type = :invalid
105
+ expect(customer).not_to be_valid
106
+ expect(customer.errors.messages[:document_type]).to be_present
107
+ end
108
+
109
+ context '#to_request' do
110
+ it 'returns customer object as a hash' do
111
+ customer_hash = customer.to_request(:payment)
112
+ hash_keys = customer_hash.keys.reject do |key|
113
+ %i[billing_address document_type].include?(key)
114
+ end
115
+ hash_keys.each do |key|
116
+ expect(customer_hash[key]).to eq customer.send(key)
117
+ end
118
+ expect(customer_hash[:billing_address]).to eq customer.address.to_request
119
+ expect(customer_hash[:document_type]).to eq 'CPF'
120
+ end
121
+ end
122
+
123
+ describe '.create', :vcr do
124
+ it 'performs the request' do
125
+ GetnetApi::Customer.create customer
126
+ expected_uri = 'https://api-sandbox.getnet.com.br/v1/customers'
127
+ expect(WebMock).to have_requested(:post, expected_uri)
128
+ end
129
+ end
130
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ describe GetnetApi::Order do
4
+ let(:order) do
5
+ GetnetApi::Order.new(
6
+ order_id: '123',
7
+ sales_tax: 0,
8
+ product_type: 'service'
9
+ )
10
+ end
11
+
12
+ it 'is valid with valid attributes' do
13
+ expect(order).to be_valid
14
+ end
15
+
16
+ it 'is not valid with order id length > 36' do
17
+ order.order_id = '1' * 37
18
+ expect(order).not_to be_valid
19
+ expect(order.errors.messages[:order_id]).to be_present
20
+ end
21
+
22
+ context '#to_request' do
23
+ it 'returns order object as a hash' do
24
+ order_hash = order.to_request
25
+ order_hash.keys.each do |key|
26
+ expect(order_hash[key]).to eq order.send(key)
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,121 @@
1
+ # frozen_string_literal: true
2
+
3
+ describe GetnetApi::Payment do
4
+
5
+ let(:card_token) do
6
+ token = ''
7
+ VCR.use_cassette('getnet_api/cardtoken/get') do
8
+ card_number = '5155901222280001' # see https://developers.getnet.com.br/api#section/Cartoes-para-Teste
9
+ token = GetnetApi::CardToken.get(card_number)['number_token']
10
+ end
11
+ end
12
+
13
+ let(:card) do
14
+ GetnetApi::Card.new(
15
+ number_token: card_token,
16
+ cardholder_name: 'JOAO DA SILVA',
17
+ security_code: '123',
18
+ brand: 'Mastercard',
19
+ expiration_month: '12',
20
+ expiration_year: '23'
21
+ )
22
+ end
23
+
24
+ let(:credit) do
25
+ GetnetApi::Credit.new(
26
+ delayed: false,
27
+ authenticated: false,
28
+ pre_authorization: false,
29
+ save_card_data: false,
30
+ transaction_type: 'FULL',
31
+ number_installments: 1,
32
+ soft_descriptor: 'Descrição para fatura',
33
+ dynamic_mcc: 1799,
34
+ card: card
35
+ )
36
+ end
37
+
38
+ let(:address) do
39
+ GetnetApi::Address.new(
40
+ street: 'Nome da Rua',
41
+ number: '123',
42
+ complement: 'Complemento',
43
+ district: 'Nome do Bairro',
44
+ city: 'São Paulo',
45
+ state: 'SP',
46
+ country: 'Brasil',
47
+ postal_code: '01010010'
48
+ )
49
+ end
50
+
51
+ let(:customer) do
52
+ GetnetApi::Customer.new(
53
+ customer_id: '123',
54
+ first_name: 'João',
55
+ last_name: 'da Silva',
56
+ name: 'João da Silva',
57
+ email: 'joao@email.com',
58
+ document_type: :pessoa_fisica,
59
+ document_number: '12332112340',
60
+ address: address,
61
+ phone_number: '5551999887766',
62
+ celphone_number: '5551999887766',
63
+ observation: 'O cliente tem interesse no plano x.'
64
+ )
65
+ end
66
+
67
+ let(:order) do
68
+ GetnetApi::Order.new(
69
+ order_id: '123',
70
+ sales_tax: 0,
71
+ product_type: 'service'
72
+ )
73
+ end
74
+
75
+ let(:payment) do
76
+ GetnetApi::Payment.new(
77
+ amount: 100,
78
+ currency: 'BRL',
79
+ order: order,
80
+ customer: customer
81
+ )
82
+ end
83
+
84
+ let(:payment_cancel) do
85
+ GetnetApi::PaymentCancel.new(
86
+ payment_id: "79447fe6-8672-4187-85de-11c4bab37aab",
87
+ cancel_amount: 100
88
+ )
89
+ end
90
+
91
+ it 'is valid with valid attributes' do
92
+ expect(payment_cancel).to be_valid
93
+ end
94
+
95
+ context '#to_request' do
96
+ it 'returns card object as a hash' do
97
+ payment_cancel_hash = payment_cancel.to_request
98
+ payment_cancel_hash.keys.each do |key|
99
+ expect(payment_cancel_hash[key]).to eq payment_cancel.send(key)
100
+ end
101
+ end
102
+ end
103
+
104
+ context '.create' do
105
+ context 'when cancelling a payment', :vcr do
106
+ it 'correctly execute the request' do
107
+ response = GetnetApi::Payment.create payment, credit, :credit
108
+ payment_id = response['payment_id']
109
+ payment_cancel = GetnetApi::PaymentCancel.new(
110
+ payment_id: payment_id,
111
+ cancel_amount: 100
112
+ )
113
+ response_cancelamento = GetnetApi::PaymentCancel.create payment_cancel
114
+
115
+ expected_uri = "https://api-sandbox.getnet.com.br/v1/payments/credit/#{payment_id}/cancel"
116
+ expect(WebMock).to have_requested(:post, expected_uri)
117
+ expect(response_cancelamento["status"]).to eq("CANCELED")
118
+ end
119
+ end
120
+ end
121
+ end