edools_mymoip 0.8.1

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 (57) hide show
  1. checksums.yaml +7 -0
  2. data/.document +5 -0
  3. data/.gitignore +15 -0
  4. data/.travis.yml +4 -0
  5. data/CHANGELOG.md +128 -0
  6. data/Gemfile +4 -0
  7. data/LICENSE.txt +20 -0
  8. data/README.md +3 -0
  9. data/Rakefile +10 -0
  10. data/lib/mymoip.rb +57 -0
  11. data/lib/mymoip/bank_debit.rb +22 -0
  12. data/lib/mymoip/commission.rb +54 -0
  13. data/lib/mymoip/credit_card.rb +73 -0
  14. data/lib/mymoip/exceptions.rb +15 -0
  15. data/lib/mymoip/formatter.rb +23 -0
  16. data/lib/mymoip/instruction.rb +134 -0
  17. data/lib/mymoip/json_parser.rb +11 -0
  18. data/lib/mymoip/payer.rb +75 -0
  19. data/lib/mymoip/payment.rb +10 -0
  20. data/lib/mymoip/payment_methods.rb +46 -0
  21. data/lib/mymoip/payment_slip.rb +74 -0
  22. data/lib/mymoip/payments/bank_debit_payment.rb +27 -0
  23. data/lib/mymoip/payments/credit_card_payment.rb +53 -0
  24. data/lib/mymoip/payments/payment_slip_payment.rb +12 -0
  25. data/lib/mymoip/purchase.rb +40 -0
  26. data/lib/mymoip/request.rb +34 -0
  27. data/lib/mymoip/requests/payment_request.rb +53 -0
  28. data/lib/mymoip/requests/transparent_request.rb +36 -0
  29. data/lib/mymoip/validators.rb +12 -0
  30. data/lib/mymoip/version.rb +3 -0
  31. data/mymoip.gemspec +30 -0
  32. data/test/fixtures/fixture.rb +73 -0
  33. data/test/fixtures/vcr_cassettes/payment_request.yml +37 -0
  34. data/test/fixtures/vcr_cassettes/payment_request_with_payment_slip.yml +32 -0
  35. data/test/fixtures/vcr_cassettes/transparent_request.yml +34 -0
  36. data/test/fixtures/vcr_cassettes/transparent_request_with_commissions.yml +35 -0
  37. data/test/lib/test_bank_debit.rb +36 -0
  38. data/test/lib/test_bank_debit_payment.rb +32 -0
  39. data/test/lib/test_commission.rb +121 -0
  40. data/test/lib/test_credit_card_payment.rb +107 -0
  41. data/test/lib/test_creditcard.rb +206 -0
  42. data/test/lib/test_formatter.rb +49 -0
  43. data/test/lib/test_instruction.rb +243 -0
  44. data/test/lib/test_mymoip.rb +79 -0
  45. data/test/lib/test_payer.rb +249 -0
  46. data/test/lib/test_payment.rb +15 -0
  47. data/test/lib/test_payment_methods.rb +54 -0
  48. data/test/lib/test_payment_request.rb +120 -0
  49. data/test/lib/test_payment_slip.rb +78 -0
  50. data/test/lib/test_payment_slip_payment.rb +8 -0
  51. data/test/lib/test_purchase.rb +109 -0
  52. data/test/lib/test_request.rb +88 -0
  53. data/test/lib/test_transparent_request.rb +71 -0
  54. data/test/lib/test_validators.rb +13 -0
  55. data/test/live_test.rb +4 -0
  56. data/test/test_helper.rb +17 -0
  57. metadata +251 -0
