ap2_mandate_verifier 1.4.0 → 1.6.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.
Files changed (37) hide show
  1. checksums.yaml +4 -4
  2. data/README.ja.md +31 -8
  3. data/README.md +31 -8
  4. data/SECURITY.md +8 -2
  5. data/docs/LONG_PAYMENT.md +82 -0
  6. data/docs/PAYMENT_ACCEPTANCE.md +90 -0
  7. data/docs/PAYMENT_POLICY.md +4 -2
  8. data/docs/PUBLIC_API.md +16 -2
  9. data/docs/RECURRING_PAYMENT.md +54 -0
  10. data/docs/RELEASE_140_CANDIDATE.md +2 -0
  11. data/docs/RELEASE_150_CANDIDATE.md +23 -0
  12. data/docs/RELEASE_160_CANDIDATE.md +30 -0
  13. data/docs/SUPPORT_CONTRACT.md +9 -1
  14. data/examples/conditional_checkout.rb +3 -3
  15. data/examples/long_payment.rb +116 -0
  16. data/examples/long_payment_acceptance.rb +89 -0
  17. data/examples/payment_acceptance.rb +77 -0
  18. data/examples/recurring_payment.rb +99 -0
  19. data/lib/ap2_mandate_verifier/checkout_jwt.rb +1 -1
  20. data/lib/ap2_mandate_verifier/checkout_receipt.rb +10 -1
  21. data/lib/ap2_mandate_verifier/conditional_checkout.rb +43 -19
  22. data/lib/ap2_mandate_verifier/input_builder.rb +1 -1
  23. data/lib/ap2_mandate_verifier/long_payment.rb +201 -0
  24. data/lib/ap2_mandate_verifier/payment.rb +52 -12
  25. data/lib/ap2_mandate_verifier/payment_acceptance/long_sqlite_store.rb +272 -0
  26. data/lib/ap2_mandate_verifier/payment_acceptance/recurring_sqlite_store.rb +124 -0
  27. data/lib/ap2_mandate_verifier/payment_acceptance/sqlite_store.rb +344 -0
  28. data/lib/ap2_mandate_verifier/payment_acceptance.rb +285 -0
  29. data/lib/ap2_mandate_verifier/payment_policy.rb +7 -2
  30. data/lib/ap2_mandate_verifier/purchase_binding.rb +11 -5
  31. data/lib/ap2_mandate_verifier/recurring_payment.rb +111 -0
  32. data/lib/ap2_mandate_verifier/root_disclosures.rb +9 -6
  33. data/lib/ap2_mandate_verifier/scoped_constraints.rb +1 -1
  34. data/lib/ap2_mandate_verifier/selective_payment.rb +1 -1
  35. data/lib/ap2_mandate_verifier/trust_context.rb +6 -0
  36. data/lib/ap2_mandate_verifier.rb +6 -0
  37. metadata +22 -5
