added notification system for successful payment

This commit is contained in:
Gitea
2025-06-20 20:34:57 +05:30
parent dbe6d49c49
commit 3afa74ecc2
3 changed files with 52 additions and 0 deletions

View File

@@ -2,6 +2,7 @@
namespace App\Listeners;
use App\NotifyMe;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
@@ -11,6 +12,7 @@ use Livewire\Livewire;
class StripeEventListener
{
use NotifyMe;
/**
* Create the event listener.
*/
@@ -27,11 +29,26 @@ class StripeEventListener
if ($event->payload['type'] === 'invoice.payment_succeeded') {
session()->flash('alert', ['type' => 'success', 'message' => 'Payment completed successfully.']);
Log::info('Payment succeeded');
$amount = $event->payload['data']['object']['amount_paid'] / 100 ?? 'Unknown'; // Convert cents to dollars
$email = $event->payload['data']['object']['customer_email'] ?? 'Unknown';
$message = "✅ Payment Success\n" .
"Email: {$email}\n" .
"Amount: $" . number_format($amount, 2) . "\n" .
"Time: " . now()->toDateTimeString();
self::sendTelegramNotification($message);
}
if ($event->payload['type'] === 'customer.subscription.deleted') {
session()->flash('alert', ['type' => 'error', 'message' => 'Subscription canceled.']);
Log::info('Subscription canceled');
$email = $event->payload['data']['object']['customer_email'] ?? 'Unknown';
$message = "❌ Subscription Canceled\n" .
"Email: {$email}\n" .
"Time: " . now()->toDateTimeString();
self::sendTelegramNotification($message);
}
}
}

32
app/NotifyMe.php Normal file
View File

@@ -0,0 +1,32 @@
<?php
namespace App;
trait NotifyMe
{
function sendTelegramNotification($message) {
$botToken = config('app.notify_tg_bot_token');
$chatId = config('app.notify_tg_chat_id');
if (!$botToken || !$chatId) {
\Log::error('Telegram bot token or chat ID not configured');
return false;
}
$url = "https://api.telegram.org/bot{$botToken}/sendMessage";
$data = [
'chat_id' => $chatId,
'text' => $message,
'parse_mode' => 'HTML'
];
try {
$response = \Http::post($url, $data);
return $response->successful();
} catch (\Exception $e) {
\Log::error('Failed to send Telegram notification: ' . $e->getMessage());
return false;
}
}
}

View File

@@ -36,6 +36,9 @@ return [
'move_or_delete' => env('MOVE_OR_DELETE', null),
'auto_fetch_mail' => env('AUTO_FETCH_MAIL', false),
'notify_tg_bot_token' => env('NOTIFY_TG_BOT_TOKEN', ''),
'notify_tg_chat_id' => env('NOTIFY_TG_CHAT_ID', ''),
/*
|--------------------------------------------------------------------------
| Application Debug Mode