spree_core 6.0.4 → 6.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/app/jobs/spree/imports/create_rows_job.rb +4 -2
- data/app/jobs/spree/imports/process_rows_job.rb +4 -2
- data/app/jobs/spree/payments/handle_modification_webhook_job.rb +93 -0
- data/app/jobs/spree/payments/handle_webhook_job.rb +4 -2
- data/app/jobs/spree/products/auto_match_taxons_job.rb +5 -2
- data/app/jobs/spree/search_provider/index_job.rb +8 -0
- data/app/jobs/spree/search_provider/remove_job.rb +3 -0
- data/app/jobs/spree/stock_locations/stock_items/create_job.rb +5 -2
- data/app/mailers/spree/base_mailer.rb +1 -1
- data/app/maintenance_tasks/spree/maintenance_tasks/upgrade/backfill_settlement_statuses.rb +40 -0
- data/app/models/concerns/spree/acted_by.rb +2 -2
- data/app/models/concerns/spree/customer_methods.rb +7 -5
- data/app/models/concerns/spree/deprecated_customer_alias.rb +11 -3
- data/app/models/concerns/spree/derived_number.rb +1 -1
- data/app/models/concerns/spree/has_custom_fields.rb +12 -10
- data/app/models/concerns/spree/integrations_concern.rb +5 -3
- data/app/models/concerns/spree/label_translatable.rb +4 -4
- data/app/models/concerns/spree/metadata.rb +3 -3
- data/app/models/concerns/spree/presentation_translatable.rb +1 -1
- data/app/models/concerns/spree/product_scopes.rb +5 -2
- data/app/models/concerns/spree/purchase/addresses.rb +8 -5
- data/app/models/concerns/spree/purchase/deprecated_aliases.rb +2 -2
- data/app/models/concerns/spree/purchase/payment_processing.rb +11 -0
- data/app/models/concerns/spree/purchase/purchase_order.rb +21 -0
- data/app/models/concerns/spree/purchase/totals.rb +3 -2
- data/app/models/concerns/spree/ransackable_attributes.rb +81 -4
- data/app/models/concerns/spree/settlement_status.rb +29 -0
- data/app/models/concerns/spree/stores/markets.rb +4 -32
- data/app/models/concerns/spree/user_address.rb +5 -2
- data/app/models/concerns/spree/user_methods.rb +2 -2
- data/app/models/spree/address.rb +29 -14
- data/app/models/spree/asset.rb +3 -3
- data/app/models/spree/base.rb +4 -4
- data/app/models/spree/calculator/shipping/flat_rate.rb +6 -4
- data/app/models/spree/cart.rb +2 -2
- data/app/models/spree/category.rb +61 -38
- data/app/models/spree/classification.rb +2 -2
- data/app/models/spree/collection_rule.rb +5 -1
- data/app/models/spree/current.rb +19 -0
- data/app/models/spree/custom_field.rb +6 -6
- data/app/models/spree/custom_field_definition.rb +10 -7
- data/app/models/spree/delivery_method.rb +21 -15
- data/app/models/spree/delivery_rate.rb +1 -1
- data/app/models/spree/digital.rb +1 -1
- data/app/models/spree/digital_asset.rb +2 -2
- data/app/models/spree/digital_link.rb +4 -4
- data/app/models/spree/export.rb +32 -3
- data/app/models/spree/exports/products.rb +9 -1
- data/app/models/spree/fulfillment/custom_events.rb +18 -10
- data/app/models/spree/fulfillment.rb +53 -29
- data/app/models/spree/fulfillment_group.rb +2 -2
- data/app/models/spree/fulfillment_item.rb +3 -3
- data/app/models/spree/fulfilment_changer.rb +1 -1
- data/app/models/spree/gateway/bogus.rb +41 -2
- data/app/models/spree/gift_card.rb +26 -14
- data/app/models/spree/import.rb +23 -15
- data/app/models/spree/import_row.rb +6 -6
- data/app/models/spree/inventory_unit.rb +1 -1
- data/app/models/spree/invitation.rb +8 -4
- data/app/models/spree/line_item.rb +10 -8
- data/app/models/spree/media.rb +3 -3
- data/app/models/spree/metafield.rb +1 -1
- data/app/models/spree/metafield_definition.rb +1 -1
- data/app/models/spree/metafields.rb +2 -2
- data/app/models/spree/option_type.rb +27 -0
- data/app/models/spree/option_value.rb +3 -3
- data/app/models/spree/order.rb +97 -49
- data/app/models/spree/order_group.rb +2 -2
- data/app/models/spree/order_inventory.rb +2 -2
- data/app/models/spree/order_merger.rb +2 -2
- data/app/models/spree/order_updater.rb +34 -20
- data/app/models/spree/payment/custom_events.rb +2 -1
- data/app/models/spree/payment/processing.rb +20 -3
- data/app/models/spree/payment.rb +194 -15
- data/app/models/spree/payment_capture_event.rb +2 -0
- data/app/models/spree/payment_method/store_credit.rb +6 -0
- data/app/models/spree/payment_method.rb +65 -11
- data/app/models/spree/payment_response.rb +19 -0
- data/app/models/spree/payment_session.rb +15 -3
- data/app/models/spree/payment_split.rb +14 -2
- data/app/models/spree/price.rb +20 -8
- data/app/models/spree/price_list.rb +12 -6
- data/app/models/spree/price_rules/zone_rule.rb +1 -1
- data/app/models/spree/product.rb +30 -28
- data/app/models/spree/product_category.rb +3 -3
- data/app/models/spree/promotion/rules/category.rb +27 -10
- data/app/models/spree/promotion/rules/country.rb +1 -1
- data/app/models/spree/promotion/rules/taxon.rb +4 -2
- data/app/models/spree/promotion/rules/user.rb +1 -1
- data/app/models/spree/promotion_action.rb +1 -1
- data/app/models/spree/promotion_rule_category.rb +5 -5
- data/app/models/spree/promotion_rule_taxon.rb +2 -2
- data/app/models/spree/prototype.rb +4 -1
- data/app/models/spree/purchase_order.rb +20 -3
- data/app/models/spree/refund.rb +7 -1
- data/app/models/spree/return.rb +7 -1
- data/app/models/spree/return_authorization_reason.rb +3 -3
- data/app/models/spree/return_reason.rb +1 -1
- data/app/models/spree/search_provider/base.rb +12 -2
- data/app/models/spree/search_provider/database.rb +8 -5
- data/app/models/spree/search_provider.rb +7 -5
- data/app/models/spree/shipment.rb +4 -1
- data/app/models/spree/shipping_method.rb +4 -1
- data/app/models/spree/shipping_rate.rb +1 -1
- data/app/models/spree/stock/coordinator.rb +2 -2
- data/app/models/spree/stock/estimator.rb +7 -5
- data/app/models/spree/stock/package.rb +10 -8
- data/app/models/spree/stock_item.rb +1 -1
- data/app/models/spree/stock_level/custom_events.rb +1 -1
- data/app/models/spree/stock_level.rb +1 -1
- data/app/models/spree/stock_location.rb +12 -8
- data/app/models/spree/stock_movement.rb +6 -6
- data/app/models/spree/stock_reservation.rb +5 -5
- data/app/models/spree/store.rb +64 -16
- data/app/models/spree/store_credit.rb +3 -3
- data/app/models/spree/store_credit_category.rb +5 -5
- data/app/models/spree/store_credit_type.rb +2 -2
- data/app/models/spree/subscriber.rb +7 -7
- data/app/models/spree/tax_rate.rb +5 -5
- data/app/models/spree/taxon.rb +3 -3
- data/app/models/spree/taxon_rule.rb +2 -2
- data/app/models/spree/taxonomy.rb +1 -1
- data/app/models/spree/variant.rb +45 -10
- data/app/models/spree/variant_media.rb +1 -1
- data/app/models/spree/wished_item.rb +1 -1
- data/app/models/spree/wishlist.rb +2 -2
- data/app/models/spree/wishlist_item/custom_events.rb +2 -2
- data/app/models/spree/zone.rb +1 -1
- data/app/models/spree/zone_member.rb +1 -1
- data/app/presenters/spree/csv/product_variant_presenter.rb +17 -8
- data/app/serializers/spree/emails/store_serializer.rb +1 -1
- data/app/services/spree/carts/associate.rb +6 -2
- data/app/services/spree/carts/create.rb +1 -1
- data/app/services/spree/carts/destroy.rb +1 -1
- data/app/services/spree/carts/remove_item.rb +1 -1
- data/app/services/spree/carts/remove_line_item.rb +2 -2
- data/app/services/spree/carts/remove_out_of_stock_items.rb +3 -1
- data/app/services/spree/carts/set_quantity.rb +2 -2
- data/app/services/spree/carts/split_by_seller.rb +4 -13
- data/app/services/spree/carts/update.rb +17 -0
- data/app/services/spree/checkout/add_store_credit.rb +3 -3
- data/app/services/spree/checkout/remove_store_credit.rb +2 -2
- data/app/services/spree/imports/row_processors/product_variant.rb +19 -0
- data/app/services/spree/orders/approve.rb +3 -3
- data/app/services/spree/orders/create_from_cart.rb +2 -0
- data/app/services/spree/orders/create_user_account.rb +2 -2
- data/app/services/spree/orders/update_statuses.rb +4 -2
- data/app/services/spree/products/auto_match_taxons.rb +5 -2
- data/app/services/spree/shipments/update.rb +2 -2
- data/app/services/spree/stock_locations/stock_items/create.rb +5 -2
- data/app/services/spree/stock_reservations/extend.rb +3 -1
- data/app/services/spree/stock_reservations/reserve.rb +3 -1
- data/app/services/spree/store_credits/apply.rb +1 -1
- data/app/services/spree/taxons/add_products.rb +4 -2
- data/app/services/spree/taxons/regenerate_products.rb +5 -2
- data/app/services/spree/taxons/remove_products.rb +4 -2
- data/app/subscribers/spree/order_status_subscriber.rb +4 -3
- data/app/subscribers/spree/payment_split_subscriber.rb +39 -21
- data/app/subscribers/spree/seller_transfer_reversal_subscriber.rb +19 -7
- data/app/tools/spree/agent_tools/query_report.rb +116 -14
- data/app/workflows/spree/carts/add_item.rb +1 -1
- data/app/workflows/spree/carts/complete.rb +1 -19
- data/app/workflows/spree/carts/recalculate.rb +1 -1
- data/app/workflows/spree/carts/recalculate_totals.rb +3 -3
- data/app/workflows/spree/fulfillments/update.rb +1 -1
- data/app/workflows/spree/orders/cancel.rb +22 -1
- data/app/workflows/spree/orders/complete.rb +7 -1
- data/app/workflows/spree/payments/capture.rb +101 -7
- data/app/workflows/spree/payments/handle_modification_webhook.rb +218 -0
- data/app/workflows/spree/payments/handle_webhook.rb +7 -1
- data/app/workflows/spree/payments/process.rb +47 -4
- data/app/workflows/spree/payments/void.rb +4 -1
- data/app/workflows/spree/products/approve.rb +17 -0
- data/app/workflows/spree/refunds/create.rb +10 -1
- data/app/workflows/spree/refunds/order_payments.rb +3 -1
- data/app/workflows/spree/seller_transfers/create.rb +1 -1
- data/config/locales/be.yml +2 -2
- data/config/locales/bg.yml +4 -4
- data/config/locales/en.yml +12 -0
- data/config/locales/ru.yml +3 -3
- data/db/migrate/20260710000001_rename_taxons_to_categories.rb +1 -1
- data/db/migrate/20260728000004_rename_shipping_to_fulfillment_and_delivery.rb +1 -1
- data/db/migrate/20260806000001_add_store_id_to_spree_tax_rates_and_categories.rb +1 -1
- data/db/migrate/20260807130001_add_country_state_codes_to_consumers.rb +1 -1
- data/db/migrate/20260809000001_rename_metafields_to_custom_fields.rb +1 -1
- data/db/migrate/20260809150001_promote_shipping_categories_to_delivery_profiles.rb +1 -1
- data/db/migrate/20260811000001_add_delivery_tracking_to_fulfillments.rb +1 -1
- data/db/migrate/20260824000001_add_store_to_spree_media.rb +1 -1
- data/db/migrate/20260828100001_unify_import_and_export_tenancy.rb +1 -1
- data/db/migrate/20261016000001_remove_default_from_spree_tax_rates_included_in_price.rb +1 -1
- data/db/migrate/20261116100001_add_status_to_spree_payment_capture_events_and_refunds.rb +17 -0
- data/lib/spree/agent_tool.rb +11 -4
- data/lib/spree/agent_tools/default_catalog.rb +2 -2
- data/lib/spree/agent_tools/legacy_tools.rb +1 -1
- data/lib/spree/core/dependencies.rb +11 -10
- data/lib/spree/core/engine.rb +4 -0
- data/lib/spree/core/number_generator.rb +4 -3
- data/lib/spree/core/preferences/preferable.rb +1 -1
- data/lib/spree/core/preferences/preferable_class_methods.rb +5 -1
- data/lib/spree/core/pricing/resolver.rb +2 -2
- data/lib/spree/core/version.rb +1 -1
- data/lib/spree/core.rb +37 -10
- data/lib/spree/i18n.rb +6 -2
- data/lib/spree/localized_number.rb +1 -1
- data/lib/spree/removed_apis.rb +274 -0
- data/lib/spree/reporting/adapters/live.rb +69 -8
- data/lib/spree/reporting/default_vocabulary.rb +11 -5
- data/lib/spree/reporting/query.rb +4 -1
- data/lib/spree/reporting/registry.rb +50 -3
- data/lib/spree/testing_support/factories/digital_asset_factory.rb +1 -1
- data/lib/spree/testing_support/factories/payment_capture_event_factory.rb +10 -0
- data/lib/spree/testing_support/factories/refund_factory.rb +10 -0
- data/lib/spree/testing_support/factories/stock_level_factory.rb +1 -1
- data/lib/spree/testing_support/factories/taxon_factory.rb +1 -1
- data/lib/spree/testing_support/factories/taxonomy_factory.rb +1 -1
- data/lib/spree/testing_support/factories/wishlist_item_factory.rb +1 -1
- data/lib/spree/testing_support/i18n.rb +7 -2
- data/lib/spree/testing_support/order_walkthrough.rb +5 -2
- data/lib/spree/upgrades/5_6_to_6_0/manifest.yml +71 -14
- data/lib/tasks/carts_migration.rake +1 -1
- data/lib/tasks/categories_and_collections.rake +1 -1
- data/lib/tasks/deliveries_migration.rake +1 -1
- data/lib/tasks/delivery_method_rules_migration.rake +1 -1
- data/lib/tasks/delivery_migration.rake +1 -1
- data/lib/tasks/delivery_profiles_migration.rake +2 -2
- data/lib/tasks/fold_store_credit_categories.rake +1 -1
- data/lib/tasks/migrate_country_state_codes.rake +1 -1
- data/lib/tasks/migrate_returns.rake +4 -4
- data/lib/tasks/migrate_rich_text_to_columns.rake +1 -1
- data/lib/tasks/store_settings.rake +1 -1
- data/lib/tasks/typed_stock_movements_migration.rake +1 -1
- metadata +26 -6
|
@@ -13,12 +13,16 @@ module Spree
|
|
|
13
13
|
|
|
14
14
|
param :metrics, type: :array, items: :string,
|
|
15
15
|
description: 'Metric names, e.g. ["orders", "net_sales"]', required: true
|
|
16
|
-
param :dimensions, type: :array, items:
|
|
16
|
+
param :dimensions, type: :array, items: %i[string object],
|
|
17
17
|
description: 'Dimensions to group by, as named by describe_reporting ' \
|
|
18
|
-
'(e.g. ["completed_at", "channel"]).
|
|
18
|
+
'(e.g. ["completed_at", "channel"]). A date dimension takes a grain as ' \
|
|
19
|
+
'{"name": "completed_at", "grain": "month"} (hour, day, week or month); ' \
|
|
20
|
+
'without one, the grain is picked to fit the time range. Omit for a single total.',
|
|
19
21
|
required: false
|
|
20
22
|
param :time_range, type: :object,
|
|
21
|
-
description: 'A preset such as {"preset": "last_30_days"}
|
|
23
|
+
description: 'A preset such as {"preset": "last_30_days"} or {"preset": "year_to_date"}, ' \
|
|
24
|
+
'or dates as {"since": "2026-01-01", "until": "2026-03-31"}. ' \
|
|
25
|
+
'Defaults to the last 30 days.',
|
|
22
26
|
required: false
|
|
23
27
|
param :filters, type: :array, items: :object,
|
|
24
28
|
description: 'Dimension filters, each {"dimension": …, "op": "eq" or "in", "value": …} — ' \
|
|
@@ -32,15 +36,38 @@ module Spree
|
|
|
32
36
|
# returned thousands is capped rather than truncated silently.
|
|
33
37
|
DEFAULT_LIMIT = 50
|
|
34
38
|
MAX_LIMIT = 500
|
|
39
|
+
# A date dimension given without a grain gets the finest one that keeps
|
|
40
|
+
# the series about this long: a month by day, a quarter by week, a year
|
|
41
|
+
# by month. The reporting default, hour, fits almost no question.
|
|
42
|
+
TARGET_POINTS = 31
|
|
43
|
+
# Models often write a range as from/to; both are read as since/until.
|
|
44
|
+
TIME_RANGE_ALIASES = { 'from' => 'since', 'to' => 'until' }.freeze
|
|
45
|
+
TIME_RANGE_KEYS = %w[preset since until].freeze
|
|
35
46
|
|
|
36
47
|
def call(metrics:, dimensions: nil, time_range: nil, filters: nil, sort: nil, limit: nil, currency: nil)
|
|
37
|
-
|
|
38
|
-
|
|
48
|
+
row_limit = [(limit || DEFAULT_LIMIT).to_i, MAX_LIMIT].min
|
|
49
|
+
raise Spree::Reporting::InvalidQuery, "limit must be between 1 and #{MAX_LIMIT}" if row_limit < 1
|
|
50
|
+
|
|
51
|
+
# An unsorted date series is reported whole or not at all: cut at the
|
|
52
|
+
# limit, it would end early next to totals for the whole range. A
|
|
53
|
+
# sorted one ("the five best days") is a ranking, cut on purpose.
|
|
54
|
+
dimensions = Array(dimensions).map { |entry| entry.is_a?(Hash) ? entry.stringify_keys : entry }
|
|
55
|
+
series = sort.blank? && dimensions.any? { |entry| time_dimension_for(entry) }
|
|
56
|
+
|
|
57
|
+
query = build_query(metrics: metrics, dimensions: dimensions, time_range: time_range, filters: filters,
|
|
58
|
+
sort: sort, currency: currency, limit: (row_limit unless series),
|
|
59
|
+
target_points: series && dimensions.one? ? [TARGET_POINTS, row_limit].min : TARGET_POINTS)
|
|
39
60
|
|
|
40
61
|
refusal = unauthorized_members(query)
|
|
41
62
|
return refusal if refusal
|
|
42
63
|
|
|
43
|
-
|
|
64
|
+
result = query.execute
|
|
65
|
+
if series && Array(result.rows).size > row_limit
|
|
66
|
+
return { error: "This report has #{result.rows.size} rows, more than the limit of #{row_limit}. " \
|
|
67
|
+
"Shorten the range, use a coarser grain, or raise limit (up to #{MAX_LIMIT})." }
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
shape(result)
|
|
44
71
|
rescue Spree::Reporting::UnknownMember => e
|
|
45
72
|
{ error: e.message, valid: Array(e.valid).map(&:to_s) }
|
|
46
73
|
rescue Spree::Reporting::InvalidQuery => e
|
|
@@ -56,20 +83,95 @@ module Spree
|
|
|
56
83
|
|
|
57
84
|
private
|
|
58
85
|
|
|
59
|
-
def build_query(metrics:, dimensions:, time_range:, filters:, sort:, limit:,
|
|
60
|
-
parameters = {
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
'filters' => Array(filters),
|
|
64
|
-
'limit' => [(limit || DEFAULT_LIMIT).to_i, MAX_LIMIT].min
|
|
65
|
-
}
|
|
66
|
-
parameters['time_range'] = time_range if time_range.present?
|
|
86
|
+
def build_query(metrics:, dimensions:, time_range:, filters:, sort:, currency:, limit:, target_points:)
|
|
87
|
+
parameters = { 'metrics' => Array(metrics), 'filters' => Array(filters) }
|
|
88
|
+
parameters['limit'] = limit if limit
|
|
89
|
+
parameters['time_range'] = normalize_time_range(time_range) if time_range.present?
|
|
67
90
|
parameters['sort'] = sort if sort.present?
|
|
68
91
|
parameters['currency'] = currency if currency.present?
|
|
69
92
|
|
|
93
|
+
ungrained = dimensions.index { |entry| ungrained_time_dimension(entry) }
|
|
94
|
+
return reporting_query(parameters.merge('dimensions' => dimensions)) if ungrained.nil?
|
|
95
|
+
|
|
96
|
+
# The range decides the grain, and only the query resolves the range
|
|
97
|
+
# (presets, the store's timezone), so it is built once at the coarsest
|
|
98
|
+
# grain to read the range back, then again at the grain that fits.
|
|
99
|
+
entry = dimensions[ungrained]
|
|
100
|
+
dimension = ungrained_time_dimension(entry)
|
|
101
|
+
entry = { 'name' => entry } unless entry.is_a?(Hash)
|
|
102
|
+
draft = dimensions.dup
|
|
103
|
+
draft[ungrained] = entry.merge('grain' => dimension.grains.last.to_s)
|
|
104
|
+
query = reporting_query(parameters.merge('dimensions' => draft))
|
|
105
|
+
|
|
106
|
+
draft[ungrained] = entry.merge('grain' => fitting_grain(dimension, query, target_points).to_s)
|
|
107
|
+
reporting_query(parameters.merge('dimensions' => draft))
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
def reporting_query(parameters)
|
|
70
111
|
Spree::Reporting::Query.new(store: context.store, params: parameters)
|
|
71
112
|
end
|
|
72
113
|
|
|
114
|
+
# The registry's time dimension an entry names, or nil. Anything else is
|
|
115
|
+
# left for the query to accept or refuse.
|
|
116
|
+
def time_dimension_for(entry)
|
|
117
|
+
name = entry.is_a?(Hash) ? entry['name'] : entry
|
|
118
|
+
return unless name.is_a?(String) || name.is_a?(Symbol)
|
|
119
|
+
|
|
120
|
+
dimension = Spree.reporting.dimensions[name.to_sym]
|
|
121
|
+
dimension if dimension&.time?
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
def ungrained_time_dimension(entry)
|
|
125
|
+
return if entry.is_a?(Hash) && ['', nil].exclude?(entry['grain'])
|
|
126
|
+
|
|
127
|
+
time_dimension_for(entry)
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
def fitting_grain(dimension, query, target_points)
|
|
131
|
+
dimension.grains.find { |grain| (bucket_count(query, grain) || Float::INFINITY) <= target_points } ||
|
|
132
|
+
dimension.grains.last
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
# Counted on the store's calendar, as the adapter buckets them, so a
|
|
136
|
+
# daylight saving change does not push 31 days over the target.
|
|
137
|
+
def bucket_count(query, grain)
|
|
138
|
+
first = query.time_range.first.in_time_zone(query.time_zone)
|
|
139
|
+
last = query.time_range.last.in_time_zone(query.time_zone)
|
|
140
|
+
|
|
141
|
+
case grain
|
|
142
|
+
when :hour then ((last - first.beginning_of_hour) / 1.hour).floor + 1
|
|
143
|
+
when :day then (last.to_date - first.to_date).to_i + 1
|
|
144
|
+
when :week then ((last.to_date.beginning_of_week(:monday) - first.to_date.beginning_of_week(:monday)).to_i / 7) + 1
|
|
145
|
+
when :month then ((last.year * 12) + last.month) - ((first.year * 12) + first.month) + 1
|
|
146
|
+
end
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
# A range in a shape the query does not read would be dropped there for
|
|
150
|
+
# the last 30 days, and the model would report on the wrong period.
|
|
151
|
+
def normalize_time_range(range)
|
|
152
|
+
return range unless range.is_a?(Hash)
|
|
153
|
+
|
|
154
|
+
range = range.to_h.stringify_keys
|
|
155
|
+
TIME_RANGE_ALIASES.each do |alias_key, key|
|
|
156
|
+
next unless range.key?(alias_key)
|
|
157
|
+
raise Spree::Reporting::InvalidQuery, "time_range takes #{alias_key} or #{key}, not both" if range.key?(key)
|
|
158
|
+
|
|
159
|
+
range[key] = range.delete(alias_key)
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
if range.key?('preset') && range.keys.intersect?(%w[since until])
|
|
163
|
+
raise Spree::Reporting::InvalidQuery, 'time_range takes a preset or since and until, not both'
|
|
164
|
+
end
|
|
165
|
+
|
|
166
|
+
unknown = range.keys - TIME_RANGE_KEYS
|
|
167
|
+
if unknown.any?
|
|
168
|
+
raise Spree::Reporting::InvalidQuery,
|
|
169
|
+
"time_range does not take #{unknown.join(', ')}. Use {\"preset\": …} or {\"since\": …, \"until\": …}."
|
|
170
|
+
end
|
|
171
|
+
|
|
172
|
+
range
|
|
173
|
+
end
|
|
174
|
+
|
|
73
175
|
# The same per-member check the reporting endpoint makes, in the same
|
|
74
176
|
# two flavours: a key is checked against its scopes, an admin against
|
|
75
177
|
# their ability. A caller who may read orders but not products cannot
|
|
@@ -16,7 +16,7 @@ module Spree
|
|
|
16
16
|
# surface only (draft orders); refused once the order is placed.
|
|
17
17
|
def perform(variant:, cart: nil, order: nil, quantity: nil, metadata: {}, options: {}, price: nil)
|
|
18
18
|
if order
|
|
19
|
-
Spree::Deprecation.warn('Calling Spree::Carts::AddItem with order: is deprecated and will be removed in Spree 6.
|
|
19
|
+
Spree::Deprecation.warn('Calling Spree::Carts::AddItem with order: is deprecated and will be removed in Spree 6.3. Pass cart: instead.')
|
|
20
20
|
cart ||= order
|
|
21
21
|
end
|
|
22
22
|
super(cart: cart, variant: variant, quantity: quantity, metadata: metadata, options: options, price: price)
|
|
@@ -295,7 +295,7 @@ module Spree
|
|
|
295
295
|
copy_promotions!(cart, order)
|
|
296
296
|
copy_tax_identifier!(cart, order)
|
|
297
297
|
copy_tax_exemptions!(cart, order, line_item_map, fulfillment_map, fee_map)
|
|
298
|
-
|
|
298
|
+
order.copy_po_document_from!(cart)
|
|
299
299
|
repoint_money_records!(cart, order)
|
|
300
300
|
Spree.cart_copiers.each { |copier| copier.call(cart: cart, order: order, line_item_map: line_item_map) }
|
|
301
301
|
|
|
@@ -403,24 +403,6 @@ module Spree
|
|
|
403
403
|
)
|
|
404
404
|
end
|
|
405
405
|
|
|
406
|
-
# The buyer's purchase order follows the number onto the order. The same
|
|
407
|
-
# blob is attached to both records rather than uploaded twice — the cart
|
|
408
|
-
# keeps its copy so a completion that rolls back leaves the buyer's
|
|
409
|
-
# upload where they left it.
|
|
410
|
-
#
|
|
411
|
-
# The order is persisted and unchanged here, so `attach` saves
|
|
412
|
-
# immediately and answers nil instead of raising when the save is
|
|
413
|
-
# refused. Left unchecked the placed order would simply have no
|
|
414
|
-
# paperwork, silently — so the failure is raised into the completion
|
|
415
|
-
# transaction rather than dropped.
|
|
416
|
-
def copy_po_document!(cart, order)
|
|
417
|
-
return unless cart.po_document.attached?
|
|
418
|
-
|
|
419
|
-
return if order.po_document.attach(cart.po_document.blob)
|
|
420
|
-
|
|
421
|
-
raise ActiveRecord::RecordInvalid, order
|
|
422
|
-
end
|
|
423
|
-
|
|
424
406
|
# Which link of the chain produced the snapshot, so a placed order's tax
|
|
425
407
|
# treatment names its own reason.
|
|
426
408
|
def source_of(resolved)
|
|
@@ -13,7 +13,7 @@ module Spree
|
|
|
13
13
|
# for callers that are removing a promotion's effects
|
|
14
14
|
def perform(cart: nil, order: nil, line_item: nil, line_item_created: false, options: {})
|
|
15
15
|
if order
|
|
16
|
-
Spree::Deprecation.warn('Calling Spree::Carts::Recalculate with order: is deprecated and will be removed in Spree 6.
|
|
16
|
+
Spree::Deprecation.warn('Calling Spree::Carts::Recalculate with order: is deprecated and will be removed in Spree 6.3. Pass cart: instead.')
|
|
17
17
|
cart ||= order
|
|
18
18
|
end
|
|
19
19
|
super(cart: cart, line_item: line_item, line_item_created: line_item_created, options: options)
|
|
@@ -143,9 +143,9 @@ module Spree
|
|
|
143
143
|
return cart.payment_splits.reload.to_a.sum(&:net_captured_amount)
|
|
144
144
|
end
|
|
145
145
|
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
146
|
+
# Loaded once, so refunds are subtracted for exactly the payments summed.
|
|
147
|
+
completed = cart.payments.completed.to_a
|
|
148
|
+
completed.sum(0.to_d, &:amount) - Spree::Refund.completed_or_unset.where(payment_id: completed.map(&:id)).sum(:amount)
|
|
149
149
|
end
|
|
150
150
|
|
|
151
151
|
def refresh_grand_total
|
|
@@ -32,7 +32,7 @@ module Spree
|
|
|
32
32
|
if shipment || shipment_attributes
|
|
33
33
|
Spree::Deprecation.warn(
|
|
34
34
|
'Calling Spree::Fulfillments::Update with shipment:/shipment_attributes: keywords is deprecated ' \
|
|
35
|
-
'and will be removed in Spree 6.
|
|
35
|
+
'and will be removed in Spree 6.3. Use fulfillment:/fulfillment_attributes: instead.'
|
|
36
36
|
)
|
|
37
37
|
end
|
|
38
38
|
|
|
@@ -33,7 +33,9 @@ module Spree
|
|
|
33
33
|
super
|
|
34
34
|
|
|
35
35
|
unless restock_items.nil?
|
|
36
|
-
Spree::Deprecation.warn(
|
|
36
|
+
Spree::Deprecation.warn(
|
|
37
|
+
'Spree::Orders::Cancel no longer accepts restock_items — it never changed behavior and will be removed in Spree 6.3.'
|
|
38
|
+
)
|
|
37
39
|
end
|
|
38
40
|
|
|
39
41
|
step :ensure_cancellable
|
|
@@ -53,6 +55,11 @@ module Spree
|
|
|
53
55
|
@amount_to_refund ||= amount_paid if refund_payments
|
|
54
56
|
|
|
55
57
|
ApplicationRecord.transaction do
|
|
58
|
+
# The row lock a capture takes to record its answer, so a capture
|
|
59
|
+
# cannot be recorded between this check and the cancel. Taken by
|
|
60
|
+
# query: the order may carry figures not yet saved.
|
|
61
|
+
Spree::Order.lock.find(order.id)
|
|
62
|
+
step :ensure_no_payment_in_flight
|
|
56
63
|
step :mark_canceled
|
|
57
64
|
step :cancel_fulfillments
|
|
58
65
|
run_hooks :after_cancel
|
|
@@ -90,6 +97,20 @@ module Spree
|
|
|
90
97
|
failure(order)
|
|
91
98
|
end
|
|
92
99
|
|
|
100
|
+
# Voiding a payment whose capture or void is in flight, or that the
|
|
101
|
+
# provider is still answering, would release a hold it may be capturing.
|
|
102
|
+
def ensure_no_payment_in_flight
|
|
103
|
+
payments = if order.grouped?
|
|
104
|
+
Spree::Payment.where(id: order.payment_splits.select(:payment_id))
|
|
105
|
+
else
|
|
106
|
+
order.payments
|
|
107
|
+
end
|
|
108
|
+
return unless payments.with_status(:processing, :requested).exists?
|
|
109
|
+
|
|
110
|
+
order.errors.add(:base, :payment_requested, message: I18n.t('spree.payment_requested_awaiting_provider'))
|
|
111
|
+
failure(order)
|
|
112
|
+
end
|
|
113
|
+
|
|
93
114
|
# A reason from another store would label this order with a vocabulary
|
|
94
115
|
# its merchant never wrote — the same scoping rule the controllers apply
|
|
95
116
|
# to every incidental id, enforced here so console and extension callers
|
|
@@ -305,9 +305,15 @@ module Spree
|
|
|
305
305
|
failure(order, result.error) if result.failure?
|
|
306
306
|
end
|
|
307
307
|
|
|
308
|
+
# Net of refunds on captured payments, as Payment#amount_owed_by_order
|
|
309
|
+
# counts them: money given back is owed again, except what a return
|
|
310
|
+
# refunded, since those goods came back too.
|
|
308
311
|
def payment_covered?
|
|
309
312
|
order.payments.reset
|
|
310
|
-
order.payments.valid.where(status: %w[pending processing completed])
|
|
313
|
+
live = order.payments.valid.where(status: %w[pending processing completed])
|
|
314
|
+
refunded = Spree::Refund.where(payment_id: order.payments.where(status: 'completed').select(:id)).sum(:amount) -
|
|
315
|
+
order.returned_items_refund_total
|
|
316
|
+
live.sum(:amount) - refunded >= order.amount_due_at_checkout
|
|
311
317
|
end
|
|
312
318
|
end
|
|
313
319
|
end
|
|
@@ -17,13 +17,15 @@ module Spree
|
|
|
17
17
|
# @param amount [BigDecimal, nil] in the currency's own units; nil
|
|
18
18
|
# captures in full, a smaller amount splits the remainder into a new
|
|
19
19
|
# pending payment. An Integer is read as hundredths of the currency,
|
|
20
|
-
# the pre-6.0 contract (deprecated, removed in 6.
|
|
20
|
+
# the pre-6.0 contract (deprecated, removed in 6.5).
|
|
21
21
|
def perform(payment:, amount: nil)
|
|
22
22
|
super
|
|
23
|
+
@amount_requested = !amount.nil?
|
|
23
24
|
@amount = capture_amount(amount)
|
|
24
25
|
|
|
25
26
|
step :ensure_capturable
|
|
26
27
|
step :ensure_within_payment_amount
|
|
28
|
+
step :ensure_within_amount_owed
|
|
27
29
|
# Veto point — fraud holds, manual review. Before any money moves.
|
|
28
30
|
run_hooks :validate
|
|
29
31
|
run_hooks :before_capture
|
|
@@ -39,7 +41,9 @@ module Spree
|
|
|
39
41
|
external_step :authorize_remainder
|
|
40
42
|
|
|
41
43
|
run_hooks :after_capture
|
|
42
|
-
|
|
44
|
+
# A requested capture is confirmed by the provider's webhook, which
|
|
45
|
+
# publishes payment.captured then.
|
|
46
|
+
payment.publish_event(response.requested? ? 'payment.capture_requested' : 'payment.captured')
|
|
43
47
|
success(payment.reload)
|
|
44
48
|
end
|
|
45
49
|
|
|
@@ -53,7 +57,7 @@ module Spree
|
|
|
53
57
|
|
|
54
58
|
Spree::Deprecation.warn(
|
|
55
59
|
'Passing an Integer amount to the payment capture workflow is deprecated and is read as hundredths of ' \
|
|
56
|
-
'the currency. Pass the amount in the currency\'s own units as a BigDecimal; removed in Spree 6.
|
|
60
|
+
'the currency. Pass the amount in the currency\'s own units as a BigDecimal; removed in Spree 6.3.'
|
|
57
61
|
)
|
|
58
62
|
Spree::Money::Rounding.from_hundredths(amount)
|
|
59
63
|
end
|
|
@@ -65,6 +69,7 @@ module Spree
|
|
|
65
69
|
# this workflow again.
|
|
66
70
|
def ensure_capturable
|
|
67
71
|
halt!(payment) if payment.completed?
|
|
72
|
+
failure(payment, I18n.t('spree.payment_requested_awaiting_provider')) if payment.requested?
|
|
68
73
|
return if payment.pending? || payment.checkout? || payment.processing? || payment.failed?
|
|
69
74
|
|
|
70
75
|
failure(payment, :payment_not_capturable)
|
|
@@ -81,12 +86,91 @@ module Spree
|
|
|
81
86
|
failure(payment, :capture_exceeds_payment_amount)
|
|
82
87
|
end
|
|
83
88
|
|
|
89
|
+
# Other payments on the order may have captured some of its total
|
|
90
|
+
# already, or the order total may have dropped since this payment was
|
|
91
|
+
# authorized; this one may take only what is left owing.
|
|
92
|
+
def ensure_within_amount_owed
|
|
93
|
+
# Another request may have captured this payment since it was loaded:
|
|
94
|
+
# that is a success, whatever is owed now.
|
|
95
|
+
halt!(payment) if refresh_from_row == 'completed'
|
|
96
|
+
|
|
97
|
+
limit_to_amount_owed(payment.amount_owed_by_order(including_in_flight: false))
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
# The owed check again, under the order's row lock and together with
|
|
101
|
+
# the claim. A payment being charged sits in processing until it is
|
|
102
|
+
# recorded, and here it counts as taken: of two captures racing on one
|
|
103
|
+
# order, the second reads after the first has claimed, so both cannot
|
|
104
|
+
# pass a check made before either moved money.
|
|
105
|
+
#
|
|
106
|
+
# The row is read again here, with its amount: a processing run may have
|
|
107
|
+
# resized this payment since it was loaded, and the claim reserves the
|
|
108
|
+
# row's amount, so the capture must not take more than that.
|
|
84
109
|
def claim
|
|
85
|
-
|
|
110
|
+
outcome = with_order_lock do
|
|
111
|
+
next false if refresh_from_row == 'completed'
|
|
112
|
+
|
|
113
|
+
refusal = amount_within_payment_refusal ||
|
|
114
|
+
amount_owed_refusal(payment.amount_owed_by_order(including_in_flight: false, including_processing: true))
|
|
115
|
+
next refusal if refusal
|
|
116
|
+
|
|
117
|
+
payment.started_processing!
|
|
118
|
+
end
|
|
119
|
+
failure(payment, outcome) if outcome.is_a?(String)
|
|
120
|
+
halt!(payment) unless outcome
|
|
86
121
|
rescue Spree::Core::GatewayError => error
|
|
87
122
|
failure(payment, error.message)
|
|
88
123
|
end
|
|
89
124
|
|
|
125
|
+
# Reads the status and amount another run may have written since this
|
|
126
|
+
# instance was loaded. Deliberately not a reload, which would discard the
|
|
127
|
+
# in-memory source (card numbers never persist).
|
|
128
|
+
def refresh_from_row
|
|
129
|
+
return payment.status unless payment.persisted?
|
|
130
|
+
|
|
131
|
+
status, amount = Spree::Payment.unscoped.where(id: payment.id).pick(:status, :amount)
|
|
132
|
+
payment.amount = amount
|
|
133
|
+
payment.clear_attribute_changes(%i[amount])
|
|
134
|
+
status
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
# The payment's own amount may have shrunk since the early check. An
|
|
138
|
+
# amount the caller asked for is then refused; with none asked for, the
|
|
139
|
+
# capture shrinks with it.
|
|
140
|
+
def amount_within_payment_refusal
|
|
141
|
+
return if @amount <= payment.amount
|
|
142
|
+
return I18n.t('spree.capture_exceeds_payment_amount') if @amount_requested
|
|
143
|
+
|
|
144
|
+
@amount = payment.amount
|
|
145
|
+
nil
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
def limit_to_amount_owed(owed)
|
|
149
|
+
refusal = amount_owed_refusal(owed)
|
|
150
|
+
failure(payment, refusal) if refusal
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
# An amount the caller asked for is refused when it exceeds what is
|
|
154
|
+
# owed. With no amount asked for — the dashboard's capture — the capture
|
|
155
|
+
# shrinks to what is owed, and record_capture splits off the rest of the
|
|
156
|
+
# authorization, which authorize_remainder then releases. Returns the
|
|
157
|
+
# refusal message, or nil.
|
|
158
|
+
def amount_owed_refusal(owed)
|
|
159
|
+
return if owed.nil? || @amount <= owed
|
|
160
|
+
return I18n.t('spree.order_payment_already_covered') if !@amount_requested && !owed.positive?
|
|
161
|
+
|
|
162
|
+
unless @amount_requested
|
|
163
|
+
@amount = owed
|
|
164
|
+
return
|
|
165
|
+
end
|
|
166
|
+
|
|
167
|
+
I18n.t('spree.capture_exceeds_amount_owed', amount: Spree::Money.new(owed, currency: payment.currency).to_s)
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
def with_order_lock(&)
|
|
171
|
+
payment.order_id ? payment.owner.with_lock(&) : yield
|
|
172
|
+
end
|
|
173
|
+
|
|
90
174
|
def capture_at_gateway
|
|
91
175
|
@response = payment.protect_from_connection_error do
|
|
92
176
|
instrument_gateway_call(:capture, payment.payment_method) do
|
|
@@ -106,14 +190,21 @@ module Spree
|
|
|
106
190
|
# Locked because the webhook settling the same payment races this
|
|
107
191
|
# request. The DB recheck — deliberately not a reload, which would
|
|
108
192
|
# discard the in-memory source (card numbers never persist) — makes
|
|
109
|
-
# the capture event exactly-once
|
|
193
|
+
# the capture event exactly-once: a payment no longer `processing` was
|
|
194
|
+
# moved by settlement, a provider outcome or another capture, and is
|
|
195
|
+
# not reopened.
|
|
110
196
|
def record_capture
|
|
111
197
|
already_captured = false
|
|
112
198
|
payment.owner.with_lock do
|
|
113
|
-
if Spree::Payment.where(id: payment.id
|
|
199
|
+
if Spree::Payment.where(id: payment.id).where.not(status: 'processing').exists?
|
|
114
200
|
already_captured = true
|
|
201
|
+
elsif response.requested?
|
|
202
|
+
# The provider accepted the capture and answers by webhook: no
|
|
203
|
+
# remainder and no completion until it does.
|
|
204
|
+
payment.capture_events.create!(amount: @amount, status: 'requested', transaction_id: response.reference)
|
|
205
|
+
payment.update!(status: 'requested')
|
|
115
206
|
else
|
|
116
|
-
payment.capture_events.create!(amount: @amount)
|
|
207
|
+
payment.capture_events.create!(amount: @amount, status: 'completed')
|
|
117
208
|
# Split before completing, so payment.completed publishes with
|
|
118
209
|
# the captured amount and the order recomputes from correct rows.
|
|
119
210
|
@remainder = payment.split_uncaptured_amount
|
|
@@ -127,6 +218,9 @@ module Spree
|
|
|
127
218
|
# outside the lock.
|
|
128
219
|
def authorize_remainder
|
|
129
220
|
return if remainder.blank?
|
|
221
|
+
# Holding more than the order still owes would only keep the
|
|
222
|
+
# customer's money tied up.
|
|
223
|
+
return unless remainder.trim_remainder_to_amount_owed!
|
|
130
224
|
|
|
131
225
|
result = Spree.payment_process_workflow.call(payment: remainder, action: :authorize)
|
|
132
226
|
failure(remainder, result.error.value.to_s) if result.failure?
|