openreceive-rails 0.5.0 → 0.6.1

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: 04b374d4260a8185bd012b7cfa4591f0132fee90de24e80d230b43afec9642a2
4
- data.tar.gz: 97b049bd0b41d7efc4b1eddd862adc48328b94a5d0b606d8f8eb8191fb05a59e
3
+ metadata.gz: 623952f1ecbcde82c42cb6c7085a53e313f15125dd6cd08fa6a03dee01a0b53e
4
+ data.tar.gz: 60dafd515aabc0cc6b515c4a14468dbb88ccb12da0c8bdb6efd845919ea1fe03
5
5
  SHA512:
6
- metadata.gz: 31f7e710ed4b526591f56b2b83354151bc4284d7ef238b617e837b2c2ef1658da2821d8da1fb21051935a4ef4d22eade92721d995b2283ca1413a2f942876549
7
- data.tar.gz: c8ea1864f564fd6218dc93a869e52466f596f4395ea3255cceeffca2b3bac630f5770ee9570dae65ec1f2cf5512b0b5c2c876e520a2639a1679f282fc0d44eca
6
+ metadata.gz: 5da10b6d09a18269c72357b9f2d3b0e2a44a8616969f49004c48947834ce526c03d3e67a600b8b42d12a1f84353a0342430b5ffe5ad8079cc7a2e23dc834bf7d
7
+ data.tar.gz: 5f903196049e7805f489a18f7a54c567aabc21ac64c2b5734bc2b83c51a5af4ec9f22a5b5f14f17e86dca7e170211c08c78d75ce960dd227dfa136ac25ac46dc
data/CHANGELOG.md CHANGED
@@ -1,5 +1,25 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.6.1 - 2026-11-26
4
+
5
+ - No changes to this gem's code. Released with the complete 0.6.1 package
6
+ family. The Rails quickstart now links "When the payer leaves the page" in
7
+ the deploying guide, for a backstop pass when no payment request follows.
8
+
9
+ ## 0.6.0 - 2026-11-24
10
+
11
+ No breaking API change in this gem.
12
+
13
+ - Fixed: a paid order is never handed a new invoice or a swap deposit
14
+ address. Before a create mints another attempt for an order that has unpaid
15
+ ones, the engine checks those with the wallet (skipping any the request's
16
+ scan just checked) and answers 409 "already paid" if one was paid.
17
+ - Added: `payments/check` answers `paid` with the attempt that paid the order
18
+ on every attempt of a paid order, and `checkouts/prepare` names the order's
19
+ newest unpaid attempt as `pending_payment_hash`, so a reloaded page shows
20
+ the receipt without another pick.
21
+ - The swap changes of 0.6.0 come from `openreceive-server`; see its changelog.
22
+
3
23
  ## 0.5.0 - 2026-11-16
4
24
 
5
25
  No breaking API change in this gem. A Rails app with a custom ("headless")
@@ -81,6 +81,21 @@ module OpenReceive
81
81
  OpenReceive.config.request_handler
82
82
  end
83
83
 
84
+ # Engine mode checks a reference's earlier unpaid attempts against the
85
+ # wallet before a create mints another, skipping those the around_action's
86
+ # gated pass already checked (OpenReceive.check_earlier_attempts!).
87
+ # Advanced mode (custom repository) has no engine-owned rows to check.
88
+ def openreceive_create_arguments
89
+ return {} if OpenReceive.config.advanced_hooks?
90
+
91
+ reconcile_pass = @openreceive_reconcile_pass
92
+ {
93
+ check_earlier_attempts: lambda do |reference|
94
+ OpenReceive.check_earlier_attempts!(reference, reconcile_pass: reconcile_pass)
95
+ end
96
+ }
97
+ end
98
+
84
99
  # Always server-generated (matches RackApp and JS): client-supplied
85
100
  # X-Request-Id values are unvalidated content and are never reflected.
86
101
  def openreceive_request_id
@@ -14,7 +14,8 @@ module OpenReceive
14
14
  openreceive_respond(openreceive_handler.create_checkout(
15
15
  raw_body: openreceive_raw_body,
16
16
  request: request,
17
- request_id: openreceive_request_id
17
+ request_id: openreceive_request_id,
18
+ **openreceive_create_arguments
18
19
  ))
