HEX
Server: LiteSpeed
System: Linux s1049.use1.mysecurecloudhost.com 4.18.0-477.27.2.lve.el8.x86_64 #1 SMP Wed Oct 11 12:32:56 UTC 2023 x86_64
User: xedaptot (3356)
PHP: 8.3.31
Disabled: NONE
Upload Files
File: /home/xedaptot/ai.naniguide.com/app/Notifications/Admin/RemoteInvoicePaid.php
<?php

namespace App\Notifications\Admin;

use App\Model\Customer;
use App\Model\Invoice;
use App\Model\PaymentGateway;
use App\Notifications\AppNotification;
use Illuminate\Database\Eloquent\Model;

/**
 * A remote-managed subscription (Stripe Subscription, Paddle, ...) auto-billed
 * a recurring invoice and the local sync layer materialized it.
 *
 * Rationale for surfacing to admins:
 *   - Renewal revenue from remote gateways doesn't touch the local cron path
 *     (createRenewOrders skips remote subs), so without this notification
 *     admins have no in-app visibility of monthly recurring income.
 *   - Group key = remote_invoice_id → idempotent: re-running sync that re-detects
 *     the same vendor event collapses into the existing row.
 *   - Severity=success, category=info — informational only, no action required.
 *
 * Body identifies customer + gateway + amount + currency in plain text so the
 * bell preview answers "who paid what via which gateway" without a click.
 */
class RemoteInvoicePaid extends AppNotification
{
    public function __construct(
        public Invoice $invoice,
        public Customer $customer,
        public PaymentGateway $gateway,
        public float $amount,
        public string $currency,
    ) {}

    public function audience(): string { return self::AUDIENCE_ADMINS; }
    public function category(): string { return self::CATEGORY_INFO; }
    public function severity(): string { return self::SEVERITY_SUCCESS; }

    /** 1 row per vendor billing event — re-sync collapses, never spams. */
    public function groupKey(): string
    {
        return 'remote-invoice-paid:' . ($this->invoice->remote_invoice_id ?: $this->invoice->id);
    }

    public function subject(): ?Model
    {
        return $this->invoice;
    }

    public function toArray(object $notifiable = null): array
    {
        $customerName = $this->customer->displayName() ?? $this->customer->name ?? 'Customer';
        $amountStr    = number_format($this->amount, 2) . ' ' . $this->currency;

        return [
            'title' => 'Recurring payment received',
            'body'  => sprintf(
                '%s — %s paid via %s.',
                $customerName,
                $amountStr,
                $this->gateway->name
            ),
            'meta' => [
                'remote_invoice_id' => $this->invoice->remote_invoice_id,
                'gateway_id'        => $this->gateway->id,
                'gateway_name'      => $this->gateway->name,
                'gateway_type'      => $this->gateway->type,
                'customer_id'       => $this->customer->id,
                'customer_name'     => $customerName,
                'invoice_uid'       => $this->invoice->uid,
                'amount'            => $this->amount,
                'currency'          => $this->currency,
            ],
            'action' => [
                'label'  => 'View invoice',
                'route'  => 'refactor.admin.invoices.index',
                'params' => [],
            ],
        ];
    }
}