diff --git a/activemerchant.gemspec b/activemerchant.gemspec index d0722f68a4a..62b302ec139 100644 --- a/activemerchant.gemspec +++ b/activemerchant.gemspec @@ -18,6 +18,7 @@ Gem::Specification.new do |s| s.add_dependency('activesupport', '>= 2.3.11') s.add_dependency('builder', '>= 2.0.0') s.add_dependency('braintree', '>= 2.0.0') + s.add_dependency('json', '>= 1.5.1') if RUBY_VERSION =~ /^1\.8\./ s.signing_key = ENV['GEM_PRIVATE_KEY'] s.cert_chain = ['gem-public_cert.pem'] diff --git a/lib/active_merchant/billing/gateways/stripe.rb b/lib/active_merchant/billing/gateways/stripe.rb index e4e37e07d77..3c5f353c4ca 100644 --- a/lib/active_merchant/billing/gateways/stripe.rb +++ b/lib/active_merchant/billing/gateways/stripe.rb @@ -1,11 +1,8 @@ -if RUBY_VERSION =~ /^1\.8\./ - require 'json' -end +require 'json' module ActiveMerchant #:nodoc: module Billing #:nodoc: class StripeGateway < Gateway - VERSION = '1.0.0' LIVE_URL = 'https://api.stripe.com/v1/' AVS_CODE_TRANSLATOR = { @@ -33,8 +30,6 @@ class StripeGateway < Gateway self.homepage_url = 'https://stripe.com/' self.display_name = 'Stripe' - @@ua = nil - def initialize(options = {}) requires!(options, :login) @api_key = options[:login] @@ -49,9 +44,9 @@ def purchase(money, creditcard, options = {}) add_customer(post, options) add_customer_data(post, options) - if (!post[:card] && !post[:customer]) + if !post[:card] && !post[:customer] raise ArgumentError.new("Customer or Credit Card required.") - elsif (post[:card] && post[:customer]) + elsif post[:card] && post[:customer] raise ArgumentError.new("Can't provide both Customer and Credit Card.") end @@ -117,7 +112,7 @@ def add_customer(post, options) end def parse(body) - JSON.parse(body, :symbolize_names => true) + JSON.parse(body) end def post_data(params) @@ -137,7 +132,7 @@ def post_data(params) def headers @@ua ||= JSON.dump({ - :bindings_version => VERSION, + :bindings_version => ActiveMerchant::VERSION, :lang => 'ruby', :lang_version => "#{RUBY_VERSION} p#{RUBY_PATCHLEVEL} (#{RUBY_RELEASE_DATE})", :platform => RUBY_PLATFORM, @@ -146,8 +141,8 @@ def headers }) { - "Authorization" => "Basic " + ActiveSupport::Base64.encode64(@api_key+":").gsub(/\n/, ''), - "User-Agent" => "Stripe/v1 ActiveMerchantBindings/#{VERSION}", + "Authorization" => "Basic " + ActiveSupport::Base64.encode64(@api_key+":").strip(), + "User-Agent" => "Stripe/v1 ActiveMerchantBindings/#{ActiveMerchant::VERSION}", "X-Stripe-Client-User-Agent" => @@ua } end @@ -158,7 +153,7 @@ def commit(url, method, parameters) begin raw_response = ssl_request(method, LIVE_URL + url, post_data(parameters), headers) response = parse(raw_response) - success = !response.key?(:error) + success = !response.key?("error") rescue ResponseError => e raw_response = e.response.body response = response_error(raw_response) @@ -166,14 +161,14 @@ def commit(url, method, parameters) response = json_error(raw_response) end - card = response[:card] || {} - avs_code = AVS_CODE_TRANSLATOR["line1: #{card[:address_line1_check]}, zip: #{card[:address_zip_check]}"] - cvc_code = CVC_CODE_TRANSLATOR[card[:cvc_check]] + card = response["card"] || {} + avs_code = AVS_CODE_TRANSLATOR["line1: #{card["address_line1_check"]}, zip: #{card["address_zip_check"]}"] + cvc_code = CVC_CODE_TRANSLATOR[card["cvc_check"]] Response.new(success, - success ? "Transaction approved" : response[:error][:message], + success ? "Transaction approved" : response["error"]["message"], response, - :test => !response[:livemode], - :authorization => response[:id], + :test => !response["livemode"], + :authorization => response["id"], :avs_result => { :code => avs_code }, :cvv_result => cvc_code ) diff --git a/test/remote/gateways/remote_stripe_test.rb b/test/remote/gateways/remote_stripe_test.rb index b248b201deb..e1bf27458f1 100644 --- a/test/remote/gateways/remote_stripe_test.rb +++ b/test/remote/gateways/remote_stripe_test.rb @@ -30,6 +30,28 @@ def test_unsuccessful_purchase assert_equal 'Your card number is invalid', response.message end + def test_successful_void + assert response = @gateway.purchase(@amount, @credit_card, @options) + assert_success response + assert response.authorization + assert void = @gateway.void(response.authorization) + assert_success void + end + + def test_unsucessful_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_store + assert response = @gateway.store(@credit_card, {:description => "Active Merchant Test Customer"}) + assert_success response + assert_equal "customer", response.params["object"] + assert_equal "Active Merchant Test Customer", response.params["description"] + assert_equal @credit_card.last_digits, response.params["active_card"]["last4"] + end + def test_invalid_login gateway = StripeGateway.new(:login => 'active_merchant_test') assert response = gateway.purchase(@amount, @credit_card, @options)