Skip to content
Merged
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
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

## 0.5.0

### Added

- Stripe adapter, `halvorai_ingest.stripe`, matching the Node.js and Go SDKs. `halvor.stripe(event)` maps a verified Stripe webhook event and queues it. PaymentIntent, refund and dispute events go on the `stripe` rail. Issuing authorization and transaction events go on the `card` rail. `map_stripe_event`, `stripe_webhook_handler` and `STRIPE_HANDLED_TYPES` are also exported. The adapter does not depend on the Stripe library.
- `tag_payment_intent` and `tag_issuing_card` write `metadata.halvor_*` from `agent_context` or from keyword overrides. These keys are agent id, name and framework, owner id and type, and instruction id. The name is cut to 128 characters and the framework to 64, so card payments show the agent's name. For the same webhook, the mapping gives the same event as `@halvorai/ingest/stripe`; the tests check this against the Node.js output for every shared fixture.

## 0.4.1

### Fixed
Expand Down
24 changes: 23 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,29 @@ To keep a custom transport, pass it as `inner`: `HalvorX402Transport(halvor, inn

Each 402 challenge becomes a `payment.requested` event. The payment that answers it becomes `payment.authorized`, linked to the challenge with `related.previous_event_id`, and the settlement becomes `payment.settled` or `payment.failed` with the same `payment_id`. A challenge without a network or token address is reported as `invalid_event` and not recorded.

Stripe, MPP and MCP adapters are in the Node.js SDK, [`@halvorai/ingest`](https://github.com/halvorAI/ingest-js). Guides for each: [docs.halvorai.com](https://docs.halvorai.com).
## Stripe

Pass each verified Stripe webhook event to `halvor.stripe(event)`. PaymentIntents, refunds and disputes are recorded on the `stripe` rail. Stripe Issuing authorizations and transactions are recorded on the `card` rail. Any other event type returns `None` and is ignored. The adapter does not import the Stripe library, and it accepts a plain dict or a `stripe.Event`.

```python
import stripe

from halvorai_ingest import agent_context
from halvorai_ingest.stripe import tag_issuing_card, tag_payment_intent

# Tag what the agent creates, so its webhooks map back to the agent, owner and instruction.
with agent_context(agent={"id": "agt_travel_12", "name": "Travel agent"}, owner={"id": "own_4410", "type": "business"}):
card = stripe.issuing.Card.create(**tag_issuing_card({"cardholder": "ich_...", "currency": "usd", "type": "virtual"}))
intent = stripe.PaymentIntent.create(**tag_payment_intent({"amount": 41200, "currency": "usd"}))

# In your webhook endpoint, after Stripe's own signature check:
event = stripe.Webhook.construct_event(payload, sig_header, endpoint_secret)
halvor.stripe(event)
```

The tags are `metadata.halvor_*` keys, taken from `agent_context` or from keyword overrides (`agent_id`, `agent_name`, `framework`, `owner_id`, `owner_type`, `instruction_id`). Put them on the card for Issuing: card webhooks carry no other agent name. An event without tags is recorded as `unattributed`. `map_stripe_event(event)` returns the event without queuing it. The mapping is the same as in the Node.js and Go SDKs.

MPP and MCP adapters are in the Node.js SDK, [`@halvorai/ingest`](https://github.com/halvorAI/ingest-js). Guides for each: [docs.halvorai.com](https://docs.halvorai.com).

## Develop

Expand Down
15 changes: 15 additions & 0 deletions halvorai_ingest/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,21 @@ def emit(self, input_event: Any) -> str:
self.notify({"code": "invalid_event", "message": str(exc) or "emit failed", "eventId": event_id})
return event_id

def stripe(self, event: Any) -> str | None:
"""
Map a Stripe webhook event (after your own signature check) and queue it. Returns the
event id, or `None` for a type the adapter does not handle. Never raises.
"""
try:
from .stripe.map import map_stripe_event

mapped = map_stripe_event(event)
except Exception as exc:
self._count("emitted", "dropped_invalid")
self.notify({"code": "invalid_event", "message": f"stripe: {exc}" if str(exc) else "stripe mapping failed"})
return None
return None if mapped is None else self.emit(mapped)

def flush(self, timeout: float | None = None, *, replay_spool: bool = True) -> dict[str, bool]:
"""
Send everything queued and wait for it. Without `timeout` this waits for the full retry
Expand Down
35 changes: 35 additions & 0 deletions halvorai_ingest/stripe/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
"""Stripe adapter: map Stripe and Stripe Issuing webhooks to APE events.

from halvorai_ingest.stripe import tag_issuing_card

event = stripe.Webhook.construct_event(payload, sig_header, endpoint_secret) # your check
halvor.stripe(event)

`halvor.stripe(event)` maps the event and queues it, returning the event id, or `None` for a
type the adapter does not handle. `map_stripe_event` returns the partial APE event without
queuing it. Verify the Stripe signature yourself first; the adapter trusts what it is given.
"""

from __future__ import annotations

from typing import TYPE_CHECKING, Any, Callable

from .map import STRIPE_HANDLED_TYPES, map_stripe_event
from .tag import tag_issuing_card, tag_payment_intent

if TYPE_CHECKING:
from ..client import HalvorClient


def stripe_webhook_handler(client: HalvorClient) -> Callable[[Any], str | None]:
"""Return a handler that maps Stripe events through `client`. Use after your own signature check."""
return client.stripe


__all__ = [
"STRIPE_HANDLED_TYPES",
"map_stripe_event",
"tag_payment_intent",
"tag_issuing_card",
"stripe_webhook_handler",
]
Loading
Loading