Created
October 19, 2010 16:31
-
-
Save mdoel/634508 to your computer and use it in GitHub Desktop.
controller that simulates a Braintree gateway. The "trade" stuff was specific to our app, so update for your situation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BraintreeSimulationController < ApplicationController | |
VALID_CC_NUMBERS = [ '4111111111111111', '5431111111111111', '6011601160116611', '341111111111111' ] | |
def transact | |
if params[:merchant_defined_field_1] == 'billing' | |
current_user.update_attributes( :bill_to_first_name => params[:firstname], | |
:bill_to_last_name => params[:lastname], | |
:bill_to_address => params[:address1], | |
:bill_to_address2 => params[:address2], | |
:bill_to_city => params[:city], | |
:bill_to_state_abbr => params[:state], | |
:bill_to_zip => params[:zip], | |
:bill_to_daytime_phone_area_code => params[:daytime_phone_area_code], | |
:bill_to_daytime_phone_prefix => params[:daytime_phone_prefix], | |
:bill_to_daytime_phone_line_number => params[:daytime_phone_line_number], | |
:bill_to_daytime_phone_extension => params[:daytime_phone_extension]) | |
end | |
case | |
when !VALID_CC_NUMBERS.include?(params[:ccnumber]) then invalid | |
when params[:amount].to_f < 1 then declined | |
else success | |
end | |
end | |
private | |
def braintree_response_with(response_params) | |
trade = Trade.find(params[:orderid]) | |
redirect_to make_trade_trade_url(trade, default_params.merge(response_params)) | |
end | |
def default_params | |
from_request = params.slice('type', 'amount', 'time', 'hash', 'orderid') | |
from_request.merge 'avsresponse' => '', | |
'username' => PAYMENT_CONFIG[:key_id], | |
'cvvresponse' => '' | |
end | |
def invalid | |
braintree_response_with 'response' => '3', | |
'responsetext' => 'Invalid Credit Card Number REFID:304746995', | |
'authcode' => '', | |
'transactionid' => '0', | |
'response_code' => '300' | |
end | |
def declined | |
braintree_response_with 'response' => '2', | |
'responsetext' => 'DECLINE', | |
'authcode' => '', | |
'transactionid' => '1083050711', | |
'response_code' => '200' | |
end | |
def success | |
braintree_response_with 'response' => '1', | |
'responsetext' => 'SUCCESS', | |
'authcode' => '123456', | |
'transactionid' => '1081825527', | |
'response_code' => '100' | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module FakewebHelpers | |
# Make sure nothing gets out (IMPORTANT) | |
FakeWeb.allow_net_connect = false | |
# Turns a fixture file name into a full path | |
def fixture_file(filename) | |
return '' if filename == '' | |
File.expand_path(RAILS_ROOT + '/spec/fixtures/' + filename) | |
end | |
# Convenience methods for stubbing URLs to fixtures | |
def stub_get(url, filename) | |
FakeWeb.register_uri(:get, url, :body => fixture_file(filename)) | |
end | |
def stub_post(url, filename) | |
FakeWeb.register_uri(:post, url, :body => fixture_file(filename)) | |
end | |
def stub_any(url, filename) | |
FakeWeb.register_uri(:any, url, :body => fixture_file(filename)) | |
end | |
def stub_braintree_transaction_query(filename) | |
url = %r|https://secure.braintreepaymentgateway.com/api/query.php| | |
stub_get url, "braintree/#{filename}" | |
end | |
end | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
map.braintree_simulation '/braintree_simulation/:action', :controller => 'braintree_simulation' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment