Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bank debit payment and Payment slip payment #35

Merged
merged 9 commits into from
Oct 4, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
55 changes: 43 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ purchase.code # Moip code or nil, depending of the checkout's return

## The hard way

**First request: what and from who**
### First request: what and from who

```ruby
payer = MyMoip::Payer.new(
Expand Down Expand Up @@ -119,7 +119,9 @@ transparent_request = MyMoip::TransparentRequest.new('your_logging_id')
transparent_request.api_call(instruction)
```

**Second request: how**
### Second request: how

#### Credit card

```ruby
credit_card = MyMoip::CreditCard.new(
Expand All @@ -133,19 +135,48 @@ credit_card = MyMoip::CreditCard.new(
owner_cpf: '52211670695'
)

credit_card_payment = MyMoip::CreditCardPayment.new(credit_card,
installments: 1)
credit_card_payment = MyMoip::CreditCardPayment.new(credit_card, installments: 1)
payment_request = MyMoip::PaymentRequest.new('your_logging_id')
payment_request.api_call(credit_card_payment, token: transparent_request.token)
```

#### Payment slip (aka boleto)

```ruby
payment_slip_payment = MyMoip::PaymentSlipPayment.new()
payment_request = MyMoip::PaymentRequest.new('your_logging_id')
payment_request.api_call(credit_card_payment,
token: transparent_request.token)
payment_request.api_call(payment_slip_payment, token: transparent_request.token)
```

**Success?**
#### Bank debit

```ruby
bank_debit = MyMoip::BankDebit.new(bank: :itau)
# you can find the available banks on MyMoip::BankDebit::AVAILABLE_BANKS

bank_debit_payment = MyMoip::BankDebitPayment.new(bank_debit)
payment_request = MyMoip::PaymentRequest.new('your_logging_id')
payment_request.api_call(bank_debit_payment, token: transparent_request.token)
```

### Success?

```ruby
payment_request.success?
```

### Payment url

For **payment slip** and **bank debit**, payment request will have a url. This
url redirect to:

- When **payment slip**: the payment slip to user print and pay
- When **bank debit**: the bank's specific payment page

```ruby
payment_request.url
```

## More!

