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
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 13bfc75ce9c2557cfc32a0e4f8c50de1a9ce84130ce46f85544f265f5ca765b9
|
|
4
|
+
data.tar.gz: adc4c0a08dfaaf0375cc2693c8c8979fae9e6e2c50ca7d4650f282158d26a51f
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 2649fc9c650ff81842e123fadfa7e96494e3f74f256946300f1fee855b2cbe301eb4f13c9000ecca1e6f4c8e7c0029c3b70893cceec63188562e870a8890de6f
|
|
7
|
+
data.tar.gz: 19f50a94a66b7fc175ffc5273689b9dddb69044c316294e37c9abd5b9f4227572c811f4345f7dd8d2e84ebca1a5565f09dfaccabbb00647c3b128c5917fb548d
|
data/app/jobs/spree/base_job.rb
CHANGED
|
@@ -11,11 +11,15 @@ module Spree
|
|
|
11
11
|
class BaseJob < ApplicationJob
|
|
12
12
|
queue_as Spree.queues.default
|
|
13
13
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
14
|
+
# Infrastructure errors that clear up on their own, so every job retries them.
|
|
15
|
+
TRANSIENT_ERRORS = [
|
|
16
|
+
ActiveRecord::Deadlocked,
|
|
17
|
+
ActiveRecord::LockWaitTimeout,
|
|
18
|
+
ActiveRecord::ConnectionNotEstablished,
|
|
19
|
+
ActiveRecord::ConnectionFailed
|
|
20
|
+
].freeze
|
|
21
|
+
|
|
22
|
+
retry_on(*TRANSIENT_ERRORS, wait: :polynomially_longer, attempts: 5)
|
|
19
23
|
retry_on ActiveRecord::RecordNotFound, wait: 2.seconds, attempts: 3
|
|
20
24
|
|
|
21
25
|
discard_on ActiveJob::DeserializationError
|
|
@@ -3,10 +3,12 @@ module Spree
|
|
|
3
3
|
class BulkGenerateJob < ::Spree::BaseJob
|
|
4
4
|
queue_as Spree.queues.gift_cards
|
|
5
5
|
|
|
6
|
-
|
|
6
|
+
# @param id [Integer] the gift card batch
|
|
7
|
+
# @param publish_events [Boolean] whether lifecycle events were on when the batch was created
|
|
8
|
+
def perform(id, publish_events: true)
|
|
7
9
|
gift_cards_batch = Spree::GiftCardBatch.find(id)
|
|
8
10
|
|
|
9
|
-
gift_cards_batch.create_gift_cards
|
|
11
|
+
gift_cards_batch.create_gift_cards(publish_events: publish_events)
|
|
10
12
|
end
|
|
11
13
|
end
|
|
12
14
|
end
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
module Spree
|
|
2
|
+
module GiftCards
|
|
3
|
+
# Publishes `gift_card.created` for the cards a batch bulk-inserted, which
|
|
4
|
+
# skipped the callbacks that would have published it.
|
|
5
|
+
#
|
|
6
|
+
# Continuable: the cursor is the last card published, so a retried or
|
|
7
|
+
# interrupted run resumes there instead of losing or repeating the rest of
|
|
8
|
+
# a batch of up to `gift_card_batch_limit` cards. A card whose event fails
|
|
9
|
+
# to publish is reported and skipped, so it cannot hold back the others;
|
|
10
|
+
# a failing query still raises and is retried.
|
|
11
|
+
class PublishCreatedEventsJob < ::Spree::BaseJob
|
|
12
|
+
include ActiveJob::Continuable
|
|
13
|
+
|
|
14
|
+
queue_as Spree.queues.gift_cards
|
|
15
|
+
|
|
16
|
+
# @param batch_id [Integer] the gift card batch
|
|
17
|
+
# @param after_id [Integer, nil] publish only for the batch's cards after this one
|
|
18
|
+
def perform(batch_id, after_id: nil)
|
|
19
|
+
@batch = Spree::GiftCardBatch.find(batch_id)
|
|
20
|
+
|
|
21
|
+
step :publish, start: after_id
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
private
|
|
25
|
+
|
|
26
|
+
def publish(step)
|
|
27
|
+
gift_cards = @batch.gift_cards.includes(:store)
|
|
28
|
+
gift_cards = gift_cards.where(Spree::GiftCard.arel_table[:id].gt(step.cursor)) if step.cursor
|
|
29
|
+
|
|
30
|
+
gift_cards.find_each(order: :asc) do |gift_card|
|
|
31
|
+
publish_created(gift_card)
|
|
32
|
+
step.set!(gift_card.id)
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def publish_created(gift_card)
|
|
37
|
+
gift_card.publish_event("#{Spree::GiftCard.event_prefix}.created")
|
|
38
|
+
# Retried by Spree::BaseJob, resuming from the cursor instead of skipping the card.
|
|
39
|
+
rescue *TRANSIENT_ERRORS
|
|
40
|
+
raise
|
|
41
|
+
rescue StandardError => e
|
|
42
|
+
Rails.error.report(e, handled: true, context: { gift_card_id: gift_card.id }, source: 'spree.core')
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
@@ -16,6 +16,34 @@ module Spree
|
|
|
16
16
|
# `{ system:, external_id: }` and name itself.
|
|
17
17
|
DEFAULT_EXTERNAL_SYSTEM = 'import'.freeze
|
|
18
18
|
|
|
19
|
+
# The file could not be fetched for a reason on the other end of the
|
|
20
|
+
# wire: the host refused, reset or timed out the connection, its name did
|
|
21
|
+
# not resolve, its certificate was bad, or it answered with an error
|
|
22
|
+
# status (a 404, a 500, a rate limit). A host that is down now may be
|
|
23
|
+
# back in a minute, so the fetch is retried; once the attempts run out
|
|
24
|
+
# the media is simply not imported, as with a blocked address, rather
|
|
25
|
+
# than raised as an error nobody can fix.
|
|
26
|
+
class FetchFailed < StandardError; end
|
|
27
|
+
|
|
28
|
+
# What the remote host can fail with while it is being fetched. Only the
|
|
29
|
+
# fetch itself is guarded: the same classes raised while the download is
|
|
30
|
+
# written to disk or attached (a full disk is an Errno too) are this
|
|
31
|
+
# server's problem and propagate.
|
|
32
|
+
#
|
|
33
|
+
# SsrfFilter::UnresolvedHostname is an SsrfFilter::Error, but it means
|
|
34
|
+
# the name did not resolve right now, not that it points somewhere
|
|
35
|
+
# forbidden, so it is retried rather than discarded with the rest.
|
|
36
|
+
FETCH_ERRORS = [
|
|
37
|
+
SocketError, Timeout::Error, OpenSSL::SSL::SSLError, SystemCallError, EOFError,
|
|
38
|
+
Net::HTTPBadResponse, Net::ProtocolError, SsrfFilter::UnresolvedHostname
|
|
39
|
+
].freeze
|
|
40
|
+
|
|
41
|
+
retry_on(FetchFailed, wait: :polynomially_longer, attempts: Spree::Config.images_save_from_url_job_attempts.to_i) do |job, error|
|
|
42
|
+
Rails.logger.warn(
|
|
43
|
+
"[Spree::Images::SaveFromUrlJob] Gave up fetching #{job.arguments[2].to_s.strip}: #{error.message}"
|
|
44
|
+
)
|
|
45
|
+
end
|
|
46
|
+
|
|
19
47
|
# @param external_id [String, Hash, Array, nil] the identities the source
|
|
20
48
|
# systems know this media by — a bare id, a `{ system:, external_id: }`
|
|
21
49
|
# hash, or a list of them when several systems name the same asset.
|
|
@@ -77,24 +105,23 @@ module Spree
|
|
|
77
105
|
max_size = Spree::Config.max_image_download_size
|
|
78
106
|
streamed_body = nil
|
|
79
107
|
|
|
80
|
-
response =
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
108
|
+
response = fetch(external_url) do |fetched|
|
|
109
|
+
# Every hop calls back, redirects included; their bodies are not the
|
|
110
|
+
# file, but are drained here within the limit, since Net::HTTP would
|
|
111
|
+
# otherwise read one whole into memory. An error status is refused
|
|
112
|
+
# before its body is read, so a large error page is not mistaken for
|
|
113
|
+
# an oversized image.
|
|
114
|
+
next read_within(fetched, max_size, keep: false) if redirect?(fetched)
|
|
115
|
+
|
|
116
|
+
raise_fetch_failed(external_url, fetched) unless success?(fetched)
|
|
117
|
+
|
|
118
|
+
streamed_body = read_within(fetched, max_size)
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
# A fetch that did not stream (a test double, say) is checked here,
|
|
122
|
+
# then read whole and held to the same limit.
|
|
123
|
+
raise_fetch_failed(external_url, response) unless success?(response)
|
|
95
124
|
|
|
96
|
-
# A fetch that did not stream (a test double, say) is read whole and
|
|
97
|
-
# held to the same limit.
|
|
98
125
|
body = streamed_body || response.body
|
|
99
126
|
raise_too_large(max_size) if body.bytesize > max_size
|
|
100
127
|
|
|
@@ -107,24 +134,67 @@ module Spree
|
|
|
107
134
|
tempfile.rewind
|
|
108
135
|
|
|
109
136
|
image.attachment.attach(io: tempfile, filename: filename)
|
|
110
|
-
image
|
|
111
|
-
|
|
137
|
+
# Saved first so a non-image file fails validation here, rather than
|
|
138
|
+
# as a custom field with no owner; together, so a failed URL leaves
|
|
139
|
+
# no image behind for the retry to duplicate.
|
|
140
|
+
image.transaction do
|
|
141
|
+
image.save!
|
|
142
|
+
image.external_url = external_url
|
|
143
|
+
end
|
|
112
144
|
ensure
|
|
113
145
|
tempfile.close
|
|
114
146
|
tempfile.unlink
|
|
115
147
|
end
|
|
116
148
|
end
|
|
117
149
|
|
|
150
|
+
# Fetches the URL through the SSRF filter, turning anything the remote
|
|
151
|
+
# end fails with into FetchFailed so it is retried. Addresses the filter
|
|
152
|
+
# blocks still raise their own SsrfFilter::Error and are discarded.
|
|
153
|
+
def fetch(external_url, &)
|
|
154
|
+
SsrfFilter.get(
|
|
155
|
+
external_url,
|
|
156
|
+
headers: {
|
|
157
|
+
'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
|
|
158
|
+
'Accept' => 'image/avif,image/webp,image/apng,image/svg+xml,image/*,*/*;q=0.8',
|
|
159
|
+
'Accept-Language' => 'en-US,en;q=0.9',
|
|
160
|
+
'Accept-Encoding' => 'gzip, deflate, br',
|
|
161
|
+
'Cache-Control' => 'no-cache',
|
|
162
|
+
'Pragma' => 'no-cache'
|
|
163
|
+
},
|
|
164
|
+
http_options: {
|
|
165
|
+
read_timeout: 60,
|
|
166
|
+
open_timeout: 30
|
|
167
|
+
},
|
|
168
|
+
&
|
|
169
|
+
)
|
|
170
|
+
rescue *FETCH_ERRORS => e
|
|
171
|
+
raise FetchFailed, "#{e.class}: #{e.message}"
|
|
172
|
+
end
|
|
173
|
+
|
|
174
|
+
def success?(response)
|
|
175
|
+
response.code.to_s.start_with?('2')
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
def redirect?(response)
|
|
179
|
+
response.code.to_s.start_with?('3')
|
|
180
|
+
end
|
|
181
|
+
|
|
182
|
+
def raise_fetch_failed(external_url, response)
|
|
183
|
+
raise FetchFailed, "#{external_url} responded with HTTP #{response.code}"
|
|
184
|
+
end
|
|
185
|
+
|
|
118
186
|
# Reads the body as it arrives and stops at the limit, so an oversized
|
|
119
187
|
# or endless response is refused mid-stream rather than held in memory
|
|
120
188
|
# first; a declared length over the limit is refused before any of it.
|
|
121
|
-
def read_within(response, max_size)
|
|
189
|
+
def read_within(response, max_size, keep: true)
|
|
122
190
|
raise_too_large(max_size) if response['Content-Length'].to_i > max_size
|
|
123
191
|
|
|
124
192
|
buffer = +''
|
|
193
|
+
size = 0
|
|
125
194
|
response.read_body do |chunk|
|
|
126
|
-
|
|
127
|
-
raise_too_large(max_size) if
|
|
195
|
+
size += chunk.bytesize
|
|
196
|
+
raise_too_large(max_size) if size > max_size
|
|
197
|
+
buffer << chunk if keep
|
|
128
198
|
end
|
|
129
199
|
buffer
|
|
130
200
|
end
|
|
@@ -14,7 +14,42 @@ module Spree
|
|
|
14
14
|
# skips a payment that is already completed.
|
|
15
15
|
class SettlementFailed < StandardError; end
|
|
16
16
|
|
|
17
|
+
# Reported, not raised: the payment is settled on the cart, but the cart
|
|
18
|
+
# cannot become an order — see DETERMINISTIC_FAILURE_CODES.
|
|
19
|
+
class CompletionBlocked < StandardError; end
|
|
20
|
+
|
|
21
|
+
# Failures of Spree::Carts::Complete that no retry can change. The cart
|
|
22
|
+
# fails a checkout requirement (emptied, no delivery method selected;
|
|
23
|
+
# out of stock alone is retried, see RECOVERABLE_REQUIREMENT_CODES),
|
|
24
|
+
# lost a batch coupon code it was priced with, or its tax engine
|
|
25
|
+
# refused the sale — and a cart a gateway took
|
|
26
|
+
# money for is fenced (Spree::Cart#payment_session_settled?), so nobody
|
|
27
|
+
# can change it into one that completes. Retrying only repeats the
|
|
28
|
+
# failure; the payment stays recorded on the cart and the failure is
|
|
29
|
+
# reported once for staff to resolve.
|
|
30
|
+
DETERMINISTIC_FAILURE_CODES = %w[validation_failed coupon_code_unavailable tax_calculation_refused].freeze
|
|
31
|
+
|
|
32
|
+
# Checkout requirements a paid cart can meet later without being
|
|
33
|
+
# changed: stock held by another cart's reservation comes back when
|
|
34
|
+
# that reservation expires or the item is restocked.
|
|
35
|
+
RECOVERABLE_REQUIREMENT_CODES = %w[out_of_stock].freeze
|
|
36
|
+
|
|
37
|
+
# The cart failed only requirements in RECOVERABLE_REQUIREMENT_CODES.
|
|
38
|
+
# Retried until every reservation that could be holding the stock has
|
|
39
|
+
# expired: the store's reservation TTL
|
|
40
|
+
# (Spree::Store#preferred_stock_reservation_ttl_minutes, unbounded)
|
|
41
|
+
# plus STOCK_GRACE after settlement. Past that, it is reported like a
|
|
42
|
+
# blocked completion.
|
|
43
|
+
class StockUnavailable < StandardError; end
|
|
44
|
+
|
|
45
|
+
STOCK_RETRY_WAIT = 5.minutes
|
|
46
|
+
# Leaves the expiry job time to release reservations past their TTL.
|
|
47
|
+
STOCK_GRACE = 10.minutes
|
|
48
|
+
|
|
17
49
|
retry_on SettlementFailed, wait: :polynomially_longer, attempts: 5
|
|
50
|
+
# Bounded by #stock_retry_deadline, not an attempt count: a store's
|
|
51
|
+
# reservation TTL can outlast any fixed number of attempts.
|
|
52
|
+
retry_on StockUnavailable, wait: STOCK_RETRY_WAIT, attempts: :unlimited
|
|
18
53
|
|
|
19
54
|
# metadata defaults to {} so jobs enqueued before it was passed still run.
|
|
20
55
|
def perform(payment_method_id:, action:, payment_session_id:, metadata: {})
|
|
@@ -28,8 +63,79 @@ module Spree
|
|
|
28
63
|
payment_session: payment_session,
|
|
29
64
|
metadata: metadata
|
|
30
65
|
)
|
|
66
|
+
return unless result&.failure?
|
|
67
|
+
|
|
68
|
+
if awaiting_stock?(result) && Time.current < stock_retry_deadline(payment_session)
|
|
69
|
+
raise StockUnavailable,
|
|
70
|
+
"Payment session #{payment_session.id} settled, but its cart is out of stock (#{result.error}); " \
|
|
71
|
+
'the buyer was charged without an order'
|
|
72
|
+
end
|
|
73
|
+
return report_blocked_completion(payment_session, result) if completion_blocked?(result)
|
|
31
74
|
|
|
32
|
-
raise SettlementFailed, "Payment session #{payment_session.id}: #{result.error}"
|
|
75
|
+
raise SettlementFailed, "Payment session #{payment_session.id}: #{result.error}"
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
private
|
|
79
|
+
|
|
80
|
+
# A workflow can fail with a bare message rather than a coded
|
|
81
|
+
# ResultError; that reads as a retryable settlement failure.
|
|
82
|
+
def failure_code(result)
|
|
83
|
+
result.error.code if result.error.respond_to?(:code)
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
def completion_blocked?(result)
|
|
87
|
+
failure_code(result).in?(DETERMINISTIC_FAILURE_CODES)
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def awaiting_stock?(result)
|
|
91
|
+
return false unless failure_code(result) == 'validation_failed'
|
|
92
|
+
|
|
93
|
+
codes = requirement_codes(result)
|
|
94
|
+
codes.any? && (codes - RECOVERABLE_REQUIREMENT_CODES).empty?
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
# Measured from when the payment was settled, which a replay of the
|
|
98
|
+
# webhook does not move. The workflow settles the session through
|
|
99
|
+
# update_all, so it is read again rather than trusted from memory,
|
|
100
|
+
# where it still carries the time it was last pending.
|
|
101
|
+
def stock_retry_deadline(payment_session)
|
|
102
|
+
session = Spree::PaymentSession.find(payment_session.id)
|
|
103
|
+
settled_at = session.own_payment&.created_at || session.updated_at
|
|
104
|
+
settled_at + Spree::StockReservation.ttl_for(session.cart || session.order) + STOCK_GRACE
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
def requirement_codes(result)
|
|
108
|
+
requirements = result.error.value.is_a?(Hash) ? Array(result.error.value[:errors]) : []
|
|
109
|
+
requirements.filter_map { |requirement| requirement[:code]&.to_s if requirement.is_a?(Hash) }
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
# Charged without an order is the outcome to make loud: reported as an
|
|
113
|
+
# unhandled error so the error tracker alerts on it, with what staff
|
|
114
|
+
# need to find the money (the payment session's gateway reference) and
|
|
115
|
+
# why the cart could not complete.
|
|
116
|
+
def report_blocked_completion(payment_session, result)
|
|
117
|
+
code = failure_code(result)
|
|
118
|
+
failure = result.error.value.is_a?(Hash) ? result.error.value : {}
|
|
119
|
+
error = CompletionBlocked.new(
|
|
120
|
+
"Payment session #{payment_session.id} settled, but its #{payment_session.cart_id ? 'cart' : 'order'} " \
|
|
121
|
+
"cannot be completed (#{code}); the buyer was charged without an order"
|
|
122
|
+
)
|
|
123
|
+
|
|
124
|
+
Rails.error.report(
|
|
125
|
+
error,
|
|
126
|
+
handled: false,
|
|
127
|
+
severity: :error,
|
|
128
|
+
context: {
|
|
129
|
+
payment_session_id: payment_session.id,
|
|
130
|
+
payment_session_external_id: payment_session.external_id,
|
|
131
|
+
payment_id: payment_session.own_payment&.id,
|
|
132
|
+
cart_id: payment_session.cart_id,
|
|
133
|
+
order_id: payment_session.order_id,
|
|
134
|
+
code: code,
|
|
135
|
+
errors: failure[:errors] || failure[:message]
|
|
136
|
+
}.compact,
|
|
137
|
+
source: 'spree.payments.webhook'
|
|
138
|
+
)
|
|
33
139
|
end
|
|
34
140
|
end
|
|
35
141
|
end
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
module Spree
|
|
2
|
+
# Security notices about a store's API keys, sent to its admins.
|
|
3
|
+
class ApiKeyMailer < BaseMailer
|
|
4
|
+
IP_ADDRESS = 'created_by_ip_address'.freeze
|
|
5
|
+
|
|
6
|
+
# The key as the email shows it, taken when the key is created. The notice
|
|
7
|
+
# is sent from this copy rather than the record, so deleting the key
|
|
8
|
+
# before the job runs cannot suppress it. The IP stays encrypted while the
|
|
9
|
+
# copy waits in the job queue.
|
|
10
|
+
#
|
|
11
|
+
# @param api_key [Spree::ApiKey]
|
|
12
|
+
# @return [Hash]
|
|
13
|
+
def self.notice_for(api_key)
|
|
14
|
+
notice = Spree::Emails::TemplateData.call(api_key, Spree::Emails::ApiKeySerializer, store: api_key.store)
|
|
15
|
+
notice.merge(IP_ADDRESS => encrypt_ip_address(notice[IP_ADDRESS]))
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
# Tells one store admin that a secret (Admin API) key was created.
|
|
19
|
+
#
|
|
20
|
+
# @param store [Spree::Store] the key's store
|
|
21
|
+
# @param notice [Hash] from {.notice_for}
|
|
22
|
+
# @param admin_user [Object] the admin to notify
|
|
23
|
+
def secret_key_created(store, notice, admin_user)
|
|
24
|
+
@current_store = store
|
|
25
|
+
|
|
26
|
+
with_store_locale(store, staff_locale(admin_user, store)) do
|
|
27
|
+
mail_template(
|
|
28
|
+
{
|
|
29
|
+
api_key: notice.merge(IP_ADDRESS => self.class.decrypt_ip_address(notice[IP_ADDRESS])),
|
|
30
|
+
user: email_data(admin_user, Spree::Emails::UserSerializer),
|
|
31
|
+
review_url: "#{Spree::Stores::DashboardUrl.call(store: store)}/#{store.prefixed_id}/settings/api-keys"
|
|
32
|
+
},
|
|
33
|
+
to: admin_user.email, store_url: store.formatted_url
|
|
34
|
+
)
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# Encrypted the same way as Spree::ApiKey#created_by_ip_address: only when
|
|
39
|
+
# Active Record encryption keys are configured.
|
|
40
|
+
def self.encrypt_ip_address(value)
|
|
41
|
+
return value if value.blank? || !ActiveRecord::Encryption.config.has_primary_key?
|
|
42
|
+
|
|
43
|
+
ActiveRecord::Encryption.encryptor.encrypt(value)
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def self.decrypt_ip_address(value)
|
|
47
|
+
return value if value.blank? || !ActiveRecord::Encryption.config.has_primary_key?
|
|
48
|
+
|
|
49
|
+
ActiveRecord::Encryption.encryptor.decrypt(value)
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
end
|
|
@@ -49,11 +49,10 @@ module Spree
|
|
|
49
49
|
super(resolved)
|
|
50
50
|
end
|
|
51
51
|
|
|
52
|
-
# Returns the Stripe-style prefixed ID, or nil for unsaved records
|
|
52
|
+
# Returns the Stripe-style prefixed ID, or nil for unsaved records and
|
|
53
|
+
# models without `has_prefix_id`.
|
|
53
54
|
def prefixed_id
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
"#{self.class._prefix_id_prefix}_#{Spree::PrefixedId::SQIDS.encode([id])}"
|
|
55
|
+
self.class.prefixed_id_for(id)
|
|
57
56
|
end
|
|
58
57
|
|
|
59
58
|
# Use prefixed_id for URL params when available.
|
|
@@ -145,12 +144,19 @@ module Spree
|
|
|
145
144
|
# Builds the prefixed id for a raw primary key without loading the row —
|
|
146
145
|
# for serializing foreign keys (e.g. Order#cart_id) where fetching the
|
|
147
146
|
# associated record just to re-encode its id would be wasted I/O.
|
|
147
|
+
#
|
|
148
|
+
# Answers nil when the model has no prefix, so no client ever sees a
|
|
149
|
+
# bare `_<sqid>`, and for a key Sqids cannot encode (not an integer,
|
|
150
|
+
# negative, or past its range).
|
|
148
151
|
# @param id [Integer, String, nil]
|
|
149
152
|
# @return [String, nil]
|
|
150
153
|
def prefixed_id_for(id)
|
|
151
|
-
return nil if
|
|
154
|
+
return nil if _prefix_id_prefix.blank?
|
|
152
155
|
|
|
153
|
-
|
|
156
|
+
key = id.is_a?(Integer) ? id : Integer(id.to_s, 10, exception: false)
|
|
157
|
+
"#{_prefix_id_prefix}_#{Spree::PrefixedId::SQIDS.encode([key])}" if key
|
|
158
|
+
rescue ArgumentError
|
|
159
|
+
nil
|
|
154
160
|
end
|
|
155
161
|
|
|
156
162
|
def find_by_prefix_id!(prefixed_id)
|
|
@@ -31,9 +31,12 @@ module Spree
|
|
|
31
31
|
!completed? && payments.completed.empty?
|
|
32
32
|
end
|
|
33
33
|
|
|
34
|
+
# Items on a canceled fulfillment keep their backordered status, but
|
|
35
|
+
# nothing waits on that stock any more.
|
|
36
|
+
#
|
|
34
37
|
# @return [Boolean]
|
|
35
38
|
def backordered?
|
|
36
|
-
fulfillment_items.any?(&:backordered?)
|
|
39
|
+
fulfillments.not_canceled.includes(:fulfillment_items).any?(&:backordered?)
|
|
37
40
|
end
|
|
38
41
|
|
|
39
42
|
# A +prices_hidden+ channel also disallows guest completion regardless
|
|
@@ -3,9 +3,9 @@ module Spree
|
|
|
3
3
|
module Market
|
|
4
4
|
extend ActiveSupport::Concern
|
|
5
5
|
|
|
6
|
+
# The including model declares `belongs_to :market` itself: an order
|
|
7
|
+
# reads a deleted market back, a cart does not.
|
|
6
8
|
included do
|
|
7
|
-
belongs_to :market, class_name: 'Spree::Market'
|
|
8
|
-
|
|
9
9
|
attr_accessor :skip_market_resolution
|
|
10
10
|
|
|
11
11
|
before_validation :ensure_market_presence
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
module Spree
|
|
2
|
+
module Purchase
|
|
3
|
+
# Request-scoped messages for the buyer (an item removed, a delivery that
|
|
4
|
+
# no longer fits), shared by Spree::Cart and Spree::Order. Never saved.
|
|
5
|
+
module Warnings
|
|
6
|
+
extend ActiveSupport::Concern
|
|
7
|
+
|
|
8
|
+
included do
|
|
9
|
+
attribute :warnings, default: -> { [] }
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
# The lock refuses unsaved attributes and reloads the record, which would
|
|
13
|
+
# either fail or drop this request's warnings, so they are carried across
|
|
14
|
+
# without counting as a change.
|
|
15
|
+
def lock!(lock = true)
|
|
16
|
+
return super unless persisted?
|
|
17
|
+
|
|
18
|
+
existing_warnings = warnings
|
|
19
|
+
clear_attribute_changes([:warnings])
|
|
20
|
+
begin
|
|
21
|
+
super
|
|
22
|
+
ensure
|
|
23
|
+
self.warnings = existing_warnings
|
|
24
|
+
clear_attribute_changes([:warnings])
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
@@ -18,8 +18,10 @@ module Spree
|
|
|
18
18
|
|
|
19
19
|
accepts_nested_attributes_for :ship_address, :bill_address
|
|
20
20
|
|
|
21
|
-
|
|
22
|
-
|
|
21
|
+
# `id` settles addresses saved in the same instant, which PostgreSQL
|
|
22
|
+
# otherwise returns in either order.
|
|
23
|
+
has_many :addresses, -> { where(deleted_at: nil).order(updated_at: :desc, id: :asc) },
|
|
24
|
+
class_name: 'Spree::Address', as: :owner, inverse_of: :owner, dependent: nil
|
|
23
25
|
|
|
24
26
|
validate :address_not_associated_with_other_user, :address_not_deprecated_in_completed_order
|
|
25
27
|
|
|
@@ -30,16 +30,40 @@ module Spree
|
|
|
30
30
|
role.role_users.find_or_create_by!(user: user)
|
|
31
31
|
end
|
|
32
32
|
|
|
33
|
-
# Revokes a user's access to the resource
|
|
33
|
+
# Revokes a user's access to the resource, including any invitation still
|
|
34
|
+
# pending for them here, so they cannot rejoin by accepting it.
|
|
34
35
|
# @param user [Spree.admin_user_class] The user to remove from the resource
|
|
35
36
|
# @return [void]
|
|
36
37
|
def remove_user(user)
|
|
37
|
-
|
|
38
|
+
# Invitations first: an acceptance locks its invitation row, so this
|
|
39
|
+
# waits for one in flight and then removes the role it granted.
|
|
40
|
+
transaction do
|
|
41
|
+
revoke_pending_invitations_for(user)
|
|
42
|
+
# One DELETE, evaluated against the latest committed rows. A per-row
|
|
43
|
+
# destroy first SELECTs from MySQL's repeatable-read snapshot, which
|
|
44
|
+
# misses a role an acceptance committed while this waited on its lock.
|
|
45
|
+
Spree::RoleUser.where(user: user, role: roles).delete_all
|
|
46
|
+
end
|
|
38
47
|
end
|
|
39
48
|
|
|
40
49
|
# this can be overridden in the base model to use a different user role, eg. 'seller'
|
|
41
50
|
def default_user_role
|
|
42
51
|
Spree::Role.default_admin_role(self)
|
|
43
52
|
end
|
|
53
|
+
|
|
54
|
+
private
|
|
55
|
+
|
|
56
|
+
# Matched by invitee and, case-insensitively, by email: an invitation sent
|
|
57
|
+
# before the account existed carries only the address.
|
|
58
|
+
def revoke_pending_invitations_for(user)
|
|
59
|
+
pending = invitations.pending
|
|
60
|
+
by_email = pending.where("LOWER(#{Spree::Invitation.table_name}.email) = LOWER(?)", user.email) if user.email.present?
|
|
61
|
+
matching = pending.where(invitee: user)
|
|
62
|
+
matching = matching.or(by_email) if by_email
|
|
63
|
+
|
|
64
|
+
# A locking read sees invitations committed after this transaction began,
|
|
65
|
+
# which MySQL's repeatable-read snapshot would otherwise hide.
|
|
66
|
+
matching.lock.destroy_all
|
|
67
|
+
end
|
|
44
68
|
end
|
|
45
69
|
end
|
data/app/models/spree/api_key.rb
CHANGED
|
@@ -52,6 +52,10 @@ module Spree
|
|
|
52
52
|
belongs_to :created_by, polymorphic: true, optional: true
|
|
53
53
|
belongs_to :revoked_by, polymorphic: true, optional: true
|
|
54
54
|
|
|
55
|
+
# Personal data: where the creating request came from, set by the Admin
|
|
56
|
+
# API and named in the security notice sent to store admins.
|
|
57
|
+
encrypts :created_by_ip_address, support_unencrypted_data: true if ActiveRecord::Encryption.config.has_primary_key?
|
|
58
|
+
|
|
55
59
|
validates :name, presence: true
|
|
56
60
|
validates_store_uniqueness :name, soft_delete_column: :revoked_at
|
|
57
61
|
validates :key_type, presence: true, inclusion: { in: KEY_TYPES }
|
|
@@ -74,6 +78,11 @@ module Spree
|
|
|
74
78
|
validate :channel_immutable, if: -> { persisted? && channel_id_changed? }
|
|
75
79
|
|
|
76
80
|
before_validation :generate_token, on: :create
|
|
81
|
+
after_create_commit :notify_store_admins, if: -> { secret? && !skip_admin_notification }
|
|
82
|
+
|
|
83
|
+
# Set when a key is minted as part of setting up a store (the CLI's own
|
|
84
|
+
# read-only key), which is not news to anyone. Never persisted.
|
|
85
|
+
attr_accessor :skip_admin_notification
|
|
77
86
|
|
|
78
87
|
scope :active, -> { where(revoked_at: nil) }
|
|
79
88
|
scope :revoked, -> { where.not(revoked_at: nil) }
|
|
@@ -176,6 +185,21 @@ module Spree
|
|
|
176
185
|
end
|
|
177
186
|
end
|
|
178
187
|
|
|
188
|
+
# A secret key can do anything its scopes allow, so every admin hears
|
|
189
|
+
# about a new one — a key nobody recognizes is the first sign of a
|
|
190
|
+
# compromised account.
|
|
191
|
+
def notify_store_admins
|
|
192
|
+
notice = Spree::ApiKeyMailer.notice_for(self)
|
|
193
|
+
|
|
194
|
+
store.users.merge(Spree::Role.admin).find_each do |admin_user|
|
|
195
|
+
Spree::ApiKeyMailer.secret_key_created(store, notice, admin_user).deliver_later
|
|
196
|
+
rescue StandardError => e
|
|
197
|
+
# The key already exists; one admin's failed enqueue must neither
|
|
198
|
+
# fail the request nor cost the other admins their notice.
|
|
199
|
+
Rails.error.report(e, source: 'spree.api_keys')
|
|
200
|
+
end
|
|
201
|
+
end
|
|
202
|
+
|
|
179
203
|
def channel_immutable
|
|
180
204
|
errors.add(:channel, :api_key_channel_immutable, message: I18n.t('spree.api_key_channel_immutable'))
|
|
181
205
|
end
|
data/app/models/spree/base.rb
CHANGED
|
@@ -270,6 +270,35 @@ class Spree::Base < ApplicationRecord
|
|
|
270
270
|
type.to_s.safe_constantize.try(:prefixed_id_for, id)
|
|
271
271
|
end
|
|
272
272
|
|
|
273
|
+
# What a client reads for `value` held in `attribute` — the inverse of
|
|
274
|
+
# {.api_type_resolver}. A foreign key becomes the prefixed id of the record
|
|
275
|
+
# it points at, a column storing a class name becomes its short name, and
|
|
276
|
+
# anything else is returned unchanged. Override with `api_type_resolver`
|
|
277
|
+
# for a column whose stored values need their own mapping.
|
|
278
|
+
#
|
|
279
|
+
# @param attribute [String, Symbol]
|
|
280
|
+
# @param value [Object] a value as this model stores it
|
|
281
|
+
# @return [Object, nil] nil when the value has no form a client may see
|
|
282
|
+
def api_value_for(attribute, value)
|
|
283
|
+
model = self.class
|
|
284
|
+
attribute = model.attribute_aliases[attribute.to_s] || attribute.to_s
|
|
285
|
+
reference = model.reflect_on_all_associations(:belongs_to).find { |reflection| reflection.foreign_key.to_s == attribute }
|
|
286
|
+
|
|
287
|
+
if attribute == model.inheritance_column
|
|
288
|
+
model.base_class.api_type_for(value)
|
|
289
|
+
elsif reference
|
|
290
|
+
if reference.polymorphic?
|
|
291
|
+
Spree::Base.polymorphic_prefixed_id(self[reference.foreign_type], value)
|
|
292
|
+
else
|
|
293
|
+
reference.klass.try(:prefixed_id_for, value)
|
|
294
|
+
end
|
|
295
|
+
elsif model.api_type_resolver(attribute)
|
|
296
|
+
Spree::Base.polymorphic_api_type(value)
|
|
297
|
+
else
|
|
298
|
+
value
|
|
299
|
+
end
|
|
300
|
+
end
|
|
301
|
+
|
|
273
302
|
# Reads the arguments of a two-state ransackable scope — one that answers a
|
|
274
303
|
# question with two named sides ("erased" / "not erased", "outstanding" /
|
|
275
304
|
# "spent") rather than narrowing to a value.
|