@@ -0,0 +1,15 @@
1
+ require_relative '../test_helper'
2
+
3
+ class TestPayment < Test::Unit::TestCase
4
+ def test_payment_method_when_payment_split
5
+ assert_equal :payment_slip, MyMoip::PaymentSlipPayment.payment_method
6
+ end
7
+
8
+ def test_payment_method_when_bank_debit
9
+ assert_equal :bank_debit, MyMoip::BankDebitPayment.payment_method
10
+ end
11
+
12
+ def test_payment_method_when_credit_card
13
+ assert_equal :credit_card, MyMoip::CreditCardPayment.payment_method
14
+ end
15
+ end
@@ -0,0 +1,54 @@
1
+ require_relative '../test_helper'
2
+
3
+ class TestPaymentMethods < Test::Unit::TestCase
4
+ def test_initialization_and_setters
5
+ params = {
6
+ payment_slip: true,
7
+ credit_card: false,
8
+ debit: true,
9
+ debit_card: false,
10
+ financing: true,
11
+ moip_wallet: false
12
+ }
13
+
14
+ subject = MyMoip::PaymentMethods.new(params)
15
+ assert_equal params[:payment_slip], subject.payment_slip
16
+ assert_equal params[:credit_card], subject.credit_card
17
+ assert_equal params[:debit], subject.debit
18
+ assert_equal params[:debit_card], subject.debit_card
19
+ assert_equal params[:financing], subject.financing
20
+ assert_equal params[:moip_wallet], subject.moip_wallet
21
+ end
22
+
23
+ def test_validates_boolean
24
+ subject = MyMoip::PaymentMethods.new
25
+ assert subject.valid?
26
+
27
+ subject.payment_slip = 1
28
+ subject.credit_card = 1
29
+ subject.debit = 1
30
+ subject.debit_card = 1
31
+ subject.financing = 1
32
+ subject.moip_wallet = 1
33
+
34
+ assert subject.invalid? &&
35
+ subject.errors[:payment_slip].present? &&
36
+ subject.errors[:credit_card].present? &&
37
+ subject.errors[:debit].present? &&
38
+ subject.errors[:debit_card].present? &&
39
+ subject.errors[:financing].present? &&
40
+ subject.errors[:moip_wallet].present?
41
+ end
42
+
43
+ def test_xml_format
44
+ subject = MyMoip::PaymentMethods.new
45
+ assert_equal '', subject.to_xml
46
+
47
+ subject.payment_slip = false
48
+ expected_format = <<XML
49
+ <FormaPagamento>CartaoDeCredito</FormaPagamento><FormaPagamento>DebitoBancario</FormaPagamento><FormaPagamento>CartaoDeDebito</FormaPagamento><FormaPagamento>FinanciamentoBancario</FormaPagamento><FormaPagamento>CarteiraMoIP</FormaPagamento>
50
+ XML
51
+ assert_equal expected_format.strip, subject.to_xml
52
+ end
53
+
54
+ end
@@ -0,0 +1,120 @@
1
+ # encoding: UTF-8
2
+ require_relative '../test_helper'
3
+
4
+ class TestPaymentRequest < Test::Unit::TestCase
5
+ def test_http_method_as_get
6
+ assert_equal :get, MyMoip::PaymentRequest::HTTP_METHOD
7
+ end
8
+
9
+ def test_path
10
+ assert_equal "/rest/pagamento?callback=?", MyMoip::PaymentRequest::PATH
11
+ end
12
+
13
+ def test_non_auth_requirement
14
+ assert_equal false, MyMoip::PaymentRequest::REQUIRES_AUTH
15
+ end
16
+
17
+ def test_generate_json
18
+ HTTParty.stubs(:send).returns("<html>some_result</html>")
19
+ request = MyMoip::PaymentRequest.new("id")
20
+
21
+ JSON.expects(:generate).with(
22
+ pagamentoWidget: {
23
+ referer: "http://localhost",
24
+ token: "big_transparent_token",
25
+ dadosPagamento: {payment: "attributes"}
26
+ }
27
+ )
28
+ request_data = stub(to_json: {payment: "attributes"})
29
+ request.api_call(request_data, token: "big_transparent_token", referer_url: "http://localhost")
30
+ end
31
+
32
+ def test_gets_default_referer_if_another_isnt_passed
33
+ MyMoip.default_referer_url = "http://localhost/default"
34
+ HTTParty.stubs(:send).returns("<html>some_result</html>")
35
+ request = MyMoip::PaymentRequest.new("id")
36
+
37
+ JSON.expects(:generate).with(
38
+ pagamentoWidget: {
39
+ referer: MyMoip.default_referer_url,
40
+ token: "big_transparent_token",
41
+ dadosPagamento: {payment: "attributes"}
42
+ }
43
+ )
44
+ request_data = stub(to_json: {payment: "attributes"})
45
+ request.api_call(request_data, token: "big_transparent_token")
46
+ end
47
+
48
+ def test_successful_status
49
+ MyMoip.default_referer_url = "http://localhost/default"
50
+ HTTParty.stubs(:send).returns(
51
+ JSON.parse '{"Status":"EmAnalise","Codigo":0,"CodigoRetorno":"","TaxaMoIP":"7.79","StatusPagamento":"Sucesso","Classificacao":{"Codigo":999,"Descricao":"Não suportado no ambiente Sandbox"},"CodigoMoIP":77316,"Mensagem":"Requisição processada com sucesso","TotalPago":"100.00"}'
52
+ )
53
+ request = MyMoip::PaymentRequest.new("id")
54
+
55
+ request_data = stub(to_json: {payment: "attributes"})
56
+ request.api_call(request_data, token: "big_transparent_token")
57
+ assert request.success?
58
+ end
59
+
60
+ def test_success_method_returns_false_in_payments_already_made
61
+ MyMoip.default_referer_url = "http://localhost/default"
62
+ HTTParty.stubs(:send).returns(
63
+ JSON.parse '{"Codigo":236,"StatusPagamento":"Falha","Mensagem":"Pagamento já foi realizado"}'
64
+ )
65
+ request = MyMoip::PaymentRequest.new("id")
66
+
67
+ request_data = stub(to_json: {payment: "attributes"})
68
+ request.api_call(request_data, token: "big_transparent_token")
69
+ assert !request.success?
70
+ end
71
+
72
+ def test_method_to_get_moip_code
73
+ instruction = Fixture.instruction(payer: Fixture.payer)
74
+ transparent_request = MyMoip::TransparentRequest.new("your_own_id")
75
+ VCR.use_cassette('transparent_request') do
76
+ transparent_request.api_call(instruction)
77
+ end
78
+ credit_card_payment = MyMoip::CreditCardPayment.new(Fixture.credit_card, installments: 1)
79
+ payment_request = MyMoip::PaymentRequest.new("your_own_id")
80
+ VCR.use_cassette('payment_request') do
81
+ payment_request.api_call(credit_card_payment, token: transparent_request.token)
82
+ end
83
+ assert_equal 102596, payment_request.code
84
+ end
85
+
86
+ def test_code_method_should_return_nil_with_blank_response
87
+ instruction = Fixture.instruction(payer: Fixture.payer)
88
+ transparent_request = MyMoip::TransparentRequest.new("your_own_id")
89
+ VCR.use_cassette('transparent_request') do
90
+ transparent_request.api_call(instruction)
91
+ end
92
+ credit_card_payment = MyMoip::CreditCardPayment.new(Fixture.credit_card, installments: 1)
93
+ payment_request = MyMoip::PaymentRequest.new("your_own_id")
94
+ assert_nil payment_request.code
95
+ end
96
+
97
+ def test_method_to_get_response_url
98
+ instruction = Fixture.instruction(payer: Fixture.payer)
99
+ transparent_request = MyMoip::TransparentRequest.new("your_own_id")
100
+ VCR.use_cassette('transparent_request') do
101
+ transparent_request.api_call(instruction)
102
+ end
103
+ payment_slip_payment = MyMoip::PaymentSlipPayment.new
104
+ payment_request = MyMoip::PaymentRequest.new("your_own_id")
105
+ VCR.use_cassette('payment_request_with_payment_slip') do
106
+ payment_request.api_call(payment_slip_payment, token: transparent_request.token)
107
+ end
108
+ assert_equal "https://desenvolvedor.moip.com.br/sandbox/Instrucao.do?token=#{transparent_request.token}", payment_request.url
109
+ end
110
+
111
+ def test_url_method_should_return_nil_with_blank_response
112
+ instruction = Fixture.instruction(payer: Fixture.payer)
113
+ transparent_request = MyMoip::TransparentRequest.new("your_own_id")
114
+ VCR.use_cassette('transparent_request') do
115
+ transparent_request.api_call(instruction)
116
+ end
117
+ payment_request = MyMoip::PaymentRequest.new("your_own_id")
118
+ assert_nil payment_request.url
119
+ end
120
+ end
@@ -0,0 +1,78 @@
1
+ require_relative '../test_helper'
2
+
3
+ class TestPaymentSlip < Test::Unit::TestCase
4
+ def test_validate_length_of_instruction_lines
5
+ subject = Fixture.payment_slip()
6
+ assert subject.valid?
7
+
8
+ subject.instruction_line_1 = ('*' * 63)
9
+ subject.instruction_line_2 = ('*' * 63)
10
+ subject.instruction_line_3 = ('*' * 63)
11
+
12
+ assert subject.valid?
13
+
14
+ subject.instruction_line_1 = ('*' * 64)
15
+ subject.instruction_line_2 = ('*' * 64)
16
+ subject.instruction_line_3 = ('*' * 64)
17
+
18
+ assert subject.invalid?
19
+ assert subject.errors[:instruction_line_1].present? &&
20
+ subject.errors[:instruction_line_2].present? &&
21
+ subject.errors[:instruction_line_3].present?
22
+ end
23
+
24
+ def test_validate_expiration_days
25
+ subject = Fixture.payment_slip(expiration_days: 99)
26
+ assert subject.valid?
27
+
28
+ subject.expiration_days = 100
29
+ assert subject.invalid? && subject.errors[:expiration_days].present?
30
+ end
31
+
32
+ def test_validate_expiration_type
33
+ subject = Fixture.payment_slip(expiration_days_type: :calendar_day)
34
+ assert subject.valid?
35
+
36
+ subject.expiration_days_type = :business_day
37
+ assert subject.valid?
38
+
39
+ subject.expiration_days_type = :another_type
40
+ assert subject.invalid? && subject.errors[:expiration_days_type].present?
41
+ end
42
+
43
+ def test_validate_logo_url
44
+ subject = Fixture.payment_slip(logo_url: 'http://www.uol.com.br')
45
+ assert subject.valid?
46
+
47
+ subject.logo_url = 'https://www.google.com/sdfsdf.png'
48
+ assert subject.valid?
49
+
50
+ subject.logo_url = 'file://www.google.com/sdfsdf.png'
51
+ assert subject.invalid? && subject.errors[:logo_url].present?
52
+ end
53
+
54
+ def test_validate_expiration_date_format
55
+ subject = Fixture.payment_slip(expiration_date: DateTime.new)
56
+ assert subject.valid?
57
+
58
+ subject.expiration_date = Date.new
59
+ assert subject.invalid? && subject.errors[:expiration_date].present?
60
+ end
61
+
62
+ def test_xml_format
63
+ subject = Fixture.payment_slip()
64
+ expected_format = <<XML
65
+ <DataVencimento>2020-01-01T00:00:00.000+00:00</DataVencimento><DiasExpiracao Tipo="Uteis">7</DiasExpiracao><Instrucao1>Line 1</Instrucao1><Instrucao2>Line 2</Instrucao2><Instrucao3>Line 3</Instrucao3><URLLogo>http://www.myurl.com/logo.png</URLLogo>
66
+ XML
67
+ assert_equal expected_format.rstrip, subject.to_xml
68
+ end
69
+
70
+ def test_xml_method_raises_exception_when_called_with_invalid_params
71
+ subject = Fixture.payment_slip
72
+ subject.stubs(:invalid?).returns(true)
73
+ assert_raise MyMoip::InvalidPaymentSlip do
74
+ subject.to_xml
75
+ end
76
+ end
77
+
78
+ end
@@ -0,0 +1,8 @@
1
+ require_relative '../test_helper'
2
+
3
+ class TestPaymentSlipPayment < Test::Unit::TestCase
4
+ def test_json_format
5
+ payment = MyMoip::PaymentSlipPayment.new()
6
+ assert_equal 'BoletoBancario', payment.to_json[:Forma]
7
+ end
8
+ end
@@ -0,0 +1,109 @@
1
+ require_relative '../test_helper'
2
+
3
+ def cc_attrs(attrs = {})
4
+ {
5
+ logo: :visa,
6
+ card_number: '4916654211627608',
7
+ expiration_date: '06/15',
8
+ security_code: '000',
9
+ owner_name: 'Juquinha da Rocha',
10
+ owner_birthday: '03/11/1980',
11
+ owner_phone: '5130405060',
12
+ owner_cpf: '52211670695'
13
+ }.merge(attrs)
14
+ end
15
+
16
+ def payer_attrs(attrs = {})
17
+ {
18
+ id: rand,
19
+ name: 'Juquinha da Rocha',
20
+ email: 'juquinha@rocha.com',
21
+ address_street: 'Felipe Neri',
22
+ address_street_number: '406',
23
+ address_street_extra: 'Sala 501',
24
+ address_neighbourhood: 'Auxiliadora',
25
+ address_city: 'Porto Alegre',
26
+ address_state: 'RS',
27
+ address_country: 'BRA',
28
+ address_cep: '90440150',
29
+ address_phone: '5130405060'
30
+ }.merge(attrs)
31
+ end
32
+
33
+ class TestPurchase < Test::Unit::TestCase
34
+ def setup
35
+ MyMoip::Request.any_instance.stubs(:api_call)
36
+ MyMoip::PaymentRequest.any_instance.stubs(:api_call)
37
+ MyMoip::TransparentRequest.any_instance.stubs(:api_call)
38
+ end
39
+
40
+ def subject
41
+ MyMoip::Purchase.new(id: '42',
42
+ price: 400,
43
+ reason: 'Payment of my product',
44
+ credit_card: cc_attrs,
45
+ payer: payer_attrs)
46
+ end
47
+
48
+ def test_new_objects_stores_already_initialized_instance_of_credit_ard
49
+ assert subject.credit_card.kind_of?(MyMoip::CreditCard)
50
+ end
51
+
52
+ def test_new_objects_stores_already_initialized_instance_of_payer
53
+ assert subject.payer.kind_of?(MyMoip::Payer)
54
+ end
55
+
56
+ def test_checkout_uses_initialized_id_for_logging
57
+ MyMoip::PaymentRequest.expects(:new).
58
+ with('42').
59
+ returns(stub_everything)
60
+ subject.checkout!
61
+ end
62
+
63
+ def test_checkout_instruction_uses_a_generated_id_for_instruction
64
+ MyMoip::Instruction.expects(:new).
65
+ with(has_entry(id: subject.id)).
66
+ returns(stub_everything)
67
+ subject.checkout!
68
+ end
69
+
70
+ def test_checkout_instruction_uses_initialized_reason
71
+ MyMoip::Instruction.expects(:new).
72
+ with(has_entry(payment_reason: 'Payment of my product')).
73
+ returns(stub_everything)
74
+ subject.checkout!
75
+ end
76
+
77
+ def test_checkout_instruction_uses_initialized_price
78
+ MyMoip::Instruction.expects(:new).
79
+ with(has_entry(values: [400.0])).
80
+ returns(stub_everything)
81
+ subject.checkout!
82
+ end
83
+
84
+ def test_checkouts_transparent_request_uses_initialized_id_for_payment_request_logging
85
+ MyMoip::TransparentRequest.expects(:new).
86
+ with('42').
87
+ returns(stub_everything)
88
+ subject.checkout!
89
+ end
90
+
91
+ def test_checkouts_transparent_request_confirm_request_with_authorized_token
92
+ MyMoip::Purchase.any_instance.stubs(:get_authorization!).
93
+ returns(stub('MyMoip request', token: 'abc'))
94
+
95
+ MyMoip::PaymentRequest.any_instance.expects(:api_call).
96
+ with(anything, has_entry(token: 'abc')).
97
+ returns(stub_everything)
98
+ subject.checkout!
99
+ end
100
+
101
+ def test_checkouts_transparent_request_assigns_its_code
102
+ MyMoip::PaymentRequest.any_instance.stubs(:success?).returns(true)
103
+ MyMoip::PaymentRequest.any_instance.stubs(:code).returns('foo_bar')
104
+ purchase = subject
105
+ purchase.checkout!
106
+
107
+ assert_equal 'foo_bar', purchase.code
108
+ end
109
+ end
@@ -0,0 +1,88 @@
1
+ require_relative '../test_helper'
2
+
3
+ class TestRequest < Test::Unit::TestCase
4
+ def setup
5
+ @default_environment = MyMoip.environment
6
+ @default_key = MyMoip.key
7
+ @default_token = MyMoip.token
8
+ @default_logger = MyMoip.logger
9
+ end
10
+
11
+ def teardown
12
+ MyMoip.environment = @default_environment
13
+ MyMoip.key = @default_key
14
+ MyMoip.token = @default_token
15
+ MyMoip.logger = @default_logger
16
+ end
17
+
18
+ def test_initializes_receiving_data_and_optional_id
19
+ request = MyMoip::Request.new("request_id")
20
+ assert_equal "request_id", request.id
21
+ end
22
+
23
+ def test_logs_api_call_method_in_info_level
24
+ logger = stub_everything
25
+ request = MyMoip::Request.new("request_id")
26
+ params = {
27
+ http_method: :post, body: "<pretty><xml></xml></pretty>", path: "/ws/alpha/EnviarInstrucao/Unica"
28
+ }
29
+
30
+ HTTParty.stubs(:send).returns("<html>some_result</html>")
31
+ logger.expects(:info).at_least_once.
32
+ with(regexp_matches(/being sent to MoIP/))
33
+
34
+ request.api_call(params, logger: logger)
35
+ end
36
+
37
+ def test_logs_api_call_method_parameters_in_debug_level
38
+ logger = stub_everything
39
+ request = MyMoip::Request.new("request_id")
40
+ params = {
41
+ http_method: :post, body: "<pretty><xml></xml></pretty>", path: "/ws/alpha/EnviarInstrucao/Unica"
42
+ }
43
+
44
+ HTTParty.stubs(:send).returns("<html>some_result</html>")
45
+ logger.expects(:debug).at_least_once.
46
+ with(regexp_matches(/request_id.+<pretty><xml><\/xml><\/pretty>/))
47
+
48
+ request.api_call(params, logger: logger)
49
+ end
50
+
51
+ def test_logs_api_call_response_in_debug_level
52
+ logger = stub_everything
53
+ request = MyMoip::Request.new("request_id")
54
+ params = {
55
+ http_method: :post, body: "<pretty><xml></xml></pretty>", path: "/ws/alpha/EnviarInstrucao/Unica"
56
+ }
57
+
58
+ HTTParty.stubs(:send).returns("<html>some_result</html>")
59
+ logger.expects(:debug).at_least_once.
60
+ with(regexp_matches(/request_id.+<html>some_result<\/html>/))
61
+
62
+ request.api_call(params, logger: logger)
63
+ end
64
+
65
+ def test_raises_error_before_api_calls_without_a_key_set
66
+ subject = MyMoip::Request.new('request_id')
67
+ MyMoip.sandbox_key = nil
68
+ assert_raises StandardError do
69
+ subject.api_call({})
70
+ end
71
+ MyMoip.sandbox_key = ''
72
+ assert_raises StandardError do
73
+ subject.api_call({})
74
+ end
75
+ end
76
+
77
+ def test_raises_error_before_api_calls_without_a_token_set
78
+ subject = MyMoip::Request.new('request_id')
79
+ MyMoip.sandbox_token = nil
80
+ assert_raises StandardError do
81
+ subject.api_call({})
82
+ end
83
+ MyMoip.sandbox_token = ''
84
+ assert_raises StandardError do
85
+ subject.api_call({})
86
+ end
87
+ end
88
+ end