spree-bank_payments 6.0.0 → 6.1.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 691381c2f0df4208b375a5d717b71965f00ce2151104ad709efa8ccc4267b3bc
4
- data.tar.gz: 8e65b580952f031b481730037abcee943117eedc0e529716667e9791103aefa5
3
+ metadata.gz: 0e70d0bdec00b95438a25a779a8e1ef5db3648906f42661bee23cba72186c03c
4
+ data.tar.gz: 935ddbb7b530efe56150eda78d0afcf1eda873216e66e1e4b50e4a78543bbb4a
5
5
  SHA512:
6
- metadata.gz: 16c6e9ffe0a561e23da7d9a2c28a62f8738476f775b965a87cf73f2d4ea24413fcf5c0ec84c8d35d406080d912ef4df854161474c3986b1706d5983ff0aee20d
7
- data.tar.gz: 8c1378d1095f1d0f826cf2772e5ce041a40df623d1980db99bda60a01c7843066176d0c535456f70cff9cd577aeeaadfc55d3491d99b81f49f66b91e44cb24f3
6
+ metadata.gz: 95591d2649a8e6cb6c318029e33ac0c499af3ba99cbcc4a33b673b1b5212aaecd1218bc4069b62dc954fe5657a3a3e6d2029daeb26e36a180019c1d7fd833c38
7
+ data.tar.gz: 8cc010e5de2156ca1543c576eb9ff8631d7f403d69072edb5ec288885432f6d84b30b2f3f29034f5503dd1197cc95544753e0efad19d8e4e2e87bfb641c9fb0d
data/CHANGELOG.md CHANGED
@@ -2,6 +2,64 @@
2
2
 
3
3
  All notable changes to this project are documented in this file.
4
4
 
5
+ ## 6.1.0
6
+
7
+ Found running the gem and its dashboard plugin end to end on a stock Spree 6
8
+ store, which sends the gem's own emails and schedules jobs with Solid Queue.
9
+ 6.0.1 was never released; its fixes ship here.
10
+
11
+ ### Added
12
+
13
+ - **An email when an unpaid order is cancelled.** The bundled mailer now sends
14
+ one on `bank_transfer.expired`: the order, the amount, the deadline that
15
+ passed, and what to do if the transfer was already sent. A store on the
16
+ mailer previously told the buyer nothing, because expiry cancels with
17
+ `notify_customer: false`.
18
+ - **The install generator schedules the jobs.** It writes `ExpireSessionsJob`,
19
+ `SendRemindersJob` and `PollJob` into `config/recurring.yml` (Solid Queue,
20
+ a stock Spree 6 store) or `config/schedule.yml` (sidekiq-cron), chosen from
21
+ the app's Active Job adapter or `--scheduler`. On an existing install, run
22
+ `bin/rails g spree:bank_payments:install --skip-migrations` to schedule
23
+ the jobs only; it schedules nothing twice. A schedule file it cannot edit
24
+ safely (an anchored or aliased `production` key, ERB, no environment keys)
25
+ is left as it is, and the jobs are listed to add by hand.
26
+
27
+ ### Changed
28
+
29
+ - **The instructions and reminder emails name the order and give the
30
+ deadline.** Subjects are "Bank transfer details for order #R123" and
31
+ "Reminder: pay for order #R123 by 11 November 2026", after the store's
32
+ name, as Spree's own emails start. The body gives the deadline as a date
33
+ and time in the store's time zone, and the reminder no longer counts days
34
+ ("within 1 days"). Every email has a plain-text part.
35
+ - The bundled mailer honours the store's "send customer emails" setting
36
+ (`send_consumer_transactional_emails`), as Spree's own customer emails do.
37
+ - The `spree.bank_payments.pay_within` translation is gone; the email copy is
38
+ under `spree.bank_payments.mailer`.
39
+ - The dashboard plugin has unit tests (`pnpm test`), run in CI and before
40
+ each npm release.
41
+
42
+ ### Fixed
43
+
44
+ - **The dashboard queue ignored every filter and the search.** The plugin
45
+ sent the table's filters as bare query parameters (`state_in=unmatched`),
46
+ and Spree's Admin API reads filters only from `q[...]`, so the queue listed
47
+ every transfer whatever the status chip, search or date range said. They
48
+ are now sent as `q[...]`, as Spree's own resource clients send them.
49
+ - **Clearing the queue's filters brought the "unmatched" default back.**
50
+ "Clear all", or "Select all" in the status chip, now lists every transfer.
51
+ Only a fresh visit, from the sidebar, opens on unmatched transfers.
52
+ - **A recorded transfer's date is read in the store's time zone.** The record
53
+ form sends the day the money arrived, and the server read it as midnight
54
+ UTC, so the queue showed a transfer received today as "about 23 hours ago"
55
+ and a store west of UTC could show the day before. The form also defaulted
56
+ to the browser's UTC date, a day ahead every evening west of UTC. Transfers
57
+ already recorded keep their identity, so recording one again still says
58
+ "already recorded". Their stored time is not changed: in a store west of
59
+ UTC, one recorded on 6.0.x still shows the day before.
60
+ - The queue and the transfer page show the day a transfer arrived as a date
61
+ in the store's time zone, not a relative time.
62
+
5
63
  ## 6.0.0
6
64
 
