adk-payments 2.3.0

Protocol-neutral agentic commerce and payment orchestration for ADK-Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
use serde_json::Value;

use crate::AP2_ALPHA_BASELINE;
use crate::domain::{
    Cart, CartLine, FulfillmentKind, FulfillmentSelection, Money, MoneyError, OrderSnapshot,
    OrderState, PaymentMethodSelection, PriceAdjustment, PriceAdjustmentKind, ProtocolDescriptor,
    ProtocolExtensionEnvelope, ProtocolExtensions, ReceiptState, TransactionRecord,
    TransactionState,
};
use crate::kernel::{
    CommerceContext, CreateCheckoutCommand, ExecutePaymentCommand, PaymentExecutionOutcome,
    SyncPaymentOutcomeCommand,
};
use crate::protocol::ap2::error::Ap2Error;
use crate::protocol::ap2::types::{
    CartMandate, IntentMandate, PaymentCurrencyAmount, PaymentMandate, PaymentReceipt,
    PaymentStatusEnvelope,
};

pub(crate) fn ap2_descriptor() -> ProtocolDescriptor {
    ProtocolDescriptor::ap2(AP2_ALPHA_BASELINE)
}

pub(crate) fn merge_extensions(
    mut left: ProtocolExtensions,
    right: ProtocolExtensions,
) -> ProtocolExtensions {
    for envelope in right.0 {
        left.push(envelope);
    }
    left
}

pub(crate) fn placeholder_cart_from_intent(intent: &IntentMandate) -> Cart {
    Cart {
        cart_id: None,
        lines: vec![CartLine {
            line_id: "intent".to_string(),
            merchant_sku: intent.skus.as_ref().and_then(|skus| skus.first().cloned()),
            title: "intent authorization".to_string(),
            quantity: 1,
            unit_price: Money::new("XXX", 0, 2),
            total_price: Money::new("XXX", 0, 2),
            product_class: Some("intent".to_string()),
            extensions: ProtocolExtensions::default(),
        }],
        subtotal: Some(Money::new("XXX", 0, 2)),
        adjustments: Vec::new(),
        total: Money::new("XXX", 0, 2),
        affiliate_attribution: None,
        extensions: ProtocolExtensions::default(),
    }
}

/// Every amount that contributes to the canonical cart built from `mandate`.
fn cart_amounts(mandate: &CartMandate) -> impl Iterator<Item = &PaymentCurrencyAmount> {
    let details = &mandate.contents.payment_request.details;
    let selected_shipping = details
        .shipping_options
        .iter()
        .flatten()
        .filter(|option| option.selected)
        .map(|option| &option.amount);
    let modifier_items = details
        .modifiers
        .iter()
        .flatten()
        .flat_map(|modifier| modifier.additional_display_items.iter().flatten())
        .map(|item| &item.amount);
    details
        .display_items
        .iter()
        .map(|item| &item.amount)
        .chain(std::iter::once(&details.total.amount))
        .chain(selected_shipping)
        .chain(modifier_items)
}

/// Returns the single scale every amount in `mandate` is normalised to.
///
/// The scale is the cart currency's ISO 4217 minor-unit scale, raised to the
/// most precise amount in the cart so that no amount is rounded. Currencies
/// without a minor unit start from scale 0.
pub(crate) fn cart_amount_scale(mandate: &CartMandate) -> Result<u32, Ap2Error> {
    let currency = &mandate.contents.payment_request.details.total.amount.currency;
    let mut scale = Money::iso_minor_unit_scale(currency).unwrap_or(0);
    for amount in cart_amounts(mandate) {
        if !amount.currency.eq_ignore_ascii_case(currency) {
            return Err(MoneyError::CurrencyMismatch {
                expected: currency.clone(),
                found: amount.currency.clone(),
            }
            .into());
        }
        scale = scale.max(amount.to_exact_money()?.scale);
    }
    Ok(scale)
}

fn money_at_scale(amount: &PaymentCurrencyAmount, scale: u32) -> Result<Money, Ap2Error> {
    Ok(amount.to_exact_money()?.rescaled(scale)?)
}