@@ -0,0 +1,201 @@
1
+ # frozen_string_literal: true
2
+
3
+ module AP2MandateVerifier
4
+ # Explicit long-content, fresh-proof profile. MONTHLY is enforced by a
5
+ # protected Host period policy at acceptance, never by this wire verifier.
6
+ module LongPayment
7
+ CHECKOUT_PROFILE_REF = "ap2.checkout.long_open.multi_item.1".freeze
8
+ PROFILE_REF = "ap2.payment.long_open.jpy_monthly.complete.1".freeze
9
+ MAX_CONTENT_LIFETIME = 400 * 86_400
10
+ MAX_OCCURRENCES = 1_000_000
11
+ TYPES = %w[payment.agent_recurrence payment.budget].freeze
12
+ class Invalid < ArgumentError
13
+ def initialize = super("invalid long payment information")
14
+ end
15
+ def self.profile?(value) = value == PROFILE_REF
16
+ def self.checkout_profile?(value) = value == CHECKOUT_PROFILE_REF
17
+
18
+ def self.constraints!(opening)
19
+ raise Invalid unless opening.instance_of?(Hash)
20
+ cs = opening.fetch("constraints")
21
+ raise Invalid unless cs.instance_of?(Array) && cs.size == 6 && cs.all? { |c| c.instance_of?(Hash) }
22
+ recurrence = cs.select { |c| c["type"] == TYPES[0] }
23
+ budget = cs.select { |c| c["type"] == TYPES[1] }
24
+ raise Invalid unless recurrence.size == 1 && budget.size == 1
25
+ recurrence, budget = recurrence.first, budget.first
26
+ Payment.send(:exact!, recurrence, %w[type frequency max_occurrences])
27
+ Payment.send(:exact!, budget, %w[type max currency])
28
+ raise Invalid unless recurrence["frequency"] == "MONTHLY" && recurrence["max_occurrences"].instance_of?(Integer) &&
29
+ recurrence["max_occurrences"].between?(1, MAX_OCCURRENCES) && budget["currency"] == "JPY" &&
30
+ budget["max"].instance_of?(Integer) && budget["max"].between?(1, ScopedConstraints::MAX_AMOUNT)
31
+ amount = cs.select { |c| c["type"] == "payment.amount_range" }
32
+ raise Invalid unless amount.size == 1 && amount.first["currency"] == "JPY"
33
+ raise Invalid if amount.first.fetch("min", 0).instance_of?(Integer) && amount.first.fetch("min", 0) > budget["max"]
34
+ if opening.key?("payment_amount")
35
+ Payment.send(:amount!, opening["payment_amount"])
36
+ raise Invalid unless opening["payment_amount"]["currency"] == "JPY" && opening["payment_amount"]["amount"] <= budget["max"]
37
+ end
38
+ opening.merge("constraints" => cs.reject { |c| TYPES.include?(c["type"]) })
39
+ end
40
+ def self.conditions!(opening, closed)
41
+ base = constraints!(opening)
42
+ budget = opening.fetch("constraints").find { |c| c["type"] == TYPES[1] }
43
+ raise Invalid unless closed.fetch("payment_amount").fetch("currency") == "JPY" && closed["payment_amount"]["amount"] <= budget.fetch("max")
44
+ Payment.send(:conditions!, base, closed)
45
+ end
46
+
47
+ def self.snapshot_checkout!(source, verified_at)
48
+ CheckoutReceipt.send(:epoch!, verified_at)
49
+ fields = %i[trust_json selection_json host_json root_presentation terminal_jws host_checkout_jws verified_at]
50
+ raise Invalid unless Object.instance_method(:instance_of?).bind_call(source, Hash) &&
51
+ Object.instance_method(:singleton_methods).bind_call(source, true).empty? && source.size == fields.size && fields.all? { |f| source.key?(f) }
52
+ limits = {selection_json: 16_384, host_json: 32_768, terminal_jws: 32_768, host_checkout_jws: 16_384}
53
+ value = fields.to_h { |f| [f, f == :verified_at ? source[f] : InputValidator.snapshot!(source[f], max_bytes: limits.fetch(f, 131_072))] }.freeze
54
+ CheckoutReceipt.send(:epoch!, value[:verified_at])
55
+ raise Invalid unless value[:verified_at] == verified_at
56
+ value
57
+ end
58
+ def self.current_context!(trust_json, selection_json, merchant_id, verified_at)
59
+ CheckoutReceipt.send(:epoch!, verified_at)
60
+ trust = InputValidator.snapshot!(trust_json)
61
+ selection = InputValidator.snapshot!(selection_json, max_bytes: 16_384)
62
+ identity = InputValidator.snapshot!(merchant_id, max_bytes: 256)
63
+ context = TrustContext.verified_context!(trust_json: trust, selection_json: selection,
64
+ expected_merchant_id: identity, verified_at: verified_at)
65
+ raise Invalid unless context.fetch(:references).fetch("profile_ref") == CHECKOUT_PROFILE_REF
66
+ context
67
+ end
68
+ def self.authority_projection(context)
69
+ binding = context.fetch(:references).merge("selected_keys" => context.fetch(:selected_keys))
70
+ keys = context.fetch(:keys)
71
+ principals = context.fetch(:selected_keys).to_h { |record| [record.fetch("role"), record.slice("identity", "purpose", "tenant_id")] }
72
+ fingerprints = {"issuer" => Digest::SHA256.hexdigest(keys.fetch(:trusted_issuer_public_key_pem)),
73
+ "agent" => Digest::SHA256.hexdigest(keys.fetch(:expected_agent_public_key_pem))}
74
+ {authority_binding: ResearchEvidence.send(:copy_frozen, binding), root_key_fingerprints: ResearchEvidence.send(:copy_frozen, fingerprints),
75
+ authority_principals: ResearchEvidence.send(:copy_frozen, principals),
76
+ tenant_id: context.fetch(:references).fetch("tenant_id"), valid_until: context.fetch(:valid_until)}.freeze
77
+ end
78
+ def self.current_authority!(trust_json:, selection_json:, merchant_id:, verified_at:)
79
+ authority_projection(current_context!(trust_json, selection_json, merchant_id, verified_at))
80
+ rescue StandardError, LoadError
81
+ raise Invalid.new, cause: nil
82
+ end
83
+ def self.authority!(checkout_inputs:, provider_key:, agent_key:, merchant_id:, verified_at:)
84
+ provider = InputValidator.snapshot!(provider_key, max_bytes: 1024)
85
+ agent = InputValidator.snapshot!(agent_key, max_bytes: 1024)
86
+ identity = InputValidator.snapshot!(merchant_id, max_bytes: 256)
87
+ inputs = snapshot_checkout!(checkout_inputs, verified_at)
88
+ host = InputValidator.parse!(inputs.fetch(:host_json))
89
+ raise Invalid unless host.fetch("binding").fetch("merchant_id") == identity
90
+ context = current_context!(inputs.fetch(:trust_json), inputs.fetch(:selection_json), identity, verified_at)
91
+ keys = context.fetch(:keys)
92
+ raise Invalid unless RootSignature.public_jwk!(provider) == RootSignature.public_jwk!(keys.fetch(:trusted_issuer_public_key_pem)) &&
93
+ RootSignature.public_jwk!(agent) == RootSignature.public_jwk!(keys.fetch(:expected_agent_public_key_pem))
94
+ authority_projection(context)
95
+ rescue StandardError, LoadError
96
+ raise Invalid.new, cause: nil
97
+ end
98
+
99
+ def self.validated_open!(source)
100
+ opening, wire = Payment.send(:root!, source, profile_ref: PROFILE_REF)
101
+ Payment.send(:open!, opening, profile_ref: PROFILE_REF)
102
+ recurrence = opening.fetch("constraints").find { |c| c["type"] == TYPES[0] }
103
+ budget = opening.fetch("constraints").find { |c| c["type"] == TYPES[1] }
104
+ root = wire.split("~", 2).first.freeze
105
+ {opening: ResearchEvidence.send(:copy_frozen, opening), root_wire: root, presentation_wire: wire,
106
+ reference: Payment.send(:digest, root).freeze, currency: "JPY".freeze, frequency: "MONTHLY".freeze,
107
+ max_occurrences: recurrence.fetch("max_occurrences"), budget_max: budget.fetch("max"), profile_ref: PROFILE_REF}.freeze
108
+ end
109
+ def self.approved_open!(open_sd_jwt:, trusted_provider_public_key_pem:, expected_agent_public_key_pem:, verified_at:, profile_ref: PROFILE_REF)
110
+ raise Invalid unless InputValidator.snapshot!(profile_ref, max_bytes: 256) == PROFILE_REF
111
+ wire = InputValidator.snapshot!(open_sd_jwt, max_bytes: 32_769)
112
+ provider = InputValidator.snapshot!(trusted_provider_public_key_pem, max_bytes: 1024)
113
+ agent = InputValidator.snapshot!(expected_agent_public_key_pem, max_bytes: 1024)
114
+ CheckoutReceipt.send(:epoch!, verified_at)
115
+ RootSignature.verified_payload!(wire.split("~", 2).first, provider)
116
+ checked = validated_open!(wire)
117
+ Payment.send(:valid_now!, checked.fetch(:opening), verified_at)
118
+ raise Invalid unless checked.fetch(:opening).fetch("cnf") == {"jwk" => RootSignature.public_jwk!(agent)}
119
+ checked
120
+ rescue StandardError, LoadError
121
+ raise Invalid.new, cause: nil
122
+ end
123
+ def self.approved_pair!(payment_open:, checkout_open:, trust_json:, selection_json:, merchant_id:, verified_at:)
124
+ payment_wire = InputValidator.snapshot!(payment_open, max_bytes: 32_769)
125
+ checkout_wire = InputValidator.snapshot!(checkout_open)
126
+ identity = InputValidator.snapshot!(merchant_id, max_bytes: 256)
127
+ context = current_context!(trust_json, selection_json, identity, verified_at)
128
+ keys = context.fetch(:keys)
129
+ checkout = ConditionalCheckout.send(:open!, checkout_wire, keys.fetch(:trusted_issuer_public_key_pem),
130
+ keys.fetch(:expected_agent_public_key_pem), verified_at, profile_ref: CHECKOUT_PROFILE_REF)
131
+ merchant, = ScopedConstraints.send(:multi_open!, checkout.fetch("constraints"))
132
+ raise Invalid unless merchant.fetch("id") == identity
133
+ checked = approved_open!(open_sd_jwt: payment_wire, trusted_provider_public_key_pem: keys.fetch(:trusted_issuer_public_key_pem),
134
+ expected_agent_public_key_pem: keys.fetch(:expected_agent_public_key_pem), verified_at: verified_at)
135
+ opening = checked.fetch(:opening)
136
+ reference = opening.fetch("constraints").find { |c| c["type"] == "payment.reference" }
137
+ raise Invalid unless reference.fetch("conditional_transaction_id") == Payment.send(:digest, checkout_wire)
138
+ economic_payment = opening.reject { |k,_| %w[iat exp cnf].include?(k) }.merge(
139
+ "constraints" => opening.fetch("constraints").reject { |c| c["type"] == "payment.reference" })
140
+ economic_checkout = checkout.reject { |k,_| %w[iat exp cnf].include?(k) }
141
+ economics = canonical({"payment_profile" => PROFILE_REF, "checkout_profile" => CHECKOUT_PROFILE_REF,
142
+ "checkout" => economic_checkout, "payment" => economic_payment})
143
+ projected = authority_projection(context)
144
+ checked.merge(checkout_opening: ResearchEvidence.send(:copy_frozen, checkout), checkout_reference: Payment.send(:digest, checkout_wire).freeze,
145
+ economic_contract: economics.freeze, not_before: [opening.fetch("iat"),checkout.fetch("iat")].max,
146
+ not_after: [opening.fetch("exp"),checkout.fetch("exp")].min, authority_binding: projected.fetch(:authority_binding),
147
+ root_key_fingerprints: projected.fetch(:root_key_fingerprints), tenant_id: projected.fetch(:tenant_id),
148
+ authority_principals: projected.fetch(:authority_principals),
149
+ authority_valid_until: projected.fetch(:valid_until)).freeze
150
+ rescue StandardError, LoadError
151
+ raise Invalid.new, cause: nil
152
+ end
153
+ def self.canonical(value)
154
+ normalized = case value
155
+ when Hash then value.keys.sort.to_h { |k| [k, JSON.parse(canonical(value[k]))] }
156
+ when Array then value.map { |v| JSON.parse(canonical(v)) }
157
+ else value
158
+ end
159
+ JSON.generate(normalized)
160
+ end
161
+
162
+ %i[issue_open issue_closed present verify details].each do |name|
163
+ define_singleton_method(name) do |**arguments|
164
+ if arguments.key?(:profile_ref)
165
+ raise Invalid unless InputValidator.snapshot!(arguments.delete(:profile_ref), max_bytes: 256) == PROFILE_REF
166
+ end
167
+ if arguments.key?(:checkout_profile_ref)
168
+ raise Invalid unless name == :issue_open && InputValidator.snapshot!(arguments.delete(:checkout_profile_ref), max_bytes: 256) == CHECKOUT_PROFILE_REF
169
+ end
170
+ arguments[:checkout_profile_ref] = CHECKOUT_PROFILE_REF if name == :issue_open
171
+ arguments[:checkout_mode] = :conditional if name == :verify && !arguments.key?(:checkout_mode)
172
+ Payment.public_send(name, **arguments, profile_ref: PROFILE_REF)
173
+ rescue StandardError, LoadError
174
+ next Payment::Verification.send(:new) if name == :verify
175
+ raise Invalid.new, cause: nil
176
+ end
177
+ end
178
+ {issue_checkout_open: :issue_open, issue_checkout_closed: :issue_closed, present_checkout: :present}.each do |name,target|
179
+ define_singleton_method(name) do |**arguments|
180
+ if arguments.key?(:profile_ref)
181
+ raise Invalid unless InputValidator.snapshot!(arguments.delete(:profile_ref), max_bytes: 256) == CHECKOUT_PROFILE_REF
182
+ end
183
+ ConditionalCheckout.public_send(target, **arguments, profile_ref: CHECKOUT_PROFILE_REF)
184
+ rescue StandardError, LoadError
185
+ raise Invalid.new, cause: nil
186
+ end
187
+ end
188
+ %i[current_authority! authority! approved_open! approved_pair!].each do |name|
189
+ implementation = method(name)
190
+ required = implementation.parameters.filter_map { |kind,key| key if kind == :keyreq }.freeze
191
+ fields = implementation.parameters.map(&:last).freeze
192
+ define_singleton_method(name) do |**arguments|
193
+ raise Invalid unless (required - arguments.keys).empty? && (arguments.keys - fields).empty?
194
+ implementation.call(**arguments)
195
+ rescue StandardError, LoadError
196
+ raise Invalid.new, cause: nil
197
+ end
198
+ end
199
+ private_class_method :constraints!, :conditions!, :snapshot_checkout!, :current_context!, :authority_projection, :validated_open!, :canonical
200
+ end
201
+ end
@@ -62,6 +62,7 @@ module AP2MandateVerifier
62
62
  # Protected approval record must cover every payment detail, not just amount.
63
63
  def self.issue_direct(payment:, approved_payment:, issued_at:, expires_at:, signer:, public_key_pem:, profile_ref: PROFILE_REF)
64
64
  profile_ref = PaymentPolicy.profile!(profile_ref)
65
+ raise Invalid if PaymentPolicy.recurring?(profile_ref) || PaymentPolicy.long?(profile_ref)
65
66
  value = copy_details!(payment, profile_ref: profile_ref)
66
67
  raise Invalid unless value == copy_details!(approved_payment, profile_ref: profile_ref)
67
68
  closed = value.merge("vct" => "mandate.payment.1", "iat" => issued_at, "exp" => expires_at)
@@ -76,6 +77,7 @@ module AP2MandateVerifier
76
77
  profile_ref: PROFILE_REF, candidate_policy: nil)
77
78
  profile_ref = PaymentPolicy.profile!(profile_ref)
78
79
  PaymentPolicy.candidate_policy!(profile_ref, candidate_policy)
80
+ raise Invalid if PaymentPolicy.long?(profile_ref) && checkout_profile_ref != LongPayment::CHECKOUT_PROFILE_REF
79
81
  RootSignature.send(:dependencies!)
80
82
  root = checkout_open!(open_checkout_sd_jwt, profile_ref: checkout_profile_ref)
