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
49 changes: 22 additions & 27 deletions ap2_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,14 @@

from absl.testing import absltest
import integration_test_utils
from ucp_sdk.models.schemas.shopping import fulfillment_resp as checkout
from ucp_sdk.models.schemas.shopping.ap2_mandate import Ap2CompleteRequest
from ucp_sdk.models.schemas.shopping.ap2_mandate import CheckoutMandate
from ucp_sdk.models.schemas.shopping.payment_resp import (
PaymentResponse as Payment,
from ucp_sdk.models.schemas.shopping import checkout as checkout
from ucp_sdk.models.schemas.shopping.payment import (
Payment,
)
from ucp_sdk.models.schemas.shopping.types import card_payment_instrument
from ucp_sdk.models.schemas.shopping.types import payment_instrument
from ucp_sdk.models.schemas.shopping.types import token_credential_resp


# Rebuild models to resolve forward references
checkout.Checkout.model_rebuild(_types_namespace={"PaymentResponse": Payment})
checkout.Checkout.model_rebuild(_types_namespace={"Payment": Payment})


class Ap2MandateTest(integration_test_utils.IntegrationTestBase):
Expand All @@ -48,31 +43,31 @@ def test_ap2_mandate_completion(self) -> None:
response_json = self.create_checkout_session()
checkout_id = checkout.Checkout(**response_json).id

credential = token_credential_resp.TokenCredentialResponse(
type="token", token="success_token"
)
instr = payment_instrument.PaymentInstrument(
root=card_payment_instrument.CardPaymentInstrument(
id="instr_1",
brand="visa",
last_digits="4242",
handler_id="mock_payment_handler",
handler_name="mock_payment_handler",
type="card",
credential=credential,
)
)
payment_data = instr.root.model_dump(mode="json", exclude_none=True)
payment_data = {
"id": "instr_1",
"brand": "visa",
"last_digits": "4242",
"handler_id": "mock_payment_handler",
"handler_name": "mock_payment_handler",
"type": "card",
"credential": {"type": "token", "token": "success_token"},
}

# SD-JWT+kb pattern:
# ^[A-Za-z0-9_-]+\.[A-Za-z0-9_-]*\.[A-Za-z0-9_-]+(~[A-Za-z0-9_-]+)*$
mandate = CheckoutMandate(root="header.payload.signature~kb_signature")
ap2_data = Ap2CompleteRequest(checkout_mandate=mandate)
#
# The UCP 01-23 SDK simplifies the AP2 protocol definitions.
# The extension payload is now defined directly against the `ap2` key.
# The `mandate` wrapper object and `ap2_data` nested objects were removed
# from the completion payload in this release to flatten the schema.

payment_payload = {
"payment_data": payment_data,
"risk_signals": {},
"ap2": ap2_data.model_dump(mode="json", exclude_none=True),
"ap2": {
**response_json,
"checkout_mandate": "header.payload.signature~kb_signature",
},
}

response = self.client.post(
Expand Down
52 changes: 20 additions & 32 deletions binding_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,14 @@

from absl.testing import absltest
import integration_test_utils
from ucp_sdk.models.schemas.shopping import fulfillment_resp as checkout
from ucp_sdk.models.schemas.shopping.payment_resp import (
PaymentResponse as Payment,
from ucp_sdk.models.schemas.shopping import checkout as checkout
from ucp_sdk.models.schemas.shopping.payment import (
Payment,
)
from ucp_sdk.models.schemas.shopping.types import binding
from ucp_sdk.models.schemas.shopping.types import card_payment_instrument
from ucp_sdk.models.schemas.shopping.types import payment_identity
from ucp_sdk.models.schemas.shopping.types import payment_instrument
from ucp_sdk.models.schemas.shopping.types import token_credential_resp


# Rebuild models to resolve forward references
checkout.Checkout.model_rebuild(_types_namespace={"PaymentResponse": Payment})
checkout.Checkout.model_rebuild(_types_namespace={"Payment": Payment})


class TokenBindingTest(integration_test_utils.IntegrationTestBase):
Expand All @@ -48,30 +43,23 @@ def test_token_binding_completion(self) -> None:
response_json = self.create_checkout_session()
checkout_id = checkout.Checkout(**response_json).id

identity = payment_identity.PaymentIdentity(
access_token="user_access_token"
)
token_binding = binding.Binding(checkout_id=checkout_id, identity=identity)

# TokenCredentialResponse allows extra fields
credential = token_credential_resp.TokenCredentialResponse(
type="stripe_token", token="success_token", binding=token_binding
)

instr = payment_instrument.PaymentInstrument(
root=card_payment_instrument.CardPaymentInstrument(
id="instr_1",
brand="visa",
last_digits="4242",
handler_id="mock_payment_handler",
handler_name="mock_payment_handler",
type="card",
credential=credential,
)
)
payment_data = instr.root.model_dump(mode="json", exclude_none=True)
payment_payload = {
"payment_data": payment_data,
"payment_data": {
"id": "instr_1",
"brand": "visa",
"last_digits": "4242",
"handler_id": "mock_payment_handler",
"handler_name": "mock_payment_handler",
"type": "card",
"credential": {
"type": "stripe_token",
"token": "success_token",
"binding": {
"checkout_id": checkout_id,
"identity": {"access_token": "user_access_token"},
},
},
},
"risk_signals": {},
}

Expand Down
59 changes: 23 additions & 36 deletions business_logic_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,22 @@

from absl.testing import absltest
import integration_test_utils
from ucp_sdk.models.schemas.shopping import buyer_consent_resp as buyer_consent
from ucp_sdk.models.schemas.shopping import checkout_update_req
from ucp_sdk.models.schemas.shopping import discount_resp as discount
from ucp_sdk.models.schemas.shopping import fulfillment_resp as checkout
from ucp_sdk.models.schemas.shopping import payment_update_req
from ucp_sdk.models.schemas.shopping.payment_resp import (
PaymentResponse as Payment,
from ucp_sdk.models.schemas.shopping import buyer_consent as buyer_consent
from ucp_sdk.models.schemas.shopping import (
checkout_update_request as checkout_update_req,
)
from ucp_sdk.models.schemas.shopping.types import buyer
from ucp_sdk.models.schemas.shopping.types import item_update_req
from ucp_sdk.models.schemas.shopping.types import line_item_update_req
from ucp_sdk.models.schemas.shopping import discount as discount
from ucp_sdk.models.schemas.shopping import checkout as checkout
from ucp_sdk.models.schemas.shopping import payment_update_request
from ucp_sdk.models.schemas.shopping.payment import (
Payment,
)
from ucp_sdk.models.schemas.shopping.types import buyer_update_request
from ucp_sdk.models.schemas.shopping.types import item_update_request
from ucp_sdk.models.schemas.shopping.types import line_item_update_request

# Rebuild models to resolve forward references
checkout.Checkout.model_rebuild(_types_namespace={"PaymentResponse": Payment})
checkout.Checkout.model_rebuild(_types_namespace={"Payment": Payment})


class BusinessLogicTest(integration_test_utils.IntegrationTestBase):
Expand Down Expand Up @@ -129,22 +131,17 @@ def test_totals_recalculation_on_update(self):
expected_price = int(expected_price)

# Update quantity to 2. Total should be 2 * expected_price.
item_update = item_update_req.ItemUpdateRequest(
item_update = item_update_request.ItemUpdateRequest(
id=checkout_obj.line_items[0].item.id,
title=checkout_obj.line_items[0].item.title,
)
line_item_update = line_item_update_req.LineItemUpdateRequest(
line_item_update = line_item_update_request.LineItemUpdateRequest(
id=checkout_obj.line_items[0].id,
item=item_update,
quantity=2,
)
payment_update = payment_update_req.PaymentUpdateRequest(
selected_instrument_id=checkout_obj.payment.selected_instrument_id,
payment_update = payment_update_request.PaymentUpdateRequest(
instruments=checkout_obj.payment.instruments,
handlers=[
h.model_dump(mode="json", exclude_none=True)
for h in checkout_obj.payment.handlers
],
)

update_payload = checkout_update_req.CheckoutUpdateRequest(
Expand Down Expand Up @@ -198,22 +195,17 @@ def test_discount_flow(self):
expected_price = int(expected_price)

# Apply Discount
item_update = item_update_req.ItemUpdateRequest(
item_update = item_update_request.ItemUpdateRequest(
id=checkout_obj.line_items[0].item.id,
title=checkout_obj.line_items[0].item.title,
)
line_item_update = line_item_update_req.LineItemUpdateRequest(
line_item_update = line_item_update_request.LineItemUpdateRequest(
id=checkout_obj.line_items[0].id,
item=item_update,
quantity=1,
)
payment_update = payment_update_req.PaymentUpdateRequest(
selected_instrument_id=checkout_obj.payment.selected_instrument_id,
payment_update = payment_update_request.PaymentUpdateRequest(
instruments=checkout_obj.payment.instruments,
handlers=[
h.model_dump(mode="json", exclude_none=True)
for h in checkout_obj.payment.handlers
],
)

update_payload = checkout_update_req.CheckoutUpdateRequest(
Expand Down Expand Up @@ -492,30 +484,25 @@ def test_buyer_info_persistence(self):
checkout_id = checkout_obj.id

# Update with buyer info
item_update = item_update_req.ItemUpdateRequest(
item_update = item_update_request.ItemUpdateRequest(
id=checkout_obj.line_items[0].item.id,
title=checkout_obj.line_items[0].item.title,
)
line_item_update = line_item_update_req.LineItemUpdateRequest(
line_item_update = line_item_update_request.LineItemUpdateRequest(
id=checkout_obj.line_items[0].id,
item=item_update,
quantity=1,
)
payment_update = payment_update_req.PaymentUpdateRequest(
selected_instrument_id=checkout_obj.payment.selected_instrument_id,
payment_update = payment_update_request.PaymentUpdateRequest(
instruments=checkout_obj.payment.instruments,
handlers=[
h.model_dump(mode="json", exclude_none=True)
for h in checkout_obj.payment.handlers
],
)

update_payload = checkout_update_req.CheckoutUpdateRequest(
id=checkout_id,
currency=checkout_obj.currency,
line_items=[line_item_update],
payment=payment_update,
buyer=buyer.Buyer(
buyer=buyer_update_request.BuyerUpdateRequest(
email="test@example.com",
first_name="Test",
last_name="User",
Expand Down
46 changes: 21 additions & 25 deletions card_credential_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,14 @@

from absl.testing import absltest
import integration_test_utils
from ucp_sdk.models.schemas.shopping import fulfillment_resp as checkout
from ucp_sdk.models.schemas.shopping.payment_resp import (
PaymentResponse as Payment,
from ucp_sdk.models.schemas.shopping import checkout as checkout
from ucp_sdk.models.schemas.shopping.payment import (
Payment,
)
from ucp_sdk.models.schemas.shopping.types import card_credential
from ucp_sdk.models.schemas.shopping.types import card_payment_instrument


# Rebuild models to resolve forward references
checkout.Checkout.model_rebuild(_types_namespace={"PaymentResponse": Payment})
checkout.Checkout.model_rebuild(_types_namespace={"Payment": Payment})


class CardCredentialTest(integration_test_utils.IntegrationTestBase):
Expand All @@ -45,25 +43,23 @@ def test_card_credential_payment(self) -> None:
response_json = self.create_checkout_session()
checkout_id = checkout.Checkout(**response_json).id

credential = card_credential.CardCredential(
type="card",
card_number_type="fpan",
number="4242424242424242",
expiry_month=12,
expiry_year=2030,
cvc="123",
name="John Doe",
)
instr = card_payment_instrument.CardPaymentInstrument(
id="instr_card",
handler_id="mock_payment_handler",
handler_name="mock_payment_handler",
type="card",
brand="Visa",
last_digits="1111",
credential=credential,
)
payment_data = instr.model_dump(mode="json", exclude_none=True)
payment_data = {
"id": "instr_card",
"handler_id": "mock_payment_handler",
"handler_name": "mock_payment_handler",
"type": "card",
"brand": "Visa",
"last_digits": "1111",
"credential": {
"type": "card",
"card_number_type": "fpan",
"number": "4242424242424242",
"expiry_month": 12,
"expiry_year": 2030,
"cvc": "123",
"name": "John Doe",
},
}
payment_payload = {
"payment_data": payment_data,
"risk_signals": {},
Expand Down
Loading
Loading