Skip to content

Instantly share code, notes, and snippets.

@batasrki
Forked from dpickett/braintree.rb
Created April 4, 2011 18:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save batasrki/902100 to your computer and use it in GitHub Desktop.
Save batasrki/902100 to your computer and use it in GitHub Desktop.
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
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
<%= 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 -%>
<%= f.button :submit %>
<%- end -%>
add_column :accounts, :payment_processor_subscription_id, :integer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment