ap2_mandate_verifier 1.6.0 → 1.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.
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+ gem "sqlite3", "= 2.10.2"
3
+ require "sqlite3"
4
+ module RubyIntegration
5
+ # Separate mock processor ledger: a committed row IS the synthetic side effect.
6
+ # This is not atomicity between an external PSP and an application's database.
7
+ class Processor
8
+ SCHEMA = "CREATE TABLE results (processor_key TEXT PRIMARY KEY, reference TEXT NOT NULL, receipt TEXT)"
9
+ def self.create(path)
10
+ File.open(path, File::WRONLY | File::CREAT | File::EXCL, 0o600) { |f| f.fsync }
11
+ db = SQLite3::Database.new(path, flags: SQLite3::Constants::Open::READWRITE)
12
+ db.execute(SCHEMA)
13
+ db.close
14
+ end
15
+ def initialize(path)
16
+ @db = SQLite3::Database.new(path, flags: SQLite3::Constants::Open::READWRITE)
17
+ raise "mock storage unavailable" unless @db.get_first_value("PRAGMA quick_check") == "ok" &&
18
+ @db.get_first_value("SELECT sql FROM sqlite_master WHERE name='results'") == SCHEMA
19
+ @db.busy_timeout = 5000
20
+ @db.execute("PRAGMA synchronous=FULL")
21
+ end
22
+ def dispatch(execution, receipt:)
23
+ @db.transaction(:immediate) do
24
+ old = query(execution.processor_key)
25
+ if old
26
+ raise "mock reference conflict" unless old.fetch("reference") == execution.reference
27
+ else
28
+ @db.execute("INSERT INTO results VALUES (?,?,?)", [execution.processor_key, execution.reference, receipt])
29
+ end
30
+ end
31
+ end
32
+ def query(key)
33
+ row = @db.get_first_row("SELECT reference,receipt FROM results WHERE processor_key=?", [key])
34
+ row && {"reference" => row[0], "receipt" => row[1]}
35
+ end
36
+ def count = @db.get_first_value("SELECT count(*) FROM results")
37
+ def close = @db.close
38
+ end
39
+ end
@@ -0,0 +1,161 @@
1
+ # frozen_string_literal: true
2
+ # Ruby-only synthetic application: no network, Rails, A2A or real payment.
3
+ require "ap2_mandate_verifier"
4
+ require "json"
5
+ require_relative "integration/inputs"
6
+ require_relative "integration/processor"
7
+ module RubyIntegration
8
+ M = AP2MandateVerifier
9
+ A = M::PaymentAcceptance
10
+ NOW = 1_800_000_000 # Explicit virtual clock. NEVER use this clock in a real app.
11
+ SCENARIOS = %w[success error pause-before-start expired-before-start crash-before-start crash-after-start crash-after-charge unknown delayed].freeze
12
+ class App
13
+ def self.init(directory)
14
+ # Partial bootstrap is a STOP. Never delete/recreate an existing directory.
15
+ Dir.mkdir(directory, 0o700)
16
+ data = Inputs.bootstrap(now: NOW)
17
+ File.open(File.join(directory, "app.json"), File::WRONLY | File::CREAT | File::EXCL, 0o600) do |file|
18
+ file.write(JSON.generate(data)); file.flush; file.fsync
19
+ end
20
+ store = A::SQLiteStore.create(path: File.join(directory, "acceptance.sqlite3"))
21
+ store.authorize!(**data.fetch("scope").transform_keys(&:to_sym), mode: :conditional,
22
+ profile_ref: M::Payment::PROFILE_REF, checkout: data.fetch("checkout"),
23
+ payment: data.fetch("expected_payment"), receipt_context: data.fetch("receipt_context"))
24
+ store.close
25
+ Processor.create(File.join(directory, "processor.sqlite3"))
26
+ File.open(directory, File::RDONLY) { |file| file.fsync }
27
+ {event: "initialized", sdk_state: "not_reserved", mock_dispatches: 0}
28
+ ensure
29
+ store&.close rescue nil
30
+ end
31
+ def initialize(directory)
32
+ @directory = directory
33
+ @data = current_data
34
+ @scope = @data.fetch("scope").transform_keys(&:to_sym)
35
+ @store = A::SQLiteStore.new(path: File.join(directory, "acceptance.sqlite3"))
36
+ @service = A::Service.new(store: @store)
37
+ @processor = Processor.new(File.join(directory, "processor.sqlite3"))
38
+ end
39
+ def close
40
+ @processor&.close
41
+ @store&.close
42
+ end
43
+ def current_data
44
+ path = File.join(@directory, "app.json")
45
+ raise "application state unavailable" unless File.file?(path) && File.size(path) <= 131_072
46
+ JSON.parse(File.binread(path))
47
+ end
48
+ def run(scenario)
49
+ raise "unknown scenario" unless SCENARIOS.include?(scenario)
50
+ inputs = Inputs.verification(current_data, now: NOW)
51
+ # Educational check only: accept/start reverify raw CURRENT inputs themselves.
52
+ raise "verification failed" unless M::Payment.verify(**inputs).verified?
53
+ accepted = @service.accept(**@scope, mode: :conditional, verification_inputs: inputs)
54
+ unless accepted.ticket
55
+ require_existing!(accepted)
56
+ return summary("no_new_ticket", accepted)
57
+ end
58
+ crash("reserved_before_start", accepted) if scenario == "crash-before-start"
59
+ # Same-process interruption retains the opaque Ticket. Restart does not.
60
+ if scenario == "pause-before-start"
61
+ @store.close
62
+ @store = A::SQLiteStore.new(path: File.join(@directory, "acceptance.sqlite3"))
63
+ @service = A::Service.new(store: @store)
64
+ end
65
+ fresh = Inputs.verification(current_data, now: scenario == "expired-before-start" ? NOW + 61 : NOW)
66
+ execution = @service.start(ticket: accepted.ticket, verification_inputs: fresh)
67
+ return summary("start_blocked", @service.lookup(**@scope)) unless execution
68
+ crash("started_before_dispatch", @service.lookup(**@scope)) if scenario == "crash-after-start"
69
+ issued_at = scenario == "delayed" ? NOW + 120 : NOW
70
+ receipt = unless scenario == "unknown"
71
+ Inputs.receipt(@data, wire: execution.closed_sd_jwt, outcome: scenario == "error" ? "Error" : "Success", now: issued_at)
72
+ end
73
+ @processor.dispatch(execution, receipt: receipt)
74
+ crash("processor_committed_before_finish", @service.lookup(**@scope)) if scenario == "crash-after-charge"
75
+ if %w[unknown delayed].include?(scenario)
76
+ return summary("awaiting_reconciliation", @service.unknown(ticket: accepted.ticket))
77
+ end
78
+ final = @service.finish(ticket: accepted.ticket, receipt_jwt: receipt, verified_at: NOW)
79
+ raise "Receipt finalization failed" unless %i[receipt_success receipt_error].include?(final.status)
80
+ summary("finished", final)
81
+ end
82
+ def retry(resigned: false)
83
+ fresh = Inputs.verification(current_data, now: NOW)
84
+ fresh[:closed_sd_jwt] = Inputs.resign(@data, now: NOW) if resigned
85
+ previous = @service.lookup(**@scope)
86
+ raise "no existing attempt" unless previous.processor_key
87
+ result = @service.accept(**@scope, mode: :conditional, verification_inputs: fresh)
88
+ raise "retry changed identity" if result.ticket || result.processor_key != previous.processor_key ||
89
+ result.closed_sd_jwt != previous.closed_sd_jwt || result.reference != previous.reference
90
+ summary(resigned ? "resigned_retry" : "exact_retry", result).merge(original_identity_preserved: true)
91
+ end
92
+ def recover
93
+ original = @service.lookup(**@scope)
94
+ require_existing!(original)
95
+ saved = @processor.query(original.processor_key) # QUERY only, never dispatch.
96
+ raise "processor reference mismatch" if saved && saved.fetch("reference") != original.reference
97
+ receipt = saved && saved.fetch("receipt")
98
+ unless receipt
99
+ return summary("operator_reconciliation_required", original).merge(result: "unknown")
100
+ end
101
+ # Receipt window is independent of the expired Mandate/challenge.
102
+ final = @service.reconcile(**@scope, receipt_jwt: receipt, verified_at: NOW + 120)
103
+ raise "recovery changed identity" if final.ticket || (final.processor_key && final.processor_key != original.processor_key)
104
+ raise "Receipt reconciliation failed" unless %i[receipt_success receipt_error].include?(final.status)
105
+ summary("reconciled", final)
106
+ end
107
+ def require_existing!(result)
108
+ raise "attempt unavailable or invalid" unless result.processor_key &&
109
+ %i[pending unknown receipt_success receipt_error].include?(result.status)
110
+ end
111
+ def inspect_state
112
+ result = @service.lookup(**@scope)
113
+ require_existing!(result)
114
+ summary("inspection", result)
115
+ end
116
+ def summary(event, result)
117
+ raise "invalid result state" unless result.processor_key &&
118
+ %i[reserved pending unknown receipt_success receipt_error].include?(result.status)
119
+ value = {event: event, sdk_state: result.status, ticket: !result.ticket.nil?, mock_dispatches: @processor.count}
120
+ if %i[receipt_success receipt_error].include?(result.status)
121
+ value[:receipt_authentic] = true
122
+ value[:reported_outcome] = result.status == :receipt_success ? "Success" : "Error"
123
+ end
124
+ value
125
+ end
126
+ def crash(event, result)
127
+ puts JSON.generate(summary(event, result).merge(simulated_process_exit: 75))
128
+ $stdout.flush
129
+ Process.exit!(75) # No ensure/normal cleanup; next CLI is a different Ruby process.
130
+ end
131
+ end
132
+ def self.cli(args)
133
+ command, directory, option = args
134
+ raise "invalid command" unless args.size.between?(2, 3) && directory &&
135
+ %w[init run recover retry inspect].include?(command) &&
136
+ (command == "run" || command == "retry" || option.nil?) &&
137
+ (command != "retry" || option.nil? || option == "resigned")
138
+ result = if command == "init"
139
+ App.init(directory)
140
+ else
141
+ app = App.new(directory)
142
+ case command
143
+ when "run" then app.run(option || "success")
144
+ when "retry" then app.retry(resigned: option == "resigned")
145
+ when "recover" then app.recover
146
+ when "inspect" then app.inspect_state
147
+ end
148
+ end
149
+ feature = $LOADED_FEATURES.find { |path| path.end_with?("/ap2_mandate_verifier.rb") }
150
+ spec = Gem.loaded_specs["ap2_mandate_verifier"]
151
+ puts JSON.generate(result.merge(sdk_version: spec&.version&.to_s || "source",
152
+ sdk_load: spec && feature.start_with?(spec.full_gem_path + "/") ? "installed_gem" : "repository_lib",
153
+ ruby: RUBY_VERSION, fixture_clock: NOW))
154
+ rescue StandardError, LoadError
155
+ warn "STOP: invalid command or unavailable/inconsistent state; preserve all files and investigate."
156
+ return 1
157
+ ensure
158
+ app&.close
159
+ end
160
+ end
161
+ exit(RubyIntegration.cli(ARGV) || 0) if $PROGRAM_NAME == __FILE__
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ap2_mandate_verifier
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.6.0
4
+ version: 1.6.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - AP2 Mandate Verifier contributors
8
8
  bindir: bin
