Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,16 @@ A payment slip can have the following attributes:
* instruction_line_1, instruction_line_2, instruction_line_3: lines of instruction (up to 63 characters each), added to the payment slip.
* logo_url: an URL pointing to an image which will be added to the body of the payment slip.

### Consultation

``` ruby
# Create a consultation request with the moip transaction token
request = MyMoip::ConsultationRequest.new('U260F1E2P0G4N0Z2T1S0M4T3C5E4J5M8L5U0G0I0U0M0H0Y0E3X7S9X6I783')
request.api_call
# After api_call you can access the xml string response and parse it
Hash.from_xml(request.xml_str)
```

### Logger

The methods that make api calls to Moip, log request and response informations. The default logger is `Logger.new(STDOUT)`, but you can set the logger you want as `api_call` option. For instance:
Expand Down
23 changes: 23 additions & 0 deletions lib/mymoip/requests/consultation_request.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module MyMoip
class ConsultationRequest < Request

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra empty line detected at body beginning.

HTTP_METHOD = :get
PATH = '/ws/alpha/ConsultarInstrucao'
REQUIRES_AUTH = true

def api_call(opts = {})
params = {
http_method: HTTP_METHOD,
requires_auth: REQUIRES_AUTH,
path: [PATH, id].join('/')
}

super(params, opts)
end

def xml_str
@response.body
end

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra empty line detected at body end.

end
end
24 changes: 23 additions & 1 deletion lib/mymoip/requests/payment_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@ class PaymentRequest < Request
REQUIRES_AUTH = false
FORMAT = :json
PAYMENT_SLIP_PATH = "/Instrucao.do?token="
STATUSES = {
"Autorizado" => 1,
"Iniciado" => 2,
"BoletoImpresso" => 3,
"Concluido" => 4,
"Cancelado" => 5,
"EmAnalise" => 6,
"Estornado" => 7,
"Reembolsado" => 9
}

attr_reader :token

Expand Down Expand Up @@ -45,7 +55,19 @@ def url

def code
@response["CodigoMoIP"]
rescue NoMethodError => e
rescue NoMethodError
nil
end

def status
STATUSES[@response["Status"]]
rescue NoMethodError
nil
end

def total_payed
@response["TotalPago"]
rescue NoMethodError
nil
end

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra empty line detected at body end.

Expand Down
99 changes: 99 additions & 0 deletions test/fixtures/vcr_cassettes/consultation_request.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions test/lib/test_consultation_request.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
require_relative '../test_helper'

class TestConsultationRequest < Test::Unit::TestCase
def test_http_method_as_post
assert_equal :get, MyMoip::ConsultationRequest::HTTP_METHOD
end

def test_path
assert_equal '/ws/alpha/ConsultarInstrucao', MyMoip::ConsultationRequest::PATH

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long. [82/80]

end

def test_auth_requirement
assert_equal true, MyMoip::ConsultationRequest::REQUIRES_AUTH
end

def test_should_provide_the_xml_str_get_by_the_request
request = MyMoip::ConsultationRequest.new('U260F1E2P0G4N0Z2T1S0M4T3C5E4J5M8L5U0G0I0U0M0H0Y0E3X7S9X6I783')

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long. [109/80]


VCR.use_cassette('consultation_request') { request.api_call }

assert_block { request.xml_str.match(/^<ns1:ConsultarTokenResponse/) }
end
end
63 changes: 36 additions & 27 deletions test/lib/test_payment_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,51 +70,60 @@ def test_success_method_returns_false_in_payments_already_made
end

def test_method_to_get_moip_code
instruction = Fixture.instruction(payer: Fixture.payer)
transparent_request = MyMoip::TransparentRequest.new("your_own_id")
VCR.use_cassette('transparent_request') do
transparent_request.api_call(instruction)
end
credit_card_payment = MyMoip::CreditCardPayment.new(Fixture.credit_card, installments: 1)
payment_request = MyMoip::PaymentRequest.new("your_own_id")
VCR.use_cassette('payment_request') do
payment_request.api_call(credit_card_payment, token: transparent_request.token)
end
assert_equal 102596, payment_request.code
make_a_successfully_payment
assert_equal 102596, @payment_request.code
end

def test_method_to_get_status
make_a_successfully_payment
assert_equal 6, @payment_request.status
end

def test_method_to_get_total_payed
make_a_successfully_payment
assert_equal '200.00', @payment_request.total_payed
end

def test_code_method_should_return_nil_with_blank_response
instruction = Fixture.instruction(payer: Fixture.payer)
transparent_request = MyMoip::TransparentRequest.new("your_own_id")
VCR.use_cassette('transparent_request') do
transparent_request.api_call(instruction)
end
credit_card_payment = MyMoip::CreditCardPayment.new(Fixture.credit_card, installments: 1)
payment_request = MyMoip::PaymentRequest.new("your_own_id")
assert_nil payment_request.code
end

def test_status_method_should_return_nil_with_blank_response
payment_request = MyMoip::PaymentRequest.new("your_own_id")
assert_nil payment_request.status
end

def test_total_payed_method_should_return_nil_with_blank_response
payment_request = MyMoip::PaymentRequest.new("your_own_id")
assert_nil payment_request.total_payed
end

def test_method_to_get_response_url
make_a_successfully_payment
assert_equal "https://desenvolvedor.moip.com.br/sandbox/Instrucao.do?token=#{@transparent_request.token}", @payment_request.url
end

def test_url_method_should_return_nil_with_blank_response
instruction = Fixture.instruction(payer: Fixture.payer)
transparent_request = MyMoip::TransparentRequest.new("your_own_id")
VCR.use_cassette('transparent_request') do
transparent_request.api_call(instruction)
end
payment_slip_payment = MyMoip::PaymentSlipPayment.new
payment_request = MyMoip::PaymentRequest.new("your_own_id")
VCR.use_cassette('payment_request_with_payment_slip') do
payment_request.api_call(payment_slip_payment, token: transparent_request.token)
end
assert_equal "https://desenvolvedor.moip.com.br/sandbox/Instrucao.do?token=#{transparent_request.token}", payment_request.url
assert_nil payment_request.url
end

def test_url_method_should_return_nil_with_blank_response
def make_a_successfully_payment
instruction = Fixture.instruction(payer: Fixture.payer)
transparent_request = MyMoip::TransparentRequest.new("your_own_id")
@transparent_request = MyMoip::TransparentRequest.new("your_own_id")
VCR.use_cassette('transparent_request') do
transparent_request.api_call(instruction)
@transparent_request.api_call(instruction)
end
credit_card_payment = MyMoip::CreditCardPayment.new(Fixture.credit_card, installments: 1)
@payment_request = MyMoip::PaymentRequest.new("your_own_id")
VCR.use_cassette('payment_request') do
@payment_request.api_call(credit_card_payment, token: @transparent_request.token)
end
payment_request = MyMoip::PaymentRequest.new("your_own_id")
assert_nil payment_request.url
end
end