diff --git a/src/Services/ShopService.php b/src/Services/ShopService.php index 32150ca..7c0f43d 100644 --- a/src/Services/ShopService.php +++ b/src/Services/ShopService.php @@ -1227,6 +1227,16 @@ class ShopService $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)) { $meta['settled_outside_stripe'] = true; $meta['stripe_amount'] = $amount; @@ -1290,6 +1300,39 @@ class ShopService 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 * balance, or null when Stripe's own `amount` already holds it. diff --git a/tests/Feature/Ledger/LedgerMetricsTest.php b/tests/Feature/Ledger/LedgerMetricsTest.php index 623ce77..1f4aeae 100644 --- a/tests/Feature/Ledger/LedgerMetricsTest.php +++ b/tests/Feature/Ledger/LedgerMetricsTest.php @@ -254,6 +254,37 @@ class LedgerMetricsTest extends TestCase $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] public function zero_amount_rows_without_a_paid_charge_stay_zero(): void {