Permalink
Browse files

Add boolean status accessors. Explain how the status changes work.

  • Loading branch information...
1 parent 625c749 commit 5560fa4c75b06926a41167df0297316ca117463d @Manfred Manfred committed Feb 11, 2012
Showing with 55 additions and 19 deletions.
  1. +28 −5 lib/supreme/response.rb
  2. +11 −13 remote/basic_test.rb
  3. +2 −1 remote/test_helper.rb
  4. +14 −0 test/response_test.rb
View
@@ -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
View
@@ -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
View
@@ -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]}"
View
@@ -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.