Fix: Adding check for duplicate transaction
This commit is contained in:
@@ -39,8 +39,26 @@ class TransactionRecorder
|
||||
* elsewhere in this codebase (see the old, now-removed
|
||||
* TransactionRecorder this replaces).
|
||||
*/
|
||||
/**
|
||||
* Idempotent on (order_id, type, reference): a successful payment
|
||||
* outcome can legitimately be reported twice for the same gateway
|
||||
* reference — e.g. Stripe's pay()/handleCallback() both call
|
||||
* resultFromIntent() and both dispatch PaymentCaptured once a
|
||||
* PaymentIntent reaches "succeeded" (checkout's synchronous capture,
|
||||
* then the webhook confirming the same outcome asynchronously) — so
|
||||
* this returns the existing row instead of writing a duplicate.
|
||||
*/
|
||||
public function record(Order $order, string $type, string $driver, PaymentResult $result): Transaction
|
||||
{
|
||||
$existing = $order->transactions()
|
||||
->where('type', $type)
|
||||
->where('reference', $result->reference)
|
||||
->first();
|
||||
|
||||
if ($existing !== null) {
|
||||
return $existing;
|
||||
}
|
||||
|
||||
return $order->transactions()->create([
|
||||
'success' => $result->status === PaymentResultStatus::Succeeded,
|
||||
'type' => $type,
|
||||
|
||||
Reference in New Issue
Block a user