radiant-shop-extension 0.10.0 → 0.11.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +5 -1
- data/VERSION +1 -1
- data/app/controllers/admin/shop/categories_controller.rb +32 -65
- data/app/controllers/admin/shop/customers_controller.rb +8 -7
- data/app/controllers/admin/shop/orders_controller.rb +37 -159
- data/app/controllers/admin/shop/packages/packings_controller.rb +90 -0
- data/app/controllers/admin/shop/packages_controller.rb +65 -0
- data/app/controllers/admin/shop/products/images_controller.rb +5 -8
- data/app/controllers/admin/shop/products/variant_templates_controller.rb +27 -0
- data/app/controllers/admin/shop/products/variants_controller.rb +60 -0
- data/app/controllers/admin/shop/products_controller.rb +67 -64
- data/app/controllers/admin/shop/variants_controller.rb +49 -0
- data/app/helpers/shop_helper.rb +5 -0
- data/app/models/form_address.rb +144 -0
- data/app/models/form_checkout.rb +200 -307
- data/app/models/form_line_item.rb +60 -0
- data/app/models/shop_address.rb +2 -2
- data/app/models/shop_category.rb +48 -51
- data/app/models/shop_customer.rb +25 -5
- data/app/models/shop_group.rb +15 -0
- data/app/models/shop_grouping.rb +6 -0
- data/app/models/shop_line_item.rb +7 -6
- data/app/models/shop_order.rb +44 -16
- data/app/models/shop_package.rb +74 -0
- data/app/models/shop_packing.rb +22 -0
- data/app/models/shop_page.rb +7 -0
- data/app/models/shop_payment.rb +5 -1
- data/app/models/shop_product.rb +86 -48
- data/app/models/shop_product_attachment.rb +5 -2
- data/app/models/shop_product_variant.rb +31 -0
- data/app/models/shop_variant.rb +25 -0
- data/app/views/admin/shop/categories/edit.html.haml +9 -10
- data/app/views/admin/shop/categories/edit/_foot.html.haml +9 -0
- data/app/views/admin/shop/categories/edit/_form.html.haml +14 -0
- data/app/views/admin/shop/categories/edit/_head.html.haml +4 -2
- data/app/views/admin/shop/categories/edit/_inputs.html.haml +2 -0
- data/app/views/admin/shop/categories/edit/_meta.html.haml +8 -7
- data/app/views/admin/shop/categories/edit/_parts.html.haml +9 -0
- data/app/views/admin/shop/categories/edit/_popups.html.haml +4 -0
- data/app/views/admin/shop/categories/edit/inputs/_name.html.haml +4 -0
- data/app/views/admin/shop/categories/edit/meta/_handle.html.haml +6 -5
- data/app/views/admin/shop/categories/edit/meta/_layouts.html.haml +6 -5
- data/app/views/admin/shop/categories/edit/meta/_page.html.haml +3 -0
- data/app/views/admin/shop/categories/edit/parts/_description.html.haml +4 -1
- data/app/views/admin/shop/categories/index/_category.html.haml +11 -10
- data/app/views/admin/shop/categories/new.html.haml +10 -9
- data/app/views/admin/shop/customers/edit.html.haml +8 -10
- data/app/views/admin/shop/customers/edit/_foot.html.haml +9 -0
- data/app/views/admin/shop/customers/edit/_form.html.haml +13 -0
- data/app/views/admin/shop/customers/edit/_head.html.haml +4 -2
- data/app/views/admin/shop/customers/edit/_inputs.html.haml +2 -0
- data/app/views/admin/shop/customers/edit/_parts.html.haml +9 -4
- data/app/views/admin/shop/customers/edit/inputs/_email.html.haml +3 -0
- data/app/views/admin/shop/customers/edit/inputs/_name.html.haml +3 -0
- data/app/views/admin/shop/customers/edit/parts/_orders.html.haml +10 -6
- data/app/views/admin/shop/customers/index.html.haml +4 -1
- data/app/views/admin/shop/customers/index/_customer.html.haml +3 -3
- data/app/views/admin/shop/customers/index/_foot.html.haml +4 -0
- data/app/views/admin/shop/customers/index/_head.html.haml +4 -0
- data/app/views/admin/shop/customers/new.html.haml +9 -9
- data/app/views/admin/shop/orders/edit.html.haml +11 -0
- data/app/views/admin/shop/orders/edit/_foot.html.haml +9 -0
- data/app/views/admin/shop/orders/edit/_form.html.haml +13 -0
- data/app/views/admin/shop/orders/edit/_head.html.haml +4 -0
- data/app/views/admin/shop/orders/edit/_inputs.html.haml +2 -0
- data/app/views/admin/shop/orders/edit/_meta.html.haml +8 -0
- data/app/views/admin/shop/orders/edit/_parts.html.haml +9 -0
- data/app/views/admin/shop/orders/edit/_popups.html.haml +4 -0
- data/app/views/admin/shop/orders/edit/parts/_address.html.haml +7 -0
- data/app/views/admin/shop/orders/edit/parts/_addresses.html.haml +9 -0
- data/app/views/admin/shop/orders/edit/parts/_customer.html.haml +3 -0
- data/app/views/admin/shop/orders/edit/parts/_items.html.haml +8 -0
- data/app/views/admin/shop/orders/index.html.haml +10 -30
- data/app/views/admin/shop/orders/index/_foot.html.haml +3 -0
- data/app/views/admin/shop/orders/index/_head.html.haml +4 -0
- data/app/views/admin/shop/orders/index/_order.html.haml +13 -0
- data/app/views/admin/shop/orders/index/buttons/_all.html.haml +1 -0
- data/app/views/admin/shop/orders/index/buttons/_new.html.haml +1 -0
- data/app/views/admin/shop/orders/index/buttons/_paid.html.haml +1 -0
- data/app/views/admin/shop/orders/index/buttons/_shipped.html.haml +1 -0
- data/app/views/admin/shop/packages/edit.html.haml +11 -0
- data/app/views/admin/shop/packages/edit/_foot.html.haml +17 -0
- data/app/views/admin/shop/packages/edit/_form.html.haml +15 -0
- data/app/views/admin/shop/packages/edit/_head.html.haml +4 -0
- data/app/views/admin/shop/packages/edit/_inputs.html.haml +2 -0
- data/app/views/admin/shop/packages/edit/_meta.html.haml +8 -0
- data/app/views/admin/shop/packages/edit/_parts.html.haml +9 -0
- data/app/views/admin/shop/packages/edit/_popups.html.haml +4 -0
- data/app/views/admin/shop/packages/edit/buttons/_browse_products.html.haml +1 -0
- data/app/views/admin/shop/packages/edit/inputs/_name.html.haml +3 -0
- data/app/views/admin/shop/packages/edit/inputs/_price.html.haml +3 -0
- data/app/views/admin/shop/packages/edit/meta/_sku.html.haml +5 -0
- data/app/views/admin/shop/packages/edit/parts/_description.html.haml +1 -0
- data/app/views/admin/shop/packages/edit/parts/_products.html.haml +3 -0
- data/app/views/admin/shop/packages/edit/popups/_browse_products.html.haml +7 -0
- data/app/views/admin/shop/packages/edit/shared/_product.html.haml +8 -0
- data/app/views/admin/shop/packages/index.html.haml +13 -0
- data/app/views/admin/shop/packages/index/_foot.html.haml +7 -0
- data/app/views/admin/shop/packages/index/_head.html.haml +4 -0
- data/app/views/admin/shop/packages/index/_package.html.haml +9 -0
- data/app/views/admin/shop/packages/new.html.haml +11 -0
- data/app/views/admin/shop/packages/remove.html.haml +12 -0
- data/app/views/admin/shop/products/edit.html.haml +9 -12
- data/app/views/admin/shop/products/edit/_foot.html.haml +19 -0
- data/app/views/admin/shop/products/edit/_form.html.haml +15 -0
- data/app/views/admin/shop/products/edit/_head.html.haml +4 -2
- data/app/views/admin/shop/products/edit/_inputs.html.haml +2 -0
- data/app/views/admin/shop/products/edit/_meta.html.haml +8 -8
- data/app/views/admin/shop/products/edit/_parts.html.haml +9 -0
- data/app/views/admin/shop/products/edit/_popups.html.haml +4 -0
- data/app/views/admin/shop/products/edit/buttons/_browse_images.html.haml +1 -1
- data/app/views/admin/shop/products/edit/buttons/_browse_templates.html.haml +1 -0
- data/app/views/admin/shop/products/edit/buttons/_new_image.html.haml +1 -1
- data/app/views/admin/shop/products/edit/buttons/_new_variant.html.haml +1 -0
- data/app/views/admin/shop/products/edit/inputs/_name.html.haml +4 -0
- data/app/views/admin/shop/products/edit/inputs/_price.html.haml +3 -0
- data/app/views/admin/shop/products/edit/meta/_category.html.haml +6 -5
- data/app/views/admin/shop/products/edit/meta/_page.html.haml +2 -0
- data/app/views/admin/shop/products/edit/meta/_sku.html.haml +6 -5
- data/app/views/admin/shop/products/edit/parts/_customers.html.haml +5 -0
- data/app/views/admin/shop/products/edit/parts/_description.html.haml +4 -1
- data/app/views/admin/shop/products/edit/parts/_images.html.haml +1 -1
- data/app/views/admin/shop/products/edit/parts/_variants.html.haml +3 -0
- data/app/views/admin/shop/products/edit/popups/_browse_images.html.haml +1 -1
- data/app/views/admin/shop/products/edit/popups/_browse_templates.html.haml +6 -0
- data/app/views/admin/shop/products/edit/popups/_new_image.html.haml +4 -4
- data/app/views/admin/shop/products/edit/popups/_new_variant.html.haml +16 -0
- data/app/views/admin/shop/products/edit/{_image.html.haml → shared/_image.html.haml} +0 -0
- data/app/views/admin/shop/products/edit/shared/_template.html.haml +6 -0
- data/app/views/admin/shop/products/edit/shared/_variant.html.haml +10 -0
- data/app/views/admin/shop/products/index.html.haml +6 -3
- data/app/views/admin/shop/products/index/_foot.html.haml +10 -0
- data/app/views/admin/shop/products/index/_head.html.haml +4 -0
- data/app/views/admin/shop/products/index/_product.html.haml +18 -10
- data/app/views/admin/shop/products/index/buttons/_discounts.html.haml +1 -0
- data/app/views/admin/shop/products/index/buttons/_packages.html.haml +1 -0
- data/app/views/admin/shop/products/index/buttons/_variants.html.haml +1 -0
- data/app/views/admin/shop/products/new.html.haml +10 -9
- data/app/views/admin/shop/variants/edit.html.haml +11 -0
- data/app/views/admin/shop/variants/edit/_foot.html.haml +9 -0
- data/app/views/admin/shop/variants/edit/_form.html.haml +13 -0
- data/app/views/admin/shop/variants/edit/_head.html.haml +4 -0
- data/app/views/admin/shop/variants/edit/_inputs.html.haml +2 -0
- data/app/views/admin/shop/variants/edit/_meta.html.haml +8 -0
- data/app/views/admin/shop/variants/edit/_parts.html.haml +9 -0
- data/app/views/admin/shop/variants/edit/_popups.html.haml +4 -0
- data/app/views/admin/shop/variants/edit/inputs/_name.html.haml +3 -0
- data/app/views/admin/shop/variants/edit/inputs/_options.html.haml +3 -0
- data/app/views/admin/shop/variants/index.html.haml +13 -0
- data/app/views/admin/shop/variants/index/_foot.html.haml +4 -0
- data/app/views/admin/shop/variants/index/_head.html.haml +4 -0
- data/app/views/admin/shop/variants/index/_variant.html.haml +9 -0
- data/app/views/admin/shop/variants/new.html.haml +11 -0
- data/app/views/admin/shop/variants/remove.html.haml +12 -0
- data/config/locales/en.yml +8 -1
- data/config/routes.rb +11 -22
- data/db/migrate/20100929011106_fix_relationship_foreign_keys.rb +101 -0
- data/db/migrate/20100929011607_drop_shop_addressables.rb +25 -0
- data/db/migrate/20100929030510_create_shop_groups.rb +19 -0
- data/db/migrate/20100929080301_shop_groups_change_add_description.rb +11 -0
- data/db/migrate/20100929081200_create_variants.rb +20 -0
- data/db/migrate/20100929143531_change_variants_add_updated_by.rb +25 -0
- data/db/migrate/20100929145641_change_created_by_to_id.rb +75 -0
- data/db/migrate/20100929235221_change_category_add_variant_id.rb +9 -0
- data/db/migrate/20100930235221_create_packages.rb +31 -0
- data/db/migrate/20101005231713_create_page_ids.rb +11 -0
- data/db/migrate/20101006051214_create_pages_for_products_and_categories.rb +73 -0
- data/db/migrate/20101007111845_add_default_position_to_attachment.rb +9 -0
- data/lib/shop/controllers/application_controller.rb +1 -1
- data/lib/shop/interface/categories.rb +29 -0
- data/lib/shop/interface/customers.rb +15 -15
- data/lib/shop/interface/orders.rb +34 -0
- data/lib/shop/interface/packages.rb +34 -0
- data/lib/shop/interface/products.rb +18 -31
- data/lib/shop/interface/variants.rb +34 -0
- data/lib/shop/models/form_extension.rb +23 -0
- data/lib/shop/models/page.rb +3 -2
- data/lib/shop/tags/address.rb +3 -3
- data/lib/shop/tags/cart.rb +12 -16
- data/lib/shop/tags/category.rb +4 -6
- data/lib/shop/tags/helpers.rb +131 -30
- data/lib/shop/tags/item.rb +16 -33
- data/lib/shop/tags/package.rb +95 -0
- data/lib/shop/tags/product.rb +11 -16
- data/lib/shop/tags/product_variant.rb +52 -0
- data/mockups/discounts/new-edit.bmml +492 -0
- data/public/javascripts/admin/extensions/shop/edit.js +20 -0
- data/public/javascripts/admin/extensions/shop/packages/edit.js +109 -0
- data/public/javascripts/admin/extensions/shop/products/edit.js +64 -65
- data/public/javascripts/admin/extensions/shop/products/index.js +31 -36
- data/public/stylesheets/sass/admin/extensions/shop/edit.sass +58 -11
- data/public/stylesheets/sass/admin/extensions/shop/index.sass +104 -53
- data/public/stylesheets/sass/admin/extensions/shop/packages/edit.sass +162 -0
- data/public/stylesheets/sass/admin/extensions/shop/products/edit.sass +115 -36
- data/public/stylesheets/sass/admin/extensions/shop/products/index.sass +12 -20
- data/radiant-shop-extension.gemspec +182 -38
- data/shop_extension.rb +29 -25
- data/spec/controllers/admin/shop/categories_controller_spec.rb +76 -191
- data/spec/controllers/admin/shop/customers_controller_spec.rb +67 -0
- data/spec/controllers/admin/shop/orders_controller_spec.rb +85 -0
- data/spec/controllers/admin/shop/packages/packings_controller_spec.rb +188 -0
- data/spec/controllers/admin/shop/packages_controller_spec.rb +32 -0
- data/spec/controllers/admin/shop/products/images_controller_spec.rb +10 -15
- data/spec/controllers/admin/shop/products/variant_templates_controller_spec.rb +53 -0
- data/spec/controllers/admin/shop/products/variants_controller_spec.rb +116 -0
- data/spec/controllers/admin/shop/products_controller_spec.rb +80 -148
- data/spec/controllers/admin/shop/variants_controller_spec.rb +67 -0
- data/spec/datasets/forms.rb +23 -8
- data/spec/datasets/shop_addresses.rb +1 -4
- data/spec/datasets/shop_categories.rb +25 -4
- data/spec/datasets/shop_groups.rb +15 -0
- data/spec/datasets/shop_line_items.rb +4 -3
- data/spec/datasets/shop_orders.rb +6 -15
- data/spec/datasets/shop_packages.rb +25 -0
- data/spec/datasets/shop_payments.rb +12 -0
- data/spec/datasets/{images.rb → shop_product_attachments.rb} +9 -1
- data/spec/datasets/shop_product_variants.rb +22 -0
- data/spec/datasets/shop_products.rb +28 -18
- data/spec/datasets/shop_variants.rb +17 -0
- data/spec/lib/shop/models/image_spec.rb +4 -4
- data/spec/lib/shop/tags/address_spec.rb +3 -3
- data/spec/lib/shop/tags/cart_spec.rb +32 -43
- data/spec/lib/shop/tags/category_spec.rb +58 -93
- data/spec/lib/shop/tags/helpers_spec.rb +162 -69
- data/spec/lib/shop/tags/item_spec.rb +116 -158
- data/spec/lib/shop/tags/package_spec.rb +342 -0
- data/spec/lib/shop/tags/product_spec.rb +29 -26
- data/spec/lib/shop/tags/product_variant_spec.rb +175 -0
- data/spec/models/form_address_spec.rb +166 -0
- data/spec/models/form_checkout_spec.rb +27 -157
- data/spec/models/shop_address_spec.rb +81 -0
- data/spec/models/shop_category_spec.rb +114 -31
- data/spec/models/shop_customer_spec.rb +105 -0
- data/spec/models/shop_group_spec.rb +38 -0
- data/spec/models/shop_line_item_spec.rb +5 -5
- data/spec/models/shop_order_spec.rb +89 -83
- data/spec/models/shop_package_spec.rb +117 -0
- data/spec/models/shop_packing_spec.rb +67 -0
- data/spec/models/shop_payment_spec.rb +19 -0
- data/spec/models/shop_product_attachment_spec.rb +4 -3
- data/spec/models/shop_product_spec.rb +171 -92
- data/spec/models/shop_product_variant_spec.rb +92 -0
- data/spec/models/shop_variant_spec.rb +47 -0
- metadata +183 -39
- data/app/controllers/shop/categories_controller.rb +0 -45
- data/app/controllers/shop/line_items_controller.rb +0 -165
- data/app/controllers/shop/orders_controller.rb +0 -16
- data/app/controllers/shop/products_controller.rb +0 -48
- data/app/models/shop_addressable.rb +0 -11
- data/app/views/admin/shop/categories/edit/_fields.html.haml +0 -28
- data/app/views/admin/shop/categories/edit/_part.html.haml +0 -3
- data/app/views/admin/shop/categories/edit/_popup.html.haml +0 -3
- data/app/views/admin/shop/customers/edit/_fields.html.haml +0 -33
- data/app/views/admin/shop/customers/index/_bottom.html.haml +0 -4
- data/app/views/admin/shop/products/edit/_fields.html.haml +0 -38
- data/app/views/admin/shop/products/edit/_part.html.haml +0 -3
- data/app/views/admin/shop/products/edit/_popup.html.haml +0 -3
- data/app/views/admin/shop/products/index/_bottom.html.haml +0 -5
- data/spec/controllers/shop/categories_controller_spec.rb +0 -42
- data/spec/controllers/shop/line_items_controller_spec.rb +0 -256
- data/spec/controllers/shop/orders_controller_specs.rb +0 -37
- data/spec/controllers/shop/products_controller_spec.rb +0 -51
- data/vendor/plugins/json_fields/.gitignore +0 -3
- data/vendor/plugins/json_fields/MIT-LICENSE +0 -20
- data/vendor/plugins/json_fields/README.rdoc +0 -65
- data/vendor/plugins/json_fields/Rakefile +0 -55
- data/vendor/plugins/json_fields/init.rb +0 -2
- data/vendor/plugins/json_fields/lib/json_fields.rb +0 -99
- data/vendor/plugins/json_fields/spec/lib/json_fields_spec.rb +0 -124
- data/vendor/plugins/json_fields/spec/spec.opts +0 -6
- data/vendor/plugins/json_fields/spec/spec_helper.rb +0 -29
- data/vendor/plugins/json_fields/spec/test_models.rb +0 -12
@@ -0,0 +1,144 @@
|
|
1
|
+
class FormAddress
|
2
|
+
include Forms::Models::Extension
|
3
|
+
include Shop::Models::FormExtension
|
4
|
+
|
5
|
+
attr_accessor :config, :data, :result, :gateway, :card, :billing, :shipping
|
6
|
+
|
7
|
+
def create
|
8
|
+
find_current_order # locate the @order object
|
9
|
+
|
10
|
+
create_result_object # A default response object
|
11
|
+
|
12
|
+
create_order_addresses # Process the addresses
|
13
|
+
|
14
|
+
@result
|
15
|
+
end
|
16
|
+
|
17
|
+
protected
|
18
|
+
|
19
|
+
def create_result_object
|
20
|
+
@result = {
|
21
|
+
:order => @order.id, # We return the order id so the thank you screen has access to it
|
22
|
+
:billing => nil,
|
23
|
+
:shipping => nil
|
24
|
+
}
|
25
|
+
end
|
26
|
+
|
27
|
+
# Assigns a shipping and billing address to the @order
|
28
|
+
def create_order_addresses
|
29
|
+
if billing?
|
30
|
+
# We're going to create a billing object
|
31
|
+
create_order_billing_address
|
32
|
+
|
33
|
+
# We're going to assign shipping to billing because they didn't send shipping
|
34
|
+
if !shipping.present? and @billing.id
|
35
|
+
@shipping = @billing
|
36
|
+
@order.update_attribute(:shipping, @shipping)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
if shipping?
|
41
|
+
create_order_shipping_address
|
42
|
+
end
|
43
|
+
|
44
|
+
if @billing.nil? or !@billing.id
|
45
|
+
@form.redirect_to = :back
|
46
|
+
end
|
47
|
+
|
48
|
+
@result[:billing] = @billing.present? ? @billing.id : false
|
49
|
+
@result[:shipping] = @shipping.present? ? @shipping.id : false
|
50
|
+
end
|
51
|
+
|
52
|
+
# Attaches a billing address to the order (and current customer)
|
53
|
+
def create_order_billing_address
|
54
|
+
# Billing Address
|
55
|
+
if billing[:id]
|
56
|
+
begin
|
57
|
+
# Use an existing Address and update its values
|
58
|
+
@billing = current_customer.billings.find(billing[:id])
|
59
|
+
@billing.update_attributes(billing)
|
60
|
+
@order.update_attribute(:billing, @billing)
|
61
|
+
rescue
|
62
|
+
# We cant find that address for that user
|
63
|
+
end
|
64
|
+
|
65
|
+
elsif @order.billing.present?
|
66
|
+
# Use the current billing and update its values
|
67
|
+
@billing = @order.billing
|
68
|
+
@billing.update_attributes(billing)
|
69
|
+
|
70
|
+
else
|
71
|
+
# Create a new address with these attributes
|
72
|
+
@billing = ShopAddress.new(billing)
|
73
|
+
if @billing.save
|
74
|
+
@order.update_attribute(:billing, @billing)
|
75
|
+
end
|
76
|
+
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
# Attaches a shipping address to the order (and current customer)
|
81
|
+
def create_order_shipping_address
|
82
|
+
# Shipping Address
|
83
|
+
|
84
|
+
if shipping[:id]
|
85
|
+
# Use an existing Address and update its values
|
86
|
+
begin
|
87
|
+
@shipping = current_customer.shippings.find(shipping[:id])
|
88
|
+
if @shipping == @billing and shipping == billing
|
89
|
+
# We have exactly the same shipping and billing data
|
90
|
+
@shipping = @billing
|
91
|
+
@order.update_attribute(:shipping, @billing)
|
92
|
+
|
93
|
+
elsif (shipping.reject!{|k,v| k == :id }).values.all?(&:blank?)
|
94
|
+
# We have just passed the id and not the data
|
95
|
+
@order.update_attribute(:shipping, @shipping)
|
96
|
+
|
97
|
+
elsif @shipping == @billing and shipping != billing
|
98
|
+
# We have conflicting data so create a new address
|
99
|
+
# the id is rejected so we'll get a new address
|
100
|
+
@order.update_attributes!({ :shipping_attributes => shipping })
|
101
|
+
@shipping = @order.shipping
|
102
|
+
end
|
103
|
+
rescue
|
104
|
+
# We cant find that address for that customer
|
105
|
+
end
|
106
|
+
|
107
|
+
elsif @order.shipping.present?
|
108
|
+
# Use the current shipping and update its values
|
109
|
+
@shipping = @order.shipping
|
110
|
+
@shipping.update_attributes(shipping)
|
111
|
+
|
112
|
+
elsif shipping.values.all?(&:blank?) or shipping == billing
|
113
|
+
# We haven't set a shipping, or we have copied billing, so use billing
|
114
|
+
@shipping = @billing
|
115
|
+
@order.update_attribute(:shipping, @billing)
|
116
|
+
|
117
|
+
else
|
118
|
+
# Create a new address with these attributes
|
119
|
+
@order.update_attributes!({ :shipping_attributes => shipping })
|
120
|
+
@shipping = @order.shipping
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
# Returns an array of billing attributes
|
125
|
+
def billing
|
126
|
+
@data[:billing]
|
127
|
+
end
|
128
|
+
|
129
|
+
# Returns an array of shipping attributes
|
130
|
+
def shipping
|
131
|
+
@data[:shipping]
|
132
|
+
end
|
133
|
+
|
134
|
+
# Returns whether form is configured for billing
|
135
|
+
def billing?
|
136
|
+
@config[:billing].present? and billing.present?
|
137
|
+
end
|
138
|
+
|
139
|
+
# Returns whether form is configured for shipping
|
140
|
+
def shipping?
|
141
|
+
@config[:shipping].present? and shipping.present?
|
142
|
+
end
|
143
|
+
|
144
|
+
end
|
data/app/models/form_checkout.rb
CHANGED
@@ -1,331 +1,224 @@
|
|
1
1
|
class FormCheckout
|
2
2
|
include Forms::Models::Extension
|
3
|
+
include Shop::Models::FormExtension
|
3
4
|
|
4
|
-
attr_accessor :config, :data, :result, :gateway, :card
|
5
|
+
attr_accessor :config, :data, :result, :gateway, :card
|
5
6
|
|
6
7
|
def create
|
7
8
|
find_current_order # locate the @order object
|
8
9
|
|
9
10
|
create_result_object # A default response object
|
10
|
-
|
11
|
-
create_order_addresses # Create @order.billing and @order.shipping
|
12
|
-
|
11
|
+
|
13
12
|
# If the form was configured for gateway and we have a billing address
|
14
|
-
if
|
15
|
-
|
16
|
-
|
13
|
+
if @order.billing.present?
|
14
|
+
if gateway.present?
|
15
|
+
prepare_gateway # Create the @gateway object
|
16
|
+
prepare_credit_card if card.present?# Create the @card object
|
17
17
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
18
|
+
if @result[:gateway] and @result[:card]
|
19
|
+
purchase! # Use @card to pay through @gateway
|
20
|
+
|
21
|
+
# We have a paid for order with a billing address
|
22
|
+
if success?
|
23
|
+
finalize_cart
|
24
|
+
# The form was configured to send a payment email
|
25
|
+
if mail.present?
|
26
|
+
configure_invoice_mail # Create some configuration variables for mailing
|
27
|
+
end
|
28
|
+
else
|
29
|
+
@result[:payment] = false
|
30
|
+
@form.redirect_to = :back
|
31
|
+
end
|
27
32
|
end
|
28
33
|
else
|
29
|
-
@
|
34
|
+
@result[:gateway] = false
|
30
35
|
end
|
31
36
|
end
|
32
37
|
@result
|
33
38
|
end
|
34
39
|
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
}
|
52
|
-
|
53
|
-
|
54
|
-
# Assigns a shipping and billing address to the @order
|
55
|
-
def create_order_addresses
|
56
|
-
if billing.present?
|
57
|
-
# We're going to create a billing object
|
58
|
-
create_order_billing_address
|
59
|
-
|
60
|
-
# We're going to assign shipping to billing because they didn't send shipping
|
61
|
-
if !shipping.present? and @billing.present?
|
62
|
-
@shipping = @billing
|
63
|
-
@order.update_attribute(:shipping_id, @shipping.id)
|
64
|
-
end
|
65
|
-
end
|
66
|
-
|
67
|
-
if shipping.present?
|
68
|
-
create_order_shipping_address
|
69
|
-
end
|
70
|
-
|
71
|
-
if @order.billing.nil?
|
72
|
-
@form.redirect_to = :back
|
73
|
-
end
|
74
|
-
|
75
|
-
@result[:billing] = @order.billing.present?
|
76
|
-
@result[:shipping] = @order.shipping.present?
|
77
|
-
end
|
78
|
-
|
79
|
-
# Attaches a billing address to the order (and current customer)
|
80
|
-
def create_order_billing_address
|
81
|
-
# Billing Address
|
82
|
-
if billing[:id]
|
83
|
-
begin
|
84
|
-
# Use an existing Address and update its values
|
85
|
-
@billing = current_customer.billings.find(billing[:id])
|
86
|
-
@billing.update_attributes(billing)
|
87
|
-
@order.update_attribute(:billing_id, @billing.id)
|
88
|
-
rescue
|
89
|
-
# We cant find that address for that user
|
90
|
-
end
|
91
|
-
|
92
|
-
elsif @order.billing.present?
|
93
|
-
# Use the current billing and update its values
|
94
|
-
@billing = @order.billing
|
95
|
-
@billing.update_attributes(billing)
|
96
|
-
|
97
|
-
else
|
98
|
-
# Create a new address with these attributes
|
99
|
-
@billing = ShopAddress.new(billing)
|
100
|
-
if @billing.save
|
101
|
-
@order.update_attribute(:billing_id, @billing.id)
|
102
|
-
end
|
103
|
-
|
104
|
-
end
|
105
|
-
|
106
|
-
end
|
107
|
-
|
108
|
-
# Attaches a shipping address to the order (and current customer)
|
109
|
-
def create_order_shipping_address
|
110
|
-
# Shipping Address
|
111
|
-
|
112
|
-
if shipping[:id]
|
113
|
-
# Use an existing Address and update its values
|
114
|
-
begin
|
115
|
-
@shipping = current_customer.shippings.find(shipping[:id])
|
116
|
-
if @shipping == @billing and shipping == billing
|
117
|
-
# We have exactly the same shipping and billing data
|
118
|
-
@shipping = @billing
|
119
|
-
@order.update_attribute(:shipping_id, @billing.id)
|
120
|
-
|
121
|
-
elsif (shipping.reject!{|k,v| k == :id }).values.all?(&:blank?)
|
122
|
-
# We have just passed the id and not the data
|
123
|
-
@order.update_attribute(:shipping_id, @shipping.id)
|
124
|
-
|
125
|
-
elsif @shipping == @billing and shipping != billing
|
126
|
-
# We have conflicting data so create a new address
|
127
|
-
# the id is rejected so we'll get a new address
|
128
|
-
@order.update_attributes!({ :shipping_attributes => shipping })
|
129
|
-
@shipping = @order.shipping
|
130
|
-
end
|
131
|
-
rescue
|
132
|
-
# We cant find that address for that customer
|
133
|
-
end
|
134
|
-
|
135
|
-
elsif @order.shipping.present?
|
136
|
-
# Use the current shipping and update its values
|
137
|
-
@shipping = @order.shipping
|
138
|
-
@shipping.update_attributes(shipping)
|
139
|
-
|
140
|
-
elsif shipping.values.all?(&:blank?) or shipping == billing
|
141
|
-
# We haven't set a shipping, or we have copied billing, so use billing
|
142
|
-
@shipping = @billing
|
143
|
-
@order.update_attribute(:shipping_id, @billing.id)
|
144
|
-
|
145
|
-
else
|
146
|
-
# Create a new address with these attributes
|
147
|
-
@order.update_attributes!({ :shipping_attributes => shipping })
|
148
|
-
@shipping = @order.shipping
|
149
|
-
end
|
150
|
-
end
|
151
|
-
|
152
|
-
# Creates a gateway instance variable based off the form configuration
|
153
|
-
def prepare_gateway
|
154
|
-
ActiveMerchant::Billing::Base.mode = gateway_mode
|
155
|
-
begin
|
156
|
-
@gateway = ActiveMerchant::Billing.const_get("#{gateway_name}Gateway").new(gateway_credentials)
|
157
|
-
@result[:gateway] = true
|
158
|
-
end
|
159
|
-
end
|
160
|
-
|
161
|
-
# Creates a payment object attached to the order
|
162
|
-
def create_payment
|
163
|
-
payment = ShopPayment.new({
|
164
|
-
:order => @order,
|
165
|
-
:gateway => gateway_name,
|
166
|
-
:amount => @order.price,
|
167
|
-
:card_type => card_type,
|
168
|
-
:card_number=> card_number_secure
|
169
|
-
})
|
170
|
-
|
171
|
-
@order.update_attribute(:status, 'paid')
|
172
|
-
|
173
|
-
@result[:payment] = payment.save
|
174
|
-
end
|
175
|
-
|
176
|
-
# Builds an ActiveMerchant card using the submitted card information
|
177
|
-
def prepare_credit_card
|
178
|
-
@card = ActiveMerchant::Billing::CreditCard.new({
|
179
|
-
:number => card_number,
|
180
|
-
:month => card_month,
|
181
|
-
:year => card_year,
|
182
|
-
:first_name => card_first_name,
|
183
|
-
:last_name => card_last_name,
|
184
|
-
:verification_value => card_verification,
|
185
|
-
:type => card_type
|
186
|
-
})
|
187
|
-
|
188
|
-
@result[:card] = @card.valid?
|
189
|
-
end
|
190
|
-
|
191
|
-
# Sets up mail to send an invoice to the billing email address
|
192
|
-
def configure_invoice_mail
|
193
|
-
@form[:extensions][:mail] ||= {}
|
194
|
-
@form[:extensions][:mail].merge!({
|
195
|
-
:recipient => @order.billing.email,
|
196
|
-
:to => @order.billing.email,
|
197
|
-
})
|
198
|
-
@form[:extensions][:mail].merge!(mail)
|
199
|
-
end
|
200
|
-
|
201
|
-
# Uses the gateway and card objects to carry out an ActiveMerchant purchase
|
202
|
-
def purchase!
|
203
|
-
result = @gateway.purchase(amount, @card, options)
|
204
|
-
|
205
|
-
@result[:message] = result.message
|
206
|
-
|
207
|
-
if result.success?
|
208
|
-
create_payment
|
209
|
-
end
|
210
|
-
end
|
211
|
-
|
212
|
-
# Returns the current logged in ShopCustomer (if it exists)
|
213
|
-
def current_customer
|
214
|
-
return @shop_customer if @shop_customer.present?
|
215
|
-
return current_user if current_user.present?
|
216
|
-
@shop_customer = ShopCustomer.find(current_user.id)
|
217
|
-
end
|
218
|
-
|
219
|
-
# Returns an array of billing attributes
|
220
|
-
def billing
|
221
|
-
@data[:billing]
|
222
|
-
end
|
223
|
-
|
224
|
-
# Returns an array of shipping attributes
|
225
|
-
def shipping
|
226
|
-
@data[:shipping]
|
227
|
-
end
|
228
|
-
|
229
|
-
# Returns the configured gateway
|
230
|
-
def gateway
|
231
|
-
@config[:gateway]
|
232
|
-
end
|
233
|
-
|
234
|
-
# Returns the name of the gateway (Eway)
|
235
|
-
def gateway_name
|
236
|
-
gateway[:name]
|
237
|
-
end
|
238
|
-
|
239
|
-
# Returns Gateway username and password etc
|
240
|
-
def gateway_credentials
|
241
|
-
gateway[:credentials]
|
242
|
-
end
|
243
|
-
|
244
|
-
# Returns whether the form is configured as testing or production
|
245
|
-
def gateway_mode
|
246
|
-
@config[:test].present? ? :test : :production
|
247
|
-
end
|
248
|
-
|
249
|
-
# Returns the submitted card attributes
|
250
|
-
def card
|
251
|
-
@data[:card]
|
252
|
-
end
|
253
|
-
|
254
|
-
# Returns card number (1234123412341234)
|
255
|
-
def card_number
|
256
|
-
card[:number].to_s.gsub('-', '')
|
257
|
-
end
|
258
|
-
|
259
|
-
# Returns the last 4 card number digits (1234)
|
260
|
-
def card_number_secure
|
261
|
-
@card.display_number
|
262
|
-
end
|
263
|
-
|
264
|
-
# Returns card month (02)
|
265
|
-
def card_month
|
266
|
-
card[:month].to_i
|
267
|
-
end
|
268
|
-
|
269
|
-
# Returns card year (2010)
|
270
|
-
def card_year
|
271
|
-
card[:year].to_i
|
272
|
-
end
|
273
|
-
|
274
|
-
# Returns card type (visa)
|
275
|
-
def card_type
|
276
|
-
card[:type]
|
277
|
-
end
|
278
|
-
|
279
|
-
# Returns card verification number (123)
|
280
|
-
def card_verification
|
281
|
-
card[:verification].to_s
|
282
|
-
end
|
283
|
-
|
284
|
-
# Splits the card names into an array we can inspect
|
285
|
-
def card_names
|
286
|
-
return @card_names if @card_names.present?
|
287
|
-
@card_names = @data[:card][:name].split(' ')
|
288
|
-
end
|
289
|
-
|
290
|
-
# Return all the strings bar the last on the card
|
291
|
-
def card_first_name
|
292
|
-
card_names[0, card_names.length - 1].join(' ') if card_names.present?
|
293
|
-
end
|
294
|
-
|
295
|
-
# Return the last string on the card
|
296
|
-
def card_last_name
|
297
|
-
card_names[-1] if card_names.present?
|
298
|
-
end
|
299
|
-
|
300
|
-
# Returns configured mail attributes
|
301
|
-
def mail
|
302
|
-
@config[:mail]
|
40
|
+
protected
|
41
|
+
|
42
|
+
def create_result_object
|
43
|
+
@result = {
|
44
|
+
:order => @order.id, # We return the order id so the thank you screen has access to it
|
45
|
+
:payment => nil,
|
46
|
+
:card => nil,
|
47
|
+
:gateway => nil,
|
48
|
+
:message => nil,
|
49
|
+
}
|
50
|
+
end
|
51
|
+
|
52
|
+
# Creates a gateway instance variable based off the form configuration
|
53
|
+
def prepare_gateway
|
54
|
+
ActiveMerchant::Billing::Base.mode = gateway_mode
|
55
|
+
begin
|
56
|
+
@gateway = ActiveMerchant::Billing.const_get("#{gateway_name}Gateway").new(gateway_credentials)
|
57
|
+
@result[:gateway] = true
|
303
58
|
end
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
59
|
+
end
|
60
|
+
|
61
|
+
# Creates a payment object attached to the order
|
62
|
+
def create_payment
|
63
|
+
payment = ShopPayment.new({
|
64
|
+
:order => @order,
|
65
|
+
:gateway => gateway_name,
|
66
|
+
:amount => @order.price,
|
67
|
+
:card_type => card_type,
|
68
|
+
:card_number=> card_number_secure
|
69
|
+
})
|
70
|
+
|
71
|
+
@order.update_attribute(:status, 'paid')
|
72
|
+
|
73
|
+
@result[:payment] = payment.save
|
74
|
+
end
|
75
|
+
|
76
|
+
# Builds an ActiveMerchant card using the submitted card information
|
77
|
+
def prepare_credit_card
|
78
|
+
@card = ActiveMerchant::Billing::CreditCard.new({
|
79
|
+
:number => card_number,
|
80
|
+
:month => card_month,
|
81
|
+
:year => card_year,
|
82
|
+
:first_name => card_first_name,
|
83
|
+
:last_name => card_last_name,
|
84
|
+
:verification_value => card_verification,
|
85
|
+
:type => card_type
|
86
|
+
})
|
87
|
+
|
88
|
+
if @card.valid?
|
89
|
+
@result[:card] = true
|
90
|
+
else
|
91
|
+
@result[:card] = false
|
92
|
+
@result[:message] = "Credit Card Invalid: #{@card.errors.full_messages.join('. ')}"
|
314
93
|
end
|
94
|
+
end
|
95
|
+
|
96
|
+
# Sets up mail to send an invoice to the billing email address
|
97
|
+
def configure_invoice_mail
|
98
|
+
@form[:extensions][:mail] ||= {}
|
99
|
+
@form[:extensions][:mail].merge!({
|
100
|
+
:recipient => @order.billing.email,
|
101
|
+
:to => @order.billing.email,
|
102
|
+
})
|
103
|
+
@form[:extensions][:mail].merge!(mail)
|
104
|
+
end
|
105
|
+
|
106
|
+
# Uses the gateway and card objects to carry out an ActiveMerchant purchase
|
107
|
+
def purchase!
|
108
|
+
result = @gateway.purchase(amount, @card, options)
|
315
109
|
|
316
|
-
|
317
|
-
def options
|
318
|
-
@config[:options] ||= {}
|
319
|
-
|
320
|
-
@config[:options][:order_id] = @order.id
|
321
|
-
@config[:options].merge!(@data[:options]) if @data[:options].present?
|
322
|
-
|
323
|
-
@config[:options]
|
324
|
-
end
|
110
|
+
@result[:message] = result.message
|
325
111
|
|
326
|
-
|
327
|
-
|
328
|
-
@result[:payment] and @result[:billing]
|
112
|
+
if result.success?
|
113
|
+
create_payment
|
329
114
|
end
|
115
|
+
end
|
116
|
+
|
117
|
+
def finalize_cart
|
118
|
+
@result[:session] = { :shop_order => nil } # We no longer need to store the current shop_roder
|
119
|
+
@order.update_attribute(:status, 'paid') # The order is now considered paid
|
120
|
+
end
|
121
|
+
|
122
|
+
# Returns the configured gateway
|
123
|
+
def gateway
|
124
|
+
@config[:gateway]
|
125
|
+
end
|
126
|
+
|
127
|
+
# Returns the name of the gateway (Eway)
|
128
|
+
def gateway_name
|
129
|
+
gateway[:name]
|
130
|
+
end
|
131
|
+
|
132
|
+
# Returns Gateway username and password etc
|
133
|
+
def gateway_credentials
|
134
|
+
gateway[:credentials]
|
135
|
+
end
|
136
|
+
|
137
|
+
# Returns whether the form is configured as testing or production
|
138
|
+
def gateway_mode
|
139
|
+
@config[:test].present? ? :test : :production
|
140
|
+
end
|
141
|
+
|
142
|
+
# Returns the submitted card attributes
|
143
|
+
def card
|
144
|
+
@data[:card]
|
145
|
+
end
|
146
|
+
|
147
|
+
# Returns card number (1234123412341234)
|
148
|
+
def card_number
|
149
|
+
card[:number].to_s.gsub('-', '')
|
150
|
+
end
|
151
|
+
|
152
|
+
# Returns the last 4 card number digits (1234)
|
153
|
+
def card_number_secure
|
154
|
+
@card.display_number
|
155
|
+
end
|
156
|
+
|
157
|
+
# Returns card month (02)
|
158
|
+
def card_month
|
159
|
+
card[:month].to_i
|
160
|
+
end
|
161
|
+
|
162
|
+
# Returns card year (2010)
|
163
|
+
def card_year
|
164
|
+
card[:year].to_i
|
165
|
+
end
|
166
|
+
|
167
|
+
# Returns card type (visa)
|
168
|
+
def card_type
|
169
|
+
card[:type]
|
170
|
+
end
|
171
|
+
|
172
|
+
# Returns card verification number (123)
|
173
|
+
def card_verification
|
174
|
+
card[:verification].to_s
|
175
|
+
end
|
176
|
+
|
177
|
+
# Splits the card names into an array we can inspect
|
178
|
+
def card_names
|
179
|
+
return @card_names if @card_names.present?
|
180
|
+
@card_names = @data[:card][:name].split(' ')
|
181
|
+
end
|
182
|
+
|
183
|
+
# Return all the strings bar the last on the card
|
184
|
+
def card_first_name
|
185
|
+
card_names[0, card_names.length - 1].join(' ') if card_names.present?
|
186
|
+
end
|
187
|
+
|
188
|
+
# Return the last string on the card
|
189
|
+
def card_last_name
|
190
|
+
card_names[-1] if card_names.present?
|
191
|
+
end
|
192
|
+
|
193
|
+
# Returns configured mail attributes
|
194
|
+
def mail
|
195
|
+
@config[:mail]
|
196
|
+
end
|
197
|
+
|
198
|
+
# Returns the amount to be charged, which is $10 on testing gateways
|
199
|
+
def amount
|
200
|
+
result = 0
|
201
|
+
if gateway_mode === :test
|
202
|
+
result = 1000
|
203
|
+
else
|
204
|
+
result = (@order.price * 100)
|
205
|
+
end
|
206
|
+
result
|
207
|
+
end
|
208
|
+
|
209
|
+
# Options for the gateway are created at config and runtime level
|
210
|
+
def options
|
211
|
+
@config[:options] ||= {}
|
212
|
+
|
213
|
+
@config[:options][:order_id] = @order.id
|
214
|
+
@config[:options].merge!(@data[:options]) if @data[:options].present?
|
215
|
+
|
216
|
+
@config[:options]
|
217
|
+
end
|
218
|
+
|
219
|
+
# Will return true if the order is paid for and has a billing address
|
220
|
+
def success?
|
221
|
+
@result[:payment] === true
|
222
|
+
end
|
330
223
|
|
331
224
|
end
|