ap2_mandate_verifier 1.5.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.
- checksums.yaml +4 -4
- data/README.ja.md +19 -8
- data/README.md +19 -8
- data/SECURITY.md +7 -3
- data/docs/LONG_PAYMENT.md +82 -0
- data/docs/PAYMENT_ACCEPTANCE.md +3 -1
- data/docs/PUBLIC_API.md +10 -2
- data/docs/RECURRING_PAYMENT.md +54 -0
- data/docs/RELEASE_150_CANDIDATE.md +2 -0
- data/docs/RELEASE_160_CANDIDATE.md +30 -0
- data/docs/SUPPORT_CONTRACT.md +6 -2
- data/examples/conditional_checkout.rb +3 -3
- data/examples/long_payment.rb +116 -0
- data/examples/long_payment_acceptance.rb +89 -0
- data/examples/recurring_payment.rb +99 -0
- data/lib/ap2_mandate_verifier/checkout_jwt.rb +1 -1
- data/lib/ap2_mandate_verifier/checkout_receipt.rb +10 -1
- data/lib/ap2_mandate_verifier/conditional_checkout.rb +43 -19
- data/lib/ap2_mandate_verifier/input_builder.rb +1 -1
- data/lib/ap2_mandate_verifier/long_payment.rb +201 -0
- data/lib/ap2_mandate_verifier/payment.rb +52 -12
- data/lib/ap2_mandate_verifier/payment_acceptance/long_sqlite_store.rb +272 -0
- data/lib/ap2_mandate_verifier/payment_acceptance/recurring_sqlite_store.rb +124 -0
- data/lib/ap2_mandate_verifier/payment_acceptance/sqlite_store.rb +38 -10
- data/lib/ap2_mandate_verifier/payment_acceptance.rb +28 -6
- data/lib/ap2_mandate_verifier/payment_policy.rb +7 -2
- data/lib/ap2_mandate_verifier/purchase_binding.rb +11 -5
- data/lib/ap2_mandate_verifier/recurring_payment.rb +111 -0
- data/lib/ap2_mandate_verifier/root_disclosures.rb +9 -6
- data/lib/ap2_mandate_verifier/scoped_constraints.rb +1 -1
- data/lib/ap2_mandate_verifier/selective_payment.rb +1 -1
- data/lib/ap2_mandate_verifier/trust_context.rb +6 -0
- data/lib/ap2_mandate_verifier.rb +4 -0
- metadata +16 -5
|
@@ -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",
|
|
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.
|
|
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
|
-
|
|
196
|
-
claims.fetch(
|
|
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.
|
|
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
|
-
|
|
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.
|
|
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.
|
|
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 + "~"
|
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module AP2MandateVerifier
|
|
4
|
+
module PaymentAcceptance
|
|
5
|
+
# Protected Host authority and period state share the occurrence transaction.
|
|
6
|
+
# An external registry feed must synchronize into this ledger before use.
|
|
7
|
+
class LongSQLiteStore < RecurringSQLiteStore
|
|
8
|
+
SCHEMA_VERSION = 3
|
|
9
|
+
MAX_CONTRACT = 524_288
|
|
10
|
+
SCHEDULE_POLICY = "host.utc_calendar_month_windows.1".freeze
|
|
11
|
+
SCHEMA = RecurringSQLiteStore::SCHEMA.merge(
|
|
12
|
+
"ap2_long_state" => "CREATE TABLE ap2_long_state (series_key TEXT PRIMARY KEY REFERENCES ap2_series(series_key), root_reference TEXT NOT NULL, epoch INTEGER NOT NULL CHECK(epoch>0), authority TEXT NOT NULL, authority_active INTEGER NOT NULL CHECK(authority_active IN (0,1)))",
|
|
13
|
+
"ap2_long_roots" => "CREATE TABLE ap2_long_roots (tenant TEXT NOT NULL, root_reference TEXT NOT NULL, series_key TEXT NOT NULL REFERENCES ap2_series(series_key), contract TEXT NOT NULL, active INTEGER NOT NULL CHECK(active IN (0,1)), PRIMARY KEY(tenant,root_reference))",
|
|
14
|
+
"ap2_long_periods" => "CREATE TABLE ap2_long_periods (series_key TEXT NOT NULL REFERENCES ap2_series(series_key), period_id TEXT NOT NULL, not_before INTEGER NOT NULL, not_after INTEGER NOT NULL, PRIMARY KEY(series_key,period_id))"
|
|
15
|
+
).transform_values(&:freeze).freeze
|
|
16
|
+
INDEXES = (RecurringSQLiteStore::INDEXES + [
|
|
17
|
+
["sqlite_autoindex_ap2_long_state_1", "ap2_long_state"],
|
|
18
|
+
["sqlite_autoindex_ap2_long_roots_1", "ap2_long_roots"],
|
|
19
|
+
["sqlite_autoindex_ap2_long_periods_1", "ap2_long_periods"]
|
|
20
|
+
]).map(&:freeze).freeze
|
|
21
|
+
|
|
22
|
+
def authorize_series!(tenant_id:, merchant_id:, approval_id:, payment_open:, checkout_open:,
|
|
23
|
+
trust_json:, selection_json:, verified_at:, periods:, schedule_policy_id:)
|
|
24
|
+
ids = series_ids!(tenant_id, merchant_id, approval_id)
|
|
25
|
+
pair = approved_pair!(ids, payment_open, checkout_open, trust_json, selection_json, verified_at)
|
|
26
|
+
windows = periods!(periods, pair, schedule_policy_id)
|
|
27
|
+
contract = pair_contract(pair).merge("schedule_policy_id" => schedule_policy_id, "periods" => windows,
|
|
28
|
+
"agreement_not_before" => pair.fetch(:not_before), "agreement_not_after" => pair.fetch(:not_after))
|
|
29
|
+
encoded = encode_contract!(contract)
|
|
30
|
+
transaction do
|
|
31
|
+
key = series_key(ids)
|
|
32
|
+
old = @db.get_first_value("SELECT CASE WHEN length(CAST(contract AS BLOB))<=#{MAX_CONTRACT} THEN contract END FROM ap2_series WHERE series_key=?", [key])
|
|
33
|
+
if old
|
|
34
|
+
raise Conflict unless old == encoded
|
|
35
|
+
next true # Never reactivate, reset, or rotate via enrollment.
|
|
36
|
+
end
|
|
37
|
+
begin
|
|
38
|
+
@db.execute("INSERT INTO ap2_series(series_key,tenant,merchant,approval,root_reference,contract) VALUES (?,?,?,?,?,?)",
|
|
39
|
+
[key, *ids, pair.fetch(:reference), encoded])
|
|
40
|
+
insert_root!(ids, key, pair)
|
|
41
|
+
@db.execute("INSERT INTO ap2_long_state(series_key,root_reference,epoch,authority,authority_active) VALUES (?,?,1,?,1)",
|
|
42
|
+
[key, pair.fetch(:reference), PaymentAcceptance.canonical(pair.fetch(:authority_binding))])
|
|
43
|
+
windows.each do |window|
|
|
44
|
+
@db.execute("INSERT INTO ap2_long_periods(series_key,period_id,not_before,not_after) VALUES (?,?,?,?)",
|
|
45
|
+
[key, *window.values_at("id", "not_before", "not_after")])
|
|
46
|
+
end
|
|
47
|
+
rescue SQLite3::ConstraintException
|
|
48
|
+
raise Conflict
|
|
49
|
+
end
|
|
50
|
+
true
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
# Protected backend CAS, not a request-driven 'current' flag.
|
|
55
|
+
def update_authority!(tenant_id:, merchant_id:, approval_id:, expected_epoch:, trust_json:, selection_json:, verified_at:)
|
|
56
|
+
expected_epoch!(expected_epoch)
|
|
57
|
+
ids = series_ids!(tenant_id, merchant_id, approval_id)
|
|
58
|
+
context = LongPayment.current_authority!(trust_json: trust_json, selection_json: selection_json,
|
|
59
|
+
merchant_id: merchant_id, verified_at: verified_at)
|
|
60
|
+
raise Invalid unless context.fetch(:tenant_id) == tenant_id
|
|
61
|
+
transaction do
|
|
62
|
+
agreement, active = series_row!(ids)
|
|
63
|
+
state, root = current_root!(ids, expected_epoch)
|
|
64
|
+
raise Invalid unless active == 1 && root.fetch("active") == 1 && verified_at < agreement.fetch("agreement_not_after")
|
|
65
|
+
raise Conflict unless root.fetch("root_key_fingerprints") == context.fetch(:root_key_fingerprints) &&
|
|
66
|
+
agreement.fetch("authority_principals") == context.fetch(:authority_principals)
|
|
67
|
+
authority = PaymentAcceptance.canonical(context.fetch(:authority_binding))
|
|
68
|
+
next state.fetch("epoch") if state.fetch("authority_active") == 1 && state.fetch("authority") == authority
|
|
69
|
+
@db.execute("UPDATE ap2_long_state SET authority=?,authority_active=1,epoch=epoch+1 WHERE series_key=? AND epoch=?",
|
|
70
|
+
[authority, series_key(ids), expected_epoch])
|
|
71
|
+
raise Conflict unless @db.changes == 1
|
|
72
|
+
expected_epoch + 1
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def invalidate_authority!(tenant_id:, merchant_id:, approval_id:, expected_epoch:)
|
|
77
|
+
expected_epoch!(expected_epoch)
|
|
78
|
+
ids = series_ids!(tenant_id, merchant_id, approval_id)
|
|
79
|
+
transaction do
|
|
80
|
+
current_root!(ids, expected_epoch)
|
|
81
|
+
@db.execute("UPDATE ap2_long_state SET authority_active=0,epoch=epoch+1 WHERE series_key=? AND epoch=?", [series_key(ids), expected_epoch])
|
|
82
|
+
raise Conflict unless @db.changes == 1
|
|
83
|
+
expected_epoch + 1
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def replace_root!(tenant_id:, merchant_id:, approval_id:, expected_epoch:, expected_root_reference:,
|
|
88
|
+
payment_open:, checkout_open:, trust_json:, selection_json:, verified_at:)
|
|
89
|
+
expected_epoch!(expected_epoch)
|
|
90
|
+
ids = series_ids!(tenant_id, merchant_id, approval_id)
|
|
91
|
+
pair = approved_pair!(ids, payment_open, checkout_open, trust_json, selection_json, verified_at)
|
|
92
|
+
transaction do
|
|
93
|
+
agreement, active = series_row!(ids)
|
|
94
|
+
state, = current_root!(ids, expected_epoch)
|
|
95
|
+
raise Invalid unless active == 1
|
|
96
|
+
raise Conflict unless state.fetch("root_reference") == expected_root_reference &&
|
|
97
|
+
agreement.fetch("economic_contract") == pair.fetch(:economic_contract) &&
|
|
98
|
+
agreement.fetch("authority_principals") == pair.fetch(:authority_principals) &&
|
|
99
|
+
pair.fetch(:not_after) <= agreement.fetch("agreement_not_after") &&
|
|
100
|
+
verified_at < agreement.fetch("agreement_not_after")
|
|
101
|
+
# Alias tombstones are never deleted/reused, even after key revocation.
|
|
102
|
+
insert_root!(ids, series_key(ids), pair)
|
|
103
|
+
@db.execute("UPDATE ap2_long_roots SET active=0 WHERE tenant=? AND root_reference=?", [ids[0], expected_root_reference])
|
|
104
|
+
@db.execute("UPDATE ap2_long_state SET root_reference=?,epoch=epoch+1,authority=?,authority_active=1 WHERE series_key=? AND epoch=?",
|
|
105
|
+
[pair.fetch(:reference), PaymentAcceptance.canonical(pair.fetch(:authority_binding)), series_key(ids), expected_epoch])
|
|
106
|
+
raise Conflict unless @db.changes == 1
|
|
107
|
+
expected_epoch + 1
|
|
108
|
+
end
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
def revoke_root!(tenant_id:, merchant_id:, approval_id:, expected_epoch:)
|
|
112
|
+
expected_epoch!(expected_epoch)
|
|
113
|
+
ids = series_ids!(tenant_id, merchant_id, approval_id)
|
|
114
|
+
transaction do
|
|
115
|
+
state, = current_root!(ids, expected_epoch)
|
|
116
|
+
@db.execute("UPDATE ap2_long_roots SET active=0 WHERE tenant=? AND root_reference=?", [ids[0], state.fetch("root_reference")])
|
|
117
|
+
@db.execute("UPDATE ap2_long_state SET authority_active=0,epoch=epoch+1 WHERE series_key=? AND epoch=?", [series_key(ids), expected_epoch])
|
|
118
|
+
raise Conflict unless @db.changes == 1
|
|
119
|
+
expected_epoch + 1
|
|
120
|
+
end
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
def usage(tenant_id:, merchant_id:, approval_id:)
|
|
124
|
+
ids = series_ids!(tenant_id, merchant_id, approval_id)
|
|
125
|
+
transaction do
|
|
126
|
+
contract, active, uses, spent = series_row!(ids)
|
|
127
|
+
state, root = current_root!(ids)
|
|
128
|
+
{currency: "JPY", reserved_occurrences: uses, reserved_amount: spent,
|
|
129
|
+
max_occurrences: contract.fetch("max_occurrences"), budget_max: contract.fetch("budget_max"), active: active == 1,
|
|
130
|
+
epoch: state.fetch("epoch"), root_reference: state.fetch("root_reference").dup.freeze,
|
|
131
|
+
authority_active: state.fetch("authority_active") == 1 && root.fetch("active") == 1}.freeze
|
|
132
|
+
end
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
def start_long!(ticket:, verified_at:, instruction:, signed_wires:, authority_binding:, valid_until:)
|
|
136
|
+
CheckoutReceipt.send(:epoch!, valid_until)
|
|
137
|
+
start_record!(ticket: ticket, verified_at: verified_at, instruction: instruction, signed_wires: signed_wires,
|
|
138
|
+
authority: PaymentAcceptance.copy!(authority_binding), fresh_valid_until: valid_until)
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
private
|
|
142
|
+
def expected_epoch!(value)
|
|
143
|
+
raise Invalid unless value.instance_of?(Integer) && value.between?(1, 9_007_199_254_740_000)
|
|
144
|
+
end
|
|
145
|
+
def approved_pair!(ids, payment, checkout, trust, selection, now)
|
|
146
|
+
pair = LongPayment.approved_pair!(payment_open: payment, checkout_open: checkout,
|
|
147
|
+
trust_json: trust, selection_json: selection, merchant_id: ids[1], verified_at: now)
|
|
148
|
+
raise Invalid unless pair.fetch(:tenant_id) == ids[0]
|
|
149
|
+
pair
|
|
150
|
+
end
|
|
151
|
+
def pair_contract(pair)
|
|
152
|
+
{"profile_ref" => LongPayment::PROFILE_REF, "root_reference" => pair.fetch(:reference),
|
|
153
|
+
"economic_contract" => pair.fetch(:economic_contract), "opening" => pair.fetch(:opening),
|
|
154
|
+
"max_occurrences" => pair.fetch(:max_occurrences), "budget_max" => pair.fetch(:budget_max), "currency" => pair.fetch(:currency),
|
|
155
|
+
"authority_principals" => pair.fetch(:authority_principals),
|
|
156
|
+
"root_key_fingerprints" => pair.fetch(:root_key_fingerprints), "not_before" => pair.fetch(:not_before), "not_after" => pair.fetch(:not_after)}
|
|
157
|
+
end
|
|
158
|
+
def encode_contract!(contract)
|
|
159
|
+
value = PaymentAcceptance.canonical(contract)
|
|
160
|
+
raise Invalid if value.bytesize > MAX_CONTRACT
|
|
161
|
+
value
|
|
162
|
+
end
|
|
163
|
+
def periods!(source, pair, policy)
|
|
164
|
+
raise Invalid unless policy == SCHEDULE_POLICY
|
|
165
|
+
list = PaymentAcceptance.copy!(source)
|
|
166
|
+
raise Invalid unless list.instance_of?(Array) && list.size.between?(1, 24)
|
|
167
|
+
months = []
|
|
168
|
+
list = list.map do |value|
|
|
169
|
+
raise Invalid unless value.instance_of?(Hash) && value.size == 3
|
|
170
|
+
item = value.transform_keys { |key| key.instance_of?(Symbol) ? key.to_s : key }
|
|
171
|
+
raise Invalid unless item.keys.sort == %w[id not_after not_before]
|
|
172
|
+
CheckoutReceipt.send(:text!, item["id"], 256)
|
|
173
|
+
%w[not_before not_after].each { |field| CheckoutReceipt.send(:epoch!, item[field]) }
|
|
174
|
+
raise Invalid unless pair.fetch(:not_before) <= item["not_before"] && item["not_before"] < item["not_after"] && item["not_after"] <= pair.fetch(:not_after)
|
|
175
|
+
start = Time.at(item.fetch("not_before")).utc
|
|
176
|
+
finish = start.month == 12 ? Time.utc(start.year + 1, 1, 1).to_i : Time.utc(start.year, start.month + 1, 1).to_i
|
|
177
|
+
raise Invalid unless item.fetch("not_after") <= finish
|
|
178
|
+
months << [start.year, start.month]
|
|
179
|
+
item
|
|
180
|
+
end.sort_by { |value| value.fetch("not_before") }
|
|
181
|
+
raise Invalid unless months.uniq.size == months.size && list.map { |value| value.fetch("id") }.uniq.size == list.size &&
|
|
182
|
+
list.each_cons(2).all? { |left,right| left.fetch("not_after") <= right.fetch("not_before") }
|
|
183
|
+
list
|
|
184
|
+
end
|
|
185
|
+
def insert_root!(ids, key, pair)
|
|
186
|
+
@db.execute("INSERT INTO ap2_long_roots(tenant,root_reference,series_key,contract,active) VALUES (?,?,?,?,1)",
|
|
187
|
+
[ids[0], pair.fetch(:reference), key, encode_contract!(pair_contract(pair))])
|
|
188
|
+
rescue SQLite3::ConstraintException
|
|
189
|
+
raise Conflict
|
|
190
|
+
end
|
|
191
|
+
def series_row!(ids)
|
|
192
|
+
row = @db.get_first_row("SELECT CASE WHEN length(CAST(contract AS BLOB))<=#{MAX_CONTRACT} THEN contract END,active,uses,spent FROM ap2_series WHERE series_key=?", [series_key(ids)])
|
|
193
|
+
raise Invalid unless row
|
|
194
|
+
contract = decode!(row[0], MAX_CONTRACT)
|
|
195
|
+
raise Unavailable unless [row[2],row[3]].all? { |n| n.instance_of?(Integer) && n >= 0 } &&
|
|
196
|
+
row[2] <= contract.fetch("max_occurrences") && row[3] <= contract.fetch("budget_max")
|
|
197
|
+
[contract, *row.drop(1)]
|
|
198
|
+
end
|
|
199
|
+
def current_root!(ids, expected = nil)
|
|
200
|
+
row = @db.get_first_row("SELECT root_reference,epoch,CASE WHEN length(CAST(authority AS BLOB))<=32768 THEN authority END,authority_active FROM ap2_long_state WHERE series_key=?", [series_key(ids)])
|
|
201
|
+
raise Invalid unless row
|
|
202
|
+
raise Unavailable unless row[1].instance_of?(Integer) && row[1].between?(1, 9_007_199_254_740_000) && [0,1].include?(row[3])
|
|
203
|
+
raise Conflict if !expected.nil? && (!expected.instance_of?(Integer) || row[1] != expected)
|
|
204
|
+
state = {"root_reference" => row[0], "epoch" => row[1], "authority" => InputValidator.snapshot!(row[2], max_bytes: 32768), "authority_active" => row[3]}
|
|
205
|
+
root = @db.get_first_row("SELECT CASE WHEN length(CAST(contract AS BLOB))<=#{MAX_CONTRACT} THEN contract END,active FROM ap2_long_roots WHERE tenant=? AND root_reference=? AND series_key=?",
|
|
206
|
+
[ids[0], row[0], series_key(ids)])
|
|
207
|
+
raise Unavailable unless root && [0,1].include?(root[1])
|
|
208
|
+
[state, decode!(root[0], MAX_CONTRACT).merge("active" => root[1])]
|
|
209
|
+
end
|
|
210
|
+
def approval_policy!(profile)
|
|
211
|
+
raise Invalid unless LongPayment.profile?(profile)
|
|
212
|
+
end
|
|
213
|
+
def period!(ids, at, occurrence)
|
|
214
|
+
CheckoutReceipt.send(:epoch!, at)
|
|
215
|
+
rows = @db.execute("SELECT period_id,not_before,not_after FROM ap2_long_periods WHERE series_key=? AND not_before<=? AND ?<not_after", [series_key(ids), at, at])
|
|
216
|
+
raise Invalid unless rows.size == 1 && rows[0][0] == occurrence
|
|
217
|
+
rows[0]
|
|
218
|
+
end
|
|
219
|
+
def validate_reservation!(scope, record)
|
|
220
|
+
approval_policy!(record.fetch("profile_ref"))
|
|
221
|
+
ids = scope_series(scope)
|
|
222
|
+
agreement, active = series_row!(ids)
|
|
223
|
+
state, root = current_root!(ids)
|
|
224
|
+
raise Invalid unless active == 1 && root.fetch("active") == 1 && state.fetch("authority_active") == 1 &&
|
|
225
|
+
PaymentAcceptance.canonical(record.fetch("authority_binding")) == state.fetch("authority")
|
|
226
|
+
wire = record.fetch("signed_wires").fetch("payment").fetch("open_sd_jwt")
|
|
227
|
+
raise Conflict unless Payment.send(:digest, wire.split("~",2).first) == state.fetch("root_reference")
|
|
228
|
+
period = period!(ids, record.fetch("accepted_at"), scope.fetch("occurrence_id"))
|
|
229
|
+
amount = record.fetch("payment").fetch("payment_amount")
|
|
230
|
+
raise Invalid unless amount.fetch("currency") == "JPY" && amount.fetch("amount").instance_of?(Integer) && amount.fetch("amount") > 0
|
|
231
|
+
record["accepted_epoch"] = state.fetch("epoch")
|
|
232
|
+
record["valid_until"] = [record.fetch("valid_until"), period[2], agreement.fetch("agreement_not_after"), root.fetch("not_after")].min
|
|
233
|
+
end
|
|
234
|
+
def validate_start!(record, at)
|
|
235
|
+
ids = scope_series(record.fetch("scope"))
|
|
236
|
+
agreement, active = series_row!(ids)
|
|
237
|
+
state, root = current_root!(ids)
|
|
238
|
+
raise Invalid unless active == 1 && root.fetch("active") == 1 && state.fetch("authority_active") == 1 &&
|
|
239
|
+
state.fetch("epoch") == record.fetch("accepted_epoch") && at < agreement.fetch("agreement_not_after") && at < root.fetch("not_after")
|
|
240
|
+
period!(ids, at, record.fetch("scope").fetch("occurrence_id"))
|
|
241
|
+
end
|
|
242
|
+
def validate_current_authority!(record, _at, authority)
|
|
243
|
+
state, = current_root!(scope_series(record.fetch("scope")))
|
|
244
|
+
raise Invalid unless authority && PaymentAcceptance.canonical(authority) == state.fetch("authority") &&
|
|
245
|
+
PaymentAcceptance.canonical(record.fetch("authority_binding")) == state.fetch("authority")
|
|
246
|
+
end
|
|
247
|
+
|
|
248
|
+
%i[authorize_series! update_authority! invalidate_authority! replace_root! revoke_root! usage start_long!].each do |name|
|
|
249
|
+
implementation = instance_method(name)
|
|
250
|
+
required = implementation.parameters.map(&:last).freeze
|
|
251
|
+
define_method(name) do |**arguments|
|
|
252
|
+
if name == :start_long!
|
|
253
|
+
ticket = arguments.fetch(:ticket)
|
|
254
|
+
raise Invalid unless ticket.instance_of?(Ticket)
|
|
255
|
+
arguments = PaymentAcceptance.copy!(arguments.reject { |key,_| key == :ticket }).merge(ticket: ticket)
|
|
256
|
+
else
|
|
257
|
+
arguments = PaymentAcceptance.copy!(arguments)
|
|
258
|
+
end
|
|
259
|
+
raise Invalid unless arguments.keys.sort == required.sort
|
|
260
|
+
implementation.bind_call(self, **arguments)
|
|
261
|
+
rescue Conflict
|
|
262
|
+
raise Conflict.new("payment acceptance conflict"), cause: nil
|
|
263
|
+
rescue Unavailable
|
|
264
|
+
raise Unavailable.new, cause: nil
|
|
265
|
+
rescue StandardError, LoadError
|
|
266
|
+
raise Invalid.new, cause: nil
|
|
267
|
+
end
|
|
268
|
+
public name
|
|
269
|
+
end
|
|
270
|
+
end
|
|
271
|
+
end
|
|
272
|
+
end
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module AP2MandateVerifier
|
|
4
|
+
module PaymentAcceptance
|
|
5
|
+
# Explicit new database, never an automatic rewrite/reset of a v1.5 ledger.
|
|
6
|
+
# Quotas are charged on durable reservation, not on an untrusted Receipt.
|
|
7
|
+
class RecurringSQLiteStore < SQLiteStore
|
|
8
|
+
SCHEMA_VERSION = 2
|
|
9
|
+
SCHEMA = SQLiteStore::SCHEMA.merge(
|
|
10
|
+
"ap2_series" => "CREATE TABLE ap2_series (series_key TEXT PRIMARY KEY, tenant TEXT NOT NULL, merchant TEXT NOT NULL, approval TEXT NOT NULL, root_reference TEXT NOT NULL, contract TEXT NOT NULL, active INTEGER NOT NULL DEFAULT 1 CHECK(active IN (0,1)), uses INTEGER NOT NULL DEFAULT 0 CHECK(uses>=0), spent INTEGER NOT NULL DEFAULT 0 CHECK(spent>=0), UNIQUE(tenant,root_reference), UNIQUE(tenant,approval))"
|
|
11
|
+
).freeze
|
|
12
|
+
INDEXES = (SQLiteStore::INDEXES + [
|
|
13
|
+
["sqlite_autoindex_ap2_series_1", "ap2_series"],
|
|
14
|
+
["sqlite_autoindex_ap2_series_2", "ap2_series"],
|
|
15
|
+
["sqlite_autoindex_ap2_series_3", "ap2_series"]
|
|
16
|
+
]).map(&:freeze).freeze
|
|
17
|
+
|
|
18
|
+
def authorize_series!(tenant_id:, merchant_id:, approval_id:, open_sd_jwt:, profile_ref:,
|
|
19
|
+
trusted_provider_public_key_pem:, expected_agent_public_key_pem:, verified_at:)
|
|
20
|
+
ids = series_ids!(tenant_id, merchant_id, approval_id)
|
|
21
|
+
checked = RecurringPayment.approved_open!(open_sd_jwt: open_sd_jwt, profile_ref: profile_ref,
|
|
22
|
+
trusted_provider_public_key_pem: trusted_provider_public_key_pem,
|
|
23
|
+
expected_agent_public_key_pem: expected_agent_public_key_pem, verified_at: verified_at)
|
|
24
|
+
contract = PaymentAcceptance.canonical({"profile_ref" => profile_ref, "root_reference" => checked.fetch(:reference),
|
|
25
|
+
"opening" => checked.fetch(:opening), "max_occurrences" => checked.fetch(:max_occurrences),
|
|
26
|
+
"budget_max" => checked.fetch(:budget_max), "currency" => checked.fetch(:currency)})
|
|
27
|
+
raise Invalid if contract.bytesize > 131_072
|
|
28
|
+
transaction do
|
|
29
|
+
key = series_key(ids)
|
|
30
|
+
old = @db.get_first_row("SELECT CASE WHEN length(CAST(contract AS BLOB))<=131072 THEN contract END FROM ap2_series WHERE series_key=?", [key])
|
|
31
|
+
raise Conflict if old && old[0] != contract
|
|
32
|
+
unless old
|
|
33
|
+
begin
|
|
34
|
+
@db.execute("INSERT INTO ap2_series(series_key,tenant,merchant,approval,root_reference,contract) VALUES (?,?,?,?,?,?)",
|
|
35
|
+
[key, *ids, checked.fetch(:reference), contract])
|
|
36
|
+
rescue SQLite3::ConstraintException
|
|
37
|
+
raise Conflict
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
true # Existing revoked grants are never reactivated or counters reset.
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def revoke_series!(tenant_id:, merchant_id:, approval_id:)
|
|
45
|
+
ids = series_ids!(tenant_id, merchant_id, approval_id)
|
|
46
|
+
transaction do
|
|
47
|
+
@db.execute("UPDATE ap2_series SET active=0 WHERE series_key=?", [series_key(ids)])
|
|
48
|
+
raise Invalid unless @db.changes == 1
|
|
49
|
+
true
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def usage(tenant_id:, merchant_id:, approval_id:)
|
|
54
|
+
ids = series_ids!(tenant_id, merchant_id, approval_id)
|
|
55
|
+
transaction do
|
|
56
|
+
contract, active, uses, spent = series_row!(ids)
|
|
57
|
+
{currency: "JPY", reserved_occurrences: uses, reserved_amount: spent,
|
|
58
|
+
max_occurrences: contract.fetch("max_occurrences"), budget_max: contract.fetch("budget_max"), active: active == 1}.freeze
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
private
|
|
63
|
+
def series_ids!(tenant, merchant, approval)
|
|
64
|
+
[tenant, merchant, approval].map { |v| InputValidator.snapshot!(v, max_bytes: 256).tap { |s| CheckoutReceipt.send(:text!, s, 256) } }.freeze
|
|
65
|
+
end
|
|
66
|
+
def series_key(ids) = PaymentAcceptance.digest(PaymentAcceptance.canonical(ids))
|
|
67
|
+
def scope_series(scope) = series_ids!(*scope.values_at("tenant_id", "merchant_id", "approval_id"))
|
|
68
|
+
def series_row!(ids)
|
|
69
|
+
row = @db.get_first_row("SELECT CASE WHEN length(CAST(contract AS BLOB))<=131072 THEN contract END,active,uses,spent FROM ap2_series WHERE series_key=?", [series_key(ids)])
|
|
70
|
+
raise Invalid unless row
|
|
71
|
+
contract = decode!(row[0], 131_072)
|
|
72
|
+
raise Unavailable unless [row[2], row[3]].all? { |v| v.instance_of?(Integer) && v >= 0 } &&
|
|
73
|
+
row[2] <= contract.fetch("max_occurrences") && row[3] <= contract.fetch("budget_max")
|
|
74
|
+
[contract, *row.drop(1)]
|
|
75
|
+
end
|
|
76
|
+
def approval_policy!(profile)
|
|
77
|
+
raise Invalid unless RecurringPayment.profile?(profile)
|
|
78
|
+
end
|
|
79
|
+
def validate_reservation!(scope, record)
|
|
80
|
+
approval_policy!(record.fetch("profile_ref"))
|
|
81
|
+
contract, active = series_row!(scope_series(scope))
|
|
82
|
+
raise Invalid unless active == 1 && contract.fetch("profile_ref") == record.fetch("profile_ref")
|
|
83
|
+
wires = record.fetch("signed_wires").fetch("payment")
|
|
84
|
+
open_wire = wires["open_sd_jwt"] || wires["open_presentation"]
|
|
85
|
+
raise Invalid unless open_wire.instance_of?(String) # Bounded wire was verified by Service.
|
|
86
|
+
# Local series identity uses base64url(SHA256(original issuer JWT)).
|
|
87
|
+
# AP2 Checkout/Receipt references retain their existing exact-wire semantics.
|
|
88
|
+
root_reference = Payment.send(:digest, open_wire.split("~", 2).first)
|
|
89
|
+
raise Conflict unless root_reference == contract.fetch("root_reference")
|
|
90
|
+
amount = record.fetch("payment").fetch("payment_amount")
|
|
91
|
+
raise Invalid unless amount.fetch("currency") == "JPY" && amount.fetch("amount").instance_of?(Integer) && amount.fetch("amount") > 0
|
|
92
|
+
end
|
|
93
|
+
def charge_reservation!(scope, record)
|
|
94
|
+
ids = scope_series(scope)
|
|
95
|
+
contract, active, uses, spent = series_row!(ids)
|
|
96
|
+
amount = record.fetch("payment").fetch("payment_amount").fetch("amount")
|
|
97
|
+
raise Invalid unless active == 1 && uses < contract.fetch("max_occurrences") && spent + amount <= contract.fetch("budget_max")
|
|
98
|
+
@db.execute("UPDATE ap2_series SET uses=uses+1,spent=spent+? WHERE series_key=? AND active=1 AND uses=? AND spent=?",
|
|
99
|
+
[amount, series_key(ids), uses, spent])
|
|
100
|
+
raise Unavailable unless @db.changes == 1
|
|
101
|
+
end
|
|
102
|
+
def validate_start!(record, verified_at)
|
|
103
|
+
contract, active = series_row!(scope_series(record.fetch("scope")))
|
|
104
|
+
raise Invalid unless active == 1 && verified_at < contract.fetch("opening").fetch("exp")
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
%i[authorize_series! revoke_series! usage].each do |name|
|
|
108
|
+
implementation = instance_method(name)
|
|
109
|
+
required = implementation.parameters.map(&:last).freeze
|
|
110
|
+
define_method(name) do |**arguments|
|
|
111
|
+
raise Invalid unless arguments.keys.sort == required.sort
|
|
112
|
+
implementation.bind_call(self, **arguments)
|
|
113
|
+
rescue Conflict
|
|
114
|
+
raise Conflict.new("payment acceptance conflict"), cause: nil
|
|
115
|
+
rescue Unavailable
|
|
116
|
+
raise Unavailable.new, cause: nil
|
|
117
|
+
rescue StandardError, LoadError
|
|
118
|
+
raise Invalid.new, cause: nil
|
|
119
|
+
end
|
|
120
|
+
public name
|
|
121
|
+
end
|
|
122
|
+
end
|
|
123
|
+
end
|
|
124
|
+
end
|