pub(crate) fn cart_from_cart_mandate(mandate: &CartMandate) -> Result<Cart, Ap2Error> {
    let scale = cart_amount_scale(mandate)?;
    let details = &mandate.contents.payment_request.details;
    let total = money_at_scale(&details.total.amount, scale)?;
    let mut lines = Vec::with_capacity(details.display_items.len());
    let mut subtotal = Money::new(total.currency.clone(), 0, scale);

    for (index, item) in details.display_items.iter().enumerate() {
        let line_total = money_at_scale(&item.amount, scale)?;
        subtotal = subtotal.checked_add(&line_total)?;
        lines.push(CartLine {
            line_id: format!("{}:{index}", mandate.contents.id),
            merchant_sku: None,
            title: item.label.clone(),
            quantity: 1,
            unit_price: line_total.clone(),
            total_price: line_total,
            product_class: None,
            extensions: ProtocolExtensions::default(),
        });
    }

    let mut adjustments = Vec::new();
    if let Some(options) = &details.shipping_options {
        for option in options.iter().filter(|option| option.selected) {
            adjustments.push(PriceAdjustment {
                adjustment_id: option.id.clone(),
                kind: PriceAdjustmentKind::Shipping,
                label: option.label.clone(),
                amount: money_at_scale(&option.amount, scale)?,
                extensions: ProtocolExtensions::default(),
            });
        }
    }

    if let Some(modifiers) = &details.modifiers {
        for modifier in modifiers {
            if let Some(items) = &modifier.additional_display_items {
                for (index, item) in items.iter().enumerate() {
                    adjustments.push(PriceAdjustment {
                        adjustment_id: format!("{}:{index}", modifier.supported_methods),
                        kind: PriceAdjustmentKind::Fee,
                        label: item.label.clone(),
                        amount: money_at_scale(&item.amount, scale)?,
                        extensions: ProtocolExtensions::default(),
                    });
                }
            }
        }
    }

    let mut allocated = subtotal.clone();
    for adjustment in &adjustments {
        allocated = allocated.checked_add(&adjustment.amount)?;
    }
    let unallocated = total.checked_sub(&allocated)?;
    if unallocated.amount_minor != 0 {
        adjustments.push(PriceAdjustment {
            adjustment_id: "ap2_unallocated_delta".to_string(),
            kind: PriceAdjustmentKind::Other("ap2".to_string()),
            label: "AP2 total reconciliation".to_string(),
            amount: unallocated,
            extensions: ProtocolExtensions::default(),
        });
    }

    Ok(Cart {
        cart_id: Some(mandate.contents.id.clone()),
        lines,
        subtotal: Some(subtotal),
        adjustments,
        total,
        affiliate_attribution: None,
        extensions: ProtocolExtensions::default(),
    })
}

pub(crate) fn fulfillment_from_cart_mandate(
    mandate: &CartMandate,
) -> Result<Option<FulfillmentSelection>, Ap2Error> {
    let Some(option) = mandate
        .contents
        .payment_request
        .details
        .shipping_options
        .as_ref()
        .and_then(|options| options.iter().find(|option| option.selected))
    else {
        return Ok(None);
    };
    Ok(Some(FulfillmentSelection {
        fulfillment_id: option.id.clone(),
        kind: FulfillmentKind::Shipping,
        label: option.label.clone(),
        amount: Some(money_at_scale(&option.amount, cart_amount_scale(mandate)?)?),
        destination: None,
        requires_user_selection: mandate
            .contents
            .payment_request
            .options
            .as_ref()
            .is_some_and(|options| options.request_shipping),
        extensions: ProtocolExtensions::default(),
    }))
}

pub(crate) fn intent_create_checkout_command(
    intent: &IntentMandate,
    context: CommerceContext,
) -> CreateCheckoutCommand {
    CreateCheckoutCommand { context, cart: placeholder_cart_from_intent(intent), fulfillment: None }
}

pub(crate) fn payment_method_selection(mandate: &PaymentMandate) -> PaymentMethodSelection {
    let reference = mandate
        .payment_mandate_contents
        .payment_response
        .details
        .as_ref()
        .and_then(|details| details.get("token"))
        .and_then(Value::as_object)
        .and_then(|token| token.get("id"))
        .and_then(Value::as_str)
        .map(str::to_string);

    PaymentMethodSelection {
        selection_kind: mandate.payment_mandate_contents.payment_response.method_name.clone(),
        reference,
        display_hint: None,
        extensions: ProtocolExtensions::default(),
    }
}

