spree_core 6.0.6 → 6.0.7
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/base_job.rb +9 -5
- data/app/jobs/spree/gift_cards/bulk_generate_job.rb +4 -2
- data/app/jobs/spree/gift_cards/publish_created_events_job.rb +46 -0
- data/app/jobs/spree/images/save_from_url_job.rb +92 -22
- data/app/jobs/spree/payments/handle_webhook_job.rb +107 -1
- data/app/mailers/spree/api_key_mailer.rb +52 -0
- data/app/models/concerns/spree/prefixed_id.rb +12 -6
- data/app/models/concerns/spree/purchase/lifecycle.rb +4 -1
- data/app/models/concerns/spree/purchase/market.rb +2 -2
- data/app/models/concerns/spree/purchase/warnings.rb +29 -0
- data/app/models/concerns/spree/user_address.rb +4 -2
- data/app/models/concerns/spree/user_management.rb +26 -2
- data/app/models/spree/api_key.rb +24 -0
- data/app/models/spree/base.rb +29 -0
- data/app/models/spree/cart.rb +90 -9
- data/app/models/spree/channel.rb +2 -1
- data/app/models/spree/country.rb +9 -0
- data/app/models/spree/credit_card.rb +2 -0
- data/app/models/spree/delivery_origin_group.rb +2 -1
- data/app/models/spree/export.rb +10 -0
- data/app/models/spree/exports/report.rb +16 -0
- data/app/models/spree/fulfillment.rb +30 -0
- data/app/models/spree/fulfillment_group.rb +13 -0
- data/app/models/spree/gift_card.rb +27 -3
- data/app/models/spree/gift_card_batch.rb +82 -11
- data/app/models/spree/invitation.rb +51 -7
- data/app/models/spree/market.rb +6 -1
- data/app/models/spree/media.rb +30 -3
- data/app/models/spree/oauth_access_token.rb +2 -0
- data/app/models/spree/order.rb +4 -4
- data/app/models/spree/payment_session.rb +21 -1
- data/app/models/spree/price_history.rb +2 -0
- data/app/models/spree/promotion.rb +6 -2
- data/app/models/spree/tax_provider/base.rb +29 -0
- data/app/serializers/spree/emails/api_key_serializer.rb +16 -0
- data/app/services/spree/orders/update.rb +32 -2
- data/app/validators/spree/attachment_content_type_validator.rb +29 -3
- data/app/views/spree/api_key_mailer/secret_key_created.liquid +31 -0
- data/app/workflows/spree/carts/add_item.rb +3 -1
- data/app/workflows/spree/carts/complete.rb +3 -1
- data/app/workflows/spree/carts/merge.rb +44 -0
- data/app/workflows/spree/carts/recalculate.rb +5 -2
- data/app/workflows/spree/carts/upsert_items.rb +45 -0
- data/app/workflows/spree/gift_cards/apply.rb +12 -0
- data/app/workflows/spree/gift_cards/destroy.rb +96 -0
- data/app/workflows/spree/gift_cards/remove.rb +4 -4
- data/app/workflows/spree/invitations/accept.rb +15 -0
- data/app/workflows/spree/payments/handle_webhook.rb +15 -8
- data/config/locales/ar.yml +15 -0
- data/config/locales/be.yml +15 -0
- data/config/locales/bg.yml +15 -0
- data/config/locales/ca.yml +15 -0
- data/config/locales/cs.yml +15 -0
- data/config/locales/da.yml +15 -0
- data/config/locales/de-CH.yml +15 -0
- data/config/locales/de.yml +15 -0
- data/config/locales/el.yml +15 -0
- data/config/locales/en.yml +19 -0
- data/config/locales/es-CL.yml +15 -0
- data/config/locales/es-EC.yml +15 -0
- data/config/locales/es-MX.yml +15 -0
- data/config/locales/es.yml +15 -0
- data/config/locales/et.yml +15 -0
- data/config/locales/fa.yml +15 -0
- data/config/locales/fi.yml +15 -0
- data/config/locales/fr.yml +15 -0
- data/config/locales/hu.yml +15 -0
- data/config/locales/id.yml +15 -0
- data/config/locales/it.yml +15 -0
- data/config/locales/ja.yml +15 -0
- data/config/locales/km.yml +15 -0
- data/config/locales/ko.yml +15 -0
- data/config/locales/ku.yml +15 -0
- data/config/locales/lt.yml +15 -0
- data/config/locales/lv.yml +15 -0
- data/config/locales/nb.yml +15 -0
- data/config/locales/ne.yml +15 -0
- data/config/locales/nl.yml +15 -0
- data/config/locales/pl.yml +15 -0
- data/config/locales/pt-BR.yml +15 -0
- data/config/locales/pt.yml +15 -0
- data/config/locales/ro.yml +15 -0
- data/config/locales/ru.yml +15 -0
- data/config/locales/sk.yml +15 -0
- data/config/locales/sl-SI.yml +15 -0
- data/config/locales/sv.yml +15 -0
- data/config/locales/th.yml +15 -0
- data/config/locales/tr.yml +15 -0
- data/config/locales/uk.yml +15 -0
- data/config/locales/vi.yml +15 -0
- data/config/locales/zh-CN.yml +15 -0
- data/config/locales/zh-TW.yml +15 -0
- data/db/migrate/20261124000001_set_default_fee_total_on_spree_orders.rb +19 -0
- data/db/migrate/20261125100001_add_created_by_ip_address_to_spree_api_keys.rb +7 -0
- data/lib/spree/core/dependencies.rb +1 -0
- data/lib/spree/core/previews/api_key_preview.rb +29 -0
- data/lib/spree/core/version.rb +1 -1
- data/lib/spree/core.rb +1 -1
- data/lib/spree/iso_data.rb +3 -1
- data/lib/spree/reporting/query.rb +19 -2
- data/lib/spree/testing_support/common_rake.rb +10 -3
- data/lib/tasks/cli.rake +4 -2
- data/lib/tasks/typed_adjustments_migration.rake +4 -1
- metadata +13 -4
data/app/models/spree/cart.rb
CHANGED
|
@@ -22,6 +22,8 @@ module Spree
|
|
|
22
22
|
include Spree::Purchase::QuantityRules
|
|
23
23
|
include Spree::Purchase::PurchaseOrder
|
|
24
24
|
include Spree::Purchase::Market
|
|
25
|
+
# A deleted market reads as none, so ensure_market_presence resolves a live one.
|
|
26
|
+
belongs_to :market, class_name: 'Spree::Market'
|
|
25
27
|
include Spree::Purchase::Currency
|
|
26
28
|
include Spree::Purchase::Locale
|
|
27
29
|
include Spree::Purchase::CheckoutSteps
|
|
@@ -37,6 +39,7 @@ module Spree
|
|
|
37
39
|
include Spree::Purchase::Totals
|
|
38
40
|
include Spree::Purchase::Lifecycle
|
|
39
41
|
include Spree::Purchase::DeprecatedAliases
|
|
42
|
+
include Spree::Purchase::Warnings
|
|
40
43
|
|
|
41
44
|
# Concurrency is manual (the API's OrderLock semantics — compare
|
|
42
45
|
# client-sent version, 409 on mismatch); Rails auto-locking must not
|
|
@@ -48,9 +51,6 @@ module Spree
|
|
|
48
51
|
attribute :metadata, default: -> { {} }
|
|
49
52
|
attribute :accept_marketing, :boolean, default: false
|
|
50
53
|
|
|
51
|
-
# Transient warnings populated by remove_out_of_stock_items!
|
|
52
|
-
attribute :warnings, default: -> { [] }
|
|
53
|
-
|
|
54
54
|
money_methods :item_total, :adjustment_total, :included_tax_total, :additional_tax_total,
|
|
55
55
|
:discount_total, :fee_total, :delivery_total, :total, :payment_total, :outstanding_balance,
|
|
56
56
|
:tax_total, :pre_tax_item_amount, :pre_tax_total, :amount_due
|
|
@@ -139,13 +139,29 @@ module Spree
|
|
|
139
139
|
completing_at.present?
|
|
140
140
|
end
|
|
141
141
|
|
|
142
|
+
# Whether this cart's contents and totals are fixed — mutations must wait
|
|
143
|
+
# until the purchase completes. True while a completion attempt holds the
|
|
144
|
+
# cart (see +completion_attempt_claimed?+), and while a gateway has
|
|
145
|
+
# accepted money for it through a payment session (see
|
|
146
|
+
# +payment_session_settled?+).
|
|
147
|
+
#
|
|
148
|
+
# @return [Boolean]
|
|
149
|
+
def completion_claimed?
|
|
150
|
+
completion_attempt_claimed? || payment_session_settled?
|
|
151
|
+
end
|
|
152
|
+
|
|
142
153
|
# Whether a live completion attempt holds this cart — its totals are fixed
|
|
143
154
|
# and money may be moving, so mutations must wait until it finishes or the
|
|
144
155
|
# claim goes stale. A payment-covered draft or a divided checkout resumes
|
|
145
156
|
# its existing result instead of editing the cart that supplied it.
|
|
146
157
|
#
|
|
158
|
+
# Completion itself checks only this, not +completion_claimed?+: a cart
|
|
159
|
+
# whose payment session settled must still be completable — by the
|
|
160
|
+
# storefront's complete call or the gateway webhook — or the buyer is
|
|
161
|
+
# charged without an order.
|
|
162
|
+
#
|
|
147
163
|
# @return [Boolean]
|
|
148
|
-
def
|
|
164
|
+
def completion_attempt_claimed?
|
|
149
165
|
return false if completing_at.blank?
|
|
150
166
|
return true if completing_at > COMPLETING_CLAIM_TTL.ago || order_group.present?
|
|
151
167
|
return false if order.nil?
|
|
@@ -153,6 +169,63 @@ module Spree
|
|
|
153
169
|
order.payments.valid.where(status: %w[pending processing completed]).sum(:amount) >= order.amount_due_at_checkout
|
|
154
170
|
end
|
|
155
171
|
|
|
172
|
+
# Whether a gateway has accepted money for this cart through a payment
|
|
173
|
+
# session that has not been placed yet: a session the gateway reported
|
|
174
|
+
# as accepted (processing or completed), or a live payment settled from
|
|
175
|
+
# one. Completing the cart — through the storefront or the gateway's
|
|
176
|
+
# webhook — places the order for exactly that money, so the cart must
|
|
177
|
+
# keep the contents and totals it was charged for. An emptied or edited
|
|
178
|
+
# cart can no longer complete, and the buyer stays charged without an
|
|
179
|
+
# order.
|
|
180
|
+
#
|
|
181
|
+
# Released when the money is no longer held: the payment recorded for an
|
|
182
|
+
# accepted session is voided or fails (an accepted session with no
|
|
183
|
+
# payment row holds the cart until it completes), and no valid payment
|
|
184
|
+
# past +checkout+ from any session remains — a failed, canceled or
|
|
185
|
+
# expired session does not release the cart while one does. A session
|
|
186
|
+
# still pending fences nothing. Nothing releases it on a timer — a
|
|
187
|
+
# settled cart whose completion keeps failing (see
|
|
188
|
+
# Spree::Payments::HandleWebhookJob) stays fenced until staff resolve the
|
|
189
|
+
# payment, because unfencing it would let the buyer change what they
|
|
190
|
+
# were charged for.
|
|
191
|
+
#
|
|
192
|
+
# A session and a payment belong together when the payment's
|
|
193
|
+
# +response_code+ is the session's +external_id+ under the same payment
|
|
194
|
+
# method: identifiers are only unique per payment method, so a void
|
|
195
|
+
# payment under another method must not release a session's claim.
|
|
196
|
+
#
|
|
197
|
+
# @return [Boolean]
|
|
198
|
+
def payment_session_settled?
|
|
199
|
+
return false unless persisted?
|
|
200
|
+
|
|
201
|
+
accepted = payment_sessions.where(status: %w[processing completed])
|
|
202
|
+
voided = payments.where(status: Spree::Payment::INVALID_STATUSES).where(session_payment_match)
|
|
203
|
+
live = payments.valid.where(session_payment_match)
|
|
204
|
+
|
|
205
|
+
# An accepted session holds the cart while it has no void or failed
|
|
206
|
+
# payment, or while a payment settled from it is still valid.
|
|
207
|
+
accepted.where.not(voided.arel.exists).exists? ||
|
|
208
|
+
accepted.where(live.arel.exists).exists? ||
|
|
209
|
+
payments.valid.where.not(status: 'checkout').where(payment_sessions.where(session_payment_match).arel.exists).exists?
|
|
210
|
+
end
|
|
211
|
+
|
|
212
|
+
# Whether a payment session on this cart may still take money, or
|
|
213
|
+
# already has: any session not canceled or expired. The gateway can
|
|
214
|
+
# capture a session while it is still +pending+ here, before its webhook
|
|
215
|
+
# lands, so only a retired session is safe to discard with the cart.
|
|
216
|
+
#
|
|
217
|
+
# Narrower in effect than +completion_claimed?+ on purpose: a pending
|
|
218
|
+
# session does not fix the cart's contents, but destroying the cart
|
|
219
|
+
# would take the session with it and leave the webhook nothing to
|
|
220
|
+
# settle (see Spree::Carts::Merge).
|
|
221
|
+
#
|
|
222
|
+
# @return [Boolean]
|
|
223
|
+
def payment_session_open?
|
|
224
|
+
return false unless persisted?
|
|
225
|
+
|
|
226
|
+
payment_sessions.where.not(status: Spree::PaymentSession::RETIRED_STATUSES).exists?
|
|
227
|
+
end
|
|
228
|
+
|
|
156
229
|
# Recomputes and persists money totals (item, tax, promotion, delivery)
|
|
157
230
|
# and derived item counts. Convenience for
|
|
158
231
|
# {Spree::Carts::RecalculateTotals}.
|
|
@@ -329,10 +402,6 @@ module Spree
|
|
|
329
402
|
def remove_unavailable_coupon_code!
|
|
330
403
|
return self unless coupon_code_unavailable?
|
|
331
404
|
|
|
332
|
-
# The lock refuses unsaved attributes and reloads the cart, so this
|
|
333
|
-
# request's warnings are set aside and put back.
|
|
334
|
-
existing_warnings = warnings
|
|
335
|
-
clear_attribute_changes([:warnings])
|
|
336
405
|
removed = with_lock do
|
|
337
406
|
# Checked under the lock, as the completion claim itself is: a
|
|
338
407
|
# completing cart's code sits on its draft order while the payment
|
|
@@ -342,10 +411,22 @@ module Spree
|
|
|
342
411
|
Spree.coupon_handler.new(self, enable_gift_cards: false).remove(read_attribute(:coupon_code))
|
|
343
412
|
true
|
|
344
413
|
end
|
|
345
|
-
self.warnings = existing_warnings
|
|
346
414
|
self.warnings |= [{ code: 'coupon_code_unavailable', message: I18n.t('spree.coupon_code_unavailable') }] if removed
|
|
347
415
|
self
|
|
348
416
|
end
|
|
349
417
|
|
|
418
|
+
private
|
|
419
|
+
|
|
420
|
+
# Correlates a payment with the payment session it was settled from, for
|
|
421
|
+
# the EXISTS subqueries in +payment_session_settled?+.
|
|
422
|
+
def session_payment_match
|
|
423
|
+
payments_table = Spree::Payment.arel_table
|
|
424
|
+
sessions_table = Spree::PaymentSession.arel_table
|
|
425
|
+
|
|
426
|
+
same_identifier = payments_table[:response_code].eq(sessions_table[:external_id])
|
|
427
|
+
same_method = payments_table[:payment_method_id].eq(sessions_table[:payment_method_id])
|
|
428
|
+
|
|
429
|
+
same_identifier.and(same_method)
|
|
430
|
+
end
|
|
350
431
|
end
|
|
351
432
|
end
|
data/app/models/spree/channel.rb
CHANGED
|
@@ -31,7 +31,8 @@ module Spree
|
|
|
31
31
|
# location serves this channel (docs/plans/6.0-channel-delivery.md).
|
|
32
32
|
has_many :channel_stock_locations, class_name: 'Spree::ChannelStockLocation',
|
|
33
33
|
dependent: :destroy, inverse_of: :channel
|
|
34
|
-
has_many :stock_locations, through: :channel_stock_locations, class_name: 'Spree::StockLocation'
|
|
34
|
+
has_many :stock_locations, through: :channel_stock_locations, class_name: 'Spree::StockLocation',
|
|
35
|
+
dependent: :destroy
|
|
35
36
|
|
|
36
37
|
attribute :active, :boolean, default: true
|
|
37
38
|
|
data/app/models/spree/country.rb
CHANGED
|
@@ -142,6 +142,15 @@ module Spree
|
|
|
142
142
|
iso3166_country&.languages_official.presence || []
|
|
143
143
|
end
|
|
144
144
|
|
|
145
|
+
# The official languages as the locales Spree lists for them (Switzerland
|
|
146
|
+
# gives de, fr, it and rm) — content locales, whether or not Spree ships
|
|
147
|
+
# translations, since the storefront and dashboard carry their own.
|
|
148
|
+
# English stands in for a country with none listed.
|
|
149
|
+
# @return [Array<String>]
|
|
150
|
+
def offerable_locales
|
|
151
|
+
official_locales.filter_map { |language| Spree::Locales.for_language(language) }.uniq.presence || ['en']
|
|
152
|
+
end
|
|
153
|
+
|
|
145
154
|
# Countries are equal when they name the same place, so they can be
|
|
146
155
|
# compared, deduplicated and used as hash keys.
|
|
147
156
|
def ==(other)
|
|
@@ -16,7 +16,8 @@ module Spree
|
|
|
16
16
|
|
|
17
17
|
has_many :delivery_origin_group_locations, class_name: 'Spree::DeliveryOriginGroupLocation',
|
|
18
18
|
dependent: :destroy, inverse_of: :delivery_origin_group
|
|
19
|
-
has_many :stock_locations, through: :delivery_origin_group_locations, class_name: 'Spree::StockLocation'
|
|
19
|
+
has_many :stock_locations, through: :delivery_origin_group_locations, class_name: 'Spree::StockLocation',
|
|
20
|
+
dependent: :destroy
|
|
20
21
|
# The group contains its zones and methods — that is how the dashboard
|
|
21
22
|
# nests them and how a merchant reads it — so deleting the group takes
|
|
22
23
|
# them with it rather than asking for them to be emptied by hand first.
|
data/app/models/spree/export.rb
CHANGED
|
@@ -108,6 +108,16 @@ module Spree
|
|
|
108
108
|
attachment.present? && attachment.attached?
|
|
109
109
|
end
|
|
110
110
|
|
|
111
|
+
# Catalog keys a delegated credential must hold, beyond the export type's
|
|
112
|
+
# own read scope, to create or download this export. Only exports whose
|
|
113
|
+
# rows depend on what they were asked for (a report) need any; nil means
|
|
114
|
+
# they could not be worked out, and the export must be refused.
|
|
115
|
+
#
|
|
116
|
+
# @return [Array<String>, nil]
|
|
117
|
+
def required_key_scopes
|
|
118
|
+
[]
|
|
119
|
+
end
|
|
120
|
+
|
|
111
121
|
def generate_async
|
|
112
122
|
Spree::Exports::GenerateJob.perform_later(id)
|
|
113
123
|
end
|
|
@@ -25,6 +25,22 @@ module Spree
|
|
|
25
25
|
self.class.model_class
|
|
26
26
|
end
|
|
27
27
|
|
|
28
|
+
# The key scopes of the members the query groups or filters by — the
|
|
29
|
+
# same list the reporting endpoint checks (see
|
|
30
|
+
# Spree::Reporting::Query#required_key_scopes).
|
|
31
|
+
#
|
|
32
|
+
# nil when the query no longer compiles — a stored export naming a
|
|
33
|
+
# dimension an extension has since removed — so the caller can refuse
|
|
34
|
+
# rather than read "nothing required". A new export with such a query
|
|
35
|
+
# is refused by validation instead.
|
|
36
|
+
#
|
|
37
|
+
# @return [Array<String>, nil]
|
|
38
|
+
def required_key_scopes
|
|
39
|
+
reporting_query.required_key_scopes
|
|
40
|
+
rescue Spree::Reporting::UnknownMember, Spree::Reporting::InvalidQuery, JSON::ParserError
|
|
41
|
+
nil
|
|
42
|
+
end
|
|
43
|
+
|
|
28
44
|
def csv_headers
|
|
29
45
|
labels = Spree::Reporting::Schema.new(store: store)
|
|
30
46
|
dimension_labels = reporting_query.dimensions.map { |d| labels.label_for(:dimensions, d[:dimension].name) }
|
|
@@ -271,6 +271,36 @@ module Spree
|
|
|
271
271
|
delivery_method&.digital? || false
|
|
272
272
|
end
|
|
273
273
|
|
|
274
|
+
# How the goods reach the customer, read from the selected rate's
|
|
275
|
+
# delivery method the way the 6.0 upgrade backfill did, so it follows a
|
|
276
|
+
# later change of rate. With no rate selected, an offered rate says
|
|
277
|
+
# nothing about the choice: the column (only the backfill writes it)
|
|
278
|
+
# answers, else the kind every fulfillment had before the column.
|
|
279
|
+
#
|
|
280
|
+
# @return [String] shipping, digital, pickup or pickup_point
|
|
281
|
+
def fulfillment_type
|
|
282
|
+
provider_class = selected_delivery_rate&.delivery_method&.provider_class
|
|
283
|
+
return super.presence || (digital? ? 'digital' : 'shipping') if provider_class.nil?
|
|
284
|
+
|
|
285
|
+
if provider_class.digital? then 'digital'
|
|
286
|
+
elsif provider_class.pickup_point? then 'pickup_point'
|
|
287
|
+
elsif provider_class.pickup? then 'pickup'
|
|
288
|
+
else 'shipping'
|
|
289
|
+
end
|
|
290
|
+
end
|
|
291
|
+
|
|
292
|
+
# Where the customer collects a pickup: the counter they chose at
|
|
293
|
+
# checkout, else the origin when it is itself a pickup counter. Under the
|
|
294
|
+
# 'any' pickup stock policy the origin can be a warehouse that ships to
|
|
295
|
+
# the counter, which the customer cannot visit.
|
|
296
|
+
#
|
|
297
|
+
# @return [Spree::StockLocation, nil] nil unless this is a pickup
|
|
298
|
+
def pickup_location
|
|
299
|
+
return unless delivery_method&.pickup?
|
|
300
|
+
|
|
301
|
+
(order || cart)&.preferred_stock_location || (stock_location if stock_location&.pickup_enabled?)
|
|
302
|
+
end
|
|
303
|
+
|
|
274
304
|
# @deprecated Use {#delivery_method}; removed in 6.3.
|
|
275
305
|
def shipping_method
|
|
276
306
|
Spree::Deprecation.warn('Spree::Fulfillment#shipping_method is deprecated and will be removed in Spree 6.3. Use #delivery_method instead.')
|
|
@@ -63,6 +63,19 @@ module Spree
|
|
|
63
63
|
primary.stock_location
|
|
64
64
|
end
|
|
65
65
|
|
|
66
|
+
# Where the customer collects the parcel when it is a pickup — see
|
|
67
|
+
# {Spree::Fulfillment#pickup_location}. Split orders share the counter
|
|
68
|
+
# the customer chose, so the first answers for all of them. A parcel
|
|
69
|
+
# canceled in every order it belongs to has nothing left to collect.
|
|
70
|
+
#
|
|
71
|
+
# @return [Spree::StockLocation, nil]
|
|
72
|
+
def pickup_location
|
|
73
|
+
return unless delivery_method&.pickup?
|
|
74
|
+
return if fulfillments.all?(&:canceled?)
|
|
75
|
+
|
|
76
|
+
orders.first&.preferred_stock_location || (stock_location if stock_location&.pickup_enabled?)
|
|
77
|
+
end
|
|
78
|
+
|
|
66
79
|
# What this parcel costs to deliver: the halves' apportioned shares added
|
|
67
80
|
# back up, which is what the customer was quoted for it before the split
|
|
68
81
|
# divided anything.
|
|
@@ -144,10 +144,24 @@ module Spree
|
|
|
144
144
|
active?
|
|
145
145
|
end
|
|
146
146
|
|
|
147
|
-
# Checks if the gift card can be deleted
|
|
147
|
+
# Checks if the gift card can be deleted. Besides a redeemed card, this
|
|
148
|
+
# refuses one an open cart or draft order still holds: Apply draws the
|
|
149
|
+
# card down and pays with a store credit made from it while the card keeps
|
|
150
|
+
# the active status, so deleting it would leave that store credit and its
|
|
151
|
+
# payment funding the order with no card behind them.
|
|
148
152
|
# @return [Boolean]
|
|
149
153
|
def can_be_deleted?
|
|
150
|
-
|
|
154
|
+
return false if redeemed? || partially_redeemed?
|
|
155
|
+
return false if amount_used.to_d.positive? || amount_authorized.to_d.positive?
|
|
156
|
+
|
|
157
|
+
!held?
|
|
158
|
+
end
|
|
159
|
+
|
|
160
|
+
# Whether an open cart or draft order has this card applied, including one
|
|
161
|
+
# in the middle of being completed.
|
|
162
|
+
# @return [Boolean]
|
|
163
|
+
def held?
|
|
164
|
+
carts.incomplete.exists? || orders.incomplete.where.not(status: 'canceled').exists?
|
|
151
165
|
end
|
|
152
166
|
|
|
153
167
|
# Displays the code in uppercase, eg. ABC1234
|
|
@@ -281,8 +295,18 @@ module Spree
|
|
|
281
295
|
self.code = code.downcase if code.present?
|
|
282
296
|
end
|
|
283
297
|
|
|
298
|
+
# Runs inside the destroy's transaction, so the row lock taken here holds
|
|
299
|
+
# until the delete commits. Spree::GiftCards::Apply locks the card before
|
|
300
|
+
# drawing it down and attaching it, so an apply either commits first (and
|
|
301
|
+
# the reloaded card shows the hold) or waits and then finds the card gone.
|
|
302
|
+
# Every destroy path gets this — the admin endpoint, and the
|
|
303
|
+
# dependent: :destroy cascades from a batch, a customer or a store.
|
|
304
|
+
#
|
|
305
|
+
# The check reads a locked fresh copy rather than reloading this record,
|
|
306
|
+
# so in-memory changes are left alone.
|
|
284
307
|
def ensure_can_be_deleted
|
|
285
|
-
|
|
308
|
+
current = self.class.lock.find(id)
|
|
309
|
+
return if current.can_be_deleted?
|
|
286
310
|
|
|
287
311
|
errors.add(:base, :cannot_destroy_used_gift_card)
|
|
288
312
|
throw(:abort)
|
|
@@ -10,6 +10,18 @@ module Spree
|
|
|
10
10
|
|
|
11
11
|
publishes_lifecycle_events
|
|
12
12
|
|
|
13
|
+
CODE_LOOKUP_BATCH_SIZE = 1_000
|
|
14
|
+
# Rounds of fresh codes tried before giving up. Each code has 64 random
|
|
15
|
+
# bits, so a round that comes back short is already a rare collision;
|
|
16
|
+
# running out of rounds means rows are being dropped for some other reason.
|
|
17
|
+
MAX_CODE_ATTEMPTS = 5
|
|
18
|
+
# Codes are the prefix plus 16 random hex characters, and must fit a
|
|
19
|
+
# 255-character column.
|
|
20
|
+
MAX_PREFIX_LENGTH = 255 - 16
|
|
21
|
+
|
|
22
|
+
# Raised when a batch cannot be filled with unique codes.
|
|
23
|
+
class CodeGenerationError < StandardError; end
|
|
24
|
+
|
|
13
25
|
#
|
|
14
26
|
# Associations
|
|
15
27
|
#
|
|
@@ -20,6 +32,7 @@ module Spree
|
|
|
20
32
|
# Validations
|
|
21
33
|
#
|
|
22
34
|
validates :codes_count, :amount, :prefix, presence: true
|
|
35
|
+
validates :prefix, length: { maximum: MAX_PREFIX_LENGTH }
|
|
23
36
|
validates :codes_count, numericality: {
|
|
24
37
|
greater_than: 0,
|
|
25
38
|
less_than_or_equal_to: ->(_record) { Spree::Config[:gift_card_batch_limit].to_i }
|
|
@@ -33,7 +46,7 @@ module Spree
|
|
|
33
46
|
before_validation :set_currency
|
|
34
47
|
after_create :generate_gift_cards
|
|
35
48
|
|
|
36
|
-
normalizes :prefix, with: ->(value) { value
|
|
49
|
+
normalizes :prefix, with: ->(value) { value.to_s.squish.presence }
|
|
37
50
|
|
|
38
51
|
money_methods :amount
|
|
39
52
|
|
|
@@ -43,37 +56,95 @@ module Spree
|
|
|
43
56
|
if codes_count < Spree::Config[:gift_card_batch_web_limit].to_i
|
|
44
57
|
create_gift_cards
|
|
45
58
|
else
|
|
46
|
-
|
|
59
|
+
# The job runs outside this write, so it carries whether lifecycle
|
|
60
|
+
# events were on when the batch was created.
|
|
61
|
+
Spree::GiftCards::BulkGenerateJob.perform_later(id, publish_events: Spree::Events.lifecycle_enabled?)
|
|
47
62
|
end
|
|
48
63
|
end
|
|
49
64
|
|
|
50
|
-
|
|
65
|
+
# @param publish_events [Boolean] false to skip the cards' gift_card.created events
|
|
66
|
+
def create_gift_cards(publish_events: true)
|
|
51
67
|
@gift_cards_to_insert = []
|
|
52
68
|
|
|
53
69
|
Spree::GiftCard.transaction do
|
|
54
|
-
|
|
55
|
-
|
|
70
|
+
published_up_to = gift_cards.maximum(:id)
|
|
71
|
+
# `insert_all` silently skips a row whose code was taken after it was
|
|
72
|
+
# checked, so top the batch up until it holds every card.
|
|
73
|
+
missing = codes_count - gift_cards.count
|
|
74
|
+
|
|
75
|
+
MAX_CODE_ATTEMPTS.times do
|
|
76
|
+
break unless missing.positive?
|
|
77
|
+
|
|
78
|
+
rows = generate_codes(missing, taken: @gift_cards_to_insert.pluck(:code)).map { |code| gift_card_hash(code) }
|
|
79
|
+
Spree::GiftCard.insert_all rows
|
|
80
|
+
@gift_cards_to_insert.concat(rows)
|
|
81
|
+
still_missing = codes_count - Spree::GiftCard.where(gift_card_batch_id: id).count
|
|
82
|
+
# A round that inserted nothing will not do better next time.
|
|
83
|
+
break if still_missing == missing
|
|
84
|
+
|
|
85
|
+
missing = still_missing
|
|
56
86
|
end
|
|
57
|
-
|
|
87
|
+
raise CodeGenerationError, "Gift card batch #{prefixed_id} is #{missing} card(s) short of #{codes_count}" if missing.positive?
|
|
88
|
+
|
|
89
|
+
publish_gift_card_created_events(after_id: published_up_to) if publish_events && @gift_cards_to_insert.any?
|
|
58
90
|
end
|
|
59
91
|
end
|
|
60
92
|
|
|
61
93
|
def generate_code
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
94
|
+
generate_codes(1, taken: Array(@gift_cards_to_insert).pluck(:code)).first
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
# Unique codes not yet used by any gift card, checked against the database
|
|
98
|
+
# a slice at a time rather than one query per code.
|
|
99
|
+
#
|
|
100
|
+
# @param count [Integer] how many codes to generate
|
|
101
|
+
# @param taken [Array<String>] codes to avoid besides the stored ones
|
|
102
|
+
# @return [Array<String>]
|
|
103
|
+
def generate_codes(count, taken: [])
|
|
104
|
+
codes = Set.new
|
|
105
|
+
taken = taken.to_set
|
|
106
|
+
|
|
107
|
+
MAX_CODE_ATTEMPTS.times do
|
|
108
|
+
break if codes.size >= count
|
|
109
|
+
|
|
110
|
+
candidates = Array.new(count - codes.size) { "#{prefix.downcase}#{SecureRandom.hex(8).downcase}" }
|
|
111
|
+
candidates = candidates.uniq.reject { |code| codes.include?(code) || taken.include?(code) }
|
|
112
|
+
|
|
113
|
+
candidates.each_slice(CODE_LOOKUP_BATCH_SIZE) do |slice|
|
|
114
|
+
stored = Spree::GiftCard.where(code: slice).pluck(:code).to_set
|
|
115
|
+
codes.merge(slice.reject { |code| stored.include?(code) })
|
|
116
|
+
end
|
|
65
117
|
end
|
|
118
|
+
raise CodeGenerationError, "Could not generate #{count} unique gift card codes" if codes.size < count
|
|
119
|
+
|
|
120
|
+
codes.to_a
|
|
66
121
|
end
|
|
67
122
|
|
|
68
123
|
private
|
|
69
124
|
|
|
70
|
-
|
|
125
|
+
# `insert_all` skips callbacks, so the cards' own `gift_card.created`
|
|
126
|
+
# lifecycle events never fire. A job publishes one per card once the cards
|
|
127
|
+
# are committed, honouring a write made while lifecycle events were
|
|
128
|
+
# silenced. It resumes from the last card published, so a failure part way
|
|
129
|
+
# through a large batch does not lose the rest.
|
|
130
|
+
#
|
|
131
|
+
# @param after_id [Integer, nil] the batch's last card before this write
|
|
132
|
+
def publish_gift_card_created_events(after_id:)
|
|
133
|
+
return unless Spree::GiftCard.publish_events && Spree::Events.lifecycle_enabled?
|
|
134
|
+
return unless Spree::GiftCard.lifecycle_event_actions.include?(:created)
|
|
135
|
+
|
|
136
|
+
ActiveRecord.after_all_transactions_commit do
|
|
137
|
+
Spree::GiftCards::PublishCreatedEventsJob.perform_later(id, after_id: after_id)
|
|
138
|
+
end
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
def gift_card_hash(code)
|
|
71
142
|
{
|
|
72
143
|
status: :active,
|
|
73
144
|
gift_card_batch_id: id,
|
|
74
145
|
amount: amount,
|
|
75
146
|
currency: currency,
|
|
76
|
-
code:
|
|
147
|
+
code: code,
|
|
77
148
|
store_id: store_id,
|
|
78
149
|
created_by_id: created_by_id,
|
|
79
150
|
expires_at: expires_at
|
|
@@ -4,6 +4,12 @@ module Spree
|
|
|
4
4
|
|
|
5
5
|
has_prefix_id :inv
|
|
6
6
|
|
|
7
|
+
# How long an invitation link works, from the email that carries it.
|
|
8
|
+
VALID_FOR = 2.weeks
|
|
9
|
+
# However often it is resent, an invitation stops working this long after
|
|
10
|
+
# it was first sent.
|
|
11
|
+
MAX_LIFETIME = 30.days
|
|
12
|
+
|
|
7
13
|
has_secure_token
|
|
8
14
|
acts_as_paranoid
|
|
9
15
|
|
|
@@ -70,10 +76,13 @@ module Spree
|
|
|
70
76
|
end
|
|
71
77
|
end
|
|
72
78
|
|
|
73
|
-
# returns true if the invitation has expired
|
|
79
|
+
# returns true if the invitation has expired. An invitation saved without
|
|
80
|
+
# an expiry (older rows) expires VALID_FOR after it was created, so it can
|
|
81
|
+
# never be accepted forever.
|
|
74
82
|
# @return [Boolean]
|
|
75
83
|
def expired?
|
|
76
|
-
expires_at
|
|
84
|
+
deadline = expires_at || created_at&.+(VALID_FOR)
|
|
85
|
+
deadline.present? && deadline < Time.current
|
|
77
86
|
end
|
|
78
87
|
|
|
79
88
|
# @deprecated Call Spree.invitation_accept_workflow — removed in 6.3.
|
|
@@ -110,14 +119,35 @@ module Spree
|
|
|
110
119
|
Rails.application.routes.url_helpers.admin_invitation_acceptance_url(self)
|
|
111
120
|
end
|
|
112
121
|
|
|
122
|
+
# Why the last #resend! refused: :expired, :already_accepted or :revoked.
|
|
123
|
+
# Nil when it sent the email, or when the record failed validation, in
|
|
124
|
+
# which case `errors` says why.
|
|
125
|
+
attr_reader :resend_refusal
|
|
126
|
+
|
|
113
127
|
# Resends the invitation email if the invitation is pending and not expired.
|
|
114
128
|
# The token is rotated first, so a link that leaked with the earlier email
|
|
115
|
-
# stops working
|
|
129
|
+
# stops working, and the invitee gets the full window again from the new
|
|
130
|
+
# email rather than whatever was left of the old one, up to MAX_LIFETIME
|
|
131
|
+
# after the invitation was created. The row is locked and re-read first,
|
|
132
|
+
# so a resend racing an acceptance or a revoke is refused, not reported as
|
|
133
|
+
# sent.
|
|
134
|
+
#
|
|
135
|
+
# @return [Boolean] true when the email went out again, false when refused
|
|
116
136
|
def resend!
|
|
117
|
-
|
|
137
|
+
sent = with_lock do
|
|
138
|
+
@resend_refusal = resend_refusal_reason
|
|
139
|
+
next false if @resend_refusal
|
|
140
|
+
|
|
141
|
+
self.expires_at = [VALID_FOR.from_now, lifetime_ends_at].min
|
|
142
|
+
regenerate_token # saves the new expiry along with the token
|
|
143
|
+
true
|
|
144
|
+
rescue ActiveRecord::RecordInvalid
|
|
145
|
+
restore_attributes
|
|
146
|
+
false
|
|
147
|
+
end
|
|
118
148
|
|
|
119
|
-
|
|
120
|
-
|
|
149
|
+
publish_event('invitation.resent') if sent
|
|
150
|
+
sent
|
|
121
151
|
end
|
|
122
152
|
|
|
123
153
|
# The staff account holding the invited address, if one exists. Matched
|
|
@@ -146,7 +176,21 @@ module Spree
|
|
|
146
176
|
end
|
|
147
177
|
|
|
148
178
|
def set_defaults
|
|
149
|
-
self.expires_at ||=
|
|
179
|
+
self.expires_at ||= VALID_FOR.from_now
|
|
180
|
+
end
|
|
181
|
+
|
|
182
|
+
def resend_refusal_reason
|
|
183
|
+
if deleted?
|
|
184
|
+
:revoked
|
|
185
|
+
elsif accepted?
|
|
186
|
+
:already_accepted
|
|
187
|
+
elsif expired? || lifetime_ends_at <= Time.current
|
|
188
|
+
:expired
|
|
189
|
+
end
|
|
190
|
+
end
|
|
191
|
+
|
|
192
|
+
def lifetime_ends_at
|
|
193
|
+
created_at + MAX_LIFETIME
|
|
150
194
|
end
|
|
151
195
|
|
|
152
196
|
# A role names what it governs, so the invitation follows it — one carrying
|
data/app/models/spree/market.rb
CHANGED
|
@@ -33,7 +33,12 @@ module Spree
|
|
|
33
33
|
def countries=(values)
|
|
34
34
|
self.country_codes = Array(values).map { |country| country.respond_to?(:iso) ? country.iso : country.to_s }
|
|
35
35
|
end
|
|
36
|
-
|
|
36
|
+
# Placed orders keep their market when it is deleted: it is only soft
|
|
37
|
+
# deleted, and an order reads it back for its withdrawal period and return
|
|
38
|
+
# window. Draft orders are still being priced, so they are released and
|
|
39
|
+
# pick a live market on their next save, as carts do.
|
|
40
|
+
has_many :orders, class_name: 'Spree::Order', dependent: nil, inverse_of: :market
|
|
41
|
+
has_many :draft_orders, -> { drafts }, class_name: 'Spree::Order', dependent: :nullify, inverse_of: :market
|
|
37
42
|
|
|
38
43
|
#
|
|
39
44
|
# Preferences
|
data/app/models/spree/media.rb
CHANGED
|
@@ -124,9 +124,24 @@ module Spree
|
|
|
124
124
|
if: :video?
|
|
125
125
|
validates :poster, content_type: Rails.application.config.active_storage.web_image_content_types,
|
|
126
126
|
if: -> { poster.attached? }
|
|
127
|
+
# The checks above trust the type the uploader declared — or, failing
|
|
128
|
+
# that, the file name — so a file of any bytes named `photo.webp` passes
|
|
129
|
+
# them. Every image is resized into its preprocessed variants right after
|
|
130
|
+
# upload, and bytes that are not an image fail there instead, in a job,
|
|
131
|
+
# once per variant. Reading the leading bytes refuses such a file here.
|
|
132
|
+
# Only a newly attached file is read: an unchanged one was checked when it
|
|
133
|
+
# arrived, and reading it again would cost a storage request per save.
|
|
134
|
+
# A row turning into an image is the exception — its file was checked, if
|
|
135
|
+
# at all, as a video — so it is read once more on that save.
|
|
136
|
+
validates_with Spree::AttachmentContentTypeValidator,
|
|
137
|
+
attributes: [:attachment], in: Rails.application.config.active_storage.web_image_content_types,
|
|
138
|
+
if: -> { image? && (attachment_changes.key?('attachment') || will_save_change_to_media_type?) }
|
|
139
|
+
validates_with Spree::AttachmentContentTypeValidator,
|
|
140
|
+
attributes: [:poster], in: Rails.application.config.active_storage.web_image_content_types,
|
|
141
|
+
if: -> { attachment_changes.key?('poster') }
|
|
127
142
|
validates :external_video_url, presence: true, if: :external_video?
|
|
128
143
|
validate :external_video_url_is_supported, if: -> { external_video? && external_video_url.present? }
|
|
129
|
-
validate :
|
|
144
|
+
validate :poster_signed_id_is_usable, if: -> { @poster_signed_id.present? }
|
|
130
145
|
|
|
131
146
|
WEBP_SAVER_OPTIONS = {
|
|
132
147
|
strip: true,
|
|
@@ -395,12 +410,24 @@ module Spree
|
|
|
395
410
|
# A tampered or expired signed id raises deep inside `attach`, which would
|
|
396
411
|
# surface as a 500 after the row is already written. Resolve it up front so
|
|
397
412
|
# a bad one reads as a validation error on the field that carried it.
|
|
398
|
-
|
|
399
|
-
|
|
413
|
+
#
|
|
414
|
+
# The poster's own checks only see it once it is attached, after save, in
|
|
415
|
+
# a second save whose failure `attach` swallows — the request succeeded and
|
|
416
|
+
# the poster silently never appeared. Its bytes are checked here instead,
|
|
417
|
+
# while a refusal can still reach the caller.
|
|
418
|
+
def poster_signed_id_is_usable
|
|
419
|
+
blob = ActiveStorage::Blob.find_signed!(@poster_signed_id)
|
|
420
|
+
return if poster_bytes_validator.permitted_blob?(self, blob)
|
|
421
|
+
|
|
422
|
+
errors.add(:poster, :spree_content_type_not_allowed)
|
|
400
423
|
rescue ActiveSupport::MessageVerifier::InvalidSignature, ActiveRecord::RecordNotFound
|
|
401
424
|
errors.add(:poster, :invalid)
|
|
402
425
|
end
|
|
403
426
|
|
|
427
|
+
def poster_bytes_validator
|
|
428
|
+
self.class.validators_on(:poster).grep(Spree::AttachmentContentTypeValidator).first
|
|
429
|
+
end
|
|
430
|
+
|
|
404
431
|
def external_video_url_is_supported
|
|
405
432
|
return if Spree::ExternalVideo.supported?(external_video_url)
|
|
406
433
|
|