9
9
  cert_chain: []
10
- date: 2026-11-05 00:00:00.000000000 Z
10
+ date: 2026-11-10 00:00:00.000000000 Z
11
11
  dependencies:
12
12
  - !ruby/object:Gem::Dependency
13
13
  name: json
@@ -117,17 +117,23 @@ files:
117
117
  - docs/RELEASE_140_CANDIDATE.md
118
118
  - docs/RELEASE_150_CANDIDATE.md
119
119
  - docs/RELEASE_160_CANDIDATE.md
120
+ - docs/RELEASE_161_CANDIDATE.md
121
+ - docs/RUBY_INTEGRATION.ja.md
122
+ - docs/RUBY_INTEGRATION.md
120
123
  - docs/SELECTIVE_CHECKOUT.md
121
124
  - docs/SELECTIVE_PAYMENT.md
122
125
  - docs/SUPPORT_CONTRACT.md
123
126
  - docs/evidence/1.1.0_publication.json
124
127
  - examples/conditional_checkout.rb
128
+ - examples/integration/inputs.rb
129
+ - examples/integration/processor.rb
125
130
  - examples/long_payment.rb
126
131
  - examples/long_payment_acceptance.rb
127
132
  - examples/payment.rb
128
133
  - examples/payment_acceptance.rb
129
134
  - examples/payment_policy.rb
130
135
  - examples/recurring_payment.rb
136
+ - examples/ruby_integration.rb
131
137
  - examples/selective_checkout.rb
132
138
  - examples/selective_checkout_payment.rb
133
139
  - examples/selective_payment.rb
@@ -180,7 +186,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
180
186
  - !ruby/object:Gem::Version
181
187
  version: '0'
182
188
  requirements: []
183
- rubygems_version: 3.6.9
189
+ rubygems_version: 4.0.20
184
190
  specification_version: 4
185
191
  summary: Ruby SDK for supported AP2 Checkout and Payment Mandates
186
192
  test_files: []