Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

initial commit

  • Loading branch information...
commit 89e83e9e3acc153cadffb5e82ae9594bec5e62a8 1 parent 1dd18dd
@jsqu99 jsqu99 authored
Showing with 353 additions and 0 deletions.
  1. +10 −0 .gitignore
  2. +1 −0  .rspec
  3. +26 −0 LICENSE
  4. +31 −0 Rakefile
  5. +9 −0 Versionfile
  6. +1 −0  app/assets/javascripts/admin/spree_reuse_credit_card.js
  7. +1 −0  app/assets/javascripts/store/spree_reuse_credit_card.js
  8. +3 −0  app/assets/stylesheets/admin/spree_reuse_credit_card.css
  9. +3 −0  app/assets/stylesheets/store/spree_reuse_credit_card.css
  10. +34 −0 app/controllers/checkout_controller_decorator.rb
  11. +11 −0 app/controllers/users_controller_decorator.rb
  12. +13 −0 app/helpers/card_reuse_helper.rb
  13. +6 −0 app/overrides/add_credit_card_list_to_payment_form.rb
  14. +6 −0 app/overrides/add_credit_card_list_to_user_account_form.html.rb
  15. +31 −0 app/views/checkout/payment/_existing_cards.html.erb
  16. +31 −0 app/views/users/_card_admin.html.erb
  17. +3 −0  config/routes.rb
  18. +10 −0 lib/card_reuse.rb
  19. +29 −0 lib/generators/spree_reuse_credit_card/install/install_generator.rb
  20. +2 −0  lib/spree_reuse_credit_card.rb
  21. +29 −0 lib/spree_reuse_credit_card/engine.rb
  22. +9 −0 script/rails
  23. +31 −0 spec/spec_helper.rb
  24. +23 −0 spree_reuse_credit_card.gemspec
