piggybak 0.2.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,339 @@
1
+ GNU GENERAL PUBLIC LICENSE
2
+ Version 2, June 1991
3
+
4
+ Copyright (C) 1989, 1991 Free Software Foundation, Inc.,
5
+ 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
6
+ Everyone is permitted to copy and distribute verbatim copies
7
+ of this license document, but changing it is not allowed.
8
+
9
+ Preamble
10
+
11
+ The licenses for most software are designed to take away your
12
+ freedom to share and change it. By contrast, the GNU General Public
13
+ License is intended to guarantee your freedom to share and change free
14
+ software--to make sure the software is free for all its users. This
15
+ General Public License applies to most of the Free Software
16
+ Foundation's software and to any other program whose authors commit to
17
+ using it. (Some other Free Software Foundation software is covered by
18
+ the GNU Lesser General Public License instead.) You can apply it to
19
+ your programs, too.
20
+
21
+ When we speak of free software, we are referring to freedom, not
22
+ price. Our General Public Licenses are designed to make sure that you
23
+ have the freedom to distribute copies of free software (and charge for
24
+ this service if you wish), that you receive source code or can get it
25
+ if you want it, that you can change the software or use pieces of it
26
+ in new free programs; and that you know you can do these things.
27
+
28
+ To protect your rights, we need to make restrictions that forbid
29
+ anyone to deny you these rights or to ask you to surrender the rights.
30
+ These restrictions translate to certain responsibilities for you if you
31
+ distribute copies of the software, or if you modify it.
32
+
33
+ For example, if you distribute copies of such a program, whether
34
+ gratis or for a fee, you must give the recipients all the rights that
35
+ you have. You must make sure that they, too, receive or can get the
36
+ source code. And you must show them these terms so they know their
37
+ rights.
38
+
39
+ We protect your rights with two steps: (1) copyright the software, and
40
+ (2) offer you this license which gives you legal permission to copy,
41
+ distribute and/or modify the software.
42
+
43
+ Also, for each author's protection and ours, we want to make certain
44
+ that everyone understands that there is no warranty for this free
45
+ software. If the software is modified by someone else and passed on, we
46
+ want its recipients to know that what they have is not the original, so
47
+ that any problems introduced by others will not reflect on the original
48
+ authors' reputations.
49
+
50
+ Finally, any free program is threatened constantly by software
51
+ patents. We wish to avoid the danger that redistributors of a free
52
+ program will individually obtain patent licenses, in effect making the
53
+ program proprietary. To prevent this, we have made it clear that any
54
+ patent must be licensed for everyone's free use or not licensed at all.
55
+
56
+ The precise terms and conditions for copying, distribution and
57
+ modification follow.
58
+
59
+ GNU GENERAL PUBLIC LICENSE
60
+ TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
61
+
62
+ 0. This License applies to any program or other work which contains
63
+ a notice placed by the copyright holder saying it may be distributed
64
+ under the terms of this General Public License. The "Program", below,
65
+ refers to any such program or work, and a "work based on the Program"
66
+ means either the Program or any derivative work under copyright law:
67
+ that is to say, a work containing the Program or a portion of it,
68
+ either verbatim or with modifications and/or translated into another
69
+ language. (Hereinafter, translation is included without limitation in
70
+ the term "modification".) Each licensee is addressed as "you".
71
+
72
+ Activities other than copying, distribution and modification are not
73
+ covered by this License; they are outside its scope. The act of
74
+ running the Program is not restricted, and the output from the Program
75
+ is covered only if its contents constitute a work based on the
76
+ Program (independent of having been made by running the Program).
77
+ Whether that is true depends on what the Program does.
78
+
79
+ 1. You may copy and distribute verbatim copies of the Program's
80
+ source code as you receive it, in any medium, provided that you
81
+ conspicuously and appropriately publish on each copy an appropriate
82
+ copyright notice and disclaimer of warranty; keep intact all the
83
+ notices that refer to this License and to the absence of any warranty;
84
+ and give any other recipients of the Program a copy of this License
85
+ along with the Program.
86
+
87
+ You may charge a fee for the physical act of transferring a copy, and
88
+ you may at your option offer warranty protection in exchange for a fee.
89
+
90
+ 2. You may modify your copy or copies of the Program or any portion
91
+ of it, thus forming a work based on the Program, and copy and
92
+ distribute such modifications or work under the terms of Section 1
93
+ above, provided that you also meet all of these conditions:
94
+
95
+ a) You must cause the modified files to carry prominent notices
96
+ stating that you changed the files and the date of any change.
97
+
98
+ b) You must cause any work that you distribute or publish, that in
99
+ whole or in part contains or is derived from the Program or any
100
+ part thereof, to be licensed as a whole at no charge to all third
101
+ parties under the terms of this License.
102
+
103
+ c) If the modified program normally reads commands interactively
104
+ when run, you must cause it, when started running for such
105
+ interactive use in the most ordinary way, to print or display an
106
+ announcement including an appropriate copyright notice and a
107
+ notice that there is no warranty (or else, saying that you provide
108
+ a warranty) and that users may redistribute the program under
109
+ these conditions, and telling the user how to view a copy of this
110
+ License. (Exception: if the Program itself is interactive but
111
+ does not normally print such an announcement, your work based on
112
+ the Program is not required to print an announcement.)
113
+
114
+ These requirements apply to the modified work as a whole. If
115
+ identifiable sections of that work are not derived from the Program,
116
+ and can be reasonably considered independent and separate works in
117
+ themselves, then this License, and its terms, do not apply to those
118
+ sections when you distribute them as separate works. But when you
119
+ distribute the same sections as part of a whole which is a work based
120
+ on the Program, the distribution of the whole must be on the terms of
121
+ this License, whose permissions for other licensees extend to the
122
+ entire whole, and thus to each and every part regardless of who wrote it.
123
+
124
+ Thus, it is not the intent of this section to claim rights or contest
125
+ your rights to work written entirely by you; rather, the intent is to
126
+ exercise the right to control the distribution of derivative or
127
+ collective works based on the Program.
128
+
129
+ In addition, mere aggregation of another work not based on the Program
130
+ with the Program (or with a work based on the Program) on a volume of
131
+ a storage or distribution medium does not bring the other work under
132
+ the scope of this License.
133
+
134
+ 3. You may copy and distribute the Program (or a work based on it,
135
+ under Section 2) in object code or executable form under the terms of
136
+ Sections 1 and 2 above provided that you also do one of the following:
137
+
138
+ a) Accompany it with the complete corresponding machine-readable
139
+ source code, which must be distributed under the terms of Sections
140
+ 1 and 2 above on a medium customarily used for software interchange; or,
141
+
142
+ b) Accompany it with a written offer, valid for at least three
143
+ years, to give any third party, for a charge no more than your
144
+ cost of physically performing source distribution, a complete
145
+ machine-readable copy of the corresponding source code, to be
146
+ distributed under the terms of Sections 1 and 2 above on a medium
147
+ customarily used for software interchange; or,
148
+
149
+ c) Accompany it with the information you received as to the offer
150
+ to distribute corresponding source code. (This alternative is
151
+ allowed only for noncommercial distribution and only if you
152
+ received the program in object code or executable form with such
153
+ an offer, in accord with Subsection b above.)
154
+
155
+ The source code for a work means the preferred form of the work for
156
+ making modifications to it. For an executable work, complete source
157
+ code means all the source code for all modules it contains, plus any
158
+ associated interface definition files, plus the scripts used to
159
+ control compilation and installation of the executable. However, as a
160
+ special exception, the source code distributed need not include
161
+ anything that is normally distributed (in either source or binary
162
+ form) with the major components (compiler, kernel, and so on) of the
163
+ operating system on which the executable runs, unless that component
164
+ itself accompanies the executable.
165
+
166
+ If distribution of executable or object code is made by offering
167
+ access to copy from a designated place, then offering equivalent
168
+ access to copy the source code from the same place counts as
169
+ distribution of the source code, even though third parties are not
170
+ compelled to copy the source along with the object code.
171
+
172
+ 4. You may not copy, modify, sublicense, or distribute the Program
173
+ except as expressly provided under this License. Any attempt
174
+ otherwise to copy, modify, sublicense or distribute the Program is
175
+ void, and will automatically terminate your rights under this License.
176
+ However, parties who have received copies, or rights, from you under
177
+ this License will not have their licenses terminated so long as such
178
+ parties remain in full compliance.
179
+
180
+ 5. You are not required to accept this License, since you have not
181
+ signed it. However, nothing else grants you permission to modify or
182
+ distribute the Program or its derivative works. These actions are
183
+ prohibited by law if you do not accept this License. Therefore, by
184
+ modifying or distributing the Program (or any work based on the
185
+ Program), you indicate your acceptance of this License to do so, and
186
+ all its terms and conditions for copying, distributing or modifying
187
+ the Program or works based on it.
188
+
189
+ 6. Each time you redistribute the Program (or any work based on the
190
+ Program), the recipient automatically receives a license from the
191
+ original licensor to copy, distribute or modify the Program subject to
192
+ these terms and conditions. You may not impose any further
193
+ restrictions on the recipients' exercise of the rights granted herein.
194
+ You are not responsible for enforcing compliance by third parties to
195
+ this License.
196
+
197
+ 7. If, as a consequence of a court judgment or allegation of patent
198
+ infringement or for any other reason (not limited to patent issues),
199
+ conditions are imposed on you (whether by court order, agreement or
200
+ otherwise) that contradict the conditions of this License, they do not
201
+ excuse you from the conditions of this License. If you cannot
202
+ distribute so as to satisfy simultaneously your obligations under this
203
+ License and any other pertinent obligations, then as a consequence you
204
+ may not distribute the Program at all. For example, if a patent
205
+ license would not permit royalty-free redistribution of the Program by
206
+ all those who receive copies directly or indirectly through you, then
207
+ the only way you could satisfy both it and this License would be to
208
+ refrain entirely from distribution of the Program.
209
+
210
+ If any portion of this section is held invalid or unenforceable under
211
+ any particular circumstance, the balance of the section is intended to
212
+ apply and the section as a whole is intended to apply in other
213
+ circumstances.
214
+
215
+ It is not the purpose of this section to induce you to infringe any
216
+ patents or other property right claims or to contest validity of any
217
+ such claims; this section has the sole purpose of protecting the
218
+ integrity of the free software distribution system, which is
219
+ implemented by public license practices. Many people have made
220
+ generous contributions to the wide range of software distributed
221
+ through that system in reliance on consistent application of that
222
+ system; it is up to the author/donor to decide if he or she is willing
223
+ to distribute software through any other system and a licensee cannot
224
+ impose that choice.
225
+
226
+ This section is intended to make thoroughly clear what is believed to
227
+ be a consequence of the rest of this License.
228
+
229
+ 8. If the distribution and/or use of the Program is restricted in
230
+ certain countries either by patents or by copyrighted interfaces, the
231
+ original copyright holder who places the Program under this License
232
+ may add an explicit geographical distribution limitation excluding
233
+ those countries, so that distribution is permitted only in or among
234
+ countries not thus excluded. In such case, this License incorporates
235
+ the limitation as if written in the body of this License.
236
+
237
+ 9. The Free Software Foundation may publish revised and/or new versions
238
+ of the General Public License from time to time. Such new versions will
239
+ be similar in spirit to the present version, but may differ in detail to
240
+ address new problems or concerns.
241
+
242
+ Each version is given a distinguishing version number. If the Program
243
+ specifies a version number of this License which applies to it and "any
244
+ later version", you have the option of following the terms and conditions
245
+ either of that version or of any later version published by the Free
246
+ Software Foundation. If the Program does not specify a version number of
247
+ this License, you may choose any version ever published by the Free Software
248
+ Foundation.
249
+
250
+ 10. If you wish to incorporate parts of the Program into other free
251
+ programs whose distribution conditions are different, write to the author
252
+ to ask for permission. For software which is copyrighted by the Free
253
+ Software Foundation, write to the Free Software Foundation; we sometimes
254
+ make exceptions for this. Our decision will be guided by the two goals
255
+ of preserving the free status of all derivatives of our free software and
256
+ of promoting the sharing and reuse of software generally.
257
+
258
+ NO WARRANTY
259
+
260
+ 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
261
+ FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
262
+ OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
263
+ PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
264
+ OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
265
+ MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
266
+ TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
267
+ PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
268
+ REPAIR OR CORRECTION.
269
+
270
+ 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
271
+ WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
272
+ REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
273
+ INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
274
+ OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
275
+ TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
276
+ YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
277
+ PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
278
+ POSSIBILITY OF SUCH DAMAGES.
279
+
280
+ END OF TERMS AND CONDITIONS
281
+
282
+ How to Apply These Terms to Your New Programs
283
+
284
+ If you develop a new program, and you want it to be of the greatest
285
+ possible use to the public, the best way to achieve this is to make it
286
+ free software which everyone can redistribute and change under these terms.
287
+
288
+ To do so, attach the following notices to the program. It is safest
289
+ to attach them to the start of each source file to most effectively
290
+ convey the exclusion of warranty; and each file should have at least
291
+ the "copyright" line and a pointer to where the full notice is found.
292
+
293
+ <one line to give the program's name and a brief idea of what it does.>
294
+ Copyright (C) <year> <name of author>
295
+
296
+ This program is free software; you can redistribute it and/or modify
297
+ it under the terms of the GNU General Public License as published by
298
+ the Free Software Foundation; either version 2 of the License, or
299
+ (at your option) any later version.
300
+
301
+ This program is distributed in the hope that it will be useful,
302
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
303
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
304
+ GNU General Public License for more details.
305
+
306
+ You should have received a copy of the GNU General Public License along
307
+ with this program; if not, write to the Free Software Foundation, Inc.,
308
+ 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
309
+
310
+ Also add information on how to contact you by electronic and paper mail.
311
+
312
+ If the program is interactive, make it output a short notice like this
313
+ when it starts in an interactive mode:
314
+
315
+ Gnomovision version 69, Copyright (C) year name of author
316
+ Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
317
+ This is free software, and you are welcome to redistribute it
318
+ under certain conditions; type `show c' for details.
319
+
320
+ The hypothetical commands `show w' and `show c' should show the appropriate
321
+ parts of the General Public License. Of course, the commands you use may
322
+ be called something other than `show w' and `show c'; they could even be
323
+ mouse-clicks or menu items--whatever suits your program.
324
+
325
+ You should also get your employer (if you work as a programmer) or your
326
+ school, if any, to sign a "copyright disclaimer" for the program, if
327
+ necessary. Here is a sample; alter the names:
328
+
329
+ Yoyodyne, Inc., hereby disclaims all copyright interest in the program
330
+ `Gnomovision' (which makes passes at compilers) written by James Hacker.
331
+
332
+ <signature of Ty Coon>, 1 April 1989
333
+ Ty Coon, President of Vice
334
+
335
+ This General Public License does not permit incorporating your program into
336
+ proprietary programs. If your program is a subroutine library, you may
337
+ consider it more useful to permit linking proprietary applications with the
338
+ library. If this is what you want to do, use the GNU Lesser General
339
+ Public License instead of this License.
data/README.md CHANGED
@@ -102,12 +102,10 @@ Recipes
102
102
  TODOs
103
103
  ========
104
104
 
105
- * Bug: Handle state options in admin: selected state or free text
106
- * Feature: Figure out how to make entire payments section read only, except for ability to refund
107
- * Feature: Add refunds: Add actionable link under payments, include on payments page only
108
- * Feature: Create rake task for copying over views, and make sure app views will override gems
109
105
  * Test: Test email send functionality
110
106
  * Test: Add unit testing
107
+ * Feature: Add API to call Refund if profiles only
108
+ * Feature: Add helper for Google Analytics order tagging
111
109
 
112
110
  Copyright
113
111
  ========
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.1
1
+ 0.3.0
@@ -1,58 +1,17 @@
1
1
  var tax_total = 0;
2
- var geodata;
3
2
 
4
3
  $(function() {
5
- piggybak.populate_geodata();
6
4
  piggybak.initialize_listeners();
7
5
  piggybak.update_shipping_options($('#piggybak_order_shipping_address_attributes_state_id'));
8
6
  piggybak.update_tax();
9
7
  });
10
8
 
11
9
  var piggybak = {
12
- populate_geodata: function() {
13
- $.ajax({
14
- url: geodata_lookup,
15
- cached: false,
16
- dataType: "JSON",
17
- success: function(data) {
18
- geodata = data;
19
- }
20
- });
21
- },
22
- update_state_option: function(type, block) {
23
- var country_field = $('#piggybak_order_' + type + '_address_attributes_country_id');
24
- var country_id = country_field.val();
25
- var new_field;
26
-
27
- if(geodata.countries["country_" + country_id].length > 0) {
28
- new_field = $('<select>');
29
- $.each(geodata.countries["country_" + country_id], function(i, j) {
30
- new_field.append($('<option>').val(j.id).html(j.name));
31
- });
32
- } else {
33
- new_field = $('<input>');
34
- }
35
- var old_field = $('#piggybak_order_' + type + '_address_attributes_state_id');
36
- new_field.attr('name', old_field.attr('name'));
37
- new_field.attr('id', old_field.attr('id'));
38
- old_field.replaceWith(new_field);
39
-
40
- if(block) {
41
- block();
42
- }
43
- return;
44
- },
45
10
  initialize_listeners: function() {
46
- $('#piggybak_order_shipping_address_attributes_country_id').change(function() {
47
- piggybak.update_state_option('shipping');
48
- });
49
- $('#piggybak_order_billing_address_attributes_country_id').change(function() {
50
- piggybak.update_state_option('billing');
51
- });
52
- $('#piggybak_order_shipping_address_attributes_state_id').change(function() {
11
+ $('#piggybak_order_shipping_address_attributes_state_id').live('click', function() {
53
12
  piggybak.update_shipping_options($(this));
54
13
  });
55
- $('#piggybak_order_billing_address_attributes_state_id').change(function() {
14
+ $('#piggybak_order_billing_address_attributes_state_id').live('change', function() {
56
15
  piggybak.update_tax();
57
16
  });
58
17
  $('#shipping select').change(function() {
@@ -71,7 +30,7 @@ var piggybak = {
71
30
  });
72
31
  var country = $('#piggybak_order_billing_address_attributes_country_id').val();
73
32
  $('#piggybak_order_shipping_address_attributes_country_id').val(country);
74
- piggybak.update_state_option('shipping', function() {
33
+ piggybak_states.update_state_option('shipping', function() {
75
34
  var state = $('#piggybak_order_billing_address_attributes_state_id').val();
76
35
  $('#piggybak_order_shipping_address_attributes_state_id').val(state);
77
36
  });
@@ -0,0 +1,52 @@
1
+ var geodata;
2
+
3
+ $(function() {
4
+ piggybak_states.populate_geodata();
5
+ piggybak_states.initialize_listeners();
6
+ });
7
+
8
+ var piggybak_states = {
9
+ initialize_listeners: function() {
10
+ $('#piggybak_order_shipping_address_attributes_country_id').change(function() {
11
+ piggybak_states.update_state_option('shipping');
12
+ });
13
+ $('#piggybak_order_billing_address_attributes_country_id').change(function() {
14
+ piggybak_states.update_state_option('billing');
15
+ });
16
+ return;
17
+ },
18
+ populate_geodata: function() {
19
+ $.ajax({
20
+ url: geodata_lookup,
21
+ cached: false,
22
+ dataType: "JSON",
23
+ success: function(data) {
24
+ geodata = data;
25
+ piggybak_states.update_state_option('shipping');
26
+ piggybak_states.update_state_option('billing');
27
+ }
28
+ });
29
+ },
30
+ update_state_option: function(type, block) {
31
+ var country_field = $('#piggybak_order_' + type + '_address_attributes_country_id');
32
+ var country_id = country_field.val();
33
+ var new_field;
34
+
35
+ if(geodata.countries["country_" + country_id].length > 0) {
36
+ new_field = $('<select>');
37
+ $.each(geodata.countries["country_" + country_id], function(i, j) {
38
+ new_field.append($('<option>').val(j.id).html(j.name));
39
+ });
40
+ } else {
41
+ new_field = $('<input>');
42
+ }
43
+ var old_field = $('#piggybak_order_' + type + '_address_attributes_state_id');
44
+ new_field.attr('name', old_field.attr('name')).attr('id', old_field.attr('id')).val(old_field.val());
45
+ old_field.replaceWith(new_field);
46
+
47
+ if(block) {
48
+ block();
49
+ }
50
+ return;
51
+ }
52
+ };
@@ -86,6 +86,20 @@ module Piggybak
86
86
  redirect_to rails_admin.edit_path('Piggybak::Order', order.id)
87
87
  end
88
88
 
89
+ def cancel
90
+ order = Order.find(params[:id])
91
+
92
+ if can?(:cancel, order)
93
+ order.payments.each do |payment|
94
+ payment.refund
95
+ end
96
+ order.update_attribute(:status, "cancelled")
97
+ flash[:notice] = "Cancelled"
98
+ end
99
+
100
+ redirect_to rails_admin.edit_path('Piggybak::Order', order.id)
101
+ end
102
+
89
103
  # AJAX Actions from checkout
90
104
  def shipping
91
105
  cart = Piggybak::Cart.new(request.cookies["cart"])
@@ -0,0 +1,13 @@
1
+ module Piggybak
2
+ class PaymentsController < ApplicationController
3
+ def refund
4
+ payment = Payment.find(params[:id])
5
+
6
+ if can?(:refund, payment)
7
+ flash[:notice] = payment.refund
8
+ end
9
+
10
+ redirect_to rails_admin.edit_path('Piggybak::Order', payment.order.id)
11
+ end
12
+ end
13
+ end
@@ -14,13 +14,9 @@ module Piggybak
14
14
  after_initialize :set_default_country
15
15
 
16
16
  def set_default_country
17
- self.country ||= Address.DEFAULT_COUNTRY
17
+ self.country ||= Country.find_by_abbr(Piggybak.config.default_country)
18
18
  end
19
19
 
20
- def self.DEFAULT_COUNTRY
21
- Country.find_by_abbr("US")
22
- end
23
-
24
20
  def admin_label
25
21
  address = "#{self.firstname} #{self.lastname}<br />"
26
22
  address += "#{self.address1}<br />"
@@ -1,5 +1,8 @@
1
1
  module Piggybak
2
2
  class Country < ActiveRecord::Base
3
3
  has_many :states
4
+
5
+ scope :shipping, where(:active_shipping => true)
6
+ scope :billing, where(:active_billing => true)
4
7
  end
5
8
  end
@@ -36,25 +36,13 @@ module Piggybak
36
36
  end
37
37
 
38
38
  self.total_due = self.total
39
- payments.each do |payment|
40
- self.total_due -= payment.total
41
- end
42
- !has_errors
43
- end
39
+ payments_total = payments.inject(0) do |payments_total, payment|
40
+ payments_total += payment.total if payment.status == "paid"
41
+ payments_total
42
+ end
43
+ self.total_due -= payments_total
44
44
 
45
- def details
46
- if !self.new_record?
47
- subtotal = self.line_items.inject(0) { |subtotal, li| subtotal + li.total }
48
- shipping = self.shipments.inject(0) { |shipping, shipment| shipping + shipment.total }
49
- return "Status: #{self.status}<br />" +
50
- "Subtotal: $#{"%.2f" % subtotal}<br />" +
51
- "Shipping: $#{"%.2f" % shipping}<br />" +
52
- "Tax: $#{"%.2f" % self.tax_charge}<br />" +
53
- "Due: $#{"%.2f" % self.total_due}<br />" +
54
- "Created at: #{self.created_at.strftime("%m-%d-%Y")}<br />" #details here"
55
- else
56
- return "New Order"
57
- end
45
+ !has_errors
58
46
  end
59
47
 
60
48
  def add_line_items(cart)
@@ -106,11 +94,15 @@ module Piggybak
106
94
 
107
95
  self.total_due = self.total
108
96
  payments.each do |payment|
109
- self.total_due -= payment.total
97
+ if payment.status == "paid"
98
+ self.total_due -= payment.total
99
+ end
110
100
  end
111
101
  end
112
102
 
113
103
  def update_status
104
+ return if self.status == "cancelled" # do nothing
105
+
114
106
  if self.total_due > 0.00
115
107
  self.status = "payment owed"
116
108
  elsif self.total_due < 0.00
@@ -31,6 +31,8 @@ module Piggybak
31
31
  end
32
32
 
33
33
  def process
34
+ ActiveMerchant::Billing::Base.mode = Piggybak.config.activemerchant_mode
35
+
34
36
  if self.new_record?
35
37
  payment_gateway = self.payment_method.klass.constantize
36
38
  gateway = payment_gateway::KLASS.new(self.payment_method.key_values)
@@ -52,6 +54,31 @@ module Piggybak
52
54
  end
53
55
  end
54
56
 
57
+ # TODO: Add refund support at some point
58
+ # Note: It is not added now, because for methods that do not store
59
+ # user profiles, a credit card number must be passed
60
+ # If encrypted credit cards are stored on the system,
61
+ # this can be updated
62
+ def refund
63
+ =begin
64
+ # ActiveMerchant::Billing::Base.mode = Piggybak.config.activemerchant_mode
65
+
66
+ begin
67
+ payment_gateway = self.payment_method.klass.constantize
68
+ gateway = payment_gateway::KLASS.new(self.payment_method.key_values)
69
+
70
+ b = gateway.refund(self.total*100, self.transaction_id, { :card_number => "4111111111111111" })
71
+ rescue Exception => e
72
+ return e.inspect
73
+ end
74
+ =end
75
+
76
+ self.update_attribute(:status, "refunded")
77
+ self.order.update_attribute(:total_due, self.order.total_due + self.total)
78
+
79
+ return "Marked as Refunded"
80
+ end
81
+
55
82
  def admin_label
56
83
  if !self.new_record?
57
84
  return "Payment ##{self.id} (#{self.created_at.strftime("%m-%d-%Y")})<br />" +
@@ -1,16 +1,18 @@
1
1
  module Piggybak
2
2
  class TaxCalculator::Percent
3
- KEYS = ["state_abbr", "rate"]
3
+ KEYS = ["state_id", "rate"]
4
4
 
5
5
  def self.available?(method, object)
6
- abbr = method.metadata.detect { |t| t.key == "state_abbr" }.value
6
+ id = method.metadata.detect { |t| t.key == "state_id" }.value
7
7
 
8
8
  if object.is_a?(Cart)
9
- state = State.find(object.extra_data["state_id"])
10
- return state.abbr == abbr
9
+ if object.extra_data["state_id"] != ""
10
+ state = State.find(object.extra_data["state_id"])
11
+ return state.id == id.to_i if state
12
+ end
11
13
  else
12
14
  if object.billing_address && object.billing_address.state
13
- return object.billing_address.state.abbr == abbr
15
+ return object.billing_address.state.id == id.to_i
14
16
  end
15
17
  end
16
18
  return false
@@ -20,11 +20,15 @@
20
20
  </div>
21
21
  <div class="item">
22
22
  <%= address.label :country_id %>
23
- <%= address.collection_select :country_id, Piggybak::Country.all, :id, :name, { :selected => address.object.country_id.to_s } %>
23
+ <%= address.collection_select :country_id, Piggybak::Country.send(type), :id, :name, { :selected => address.object.country_id.to_s } %>
24
24
  </div>
25
25
  <div class="item">
26
26
  <%= address.label :state_id %>
27
+ <% if address.object.country.states.any? -%>
27
28
  <%= address.collection_select :state_id, address.object.country.states, :id, :name, { :selected => address.object.state_id.to_s } %>
29
+ <% else -%>
30
+ <%= address.text_field :state_id %>
31
+ <% end -%>
28
32
  </div>
29
33
  <div class="item">
30
34
  <%= address.label :zip %>
@@ -33,7 +33,7 @@
33
33
  <div id="billing_address">
34
34
  <h3>Billing Address</h3>
35
35
  <%= f.fields_for :billing_address do |billing_address| %>
36
- <%= render "address_form", :address => billing_address %>
36
+ <%= render "address_form", :address => billing_address, :type => "billing" %>
37
37
  <% end -%>
38
38
  </div>
39
39
  <div id="shipping_address">
@@ -43,7 +43,7 @@
43
43
  </h3>
44
44
 
45
45
  <%= f.fields_for :shipping_address do |shipping_address| %>
46
- <%= render "address_form", :address => shipping_address %>
46
+ <%= render "address_form", :address => shipping_address, :type => "shipping" %>
47
47
  <% end -%>
48
48
  </div>
49
49
 
@@ -109,7 +109,7 @@
109
109
 
110
110
  </div>
111
111
 
112
- <%= javascript_include_tag "piggybak" %>
112
+ <%= javascript_include_tag "piggybak", "piggybak.states" %>
113
113
  <script type="text/javascript">
114
114
  var shipping_lookup = "<%= piggybak.orders_shipping_url %>";
115
115
  var tax_lookup = "<%= piggybak.orders_tax_url %>";
@@ -0,0 +1,7 @@
1
+ = form.fields_for field.name do |nested_form|
2
+ %blockquote
3
+ - if nested_form.object.new_record?
4
+ = nested_form.link_to_remove "cancel", :class => 'label notice'
5
+ = nested_form.generate({:action => :create, :model_config => field.associated_model_config, :nested_in => field.name })
6
+ %hr
7
+ = form.link_to_add t("admin.index.add_new"), field.name, :class => 'label notice'
@@ -0,0 +1,15 @@
1
+ :ruby
2
+ type = form.jquery_namespace(field).match("billing") ? "billing" : "shipping"
3
+
4
+ = form.collection_select "country_id", Piggybak::Country.send(type), :id, :name, { :selected => form.object.country_id.to_s }
5
+
6
+ - if form.object.country.states.any?
7
+ = form.collection_select "state_id", form.object.country.states, :id, :name, { :selected => form.object.state_id.to_s }
8
+ - else
9
+ = form.text_field "state_id"
10
+
11
+ - if type == "billing"
12
+ = javascript_include_tag "piggybak.states"
13
+
14
+ :javascript
15
+ var geodata_lookup = "#{piggybak.orders_geodata_url}";
@@ -0,0 +1,38 @@
1
+ :ruby
2
+ config = field.associated_model_config
3
+ related_id = params[:associations] && params[:associations][field.name.to_s]
4
+ source_abstract_model = RailsAdmin.config(form.object.class).abstract_model
5
+
6
+ if form.object.new_record? && related_id.present? && related_id != 'new'
7
+ selected = config.abstract_model.get(related_id)
8
+ selected_id = selected.id
9
+ selected_name = selected.send(config.object_label_method)
10
+ else
11
+ selected_id = field.selected_id
12
+ selected_name = field.formatted_value
13
+ end
14
+
15
+ current_action = params[:action].in?(['create', 'new']) ? 'create' : 'update'
16
+
17
+ edit_url = authorized?(:edit, config.abstract_model) ? edit_path(:model_name => config.abstract_model.to_param, :modal => true, :id => '__ID__') : ''
18
+
19
+ xhr = !field.associated_collection_cache_all
20
+
21
+ collection = xhr ? [[selected_name, selected_id]] : controller.list_entries(config, :index, field.associated_collection_scope, false).map { |o| [o.send(config.object_label_method), o.id] }
22
+
23
+ = form.select field.method_name, collection, { :selected => selected_id, :include_blank => true }, field._html_attributes.reverse_merge({:placeholder => t('admin.index.search')})
24
+
25
+ = form.javascript_for(field) do
26
+ :plain
27
+ $("#{form.jquery_namespace(field)} select").filteringSelect({
28
+ xhr: #{xhr},
29
+ createQuery: function(query) {
30
+ return { query: query }
31
+ },
32
+ remote_source: "#{index_path(config.abstract_model.to_param, :source_object_id => form.object.id, :source_abstract_model => source_abstract_model.to_param, :associated_collection => field.name, :current_action => current_action, :compact => true).html_safe}"
33
+ });
34
+ if($("#modal").length) { // hide link if we already are inside a dialog (endless issues on nested dialogs with JS)
35
+ $("#{form.jquery_namespace(field)} .btn").hide();
36
+ } else {
37
+ $("#{form.jquery_namespace(field)}").remoteForm();
38
+ }
@@ -0,0 +1,44 @@
1
+ <% if form.object.new_record? %>
2
+ N/A
3
+ <% else -%>
4
+ <table cellpadding="0" cellspacing="0">
5
+ <tr>
6
+ <td>Status</td>
7
+ <td><%= form.object.status %></td>
8
+ </tr>
9
+ <tr>
10
+ <td>Subtotal</td>
11
+ <td>$<%= "%.2f" % form.object.line_items.inject(0) { |subtotal, li| subtotal + li.total } %></td>
12
+ </tr>
13
+ <tr>
14
+ <td>Shipping</td>
15
+ <td>$<%= "%.2f" % form.object.shipments.inject(0) { |shipping, shipment| shipping + shipment.total } %></td>
16
+ </tr>
17
+ <tr>
18
+ <td>Tax</td>
19
+ <td>$<%= "%.2f" % form.object.tax_charge %></td>
20
+ </tr>
21
+ <tr>
22
+ <td>Order Total</td>
23
+ <td>$<%= "%.2f" % form.object.total %></td>
24
+ </tr>
25
+ <tr>
26
+ <td>Total Due</td>
27
+ <td>$<%= "%.2f" % form.object.total_due %></td>
28
+ </tr>
29
+ <tr>
30
+ <td>Created At</td>
31
+ <td><%= form.object.created_at.strftime("%m-%d-%Y") %></td>
32
+ </tr>
33
+ <tr>
34
+ <td>Actions</td>
35
+ <td>
36
+ <ul>
37
+ <li><%= link_to "Send Email Confirmation", piggybak.email_order_url(form.object.id) %></li>
38
+ <li><%= link_to "View as Text", piggybak.download_order_url(form.object.id) %></li>
39
+ <li><%= link_to "Cancel Order", piggybak.cancel_order_url(form.object.id) %> (marks payments as refunded, marks order as cancelled)</li>
40
+ </ul>
41
+ </td>
42
+ </tr>
43
+ </table>
44
+ <% end -%>
@@ -0,0 +1,6 @@
1
+ - if form.object.new_record?
2
+ N/A
3
+ -elsif form.object.status == "refunded"
4
+ Payment Marked as Refunded
5
+ - else
6
+ = link_to "Mark as Refunded", piggybak.refund_payment_url(form.object.id)
data/config/routes.rb CHANGED
@@ -18,7 +18,9 @@ Piggybak::Engine.routes.draw do
18
18
  match "/orders" => "orders#list", :as => :orders_list
19
19
 
20
20
  # admin actions
21
- match "/admin/orders/:id/email" => "orders#email", :as => :orders_email
22
- match "/admin/orders/:id/download" => "orders#download", :as => :orders_download, :format => "txt"
21
+ match "/admin/orders/:id/email" => "orders#email", :as => :email_order
22
+ match "/admin/orders/:id/download" => "orders#download", :as => :download_order, :format => "txt"
23
+ match "/admin/orders/:id/cancel" => "orders#cancel", :as => :cancel_order
24
+ match "/admin/payments/:id/refund" => "payments#refund", :as => :refund_payment
23
25
  end
24
26
  end
@@ -0,0 +1,11 @@
1
+ class AddActiveToCountries < ActiveRecord::Migration
2
+ def up
3
+ add_column :countries, :active_shipping, :boolean, :default => false
4
+ add_column :countries, :active_billing, :boolean, :default => false
5
+ end
6
+
7
+ def down
8
+ remove_column :countries, :active_shipping
9
+ remove_column :countries, :active_billing
10
+ end
11
+ end
data/lib/piggybak.rb CHANGED
@@ -79,14 +79,11 @@ module Piggybak
79
79
  end
80
80
  edit do
81
81
  field :details do
82
- read_only true
83
- help "Autopopulated"
84
- end
85
- field :actions do
86
- partial "actions"
82
+ partial "order_details"
87
83
  help ""
88
84
  end
89
85
  field :user do
86
+ partial "no_edit_form_filtering_select"
90
87
  read_only do
91
88
  !bindings[:object].new_record?
92
89
  end
@@ -101,7 +98,9 @@ module Piggybak
101
98
  end
102
99
  field :line_items
103
100
  field :shipments
104
- field :payments
101
+ field :payments do
102
+ partial "form_nested_no_destroy"
103
+ end
105
104
  end
106
105
  end
107
106
 
@@ -110,6 +109,20 @@ module Piggybak
110
109
  parent Piggybak::Order
111
110
  object_label_method :admin_label
112
111
  visible false
112
+
113
+ edit do
114
+ field :firstname
115
+ field :lastname
116
+ field :address1
117
+ field :address2
118
+ field :city
119
+ field :zip
120
+ field :location do
121
+ partial "location_select"
122
+ help "Required"
123
+ label "Country & State"
124
+ end
125
+ end
113
126
  end
114
127
 
115
128
  config.model Piggybak::LineItem do
@@ -118,7 +131,9 @@ module Piggybak
118
131
  visible false
119
132
 
120
133
  edit do
121
- field :variant
134
+ field :variant do
135
+ partial "no_edit_form_filtering_select"
136
+ end
122
137
  field :quantity
123
138
  field :total do
124
139
  read_only true
@@ -136,7 +151,9 @@ module Piggybak
136
151
  visible false
137
152
 
138
153
  edit do
139
- field :shipping_method
154
+ field :shipping_method do
155
+ partial "no_edit_form_filtering_select"
156
+ end
140
157
  field :status
141
158
  field :total do
142
159
  read_only true
@@ -154,11 +171,8 @@ module Piggybak
154
171
  visible false
155
172
 
156
173
  edit do
157
- #field :details do
158
- # read_only true
159
- # help "Autopopulated"
160
- #end
161
174
  field :payment_method do
175
+ partial "no_edit_form_filtering_select"
162
176
  read_only do
163
177
  !bindings[:object].new_record?
164
178
  end
@@ -169,11 +183,13 @@ module Piggybak
169
183
  end
170
184
  end
171
185
  field :month do
186
+ label "Exp. Month"
172
187
  read_only do
173
188
  !bindings[:object].new_record?
174
189
  end
175
190
  end
176
191
  field :year do
192
+ label "Exp. Year"
177
193
  read_only do
178
194
  !bindings[:object].new_record?
179
195
  end
@@ -190,6 +206,10 @@ module Piggybak
190
206
  end
191
207
  help "This will automatically be calculated at the time of processing."
192
208
  end
209
+ field :actions do
210
+ partial "payment_refund"
211
+ help "Admin driven refunds are not supported because credit cards are not stored."
212
+ end
193
213
  end
194
214
  end
195
215
 
@@ -299,6 +319,8 @@ module Piggybak
299
319
  edit do
300
320
  field :name
301
321
  field :abbr
322
+ field :active_shipping
323
+ field :active_billing
302
324
  end
303
325
  end
304
326
 
@@ -4,6 +4,8 @@ module Piggybak
4
4
  attr_accessor :payment_calculators
5
5
  attr_accessor :shipping_calculators
6
6
  attr_accessor :tax_calculators
7
+ attr_accessor :default_country
8
+ attr_accessor :activemerchant_mode
7
9
 
8
10
  def reset
9
11
  @payment_calculators = ["::Piggybak::PaymentCalculator::Fake",
@@ -12,6 +14,10 @@ module Piggybak
12
14
  "::Piggybak::ShippingCalculator::Free",
13
15
  "::Piggybak::ShippingCalculator::Range"]
14
16
  @tax_calculators = ["::Piggybak::TaxCalculator::Percent"]
17
+
18
+ @default_country = "US"
19
+
20
+ @activemerchant_mode = :production
15
21
  end
16
22
  end
17
23
 
data/piggybak.gemspec CHANGED
@@ -5,26 +5,30 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "piggybak"
8
- s.version = "0.2.1"
8
+ s.version = "0.3.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Steph Skardal", "Brian Buchalter"]
12
- s.date = "2012-01-07"
12
+ s.date = "2012-01-09"
13
13
  s.description = "Mountable ecommerce"
14
14
  s.email = "steph@endpoint.com"
15
15
  s.extra_rdoc_files = [
16
+ "LICENSE",
16
17
  "README.md"
17
18
  ]
18
19
  s.files = [
19
20
  ".document",
20
21
  "Gemfile",
21
22
  "Gemfile.lock",
23
+ "LICENSE",
22
24
  "README.md",
23
25
  "Rakefile",
24
26
  "VERSION",
25
27
  "app/assets/javascripts/piggybak.js",
28
+ "app/assets/javascripts/piggybak.states.js",
26
29
  "app/controllers/piggybak/cart_controller.rb",
27
30
  "app/controllers/piggybak/orders_controller.rb",
31
+ "app/controllers/piggybak/payments_controller.rb",
28
32
  "app/helpers/piggybak_helper.rb",
29
33
  "app/mailers/piggybak/notifier.rb",
30
34
  "app/models/piggybak/address.rb",
@@ -60,7 +64,11 @@ Gem::Specification.new do |s|
60
64
  "app/views/piggybak/orders/no_access.text.erb",
61
65
  "app/views/piggybak/orders/receipt.html.erb",
62
66
  "app/views/piggybak/orders/show.html.erb",
63
- "app/views/rails_admin/main/_actions.html.erb",
67
+ "app/views/rails_admin/main/_form_nested_no_destroy.html.haml",
68
+ "app/views/rails_admin/main/_location_select.html.haml",
69
+ "app/views/rails_admin/main/_no_edit_form_filtering_select.html.haml",
70
+ "app/views/rails_admin/main/_order_details.html.erb",
71
+ "app/views/rails_admin/main/_payment_refund.html.haml",
64
72
  "config/routes.rb",
65
73
  "db/migrate/20111227150106_create_orders.rb",
66
74
  "db/migrate/20111227150322_create_addresses.rb",
@@ -78,6 +86,7 @@ Gem::Specification.new do |s|
78
86
  "db/migrate/20120102162703_create_tax_method_values.rb",
79
87
  "db/migrate/20120104020930_populate_countries_and_states.rb",
80
88
  "db/migrate/20120106010412_create_credits.rb",
89
+ "db/migrate/20120107135459_add_active_to_countries.rb",
81
90
  "lib/acts_as_orderer/base.rb",
82
91
  "lib/acts_as_variant/base.rb",
83
92
  "lib/currency.rb",
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: piggybak
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.2.1
5
+ version: 0.3.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - Steph Skardal
@@ -11,7 +11,7 @@ autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
13
 
14
- date: 2012-01-07 00:00:00 Z
14
+ date: 2012-01-09 00:00:00 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: countries
@@ -108,17 +108,21 @@ executables: []
108
108
  extensions: []
109
109
 
110
110
  extra_rdoc_files:
111
+ - LICENSE
111
112
  - README.md
112
113
  files:
113
114
  - .document
114
115
  - Gemfile
115
116
  - Gemfile.lock
117
+ - LICENSE
116
118
  - README.md
117
119
  - Rakefile
118
120
  - VERSION
119
121
  - app/assets/javascripts/piggybak.js
122
+ - app/assets/javascripts/piggybak.states.js
120
123
  - app/controllers/piggybak/cart_controller.rb
121
124
  - app/controllers/piggybak/orders_controller.rb
125
+ - app/controllers/piggybak/payments_controller.rb
122
126
  - app/helpers/piggybak_helper.rb
123
127
  - app/mailers/piggybak/notifier.rb
124
128
  - app/models/piggybak/address.rb
@@ -154,7 +158,11 @@ files:
154
158
  - app/views/piggybak/orders/no_access.text.erb
155
159
  - app/views/piggybak/orders/receipt.html.erb
156
160
  - app/views/piggybak/orders/show.html.erb
157
- - app/views/rails_admin/main/_actions.html.erb
161
+ - app/views/rails_admin/main/_form_nested_no_destroy.html.haml
162
+ - app/views/rails_admin/main/_location_select.html.haml
163
+ - app/views/rails_admin/main/_no_edit_form_filtering_select.html.haml
164
+ - app/views/rails_admin/main/_order_details.html.erb
165
+ - app/views/rails_admin/main/_payment_refund.html.haml
158
166
  - config/routes.rb
159
167
  - db/migrate/20111227150106_create_orders.rb
160
168
  - db/migrate/20111227150322_create_addresses.rb
@@ -172,6 +180,7 @@ files:
172
180
  - db/migrate/20120102162703_create_tax_method_values.rb
173
181
  - db/migrate/20120104020930_populate_countries_and_states.rb
174
182
  - db/migrate/20120106010412_create_credits.rb
183
+ - db/migrate/20120107135459_add_active_to_countries.rb
175
184
  - lib/acts_as_orderer/base.rb
176
185
  - lib/acts_as_variant/base.rb
177
186
  - lib/currency.rb
@@ -191,7 +200,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
191
200
  requirements:
192
201
  - - ">="
193
202
  - !ruby/object:Gem::Version
194
- hash: 2778086706911643141
203
+ hash: -3349211022741350095
195
204
  segments:
196
205
  - 0
197
206
  version: "0"
@@ -1,12 +0,0 @@
1
- <ul>
2
- <% if params[:action] == 'edit' -%>
3
- <li><%= link_to "Send Email Confirmation", piggybak.orders_email_url(params[:id]) %></li>
4
- <% else -%>
5
- <li><b>You may not send a confirmation email until you have a saved order.</b></li>
6
- <% end -%>
7
- <% if params[:action] == 'edit' -%>
8
- <li><%= link_to "View as Text", piggybak.orders_download_url(params[:id]) %></li>
9
- <% else -%>
10
- <li><b>You may not download until you have a saved order.</b></li>
11
- <% end -%>
12
- </ul>