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
5 changes: 5 additions & 0 deletions .changelog/preserve-receipt-subscription-id.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
pympp: patch
---

Preserve `subscriptionId` when parsing and formatting payment receipts.
3 changes: 3 additions & 0 deletions src/mpp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,7 @@ class Receipt:
reference: str
method: str = "tempo"
external_id: str | None = None
subscription_id: str | None = None
extra: dict[str, Any] | None = None

@classmethod
Expand All @@ -398,6 +399,7 @@ def success(
timestamp: datetime | None = None,
method: str = "tempo",
external_id: str | None = None,
subscription_id: str | None = None,
) -> Receipt:
"""Create a success receipt with current timestamp."""
return cls(
Expand All @@ -406,6 +408,7 @@ def success(
reference=reference,
method=method,
external_id=external_id,
subscription_id=subscription_id,
)


Expand Down
3 changes: 3 additions & 0 deletions src/mpp/_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,7 @@ def parse_payment_receipt(header: str) -> Receipt:
reference=str(data["reference"]),
method=method,
external_id=str(data["externalId"]) if data.get("externalId") else None,
subscription_id=str(data["subscriptionId"]) if data.get("subscriptionId") else None,
extra=extra if isinstance(extra, dict) else None,
)

Expand All @@ -345,6 +346,8 @@ def format_payment_receipt(receipt: Receipt) -> str:
payload["method"] = receipt.method
if receipt.external_id:
payload["externalId"] = receipt.external_id
if receipt.subscription_id:
payload["subscriptionId"] = receipt.subscription_id
if receipt.extra:
payload["extra"] = receipt.extra
return _b64_encode(payload)
18 changes: 18 additions & 0 deletions tests/test_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,7 @@ def test_roundtrip_with_optional_fields(self) -> None:
method="tempo",
external_id="order-123",
extra={"plan": "pro"},
subscription_id="sub_123",
)

header = receipt.to_payment_receipt()
Expand All @@ -346,6 +347,23 @@ def test_roundtrip_with_optional_fields(self) -> None:
assert parsed.method == "tempo"
assert parsed.external_id == "order-123"
assert parsed.extra == {"plan": "pro"}
assert parsed.subscription_id == "sub_123"

def test_parse_preserves_foreign_subscription_id(self) -> None:
payload = {
"status": "success",
"method": "tempo",
"timestamp": "2024-01-20T12:00:00Z",
"reference": "0xabc123def456",
"subscriptionId": "sub_123",
}
header = base64.urlsafe_b64encode(json.dumps(payload).encode()).decode().rstrip("=")

parsed = Receipt.from_payment_receipt(header)

assert parsed.subscription_id == "sub_123"
roundtripped = Receipt.from_payment_receipt(parsed.to_payment_receipt())
assert roundtripped.subscription_id == "sub_123"

def test_parse_invalid_timestamp(self) -> None:
payload = {
Expand Down