spree_payment_calculator 0.70.0
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +26 -0
- data/README.md +18 -0
- data/app/assets/javascripts/admin/spree_payment_calculator.js +1 -0
- data/app/assets/javascripts/store/spree_payment_calculator.js +1 -0
- data/app/assets/stylesheets/admin/spree_payment_calculator.css +3 -0
- data/app/assets/stylesheets/store/spree_payment_calculator.css +3 -0
- data/app/model/payment_calculator/flat_percent_item_total.rb +20 -0
- data/app/model/payment_calculator/flat_rate.rb +17 -0
- data/app/model/payment_calculator/flexi_rate.rb +33 -0
- data/app/model/payment_calculator/per_item.rb +17 -0
- data/app/model/payment_calculator/price_sack.rb +30 -0
- data/app/model/payment_decorator.rb +26 -0
- data/app/model/payment_method_decorator.rb +3 -0
- data/app/overrides/add_calculators_to_payment_methods.rb +5 -0
- data/config/locales/en.yml +3 -0
- data/config/locales/it.yml +3 -0
- data/config/routes.rb +3 -0
- data/lib/generators/spree_payment_calculator/install/install_generator.rb +29 -0
- data/lib/spree/environment/calculators.rb +9 -0
- data/lib/spree_payment_calculator/engine.rb +37 -0
- data/lib/spree_payment_calculator.rb +2 -0
- metadata +114 -0
data/LICENSE
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
Copyright (c) 2012 [name of plugin creator]
|
2
|
+
All rights reserved.
|
3
|
+
|
4
|
+
Redistribution and use in source and binary forms, with or without modification,
|
5
|
+
are permitted provided that the following conditions are met:
|
6
|
+
|
7
|
+
* Redistributions of source code must retain the above copyright notice,
|
8
|
+
this list of conditions and the following disclaimer.
|
9
|
+
* Redistributions in binary form must reproduce the above copyright notice,
|
10
|
+
this list of conditions and the following disclaimer in the documentation
|
11
|
+
and/or other materials provided with the distribution.
|
12
|
+
* Neither the name Spree nor the names of its contributors may be used to
|
13
|
+
endorse or promote products derived from this software without specific
|
14
|
+
prior written permission.
|
15
|
+
|
16
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
17
|
+
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
18
|
+
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
19
|
+
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
20
|
+
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
21
|
+
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
22
|
+
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
23
|
+
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
24
|
+
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
25
|
+
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
26
|
+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
data/README.md
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
SpreePaymentCalculator
|
2
|
+
======================
|
3
|
+
|
4
|
+
Added a calculator for PaymentMethod for Spree
|
5
|
+
|
6
|
+
|
7
|
+
Basic Installation
|
8
|
+
------------------
|
9
|
+
|
10
|
+
1. Add the following to your Gemfile
|
11
|
+
<pre>
|
12
|
+
gem 'spree_payment_calculator', '~> 0.70.0'
|
13
|
+
</pre>
|
14
|
+
2. Run `bundle install`
|
15
|
+
3. Go to Payment Method in the Admin panel and add a calculator for each type of payment you prefer
|
16
|
+
|
17
|
+
|
18
|
+
Copyright (c) 2012 [Damiano Giacomello], released under the New BSD License
|
@@ -0,0 +1 @@
|
|
1
|
+
//= require admin/spree_core
|
@@ -0,0 +1 @@
|
|
1
|
+
//= require store/spree_core
|
@@ -0,0 +1,20 @@
|
|
1
|
+
class PaymentCalculator::FlatPercentItemTotal < Calculator
|
2
|
+
preference :flat_percent, :decimal, :default => 0
|
3
|
+
|
4
|
+
# Register the calculator
|
5
|
+
def self.register
|
6
|
+
super
|
7
|
+
PaymentMethod.register_calculator(self)
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.description
|
11
|
+
I18n.t("flat_percent")
|
12
|
+
end
|
13
|
+
|
14
|
+
def compute(object)
|
15
|
+
return unless object.present? and object.line_items.present?
|
16
|
+
item_total = object.line_items.map(&:amount).sum
|
17
|
+
value = item_total * self.preferred_flat_percent / 100.0
|
18
|
+
(value * 100).round.to_f / 100
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
class PaymentCalculator::FlatRate < Calculator
|
2
|
+
preference :amount, :decimal, :default => 0
|
3
|
+
|
4
|
+
# Register the calculator
|
5
|
+
def self.register
|
6
|
+
super
|
7
|
+
PaymentMethod.register_calculator(self)
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.description
|
11
|
+
I18n.t("flat_rate_per_order")
|
12
|
+
end
|
13
|
+
|
14
|
+
def compute(object=nil)
|
15
|
+
self.preferred_amount
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
class PaymentCalculator::FlexiRate < Calculator
|
2
|
+
preference :first_item, :decimal, :default => 0
|
3
|
+
preference :additional_item, :decimal, :default => 0
|
4
|
+
preference :max_items, :decimal, :default => 0
|
5
|
+
|
6
|
+
# Register the calculator
|
7
|
+
def self.register
|
8
|
+
super
|
9
|
+
PaymentMethod.register_calculator(self)
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.description
|
13
|
+
I18n.t("flexible_rate")
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.available?(object)
|
17
|
+
true
|
18
|
+
end
|
19
|
+
|
20
|
+
def compute(object)
|
21
|
+
sum = 0
|
22
|
+
max = self.preferred_max_items
|
23
|
+
items_count = object.line_items.map(&:quantity).sum
|
24
|
+
items_count.times do |i|
|
25
|
+
if (i % max == 0) && (max > 0)
|
26
|
+
sum += self.preferred_first_item
|
27
|
+
else
|
28
|
+
sum += self.preferred_additional_item
|
29
|
+
end
|
30
|
+
end
|
31
|
+
return(sum)
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
class PaymentCalculator::PerItem < Calculator
|
2
|
+
preference :amount, :decimal, :default => 0
|
3
|
+
|
4
|
+
# Register the calculator
|
5
|
+
def self.register
|
6
|
+
super
|
7
|
+
PaymentMethod.register_calculator(self)
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.description
|
11
|
+
I18n.t("flat_rate_per_item")
|
12
|
+
end
|
13
|
+
|
14
|
+
def compute(object=nil)
|
15
|
+
self.preferred_amount * object.line_items.length
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
class PaymentCalculator::PriceSack < Calculator
|
2
|
+
preference :minimal_amount, :decimal, :default => 0
|
3
|
+
preference :normal_amount, :decimal, :default => 0
|
4
|
+
preference :discount_amount, :decimal, :default => 0
|
5
|
+
|
6
|
+
# Register the calculator
|
7
|
+
def self.register
|
8
|
+
super
|
9
|
+
PaymentMethod.register_calculator(self)
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.description
|
13
|
+
I18n.t("price_sack")
|
14
|
+
end
|
15
|
+
|
16
|
+
# as object we always get line items, as calculable we have Coupon, ShippingMethod
|
17
|
+
def compute(object)
|
18
|
+
if object.is_a?(Array)
|
19
|
+
base = object.map{ |o| o.respond_to?(:amount) ? o.amount : o.to_d }.sum
|
20
|
+
else
|
21
|
+
base = object.respond_to?(:amount) ? object.amount : object.to_d
|
22
|
+
end
|
23
|
+
|
24
|
+
if base >= self.preferred_minimal_amount
|
25
|
+
self.preferred_normal_amount
|
26
|
+
else
|
27
|
+
self.preferred_discount_amount
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
Payment.class_eval do
|
2
|
+
has_one :adjustment, :as => :source
|
3
|
+
before_save :remove_old_adjustment
|
4
|
+
after_save :ensure_correct_adjustment, :update_order, :if => Proc.new {|p| !p.payment_method.calculator.blank?}
|
5
|
+
|
6
|
+
private
|
7
|
+
def remove_old_adjustment
|
8
|
+
order.adjustments.each do |adjustment|
|
9
|
+
adjustment.destroy if adjustment.source_type == "Payment"
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def ensure_correct_adjustment
|
14
|
+
if adjustment
|
15
|
+
adjustment.originator = payment_method
|
16
|
+
adjustment.save
|
17
|
+
else
|
18
|
+
payment_method.create_adjustment(I18n.t(:payment_adjustment), order, self, true)
|
19
|
+
reload #ensure adjustment is present on later saves
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def update_order
|
24
|
+
order.update!
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,5 @@
|
|
1
|
+
Deface::Override.new(:virtual_path => 'admin/payment_methods/_form',
|
2
|
+
:name => 'add_calculators_to_payment_methods',
|
3
|
+
:insert_before => %q{[id='preference-settings']},
|
4
|
+
:text => %q{<% @calculators = PaymentMethod.calculators.sort_by(&:name) rescue nil %><%= render :partial => 'admin/shared/calculator_fields', :locals => {:f => f}},
|
5
|
+
:disabled => false)
|
data/config/routes.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
module SpreePaymentCalculator
|
2
|
+
module Generators
|
3
|
+
class InstallGenerator < Rails::Generators::Base
|
4
|
+
|
5
|
+
def add_javascripts
|
6
|
+
#append_file "app/assets/javascripts/store/all.js", "//= require store/spree_payment_calculator\n"
|
7
|
+
#append_file "app/assets/javascripts/admin/all.js", "//= require admin/spree_payment_calculator\n"
|
8
|
+
end
|
9
|
+
|
10
|
+
def add_stylesheets
|
11
|
+
#inject_into_file "app/assets/stylesheets/store/all.css", " *= require store/spree_payment_calculator\n", :before => /\*\//, :verbose => true
|
12
|
+
#inject_into_file "app/assets/stylesheets/admin/all.css", " *= require admin/spree_payment_calculator\n", :before => /\*\//, :verbose => true
|
13
|
+
end
|
14
|
+
|
15
|
+
def add_migrations
|
16
|
+
run 'bundle exec rake railties:install:migrations FROM=spree_payment_calculator'
|
17
|
+
end
|
18
|
+
|
19
|
+
def run_migrations
|
20
|
+
res = ask "Would you like to run the migrations now? [Y/n]"
|
21
|
+
if res == "" || res.downcase == "y"
|
22
|
+
run 'bundle exec rake db:migrate'
|
23
|
+
else
|
24
|
+
puts "Skiping rake db:migrate, don't forget to run it!"
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
module SpreePaymentCalculator
|
2
|
+
class Engine < Rails::Engine
|
3
|
+
engine_name 'spree_payment_calculator'
|
4
|
+
|
5
|
+
config.autoload_paths += %W(#{config.root}/lib)
|
6
|
+
|
7
|
+
# use rspec for tests
|
8
|
+
config.generators do |g|
|
9
|
+
g.test_framework :rspec
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.activate
|
13
|
+
Dir.glob(File.join(File.dirname(__FILE__), "../../app/**/*_decorator*.rb")) do |c|
|
14
|
+
Rails.configuration.cache_classes ? require(c) : load(c)
|
15
|
+
end
|
16
|
+
|
17
|
+
Dir.glob(File.join(File.dirname(__FILE__), "../../app/overrides/*.rb")) do |c|
|
18
|
+
Rails.application.config.cache_classes ? require(c) : load(c)
|
19
|
+
end
|
20
|
+
|
21
|
+
Dir.glob(File.join(File.dirname(__FILE__), "../../lib/spree/environment/calculator.rb")) do |c|
|
22
|
+
Rails.application.config.cache_classes ? require(c) : load(c)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
initializer 'spree.register.calculators' do |app|
|
26
|
+
app.config.spree.calculators.payment_methods = [
|
27
|
+
PaymentCalculator::PriceSack,
|
28
|
+
PaymentCalculator::FlatPercentItemTotal,
|
29
|
+
PaymentCalculator::FlatRate,
|
30
|
+
PaymentCalculator::FlexiRate,
|
31
|
+
PaymentCalculator::PerItem
|
32
|
+
]
|
33
|
+
end
|
34
|
+
|
35
|
+
config.to_prepare &method(:activate).to_proc
|
36
|
+
end
|
37
|
+
end
|
metadata
ADDED
@@ -0,0 +1,114 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: spree_payment_calculator
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.70.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Damiano Giacomello
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-06-20 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: spree_core
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 0.70.5
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 0.70.5
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: spree_auth
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: 0.70.5
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 0.70.5
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rspec-rails
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
description:
|
63
|
+
email: damiano.giacomello@diginess.it
|
64
|
+
executables: []
|
65
|
+
extensions: []
|
66
|
+
extra_rdoc_files: []
|
67
|
+
files:
|
68
|
+
- README.md
|
69
|
+
- LICENSE
|
70
|
+
- lib/generators/spree_payment_calculator/install/install_generator.rb
|
71
|
+
- lib/spree/environment/calculators.rb
|
72
|
+
- lib/spree_payment_calculator/engine.rb
|
73
|
+
- lib/spree_payment_calculator.rb
|
74
|
+
- app/assets/javascripts/admin/spree_payment_calculator.js
|
75
|
+
- app/assets/javascripts/store/spree_payment_calculator.js
|
76
|
+
- app/assets/stylesheets/admin/spree_payment_calculator.css
|
77
|
+
- app/assets/stylesheets/store/spree_payment_calculator.css
|
78
|
+
- app/model/payment_calculator/flat_percent_item_total.rb
|
79
|
+
- app/model/payment_calculator/flat_rate.rb
|
80
|
+
- app/model/payment_calculator/flexi_rate.rb
|
81
|
+
- app/model/payment_calculator/per_item.rb
|
82
|
+
- app/model/payment_calculator/price_sack.rb
|
83
|
+
- app/model/payment_decorator.rb
|
84
|
+
- app/model/payment_method_decorator.rb
|
85
|
+
- app/overrides/add_calculators_to_payment_methods.rb
|
86
|
+
- config/locales/en.yml
|
87
|
+
- config/locales/it.yml
|
88
|
+
- config/routes.rb
|
89
|
+
homepage:
|
90
|
+
licenses: []
|
91
|
+
post_install_message:
|
92
|
+
rdoc_options: []
|
93
|
+
require_paths:
|
94
|
+
- lib
|
95
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
96
|
+
none: false
|
97
|
+
requirements:
|
98
|
+
- - ! '>='
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: 1.8.7
|
101
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
102
|
+
none: false
|
103
|
+
requirements:
|
104
|
+
- - ! '>='
|
105
|
+
- !ruby/object:Gem::Version
|
106
|
+
version: '0'
|
107
|
+
requirements:
|
108
|
+
- none
|
109
|
+
rubyforge_project:
|
110
|
+
rubygems_version: 1.8.22
|
111
|
+
signing_key:
|
112
|
+
specification_version: 3
|
113
|
+
summary: Added a calculator for PaymentMethod for Spree
|
114
|
+
test_files: []
|