fix(ledger): refund rows take the buyer of their charge

The importer expands only the refund, which names no customer, so refunds
were stored without customer_id/email and never netted against the buyer in
customerLedgerTotals / ledgerAmountByCustomer. The row builder now copies the
buyer from the ledger's charge row of the same payment (payment intent or
charge id). Fills nulls only; a later import heals existing rows.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Fabian @ Blax Software 2026-11-14 11:17:15 +01:00
parent 159502de52
commit 193d0615e6
2 changed files with 74 additions and 0 deletions

View File

@ -1227,6 +1227,16 @@ class ShopService
$meta['payment_intent'] = $paymentIntent; $meta['payment_intent'] = $paymentIntent;
} }
// The importer expands only the refund, which names no customer: take
// the buyer of the same payment's charge row, so the refund nets
// against them in the per-customer totals.
if (! is_string($customerId) && ! is_string($customerEmail)) {
[$customerId, $customerEmail] = $this->ledgerBuyerOfPayment(
$meta['payment_intent'] ?? null,
is_string($source->charge ?? null) ? $source->charge : null,
);
}
if ($external = $this->externallySettledAmount($txn, $source)) { if ($external = $this->externallySettledAmount($txn, $source)) {
$meta['settled_outside_stripe'] = true; $meta['settled_outside_stripe'] = true;
$meta['stripe_amount'] = $amount; $meta['stripe_amount'] = $amount;
@ -1290,6 +1300,39 @@ class ShopService
return [$source, is_string($sourceId) ? $sourceId : null]; return [$source, is_string($sourceId) ? $sourceId : null];
} }
/**
* The buyer the ledger already holds for a payment: its charge row, found
* by payment intent or charge id. [null, null] when that row is not
* recorded yet or names no buyer either.
*
* @return array{0: string|null, 1: string|null}
*/
protected function ledgerBuyerOfPayment(?string $paymentIntent, ?string $chargeId): array
{
if (! $paymentIntent && ! $chargeId) {
return [null, null];
}
try {
$row = $this->ledgerModel()::query()
->whereIn('source_type', ['charge', 'payment'])
->where(fn ($q) => $q->whereNotNull('customer_id')->orWhereNotNull('customer_email'))
->where(function ($q) use ($paymentIntent, $chargeId) {
if ($paymentIntent) {
$q->orWhere('meta->payment_intent', $paymentIntent);
}
if ($chargeId) {
$q->orWhere('source_id', $chargeId);
}
})
->first(['customer_id', 'customer_email']);
} catch (\Throwable $e) {
return [null, null];
}
return [$row?->customer_id, $row?->customer_email];
}
/** /**
* Signed gross for a payment or refund that settled outside the Stripe * Signed gross for a payment or refund that settled outside the Stripe
* balance, or null when Stripe's own `amount` already holds it. * balance, or null when Stripe's own `amount` already holds it.

View File

@ -254,6 +254,37 @@ class LedgerMetricsTest extends TestCase
$this->assertSame(1540, Shop::customerLedgerTotals('cus_Q')['amount']); $this->assertSame(1540, Shop::customerLedgerTotals('cus_Q')['amount']);
} }
#[Test]
public function imported_refund_without_a_customer_takes_the_buyer_of_its_charge(): void
{
// The charge row knows the buyer; the importer expands only the refund,
// which carries the payment intent and charge id but no customer (prod
// shape of the Nov 2025 PayPal refunds).
$this->txn(['amount' => 1900, 'fee' => 57, 'net' => 1843, 'source_id' => 'py_r1', 'customer_id' => 'cus_R', 'meta' => ['payment_intent' => 'pi_r1']]);
$this->txn(['amount' => 280, 'fee' => 8, 'net' => 272, 'source_id' => 'ch_r2', 'customer_email' => 'r2@x.io']);
Shop::recordBalanceTransaction((object) [
'id' => 'txn_r1', 'type' => 'payment_refund', 'amount' => -1900, 'fee' => 0, 'net' => -1900, 'currency' => 'eur', 'created' => 1730000000,
'source' => (object) ['id' => 'pyr_r1', 'object' => 'refund', 'charge' => 'py_r1', 'payment_intent' => 'pi_r1'],
]);
Shop::recordBalanceTransaction((object) [
'id' => 'txn_r2', 'type' => 'refund', 'amount' => -280, 'fee' => 0, 'net' => -280, 'currency' => 'eur', 'created' => 1730000000,
'source' => (object) ['id' => 're_r2', 'object' => 'refund', 'charge' => 'ch_r2'],
]);
// No charge row to learn from: the refund stays without a buyer.
Shop::recordBalanceTransaction((object) [
'id' => 'txn_r3', 'type' => 'refund', 'amount' => -500, 'fee' => 0, 'net' => -500, 'currency' => 'eur', 'created' => 1730000000,
'source' => (object) ['id' => 're_r3', 'object' => 'refund', 'charge' => 'ch_unknown', 'payment_intent' => 'pi_unknown'],
]);
$this->assertSame('cus_R', StripeTransaction::where('stripe_id', 'txn_r1')->value('customer_id'));
$this->assertSame(0, Shop::customerLedgerTotals('cus_R')['amount']);
$this->assertSame('r2@x.io', StripeTransaction::where('stripe_id', 'txn_r2')->value('customer_email'));
$this->assertSame(0, Shop::customerLedgerTotals([], 'r2@x.io')['amount']);
$this->assertNull(StripeTransaction::where('stripe_id', 'txn_r3')->value('customer_id'));
$this->assertNull(StripeTransaction::where('stripe_id', 'txn_r3')->value('customer_email'));
}
#[Test] #[Test]
public function zero_amount_rows_without_a_paid_charge_stay_zero(): void public function zero_amount_rows_without_a_paid_charge_stay_zero(): void
{ {