81
83
  conditions = InputBuilder.send(:copy!, constraints)
@@ -97,14 +99,15 @@ module AP2MandateVerifier
97
99
  opening, root = verified_root!(open_sd_jwt, trusted_provider_public_key_pem, profile_ref: profile_ref)
98
100
  open!(opening, profile_ref: profile_ref); valid_now!(opening, verified_at)
99
101
  raise Invalid unless opening.fetch("cnf") == {"jwk" => RootSignature.public_jwk!(agent_public_key_pem)}
100
- value = copy_details!(payment, profile_ref: profile_ref).merge("vct" => "mandate.payment.1", "iat" => verified_at, "exp" => opening.fetch("exp"))
102
+ value = copy_details!(payment, profile_ref: profile_ref).merge("vct" => "mandate.payment.1",
103
+ "iat" => PaymentPolicy.long?(profile_ref) ? opening.fetch("iat") : verified_at, "exp" => opening.fetch("exp"))
101
104
  closed!(value, profile_ref: profile_ref); conditions!(opening, value, profile_ref: profile_ref)
102
105
  audience = InputValidator.snapshot!(audience, max_bytes: 512)
103
106
  nonce = InputValidator.snapshot!(nonce, max_bytes: 512)
104
107
  text!(audience, 512); text!(nonce, 512)
