Skip to content

Commit

Permalink
Add boolean status accessors. Explain how the status changes work.
Browse files Browse the repository at this point in the history
  • Loading branch information
Manfred committed Feb 11, 2012
1 parent 625c749 commit 5560fa4
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 19 deletions.
33 changes: 28 additions & 5 deletions lib/supreme/response.rb
Expand Up @@ -72,16 +72,39 @@ def paid
text('//payed')
end

# A payment will return paid? for just one request. If you issue it too early you might never
# get a truthy value from this.
def paid?
paid == 'true'
end

# Returns the status of the payment. This is probably the best way to check if the payment has
# succeeded. It returns one of the following values:
#
# * <tt>Open</tt> – Payment is still processing.
# * <tt>Success</tt> – The payment was successful.
# * <tt>Cancelled</tt> – The payment was explicitly cancelled by the customer.
# * <tt>Failure</tt> – The payment failed.
# * <tt>Expired</tt> – The customer abandoned the payment, we don't expect them to finish it.
# * <tt>CheckedBefore</tt> – You've requested the payment status before.
#
# You can also check the status of the payment with one of the boolean accessors: open?, success?,
# cancelled?, failed?, expired?, and checked_before?.
def status
test('//status')
end

def message
test('//status')
text('//status')
end

[
['Open', :open?],
['Success', :success?],
['Cancelled', :cancelled?],
['Failure', :failed?],
['Expired', :expired?],
['CheckedBefore', :checked_before?]
].each do |expected, accessor|
define_method accessor do
status == expected
end
end

def customer
Expand Down
24 changes: 11 additions & 13 deletions remote/basic_test.rb
Expand Up @@ -18,20 +18,18 @@
puts
puts "Open the following link and complete the transaction: "
puts transaction.url.gsub('&amp;', '&')
puts

sleep 5
puts "Waiting 20 seconds for you to complete the payment"
20.times { $stdout.write('.'); sleep 1 }
puts

transaction_id = transaction.transaction_id
20.times do
status = Supreme.api.check(:transaction_id => transaction_id)

assert(1399 == status.amount.to_i, "Expected the status amount to be the same as the one we set (was #{status.amount})")
if status.paid?
puts "The transation was completed successfully!"
exit 1
else
puts "[…] Not paid yet"
end

sleep 2
status = Supreme.api.check(:transaction_id => transaction_id)
assert(1399 == status.amount.to_i, "Expected the status amount to be the same as the one we set (was #{status.amount})")

if status.success?
puts "The transation was completed successfully!"
else
puts "The transation failed: #{status.status}"
end
3 changes: 2 additions & 1 deletion remote/test_helper.rb
Expand Up @@ -20,10 +20,11 @@
exit(-1)
end

$stdout.sync = true
module Kernel
def assert(expectation, message=nil)
if expectation
$stderr.write('.')
$stdout.write('.')
else
message ||= "assertion failed"
puts "FAILURE: #{caller(0)[1]}"
Expand Down
14 changes: 14 additions & 0 deletions test/response_test.rb
@@ -1 +1,15 @@
require File.expand_path('../test_helper', __FILE__)

class StatusTest < Test::Unit::TestCase
def test_boolean_status_accessors
status = Supreme::Status.new(stub)
status.stubs(:status).returns('Failure')

assert !status.open?
assert !status.success?
assert !status.cancelled?
assert status.failed?
assert !status.expired?
assert !status.checked_before?
end
end

0 comments on commit 5560fa4

Please sign in to comment.