Skip to content

Commit

Permalink
paying an invoice
Browse files Browse the repository at this point in the history
  • Loading branch information
Celestino Gomes committed Sep 30, 2013
1 parent 09bb15a commit 043456a
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 6 deletions.
27 changes: 24 additions & 3 deletions lib/charging/invoice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def create!

raise Http::LastResponseError.new(last_response) if last_response.code != 201

reload_attributes_after_create!
reload_attributes!(last_response.headers[:location])
rescue ::RestClient::Exception => exception
@last_response = exception.response

Expand All @@ -43,6 +43,27 @@ def create!
@errors = [$ERROR_INFO.message] if $ERROR_INFO
end

# Pays current invoice at API. You can pass <tt>paid_amount</tt>,
# <tt>payment_date</tt> and <tt>note</tt> about payment.
# Default values:
# - <tt>amount</tt>: amount
# - <tt>date</tt>: Time.now.strftime('%Y-%m-%d')
def pay!(paid_amount = amount, payment_date = Time.now, note = false)
attributes = {
amount: paid_amount,
date: payment_date.strftime('%Y-%m-%d')
}
attributes[:note] = note if note

response = Http.post("/invoices/#{uuid}/pay/", domain.token, MultiJson.encode(attributes), etag: self.etag)

raise Http::LastResponseError.new(response) if response.code != 201

reload_attributes!(self.uri)
rescue ::RestClient::Exception => excetion
raise Http::LastResponseError.new(excetion.response)
end

# Finds an invoice by uuid. It requites an <tt>domain</tt> and a
# <tt>uuid</tt>.
#
Expand Down Expand Up @@ -98,8 +119,8 @@ def self.load_persisted_invoice(attributes, response, domain, charge_account = n

private

def reload_attributes_after_create!
response = Http.get(last_response.headers[:location], domain.token)
def reload_attributes!(uri)
response = Http.get(uri, domain.token)

new_invoice = Invoice.load_persisted_invoice(MultiJson.decode(response.body), response, domain, charge_account)

Expand Down
18 changes: 17 additions & 1 deletion spec/charging/invoice_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@

its(:uri) { should eq "http://sandbox.charging.financeconnect.com.br/invoices/#{uuid}/" }
its(:uuid) { should eq uuid }
its(:etag) { should eq '"75de28a49a515edef88b3285921808a3d86ea5bf"' }
its(:etag) { should eq '"32a54d2e9e108dc5bb172eec680dfd7fdb0f57a8"' }
its(:domain) { should eq domain }
end
end
Expand Down Expand Up @@ -203,4 +203,20 @@
end
end
end

describe '#pay!' do
let!(:invoice) {
VCR.use_cassette('finding an invoice by uuid') do
described_class.find_by_uuid(domain, uuid)
end
}

it 'should update paid value' do
VCR.use_cassette('paying_an_invoice') do
invoice.pay!
end

expect(invoice.paid).to eq(invoice.amount)
end
end
end
2 changes: 1 addition & 1 deletion spec/fixtures/vcr_cassettes/creating_an_invoice.yml

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

2 changes: 1 addition & 1 deletion spec/fixtures/vcr_cassettes/finding_an_invoice_by_uuid.yml

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

109 changes: 109 additions & 0 deletions spec/fixtures/vcr_cassettes/paying_an_invoice.yml

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

0 comments on commit 043456a

Please sign in to comment.