105
108
  claims = {"delegate_payload" => [value], "iat" => verified_at,
106
109
  "aud" => audience, "nonce" => nonce, "sd_hash" => digest(root)}
107
- claims["ap2_profile"] = profile_ref if PaymentPolicy.enabled?(profile_ref)
110
+ claims["ap2_profile"] = profile_ref if PaymentPolicy.marked?(profile_ref)
108
111
  preflight!(claims, "kb+sd-jwt", profile_ref: profile_ref)
109
112
  terminal = MerchantSignature.sign!(claims, signer, agent_public_key_pem, typ: "kb+sd-jwt")
110
113
  Presentation.send(:new, root, terminal + "~")
@@ -119,6 +122,9 @@ module AP2MandateVerifier
119
122
  opening, root = root!(open_sd_jwt, profile_ref: profile_ref)
120
123
  open!(opening, profile_ref: profile_ref)
121
124
  raise Invalid unless closed.fetch("sd_hash") == digest(root)
125
+ if PaymentPolicy.long?(profile_ref)
126
+ raise Invalid unless %w[iat exp].all? { |f| closed.fetch("delegate_payload").first.fetch(f) == opening.fetch(f) }
127
+ end
122
128
  conditions!(opening, closed.fetch("delegate_payload").first, profile_ref: profile_ref)
