Skip to content
Browse files

omg it works again. braintree's examples for gateway request/response…

… don't actually work, checkout the conditional checks. need to talk to those guys about this stuff
  • Loading branch information...
1 parent 620d56a commit 1863fae5ffa0709626ebe366041c6240e736b902 @atmos committed Jan 4, 2009
View
11 app/models/braintree/gateway_request.rb
@@ -1,7 +1,7 @@
module Braintree
class GatewayRequest
attr_accessor :orderid, :amount, :key, :key_id, :time, :response_url,
- :type, :customer_vault
+ :type, :customer_vault, :customer_vault_id
attr_reader :hash
@@ -12,7 +12,12 @@ def initialize(attributes = nil)
end
def hash
- Digest::MD5.hexdigest([self.orderid, self.amount, self.time, self.key].join("|"))
+ items = if customer_vault_id.nil?
+ [orderid, amount, time, BRAINTREE[:key]]
+ else
+ [orderid, amount, customer_vault_id, time, BRAINTREE[:key]]
+ end
+ Digest::MD5.hexdigest(items.join('|'))
end
def self.formatted_time_value
@@ -21,7 +26,7 @@ def self.formatted_time_value
def hash_attributes
{ 'orderid' => orderid, 'amount' => amount, 'key_id' => key_id,
- 'time' => time, 'hash' => hash }
+ 'time' => time, 'hash' => hash, 'customer_vault_id' => customer_vault_id }
end
def post(params)
View
11 app/models/braintree/gateway_response.rb
@@ -29,15 +29,18 @@ def response_status
# The hash sent with the Gateway Response should equal a hash that can get
# generated using the key and the sent parameters.
def is_valid?
- return true if self.hash == self.generated_hash
+ hash == generated_hash
end
# Takes the values of the Gateway Response and generates a hash from them using
# MD5 and format listed in the documentation.
def generated_hash
- Digest::MD5.hexdigest([self.orderid, self.amount, self.response,
- self.transactionid, self.avsresponse,
- self.cvvresponse, self.time, BRAINTREE[:key]].join("|"))
+ items = if customer_vault_id.nil?
+ [orderid, amount, response, transactionid, avsresponse, cvvresponse, time, BRAINTREE[:key]]
+ else
+ [orderid, amount, response, transactionid, avsresponse, cvvresponse, customer_vault_id, time, BRAINTREE[:key]]
+ end
+ Digest::MD5.hexdigest(items.join('|'))
end
# AVS_RESPONSE_CODES
View
6 spec/requests/credit_cards/adding_a_card_spec.rb
@@ -29,7 +29,7 @@
describe "and having it succeed" do
it "should display basic info about the card stored in the vault" do
params = quentin_form_info.merge({'type'=>'sale','payment' => 'creditcard'})
- api_response = Braintree::Spec::ApiRequest.new('10.00', params)
+ api_response = Braintree::Spec::ApiRequest.new('10.00', nil, params)
response = request("/credit_cards/new_response", :params => api_response.params)
response.should redirect_to('/')
@@ -44,7 +44,7 @@
describe "and having it declined" do
it "should display the signup form again, pre-populated with the info from the failed transaction" do
params = quentin_form_info.merge({'type'=>'sale','payment' => 'creditcard'})
- api_response = Braintree::Spec::ApiRequest.new('0.99', params)
+ api_response = Braintree::Spec::ApiRequest.new('0.99', nil, params)
response = request("/credit_cards/new_response", :params => api_response.params)
response.should redirect_to("/credit_cards/new")
@@ -74,7 +74,7 @@
describe "and having it declined for bad cvv" do
it "should display the signup form again, pre-populated with the info from the failed transaction" do
params = quentin_form_info.merge({'type'=>'sale','payment' => 'creditcard', 'cvv' => '911'})
- api_response = Braintree::Spec::ApiRequest.new('10.00', params)
+ api_response = Braintree::Spec::ApiRequest.new('10.00', nil, params)
response = request("/credit_cards/new_response", :params => api_response.params)
response.should redirect_to("/credit_cards/new")
View
7 spec/requests/credit_cards/updating_a_card_spec.rb
@@ -10,14 +10,17 @@
end
describe "submitting the form at /credit_cards/1/edit", :given => 'a user with a credit card in the vault' do
+ before(:each) do
+ @token = User.first.credit_cards.first.token
+ end
include BrainTreeEditFormHelper
describe "and a successful response" do
it "tells the user that updating succeeded" do
query_params = {'ccexp' => '1011', 'customer_vault' => 'update_customer',
'customer_vault_id' => User.first.credit_cards.first.token,
'redirect' => 'http://example.org/credit_cards/1/edit_response' }
- api_response = Braintree::Spec::ApiRequest.new('', query_params)
+ api_response = Braintree::Spec::ApiRequest.new('', @token, query_params)
response = request("/credit_cards/1/edit_response", :params => api_response.params)
response.should redirect_to('/credit_cards')
@@ -34,7 +37,7 @@
'customer_vault_id' => User.first.credit_cards.first.token,
'redirect' => 'http://example.org/credit_cards/1/edit_response' }
- api_response = Braintree::Spec::ApiRequest.new('', query_params)
+ api_response = Braintree::Spec::ApiRequest.new('', @token, query_params)
response = request("/credit_cards/1/edit_response", :params => api_response.params)
response.should redirect_to('/credit_cards/1/edit')
View
4 spec/requests/payments/issuing_a_transaction_spec.rb
@@ -16,7 +16,7 @@
query_params = { 'customer_vault_id' => @token, 'type' => 'sale', 'amount' => '10.00',
'redirect' => 'http://example.org/credit_cards/1/new_response' }
- api_response = Braintree::Spec::ApiRequest.new('10.00', query_params)
+ api_response = Braintree::Spec::ApiRequest.new('10.00', @token, query_params)
response = request("/credit_cards/1/payments/new_response", :params => api_response.params)
response.should redirect_to("/credit_cards/1")
@@ -31,7 +31,7 @@
query_params = { 'customer_vault_id' => @token, 'type' => 'sale', 'amount' => '0.99',
'redirect' => 'http://example.org/credit_cards/1/new_response' }
- api_response = Braintree::Spec::ApiRequest.new('0.99', query_params)
+ api_response = Braintree::Spec::ApiRequest.new('0.99', @token, query_params)
response = request("/credit_cards/1/payments/new_response", :params => api_response.params)
response.should redirect_to("/credit_cards/1/payments/new")
View
3 spec/spec_helper.rb
@@ -51,7 +51,8 @@ def quentin_form_info
response.should redirect_to '/'
response = request("/credit_cards/new")
- api_response = Braintree::Spec::ApiRequest.new('10.00', quentin_form_info.merge({'type'=>'sale', 'payment'=>'creditcard'}))
+ api_response = Braintree::Spec::ApiRequest.new('10.00', nil,
+ quentin_form_info.merge({'type'=>'sale', 'payment'=>'creditcard'}))
response = request("/credit_cards/new_response", :params => api_response.params)
response.should redirect_to('/')
View
6 spec/spec_helpers/braintree/api_helper.rb
@@ -1,13 +1,13 @@
module Braintree
module Spec
class ApiRequest
- def initialize(amount, params)
- @response = Braintree::GatewayRequest.new(:amount => amount).post(params)
+ def initialize(amount, customer_vault_id, params)
+ @response = Braintree::GatewayRequest.new(:amount => amount, :customer_vault_id => customer_vault_id).post(params)
end
def params
params = @response.query_values
- params.reject! { |k,v| v == true }
+ params.reject { |k,v| v == true }
end
end
end

0 comments on commit 1863fae

Please sign in to comment.
Something went wrong with that request. Please try again.