Skip to content

Commit

Permalink
shuffling the card addition spec some to really test against braintre…
Browse files Browse the repository at this point in the history
…e, gonna refactor the others after josh takes a look at the failing spec
  • Loading branch information
atmos committed Dec 31, 2008
1 parent 20025f1 commit 3ab1fdd
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 50 deletions.
7 changes: 6 additions & 1 deletion app/controllers/credit_cards.rb
Expand Up @@ -5,7 +5,7 @@ def index

def new
@credit_card = CreditCard.new
@gateway_request = Braintree::GatewayRequest.new
@gateway_request = Braintree::GatewayRequest.new(:orderid => Digest::SHA1.hexdigest(Time.now.to_s))
render
end

Expand All @@ -15,8 +15,13 @@ def new_response
case @gateway_response.response_status
when 'approved'
session.user.credit_cards.create(:token => @gateway_response.customer_vault_id)
query_params = {:transaction_id => params['transactionid'], :transaction_type => 'cc', :action_type => 'sale'}
transaction_info = BrainTree::Query.new(query_params).run
redirect('/', :message => {:notice => 'Successfully stored your card info securely.'})
else
query_params = {:transaction_id => params['transactionid'], :transaction_type => 'cc', :action_type => 'sale'}
transaction_info = BrainTree::Query.new(query_params).run
Merb.logger.info! transaction_info.inspect
redirect(url(:new_credit_card), :message => {:notice => @gateway_response.responsetext})
end
end
Expand Down
31 changes: 30 additions & 1 deletion app/models/braintree/gateway_request.rb
Expand Up @@ -8,7 +8,7 @@ class GatewayRequest
def initialize(attributes = nil)
attributes.each { |k,v| self.send("#{k}=", v) } unless attributes.nil?
self.key, self.key_id = BRAINTREE[:key], BRAINTREE[:key_id]
self.time = self.class.formatted_time_value
self.time = self.class.formatted_time_value
end

def hash
Expand All @@ -18,5 +18,34 @@ def hash
def self.formatted_time_value
Time.now.getutc.strftime("%Y%m%d%H%M%S")
end

def hash_attributes
{ 'orderid' => orderid, 'amount' => amount, 'key_id' => key_id,
'time' => time, 'response_url' => 'http://example.org/response',
'type' => '', 'customer_vault' => customer_vault, 'hash' => hash }
end

def post(params)
uri = Addressable::URI.parse(BRAINTREE[:transact_api_url])

server = Net::HTTP.new(uri.host, 443)
server.use_ssl = true
server.read_timeout = 20
server.verify_mode = OpenSSL::SSL::VERIFY_NONE

resp = server.start do |http|
req = Net::HTTP::Post.new(uri.path)
req.set_form_data(hash_attributes.merge(params))
http.request(req)
end
case resp
when Net::HTTPRedirection
Addressable::URI.parse(resp.header['Location'])
when Net::HTTPSuccess
resp
else
resp.error!
end
end
end
end
2 changes: 2 additions & 0 deletions app/views/credit_cards/_form.html.haml
Expand Up @@ -18,6 +18,8 @@
- if @credit_card.new_record?
= hidden_field :id => 'customer_vault', :name => 'customer_vault', :value => 'add_customer'
= hidden_field :id => 'redirect', :name => 'redirect', :value => absolute_url(:new_response_credit_cards)
= hidden_field :id => 'type', :name => 'type', :value => 'sale'
= hidden_field :id => 'amount', :name => 'amount', :value => '10.00'
- else
= hidden_field :id => 'customer_vault', :name => 'customer_vault', :value => 'update_customer'
= hidden_field :id => 'customer_vault_id', :name => 'customer_vault_id', :value => @credit_card.token
Expand Down
1 change: 1 addition & 0 deletions config/dependencies.rb
Expand Up @@ -33,3 +33,4 @@
dependency 'webrat', '=0.3.2', :require_as => nil
dependency 'mongrel', '>1.0', :require_as => nil
dependency 'libxml-ruby', '=0.9.7', :require_as => 'libxml'
dependency 'ruby-debug', '=0.10.3', :require_as => nil
87 changes: 40 additions & 47 deletions spec/requests/credit_cards/adding_a_card_spec.rb
@@ -1,8 +1,8 @@
require File.join(File.dirname(__FILE__), '..', '..', 'spec_helper.rb')

describe "CreditCards#new", :given => 'an authenticated user' do
describe "/credit_cards/new" do
it "should display a braintree transparent redirect form for customer vault creation" do
describe "adding a credit card", :given => 'an authenticated user' do
describe "the signup form" do
it "should be valid" do
response = request("/credit_cards/new")
response.should be_successful
response.should have_selector("form[action='https://secure.braintreepaymentgateway.com/api/transact.php'][method='post']")
Expand All @@ -13,57 +13,50 @@
response.should have_selector("form input#city[value='']")
response.should have_selector("form input#state[value='']")
response.should have_selector("form input#country[value='']")
response.should have_selector("form input#ccnumber[value='']")
response.should have_selector("form input#ccexp[value='']")