123
129
  end
124
130
  Presentation.send(:new, open_sd_jwt && root, wire)
@@ -133,6 +139,7 @@ module AP2MandateVerifier
133
139
  profile_ref: PROFILE_REF, trusted_pisp: nil, execution_at: nil)
134
140
  profile_ref = PaymentPolicy.profile!(profile_ref)
135
141
  context = PaymentPolicy.context!(profile_ref, trusted_pisp, execution_at)
142
+ raise Invalid if checkout_mode == :direct && (PaymentPolicy.recurring?(profile_ref) || PaymentPolicy.long?(profile_ref))
136
143
  trusted_provider_public_key_pem = InputValidator.snapshot!(trusted_provider_public_key_pem, max_bytes: 1_024)
137
144
  if checkout_mode == :conditional
138
145
  expected_agent_public_key_pem = InputValidator.snapshot!(expected_agent_public_key_pem, max_bytes: 1_024)
@@ -154,6 +161,11 @@ module AP2MandateVerifier
154
161
  [key, key == :verified_at ? value : InputValidator.snapshot!(value, max_bytes: limits.fetch(key, 131_072))]
155
162
  end.freeze
156
163
  raise Invalid unless checkout_inputs[:verified_at] == verified_at
164
+ if PaymentPolicy.long?(profile_ref)
165
+ host = InputValidator.parse!(checkout_inputs.fetch(:host_json))
166
+ LongPayment.authority!(checkout_inputs: checkout_inputs, provider_key: trusted_provider_public_key_pem,
167
+ agent_key: expected_agent_public_key_pem, merchant_id: host.fetch("binding").fetch("merchant_id"), verified_at: verified_at)
168
+ end
157
169
  if checkout_mode == :conditional
158
170
  expected_agent_public_key_pem = InputValidator.snapshot!(expected_agent_public_key_pem, max_bytes: 1_024)
159
171
  end
@@ -192,8 +204,12 @@ module AP2MandateVerifier
192
204
  verified_at - terminal.fetch("iat") <= 300
193
205
  claims = terminal.fetch("delegate_payload").first
194
206
  closed!(claims, profile_ref: profile_ref); valid_now!(claims, verified_at)
195
- raise Invalid unless terminal.fetch("iat") == claims.fetch("iat") && claims.fetch("iat") >= opening.fetch("iat") &&
196
- claims.fetch("exp") <= opening.fetch("exp")
207
+ if PaymentPolicy.long?(profile_ref)
208
+ raise Invalid unless %w[iat exp].all? { |f| claims.fetch(f) == opening.fetch(f) }
209
+ else
210
+ raise Invalid unless terminal.fetch("iat") == claims.fetch("iat") && claims.fetch("iat") >= opening.fetch("iat") &&
211
+ claims.fetch("exp") <= opening.fetch("exp")
212
+ end
197
213
  conditions!(opening, claims, profile_ref: profile_ref)
