flex_commerce_api 0.6.57
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.env.example +6 -0
- data/.gitignore +14 -0
- data/.rspec +4 -0
- data/.rubocop.yml +1065 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/.travis.yml +3 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +56 -0
- data/Rakefile +1 -0
- data/app/models/address.rb +41 -0
- data/app/models/asset_file.rb +26 -0
- data/app/models/asset_folder.rb +14 -0
- data/app/models/bundle.rb +20 -0
- data/app/models/bundle_group.rb +15 -0
- data/app/models/cart.rb +136 -0
- data/app/models/category.rb +70 -0
- data/app/models/category_tree.rb +11 -0
- data/app/models/component.rb +8 -0
- data/app/models/container_coupon.rb +12 -0
- data/app/models/country.rb +11 -0
- data/app/models/coupon.rb +18 -0
- data/app/models/customer_account.rb +96 -0
- data/app/models/customer_account_authentication.rb +5 -0
- data/app/models/customer_segment.rb +6 -0
- data/app/models/customer_segment_member.rb +6 -0
- data/app/models/data_attribute.rb +6 -0
- data/app/models/data_store_record.rb +9 -0
- data/app/models/data_store_type.rb +9 -0
- data/app/models/discount_summary.rb +12 -0
- data/app/models/email.rb +5 -0
- data/app/models/ewis_opt_in.rb +8 -0
- data/app/models/external_url.rb +6 -0
- data/app/models/free_shipping_promotion.rb +12 -0
- data/app/models/import.rb +6 -0
- data/app/models/import_entry.rb +6 -0
- data/app/models/line_item.rb +34 -0
- data/app/models/line_item_discount.rb +7 -0
- data/app/models/markdown_price.rb +11 -0
- data/app/models/menu.rb +36 -0
- data/app/models/menu_item.rb +7 -0
- data/app/models/menu_item_item.rb +5 -0
- data/app/models/note.rb +18 -0
- data/app/models/order.rb +38 -0
- data/app/models/password_recovery.rb +20 -0
- data/app/models/payment_address_verification.rb +13 -0
- data/app/models/payment_process.rb +13 -0
- data/app/models/payment_provider.rb +15 -0
- data/app/models/payment_transaction.rb +13 -0
- data/app/models/product.rb +99 -0
- data/app/models/product_asset_file.rb +12 -0
- data/app/models/promotion.rb +19 -0
- data/app/models/promotion_qualifying_product_exclusion.rb +8 -0
- data/app/models/redirect.rb +14 -0
- data/app/models/refund.rb +14 -0
- data/app/models/remote_address.rb +22 -0
- data/app/models/remote_line_item.rb +11 -0
- data/app/models/remote_order.rb +15 -0
- data/app/models/remote_shipping_method.rb +12 -0
- data/app/models/report.rb +18 -0
- data/app/models/report_invocation.rb +18 -0
- data/app/models/retail_store.rb +18 -0
- data/app/models/role.rb +6 -0
- data/app/models/search_suggestion.rb +17 -0
- data/app/models/section.rb +9 -0
- data/app/models/session.rb +8 -0
- data/app/models/shipping_method.rb +26 -0
- data/app/models/slug.rb +19 -0
- data/app/models/static_page.rb +60 -0
- data/app/models/static_page_folder.rb +8 -0
- data/app/models/stock_level.rb +21 -0
- data/app/models/tax_code.rb +6 -0
- data/app/models/taxonomy.rb +5 -0
- data/app/models/template.rb +9 -0
- data/app/models/template_component.rb +11 -0
- data/app/models/template_definition.rb +12 -0
- data/app/models/template_section.rb +12 -0
- data/app/models/user.rb +8 -0
- data/app/models/user_profile.rb +6 -0
- data/app/models/v2/create_order.rb +10 -0
- data/app/models/v2/deallocate_order.rb +10 -0
- data/app/models/v2/line_item.rb +9 -0
- data/app/models/v2/order.rb +9 -0
- data/app/models/v2/unallocate_order.rb +10 -0
- data/app/models/variant.rb +18 -0
- data/app/models/webhook.rb +17 -0
- data/app/services/param_to_shql.rb +72 -0
- data/app/services/surrogate_keys.rb +44 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/config/locales/payments.en.yml +3 -0
- data/flex-commerce-api.gemspec +41 -0
- data/lib/flex_commerce.rb +95 -0
- data/lib/flex_commerce_api.rb +21 -0
- data/lib/flex_commerce_api/api_base.rb +11 -0
- data/lib/flex_commerce_api/base_resource.rb +250 -0
- data/lib/flex_commerce_api/config.rb +55 -0
- data/lib/flex_commerce_api/error/access_denied.rb +6 -0
- data/lib/flex_commerce_api/error/bad_request.rb +10 -0
- data/lib/flex_commerce_api/error/base.rb +12 -0
- data/lib/flex_commerce_api/error/client_error.rb +7 -0
- data/lib/flex_commerce_api/error/connection_error.rb +6 -0
- data/lib/flex_commerce_api/error/internal_server.rb +37 -0
- data/lib/flex_commerce_api/error/not_found.rb +13 -0
- data/lib/flex_commerce_api/error/record_invalid.rb +16 -0
- data/lib/flex_commerce_api/error/unexpected_status.rb +7 -0
- data/lib/flex_commerce_api/errors.rb +13 -0
- data/lib/flex_commerce_api/json_api_client_extension/builder.rb +28 -0
- data/lib/flex_commerce_api/json_api_client_extension/capture_surrogate_keys_middleware.rb +16 -0
- data/lib/flex_commerce_api/json_api_client_extension/flexible_connection.rb +59 -0
- data/lib/flex_commerce_api/json_api_client_extension/has_many_association_proxy.rb +60 -0
- data/lib/flex_commerce_api/json_api_client_extension/included_data.rb +27 -0
- data/lib/flex_commerce_api/json_api_client_extension/json_format_middleware.rb +20 -0
- data/lib/flex_commerce_api/json_api_client_extension/logging_middleware.rb +24 -0
- data/lib/flex_commerce_api/json_api_client_extension/paginator.rb +26 -0
- data/lib/flex_commerce_api/json_api_client_extension/parse_json.rb +23 -0
- data/lib/flex_commerce_api/json_api_client_extension/parsers/parser.rb +16 -0
- data/lib/flex_commerce_api/json_api_client_extension/previewed_request_middleware.rb +17 -0
- data/lib/flex_commerce_api/json_api_client_extension/remote_builder.rb +29 -0
- data/lib/flex_commerce_api/json_api_client_extension/requestor.rb +42 -0
- data/lib/flex_commerce_api/json_api_client_extension/save_request_body_middleware.rb +20 -0
- data/lib/flex_commerce_api/json_api_client_extension/status_middleware.rb +40 -0
- data/lib/flex_commerce_api/v2/api_base.rb +13 -0
- data/lib/flex_commerce_api/version.rb +3 -0
- data/lib/json_erb.rb +9 -0
- data/lib/json_struct.rb +73 -0
- data/lib/patches.rb +4 -0
- data/lib/patches/json_api_client/resource.rb +50 -0
- data/lib/paypal_express.rb +3 -0
- data/lib/paypal_express/additional_info.rb +45 -0
- data/lib/paypal_express/api.rb +86 -0
- data/lib/paypal_express/auth.rb +83 -0
- data/lib/paypal_express/cart_shipping_method.rb +38 -0
- data/lib/paypal_express/exception/access_denied.rb +10 -0
- data/lib/paypal_express/exception/connection_error.rb +10 -0
- data/lib/paypal_express/exception/not_authorized.rb +10 -0
- data/lib/paypal_express/exception/transaction.rb +15 -0
- data/lib/paypal_express/generate_summary.rb +118 -0
- data/lib/paypal_express/process/paypal_params.rb +123 -0
- data/lib/paypal_express/process/response_parser.rb +146 -0
- data/lib/paypal_express/setup.rb +94 -0
- data/lib/paypal_express/shipping_methods_for_cart.rb +46 -0
- data/lib/retry.rb +20 -0
- data/schemas/jsonapi/schema.json +370 -0
- data/schemas/shift/v1/documents/collection/address.json +45 -0
- data/schemas/shift/v1/documents/collection/asset_file.json +43 -0
- data/schemas/shift/v1/documents/collection/asset_folder.json +43 -0
- data/schemas/shift/v1/documents/collection/customer_account.json +50 -0
- data/schemas/shift/v1/documents/collection/markdown_price.json +43 -0
- data/schemas/shift/v1/documents/collection/product.json +43 -0
- data/schemas/shift/v1/documents/collection/variant.json +43 -0
- data/schemas/shift/v1/documents/member/address.json +39 -0
- data/schemas/shift/v1/documents/member/asset_file.json +37 -0
- data/schemas/shift/v1/documents/member/asset_folder.json +39 -0
- data/schemas/shift/v1/documents/member/customer_account.json +44 -0
- data/schemas/shift/v1/documents/member/markdown_price.json +37 -0
- data/schemas/shift/v1/documents/member/product.json +39 -0
- data/schemas/shift/v1/documents/member/variant.json +46 -0
- data/schemas/shift/v1/resources/address.json +130 -0
- data/schemas/shift/v1/resources/asset_file.json +146 -0
- data/schemas/shift/v1/resources/asset_folder.json +188 -0
- data/schemas/shift/v1/resources/customer_account.json +339 -0
- data/schemas/shift/v1/resources/markdown_price.json +52 -0
- data/schemas/shift/v1/resources/product.json +230 -0
- data/schemas/shift/v1/resources/variant.json +298 -0
- data/tasks/json_schema.thor +275 -0
- data/todo.md +8 -0
- metadata +470 -0
data/.ruby-gemset
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
flex-ruby-gem
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.3.0
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015-2017 Shift Commerce Ltd
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
# Flex::Commerce::Api
|
2
|
+
|
3
|
+
Allows any ruby application to use the FlexCommerce platform using its API
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'flex-commerce-api'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install flex-commerce-api
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
The gem provides many models in the FlexCommerce namespace. The example below is a rails controller
|
24
|
+
accessing a list of products.
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
class ProductsController < ApplicationController
|
28
|
+
|
29
|
+
# GET /products
|
30
|
+
def index
|
31
|
+
@products = FlexCommerce::Product.paginate(params[:page])
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
```
|
36
|
+
|
37
|
+
To any rails developer this will look familiar.
|
38
|
+
|
39
|
+
However, we do not force you to use rails. We appreciate that there are many frameworks out there
|
40
|
+
and whilst rails is an excellent tool, for smaller projects you may want to look at others such
|
41
|
+
as sinatra etc...
|
42
|
+
|
43
|
+
|
44
|
+
## Development
|
45
|
+
|
46
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
|
47
|
+
|
48
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
49
|
+
|
50
|
+
## Contributing
|
51
|
+
|
52
|
+
1. Fork it ( https://github.com/[my-github-username]/flex-commerce-api/fork )
|
53
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
54
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
55
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
56
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require "flex_commerce_api/api_base"
|
2
|
+
module FlexCommerce
|
3
|
+
#
|
4
|
+
# A flex commerce Address model
|
5
|
+
#
|
6
|
+
# This model provides access to the flex commerce addresses
|
7
|
+
#
|
8
|
+
# It is used much like an active record model.
|
9
|
+
#
|
10
|
+
# Examples:
|
11
|
+
#
|
12
|
+
#
|
13
|
+
#
|
14
|
+
# # Fetching all addresses for a customer account
|
15
|
+
#
|
16
|
+
# customer_account.addresses.all #fetches all addresses(actually the first page in case there are thousands)
|
17
|
+
#
|
18
|
+
#
|
19
|
+
class Address < FlexCommerceApi::ApiBase
|
20
|
+
|
21
|
+
# @method all
|
22
|
+
# Returns all addresses
|
23
|
+
# @return [FlexCommerce::Address[]] An array of categories or an empty array
|
24
|
+
class << self
|
25
|
+
def path(params, instance = nil)
|
26
|
+
if params[:filter] && params[:filter].key?(:customer_account_id)
|
27
|
+
customer_account_id = params[:filter].delete(:customer_account_id)
|
28
|
+
params.delete(:filter) if params[:filter].empty?
|
29
|
+
"customer_accounts/#{customer_account_id}/addresses"
|
30
|
+
elsif instance && instance.try(:customer_account_id).try(:present?)
|
31
|
+
"customer_accounts/#{instance.customer_account_id}/addresses"
|
32
|
+
else
|
33
|
+
super
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
|
39
|
+
has_one :customer_account
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require "flex_commerce_api/api_base"
|
2
|
+
module FlexCommerce
|
3
|
+
#
|
4
|
+
# A flex commerce asset file model
|
5
|
+
#
|
6
|
+
# This model provides access to the flex asset file
|
7
|
+
#
|
8
|
+
# It is used much like an active record model.
|
9
|
+
#
|
10
|
+
class AssetFile < FlexCommerceApi::ApiBase
|
11
|
+
has_one :asset_folder
|
12
|
+
def self.path(params, resource)
|
13
|
+
internal_params = params.with_indifferent_access
|
14
|
+
if !internal_params.key?("asset_folder_id") && !internal_params.key?("path") && resource && resource.relationships["asset_folder"]["links"]["related"]=~/^\/asset_folders/
|
15
|
+
resource.relationships["asset_folder"]["links"]["related"].gsub(/\.json_api$/, '') + "/asset_files"
|
16
|
+
elsif internal_params.key?("path") && internal_params["path"].key?("asset_folder_id") && internal_params["path"]["asset_folder_id"].is_a?(String)
|
17
|
+
# As the asset_folder_id is going into the url, and the developer may sent it anything, then we should escape it
|
18
|
+
new_params = internal_params.deep_dup
|
19
|
+
new_params["path"]["asset_folder_id"] = URI.escape(new_params["path"]["asset_folder_id"])
|
20
|
+
super(new_params, resource)
|
21
|
+
else
|
22
|
+
super
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require "flex_commerce_api/api_base"
|
2
|
+
module FlexCommerce
|
3
|
+
#
|
4
|
+
# A flex commerce asset folder model
|
5
|
+
#
|
6
|
+
# This model provides access to the flex asset folder and associated files
|
7
|
+
#
|
8
|
+
# It is used much like an active record model.
|
9
|
+
#
|
10
|
+
class AssetFolder < FlexCommerceApi::ApiBase
|
11
|
+
has_many :asset_files
|
12
|
+
has_many :sub_folders, class_name: "::FlexCommerce::AssetFolder"
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require "flex_commerce_api/api_base"
|
2
|
+
module FlexCommerce
|
3
|
+
#
|
4
|
+
# A flex commerce Bundle model
|
5
|
+
#
|
6
|
+
# It is used much like an active record model.
|
7
|
+
#
|
8
|
+
# Examples:
|
9
|
+
#
|
10
|
+
# # Fetch all reports, paginated
|
11
|
+
# FlexCommerce::Bundle.all
|
12
|
+
#
|
13
|
+
# # Fetch by slug
|
14
|
+
# FlexCommerce::Bundle.find("slug:test")
|
15
|
+
#
|
16
|
+
class Bundle < FlexCommerceApi::ApiBase
|
17
|
+
has_many :bundle_groups, class_name: "::FlexCommerce::BundleGroup"
|
18
|
+
has_many :slugs, class_name: "::FlexCommerce::Slug"
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require "flex_commerce_api/api_base"
|
2
|
+
module FlexCommerce
|
3
|
+
#
|
4
|
+
# A flex commerce Bundle Group model
|
5
|
+
#
|
6
|
+
# This model provides access to the flex commerce bundle group
|
7
|
+
#
|
8
|
+
# It is generally used as a relationship of products rather than by itself
|
9
|
+
#
|
10
|
+
#
|
11
|
+
class BundleGroup < FlexCommerceApi::ApiBase
|
12
|
+
belongs_to :bundle, class_name: "::FlexCommerce::Bundle"
|
13
|
+
has_many :products, class_name: "::FlexCommerce::Product"
|
14
|
+
end
|
15
|
+
end
|
data/app/models/cart.rb
ADDED
@@ -0,0 +1,136 @@
|
|
1
|
+
require "flex_commerce_api/api_base"
|
2
|
+
module FlexCommerce
|
3
|
+
#
|
4
|
+
# A flex commerce Cart model
|
5
|
+
#
|
6
|
+
# This model provides access to the flex commerce cart and associated line_items.
|
7
|
+
# This model allows you to create a cart, update its line items and delete a cart.
|
8
|
+
#
|
9
|
+
# It is used much like an active record model.
|
10
|
+
#
|
11
|
+
# Examples:
|
12
|
+
#
|
13
|
+
#
|
14
|
+
#
|
15
|
+
# # Creating a cart
|
16
|
+
#
|
17
|
+
# FlexCommerce::Cart.create #creates and returns a new cart ready for use
|
18
|
+
#
|
19
|
+
# # Fetching its line items
|
20
|
+
#
|
21
|
+
# cart.line_items
|
22
|
+
#
|
23
|
+
# # Finding a cart
|
24
|
+
#
|
25
|
+
# FlexCommerce::Cart.find(<<cart_id>>) # Finds the cart with this unique id
|
26
|
+
#
|
27
|
+
#
|
28
|
+
class Cart < FlexCommerceApi::ApiBase
|
29
|
+
# @method find
|
30
|
+
# @param [String] spec
|
31
|
+
# Finds a cart
|
32
|
+
# @return [FlexCommerce::Cart] cart The cart
|
33
|
+
# @raise [FlexCommerceApi::Error::NotFound] If not found
|
34
|
+
|
35
|
+
# @method line_items
|
36
|
+
# Provides a list of associated line_items
|
37
|
+
# @return [FlexCommerce::LineItem[]]
|
38
|
+
|
39
|
+
# @TODO Document other popular methods that we will support
|
40
|
+
|
41
|
+
has_many :line_items, class_name: "::FlexCommerce::LineItem"
|
42
|
+
has_many :discount_summaries, class_name: "::FlexCommerce::DiscountSummary"
|
43
|
+
has_many :available_shipping_methods, class_name: "::FlexCommerceApi::ApiBase"
|
44
|
+
has_many :available_shipping_promotions, class_name: "::FlexCommerce::Promotion"
|
45
|
+
has_one :shipping_address, class_name: "::FlexCommerce::Address"
|
46
|
+
has_one :billing_address, class_name: "::FlexCommerce::Address"
|
47
|
+
has_one :shipping_method, class_name: "::FlexCommerce::ShippingMethod"
|
48
|
+
has_one :free_shipping_promotion, class_name: "::FlexCommerce::FreeShippingPromotion"
|
49
|
+
|
50
|
+
# properties
|
51
|
+
property :line_items_count, type: :integer, default: 0
|
52
|
+
property :total_discount, default: 0
|
53
|
+
|
54
|
+
# Here we override line_items to provide a proxy to the array so we can use new and create on it in the normal
|
55
|
+
# active record way
|
56
|
+
def line_items
|
57
|
+
has_many_association_proxy :line_items, (super || []), inverse_of: :container
|
58
|
+
end
|
59
|
+
|
60
|
+
def empty?
|
61
|
+
line_items_count == 0
|
62
|
+
end
|
63
|
+
|
64
|
+
# Merges another cart into this one using the API
|
65
|
+
# @param [FlexCommerce::Cart] other_cart The cart to merge from
|
66
|
+
def merge!(other_cart)
|
67
|
+
self.last_result_set = self.class.requestor.custom("merge", { request_method: :patch }, data: {type: "carts", attributes: { from_cart_id: other_cart.id, to_cart_id: id } } )
|
68
|
+
mark_as_persisted!
|
69
|
+
if updated = last_result_set.first
|
70
|
+
self.attributes = updated.attributes
|
71
|
+
self.relationships = updated.relationships
|
72
|
+
clear_changes_information
|
73
|
+
end
|
74
|
+
self
|
75
|
+
end
|
76
|
+
|
77
|
+
# This method is used when true stock levels re required - potentially from an external system
|
78
|
+
# To be used during checkout phases etc..
|
79
|
+
# Adds errors to the line items "unit_quantity" attribute if we do not have enough
|
80
|
+
def validate_stock!
|
81
|
+
return {} if empty?
|
82
|
+
stock_levels.each_with_object({}) do |stock_level, obj|
|
83
|
+
line_items.detect { |li| li.item.sku == stock_level.id }.tap do |li|
|
84
|
+
next if li.nil?
|
85
|
+
if stock_level.stock_available <= 0
|
86
|
+
obj[li.item.sku] = {
|
87
|
+
stock_level: 0,
|
88
|
+
line_item_quantity: li.unit_quantity,
|
89
|
+
message: "Out of stock"
|
90
|
+
}
|
91
|
+
elsif stock_level.stock_available < li.unit_quantity
|
92
|
+
obj[li.item.sku] = {
|
93
|
+
stock_level: stock_level.stock_available,
|
94
|
+
line_item_quantity: li.unit_quantity,
|
95
|
+
message: "Only #{stock_level.stock_available} in stock"
|
96
|
+
}
|
97
|
+
end
|
98
|
+
li.errors.add(:unit_quantity, obj.dig(li.item.sku, :message)) unless obj.dig(li.item.sku, :message).nil?
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
def add_payment_transaction(transaction)
|
104
|
+
self.class.requestor.custom("relationships/payment_transactions", {request_method: :post}, {id: id, data: [type: "payment_transactions", id: transaction.id.to_s]})
|
105
|
+
end
|
106
|
+
|
107
|
+
def self.create(args = {})
|
108
|
+
if FlexCommerceApi.config.order_test_mode
|
109
|
+
super(args.merge(test: true))
|
110
|
+
else
|
111
|
+
super
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
def available_shipping_methods
|
116
|
+
return super if relationships[:available_shipping_methods].key?("data")
|
117
|
+
shipping_methods = get_related(:available_shipping_methods).to_a
|
118
|
+
if shipping_methods.any? { |sm| sm.is_a?(FlexCommerce::RemoteShippingMethod) }
|
119
|
+
shipping_method_references = shipping_methods.map(&:reference)
|
120
|
+
# We are filtering in memory here as there will never be many shipping methods and they will almost certainly be in the cache anyway
|
121
|
+
FlexCommerce::ShippingMethod.all.select { |shipping_method| shipping_method_references.include?(shipping_method.reference)}
|
122
|
+
else
|
123
|
+
shipping_methods
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
|
128
|
+
private
|
129
|
+
|
130
|
+
def stock_levels
|
131
|
+
StockLevel.where(skus: line_items.map { |li| li.item.sku }.join(",")).all
|
132
|
+
end
|
133
|
+
|
134
|
+
|
135
|
+
end
|
136
|
+
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
require "flex_commerce_api/api_base"
|
2
|
+
module FlexCommerce
|
3
|
+
#
|
4
|
+
# A flex commerce Category model
|
5
|
+
#
|
6
|
+
# This model provides access to the flex commerce categories and associated products.
|
7
|
+
# As managing the categories is the job of the administration panel, this
|
8
|
+
# model is read only.
|
9
|
+
#
|
10
|
+
# It is used much like an active record model.
|
11
|
+
#
|
12
|
+
# Examples:
|
13
|
+
#
|
14
|
+
#
|
15
|
+
#
|
16
|
+
# # Fetching all categories
|
17
|
+
#
|
18
|
+
# FlexCommerce::Category.all #fetches all categories(actually the first page in case there are thousands)
|
19
|
+
#
|
20
|
+
# # Pagination
|
21
|
+
#
|
22
|
+
# FlexCommerce::Category.paginate(page:2).all # Fetches page 2 of categories.
|
23
|
+
# The page size is predefined on the server side
|
24
|
+
#
|
25
|
+
# # Finding categories
|
26
|
+
#
|
27
|
+
# FlexCommerce::Product.find("my-category-slug") # Finds the category with this unique id
|
28
|
+
#
|
29
|
+
# # Finding nested categories of the category
|
30
|
+
#
|
31
|
+
# FlexCommerce::Category.find("my-category-slug").categories
|
32
|
+
#
|
33
|
+
# # Finding products within the categories
|
34
|
+
#
|
35
|
+
# FlexCommerce::Category.find("my-category-slug").products
|
36
|
+
#
|
37
|
+
# or if you already know the category id - then
|
38
|
+
#
|
39
|
+
# @TODO Look into this
|
40
|
+
#
|
41
|
+
class Category < FlexCommerceApi::ApiBase
|
42
|
+
# @method find
|
43
|
+
# @param [String] spec
|
44
|
+
# Finds a category
|
45
|
+
# @return [FlexCommerce::Category] category The category
|
46
|
+
# @raise [FlexCommerceApi::Error::NotFound] If not found
|
47
|
+
|
48
|
+
# @method all
|
49
|
+
# Returns all categories
|
50
|
+
# @return [FlexCommerce::Category[]] An array of categories or an empty array
|
51
|
+
|
52
|
+
# @method paginate
|
53
|
+
# Paginates the list of categories by a preset page size
|
54
|
+
# @param [Hash] options The options to paginate with
|
55
|
+
# @param options [Numeric|String] :page The page to fetch
|
56
|
+
|
57
|
+
# @method categories
|
58
|
+
# Provides a list of associated sub categories
|
59
|
+
# @return [FlexCommerce::Category[]]
|
60
|
+
|
61
|
+
# @TODO Document other popular methods that we will support
|
62
|
+
|
63
|
+
has_many :categories, class_name: "::FlexCommerce::Category"
|
64
|
+
has_many :products, class_name: "::FlexCommerce::Product"
|
65
|
+
has_many :slugs, class_name: "::FlexCommerce::Slug"
|
66
|
+
belongs_to :category_tree, class_name: "::FlexCommerce::CategoryTree"
|
67
|
+
has_one :template_definition, class_name: "::FlexCommerce::TemplateDefinition"
|
68
|
+
has_one :template, class_name: "::FlexCommerce::Template"
|
69
|
+
end
|
70
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
require "flex_commerce_api/api_base"
|
2
|
+
module FlexCommerce
|
3
|
+
#
|
4
|
+
# A flex commerce Category tree model
|
5
|
+
#
|
6
|
+
# This model provides access to the flex commerce category tree and associated categories.
|
7
|
+
class CategoryTree < FlexCommerceApi::ApiBase
|
8
|
+
has_many :slugs, class_name: "::FlexCommerce::Slug"
|
9
|
+
has_many :categories, class_name: "::FlexCommerce::Category"
|
10
|
+
end
|
11
|
+
end
|