19
20
  end
20
21
  end
@@ -7,7 +7,7 @@ module OpenReceive
7
7
  end
8
8
 
9
9
  def create
10
- respond_with(:create_swap)
10
+ respond_with(:create_swap, **openreceive_create_arguments)
11
11
  end
12
12
 
13
13
  def status
@@ -20,12 +20,13 @@ module OpenReceive
20
20
 
21
21
  private
22
22
 
23
- def respond_with(method)
23
+ def respond_with(method, **arguments)
24
24
  openreceive_respond(openreceive_handler.public_send(
25
25
  method,
26
26
  raw_body: openreceive_raw_body,
27
27
  request: request,
28
- request_id: openreceive_request_id
28
+ request_id: openreceive_request_id,
29
+ **arguments
29
30
  ))
30
31
  end
31
32
  end
@@ -279,6 +279,21 @@ class OpenReceivePayment < ActiveRecord::Base
279
279
  end
280
280
  end
281
281
 
282
+ # One reference's pending attempts, superseded ones included (a superseded
283
+ # invoice can still be paid), as the same reconciliation DTO. A create
284
+ # checks these against the wallet before minting another attempt.
285
+ def self.pending_attempts_for(reference)
286
+ OpenReceiveMeta.assert_supported_schema!
287
+ pending.where(reference: reference.to_s).order(created_at: :asc, payment_hash: :asc)
288
+ .pluck(:payment_hash, :created_at, :checkout_data).map do |hash, created_at, checkout|
289
+ {
290
+ "payment_hash" => hash, "created_at" => created_at.to_i,
291
+ "expires_at" => settlement_expires_at(checkout, hash),
292
+ "created_at_source" => checkout["created_at_source"] || checkout[:created_at_source] || "host"
293
+ }
294
+ end
295
+ end
296
+
282
297
  # Same reconciliation DTO for authenticated by-hash notification lookup.
283
298
  def self.find_pending_attempt(payment_hash)
284
299
  OpenReceiveMeta.assert_supported_schema!
@@ -382,14 +382,15 @@ module OpenReceive
382
382
  lambda do |action:, request:, reference:, input:, pay_in_asset: nil|
383
383
  # Before the host is asked for a price: a host may well answer nil for an
384
384
  # order it already knows is paid, and "unknown order" is the wrong thing
385
- # to tell the payer who paid it.
386
- if %w[checkout.prepare checkout.create swap.create].include?(action)
385
+ # to tell the payer who paid it. payments/check reads it too, to answer
386
+ # `paid` on every attempt of a paid order.
387
+ paid = nil
388
+ if %w[checkout.prepare checkout.create swap.create payment.check].include?(action)
387
389
  paid = OpenReceivePayment.where(reference: reference, status: "settled").order(:created_at).first
388
- if paid
390
+ if paid && action != "payment.check"
389
391
  raise OpenReceive::Server::ConflictError, "This reference is already paid." unless action == "checkout.prepare"
390
392
 
391
- next({ payment_hash: paid.payment_hash, checkout: paid.checkout_data, paid: true,
392
- paid_at: paid.paid_at&.to_i, swap: !paid.swap_data.nil? }.compact)
393
+ next(paid_attempt(paid).merge(paid: true))
393
394
  end
394
395
  end
395
396
  pricing = %w[checkout.prepare swap.quote checkout.create swap.create].include?(action)
@@ -400,8 +401,16 @@ module OpenReceive
400
401
  # the minting service stays exactly the price.
401
402
  amount = price_only(price)
402
403
  description = price_description(price)
403
- if %w[checkout.prepare swap.quote].include?(action)
404
- next({ amount: amount, description: description }.compact)
404
+ next({ amount: amount, description: description }.compact) if action == "swap.quote"
405
+
406
+ if action == "checkout.prepare"
407
+ # The attempt a picker opened on this reference watches: the newest
408
+ # one still pending, superseded or not, since a superseded invoice
409
+ # can still be paid. Any pending attempt would do (`paid` answers for
410
+ # the whole order); the newest is the one most likely still on the
411
+ # payer's wallet screen.
412
+ pending = OpenReceivePayment.where(reference: reference).pending.newest_first.pick(:payment_hash)
413
+ next({ amount: amount, description: description, pending_payment_hash: pending }.compact)
405
414
  end
