Skip to content

Commit

Permalink
Fix a few style errors.
Browse files Browse the repository at this point in the history
Add missing remote tests.
Stop symbolizing names in JSON parse.
  • Loading branch information
boucher committed Jun 16, 2011
1 parent ca39e94 commit 946c19e
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 19 deletions.
1 change: 1 addition & 0 deletions activemerchant.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down
33 changes: 14 additions & 19 deletions lib/active_merchant/billing/gateways/stripe.rb
Original file line number Diff line number Diff line change
@@ -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 = {
Expand Down Expand Up @@ -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]
Expand All @@ -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

Expand Down Expand Up @@ -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)
Expand All @@ -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,
Expand All @@ -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
Expand All @@ -158,22 +153,22 @@ 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)
rescue JSON::ParserError
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
)
Expand Down
22 changes: 22 additions & 0 deletions test/remote/gateways/remote_stripe_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 946c19e

Please sign in to comment.