adk_payments/protocol/ap2/
error.rs1use adk_core::{AdkError, ErrorCategory, ErrorComponent};
2use thiserror::Error;
3
4use crate::domain::MoneyError;
5
6#[derive(Debug, Error, PartialEq, Eq)]
8pub enum Ap2Error {
9 #[error(
10 "merchant authorization is required for cart mandate `{cart_id}`. Provide the merchant-signed authorization artifact before continuing."
11 )]
12 MissingMerchantAuthorization { cart_id: String },
13
14 #[error(
15 "user authorization is required for payment mandate `{payment_mandate_id}`. Obtain explicit user approval or a valid autonomous intent before continuing."
16 )]
17 MissingUserAuthorization { payment_mandate_id: String },
18
19 #[error(
20 "a detached user authorization artifact is required for the human-not-present intent on transaction `{transaction_id}`. Provide the signed intent authorization before continuing."
21 )]
22 MissingIntentAuthorization { transaction_id: String },
23
24 #[error(
25 "timestamp field `{field}` contains an invalid RFC 3339 value `{value}`. Normalize the AP2 artifact timestamp before retrying."
26 )]
27 InvalidTimestamp { field: String, value: String },
28
29 #[error(
30 "the AP2 artifact in `{field}` expired at `{expires_at}`. Refresh the mandate or obtain a new authorization before retrying."
31 )]
32 ExpiredArtifact { field: String, expires_at: String },
33
34 #[error(
35 "no AP2 verifier is configured for `{artifact_kind}`, so the artifact cannot be verified and is rejected. Configure a cryptographic verifier on `Ap2Adapter`, or call `allow_unverified_authorizations()` for local development only."
36 )]
37 AuthorizationVerifierNotConfigured { artifact_kind: String },
38
39 #[error(
40 "human-not-present transaction `{transaction_id}` lacks explicit authority constraints. Add merchant or SKU constraints before autonomous execution; a refundability requirement alone does not bound the agent's authority."
41 )]
42 MissingAuthorityConstraints { transaction_id: String },
43
44 #[error(
45 "cart mandate names merchant `{claimed}`, but its authorization was verified for merchant `{verified}`. Reject the cart or have the verified merchant sign it."
46 )]
47 MerchantIdentityMismatch { claimed: String, verified: String },
48
49 #[error(
50 "merchant `{merchant_name}` is outside the intent mandate authority constraints. Narrow the checkout or obtain fresh user approval."
51 )]
52 MerchantNotAuthorized { merchant_name: String },
53
54 #[error(
55 "the intent mandate constrains SKUs, but the cart mandate does not expose verifiable SKU identifiers. Use merchant constraints or include exact SKUs in cart metadata."
56 )]
57 SkuConstraintUnverifiable,
58
59 #[error(
60 "the cart includes SKU `{sku}` which is outside the signed intent authority constraints. Obtain fresh approval before continuing."
61 )]
62 SkuNotAuthorized { sku: String },
63
64 #[error(
65 "the intent mandate requires refundable items, but the cart contains non-refundable items. Return to the user or rebuild the cart with refundable items."
66 )]
67 RefundabilityRequired,
68
69 #[error(
70 "payment mandate `{payment_mandate_id}` does not match the current cart for field `{field}`. Rebuild the payment mandate from the latest cart state before retrying."
71 )]
72 PaymentMandateMismatch { payment_mandate_id: String, field: String },
73
74 #[error(
75 "payment mandate `{payment_mandate_id}` was already executed for transaction `{transaction_id}`. Payment mandates are single-use; create a new mandate for another payment."
76 )]
77 PaymentMandateReplayed { payment_mandate_id: String, transaction_id: String },
78
79 #[error(
80 "transaction `{transaction_id}` already executed payment mandate `{executed_payment_mandate_id}`. Start a new transaction for another payment."
81 )]
82 TransactionAlreadyPaid { transaction_id: String, executed_payment_mandate_id: String },
83
84 #[error(
85 "transaction `{transaction_id}` is `{state}` and no longer accepts payment mandates. Start a new transaction for another payment."
86 )]
87 TransactionNotPayable { transaction_id: String, state: String },
88
89 #[error(
90 "transaction `{transaction_id}` is bound to payment mandate `{bound_payment_mandate_id}`, not `{payment_mandate_id}`. Resubmit the bound mandate or start a new transaction."
91 )]
92 PaymentMandateRebind {
93 transaction_id: String,
94 bound_payment_mandate_id: String,
95 payment_mandate_id: String,
96 },
97
98 #[error("invalid AP2 amount: {0}")]
99 InvalidAmount(#[from] MoneyError),
100
101 #[error(
102 "canonical transaction `{transaction_id}` was not found. Create or resume the AP2 transaction before continuing."
103 )]
104 TransactionNotFound { transaction_id: String },
105
106 #[error(
107 "transaction `{transaction_id}` requires a return-to-user intervention, but no intervention service is configured. Wire an intervention backend or require explicit user authorization."
108 )]
109 InterventionServiceRequired { transaction_id: String },
110
111 #[error(
112 "the AP2 AgentCard extension URI `{uri}` is not supported. Use `{expected}` for the AP2 alpha baseline."
113 )]
114 InvalidExtensionUri { uri: String, expected: String },
115
116 #[error(
117 "the AP2 AgentCard extension must declare at least one AP2 role. Advertise one or more of shopper, merchant, credentials-provider, or payment-processor."
118 )]
119 MissingA2aRoles,
120}
121
122impl From<Ap2Error> for AdkError {
123 fn from(value: Ap2Error) -> Self {
124 let message = value.to_string();
125
126 match value {
127 Ap2Error::MissingMerchantAuthorization { .. }
128 | Ap2Error::MissingUserAuthorization { .. }
129 | Ap2Error::MissingIntentAuthorization { .. }
130 | Ap2Error::InvalidTimestamp { .. }
131 | Ap2Error::ExpiredArtifact { .. }
132 | Ap2Error::MissingAuthorityConstraints { .. }
133 | Ap2Error::MerchantNotAuthorized { .. }
134 | Ap2Error::SkuConstraintUnverifiable
135 | Ap2Error::SkuNotAuthorized { .. }
136 | Ap2Error::RefundabilityRequired
137 | Ap2Error::PaymentMandateMismatch { .. }
138 | Ap2Error::InvalidExtensionUri { .. }
139 | Ap2Error::MissingA2aRoles => AdkError::new(
140 ErrorComponent::Server,
141 ErrorCategory::InvalidInput,
142 "payments.ap2.invalid_input",
143 message,
144 ),
145 Ap2Error::MerchantIdentityMismatch { .. } => AdkError::new(
146 ErrorComponent::Server,
147 ErrorCategory::Forbidden,
148 "payments.ap2.merchant_identity_mismatch",
149 message,
150 ),
151 Ap2Error::AuthorizationVerifierNotConfigured { .. } => AdkError::new(
152 ErrorComponent::Server,
153 ErrorCategory::Unauthorized,
154 "payments.ap2.verifier_not_configured",
155 message,
156 ),
157 Ap2Error::PaymentMandateReplayed { .. }
158 | Ap2Error::TransactionAlreadyPaid { .. }
159 | Ap2Error::TransactionNotPayable { .. }
160 | Ap2Error::PaymentMandateRebind { .. } => AdkError::new(
161 ErrorComponent::Server,
162 ErrorCategory::Forbidden,
163 "payments.ap2.payment_mandate_refused",
164 message,
165 ),
166 Ap2Error::InvalidAmount(_) => AdkError::new(
167 ErrorComponent::Server,
168 ErrorCategory::InvalidInput,
169 "payments.ap2.invalid_amount",
170 message,
171 ),
172 Ap2Error::TransactionNotFound { .. } => AdkError::new(
173 ErrorComponent::Server,
174 ErrorCategory::NotFound,
175 "payments.ap2.not_found",
176 message,
177 ),
178 Ap2Error::InterventionServiceRequired { .. } => AdkError::new(
179 ErrorComponent::Server,
180 ErrorCategory::Unavailable,
181 "payments.ap2.intervention_required",
182 message,
183 ),
184 }
185 }
186}