response.should have_selector("form input#type[value='sale'][type='hidden']")
response.should have_selector("form input#amount[value='10.00'][type='hidden']")
end
end
describe "/credit_cards/new_response" do
describe "given a successful response" do
it "store the response token in an associated object for the user" do
gw_response = Braintree::GatewayResponse.new(:orderid => '', :amount => '',
:response => '1', :transactionid => '0',
:avsresponse => '', :cvvresponse => '')
request_params = {"avsresponse" => "", "response"=> "1",
"authcode" => "", "orderid" => "",
"customer_vault_id"=>"1074650921", "responsetext"=>"Customer Added",
"hash"=> gw_response.generated_hash, "response_code"=>"100",
"username"=>"776320", "time"=>gw_response.time,
"amount"=>"", "transactionid"=>"0",
"type"=>"", "cvvresponse"=>""}
response = request("/credit_cards/new_response", :params => request_params)
response.should redirect_to('/')
describe "a successful transaction on signup" do
it "should be successful and display basic card info in the ui" do
api_response = Braintree::GatewayRequest.new(:amount => '10.00').post(quentin_form_info)
params = api_response.query_values
params.reject! { |k,v| v == true }

response = request("/credit_cards/new_response", :params => params)
response.should redirect_to('/')

response = request(response.headers['Location'])
response.should be_successful
response.should have_selector("div#main-container:contains('Successfully stored your card info securely.')")
response.should have_selector("div#main-container table tbody td")
end
response = request(response.headers['Location'])
response.should be_successful
response.should have_selector("div#main-container:contains('Successfully stored your card info securely.')")
response.should have_selector("div#main-container table tbody td")
end
end
describe "a declined transaction on signup" do
it "should be successful and display basic card info in the ui" do
api_response = Braintree::GatewayRequest.new(:amount => '0.99').post(quentin_form_info)
params = api_response.query_values
params.reject! { |k,v| v == true }

describe "given an invalid card number" do
it "should display the card input form again" do
gw_response = Braintree::GatewayResponse.new(:orderid => '', :amount => '',
:response => '3', :transactionid => '0',
:avsresponse => '', :cvvresponse => '')
request_params = {"avsresponse"=>"",
"response"=>"3",
"authcode"=>"", "orderid"=>"",
"responsetext"=>"Invalid card number REFID:999999999",
"hash"=> gw_response.generated_hash,
"response_code"=>"300",
"username"=>"776320",
"time"=> gw_response.time,
"amount"=>"",
"transactionid"=>"0",
"type"=>"",
"cvvresponse"=>""}
response = request("/credit_cards/new_response", :params => request_params)
response.should redirect_to('/credit_cards/new')
response = request("/credit_cards/new_response", :params => params)
response.should redirect_to("/credit_cards/new")

response = request(response.headers['Location'])
response.should be_successful
response.should have_selector("div#main-container:contains('Invalid card number REFID:999999999')")
response.should have_selector("form[action='https://secure.braintreepaymentgateway.com/api/transact.php'][method='post']")
end
response = request(response.headers['Location'])
response.should be_successful
response.should have_selector("div#main-container:contains('DECLINE')")
response.should have_selector("form[action='https://secure.braintreepaymentgateway.com/api/transact.php'][method='post']")
# response.should have_selector("form input#firstname[value='Quentin']")
# response.should have_selector("form input#lastname[value='Blake']")
# response.should have_selector("form input#email[value='quentin@example.org']")
# response.should have_selector("form input#address1[value='187 Drive By Blvd']")
# response.should have_selector("form input#city[value='Compton']")
# response.should have_selector("form input#state[value='CA']")
# response.should have_selector("form input#country[value='US']")
# response.should have_selector("form input#ccnumber[value='']")
# response.should have_selector("form input#ccexp[value='']")
end
end
end
@@ -1,4 +1,4 @@
require File.join(File.dirname(__FILE__), '..', '..', 'spec_helper.rb')
require File.join(File.dirname(__FILE__), '..', 'spec_helper.rb')

describe "CreditCards#index", :given => 'an authenticated user' do
describe "/" do
Expand Down
26 changes: 26 additions & 0 deletions spec/spec_helper.rb
Expand Up @@ -9,6 +9,7 @@
require "merb-core"
require "spec" # Satisfies Autotest and anyone else not using the Rake tasks
require 'pp'
require 'ruby-debug'

# this loads all plugins required in your init file so don't add them
# here again, Merb will do it for you
Expand All @@ -25,10 +26,35 @@
:password => 'lolerskates', :password_confirmation => 'lolerskates')
user.credit_cards.create(:token => '407702761')
end

def quentin_form_info
{ 'firstname' => 'quentin', 'lastname' => 'Blake',
'email' => 'quentin@example.org', 'address1' => '187 Drive By Blvd',
'city' => 'Compton', 'state' => 'CA', 'country' => 'US', 'ccv' => '999',
'ccexp' => '1010', 'ccnumber' => '4111111111111111',
'customer_vault' => 'add_customer', 'customer_vault_id' => '',
'payment' => 'creditcard', 'type' => 'sale',
'redirect' => 'http://example.org/credit_cards/new_response'
}
end
end

given "an authenticated user" do
response = request url(:perform_login), :method => "PUT",
:params => { :login => 'quentin', :password => 'lolerskates' }
response.should redirect_to '/'
end

given "a user with a credit card in the vault" do
response = request url(:perform_login), :method => "PUT",
:params => { :login => 'quentin', :password => 'lolerskates' }
response.should redirect_to '/'
response = request("/credit_cards/new")

api_response = Braintree::GatewayRequest.new(:amount => '10.00').post(quentin_form_info)
params = api_response.query_values
params.reject! { |k,v| v == true }

response = request("/credit_cards/new_response", :params => params)
response.should redirect_to('/')
end

0 comments on commit 3ab1fdd

Please sign in to comment.