feat: add Discord admin feed for daily stats and Stripe events (#458)
## What Adds a private Discord admin feed with two flows, both posting through one webhook (`DISCORD_WEBHOOK_URL`): ### 1. Daily stats — `stats:daily-report` Scheduled **09:00 Europe/Madrid**. Posts an embed with: - New users created **yesterday** (Madrid calendar day, converted to UTC for the query) - Total users - Active/trialing counts + current & projected **MRR / ARR** per currency Reuses the #457 Stripe stats logic, extracted into `SubscriptionStatsCollector` so the existing `stripe:subscription-stats` command and this report share one source of truth. ### 2. Stripe events — `PostStripeEventToDiscord` Queued listener on Cashier's `WebhookReceived`. Posts on: - `customer.subscription.created` / `updated` / `deleted` - `invoice.payment_succeeded` / `payment_failed` ## Setup - `DISCORD_WEBHOOK_URL` — added to prod ✅ (set locally to test) - Stripe dashboard must send the 5 event types above to the existing Cashier `/stripe/webhook` endpoint - A queue worker must be running (listener is `ShouldQueue`) ## Tests 13 passing: Discord client (3), daily report (2), Stripe event listener (4), plus the existing stats command (4) still green after the refactor.
This commit is contained in:
parent
670a0a65c7
commit
0b528b7902
|
|
@ -0,0 +1,117 @@
|
|||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use App\Models\User;
|
||||
use App\Services\Discord\DiscordWebhook;
|
||||
use App\Services\Stripe\SubscriptionStatsCollector;
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Stripe\Exception\ApiErrorException;
|
||||
|
||||
class SendDailyStatsReportCommand extends Command
|
||||
{
|
||||
protected $signature = 'stats:daily-report';
|
||||
|
||||
protected $description = 'Post yesterday\'s user and Stripe subscription stats to the Discord admin channel';
|
||||
|
||||
private const TIMEZONE = 'Europe/Madrid';
|
||||
|
||||
public function __construct(
|
||||
private SubscriptionStatsCollector $collector,
|
||||
private DiscordWebhook $discord,
|
||||
) {
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
public function handle(): int
|
||||
{
|
||||
try {
|
||||
$stats = $this->collector->collect();
|
||||
} catch (ApiErrorException $exception) {
|
||||
$this->error("Stripe API error: {$exception->getMessage()}");
|
||||
|
||||
return self::FAILURE;
|
||||
}
|
||||
|
||||
$yesterdayStart = Carbon::now(self::TIMEZONE)->subDay()->startOfDay();
|
||||
$todayStart = Carbon::now(self::TIMEZONE)->startOfDay();
|
||||
|
||||
$newUsers = User::query()
|
||||
->whereBetween('created_at', [$yesterdayStart->copy()->utc(), $todayStart->copy()->utc()])
|
||||
->count();
|
||||
|
||||
$this->discord->send('', [
|
||||
$this->buildEmbed($stats, $newUsers, User::query()->count(), $yesterdayStart),
|
||||
]);
|
||||
|
||||
$this->info('Daily stats report sent to Discord.');
|
||||
|
||||
return self::SUCCESS;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array{active: array<string, array{count: int, mrr: float}>, trialing: array<string, array{count: int, mrr: float}>} $stats
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
private function buildEmbed(array $stats, int $newUsers, int $totalUsers, Carbon $day): array
|
||||
{
|
||||
$fields = [
|
||||
[
|
||||
'name' => '👥 Users',
|
||||
'value' => "New yesterday: **{$newUsers}**\nTotal: **{$totalUsers}**",
|
||||
'inline' => false,
|
||||
],
|
||||
];
|
||||
|
||||
$currencies = array_unique(array_merge(array_keys($stats['active']), array_keys($stats['trialing'])));
|
||||
sort($currencies);
|
||||
|
||||
foreach ($currencies as $currency) {
|
||||
$active = $stats['active'][$currency] ?? ['count' => 0, 'mrr' => 0.0];
|
||||
$trialing = $stats['trialing'][$currency] ?? ['count' => 0, 'mrr' => 0.0];
|
||||
|
||||
$currentMrr = $active['mrr'];
|
||||
$projectedMrr = $active['mrr'] + $trialing['mrr'];
|
||||
|
||||
$fields[] = [
|
||||
'name' => '💳 '.strtoupper($currency),
|
||||
'value' => implode("\n", [
|
||||
"Active: **{$active['count']}** ({$this->money($currentMrr, $currency)} MRR)",
|
||||
"Trialing: **{$trialing['count']}** ({$this->money($trialing['mrr'], $currency)} MRR)",
|
||||
"Current MRR/ARR: **{$this->money($currentMrr, $currency)}** / **{$this->money($currentMrr * 12, $currency)}**",
|
||||
"Projected MRR/ARR: **{$this->money($projectedMrr, $currency)}** / **{$this->money($projectedMrr * 12, $currency)}**",
|
||||
]),
|
||||
'inline' => false,
|
||||
];
|
||||
}
|
||||
|
||||
if ($currencies === []) {
|
||||
$fields[] = [
|
||||
'name' => '💳 Subscriptions',
|
||||
'value' => 'No active or trialing subscriptions.',
|
||||
'inline' => false,
|
||||
];
|
||||
}
|
||||
|
||||
return [
|
||||
'title' => '📊 Daily Stats — '.$day->format('D, d M Y'),
|
||||
'color' => 0x5865F2,
|
||||
'fields' => $fields,
|
||||
];
|
||||
}
|
||||
|
||||
private function money(float $amount, string $currency): string
|
||||
{
|
||||
$symbol = match (strtolower($currency)) {
|
||||
'eur' => '€',
|
||||
'gbp' => '£',
|
||||
'usd' => '$',
|
||||
'jpy' => '¥',
|
||||
'brl' => 'R$',
|
||||
default => strtoupper($currency).' ',
|
||||
};
|
||||
|
||||
return $symbol.number_format($amount, 2);
|
||||
}
|
||||
}
|
||||
|
|
@ -2,10 +2,9 @@
|
|||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use App\Services\Stripe\SubscriptionStatsCollector;
|
||||
use Illuminate\Console\Command;
|
||||
use Laravel\Cashier\Cashier;
|
||||
use Stripe\Exception\ApiErrorException;
|
||||
use Stripe\Subscription;
|
||||
|
||||
class StripeSubscriptionStatsCommand extends Command
|
||||
{
|
||||
|
|
@ -23,74 +22,29 @@ class StripeSubscriptionStatsCommand extends Command
|
|||
*/
|
||||
private array $trialing = [];
|
||||
|
||||
public function __construct(private SubscriptionStatsCollector $collector)
|
||||
{
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
public function handle(): int
|
||||
{
|
||||
try {
|
||||
$this->collect('active', $this->active);
|
||||
$this->collect('trialing', $this->trialing);
|
||||
$stats = $this->collector->collect();
|
||||
} catch (ApiErrorException $exception) {
|
||||
$this->error("Stripe API error: {$exception->getMessage()}");
|
||||
|
||||
return self::FAILURE;
|
||||
}
|
||||
|
||||
$this->active = $stats['active'];
|
||||
$this->trialing = $stats['trialing'];
|
||||
|
||||
$this->render();
|
||||
|
||||
return self::SUCCESS;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, array{count: int, mrr: float}> $bucket
|
||||
*
|
||||
* @throws ApiErrorException
|
||||
*/
|
||||
private function collect(string $status, array &$bucket): void
|
||||
{
|
||||
$subscriptions = Cashier::stripe()->subscriptions->all([
|
||||
'status' => $status,
|
||||
'limit' => 100,
|
||||
'expand' => ['data.items.data.price'],
|
||||
]);
|
||||
|
||||
/** @var Subscription $subscription */
|
||||
foreach ($subscriptions->autoPagingIterator() as $subscription) {
|
||||
$currency = strtolower((string) $subscription->currency);
|
||||
|
||||
$bucket[$currency] ??= ['count' => 0, 'mrr' => 0.0];
|
||||
$bucket[$currency]['count']++;
|
||||
$bucket[$currency]['mrr'] += $this->monthlyValue($subscription);
|
||||
}
|
||||
}
|
||||
|
||||
private function monthlyValue(Subscription $subscription): float
|
||||
{
|
||||
$monthly = 0.0;
|
||||
|
||||
foreach ($subscription->items->data as $item) {
|
||||
$price = $item->price;
|
||||
|
||||
if ($price->recurring === null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$amount = ($price->unit_amount ?? 0) / 100;
|
||||
$quantity = $item->quantity ?? 1;
|
||||
$intervalCount = $price->recurring->interval_count ?: 1;
|
||||
|
||||
$perMonth = match ($price->recurring->interval) {
|
||||
'day' => $amount * 365 / 12,
|
||||
'week' => $amount * 52 / 12,
|
||||
'month' => $amount,
|
||||
'year' => $amount / 12,
|
||||
default => 0.0,
|
||||
};
|
||||
|
||||
$monthly += ($perMonth / $intervalCount) * $quantity;
|
||||
}
|
||||
|
||||
return $monthly;
|
||||
}
|
||||
|
||||
private function render(): void
|
||||
{
|
||||
$currencies = array_unique(array_merge(array_keys($this->active), array_keys($this->trialing)));
|
||||
|
|
|
|||
|
|
@ -0,0 +1,105 @@
|
|||
<?php
|
||||
|
||||
namespace App\Listeners;
|
||||
|
||||
use App\Services\Discord\DiscordWebhook;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Laravel\Cashier\Events\WebhookReceived;
|
||||
|
||||
class PostStripeEventToDiscord implements ShouldQueue
|
||||
{
|
||||
/**
|
||||
* @var array<string, array{title: string, color: int}>
|
||||
*/
|
||||
private const EVENTS = [
|
||||
'customer.subscription.created' => ['title' => '🎉 New subscription', 'color' => 0x57F287],
|
||||
'customer.subscription.updated' => ['title' => '🔄 Subscription updated', 'color' => 0x5865F2],
|
||||
'customer.subscription.deleted' => ['title' => '👋 Subscription cancelled', 'color' => 0xED4245],
|
||||
'invoice.payment_succeeded' => ['title' => '💰 Payment succeeded', 'color' => 0x57F287],
|
||||
'invoice.payment_failed' => ['title' => '⚠️ Payment failed', 'color' => 0xED4245],
|
||||
];
|
||||
|
||||
public function __construct(private DiscordWebhook $discord) {}
|
||||
|
||||
public function handle(WebhookReceived $event): void
|
||||
{
|
||||
$type = $event->payload['type'] ?? null;
|
||||
|
||||
if (! is_string($type) || ! array_key_exists($type, self::EVENTS)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->discord->send('', [$this->buildEmbed($type, $event->payload)]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $payload
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
private function buildEmbed(string $type, array $payload): array
|
||||
{
|
||||
$object = $payload['data']['object'] ?? [];
|
||||
|
||||
return [
|
||||
'title' => self::EVENTS[$type]['title'],
|
||||
'color' => self::EVENTS[$type]['color'],
|
||||
'fields' => $this->fields($type, $object),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $object
|
||||
* @return array<int, array<string, mixed>>
|
||||
*/
|
||||
private function fields(string $type, array $object): array
|
||||
{
|
||||
$fields = [];
|
||||
|
||||
if (str_starts_with($type, 'invoice.')) {
|
||||
$amount = match ($type) {
|
||||
'invoice.payment_failed' => $object['amount_due'] ?? 0,
|
||||
default => $object['amount_paid'] ?? 0,
|
||||
};
|
||||
|
||||
$fields[] = [
|
||||
'name' => 'Amount',
|
||||
'value' => $this->money((int) $amount, (string) ($object['currency'] ?? 'usd')),
|
||||
'inline' => true,
|
||||
];
|
||||
$fields[] = [
|
||||
'name' => 'Customer',
|
||||
'value' => (string) ($object['customer_email'] ?? $object['customer'] ?? 'unknown'),
|
||||
'inline' => true,
|
||||
];
|
||||
|
||||
return $fields;
|
||||
}
|
||||
|
||||
$fields[] = [
|
||||
'name' => 'Status',
|
||||
'value' => (string) ($object['status'] ?? 'unknown'),
|
||||
'inline' => true,
|
||||
];
|
||||
$fields[] = [
|
||||
'name' => 'Customer',
|
||||
'value' => (string) ($object['customer'] ?? 'unknown'),
|
||||
'inline' => true,
|
||||
];
|
||||
|
||||
return $fields;
|
||||
}
|
||||
|
||||
private function money(int $amount, string $currency): string
|
||||
{
|
||||
$symbol = match (strtolower($currency)) {
|
||||
'eur' => '€',
|
||||
'gbp' => '£',
|
||||
'usd' => '$',
|
||||
'jpy' => '¥',
|
||||
'brl' => 'R$',
|
||||
default => strtoupper($currency).' ',
|
||||
};
|
||||
|
||||
return $symbol.number_format($amount / 100, 2);
|
||||
}
|
||||
}
|
||||
|
|
@ -9,13 +9,16 @@ use App\Events\TransactionUpdated;
|
|||
use App\Http\Responses\RegisterResponse;
|
||||
use App\Listeners\ApplyAutomationRules;
|
||||
use App\Listeners\AssignTransactionToBudget;
|
||||
use App\Listeners\PostStripeEventToDiscord;
|
||||
use App\Listeners\UnassignTransactionFromBudget;
|
||||
use App\Services\Banking\EnableBankingProvider;
|
||||
use App\Services\Discord\DiscordWebhook;
|
||||
use Illuminate\Cache\RateLimiting\Limit;
|
||||
use Illuminate\Support\Facades\Event;
|
||||
use Illuminate\Support\Facades\RateLimiter;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
use Laravel\Cashier\Cashier;
|
||||
use Laravel\Cashier\Events\WebhookReceived;
|
||||
use Laravel\Fortify\Contracts\RegisterResponse as RegisterResponseContract;
|
||||
|
||||
class AppServiceProvider extends ServiceProvider
|
||||
|
|
@ -35,6 +38,10 @@ class AppServiceProvider extends ServiceProvider
|
|||
base_path(config('services.enablebanking.private_key_path')),
|
||||
);
|
||||
});
|
||||
|
||||
$this->app->bind(DiscordWebhook::class, function () {
|
||||
return new DiscordWebhook(config('services.discord.webhook_url'));
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -46,6 +53,7 @@ class AppServiceProvider extends ServiceProvider
|
|||
Event::listen(TransactionCreated::class, AssignTransactionToBudget::class);
|
||||
Event::listen(TransactionUpdated::class, AssignTransactionToBudget::class);
|
||||
Event::listen(TransactionDeleted::class, UnassignTransactionFromBudget::class);
|
||||
Event::listen(WebhookReceived::class, PostStripeEventToDiscord::class);
|
||||
|
||||
RateLimiter::for('emails', function (object $job): Limit {
|
||||
return Limit::perSecond(30);
|
||||
|
|
|
|||
|
|
@ -0,0 +1,37 @@
|
|||
<?php
|
||||
|
||||
namespace App\Services\Discord;
|
||||
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class DiscordWebhook
|
||||
{
|
||||
public function __construct(private ?string $webhookUrl) {}
|
||||
|
||||
/**
|
||||
* @param array<int, array<string, mixed>> $embeds
|
||||
*/
|
||||
public function send(string $content = '', array $embeds = []): void
|
||||
{
|
||||
if (blank($this->webhookUrl)) {
|
||||
Log::warning('Discord webhook URL not configured, skipping message.');
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$payload = array_filter([
|
||||
'content' => $content !== '' ? $content : null,
|
||||
'embeds' => $embeds !== [] ? $embeds : null,
|
||||
]);
|
||||
|
||||
$response = Http::asJson()->post($this->webhookUrl, $payload);
|
||||
|
||||
if ($response->failed()) {
|
||||
Log::warning('Discord webhook request failed.', [
|
||||
'status' => $response->status(),
|
||||
'body' => $response->body(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,79 @@
|
|||
<?php
|
||||
|
||||
namespace App\Services\Stripe;
|
||||
|
||||
use Laravel\Cashier\Cashier;
|
||||
use Stripe\Exception\ApiErrorException;
|
||||
use Stripe\Subscription;
|
||||
|
||||
class SubscriptionStatsCollector
|
||||
{
|
||||
/**
|
||||
* @return array{active: array<string, array{count: int, mrr: float}>, trialing: array<string, array{count: int, mrr: float}>}
|
||||
*
|
||||
* @throws ApiErrorException
|
||||
*/
|
||||
public function collect(): array
|
||||
{
|
||||
return [
|
||||
'active' => $this->collectStatus('active'),
|
||||
'trialing' => $this->collectStatus('trialing'),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, array{count: int, mrr: float}>
|
||||
*
|
||||
* @throws ApiErrorException
|
||||
*/
|
||||
private function collectStatus(string $status): array
|
||||
{
|
||||
$bucket = [];
|
||||
|
||||
$subscriptions = Cashier::stripe()->subscriptions->all([
|
||||
'status' => $status,
|
||||
'limit' => 100,
|
||||
'expand' => ['data.items.data.price'],
|
||||
]);
|
||||
|
||||
/** @var Subscription $subscription */
|
||||
foreach ($subscriptions->autoPagingIterator() as $subscription) {
|
||||
$currency = strtolower((string) $subscription->currency);
|
||||
|
||||
$bucket[$currency] ??= ['count' => 0, 'mrr' => 0.0];
|
||||
$bucket[$currency]['count']++;
|
||||
$bucket[$currency]['mrr'] += $this->monthlyValue($subscription);
|
||||
}
|
||||
|
||||
return $bucket;
|
||||
}
|
||||
|
||||
private function monthlyValue(Subscription $subscription): float
|
||||
{
|
||||
$monthly = 0.0;
|
||||
|
||||
foreach ($subscription->items->data as $item) {
|
||||
$price = $item->price;
|
||||
|
||||
if ($price->recurring === null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$amount = ($price->unit_amount ?? 0) / 100;
|
||||
$quantity = $item->quantity ?? 1;
|
||||
$intervalCount = $price->recurring->interval_count ?: 1;
|
||||
|
||||
$perMonth = match ($price->recurring->interval) {
|
||||
'day' => $amount * 365 / 12,
|
||||
'week' => $amount * 52 / 12,
|
||||
'month' => $amount,
|
||||
'year' => $amount / 12,
|
||||
default => 0.0,
|
||||
};
|
||||
|
||||
$monthly += ($perMonth / $intervalCount) * $quantity;
|
||||
}
|
||||
|
||||
return $monthly;
|
||||
}
|
||||
}
|
||||
|
|
@ -43,4 +43,8 @@ return [
|
|||
'redirect_url' => env('ENABLEBANKING_REDIRECT_URL'),
|
||||
],
|
||||
|
||||
'discord' => [
|
||||
'webhook_url' => env('DISCORD_WEBHOOK_URL'),
|
||||
],
|
||||
|
||||
];
|
||||
|
|
|
|||
|
|
@ -11,3 +11,4 @@ Schedule::command('loans:generate-balances')->monthlyOn(1, '00:00');
|
|||
Schedule::command('resend:sync-leads')->dailyAt('03:00');
|
||||
Schedule::command('leads:send-invitations --force --limit=150')->dailyAt('09:00');
|
||||
Schedule::command('leads:send-re-invitations --force')->dailyAt('09:00');
|
||||
Schedule::command('stats:daily-report')->dailyAt('09:00')->timezone('Europe/Madrid');
|
||||
|
|
|
|||
|
|
@ -0,0 +1,36 @@
|
|||
<?php
|
||||
|
||||
use App\Services\Discord\DiscordWebhook;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
|
||||
test('posts content and embeds to the webhook url', function () {
|
||||
Http::fake();
|
||||
|
||||
(new DiscordWebhook('https://discord.test/webhook'))
|
||||
->send('hello', [['title' => 'Test embed']]);
|
||||
|
||||
Http::assertSent(function ($request) {
|
||||
return $request->url() === 'https://discord.test/webhook'
|
||||
&& $request['content'] === 'hello'
|
||||
&& $request['embeds'][0]['title'] === 'Test embed';
|
||||
});
|
||||
});
|
||||
|
||||
test('omits empty content and embeds from the payload', function () {
|
||||
Http::fake();
|
||||
|
||||
(new DiscordWebhook('https://discord.test/webhook'))->send('only content');
|
||||
|
||||
Http::assertSent(function ($request) {
|
||||
return $request['content'] === 'only content'
|
||||
&& ! isset($request['embeds']);
|
||||
});
|
||||
});
|
||||
|
||||
test('does not post when the webhook url is missing', function () {
|
||||
Http::fake();
|
||||
|
||||
(new DiscordWebhook(null))->send('hello');
|
||||
|
||||
Http::assertNothingSent();
|
||||
});
|
||||
|
|
@ -0,0 +1,59 @@
|
|||
<?php
|
||||
|
||||
use App\Listeners\PostStripeEventToDiscord;
|
||||
use App\Services\Discord\DiscordWebhook;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Laravel\Cashier\Events\WebhookReceived;
|
||||
|
||||
beforeEach(function () {
|
||||
config()->set('services.discord.webhook_url', 'https://discord.test/webhook');
|
||||
});
|
||||
|
||||
function handleStripeWebhook(array $payload): void
|
||||
{
|
||||
(new PostStripeEventToDiscord(app(DiscordWebhook::class)))
|
||||
->handle(new WebhookReceived($payload));
|
||||
}
|
||||
|
||||
test('posts a message for a created subscription', function () {
|
||||
Http::fake();
|
||||
|
||||
handleStripeWebhook([
|
||||
'type' => 'customer.subscription.created',
|
||||
'data' => ['object' => ['status' => 'active', 'customer' => 'cus_123']],
|
||||
]);
|
||||
|
||||
Http::assertSent(fn ($request) => str_contains($request['embeds'][0]['title'], 'New subscription'));
|
||||
});
|
||||
|
||||
test('posts the formatted amount for a succeeded payment', function () {
|
||||
Http::fake();
|
||||
|
||||
handleStripeWebhook([
|
||||
'type' => 'invoice.payment_succeeded',
|
||||
'data' => ['object' => ['amount_paid' => 1999, 'currency' => 'eur', 'customer_email' => 'a@b.com']],
|
||||
]);
|
||||
|
||||
Http::assertSent(fn ($request) => collect($request['embeds'][0]['fields'])
|
||||
->contains(fn ($field) => $field['value'] === '€19.99'));
|
||||
});
|
||||
|
||||
test('uses amount_due for a failed payment', function () {
|
||||
Http::fake();
|
||||
|
||||
handleStripeWebhook([
|
||||
'type' => 'invoice.payment_failed',
|
||||
'data' => ['object' => ['amount_due' => 500, 'currency' => 'usd']],
|
||||
]);
|
||||
|
||||
Http::assertSent(fn ($request) => str_contains($request['embeds'][0]['title'], 'Payment failed')
|
||||
&& collect($request['embeds'][0]['fields'])->contains(fn ($field) => $field['value'] === '$5.00'));
|
||||
});
|
||||
|
||||
test('ignores events outside the handled list', function () {
|
||||
Http::fake();
|
||||
|
||||
handleStripeWebhook(['type' => 'charge.refunded', 'data' => ['object' => []]]);
|
||||
|
||||
Http::assertNothingSent();
|
||||
});
|
||||
|
|
@ -0,0 +1,49 @@
|
|||
<?php
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
|
||||
beforeEach(function () {
|
||||
config()->set('services.discord.webhook_url', 'https://discord.test/webhook');
|
||||
});
|
||||
|
||||
test('posts yesterday user counts and stripe stats to discord', function () {
|
||||
Http::fake();
|
||||
bindMockStripeClientForStats([
|
||||
'active' => [makeStripeSubscription('eur', 1000, 'month')],
|
||||
'trialing' => [makeStripeSubscription('eur', 1000, 'month')],
|
||||
]);
|
||||
|
||||
$tz = 'Europe/Madrid';
|
||||
User::factory()->create(['created_at' => Carbon::now($tz)->subDay()->setTime(12, 0)->utc()]);
|
||||
User::factory()->create(['created_at' => Carbon::now($tz)->subDays(10)->utc()]);
|
||||
|
||||
$this->artisan('stats:daily-report')->assertSuccessful();
|
||||
|
||||
Http::assertSent(function ($request) {
|
||||
$embed = $request['embeds'][0];
|
||||
$users = collect($embed['fields'])->firstWhere('name', '👥 Users');
|
||||
|
||||
return $request->url() === 'https://discord.test/webhook'
|
||||
&& str_contains($users['value'], 'New yesterday: **1**')
|
||||
&& str_contains($users['value'], 'Total: **2**')
|
||||
&& collect($embed['fields'])->contains(fn ($f) => str_contains($f['value'], '€10.00'));
|
||||
});
|
||||
});
|
||||
|
||||
test('reports zero new users when none were created yesterday', function () {
|
||||
Http::fake();
|
||||
bindMockStripeClientForStats(['active' => [], 'trialing' => []]);
|
||||
|
||||
User::factory()->create(['created_at' => Carbon::now('Europe/Madrid')->subDays(5)->utc()]);
|
||||
|
||||
$this->artisan('stats:daily-report')->assertSuccessful();
|
||||
|
||||
Http::assertSent(function ($request) {
|
||||
$users = collect($request['embeds'][0]['fields'])->firstWhere('name', '👥 Users');
|
||||
|
||||
return str_contains($users['value'], 'New yesterday: **0**')
|
||||
&& str_contains($users['value'], 'Total: **1**');
|
||||
});
|
||||
});
|
||||
|
|
@ -1,64 +1,5 @@
|
|||
<?php
|
||||
|
||||
use Stripe\Collection;
|
||||
use Stripe\Service\SubscriptionService;
|
||||
use Stripe\StripeClient;
|
||||
use Stripe\Subscription;
|
||||
|
||||
function makeStripeSubscription(string $currency, int $unitAmount, string $interval, int $quantity = 1, int $intervalCount = 1): Subscription
|
||||
{
|
||||
return Subscription::constructFrom([
|
||||
'object' => 'subscription',
|
||||
'currency' => $currency,
|
||||
'items' => [
|
||||
'object' => 'list',
|
||||
'data' => [
|
||||
[
|
||||
'object' => 'subscription_item',
|
||||
'quantity' => $quantity,
|
||||
'price' => [
|
||||
'object' => 'price',
|
||||
'unit_amount' => $unitAmount,
|
||||
'recurring' => [
|
||||
'interval' => $interval,
|
||||
'interval_count' => $intervalCount,
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
function makeSubscriptionCollection(array $subscriptions): Collection
|
||||
{
|
||||
return Collection::constructFrom([
|
||||
'object' => 'list',
|
||||
'has_more' => false,
|
||||
'data' => array_map(fn (Subscription $s) => $s->toArray(), $subscriptions),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, list<Subscription>> $byStatus
|
||||
*/
|
||||
function bindMockStripeClientForStats(array $byStatus): void
|
||||
{
|
||||
$subscriptionService = Mockery::mock(SubscriptionService::class);
|
||||
|
||||
$subscriptionService->shouldReceive('all')
|
||||
->andReturnUsing(function (array $params) use ($byStatus): Collection {
|
||||
$status = $params['status'] ?? 'active';
|
||||
|
||||
return makeSubscriptionCollection($byStatus[$status] ?? []);
|
||||
});
|
||||
|
||||
$stripeClient = Mockery::mock(StripeClient::class);
|
||||
$stripeClient->subscriptions = $subscriptionService;
|
||||
|
||||
app()->bind(StripeClient::class, fn () => $stripeClient);
|
||||
}
|
||||
|
||||
test('warns when there are no subscriptions', function () {
|
||||
bindMockStripeClientForStats(['active' => [], 'trialing' => []]);
|
||||
|
||||
|
|
|
|||
|
|
@ -10,6 +10,10 @@ use App\Models\Transaction;
|
|||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Stripe\Collection as StripeCollection;
|
||||
use Stripe\Service\SubscriptionService;
|
||||
use Stripe\StripeClient;
|
||||
use Stripe\Subscription;
|
||||
use Tests\TestCase;
|
||||
|
||||
/*
|
||||
|
|
@ -152,6 +156,68 @@ function assertMaxQueries(int $max, Closure $callback, string $context = ''): vo
|
|||
expect($result['count'])->toBeLessThanOrEqual($max);
|
||||
}
|
||||
|
||||
/**
|
||||
* Build a fake Stripe subscription object for stats tests.
|
||||
*/
|
||||
function makeStripeSubscription(string $currency, int $unitAmount, string $interval, int $quantity = 1, int $intervalCount = 1): Subscription
|
||||
{
|
||||
return Subscription::constructFrom([
|
||||
'object' => 'subscription',
|
||||
'currency' => $currency,
|
||||
'items' => [
|
||||
'object' => 'list',
|
||||
'data' => [
|
||||
[
|
||||
'object' => 'subscription_item',
|
||||
'quantity' => $quantity,
|
||||
'price' => [
|
||||
'object' => 'price',
|
||||
'unit_amount' => $unitAmount,
|
||||
'recurring' => [
|
||||
'interval' => $interval,
|
||||
'interval_count' => $intervalCount,
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param list<Subscription> $subscriptions
|
||||
*/
|
||||
function makeSubscriptionCollection(array $subscriptions): StripeCollection
|
||||
{
|
||||
return StripeCollection::constructFrom([
|
||||
'object' => 'list',
|
||||
'has_more' => false,
|
||||
'data' => array_map(fn (Subscription $s) => $s->toArray(), $subscriptions),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Bind a mocked Stripe client that returns subscriptions per status.
|
||||
*
|
||||
* @param array<string, list<Subscription>> $byStatus
|
||||
*/
|
||||
function bindMockStripeClientForStats(array $byStatus): void
|
||||
{
|
||||
$subscriptionService = Mockery::mock(SubscriptionService::class);
|
||||
|
||||
$subscriptionService->shouldReceive('all')
|
||||
->andReturnUsing(function (array $params) use ($byStatus): StripeCollection {
|
||||
$status = $params['status'] ?? 'active';
|
||||
|
||||
return makeSubscriptionCollection($byStatus[$status] ?? []);
|
||||
});
|
||||
|
||||
$stripeClient = Mockery::mock(StripeClient::class);
|
||||
$stripeClient->subscriptions = $subscriptionService;
|
||||
|
||||
app()->bind(StripeClient::class, fn () => $stripeClient);
|
||||
}
|
||||
|
||||
function createCategoryViaUI($page, string $name, string $color = 'green', string $type = 'Expense'): void
|
||||
{
|
||||
$page->click('Create Category')
|
||||
|
|
|
|||
Loading…
Reference in New Issue