406
415
 
407
416
  requested_hash = input["payment_hash"] || input[:payment_hash]
@@ -424,11 +433,19 @@ module OpenReceive
424
433
  description: description,
425
434
  payment_hash: payment&.payment_hash,
426
435
  checkout: payment&.checkout_data,
427
- swap_data: payment&.swap_data
436
+ swap_data: payment&.swap_data,
437
+ paid_attempt: paid && paid_attempt(paid)
428
438
  }.compact
429
439
  end
430
440
  end
431
441
 
442
+ # The attempt that paid a reference, as the request handler builds its
443
+ # `paid` object (OpenAPI PaidAttempt) from it.
444
+ def paid_attempt(payment)
445
+ { payment_hash: payment.payment_hash, checkout: payment.checkout_data,
446
+ paid_at: payment.paid_at&.to_i, swap: !payment.swap_data.nil? }.compact
447
+ end
448
+
432
449
  # The price alone. A host that returns a display string beside it must not
433
450
  # have that string reach the amount resolver.
434
451
  def price_only(price)
@@ -5,6 +5,6 @@ module OpenReceive
5
5
  # top-level `::Rails` framework constant — engine code always references the framework as
6
6
  # `::Rails` to avoid shadowing.
7
7
  module Rails
8
- VERSION = "0.5.0"
8
+ VERSION = "0.6.1"
9
9
  end
10
10
  end
@@ -15,6 +15,11 @@ module OpenReceive
15
15
  # Wallet-history pages a request-path pass may walk, mirroring the JS
16
16
  # OPENRECEIVE_RECONCILE_SCAN_MAX_PAGES.
17
17
  RECONCILE_SCAN_MAX_PAGES = 50
18
+ # How far back an earlier attempt's invoice may have expired and still be
19
+ # checked before a new attempt is minted. A payment lands before its invoice
20
+ # expires; this only covers the scan that has not caught up with it yet.
21
+ # Mirrors the JS handler's EARLIER_ATTEMPT_LOOKBACK_SECONDS.
22
+ EARLIER_ATTEMPT_LOOKBACK_SECONDS = 10 * 60
18
23
  # Cap on the `openreceive:notifications` worker's resubscribe backoff, and
19
24
  # the subscription lifetime past which the ramp resets to 1s.
20
25
  NOTIFICATIONS_MAX_BACKOFF_SECONDS = 60
@@ -172,6 +177,33 @@ module OpenReceive
172
177
  { "reason" => "scan_failed" }
173
178
  end
174
179
 
180
+ # The engine's create routes call this right before a new attempt is
181
+ # minted (the request handler's check_earlier_attempts hook): the
182
+ # reference's pending attempts whose invoice expired no more than
183
+ # EARLIER_ATTEMPT_LOOKBACK_SECONDS ago, minus those this request's own
184
+ # gated pass (`reconcile_pass`) already checked, go through one ungated
185
+ # reconcile_attempts! pass. Returns its checks; the handler refuses the
186
+ # mint as "already paid" when one came back settled, and as retryable
187
+ # when this raises. The gated pass at the top of the request is skipped
188
+ # while the gate is closed (3 to 12 s between scans), so a payment made
189
+ # just before a reload would otherwise go unseen. Mirrors the JS handler's
190
+ # assertEarlierAttemptsUnpaid.
191
+ def check_earlier_attempts!(reference, reconcile_pass: nil, now: nil)
192
+ observed_at = Integer(now || Time.now.to_i)
193
+ checked = if reconcile_pass.is_a?(Hash) && reconcile_pass["reason"] == "ran"
194
+ Array(reconcile_pass["checks"]).map { |check| check.fetch("payment_hash").to_s.downcase }
195
+ else
196
+ []
197
+ end
198
+ earlier = OpenReceivePayment.pending_attempts_for(reference).select do |attempt|
199
+ attempt.fetch("expires_at") + EARLIER_ATTEMPT_LOOKBACK_SECONDS > observed_at &&
200
+ !checked.include?(attempt.fetch("payment_hash").downcase)
201
+ end
202
+ return [] if earlier.empty?
203
+
204
+ reconcile_attempts!(earlier, now: observed_at)
205
+ end
206
+
175
207
  # Opt-in NWC-02 notifications: subscribe to the configured NWC client's