/// Builds the payment command, expressing the amount at the cart's `scale`.
pub(crate) fn execute_payment_command(
    mandate: &PaymentMandate,
    scale: u32,
    context: CommerceContext,
    supporting_evidence_refs: Vec<crate::domain::EvidenceReference>,
) -> Result<ExecutePaymentCommand, Ap2Error> {
    Ok(ExecutePaymentCommand {
        context,
        amount: money_at_scale(
            &mandate.payment_mandate_contents.payment_details_total.amount,
            scale,
        )?,
        selected_payment_method: Some(payment_method_selection(mandate)),
        supporting_evidence_refs,
        extensions: ProtocolExtensions::default(),
    })
}

pub(crate) fn sync_payment_outcome_command(
    record: Option<&TransactionRecord>,
    receipt: &PaymentReceipt,
    context: CommerceContext,
) -> SyncPaymentOutcomeCommand {
    let outcome = match receipt.payment_status {
        PaymentStatusEnvelope::Success(_) => PaymentExecutionOutcome::Completed,
        PaymentStatusEnvelope::Error(_) | PaymentStatusEnvelope::Failure(_) => {
            PaymentExecutionOutcome::Failed
        }
    };
    let order_state = match outcome {
        PaymentExecutionOutcome::Completed => OrderState::Completed,
        PaymentExecutionOutcome::Failed => OrderState::Failed,
        PaymentExecutionOutcome::Authorized | PaymentExecutionOutcome::InterventionRequired => {
            OrderState::Authorized
        }
    };
    let receipt_state = match outcome {
        PaymentExecutionOutcome::Completed => ReceiptState::Settled,
        PaymentExecutionOutcome::Failed => ReceiptState::Failed,
        PaymentExecutionOutcome::Authorized => ReceiptState::Authorized,
        PaymentExecutionOutcome::InterventionRequired => ReceiptState::Pending,
    };

    SyncPaymentOutcomeCommand {
        context,
        outcome,
        order: Some(OrderSnapshot {
            order_id: record
                .and_then(|record| record.order.as_ref().and_then(|order| order.order_id.clone()))
                .or_else(|| Some(receipt.payment_mandate_id.clone())),
            receipt_id: Some(receipt.payment_id.clone()),
            state: order_state,
            receipt_state,
            extensions: ProtocolExtensions::default(),
        }),
        intervention: None,
        generated_evidence_refs: Vec::new(),
    }
}

pub(crate) fn update_record_extensions(
    record: &mut TransactionRecord,
    envelope: ProtocolExtensionEnvelope,
) {
    if !record.extensions.as_slice().contains(&envelope) {
        record.attach_extension(envelope);
    }
}

pub(crate) fn update_record_state_from_receipt(
    record: &mut TransactionRecord,
    receipt: &PaymentReceipt,
) {
    if record.order.is_none() {
        record.order = Some(OrderSnapshot {
            order_id: Some(receipt.payment_mandate_id.clone()),
            receipt_id: Some(receipt.payment_id.clone()),
            state: OrderState::Draft,
            receipt_state: ReceiptState::Pending,
            extensions: ProtocolExtensions::default(),
        });
    }

    if let Some(order) = &mut record.order {
        order.receipt_id = Some(receipt.payment_id.clone());
        match receipt.payment_status {
            PaymentStatusEnvelope::Success(_) => {
                order.state = OrderState::Completed;
                order.receipt_state = ReceiptState::Settled;
            }
            PaymentStatusEnvelope::Error(_) | PaymentStatusEnvelope::Failure(_) => {
                order.state = OrderState::Failed;
                order.receipt_state = ReceiptState::Failed;
            }
        }
    }

    match receipt.payment_status {
        PaymentStatusEnvelope::Success(_) => {
            record.state = TransactionState::Completed;
        }
        PaymentStatusEnvelope::Error(_) | PaymentStatusEnvelope::Failure(_) => {
            record.state = TransactionState::Failed;
        }
    }
}

#[cfg(test)]
mod tests {
    use proptest::prelude::*;
    use serde_json::{Number, json};

    use super::*;

    /// One generated decimal amount: magnitude, fraction digits, and sign.
    fn decimal() -> impl Strategy<Value = (i64, u32)> {
        (0_i64..1_000_000_000, 0_u32..=4, any::<bool>()).prop_map(|(magnitude, scale, negative)| {
            (if negative { -magnitude } else { magnitude }, scale)
        })
    }