198
214
  reference = opening.fetch("constraints").find { |c| c["type"] == "payment.reference" }
199
215
  raise Invalid unless reference.fetch("conditional_transaction_id") == digest(checkout_inputs.fetch(:root_presentation))
@@ -209,7 +225,7 @@ module AP2MandateVerifier
209
225
 
210
226
  def self.reference(closed_sd_jwt:, profile_ref: PROFILE_REF)
211
227
  profile_ref = InputValidator.snapshot!(profile_ref, max_bytes: 256)
212
- raise Invalid unless profile_ref == PROFILE_REF || PaymentPolicy.enabled?(profile_ref)
228
+ raise Invalid unless profile_ref == PROFILE_REF || PaymentPolicy.marked?(profile_ref)
213
229
  # No trust or authorization claim: parse either supported final grammar.
214
230
  wire = InputValidator.snapshot!(closed_sd_jwt, max_bytes: 32_769)
215
231
  payload = jwt_payload!(wire.split("~", -1).first, nil)
@@ -229,15 +245,24 @@ module AP2MandateVerifier
229
245
  exact!(value, PaymentPolicy.fields(profile_ref))
230
246
  raise Invalid unless value["transaction_id"].instance_of?(String) && value["transaction_id"].match?(/\A[A-Za-z0-9_-]{43}\z/)
231
247
  payee!(value["payee"]); amount!(value["payment_amount"]); instrument!(value["payment_instrument"])
248
+ raise Invalid if (PaymentPolicy.recurring?(profile_ref) || PaymentPolicy.long?(profile_ref)) && value["payment_amount"]["currency"] != "JPY"
232
249
  PaymentPolicy.details!(value) if PaymentPolicy.enabled?(profile_ref)
233
250
  value
234
251
  end
235
252
  def self.closed!(value, profile_ref: PROFILE_REF)
236
253
  exact!(value, PaymentPolicy.fields(profile_ref) + %w[vct iat exp])
237
254
  raise Invalid unless value["vct"] == "mandate.payment.1"
238
- copy_details!(value.slice(*PaymentPolicy.fields(profile_ref)), profile_ref: profile_ref); times!(value)
255
+ copy_details!(value.slice(*PaymentPolicy.fields(profile_ref)), profile_ref: profile_ref); times!(value, profile_ref: profile_ref)
239
256
  end
240
- def self.open!(value, profile_ref: PROFILE_REF)
257
+ def self.open!(value, profile_ref: PROFILE_REF, base_constraints: false)
258
+ if PaymentPolicy.long?(profile_ref) && !base_constraints
259
+ open!(LongPayment.send(:constraints!, value), profile_ref: profile_ref, base_constraints: true)
260
+ return nil
261
+ end
262
+ if PaymentPolicy.recurring?(profile_ref)
263
+ open!(RecurringPayment.send(:constraints!, value))
264
+ return nil
265
+ end
241
266
  if PaymentPolicy.enabled?(profile_ref)
242
267
  base = PaymentPolicy.constraints!(value)
243
268
  open!(base)
@@ -245,7 +270,7 @@ module AP2MandateVerifier
245
270
  end
246
271
  raise Invalid unless value.instance_of?(Hash) && (value.keys - FIELDS.drop(1) - %w[vct iat exp cnf constraints]).empty?
247
272
  raise Invalid unless value["vct"] == "mandate.payment.open.1"
248
- times!(value); exact!(value["cnf"], ["jwk"])
273
+ times!(value, profile_ref: profile_ref); exact!(value["cnf"], ["jwk"])
249
274
  jwk = value["cnf"]["jwk"]
250
275
  exact!(jwk, %w[kty crv x y])
251
276
  raise Invalid unless jwk["kty"] == "EC" && jwk["crv"] == "P-256" &&
@@ -274,6 +299,14 @@ module AP2MandateVerifier
274
299
  end
275
300
  end
276
301
  def self.conditions!(opening, closed, profile_ref: PROFILE_REF)
302
+ if PaymentPolicy.long?(profile_ref)
303
+ LongPayment.send(:conditions!, opening, closed)
304
+ return nil
305
+ end
306
+ if PaymentPolicy.recurring?(profile_ref)
307
+ RecurringPayment.send(:conditions!, opening, closed)
308
+ return nil
309
+ end
277
310
  if PaymentPolicy.enabled?(profile_ref)