176
208
  # `payment_received` notifications. Notifications are authenticated wallet
177
209
  # data — a payload that satisfies the shared settlement rule (`settled_at`
@@ -268,6 +300,54 @@ module OpenReceive
268
300
  false
269
301
  end
270
302
 
303
+ # One ungated, bounded reconcile over exactly these attempts (the JS
304
+ # reconcileHostPayments({ attempts })): no gate claim and no scheduler
305
+ # checkpoint, one Service#reconcile_payments walk capped at
306
+ # RECONCILE_SCAN_MAX_PAGES and RECONCILE_SCAN_TIMEOUT_SECONDS. Settlements
307
+ # go through the settlement hook (write-once + on_paid) and terminal
308
+ # transitions are recorded, like any pass. Unlike the gated pass it raises
309
+ # instead of warning: a failed delivery, a wallet error, or a walk the
310
+ # deadline cut short means the check did not finish, and its caller must
311
+ # not act as if it had. A hash the page cap left undecided is simply
312
+ # omitted, as in the JS walk.
313
+ def reconcile_attempts!(attempts, now: nil)
314
+ return [] if attempts.empty?
315
+
316
+ observed_at = Integer(now || Time.now.to_i)
317
+ deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + RECONCILE_SCAN_TIMEOUT_SECONDS
318
+ checks = config.service.reconcile_payments(
319
+ "attempts" => attempts, "max_pages" => RECONCILE_SCAN_MAX_PAGES, "deadline" => deadline
320
+ )
321
+ by_hash = attempts.to_h { |attempt| [attempt.fetch("payment_hash").downcase, attempt] }
322
+ # One failing delivery must not starve the rest; the first failure is
323
+ # raised once every check has had its turn.
324
+ failures = []
325
+ checks.each do |checked|
326
+ attempt = by_hash[checked.fetch("payment_hash").downcase]
327
+ next if attempt.nil?
328
+
329
+ if checked["status"] == "settled"
330
+ config.settlement_hook.call(
331
+ "payment_hash" => checked.fetch("payment_hash"),
332
+ "paid_at" => checked.fetch("paid_at"),
333
+ "details" => checked["details"]
334
+ )
335
+ else
336
+ record_attempt_transition(attempt, checked, observed_at)
337
+ end
338
+ rescue StandardError => e
339
+ failures << e
340
+ end
341
+ raise failures.first unless failures.empty?
342
+ if checks.length < attempts.length && Process.clock_gettime(Process::CLOCK_MONOTONIC) >= deadline
343
+ raise OpenReceive::WalletUnavailableError,
344
+ "the wallet history walk reached its #{RECONCILE_SCAN_TIMEOUT_SECONDS}s deadline " \
345
+ "before every attempt was checked"
346
+ end
347
+
348
+ checks
349
+ end
350
+
271
351
  # Closure is decided by the shared reconciliation rules from a scan result
272
352
  # the wallet actually returned; a nil transition means keep waiting.
273
353
  def record_attempt_transition(attempt, checked, observed_at)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: openreceive-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - OpenReceive
@@ -15,28 +15,28 @@ dependencies:
15
15
  requirements:
16
16
  - - '='
17
17
  - !ruby/object:Gem::Version
18
- version: 0.5.0
18
+ version: 0.6.1
19
19
  type: :runtime
20
20
  prerelease: false
21
21
  version_requirements: !ruby/object:Gem::Requirement
22
22
  requirements:
23
23
  - - '='
24
24
  - !ruby/object:Gem::Version
25
- version: 0.5.0
25
+ version: 0.6.1
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: openreceive-server
28
28
  requirement: !ruby/object:Gem::Requirement
29
29
  requirements:
30
30
  - - '='
31
31
  - !ruby/object:Gem::Version
32
- version: 0.5.0
32
+ version: 0.6.1
33
33
  type: :runtime
34
34
  prerelease: false
35
35
  version_requirements: !ruby/object:Gem::Requirement
36
36
  requirements:
37
37
  - - '='
38
38
  - !ruby/object:Gem::Version
39
- version: 0.5.0
39
+ version: 0.6.1
40
40
  - !ruby/object:Gem::Dependency
41
41
  name: rails
42
42
  requirement: !ruby/object:Gem::Requirement