View
10 .gitignore
@@ -0,0 +1,10 @@
+\#*
+*~
+.#*
+.DS_Store
+.idea
+.project
+tmp
+nbproject
+*.swp
+spec/dummy
View
1  .rspec
@@ -0,0 +1 @@
+--colour
View
26 LICENSE
@@ -0,0 +1,26 @@
+Copyright (c) 2011 [name of plugin creator]
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without modification,
+are permitted provided that the following conditions are met:
+
+ * Redistributions of source code must retain the above copyright notice,
+ this list of conditions and the following disclaimer.
+ * Redistributions in binary form must reproduce the above copyright notice,
+ this list of conditions and the following disclaimer in the documentation
+ and/or other materials provided with the distribution.
+ * Neither the name Spree nor the names of its contributors may be used to
+ endorse or promote products derived from this software without specific
+ prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
View
31 Rakefile
@@ -0,0 +1,31 @@
+require 'rake'
+require 'rake/testtask'
+require 'rake/packagetask'
+require 'rubygems/package_task'
+require 'rspec/core/rake_task'
+require 'cucumber/rake/task'
+require 'spree_core/testing_support/common_rake'
+
+RSpec::Core::RakeTask.new
+Cucumber::Rake::Task.new
+
+task :default => [:spec, :cucumber ]
+
+spec = eval(File.read('spree_reuse_credit_card.gemspec'))
+
+Gem::PackageTask.new(spec) do |p|
+ p.gem_spec = spec
+end
+
+desc "Release to gemcutter"
+task :release => :package do
+ require 'rake/gemcutter'
+ Rake::Gemcutter::Tasks.new(spec).define
+ Rake::Task['gem:push'].invoke
+end
+
+desc "Generates a dummy app for testing"
+task :test_app do
+ ENV['LIB_NAME'] = 'spree_reuse_credit_card'
+ Rake::Task['common:test_app'].invoke
+end
View
9 Versionfile
@@ -0,0 +1,9 @@
+# This file is used to designate compatibilty with different versions of Spree
+# Please see http://spreecommerce.com/documentation/extensions.html#versionfile for details
+
+# Examples
+#
+# "0.70.x" => { :branch => "master"}
+# "0.60.x" => { :branch => "0-60-stable" }
+# "0.40.x" => { :tag => "v1.0.0", :version => "1.0.0" }
+
View
1  app/assets/javascripts/admin/spree_reuse_credit_card.js
@@ -0,0 +1 @@
+//= require admin/spree_core
View
1  app/assets/javascripts/store/spree_reuse_credit_card.js
@@ -0,0 +1 @@
+//= require store/spree_core
View
3  app/assets/stylesheets/admin/spree_reuse_credit_card.css
@@ -0,0 +1,3 @@
+/*
+ *= require admin/spree_core
+*/
View
3  app/assets/stylesheets/store/spree_reuse_credit_card.css
@@ -0,0 +1,3 @@
+/*
+ *= require store/spree_core
+*/
View
34 app/controllers/checkout_controller_decorator.rb
@@ -0,0 +1,34 @@
+CheckoutController.class_eval do
+
+ require 'card_reuse'
+
+ private
+
+ def before_payment
+ current_order.payments.destroy_all if request.put?
+ @cards = all_credit_cards_for_user(@order.user)
+ end
+
+ # we are overriding this method in order to substitue in the exisiting card information
+ def object_params
+ # For payment step, filter order parameters to produce the expected nested attributes for a single payment and its source, discarding attributes for payment methods other than the one selected
+ if @order.payment?
+ if params[:payment_source].present? && source_params = params.delete(:payment_source)[params[:order][:payments_attributes].first[:payment_method_id].underscore]
+
+ if params[:existing_card]
+ params[:order][:payments_attributes].first[:source_id] = params[:existing_card]
+ params[:order][:payments_attributes].first[:source_type] = 'Creditcard'
+ else
+ params[:order][:payments_attributes].first[:source_attributes] = source_params
+ end
+
+ end
+ if (params[:order][:payments_attributes])
+ params[:order][:payments_attributes].first[:amount] = @order.total
+ end
+ end
+
+ params[:order]
+ end
+
+end
View
11 app/controllers/users_controller_decorator.rb
@@ -0,0 +1,11 @@
+UsersController.class_eval do
+ require 'card_reuse'
+
+ before_filter :load_existing_cards, :only => :show
+
+ private
+
+ def load_existing_cards
+ @cards = all_credit_cards_for_user(@user)
+ end
+end
View
13 app/helpers/card_reuse_helper.rb
@@ -0,0 +1,13 @@
+module CardReuseHelper
+ def all_credit_cards_for_user(user)
+ return nil unless user
+
+ # assumption is that all payments to mcsweeney's are via credit card
+ orders = Order.where(:user_id => user.id).complete.order(:created_at)
+ payments = orders.map(&:payments).flatten
+
+ payments.reject! {|payment| payment.state != 'completed'}
+
+ payments.map(&:source)
+ end
+end
View
6 app/overrides/add_credit_card_list_to_payment_form.rb
@@ -0,0 +1,6 @@
+Deface::Override.new(
+ :name => 'add_credit_card_list_to_payment_form',
+ :virtual_path => 'checkout/payment/_gateway',
+ :insert_before => '[data-hook=card_number]',
+ :partial =>'checkout/payment/existing_cards'
+)
View
6 app/overrides/add_credit_card_list_to_user_account_form.html.rb
@@ -0,0 +1,6 @@
+Deface::Override.new(
+ :name => 'add_credit_card_list_to_user_account_form',
+ :virtual_path => 'users/show',
+ :insert_after => '[data-hook=account_my_orders]',
+ :partial =>'shared/card_admin'
+)
View
31 app/views/checkout/payment/_existing_cards.html.erb
@@ -0,0 +1,31 @@
+<h2>Use one of these cards on file?</h2>
+
+<p class="field">
+
+ <% if @cards.present? %>
+ <table class="order-summary" style="width:545px;">
+ <thead>
+ <tr>
+ <th>Card Number(last four digits)</th>
+ <th>Expiration Month</th>
+ <th>Expiration Year</th>
+ <th></th>
+ </tr>
+ </thead>
+ <tbody>
+ <% @cards.each do |card| %>
+ <tr class="<%= cycle('even', 'odd') %>">
+ <td><%= card.last_digits %></td>
+ <td><%= card.month %></td>
+ <td><%= card.year %></td>
+ <td><%= radio_button_tag "existing_card", card.id %></td>
+<!-- #{param_prefix} -->
+ </tr>
+ <% end %>
+ </tbody>
+ </table>
+ <% end %>
+</p>
+
+
+
View
31 app/views/users/_card_admin.html.erb
@@ -0,0 +1,31 @@
+<%# requires local being passed in for user %>
+
+<h2>Credit Cards on File</h2>
+
+<p class="field">
+ <% if @cards.present? %>
+ <table class="order-summary" style="width:545px;">
+ <thead>
+ <tr>
+ <th>Card Number(last four digits)</th>
+ <th>Expiration Month</th>
+ <th>Expiration Year</th>
+ <th></th>
+ </tr>
+ </thead>
+ <tbody>
+ <% @cards.each do |card| %>
+ <tr class="<%= cycle('even', 'odd') %>">
+ <td><%= card.last_digits %></td>
+ <td><%= card.month %></td>
+ <td><%= card.year %></td>
+ <td><%= radio_button_tag 'credit_card_id', card.id %></td>
+ </tr>
+ <% end %>
+ </tbody>
+ </table>
+ <% end %>
+</p>
+
+
+
View
3  config/routes.rb
@@ -0,0 +1,3 @@
+Rails.application.routes.draw do
+ # Add your extension routes here
+end
View
10 lib/card_reuse.rb
@@ -0,0 +1,10 @@
+module CardReuse
+ def all_credit_cards_for_user(user)
+ return nil unless user
+
+ orders = Order.where(:user_id => user.id).complete.order(:created_at).joins(:payments).where('payments.source_type' => 'Creditcard').where('payments.state' => 'completed')
+ payments = orders.map(&:payments).flatten
+
+ payments.map(&:source)
+ end
+end
View
29 lib/generators/spree_reuse_credit_card/install/install_generator.rb
@@ -0,0 +1,29 @@
+module SpreeReuseCreditCard
+ module Generators
+ class InstallGenerator < Rails::Generators::Base
+
+ def add_javascripts
+ append_file "app/assets/javascripts/store/all.js", "//= require store/spree_reuse_credit_card\n"
+ append_file "app/assets/javascripts/admin/all.js", "//= require admin/spree_reuse_credit_card\n"
+ end
+
+ def add_stylesheets
+ inject_into_file "app/assets/stylesheets/store/all.css", " *= require store/spree_reuse_credit_card\n", :before => /\*\//, :verbose => true
+ inject_into_file "app/assets/stylesheets/admin/all.css", " *= require admin/spree_reuse_credit_card\n", :before => /\*\//, :verbose => true
+ end
+
+ def add_migrations
+ run 'bundle exec rake railties:install:migrations FROM=spree_reuse_credit_card'
+ end
+
+ def run_migrations
+ res = ask "Would you like to run the migrations now? [Y/n]"
+ if res == "" || res.downcase == "y"
+ run 'bundle exec rake db:migrate'
+ else
+ puts "Skiping rake db:migrate, don't forget to run it!"
+ end
+ end
+ end
+ end
+end
View
2  lib/spree_reuse_credit_card.rb
@@ -0,0 +1,2 @@
+require 'spree_core'
+require 'spree_reuse_credit_card/engine'
View
29 lib/spree_reuse_credit_card/engine.rb
@@ -0,0 +1,29 @@
+module SpreeReuseCreditCard
+ class Engine < Rails::Engine
+ engine_name 'spree_reuse_credit_card'
+
+ config.autoload_paths += %W(#{config.root}/lib)
+
+ # use rspec for tests
+ config.generators do |g|
+ g.test_framework :rspec
+ end
+
+ def self.activate
+ Dir.glob(File.join(File.dirname(__FILE__), "../../app/**/*_decorator*.rb")) do |c|
+ Rails.application.config.cache_classes ? require(c) : load(c)
+ end
+
+ Dir.glob(File.join(File.dirname(__FILE__), "../../app/overrides/*.rb")) do |c|
+ Rails.application.config.cache_classes ? require(c) : load(c)
+ end
+
+ # Yeah, I know. Probably wrong place for this
+ # There are problems putting it in other places as well
+ # (re-educate me if I'm thoroughly confused (which I am))
+ Spree::Config.set(:auto_capture => true) rescue nil
+ end
+
+ config.to_prepare &method(:activate).to_proc
+ end
+end
View
9 script/rails
@@ -0,0 +1,9 @@
+#!/usr/bin/env ruby
+# This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application.
+
+
+ENGINE_PATH = File.expand_path('../..', __FILE__)
+APP_PATH = File.expand_path('../../../config/application', __FILE__)
+require File.expand_path('../../../config/boot', __FILE__)
+require 'rails/commands'
+
View
31 spec/spec_helper.rb
@@ -0,0 +1,31 @@
+# Configure Rails Environment
+ENV["RAILS_ENV"] = "test"
+
+
+require File.expand_path("../../../config/environment.rb", __FILE__)
+
+
+require 'rspec/rails'
+
+# Requires supporting ruby files with custom matchers and macros, etc,
+# in spec/support/ and its subdirectories.
+Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
+
+RSpec.configure do |config|
+ # == Mock Framework
+ #
+ # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
+ #
+ # config.mock_with :mocha
+ # config.mock_with :flexmock
+ # config.mock_with :rr
+ config.mock_with :rspec
+
+ # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
+ config.fixture_path = "#{::Rails.root}/spec/fixtures"
+
+ # If you're not using ActiveRecord, or you'd prefer not to run each of your
+ # examples within a transaction, remove the following line or assign false
+ # instead of true.
+ config.use_transactional_fixtures = true
+end
View
23 spree_reuse_credit_card.gemspec
@@ -0,0 +1,23 @@
+# encoding: UTF-8
+Gem::Specification.new do |s|
+ s.platform = Gem::Platform::RUBY
+ s.name = 'spree_reuse_credit_card'
+ s.version = '0.70.4.beta'
+ s.summary = 'Add gem summary here'
+ s.description = 'Add (optional) gem description here'
+ s.required_ruby_version = '>= 1.8.7'
+
+ # s.author = 'David Heinemeier Hansson'
+ # s.email = 'david@loudthinking.com'
+ # s.homepage = 'http://www.rubyonrails.org'
+ # s.rubyforge_project = 'actionmailer'
+
+ #s.files = `git ls-files`.split("\n")
+ #s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
+ s.require_path = 'lib'
+ s.requirements << 'none'
+
+ s.add_dependency 'spree_core', '>= 0.70.4.beta'
+ s.add_development_dependency 'rspec-rails'
+end
+
Please sign in to comment.
Something went wrong with that request. Please try again.