Yes, you should read (and help improve!) the docs.
Expand Down Expand Up @@ -213,7 +244,7 @@ instruction = MyMoip::Instruction.new(
### Notification and return URLs

URLs configured at MoIP account can be overrided by passing new URLs values to the instruction object.
A notification URL is used for MoIP NASP notification system, responsible for transaction changes signals,
A notification URL is used for MoIP NASP notification system, responsible for transaction changes signals,
and a return URL is used to return to your website when a payment is using external websites.

```ruby
Expand All @@ -229,7 +260,7 @@ instruction = MyMoip::Instruction.new(

### Payment methods configuration

If you don't need all the payment methods available, you can choose some by configuring
If you don't need all the payment methods available, you can choose some by configuring
PaymentMethods and adding it to the instruction:

```ruby
Expand All @@ -238,7 +269,7 @@ payment_methods = MyMoip::PaymentMethods.new(
credit_card: true,
debit: true,
debit_card: true,
financing: true,
financing: true,
moip_wallet: true
)

Expand All @@ -251,7 +282,7 @@ instruction = MyMoip::Instruction.new(
)
```

### Payment slip (aka boleto) configuration
### Payment slip (aka boleto) configuration

You can optionally configure your payment slip creating a PaymentSlip and adding to the instruction:

Expand All @@ -265,7 +296,7 @@ payment_slip = MyMoip::PaymentSlip.new(
instruction_line_3: 'This is a test! :)',
logo_url: 'https://example.com/logo.png'
)

instruction = MyMoip::Instruction.new(
id: 'instruction_id_defined_by_you',
payment_reason: 'Order in Buy Everything Store',
Expand Down
22 changes: 22 additions & 0 deletions lib/mymoip/bank_debit.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module MyMoip
class BankDebit
include ActiveModel::Validations

attr_accessor :bank

AVAILABLE_BANKS = [:banco_do_brasil, :bradesco, :banrisul, :itau]

validates :bank, presence: true, inclusion: AVAILABLE_BANKS

def initialize(attrs)
attrs.each do |attr, value|
public_send(:"#{attr}=", value)
end
end

def bank=(value)
value = value.to_sym unless value.nil?
@bank = value
end
end
end
27 changes: 27 additions & 0 deletions lib/mymoip/bank_debit_payment.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module MyMoip
class BankDebitPayment
attr_accessor :bank_debit

def initialize(bank_debit)
@bank_debit = bank_debit
end

def to_json
raise InvalidBankDebit, "No bank debit information provided." if @bank_debit.nil?
raise InvalidBankDebit if @bank_debit.invalid?

json = {
Forma: "DebitoBancario",
}

json[:Instituicao] = {
banco_do_brasil: "BancoDoBrasil",
bradesco: "Bradesco",
banrisul: "Banrisul",
itau: "Itau"
}.fetch(@bank_debit.bank)

json
end
end
end
2 changes: 2 additions & 0 deletions lib/mymoip/exceptions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ class InvalidInstruction < Error; end
class InvalidPayer < Error; end

class InvalidPaymentSlip < Error; end

class InvalidBankDebit < Error; end
end
12 changes: 12 additions & 0 deletions lib/mymoip/payment_slip_payment.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module MyMoip
class PaymentSlipPayment

def to_json
json = {
Forma: 'BoletoBancario'
}

json
end
end
end
19 changes: 14 additions & 5 deletions lib/mymoip/requests/payment_request.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
module MyMoip
class PaymentRequest < Request

HTTP_METHOD = :get
PATH = "/rest/pagamento?callback=?"
REQUIRES_AUTH = false
FORMAT = :json
HTTP_METHOD = :get
PATH = "/rest/pagamento?callback=?"
REQUIRES_AUTH = false
FORMAT = :json
PAYMENT_SLIP_PATH = "/Instrucao.do?token="

attr_reader :token

def api_call(data, opts)
@token = opts[:token]

opts[:referer_url] ||= MyMoip.default_referer_url
opts[:parser] ||= MyMoip::JsonParser

json = JSON.generate({
pagamentoWidget: {
referer: opts[:referer_url],
token: opts[:token],
token: token,
dadosPagamento: data.to_json
}
})
Expand All @@ -34,6 +39,10 @@ def success?
@response && @response["StatusPagamento"] == "Sucesso"
end

def url
MyMoip.api_url + PAYMENT_SLIP_PATH + token if success?
end

def code
@response["CodigoMoIP"]
rescue NoMethodError => e
Expand Down
6 changes: 5 additions & 1 deletion test/fixtures/fixture.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,11 @@ def self.payment_slip(params = {})
instruction_line_3: 'Line 3',
logo_url: 'http://www.myurl.com/logo.png'
}.merge(params)

MyMoip::PaymentSlip.new(params)
end

def self.bank_debit(params = {})
params = { bank: :itau }.merge(params)
MyMoip::BankDebit.new(params)
end
end
32 changes: 32 additions & 0 deletions test/fixtures/vcr_cassettes/payment_request_with_payment_slip.yml

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

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

class TestBankDebit < Test::Unit::TestCase
def test_initialization_and_setters
subject = MyMoip::BankDebit.new(bank: :itau)
assert_equal :itau, subject.bank
end

def test_initialization_and_setters_with_string_keys
subject = MyMoip::BankDebit.new('bank' => :itau)
assert_equal :itau, subject.bank
end

def test_validate_presence_of_bank_attribute
subject = Fixture.bank_debit(bank: nil)
assert subject.invalid? && subject.errors[:bank].present?,
'should be invalid without a bank name'
end

def test_converts_bank_string_to_symbol
subject = Fixture.bank_debit(bank: "itau")
assert_equal :itau, subject.bank
end

def test_accepts_any_bank_from_available_banks_constant
MyMoip::BankDebit::AVAILABLE_BANKS.each do |bank|
subject = Fixture.bank_debit(bank: bank)
assert subject.valid?, 'should be valid'
end
end

def test_dont_accept_bank_out_of_available_banks_constant
subject = Fixture.bank_debit(bank: :unavailable_bank)
assert subject.invalid? && subject.errors[:bank].present?, 'should not be valid'
end
end
32 changes: 32 additions & 0 deletions test/lib/test_bank_debit_payment.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
require_relative '../test_helper'

class TestBankDebitPayment < Test::Unit::TestCase
def test_initialization_and_setters
bank_debit = Fixture.bank_debit
subject = MyMoip::BankDebitPayment.new(bank_debit)
assert_equal bank_debit, subject.bank_debit
end

def test_json_format
payment = MyMoip::BankDebitPayment.new(Fixture.bank_debit)
assert_equal "DebitoBancario", payment.to_json[:Forma]
end

def test_to_json_should_accept_any_bank_from_available_banks_constant
MyMoip::BankDebit::AVAILABLE_BANKS.each do |bank|
payment = MyMoip::BankDebitPayment.new(Fixture.bank_debit(bank: bank))
assert_nothing_raised(KeyError) { payment.to_json }
end
end

def test_to_json_method_raises_an_exception_when_called_without_a_bank_debit
subject = MyMoip::BankDebitPayment.new(nil)
assert_raise(MyMoip::InvalidBankDebit) { subject.to_json }
end

def test_to_json_method_raises_an_exception_when_called_with_a_invalid_bank_debit
subject = MyMoip::BankDebitPayment.new(Fixture.bank_debit)
MyMoip::BankDebit.any_instance.stubs(:invalid?).returns(true)
assert_raise(MyMoip::InvalidBankDebit) { subject.to_json }
end
end
24 changes: 24 additions & 0 deletions test/lib/test_payment_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,28 @@ def test_code_method_should_return_nil_with_blank_response
payment_request = MyMoip::PaymentRequest.new("your_own_id")
assert_nil payment_request.code
end

def test_method_to_get_response_url
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
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_request = MyMoip::PaymentRequest.new("your_own_id")
assert_nil payment_request.url
end
end
8 changes: 8 additions & 0 deletions test/lib/test_payment_slip_payment.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require_relative '../test_helper'

class TestPaymentSlipPayment < Test::Unit::TestCase
def test_json_format
payment = MyMoip::PaymentSlipPayment.new()
assert_equal 'BoletoBancario', payment.to_json[:Forma]
end
end