---
title: "Recording pipeline — Purchases for Laravel | Roundly"
description: "How handle(), sync() and replay() turn a result into a Purchase, Subscription or PurchaseRefund — audited, idempotent, events only on change."
url: https://roundly-consulting.com/open-source/docs/purchases-for-laravel/recording
language: en
---

[All packages](https://roundly-consulting.com/open-source.md)

[Purchases for Laravel](https://roundly-consulting.com/open-source/docs/purchases-for-laravel.md)

# Recording pipeline

handle(), sync(), replay() and the queued job share one recording core. It routes a result by type() — refunds become a PurchaseRefund, purchases a Purchase, subscriptions a Subscription — and dispatches the matching events. A Notification or Unknown result is informational: recorded nowhere, it fires nothing and can never overwrite a subscription’s status. What differs is the audit row around it:

| Entry | Audit row | Recording | Returns |
| --- | --- | --- | --- |
| `handle()` | Written first (origin provider, signature verified) | Now, or on the queue when queue.enabled | The record — or the audit notification when queued or informational |
| `sync()` | Written first (origin host, signature\_verified false) | Always synchronous | The record, or null for an informational result |
| `replay()` | None written — the stored row is marked processed | Synchronous, ordered by event time | The record, or null for an informational notification |

## Idempotent by design

Every record is keyed on the unique (provider, provider\_id) pair, so a store that redelivers a notification updates the same row instead of duplicating it. Line items are re-synced whenever the result carries any; a null price leaves the stored price untouched; the raw payload is kept in the model’s meta column. A new subscription without a name from the store is named after the product id, then the provider id; a later event without a name leaves the stored one alone.

## Ordered by event time

Recording is ordered by occurredAt(). Each purchase, subscription and refund stores the time of the latest event applied to it (last\_event\_at): an older event — a redelivery, an out-of-order delivery, a replay — changes nothing and fires nothing, and a refunded row stays refunded unless an event provably newer than the refund reverses it (Apple’s REFUND\_REVERSED, a won Stripe dispute). A result that cannot say when it happened (occurredAt() null — a GenericResult you built without one) is applied as before, but never un-refunds anything. The row is locked while this is decided, so two deliveries racing each other cannot interleave. A row you soft-deleted is still kept up to date by later notifications (it stays deleted), but fires no lifecycle event.

## Concurrent first deliveries

The recording transaction runs up to three attempts. On MySQL, locking a key that doesn’t exist yet takes a gap lock, so two first deliveries of the same purchase, subscription or refund can deadlock (error 1213) on their inserts; the loser is retried and finds the winner’s row instead of failing its webhook or queued job.

## Dates and the app timezone

Every store reports its dates in UTC, while Eloquent stores the wall-clock time of the instance it is handed. So every provider date — active\_from, trial\_ends\_at, ends\_at, refunded\_at and the event time — is moved into the application timezone before it is stored: on a host whose app.timezone isn’t UTC, a subscription still expires at its real instant.

## Recording a result you obtained yourself

Use Purchases::sync() — or its action, SyncProviderResultAction — for a result that didn’t arrive as a webhook; see The Purchases facade and DI and actions. The Record\* actions behind the core are @internal — they skip the audit row that sync() writes and marks processed.

## Show your open-source love

This package is free and MIT-licensed. If it saves you time, a one-off donation or a Patreon membership keeps it maintained, tested and documented.

[More ways to support, including crypto](https://roundly-consulting.com/support-us.md)

By donating, you agree to our [donation terms](https://roundly-consulting.com/donation-terms.md).

[Support our open source work (opens in a new tab)](https://donate.stripe.com/dRmeVe8FX5PF1Qd9pXcEw00) [Join us on Patreon (opens in a new tab)](https://www.patreon.com/cw/roundly)

## Want this built into your product?

We integrate our packages into custom Laravel and AI builds. Tell us what you're working on and we'll reply within 48 hours.

[Get a quote in 48 hours](https://roundly-consulting.com/contact.md) [Browse all packages](https://roundly-consulting.com/open-source/docs/purchases-for-laravel.md)