278
311
  PaymentPolicy.conditions!(opening, closed)
279
312
  base = PaymentPolicy.constraints!(opening)
@@ -306,9 +339,10 @@ module AP2MandateVerifier
306
339
  ScopedConstraints.send(:amount!, v["amount"])
307
340
  raise Invalid unless ScopedConstraints::CURRENCIES.include?(v["currency"])
308
341
  end
309
- def self.times!(v)
342
+ def self.times!(v, profile_ref: PROFILE_REF)
310
343
  %w[iat exp].each { |f| CheckoutReceipt.send(:epoch!, v[f]) }
311
- raise Invalid unless v["exp"] > v["iat"] && v["exp"] - v["iat"] <= MAX_LIFETIME
344
+ limit = PaymentPolicy.long?(profile_ref) ? LongPayment::MAX_CONTENT_LIFETIME : MAX_LIFETIME
345
+ raise Invalid unless v["exp"] > v["iat"] && v["exp"] - v["iat"] <= limit
312
346
  end
313
347
  def self.valid_now!(v, now)
314
348
  CheckoutReceipt.send(:epoch!, now)
@@ -319,6 +353,8 @@ module AP2MandateVerifier
319
353
  profile = InputValidator.snapshot!(profile_ref, max_bytes: 256)
320
354
  opening = if profile == TrustContext::SELECTIVE_PROFILE_REF
321
355
  SelectiveCheckout.send(:parse!, wire, complete: false).fetch(:opening)
356
+ elsif defined?(LongPayment) && profile == LongPayment::CHECKOUT_PROFILE_REF
357
+ RootDisclosures.send(:structural_root!, wire, profile_ref: profile).fetch("delegate_payload").first
322
358
  else
323
359
  raise Invalid unless profile == ScopedConstraints::MULTI_PROFILE_REF
324
360
  RootDisclosures.send(:structural_root!, wire).fetch("delegate_payload").first
@@ -343,7 +379,7 @@ module AP2MandateVerifier
343
379
  claims = jwt_payload!(compact, conditional ? "kb+sd-jwt" : "example+sd-jwt")
344
380
  fields = conditional ? %w[delegate_payload iat aud nonce sd_hash] : %w[delegate_payload _sd_alg]
345
381
  fields += ["_sd_alg"] if conditional && claims.key?("_sd_alg")
346
- if PaymentPolicy.enabled?(profile_ref)
382
+ if PaymentPolicy.marked?(profile_ref)
347
383
  fields += ["ap2_profile"]
348
384
  raise Invalid unless claims["ap2_profile"] == profile_ref
349
385
  end
@@ -365,6 +401,10 @@ module AP2MandateVerifier
365
401
  value = Buyer.send(:decode_disclosure!, disclosure, 2)
366
402
  claims = claims.merge("delegate_payload" => [value[1]])
367
403
  end
404
+ if (PaymentPolicy.recurring?(profile_ref) || PaymentPolicy.long?(profile_ref)) && !conditional
405
+ raise Invalid unless claims.fetch("delegate_payload").first.instance_of?(Hash) &&
406
+ claims.fetch("delegate_payload").first["vct"] == "mandate.payment.open.1"
407
+ end
368
408
  [claims, wire]
369
409
  end
370
410
  def self.root!(source, profile_ref: PROFILE_REF)
@@ -381,7 +421,7 @@ module AP2MandateVerifier
381
421
  require "securerandom"
382
422
  disclosure = Buyer.send(:encode, [Buyer.send(:encode, SecureRandom.random_bytes(16)), value])
383
423
  claims = {"delegate_payload" => [{"..." => digest(disclosure)}], "_sd_alg" => "sha-256"}
384
- claims["ap2_profile"] = profile_ref if PaymentPolicy.enabled?(profile_ref)
424
+ claims["ap2_profile"] = profile_ref if PaymentPolicy.marked?(profile_ref)
385
425
  preflight!(claims, "example+sd-jwt", disclosure, profile_ref: profile_ref)
386
426
  wire = MerchantSignature.sign!(claims, signer, key,
387
427
  typ: "example+sd-jwt") + "~" + disclosure + "~"