Permalink
Browse files

initial import for the braintree slice

  • Loading branch information...
atmos committed Jan 3, 2009
0 parents commit 9a6188bd8ff3b4e052b19000fb980c777a9f01ff
Showing with 1,100 additions and 0 deletions.
  1. +20 −0 LICENSE
  2. +76 −0 README
  3. +55 −0 Rakefile
  4. +15 −0 TODO
  5. +5 −0 app/controllers/application.rb
  6. +63 −0 app/controllers/credit_cards.rb
  7. +7 −0 app/controllers/main.rb
  8. +20 −0 app/controllers/payments.rb
  9. +64 −0 app/helpers/application_helper.rb
  10. +27 −0 app/views/credit_cards/_form.html.haml
  11. +6 −0 app/views/credit_cards/_gateway_request.html.haml
  12. +8 −0 app/views/credit_cards/destroy.html.haml
  13. +6 −0 app/views/credit_cards/edit.html.haml
  14. +24 −0 app/views/credit_cards/index.html.haml
  15. +6 −0 app/views/credit_cards/new.html.haml
  16. +30 −0 app/views/credit_cards/show.html.haml
  17. +16 −0 app/views/layout/braintree_transparent_redirect_slice.html.erb
  18. +1 −0 app/views/main/index.html.erb
  19. +2 −0 app/views/payments/index.html.haml
  20. +18 −0 app/views/payments/new.html.haml
  21. +43 −0 config/init.rb
  22. +5 −0 config/router.rb
  23. +82 −0 lib/braintree_transparent_redirect_slice.rb
  24. +103 −0 lib/braintree_transparent_redirect_slice/merbtasks.rb
  25. +20 −0 lib/braintree_transparent_redirect_slice/slicetasks.rb
  26. +53 −0 lib/braintree_transparent_redirect_slice/spectasks.rb
  27. 0 public/javascripts/master.js
  28. +2 −0 public/stylesheets/master.css
  29. +20 −0 spec/braintree_transparent_redirect_slice_spec.rb
  30. +104 −0 spec/requests/credit_cards/adding_a_card_spec.rb
  31. +15 −0 spec/requests/credit_cards/deleting_a_card_spec.rb
  32. +48 −0 spec/requests/credit_cards/updating_a_card_spec.rb
  33. +30 −0 spec/requests/main_spec.rb
  34. +44 −0 spec/requests/payments/issuing_a_transaction_spec.rb
  35. +58 −0 spec/spec_helper.rb
  36. +2 −0 stubs/app/controllers/application.rb
  37. +2 −0 stubs/app/controllers/main.rb
