Permalink
Browse files

Merge pull request #144 from 'stripe/master'

  • Loading branch information...
2 parents b241b7a + 9c640ab commit 544a7ad6c5503a65ead17f6de1059d4f2d787d82 @jduff jduff committed Jun 27, 2011
View
23 lib/active_merchant/billing/gateways/stripe.rb
@@ -42,16 +42,33 @@ def purchase(money, creditcard, options = {})
add_creditcard(post, creditcard, options)
add_customer(post, options)
add_customer_data(post, options)
+ add_flags(post, options)
raise ArgumentError.new("Customer or Credit Card required.") if !post[:card] && !post[:customer]
commit('charges', post)
end
+ def authorize(money, creditcard, options = {})
+ purchase(money, creditcard, options.merge(:uncaptured => true))
+ end
+
+ def capture(money, identification, options = {})
+ commit("charges/#{CGI.escape(identification)}/capture", {})
+ end
+
def void(identification, options={})
commit("charges/#{CGI.escape(identification)}/refund", {})
end
+ def refund(money, identification, options = {})
+ post = {}
+
+ post[:amount] = amount(money) if money
+
+ commit("charges/#{CGI.escape(identification)}/refund", post)
+ end
+
def store(creditcard, options={})
post = {}
add_creditcard(post, creditcard, options)
@@ -106,6 +123,10 @@ def add_customer(post, options)
post[:customer] = options[:customer] if options[:customer] && !post[:card]
end
+ def add_flags(post, options)
+ post[:uncaptured] = true if options[:uncaptured]
+ end
+
def parse(body)
JSON.parse(body)
end
@@ -136,7 +157,7 @@ def headers
})
{
- "Authorization" => "Basic " + ActiveSupport::Base64.encode64(@api_key+":").strip,
+ "Authorization" => "Basic " + ActiveSupport::Base64.encode64(@api_key.to_s + ":").strip,
"User-Agent" => "Stripe/v1 ActiveMerchantBindings/#{ActiveMerchant::VERSION}",
"X-Stripe-Client-User-Agent" => @@ua
}
View
46 test/remote/gateways/remote_stripe_test.rb
@@ -30,6 +30,36 @@ def test_unsuccessful_purchase
assert_equal 'Your card number is invalid', response.message
end
+ def test_successful_authorize
+ assert response = @gateway.authorize(@amount, @credit_card, @options)
+ assert_success response
+ assert_equal "charge", response.params["object"]
+ assert response.params["paid"]
+ assert response.params["uncaptured"]
+ end
+
+ def test_unsuccessful_authorize
+ assert response = @gateway.authorize(@amount, @declined_card, @options)
+ assert_failure response
+ assert_equal 'Your card number is invalid', response.message
+ end
+
+ def test_successful_capture
+ assert response = @gateway.authorize(@amount, @credit_card, @options)
+ assert_success response
+ assert response.authorization
+
+ assert captured = @gateway.capture(nil, response.authorization)
+ assert_success captured
+ assert !captured.params["uncaptured"]
+ end
+
+ def test_unsuccessful_capture
+ assert captured = @gateway.capture(nil, "active_merchant_fake_charge")
+ assert_failure captured
+ assert_equal "Invalid charge id: active_merchant_fake_charge", captured.message
+ end
+
def test_successful_void
assert response = @gateway.purchase(@amount, @credit_card, @options)
assert_success response
@@ -38,12 +68,26 @@ def test_successful_void
assert_success void
end
- def test_unsucessful_void
+ def test_unsuccessful_void
assert void = @gateway.void("active_merchant_fake_charge")
assert_failure void
assert_equal "Invalid charge id: active_merchant_fake_charge", void.message
end
+ def test_successful_refund
+ assert response = @gateway.purchase(@amount, @credit_card, @options)
+ assert_success response
+ assert response.authorization
+ assert void = @gateway.refund(@amount - 20, response.authorization)
+ assert_success void
+ end
+
+ def test_unsuccessful_refund
+ assert refund = @gateway.refund(@amount, "active_merchant_fake_charge")
+ assert_failure refund
+ assert_equal "Invalid charge id: active_merchant_fake_charge", refund.message
+ end
+
def test_successful_store
assert response = @gateway.store(@credit_card, {:description => "Active Merchant Test Customer"})
assert_success response
View
143 test/unit/gateways/stripe_test.rb
@@ -8,7 +8,8 @@ def setup
@gateway = StripeGateway.new(:login => 'login')
@credit_card = credit_card()
- @amount = 100
+ @amount = 400
+ @refund_amount = 200
@options = {
:billing_address => address(),
@@ -28,6 +29,32 @@ def test_successful_purchase
assert response.test?
end
+ def test_successful_authorize
+ @gateway.expects(:ssl_request).returns(successful_authorize_response)
+
+ assert response = @gateway.authorize(@amount, @credit_card, @options)
+ assert_instance_of Response, response
+ assert_success response
+ assert response
+
+ # Replace with authorization number from the successful response
+ assert_equal 'ch_test_charge', response.authorization
+ assert response.test?
+ end
+
+ def test_successful_capture
+ @gateway.expects(:ssl_request).returns(successful_purchase_response)
+
+ assert response = @gateway.capture(nil, 'ch_test_charge')
+ assert_instance_of Response, response
+ assert_success response
+ assert response
+
+ # Replace with authorization number from the successful response
+ assert_equal 'ch_test_charge', response.authorization
+ assert response.test?
+ end
+
def test_successful_void
@gateway.expects(:ssl_request).returns(successful_purchase_response(true))
@@ -40,6 +67,18 @@ def test_successful_void
assert response.test?
end
+ def test_successful_refund
+ @gateway.expects(:ssl_request).returns(successful_partially_refunded_response)
+
+ assert response = @gateway.refund(@refund_amount, 'ch_test_charge')
+ assert_instance_of Response, response
+ assert_success response
+
+ # Replace with authorization number from the successful response
+ assert_equal 'ch_test_charge', response.authorization
+ assert response.test?
+ end
+
def test_unsuccessful_request
@gateway.expects(:ssl_request).returns(failed_purchase_response)
@@ -56,14 +95,6 @@ def test_invalid_raw_response
assert_match /^Invalid response received from the Stripe API/, response.message
end
- def test_ensure_does_not_respond_to_authorize
- assert !@gateway.respond_to?(:authorize)
- end
-
- def test_ensure_does_not_respond_to_capture
- assert !@gateway.respond_to?(:capture) || @gateway.method(:capture).owner != @gateway.class
- end
-
def test_add_customer
post = {}
@gateway.send(:add_customer, post, {:customer => "test_customer"})
@@ -92,6 +123,10 @@ def test_add_address
assert_equal @options[:billing_address][:country], post[:card][:address_country]
end
+ def test_ensure_does_not_respond_to_credit
+ assert !@gateway.respond_to?(:credit)
+ end
+
def test_gateway_without_credentials
assert_raises ArgumentError do
StripeGateway.new
@@ -109,27 +144,75 @@ def test_purchase_without_card_or_customer
# Place raw successful response from gateway here
def successful_purchase_response(refunded=false)
<<-RESPONSE
- {
- "attempted": true,
- "refunded": #{refunded},
- "paid": true,
- "amount": 400,
- "card": {
- "type": "Visa",
- "country": "US",
- "exp_month": 9,
- "last4": "4242",
- "exp_year": #{Time.now.year + 1},
- "object": "card",
- "id": "cc_test_card"
- },
- "id": "ch_test_charge",
- "livemode": false,
- "description": "Test Purchase",
- "currency": "usd",
- "object": "charge",
- "created": 1307309607
- }
+{
+ "amount": 400,
+ "created": 1309131571,
+ "currency": "usd",
+ "description": "Test Purchase",
+ "id": "ch_test_charge",
+ "livemode": false,
+ "object": "charge",
+ "paid": true,
+ "refunded": #{refunded},
+ "card": {
+ "country": "US",
+ "exp_month": 9,
+ "exp_year": #{Time.now.year + 1},
+ "last4": "4242",
+ "object": "card",
+ "type": "Visa"
+ }
+}
+ RESPONSE
+ end
+
+ def successful_partially_refunded_response
+ <<-RESPONSE
+{
+ "amount": 400,
+ "amount_refunded": 200,
+ "created": 1309131571,
+ "currency": "usd",
+ "description": "Test Purchase",
+ "id": "ch_test_charge",
+ "livemode": false,
+ "object": "charge",
+ "paid": true,
+ "refunded": true,
+ "card": {
+ "country": "US",
+ "exp_month": 9,
+ "exp_year": #{Time.now.year + 1},
+ "last4": "4242",
+ "object": "card",
+ "type": "Visa"
+ }
+}
+ RESPONSE
+ end
+
+ def successful_authorize_response
+ <<-RESPONSE
+{
+ "amount": 400,
+ "created": 1309131571,
+ "currency": "usd",
+ "description": "Test Purchase",
+ "id": "ch_test_charge",
+ "livemode": false,
+ "object": "charge",
+ "paid": true,
+ "refunded": true,
+ "uncaptured": true,
+ "card": {
+ "country": "US",
+ "exp_month": 9,
+ "exp_year": #{Time.now.year + 1},
+ "last4": "4242",
+ "object": "card",
+ "type": "Visa"
+ }
+}
RESPONSE
end

0 comments on commit 544a7ad

Please sign in to comment.