7
65
  Spree 6 support. The major version tracks Spree's, so 6.x supports Spree 6.
data/README.md CHANGED
@@ -26,6 +26,15 @@ bin/rails g spree:bank_payments:install
26
26
  The generator copies the migrations and then asks whether to run them. Pass
27
27
  `--auto-run-migrations` to skip the prompt (useful in scripted installs).
28
28
 
29
+ It then schedules the gem's jobs (see [Scheduling](#scheduling)) in the
30
+ app's scheduler: `config/recurring.yml` when Active Job runs on Solid Queue,
31
+ as a stock Spree 6 store does, or `config/schedule.yml` when it runs on
32
+ Sidekiq with `sidekiq-cron`. Pass `--scheduler=solid_queue`,
33
+ `--scheduler=sidekiq_cron` or `--scheduler=none` to choose. With any other
34
+ scheduler, or a schedule file it cannot edit safely, it lists the jobs for
35
+ you to add. On an existing install, `--skip-migrations` schedules the jobs
36
+ without touching migrations.
37
+
29
38
  ## Configuration
30
39
 
31
40
  Add a **Bank transfer** payment method in the Spree dashboard (its API type
@@ -294,8 +303,27 @@ expires and no reminders send:
294
303
  | `Spree::BankPayments::SendRemindersJob` | Daily | Sends payment reminders as the expiry deadline approaches |
295
304
  | `Spree::BankPayments::PollJob` | Every `poll_interval_minutes` (default 15) | Polls the configured reconciler for new transfers; a successful run is what arms the health gate below |
296
305
 
297
- Wire all three into your scheduler (`sidekiq-cron`, `whenever`, etc.) as part
298
- of installing this gem, not as an afterthought.
306
+ `bin/rails g spree:bank_payments:install` writes them into Solid Queue's
307
+ `config/recurring.yml` or sidekiq-cron's `config/schedule.yml` (or the
308
+ `schedule.yaml` the app already has); running it again adds nothing twice.
309
+ On Solid Queue:
310
+
311
+ ```yaml
312
+ production:
313
+ bank_payments_expire_sessions:
314
+ class: Spree::BankPayments::ExpireSessionsJob
315
+ schedule: every hour at minute 23
316
+ bank_payments_send_reminders:
317
+ class: Spree::BankPayments::SendRemindersJob
318
+ schedule: every day at 9am
319
+ bank_payments_poll:
320
+ class: Spree::BankPayments::PollJob
321
+ schedule: every 15 minutes
322
+ ```
323
+
324
+ With another scheduler (`whenever`, a Kubernetes CronJob), add the same three
325
+ by hand. `PollJob`'s line assumes the default 15-minute poll interval: match
326
+ it to the gateway's `poll_interval_minutes`.
299
327
 
300
328
  ## The health gate
301
329
 
@@ -370,7 +398,19 @@ empty, so a block subscribed first leaves every one of Spree's own
370
398
  subscribers unregistered.
371
399
 
372
400
  A bundled mailer, `Spree::BankPayments::InstructionsMailerSubscriber`, emails
373
- the buyer on `instructions_ready` and `reminder_due`. Disable it with:
401
+ the buyer, through Spree's email layout:
402
+
403
+ | Event | Email |
404
+ |---|---|
405
+ | `instructions_ready` | "Bank transfer details for order #R123": the reference, the amount, the deadline, and the bank details |
406
+ | `reminder_due` | "Reminder: pay for order #R123 by 11 November 2026", with the same details |
407
+ | `expired` | "Order #R123 cancelled: bank transfer not received". No bank details, and a line telling a buyer who already paid to reply |
408
+
409
+ Each gives the deadline as a date and a time in the store's time zone, and
410
+ has a plain-text part. Expiry cancels the order with `notify_customer:
411
+ false`, so the expired email is the only one the buyer gets about it. A
412
+ store whose settings turn off customer emails gets none of these either.
413
+ Disable the mailer alone with:
374
414
 
