Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions code/sdk/python/ap2/sdk/constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
AgentRecurrence,
AllowedPayees,
AllowedPaymentInstruments,
AllowedPisps,

Check warning on line 22 in code/sdk/python/ap2/sdk/constraints.py

View workflow job for this annotation

GitHub Actions / spellcheck

Unknown word (Pisps)
AmountRange,
Budget,
ExecutionDate,
Expand Down Expand Up @@ -92,6 +92,8 @@
violations: list[str] = []
amount = closed_mandate.payment_amount

if amount.amount < 0:
violations.append(f'Amount {amount.amount} cannot be negative')
if (
self.constraint.currency
and amount.currency != self.constraint.currency
Expand All @@ -103,6 +105,7 @@
if (
self.constraint.min is not None
and amount.amount < self.constraint.min
and amount.amount >= 0
):
violations.append(
f'Amount {amount.amount} below minimum {self.constraint.min}'
Expand Down Expand Up @@ -251,10 +254,10 @@
return [f'Payment instrument {instrument.id} not in allowed list']


class AllowedPispEvaluator(PaymentConstraintEvaluator):

Check warning on line 257 in code/sdk/python/ap2/sdk/constraints.py

View workflow job for this annotation

GitHub Actions / spellcheck

Unknown word (Pisp)
"""Evaluates if the PISP is allowed."""

Check warning on line 258 in code/sdk/python/ap2/sdk/constraints.py

View workflow job for this annotation

GitHub Actions / spellcheck

Unknown word (PISP)

def __init__(self, constraint: AllowedPisps):

Check warning on line 260 in code/sdk/python/ap2/sdk/constraints.py

View workflow job for this annotation

GitHub Actions / spellcheck

Unknown word (Pisps)
self.constraint = constraint

def evaluate(
Expand All @@ -262,17 +265,17 @@
closed_mandate: PaymentMandate,
open_checkout_hash: str | None = None,
) -> list[str]:
if not closed_mandate.pisp:

Check warning on line 268 in code/sdk/python/ap2/sdk/constraints.py

View workflow job for this annotation

GitHub Actions / spellcheck

Unknown word (pisp)
return ['Missing PISP in closed mandate']

Check warning on line 269 in code/sdk/python/ap2/sdk/constraints.py

View workflow job for this annotation

GitHub Actions / spellcheck

Unknown word (PISP)

if any(
allowed.domain_name == closed_mandate.pisp.domain_name

Check warning on line 272 in code/sdk/python/ap2/sdk/constraints.py

View workflow job for this annotation

GitHub Actions / spellcheck

Unknown word (pisp)
and allowed.legal_name == closed_mandate.pisp.legal_name

Check warning on line 273 in code/sdk/python/ap2/sdk/constraints.py

View workflow job for this annotation

GitHub Actions / spellcheck

Unknown word (pisp)
and allowed.brand_name == closed_mandate.pisp.brand_name

Check warning on line 274 in code/sdk/python/ap2/sdk/constraints.py

View workflow job for this annotation

GitHub Actions / spellcheck

Unknown word (pisp)
for allowed in self.constraint.allowed
):
return []
return [f'PISP {closed_mandate.pisp} not in allowed list']

Check warning on line 278 in code/sdk/python/ap2/sdk/constraints.py

View workflow job for this annotation

GitHub Actions / spellcheck

Unknown word (PISP)


class BudgetEvaluator(PaymentConstraintEvaluator):
Expand All @@ -291,6 +294,14 @@
closed_mandate: PaymentMandate,
open_checkout_hash: str | None = None,
) -> list[str]:
if closed_mandate.payment_amount.amount < 0:
return [
(
'Payment amount '
f'{closed_mandate.payment_amount.amount} cannot be negative'
)
]

if closed_mandate.payment_amount.currency != self.constraint.currency:
return [
'Budget currency mismatch: expected '
Expand Down
37 changes: 37 additions & 0 deletions code/sdk/python/ap2/tests/constraints_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,29 @@ def test_payment_amount_no_min():
assert violations == []


def test_payment_amount_negative_rejected():
"""Negative payment amount is rejected even if min is absent."""
violations = check_payment_constraints(
_open_payment(
constraints=[
AmountRange(max=2000, currency='USD'),
]
),
_closed_payment(payment_amount=Amount(amount=-100, currency='USD')),
)
assert any('cannot be negative' in v for v in violations)

violations_with_min = check_payment_constraints(
_open_payment(
constraints=[
AmountRange(min=500, max=2000, currency='USD'),
]
),
_closed_payment(payment_amount=Amount(amount=-100, currency='USD')),
)
assert any('cannot be negative' in v for v in violations_with_min)


# ── check_payment_constraints – amount boundary cases (parameterized) ────


Expand Down Expand Up @@ -492,6 +515,20 @@ def test_payment_budget_missing_context():
)


def test_payment_budget_negative_amount_rejected():
"""Negative payment amount is rejected by budget evaluator."""
violations = check_payment_constraints(
_open_payment(
constraints=[
Budget(max=10.0, currency='USD'),
]
),
_closed_payment(payment_amount=Amount(amount=-100, currency='USD')),
mandate_context=MandateContext(total_amount=500, total_uses=1),
)
assert any('cannot be negative' in v for v in violations)


# ── check_payment_constraints – execution_date ───────────────────────────


Expand Down
Loading