Created
April 4, 2011 01:30
-
-
Save dpickett/901023 to your computer and use it in GitHub Desktop.
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
if Rails.env.production? | |
Braintree::Configuration.environment = Rails.env.staging? ? :sandbox : :production | |
Braintree::Configuration.merchant_id = ENV["braintree_merchant_id"] | |
Braintree::Configuration.public_key = ENV["braintree_public_key"] | |
Braintree::Configuration.private_key = ENV["braintree_private_key"] | |
else | |
Braintree::Configuration.environment = :sandbox | |
Braintree::Configuration.merchant_id = "<super secret>" | |
Braintree::Configuration.public_key = "<super secret>" | |
Braintree::Configuration.private_key = "<super secret>" | |
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
if Rails.env.test? | |
::Rails.application.config.middleware.use BraintreeTestApp | |
end | |
class BraintreeTestApp | |
def initialize(app) | |
@app = app | |
end | |
def call(env) | |
@env = env | |
config = Braintree::Configuration.instantiate | |
if request.path =~ /\/merchants\/#{config.merchant_id}\/transparent_redirect_requests$/ | |
#proxy post to braintree | |
uri = URI.parse(config.protocol + "://" + config.server + ":" + | |
config.port.to_s + request.path) | |
http = Net::HTTP.new(uri.host, uri.port) | |
http.use_ssl = true | |
res = http.post(uri.path, request.body.read) | |
if res.code == "303" | |
header_hash = res.header.to_hash | |
header_hash["location"].first.gsub!("http://localhost:3000/", "http://www.example.com/") | |
[303, {"location" => header_hash["location"].first}, ""] | |
else | |
raise "unexpected response from Braintree: expected a 303" | |
end | |
else | |
@app.call(env) | |
end | |
end | |
def request | |
@request = Rack::Request.new(@env) | |
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 Rack | |
module Utils | |
def parse_nested_query(qs, d = nil) | |
params = ActiveSupport::OrderedHash.new | |
(qs || '').split(d ? /[#{d}] */n : DEFAULT_SEP).each do |p| | |
k, v = unescape(p).split('=', 2) | |
normalize_params(params, k, v) | |
end | |
return params | |
end | |
end | |
end | |
module Rack | |
module Test | |
class Session | |
alias_method :old_env_for, :env_for | |
def env_for(path, env) | |
if env[:params] && env[:params].empty? | |
env[:params] = ActiveSupport::OrderedHash.new | |
end | |
old_env_for(path, env) | |
end | |
end | |
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
<%= simple_form_for @braintree_customer, | |
:as => :customer, | |
:url => Braintree::TransparentRedirect.url, | |
:html => {:class => "form"} do |f| %> | |
<%= f.input :first_name, :required => true %> | |
<%= f.input :last_name, :required => true %> | |
<%= f.input :email, :required => true %> | |
<%= f.fields_for :credit_card, @braintree_customer.credit_card do |cc| -%> | |
<%= cc.input :number, | |
:input_html => {:autocomplete => "off" }, | |
:wrapper_html => {:class => "credit_card_number"}, | |
:hint => image_tag("/images/cards_accepted.png", :alt => "MasterCard, Visa") %> | |
<%= cc.input :expiration_month, :collection => 1..12, | |
:required => true, | |
:wrapper_html => {:class => "expiration_month"} %> | |
<%= cc.input :expiration_year, | |
:wrapper_html => {:class => "expiration_year"}, | |
:collection => Time.zone.now.year..(Time.zone.now.year + 25) %> | |
<%= cc.input :cvv, | |
:input_html => {:autocomplete => "off" }, | |
:required => true, | |
:wrapper_html => {:class => "cvv"} %> | |
<%- end -%> | |
<%= hidden_field_tag :tr_data, @braintree_customer.redirect_data %> | |
<%= f.button :submit %> | |
<%- 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
add_column :accounts, :payment_processor_subscription_id, :integer |
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
VCR.config do |c| | |
c.cassette_library_dir = 'features/cassettes' | |
c.stub_with :webmock | |
c.default_cassette_options = { :record => :new_episodes } | |
end | |
VCR.cucumber_tags do |t| | |
t.tags '@braintree_valid', | |
'@braintree_invalid', | |
'@braintree_valid_bad_account' | |
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
VCR.config do |c| | |
c.cassette_library_dir = 'features/cassettes' | |
c.stub_with :webmock | |
braintree_config = Braintree::Configuration.instantiate | |
c.default_cassette_options = { :record => :new_episodes, :erb => { | |
:braintree_merchant_id => braintree_config.merchant_id, | |
:braintree_public_key => braintree_config.public_key, | |
:braintree_private_key => braintree_config.private_key | |
} } | |
end | |
module VcrHelper | |
def self.obscure_credentials | |
braintree_config = Braintree::Configuration.instantiate | |
[ | |
"spec/cassettes/**/*.yml", | |
"features/cassettes/**/*.yml" | |
].each do |glob| | |
Dir.glob(Rails.root.join(glob)).each do |f| | |
contents = File.read(f) | |
File.open(f, "w") do |j| | |
j << contents.gsub(CGI.escape(braintree_config.merchant_id), "<%= braintree_merchant_id %>"). | |
gsub(CGI.escape(braintree_config.public_key), "<%= braintree_public_key %>"). | |
gsub(CGI.escape(braintree_config.private_key), "<%= braintree_private_key %>") | |
end | |
end | |
end | |
end | |
end | |
at_exit do | |
VcrHelper.obscure_credentials | |
end | |
Sorry, made a typo in my previous comment. I've updated it.
hoverlover is correct, this would not actually work right. Saying "if not Rails.env.test?" in the first check would give the effect the author wants.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm well aware of the ternary operator. Maybe you didn't see this part of my comment:
If execution falls through the
if Rails.env.production?
branch,Rails.env.staging?
will always return false, right? So in your gist,:production
would always be the outcome. That's why I said you might as well just hard-code it to:production
.