375
415
  ```ruby
376
416
  Spree::BankPayments::Config.disable_default_mailer = true
@@ -36,7 +36,7 @@ module Spree
36
36
  payment_method: store_bank_transfer_method(params[:payment_method_id]),
37
37
  amount: params[:amount], currency: params[:currency], reference: params[:reference],
38
38
  payer_name: params[:payer_name], occurred_at: params[:occurred_at],
39
- recorded_by: current_user
39
+ time_zone: current_store.preferred_timezone, recorded_by: current_user
40
40
  )
41
41
 
42
42
  render json: serialize_resource(transfer).merge('already_recorded' => already_recorded),
@@ -1,32 +1,64 @@
1
1
  module Spree
2
2
  module BankPayments
3
+ # The default mailer: what the buyer is told about a bank transfer. The
4
+ # instructions when the order is placed, a reminder as the deadline nears,
5
+ # and word that the order was cancelled when the deadline passed unpaid.
6
+ #
7
+ # Every deadline is shown in the store's time zone, as a date and a time:
8
+ # the session expires at an instant, and a buyer reading a UTC date, or a
9
+ # count of days, can be told the wrong day.
3
10
  class InstructionsMailer < Spree::BaseMailer
11
+ helper_method :deadline
12
+
4
13
  # @param payment_session_id [String] the session's prefixed id, as event
5
14
  # payloads carry it
6
15
  def instructions(payment_session_id)
7
- @session = ::Spree::PaymentSessions::BankTransfer.find_by_prefix_id!(payment_session_id)
8
- @order = @session.order
9
- @detail_sets = @session.bank_detail_sets
10
-
11
- with_store_locale(@order.store) do
12
- mail(to: @order.email, subject: I18n.t('spree.bank_payments.reference'))
13
- end
16
+ buyer_mail(:instructions, payment_session_id)
14
17
  end
15
18
 
16
19
  # @param payment_session_id [String] the session's prefixed id
17
20
  def reminder(payment_session_id)
21
+ buyer_mail(:reminder, payment_session_id) { { deadline: deadline(:date) } }
22
+ end
23
+
24
+ # Sent once the order has been cancelled for want of the transfer. It
25
+ # carries no bank details: the order is gone, and a transfer made now
26
+ # matches nothing.
27
+ #
28
+ # @param payment_session_id [String] the session's prefixed id
29
+ def expired(payment_session_id)
30
+ buyer_mail(:expired, payment_session_id)
31
+ end
32
+
33
+ def current_store
34
+ @current_store ||= @order&.store
35
+ end
36
+
37
+ private
38
+
39
+ # @param kind [Symbol] :date for the date alone, as subjects show it
40
+ # @return [String] the session's deadline in the store's time zone
41
+ def deadline(kind = :time)
42
+ I18n.l(@deadline, format: I18n.t("spree.bank_payments.mailer.deadline_#{kind}_format"))
43
+ end
44
+
45
+ # The subject's interpolations, beyond the store and the order number,
46
+ # come from the block, evaluated in the store's locale.
47
+ def buyer_mail(action, payment_session_id)
18
48
  @session = ::Spree::PaymentSessions::BankTransfer.find_by_prefix_id!(payment_session_id)
19
49
  @order = @session.order
20
50
  @detail_sets = @session.bank_detail_sets
21
- @days_remaining = ((@session.expires_at.to_date - Date.current).to_i)
51
+ @deadline = @session.expires_at.in_time_zone(store_time_zone)
22
52
 
23
53
  with_store_locale(@order.store) do
24
- mail(to: @order.email, subject: I18n.t('spree.bank_payments.pay_within', days: @days_remaining))
54
+ subject = I18n.t("spree.bank_payments.mailer.#{action}.subject",
55
+ store: @order.store.name, number: @order.number, **(block_given? ? yield : {}))
56
+ mail(to: @order.email, subject: subject)
25
57
  end
26
58
  end
27
59
 
28
- def current_store
29
- @current_store ||= @order&.store
60
+ def store_time_zone
61
+ ActiveSupport::TimeZone[@order.store.preferred_timezone.to_s] || Time.zone
30
62
  end
31
63
  end
32
64
  end
@@ -17,7 +17,7 @@ module Spree
17
17
  include ActiveModel::Model
18
18
  include ActiveModel::Validations
19
19
 
20
- attr_accessor :payment_method, :amount, :currency, :reference, :payer_name, :occurred_at
20
+ attr_accessor :payment_method, :amount, :currency, :reference, :payer_name, :occurred_at, :time_zone
21
21
 
22
22
  validates :payment_method, presence: { message: :must_be_store_bank_transfer }
23
23
  validates :currency, presence: true
@@ -35,16 +35,33 @@ module Spree
35
35
  nil
36
36
  end
37
37
 
38
+ # A bare date is the day the money arrived on the store's calendar,
39
+ # so it is read in the store's time zone; a timestamp with an offset
40
+ # names its own instant.
38
41
  def occurred_at_value
39
42
  return if occurred_at.to_s.strip.empty?
40
43
 
41
- Time.zone.parse(occurred_at.to_s)
44
+ zone.parse(occurred_at.to_s)
42
45
  rescue ArgumentError
43
46
  nil
44
47
  end
45
48
 
49
+ # The date in the transfer's identity, as 6.0 derived it: the typed
50
+ # day itself, or a timestamp's date in Time.zone. Reading the date in
51
+ # the store's zone must not make a transfer recorded before look new.
52
+ def identity_date
53
+ typed = occurred_at.to_s.strip
54
+ return Date.iso8601(typed) if typed.match?(/\A\d{4}-\d{2}-\d{2}\z/)
55
+
56
+ occurred_at_value.in_time_zone(Time.zone).to_date
57
+ end
58
+
46
59
  private
47
60
 
61
+ def zone
62
+ ActiveSupport::TimeZone[time_zone.to_s] || Time.zone
63
+ end
64
+
48
65
  def amount_positive
49
66
  errors.add(:amount, :greater_than, count: 0) unless amount_value&.positive?
50
67
  end
@@ -54,12 +71,16 @@ module Spree
54
71
  end
55
72
  end
56
73
 
74
+ # @param time_zone [String, nil] the store's time zone, which a bare
75
+ # date in +occurred_at+ is read in; Time.zone when nil
57
76
  # @return [Array(Spree::BankPayments::IncomingTransfer, Boolean)] the
58
77
  # transfer, and whether it had already been recorded
59
78
  # @raise [ActiveModel::ValidationError] when the input is invalid
60
- def self.call(payment_method:, amount:, currency:, reference:, occurred_at:, payer_name: nil, recorded_by: nil)
79
+ def self.call(payment_method:, amount:, currency:, reference:, occurred_at:, payer_name: nil, recorded_by: nil,
80
+ time_zone: nil)
61
81
  entry = Entry.new(payment_method: payment_method, amount: amount, currency: currency,
62
- reference: reference, payer_name: payer_name, occurred_at: occurred_at)
82
+ reference: reference, payer_name: payer_name, occurred_at: occurred_at,
83
+ time_zone: time_zone)
63
84
  entry.validate!
64
85
 
65
86
  new(entry, recorded_by).call
@@ -92,7 +113,7 @@ module Spree
92
113
  currency,
93
114
  IncomingTransfer.normalize_reference(entry.reference),
94
115
  entry.payer_name.to_s.strip.downcase,
95
- entry.occurred_at_value.to_date.iso8601
116
+ entry.identity_date.iso8601
96
117
  ].join('|'))
97
118
 
98
119
  "#{PROVIDER}-#{digest[0, 32]}"
@@ -1,28 +1,44 @@
1
1
  module Spree
2
2
  module BankPayments
3
- # The default mailer: emails the buyer when instructions are ready and
4
- # when a reminder is due. Stores that tell buyers some other way (a
3
+ # The default mailer: emails the buyer when instructions are ready, when a
4
+ # reminder is due, and when the order is cancelled for want of the
5
+ # transfer (the cancellation itself sends no email). Stores that tell buyers some other way (a
5
6
  # storefront sending its own email from webhooks, say) set
6
7
  # Spree::BankPayments::Config.disable_default_mailer, which is read on
7
- # every event so an initializer can set it at any point in boot.
8
+ # every event so an initializer can set it at any point in boot. A store
9
+ # that turns off customer emails in its settings gets none of these
10
+ # either.
8
11
  #
9
12
  # Synchronous: deliver_later already moves the sending off the request.
10
13
  class InstructionsMailerSubscriber < Spree::Subscriber
11
- subscribes_to 'bank_transfer.instructions_ready', 'bank_transfer.reminder_due', async: false
14
+ subscribes_to 'bank_transfer.instructions_ready', 'bank_transfer.reminder_due', 'bank_transfer.expired',
15
+ async: false
12
16
 
13
17
  on 'bank_transfer.instructions_ready', :send_instructions
14
18
  on 'bank_transfer.reminder_due', :send_reminder
19
+ on 'bank_transfer.expired', :send_expired
15
20
 
16
21
  def send_instructions(event)
17
- return if Spree::BankPayments::Config.disable_default_mailer
18
-
19
- Spree::BankPayments::InstructionsMailer.instructions(event.payload['payment_session_id']).deliver_later
22
+ deliver_email(:instructions, event)
20
23
  end
21
24
 
22
25
  def send_reminder(event)
26
+ deliver_email(:reminder, event)
27
+ end
28
+
29
+ def send_expired(event)
30
+ deliver_email(:expired, event)
31
+ end
32
+
33
+ private
34
+
35
+ # Also honours the store's own switch for customer emails, which every
36
+ # one of Spree's customer emails checks.
37
+ def deliver_email(email, event)
23
38
  return if Spree::BankPayments::Config.disable_default_mailer
39
+ return unless (event.store || Spree::Store.default).prefers_send_consumer_transactional_emails?
24
40
 
25
- Spree::BankPayments::InstructionsMailer.reminder(event.payload['payment_session_id']).deliver_later
41
+ Spree::BankPayments::InstructionsMailer.public_send(email, event.payload['payment_session_id']).deliver_later
26
42
  end
27
43
  end
28
44
  end
@@ -0,0 +1,24 @@
1
+ <p>
2
+ <strong><%= t('spree.bank_payments.reference') %>:</strong> <%= @session.reference %><br>
3
+ <strong><%= t('spree.bank_payments.amount_to_transfer') %>:</strong> <%= @session.money.to_s %><br>
4
+ <strong><%= t('spree.bank_payments.transfer_by') %>:</strong> <%= deadline %>
5
+ </p>
6
+
7
+ <p><%= t('spree.bank_payments.reference_help') %></p>
8
+
9
+ <% @detail_sets.each do |set| %>
10
+ <% if set.label.present? %><h3><%= set.label %></h3><% end %>
11
+
12
+ <ul>
13
+ <% if set.beneficiary_name.present? %>
14
+ <li><%= t('spree.bank_payments.beneficiary') %>: <%= set.beneficiary_name %></li>
15
+ <% end %>
16
+
17
+ <%# Label/value pairs, rendered generically: bank coordinates are not
18
+ standardised across countries, so the view must never assume which
19
+ fields exist. %>
20
+ <% set.fields.each do |label, value| %>
21
+ <li><%= label %>: <%= value %></li>
22
+ <% end %>
23
+ </ul>
24
+ <% end %>
@@ -0,0 +1,11 @@
1
+ <%= t('spree.bank_payments.reference') %>: <%= @session.reference %>
2
+ <%= t('spree.bank_payments.amount_to_transfer') %>: <%= @session.money.to_s %>
3
+ <%= t('spree.bank_payments.transfer_by') %>: <%= deadline %>
4
+
5
+ <%= t('spree.bank_payments.reference_help') %>
6
+ <% @detail_sets.each do |set| %>
7
+
8
+ <% if set.label.present? %><%= set.label %>
9
+ <% end %><% if set.beneficiary_name.present? %><%= t('spree.bank_payments.beneficiary') %>: <%= set.beneficiary_name %>
10
+ <% end %><% set.fields.each do |label, value| %><%= label %>: <%= value %>
11
+ <% end %><% end %>
@@ -0,0 +1,8 @@
1
+ <%= render layout: 'spree/shared/mailer_hero' do %>
2
+ <h1><%= t('spree.bank_payments.mailer.expired.heading') %></h1>
3
+ <p class="sub"><%= t('spree.bank_payments.mailer.expired.lead', number: @order.number, amount: @session.money.to_s, deadline: deadline) %></p>
4
+ <% end %>
5
+
6
+ <p><strong><%= t('spree.bank_payments.reference') %>:</strong> <%= @session.reference %></p>
7
+
8
+ <p><%= t('spree.bank_payments.mailer.expired.already_sent') %></p>
@@ -0,0 +1,7 @@
1
+ <%= t('spree.bank_payments.mailer.expired.heading') %>
2
+
3
+ <%= t('spree.bank_payments.mailer.expired.lead', number: @order.number, amount: @session.money.to_s, deadline: deadline) %>
4
+
5
+ <%= t('spree.bank_payments.reference') %>: <%= @session.reference %>
6
+
7
+ <%= t('spree.bank_payments.mailer.expired.already_sent') %>
@@ -1,27 +1,9 @@
1
- <h1><%= I18n.t('spree.bank_payments.reference') %>: <%= @session.reference %></h1>
2
-
3
- <p><%= I18n.t('spree.bank_payments.reference_help') %></p>
4
-
5
- <p>
6
- <strong><%= I18n.t('spree.bank_payments.amount_to_transfer') %>:</strong>
7
- <%= @session.money.to_s %>
8
- </p>
9
-
10
- <% @detail_sets.each do |set| %>
11
- <% if set.label.present? %><h3><%= set.label %></h3><% end %>
12
-
13
- <ul>
14
- <% if set.beneficiary_name.present? %>
15
- <li><%= I18n.t('spree.bank_payments.beneficiary') %>: <%= set.beneficiary_name %></li>
16
- <% end %>
17
-
18
- <%# Label/value pairs, rendered generically: bank coordinates are not
19
- standardised across countries, so the view must never assume which
20
- fields exist. %>
21
- <% set.fields.each do |label, value| %>
22
- <li><%= label %>: <%= value %></li>
23
- <% end %>
24
- </ul>
1
+ <%= render layout: 'spree/shared/mailer_hero' do %>
2
+ <h1><%= t('spree.bank_payments.mailer.instructions.heading') %></h1>
3
+ <p class="sub"><%= t('spree.bank_payments.mailer.instructions.lead', number: @order.number, amount: @session.money.to_s, deadline: deadline) %></p>
25
4
  <% end %>
26
5
 
27
- <p><%= I18n.t('spree.bank_payments.dispatch_note') %></p>
6
+ <%= render 'payment_details' %>
7
+
8
+ <p><%= t('spree.bank_payments.mailer.cancel_warning') %></p>
9
+ <p><%= t('spree.bank_payments.dispatch_note') %></p>
@@ -0,0 +1,8 @@
1
+ <%= t('spree.bank_payments.mailer.instructions.heading') %>
2
+
3
+ <%= t('spree.bank_payments.mailer.instructions.lead', number: @order.number, amount: @session.money.to_s, deadline: deadline) %>
4
+
5
+ <%= render('payment_details').strip %>
6
+
7
+ <%= t('spree.bank_payments.mailer.cancel_warning') %>
8
+ <%= t('spree.bank_payments.dispatch_note') %>
@@ -1,29 +1,9 @@
1
- <p><%= I18n.t('spree.bank_payments.pay_within', days: @days_remaining) %></p>
2
-
3
- <h1><%= I18n.t('spree.bank_payments.reference') %>: <%= @session.reference %></h1>
4
-
5
- <p><%= I18n.t('spree.bank_payments.reference_help') %></p>
6
-
7
- <p>
8
- <strong><%= I18n.t('spree.bank_payments.amount_to_transfer') %>:</strong>
9
- <%= @session.money.to_s %>
10
- </p>
11
-
12
- <% @detail_sets.each do |set| %>
13
- <% if set.label.present? %><h3><%= set.label %></h3><% end %>
14
-
15
- <ul>
16
- <% if set.beneficiary_name.present? %>
17
- <li><%= I18n.t('spree.bank_payments.beneficiary') %>: <%= set.beneficiary_name %></li>
18
- <% end %>
19
-
20
- <%# Label/value pairs, rendered generically: bank coordinates are not
21
- standardised across countries, so the view must never assume which
22
- fields exist. %>
23
- <% set.fields.each do |label, value| %>
24
- <li><%= label %>: <%= value %></li>
25
- <% end %>
26
- </ul>
1
+ <%= render layout: 'spree/shared/mailer_hero' do %>
2
+ <h1><%= t('spree.bank_payments.mailer.reminder.heading') %></h1>
3
+ <p class="sub"><%= t('spree.bank_payments.mailer.reminder.lead', number: @order.number, amount: @session.money.to_s, deadline: deadline) %></p>
27
4
  <% end %>
28
5
 
29
- <p><%= I18n.t('spree.bank_payments.dispatch_note') %></p>
6
+ <%= render 'payment_details' %>
7
+
8
+ <p><%= t('spree.bank_payments.mailer.reminder.already_sent') %></p>
9
+ <p><%= t('spree.bank_payments.dispatch_note') %></p>
@@ -0,0 +1,8 @@
1
+ <%= t('spree.bank_payments.mailer.reminder.heading') %>
2
+
3
+ <%= t('spree.bank_payments.mailer.reminder.lead', number: @order.number, amount: @session.money.to_s, deadline: deadline) %>
4
+
5
+ <%= render('payment_details').strip %>
6
+
7
+ <%= t('spree.bank_payments.mailer.reminder.already_sent') %>
8
+ <%= t('spree.bank_payments.dispatch_note') %>
@@ -25,7 +25,25 @@ en:
25
25
  order_not_placed: "This bank transfer's order was never placed, so there is nothing to apply it to."
26
26
  discount_label: "Bank Transfer discount (%{percent}%)"
27
27
  payment_method_label: "Bank Transfer — save %{percent}%"
28
- pay_within: "Please transfer within %{days} days"
28
+ mailer:
29
+ # I18n.l formats: the deadline is shown in the store's time zone.
30
+ deadline_time_format: "%-d %B %Y, %H:%M %Z"
31
+ deadline_date_format: "%-d %B %Y"
32
+ cancel_warning: "If we don’t receive it by then, we’ll cancel the order."
33
+ instructions:
34
+ subject: "%{store} Bank transfer details for order #%{number}"
35
+ heading: "Pay by bank transfer"
36
+ lead: "Thanks for your order #%{number}. To complete it, please transfer %{amount} by %{deadline}, quoting your payment reference."
37
+ reminder:
38
+ subject: "%{store} Reminder: pay for order #%{number} by %{deadline}"
39
+ heading: "Your bank transfer is due"
40
+ lead: "We haven’t received your bank transfer for order #%{number} yet. Please transfer %{amount} by %{deadline}, quoting your payment reference, or we’ll cancel the order."
41
+ already_sent: "If you’ve already sent it, thank you: a transfer can take a few days to arrive."
42
+ expired:
43
+ subject: "%{store} Order #%{number} cancelled: bank transfer not received"
44
+ heading: "Your order has been cancelled"
45
+ lead: "We didn’t receive your bank transfer of %{amount} for order #%{number} by %{deadline}, so we’ve cancelled the order."
46
+ already_sent: "If you’ve already sent the transfer, reply to this email and we’ll sort it out."
29
47
  dispatch_note: "Your order is dispatched once funds clear."
30
48
  reference: "Payment reference"
31
49
  reference_help: "Please quote this reference exactly, or we may not be able to match your payment."
@@ -2,14 +2,41 @@ module Spree
2
2
  module BankPayments
3
3
  module Generators
4
4
  class InstallGenerator < Rails::Generators::Base
5
- desc 'Copies Spree::BankPayments migrations into the host application and runs them.'
5
+ desc 'Copies Spree::BankPayments migrations into the host application, runs them, ' \
6
+ "and schedules the gem's jobs."
7
+
8
+ # The jobs that must run on a schedule: without them nothing expires
9
+ # and no reminder is sent. Each with its Solid Queue schedule and its
10
+ # cron line. PollJob runs at the gateway's default poll interval (15
11
+ # minutes); a gateway set to another interval needs its line changed.
12
+ SCHEDULE = [
13
+ ['bank_payments_expire_sessions', 'Spree::BankPayments::ExpireSessionsJob',
14
+ 'every hour at minute 23', '23 * * * *'],
15
+ ['bank_payments_send_reminders', 'Spree::BankPayments::SendRemindersJob',
16
+ 'every day at 9am', '0 9 * * *'],
17
+ ['bank_payments_poll', 'Spree::BankPayments::PollJob',
18
+ 'every 15 minutes', '*/15 * * * *']
19
+ ].freeze
6
20
 
7
21
  class_option :auto_run_migrations,
8
22
  type: :boolean,
9
23
  default: false,
10
24
  desc: 'Run the copied migrations immediately instead of asking'
11
25
 
26
+ class_option :skip_migrations,
27
+ type: :boolean,
28
+ default: false,
29
+ desc: 'Copy and run no migrations, only schedule the jobs (on an existing install)'
30
+
31
+ class_option :scheduler,
32
+ type: :string,
33
+ enum: %w[solid_queue sidekiq_cron none],
34
+ desc: "Where to schedule the jobs. Default: the app's Active Job adapter " \
35
+ '(Solid Queue, or Sidekiq with sidekiq-cron)'
36
+
12
37
  def copy_migrations
38
+ return say_status(:skip, 'migrations (--skip-migrations)', :yellow) if options[:skip_migrations]
39
+
13
40
  # Scoped to this engine only: sets ENV["FROM"] = "spree_bank_payments" before
14
41
  # delegating to railties:install:migrations, so migrations pending on other
15
42
  # railties (e.g. action_mailbox, acts_as_taggable_on) are never touched.
@@ -17,6 +44,8 @@ module Spree
17
44
  end
18
45
 
19
46
  def run_migrations
47
+ return if options[:skip_migrations]
48
+
20
49
  if run_migrations?
21
50
  rake 'db:migrate'
22
51
  else
@@ -24,8 +53,180 @@ module Spree
24
53
  end
25
54
  end
26
55
 
56
+ # Writes the jobs into config/recurring.yml (Solid Queue, what a stock
57
+ # Spree 6 store runs) or config/schedule.yml (sidekiq-cron), leaving
58
+ # the entries already there alone. Running it again adds nothing. A
59
+ # file it cannot edit safely is left as it is, and the jobs are listed
60
+ # for adding by hand: the edited file is parsed before it is written,
61
+ # and must keep every entry it had.
62
+ def add_schedule
63
+ case scheduler
64
+ when 'solid_queue' then schedule_in_solid_queue
65
+ when 'sidekiq_cron' then schedule_in_sidekiq_cron
66
+ else say_schedule_by_hand
67
+ end
68
+ end
69
+
27
70
  private
28
71
 
72
+ RECURRING = 'config/recurring.yml'.freeze
73
+ # sidekiq-cron loads schedule.yml, or schedule.yaml when there is no
74
+ # schedule.yml. Writing a .yml beside a .yaml would switch it over, and
75
+ # it then deletes the jobs the .yaml had scheduled.
76
+ SIDEKIQ_CRON_FILES = %w[config/schedule.yml config/schedule.yaml].freeze
77
+
78
+ def scheduler
79
+ options[:scheduler] || detected_scheduler
80
+ end
81
+
82
+ # A Rails 8 app often sets Solid Queue in production.rb only, and the
83
+ # generator runs in development: a bundled solid_queue with a
84
+ # recurring.yml settles it.
85
+ def detected_scheduler
86
+ # Spree's test-app task runs this with no application loaded.
87
+ case Rails.application&.config&.active_job&.queue_adapter.to_s
88
+ when 'solid_queue' then 'solid_queue'
89
+ when 'sidekiq' then bundled?('sidekiq-cron') ? 'sidekiq_cron' : 'none'
90
+ else bundled?('solid_queue') && schedule_file?(RECURRING) ? 'solid_queue' : 'none'
91
+ end
92
+ end
93
+
94
+ def bundled?(gem_name)
95
+ schedule_file?('Gemfile.lock') && schedule_text('Gemfile.lock').match?(/^ #{Regexp.escape(gem_name)} \(/)
96
+ end
97
+
98
+ def schedule_in_solid_queue
99
+ return create_file(RECURRING, "production:\n#{recurring_entries(' ')}") unless schedule_file?(RECURRING)
100
+ return if already_scheduled?(RECURRING)
101
+
102
+ original = schedule_text(RECURRING)
103
+ updated = with_recurring_entries(original)
104
+ return write_schedule(RECURRING, updated) if updated && recurring_kept?(original, updated)
105
+
106
+ say_schedule_by_hand(RECURRING)
107
+ end
108
+
109
+ # Inserts the tasks under a plain `production:` key, at the indentation
110
+ # of its first task. An anchored or aliased key, or a file with no
111
+ # production key, gets nothing.
112
+ def with_recurring_entries(original)
113
+ key = original.match(/^production:[ \t]*(?:#.*)?(\r?\n|\z)/)
114
+ return unless key
115
+
116
+ rest = original[key.end(0)..]
117
+ indent = rest[/\A(?:[ \t]*(?:#.*)?\r?\n)*([ \t]+)\S/, 1] || ' '
118
+ "#{original[0...key.end(0)]}#{"\n" if key[1].empty?}#{recurring_entries(indent)}#{rest}"
119
+ end
120
+
121
+ def recurring_entries(indent)
122
+ tasks = SCHEDULE.map do |key, job, every, _cron|
123
+ "#{indent}#{key}:\n#{indent * 2}class: #{job}\n#{indent * 2}schedule: #{every}\n"
124
+ end
125
+ "#{indent}# spree-bank_payments: expiry, reminders and polling\n#{tasks.join}"
126
+ end
127
+
128
+ def recurring_kept?(original, updated)
129
+ before = load_yaml(original)
130
+ after = load_yaml(updated)
131
+ return false unless before.is_a?(Hash) && after.is_a?(Hash) && after['production'].is_a?(Hash)
132
+
133
+ old_tasks = before['production'] || {}
134
+ old_tasks.is_a?(Hash) &&
135
+ after.except('production') == before.except('production') &&
136
+ after['production'].slice(*old_tasks.keys) == old_tasks &&
137
+ SCHEDULE.all? { |key, job, *| after['production'].dig(key, 'class') == job }
138
+ end
139
+
140
+ def schedule_in_sidekiq_cron
141
+ path = SIDEKIQ_CRON_FILES.find { |file| schedule_file?(file) }
142
+ unless path
143
+ create_file SIDEKIQ_CRON_FILES.first, cron_entries(:hash)
144
+ return say_cron_note(SIDEKIQ_CRON_FILES.first)
145
+ end
146
+ return if already_scheduled?(path)
147
+
148
+ original = schedule_text(path)
149
+ updated = with_cron_entries(original)
150
+ return say_schedule_by_hand(path) unless updated && cron_kept?(original, updated)
151
+
152
+ write_schedule(path, updated)
153
+ say_cron_note(path)
154
+ end
155
+
156
+ # sidekiq-cron accepts a hash of jobs or a list of them. A file with
157
+ # ERB in it cannot be checked before it is written.
158
+ def with_cron_entries(original)
159
+ return if original.include?('<%')
160
+
161
+ existing = load_yaml(original)
162
+ format = existing.is_a?(Array) ? :list : :hash
163
+ return unless existing.nil? || existing.is_a?(Hash) || existing.is_a?(Array)
164
+
165
+ separator = original.empty? || original.end_with?("\n") ? '' : "\n"
166
+ "#{original}#{separator}#{"\n" unless original.strip.empty?}#{cron_entries(format)}"
167
+ rescue Psych::Exception
168
+ nil
169
+ end
170
+
171
+ def cron_entries(format)
172
+ jobs = SCHEDULE.map do |key, job, _every, cron|
173
+ fields = "cron: \"#{cron}\"\n class: #{job}\n queue: default\n"
174
+ format == :list ? "- name: #{key}\n #{fields}" : "#{key}:\n #{fields}"
175
+ end
176
+ "# spree-bank_payments: expiry, reminders and polling\n#{jobs.join}"
177
+ end
178
+
179
+ def cron_kept?(original, updated)
180
+ before = load_yaml(original)
181
+ after = load_yaml(updated)
182
+ ours = SCHEDULE.map(&:first)
183
+
184
+ case before
185
+ when Array
186
+ after.is_a?(Array) && after.first(before.size) == before && (ours - after.filter_map { |job| job['name'] }).empty?
187
+ when Hash, nil, false
188
+ old_jobs = before || {}
189
+ after.is_a?(Hash) && after.slice(*old_jobs.keys) == old_jobs && (ours - after.keys).empty?
190
+ else
191
+ false
192
+ end
193
+ end
194
+
195
+ def say_cron_note(path)
196
+ say_status :note, "sidekiq-cron loads #{path} when Sidekiq starts; if you load another file, move these entries there",
197
+ :yellow
198
+ end
199
+
200
+ def load_yaml(content)
201
+ YAML.safe_load(content, aliases: true)
202
+ rescue Psych::Exception
203
+ :unreadable
204
+ end
205
+
206
+ def schedule_file?(path) = File.exist?(File.join(destination_root, path))
207
+ def schedule_text(path) = File.read(File.join(destination_root, path))
208
+
209
+ def write_schedule(path, content)
210
+ create_file path, content, force: true
211
+ end
212
+
213
+ def already_scheduled?(path)
214
+ return false unless schedule_text(path).include?('Spree::BankPayments::SendRemindersJob')
215
+
216
+ say_status :skip, "#{path} already schedules the bank transfer jobs", :yellow
217
+ true
218
+ end
219
+
220
+ # @param path [String, nil] the schedule file that could not be edited
221
+ def say_schedule_by_hand(path = nil)
222
+ reason = path ? "#{path} could not be edited safely; " : ''
223
+ say_status :todo, "#{reason}schedule these jobs, or nothing expires and no reminder is sent:", :yellow
224
+ SCHEDULE.each { |_key, job, _every, cron| say " #{cron.ljust(14)} #{job}" }
225
+ return if path
226
+
227
+ say ' To write them for you, run again with --scheduler=solid_queue or --scheduler=sidekiq_cron.'
228
+ end
229
+
29
230
  # `ask` returns "" in a non-interactive run (CI, scripted installs), and
30
231
  # "" is not "n", so the default stays "yes, migrate" -- matching the
31
232
  # previous unconditional behaviour.
@@ -1,6 +1,6 @@
1
1
  module Spree
2
2
  module BankPayments
3
3
  # Major version tracks Spree's major version: 6.x supports Spree 6.
4
- VERSION = '6.0.0'.freeze
4
+ VERSION = '6.1.0'.freeze
5
5
  end
6
6
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: spree-bank_payments
3
3
  version: !ruby/object:Gem::Version
4
- version: 6.0.0
4
+ version: 6.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aypex
@@ -78,8 +78,14 @@ files:
78
78
  - app/services/spree/bank_payments/sync_accounts.rb
79
79
  - app/subscribers/spree/bank_payments/instructions_mailer_subscriber.rb
80
80
  - app/subscribers/spree/bank_payments/order_placed_subscriber.rb
81
+ - app/views/spree/bank_payments/instructions_mailer/_payment_details.html.erb
82
+ - app/views/spree/bank_payments/instructions_mailer/_payment_details.text.erb
83
+ - app/views/spree/bank_payments/instructions_mailer/expired.html.erb
84
+ - app/views/spree/bank_payments/instructions_mailer/expired.text.erb
81
85
  - app/views/spree/bank_payments/instructions_mailer/instructions.html.erb
86
+ - app/views/spree/bank_payments/instructions_mailer/instructions.text.erb
82
87
  - app/views/spree/bank_payments/instructions_mailer/reminder.html.erb
88
+ - app/views/spree/bank_payments/instructions_mailer/reminder.text.erb
83
89
  - config/initializers/spree.rb
84
90
  - config/locales/en.yml
85
91
  - config/routes.rb