20 LICENSE
@@ -0,0 +1,20 @@
+Copyright (c) 2009 Your Name
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
76 README
@@ -0,0 +1,76 @@
+BraintreeTransparentRedirectSlice
+=================================
+
+A slice for the Merb framework.
+
+------------------------------------------------------------------------------
+
+
+To see all available tasks for BraintreeTransparentRedirectSlice run:
+
+rake -T slices:braintree_transparent_redirect_slice
+
+------------------------------------------------------------------------------
+
+Instructions for installation:
+
+file: config/init.rb
+
+# add the slice as a regular dependency
+
+dependency 'braintree_transparent_redirect_slice'
+
+# if needed, configure which slices to load and in which order
+
+Merb::Plugins.config[:merb_slices] = { :queue => ["BraintreeTransparentRedirectSlice", ...] }
+
+# optionally configure the plugins in a before_app_loads callback
+
+Merb::BootLoader.before_app_loads do
+
+ Merb::Slices::config[:braintree_transparent_redirect_slice][:option] = value
+
+end
+
+file: config/router.rb
+
+# example: /braintree_transparent_redirect_slice/:controller/:action/:id
+
+add_slice(:BraintreeTransparentRedirectSlice)
+
+# example: /:lang/:controller/:action/:id
+
+add_slice(:BraintreeTransparentRedirectSlice, :path => ':lang')
+
+# example: /:controller/:action/:id
+
+slice(:BraintreeTransparentRedirectSlice)
+
+Normally you should also run the following rake task:
+
+rake slices:braintree_transparent_redirect_slice:install
+
+------------------------------------------------------------------------------
+
+You can put your application-level overrides in:
+
+host-app/slices/braintree_transparent_redirect_slice/app - controllers, models, views ...
+
+Templates are located in this order:
+
+1. host-app/slices/braintree_transparent_redirect_slice/app/views/*
+2. gems/braintree_transparent_redirect_slice/app/views/*
+3. host-app/app/views/*
+
+You can use the host application's layout by configuring the
+braintree_transparent_redirect_slice slice in a before_app_loads block:
+
+Merb::Slices.config[:braintree_transparent_redirect_slice] = { :layout => :application }
+
+By default :braintree_transparent_redirect_slice is used. If you need to override
+stylesheets or javascripts, just specify your own files in your layout
+instead/in addition to the ones supplied (if any) in
+host-app/public/slices/braintree_transparent_redirect_slice.
+
+In any case don't edit those files directly as they may be clobbered any time
+rake braintree_transparent_redirect_slice:install is run.
@@ -0,0 +1,55 @@
+require 'rubygems'
+require 'rake/gempackagetask'
+
+require 'merb-core'
+require 'merb-core/tasks/merb'
+
+GEM_NAME = "braintree_transparent_redirect_slice"
+AUTHOR = "Your Name"
+EMAIL = "Your Email"
+HOMEPAGE = "http://merbivore.com/"
+SUMMARY = "Merb Slice that provides ..."
+GEM_VERSION = "1.0.7.1"
+
+spec = Gem::Specification.new do |s|
+ s.rubyforge_project = 'merb'
+ s.name = GEM_NAME
+ s.version = GEM_VERSION
+ s.platform = Gem::Platform::RUBY
+ s.has_rdoc = true
+ s.extra_rdoc_files = ["README", "LICENSE", 'TODO']
+ s.summary = SUMMARY
+ s.description = s.summary
+ s.author = AUTHOR
+ s.email = EMAIL
+ s.homepage = HOMEPAGE
+ s.add_dependency('merb-slices', '>= 1.0.7.1')
+ s.require_path = 'lib'
+ s.files = %w(LICENSE README Rakefile TODO) + Dir.glob("{lib,spec,app,public,stubs}/**/*")
+end
+
+Rake::GemPackageTask.new(spec) do |pkg|
+ pkg.gem_spec = spec
+end
+
+desc "Install the gem"
+task :install do
+ Merb::RakeHelper.install(GEM_NAME, :version => GEM_VERSION)
+end
+
+desc "Uninstall the gem"
+task :uninstall do
+ Merb::RakeHelper.uninstall(GEM_NAME, :version => GEM_VERSION)
+end
+
+desc "Create a gemspec file"
+task :gemspec do
+ File.open("#{GEM_NAME}.gemspec", "w") do |file|
+ file.puts spec.to_ruby
+ end
+end
+
+require 'spec/rake/spectask'
+require 'merb-core/test/tasks/spectasks'
+desc 'Default: run spec examples'
+task :default => 'spec'
15 TODO
@@ -0,0 +1,15 @@
+TODO:
+
+- Fix BraintreeTransparentRedirectSlice.description and BraintreeTransparentRedirectSlice.version
+- Fix LICENSE with your name
+- Fix Rakefile with your name and contact info
+- Add your code to lib/braintree_transparent_redirect_slice.rb
+- Add your Merb rake tasks to lib/braintree_transparent_redirect_slice/merbtasks.rb
+
+Remove anything that you don't need:
+
+- app/controllers/main.rb BraintreeTransparentRedirectSlice::Main controller
+- app/views/layout/braintree_transparent_redirect_slice.html.erb
+- spec/controllers/main_spec.rb controller specs
+- public/* any public files
+- stubs/* any stub files
@@ -0,0 +1,5 @@
+class BraintreeTransparentRedirectSlice::Application < Merb::Controller
+
+ controller_for_slice
+
+end
@@ -0,0 +1,63 @@
+class CreditCards < Application
+ def index
+ render
+ end
+
+ def new
+ @credit_card = CreditCard.new
+ @credit_card_info = @credit_card.info
+
+ unless message[:transaction_id].nil?
+ @credit_card_info = Braintree::TransactionInfo.new(message[:transaction_id])
+ end
+
+ @gateway_request = Braintree::GatewayRequest.new(:orderid => Digest::SHA1.hexdigest(Time.now.to_s))
+ render
+ end
+
+ def new_response
+ @gateway_response = Braintree::GatewayResponse.new(params)
+ raise Unauthorized unless @gateway_response.is_valid?
+ case @gateway_response.response_status
+ when 'approved'
+ if @gateway_response.cvv_matches?
+ session.user.credit_cards.create(:token => @gateway_response.customer_vault_id)
+ redirect('/', :message => {:notice => 'Successfully stored your card info securely.'})
+ else
+ redirect(url(:new_credit_card), :message => {:notice => @gateway_response.cvvresponse_string, :transaction_id => params['transactionid']})
+ end
+ else
+ redirect(url(:new_credit_card), :message => {:notice => @gateway_response.responsetext, :transaction_id => params['transactionid']})
+ end
+ end
+
+ def edit(id)
+ fetch_credit_card(id)
+ @credit_card_info = @credit_card.info
+ @gateway_request = Braintree::GatewayRequest.new
+ render
+ end
+
+ def edit_response(id)
+ @gateway_response = Braintree::GatewayResponse.new(params.reject { |k,v| k == 'id' })
+ raise Unauthorized unless @gateway_response.is_valid?
+ case @gateway_response.response_status
+ when 'approved'
+ redirect(url(:credit_cards), :message => {:notice => 'Successfully updated your info in the vault.'})
+ else
+ fetch_credit_card(id)
+ redirect(url(:edit_credit_card, @credit_card), :message => {:notice => @gateway_response.responsetext})
+ end
+ end
+
+ def show(id)
+ fetch_credit_card(id)
+ render
+ end
+
+ def destroy(id)
+ fetch_credit_card(id)
+ @gateway_request = Braintree::GatewayRequest.new
+ render
+ end
+end
@@ -0,0 +1,7 @@
+class BraintreeTransparentRedirectSlice::Main < BraintreeTransparentRedirectSlice::Application
+
+ def index
+ render
+ end
+
+end
@@ -0,0 +1,20 @@
+class Payments < Application
+ def new(credit_card_id)
+ fetch_credit_card(credit_card_id)
+
+ @gateway_request = Braintree::GatewayRequest.new(:amount => '10.00')
+ render
+ end
+
+ def new_response(credit_card_id)
+ @gateway_response = Braintree::GatewayResponse.new(params.reject { |k,v| k == 'credit_card_id' })
+ raise Unauthorized unless @gateway_response.is_valid?
+ case @gateway_response.response_status
+ when 'approved'
+ fetch_credit_card(credit_card_id)
+ redirect(url(:credit_card, @credit_card), :message => {:notice => 'Successfully charged your Credit Card.'})
+ else
+ redirect(url(:new_credit_card_payment), :message => {:notice => @gateway_response.responsetext})
+ end
+ end
+end
@@ -0,0 +1,64 @@
+module Merb
+ module BraintreeTransparentRedirectSlice
+ module ApplicationHelper
+
+ # @param *segments<Array[#to_s]> Path segments to append.
+ #
+ # @return <String>
+ # A path relative to the public directory, with added segments.
+ def image_path(*segments)
+ public_path_for(:image, *segments)
+ end
+
+ # @param *segments<Array[#to_s]> Path segments to append.
+ #
+ # @return <String>
+ # A path relative to the public directory, with added segments.
+ def javascript_path(*segments)
+ public_path_for(:javascript, *segments)
+ end
+
+ # @param *segments<Array[#to_s]> Path segments to append.
+ #
+ # @return <String>
+ # A path relative to the public directory, with added segments.
+ def stylesheet_path(*segments)
+ public_path_for(:stylesheet, *segments)
+ end
+
+ # Construct a path relative to the public directory
+ #
+ # @param <Symbol> The type of component.
+ # @param *segments<Array[#to_s]> Path segments to append.
+ #
+ # @return <String>
+ # A path relative to the public directory, with added segments.
+ def public_path_for(type, *segments)
+ ::BraintreeTransparentRedirectSlice.public_path_for(type, *segments)
+ end
+
+ # Construct an app-level path.
+ #
+ # @param <Symbol> The type of component.
+ # @param *segments<Array[#to_s]> Path segments to append.
+ #
+ # @return <String>
+ # A path within the host application, with added segments.
+ def app_path_for(type, *segments)
+ ::BraintreeTransparentRedirectSlice.app_path_for(type, *segments)
+ end
+
+ # Construct a slice-level path.
+ #
+ # @param <Symbol> The type of component.
+ # @param *segments<Array[#to_s]> Path segments to append.
+ #
+ # @return <String>
+ # A path within the slice source (Gem), with added segments.
+ def slice_path_for(type, *segments)
+ ::BraintreeTransparentRedirectSlice.slice_path_for(type, *segments)
+ end
+
+ end
+ end
+end
@@ -0,0 +1,27 @@
+- info = @credit_card_info
+= text_field :id => 'firstname', :name => 'firstname', :label => 'First Name', :value => info.first_name, :class => 'required'
+= text_field :id => 'lastname', :name => 'lastname', :label => 'Last Name', :value => info.last_name, :class => 'required'
+= text_field :id => 'email', :name => 'email', :value => info.email, :label => 'Email Address', :class => 'required'
+= text_field :id => 'address1', :name => 'address1', :value => info.address_1, :label => 'Address', :class => 'required'
+= text_field :id => 'city', :name => 'city', :value => info.city, :label => 'City', :class => 'required'
+= text_field :id => 'state', :name => 'state', :value => info.state, :size => 2, :label => 'State', :class => 'required'
+= text_field :id => 'zip', :name => 'zip', :value => info.postal_code, :label => 'Postal Code', :class => 'required'
+= text_field :id => 'country', :name => 'country', :value => info.country, :label => 'Country', :class => 'required'
+
+= partial "gateway_request"
+
+- if @credit_card.new_record?
+ = text_field :id => 'ccnumber', :name => 'ccnumber', :value => '', :label => 'Credit Card Number', :class => 'required'
+ = text_field :id => 'cvv', :name => 'cvv', :value => '', :label => 'CVV', :class => 'required'
+= text_field :id => 'ccexp', :name => 'ccexp', :value => info.cc_exp, :label => 'Expiration Date(MMYY)', :class => 'required'
+= hidden_field :id => 'orderid', :name => 'orderid', :value => ''
+
+- 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
+ = hidden_field :id => 'redirect', :name => 'redirect', :value => absolute_url(:edit_response_credit_card, @credit_card)
@@ -0,0 +1,6 @@
+= hidden_field :id => 'payment', :name => 'payment', :value => 'creditcard'
+= hidden_field :id => 'key_id', :name => 'key_id', :value => @gateway_request.key_id
+= hidden_field :id => 'order_id', :name => 'order_id', :value => @gateway_request.orderid
+= hidden_field :id => 'amount', :name => 'amount', :value => @gateway_request.amount
+= hidden_field :id => 'time', :name => 'time', :value => @gateway_request.time
+= hidden_field :id => 'hash', :name => 'hash', :value => @gateway_request.hash
@@ -0,0 +1,8 @@
+= form :action => BRAINTREE[:transact_api_url], :id => 'delete_vault_form' do
+ %fieldset
+ %legend I'm about to delete your billing information, are you sure you want to?
+ = partial "gateway_request"
+ = hidden_field :id => 'customer_vault', :name => 'customer_vault', :value => 'delete_customer'
+ = hidden_field :id => 'customer_vault_id', :name => 'customer_vault_id', :value => @credit_card.token
+ = hidden_field :id => 'redirect', :name => 'redirect', :value => absolute_url(:credit_cards)
+ %input{:type => 'submit', :name => 'submit', :value => 'Delete'}
@@ -0,0 +1,6 @@
+= form :action => BRAINTREE[:transact_api_url], :id => 'edit_vault_form' do
+ %fieldset
+ %legend This is what you can update for this credit card
+ = partial "form"
+ %input{:type => 'submit', :name => 'submit', :value => 'Update'}
+
Oops, something went wrong.

0 comments on commit 9a6188b

Please sign in to comment.