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
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Breaking

- `events.refund()` takes a required keyword `refund_id`, the store's own id
for the refund, such as WooCommerce refund 2201, and sends it as `refundId`.
Siren now refuses a refund without one with 400. Siren records each refund
id once per order, so sending the same refund again is safe.

## [0.2.0] - 2026-10-19

This is a minor bump, because the changes below break existing callers. Each
Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ client.events.sale(
Refunds and referred visits:

```python
client.events.refund(source="stripe", external_id="cs_test_a1b2c3")
# external_id names the order, refund_id names this refund (your store's id for it).
# Siren requires refund_id and records each refund id once per order.
client.events.refund(source="stripe", external_id="cs_test_a1b2c3", refund_id="re_3Nf8a2")
client.events.site_visited(collaborator_id=88, user_id=12345)

# Custom event types registered on your org:
Expand Down Expand Up @@ -272,7 +274,7 @@ Webhook verification failures raise `SignatureVerificationError`.

```python
try:
client.events.refund(source="stripe", external_id="unknown")
client.events.refund(source="stripe", external_id="unknown", refund_id="re_1")
except siren.NotFoundError:
print("no matching sale")
except siren.RateLimitError as err:
Expand Down
8 changes: 7 additions & 1 deletion openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -730,10 +730,16 @@ components:
example: 49.99
RefundPayload:
type: object
required: [source, externalId]
required: [source, externalId, refundId]
properties:
source: { type: string, minLength: 1, maxLength: 249, example: stripe }
externalId: { type: [string, number], minLength: 1, maxLength: 32, example: cs_test_a1b2c3 }
refundId:
description: The store's own id for this refund, such as WooCommerce refund 2201. `externalId` names the order and `refundId` names one refund of it. Siren records each refund id once per order.
type: [string, number]
minLength: 1
maxLength: 191
example: re_3Nf8a2
SiteVisitedPayload:
type: object
required: [collaboratorId]
Expand Down
20 changes: 14 additions & 6 deletions src/siren/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,24 @@ def sale(
payload["items"] = [_normalize_line_item(item) for item in items]
return self._post("sale", payload)

def refund(self, *, source: str, external_id: Union[str, int]) -> EventResult:
"""Reverse a previously recorded sale (``POST /event/refund``).

The sale is matched by ``(external_id, source)``; both must match the
original sale exactly. Raises :class:`siren.NotFoundError` when no
def refund(
self, *, source: str, external_id: Union[str, int], refund_id: Union[str, int]
) -> EventResult:
"""Record a refund of a previously recorded sale (``POST /event/refund``).

The sale is matched by ``(external_id, source)``, and both must match
the original sale exactly. ``refund_id`` is the store's own id for
this refund, such as WooCommerce refund 2201. ``external_id`` names
the order and ``refund_id`` names one refund of it. Siren requires it
and records each refund id once per order, so sending the same refund
again is safe. Raises :class:`siren.NotFoundError` when no
matching sale transaction exists, unless the organization's processing
is paused. Then Siren queues the refund (``result.queued``) and checks
the sale when it replays the queue.
"""
return self._post("refund", {"source": source, "externalId": external_id})
return self._post(
"refund", {"source": source, "externalId": external_id, "refundId": refund_id}
)

def site_visited(
self, *, collaborator_id: int, user_id: Optional[int] = None
Expand Down
18 changes: 14 additions & 4 deletions tests/test_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,22 @@ def test_items_omitted_from_payload_when_not_given(self, client, mocked_api):


class TestRefund:
def test_refund_id_is_required(self, client):
with pytest.raises(TypeError):
client.events.refund(source="stripe", external_id="cs_test_a1b2c3") # type: ignore[call-arg]

def test_payload_mapping(self, client, mocked_api):
route = mocked_api.post("/event/refund").respond(200, headers={"X-Siren-OID": "8"})

result = client.events.refund(source="stripe", external_id="cs_test_a1b2c3")
result = client.events.refund(
source="stripe", external_id="cs_test_a1b2c3", refund_id="re_3Nf8a2"
)

assert _body(route) == {"source": "stripe", "externalId": "cs_test_a1b2c3"}
assert _body(route) == {
"source": "stripe",
"externalId": "cs_test_a1b2c3",
"refundId": "re_3Nf8a2",
}
assert result.opportunity_id == 8

def test_unknown_sale_raises_not_found(self, client, mocked_api):
Expand All @@ -135,7 +145,7 @@ def test_unknown_sale_raises_not_found(self, client, mocked_api):
)

with pytest.raises(siren.NotFoundError) as excinfo:
client.events.refund(source="stripe", external_id="nope")
client.events.refund(source="stripe", external_id="nope", refund_id="re_1")

assert excinfo.value.status_code == 404
assert excinfo.value.code == "not_found"
Expand Down Expand Up @@ -242,7 +252,7 @@ def test_queued_event_is_reported(self, client, mocked_api):
def test_empty_body_gives_empty_dict(self, client, mocked_api):
mocked_api.post("/event/refund").respond(200)

result = client.events.refund(source="woo", external_id="1")
result = client.events.refund(source="woo", external_id="1", refund_id="2201")

assert result.body == {}
assert result.status is None
Expand Down
Loading