    fn number(amount_minor: i64, scale: u32) -> Number {
        Money::new("USD", amount_minor, scale).to_decimal_string().parse().unwrap()
    }

    fn mandate(currency: &str, items: &[(i64, u32)], total: (i64, u32)) -> CartMandate {
        let display_items: Vec<_> = items
            .iter()
            .enumerate()
            .map(|(index, (amount_minor, scale))| {
                json!({
                    "label": format!("item-{index}"),
                    "amount": {"currency": currency, "value": number(*amount_minor, *scale)}
                })
            })
            .collect();
        serde_json::from_value(json!({
            "contents": {
                "id": "cart-prop",
                "user_cart_confirmation_required": false,
                "payment_request": {
                    "method_data": [{"supported_methods": "CARD"}],
                    "details": {
                        "id": "cart-prop",
                        "display_items": display_items,
                        "total": {
                            "label": "Total",
                            "amount": {"currency": currency, "value": number(total.0, total.1)}
                        }
                    }
                },
                "cart_expiry": "2099-01-01T00:00:00Z",
                "merchant_name": "Merchant"
            },
            "merchant_authorization": "signed"
        }))
        .unwrap()
    }

    /// Exact sum of `items` at scale `scale`, computed independently in `i128`.
    fn reference_sum(items: &[(i64, u32)], scale: u32) -> i128 {
        items
            .iter()
            .map(|(amount_minor, item_scale)| {
                i128::from(*amount_minor) * 10_i128.pow(scale - item_scale)
            })
            .sum()
    }

    proptest! {
        #![proptest_config(ProptestConfig::with_cases(256))]

        #[test]
        fn balanced_cart_has_one_scale_and_no_reconciliation(
            currency in prop::sample::select(vec!["USD", "JPY", "KWD", "XXX"]),
            items in prop::collection::vec(decimal(), 1..6),
        ) {
            let item_scale = items.iter().map(|(_, scale)| *scale).max().unwrap_or(0);
            let total_minor = i64::try_from(reference_sum(&items, item_scale)).unwrap();
            let cart = cart_from_cart_mandate(&mandate(currency, &items, (total_minor, item_scale)))
                .unwrap();

            let scale = cart.total.scale;
            let significant = items
                .iter()
                .chain(std::iter::once(&(total_minor, item_scale)))
                .map(|(amount, scale)| Money::new(currency, *amount, *scale))
                .map(|money| Money::parse_decimal(currency, &money.to_decimal_string()).unwrap().scale)
                .max()
                .unwrap_or(0);
            prop_assert_eq!(
                scale,
                Money::iso_minor_unit_scale(currency).unwrap_or(0).max(significant)
            );
            prop_assert!(cart.lines.iter().all(|line| line.total_price.scale == scale));
            let line_sum: i128 =
                cart.lines.iter().map(|line| i128::from(line.total_price.amount_minor)).sum();
            prop_assert_eq!(line_sum, i128::from(cart.total.amount_minor));
            prop_assert_eq!(cart.subtotal, Some(cart.total.clone()));
            prop_assert!(cart.adjustments.is_empty());
            prop_assert_eq!(
                cart.total.compare_amount(&Money::new(currency, total_minor, item_scale)),
                Some(std::cmp::Ordering::Equal)
            );
        }

        #[test]
        fn unbalanced_cart_reconciles_the_exact_difference(
            items in prop::collection::vec(decimal(), 1..6),
            total in decimal(),
        ) {
            let cart = cart_from_cart_mandate(&mandate("USD", &items, total)).unwrap();
            let scale = cart.total.scale;
            let lines: i128 =
                cart.lines.iter().map(|line| i128::from(line.total_price.amount_minor)).sum();
            let adjustments: i128 =
                cart.adjustments.iter().map(|adjustment| i128::from(adjustment.amount.amount_minor)).sum();

            prop_assert!(cart.adjustments.iter().all(|adjustment| adjustment.amount.scale == scale));
            prop_assert_eq!(lines + adjustments, i128::from(cart.total.amount_minor));
            prop_assert_eq!(cart.adjustments.is_empty(), lines == i128::from(cart.total.amount_minor));
        }
    }
}