fix: stripe webhooks

This commit is contained in:
Andras Bacsai 2024-02-06 11:11:26 +01:00
parent 6869c582ff
commit 45b736bb01
5 changed files with 31 additions and 22 deletions

View File

@ -298,10 +298,8 @@ function validate_cron_expression($expression_to_validate): bool
function send_internal_notification(string $message): void function send_internal_notification(string $message): void
{ {
try { try {
$baseUrl = config('app.name');
$team = Team::find(0); $team = Team::find(0);
$team?->notify(new GeneralNotification("👀 {$baseUrl}: " . $message)); $team?->notify(new GeneralNotification($message));
ray("👀 {$baseUrl}: " . $message);
} catch (\Throwable $e) { } catch (\Throwable $e) {
ray($e->getMessage()); ray($e->getMessage());
} }

View File

@ -7,7 +7,7 @@ return [
// The release version of your application // The release version of your application
// Example with dynamic git hash: trim(exec('git --git-dir ' . base_path('.git') . ' log --pretty="%h" -n1 HEAD')) // Example with dynamic git hash: trim(exec('git --git-dir ' . base_path('.git') . ' log --pretty="%h" -n1 HEAD'))
'release' => '4.0.0-beta.209', 'release' => '4.0.0-beta.210',
// When left empty or `null` the Laravel environment will be used // When left empty or `null` the Laravel environment will be used
'environment' => config('app.env'), 'environment' => config('app.env'),

View File

@ -1,3 +1,3 @@
<?php <?php
return '4.0.0-beta.209'; return '4.0.0-beta.210';

View File

@ -764,7 +764,6 @@ Route::post('/payments/stripe/events', function () {
]); ]);
$type = data_get($event, 'type'); $type = data_get($event, 'type');
$data = data_get($event, 'data.object'); $data = data_get($event, 'data.object');
ray('Event: ' . $type);
switch ($type) { switch ($type) {
case 'checkout.session.completed': case 'checkout.session.completed':
$clientReferenceId = data_get($data, 'client_reference_id'); $clientReferenceId = data_get($data, 'client_reference_id');
@ -779,7 +778,8 @@ Route::post('/payments/stripe/events', function () {
$team = Team::find($teamId); $team = Team::find($teamId);
$found = $team->members->where('id', $userId)->first(); $found = $team->members->where('id', $userId)->first();
if (!$found->isAdmin()) { if (!$found->isAdmin()) {
throw new Exception("User {$userId} is not an admin or owner of team {$team->id}."); send_internal_notification("User {$userId} is not an admin or owner of team {$team->id}, customerid: {$customerId}, subscriptionid: {$subscriptionId}.");
throw new Exception("User {$userId} is not an admin or owner of team {$team->id}, customerid: {$customerId}, subscriptionid: {$subscriptionId}.");
} }
$subscription = Subscription::where('team_id', $teamId)->first(); $subscription = Subscription::where('team_id', $teamId)->first();
if ($subscription) { if ($subscription) {
@ -819,25 +819,35 @@ Route::post('/payments/stripe/events', function () {
break; break;
case 'invoice.payment_failed': case 'invoice.payment_failed':
$customerId = data_get($data, 'customer'); $customerId = data_get($data, 'customer');
$subscription = Subscription::where('stripe_customer_id', $customerId)->firstOrFail(); $subscription = Subscription::where('stripe_customer_id', $customerId)->first();
if (!$subscription) {
send_internal_notification('invoice.payment_failed failed but no subscription found in Coolify for customer: ' . $customerId);
return response('No subscription found in Coolify.');
}
$team = data_get($subscription, 'team'); $team = data_get($subscription, 'team');
if (!$team) { if (!$team) {
throw new Exception('No team found for subscription: ' . $subscription->id); send_internal_notification('invoice.payment_failed failed but no team found in Coolify for customer: ' . $customerId);
return response('No team found in Coolify.');
} }
if (!$subscription->stripe_invoice_paid) { if (!$subscription->stripe_invoice_paid) {
SubscriptionInvoiceFailedJob::dispatch($team); SubscriptionInvoiceFailedJob::dispatch($team);
send_internal_notification('Invoice payment failed: ' . $subscription->team->id); send_internal_notification('Invoice payment failed: ' . $customerId);
} else { } else {
send_internal_notification('Invoice payment failed but already paid: ' . $subscription->team->id); send_internal_notification('Invoice payment failed but already paid: ' . $customerId);
} }
break; break;
case 'payment_intent.payment_failed': case 'payment_intent.payment_failed':
$customerId = data_get($data, 'customer'); $customerId = data_get($data, 'customer');
$subscription = Subscription::where('stripe_customer_id', $customerId)->firstOrFail(); $subscription = Subscription::where('stripe_customer_id', $customerId)->first();
$subscription->update([ if (!$subscription) {
'stripe_invoice_paid' => false, send_internal_notification('payment_intent.payment_failed, no subscription found in Coolify for customer: ' . $customerId);
]); return response('No subscription found in Coolify.');
send_internal_notification('Subscription payment failed: ' . $subscription->team->id); }
if ($subscription->stripe_invoice_paid) {
send_internal_notification('payment_intent.payment_failed but invoice is active for customer: ' . $customerId);
return;
}
send_internal_notification('Subscription payment failed for customer: ' . $customerId);
break; break;
case 'customer.subscription.updated': case 'customer.subscription.updated':
$customerId = data_get($data, 'customer'); $customerId = data_get($data, 'customer');
@ -868,7 +878,7 @@ Route::post('/payments/stripe/events', function () {
$subscription->update([ $subscription->update([
'stripe_invoice_paid' => false, 'stripe_invoice_paid' => false,
]); ]);
send_internal_notification('Subscription paused or incomplete for team: ' . $subscription->team->id); send_internal_notification('Subscription paused or incomplete for customer: ' . $customerId);
} }
// Trial ended but subscribed, reactive servers // Trial ended but subscribed, reactive servers
@ -878,7 +888,7 @@ Route::post('/payments/stripe/events', function () {
} }
if ($feedback) { if ($feedback) {
$reason = "Cancellation feedback for {$subscription->team->id}: '" . $feedback . "'"; $reason = "Cancellation feedback for {$customerId}: '" . $feedback . "'";
if ($comment) { if ($comment) {
$reason .= ' with comment: \'' . $comment . "'"; $reason .= ' with comment: \'' . $comment . "'";
} }
@ -888,7 +898,7 @@ Route::post('/payments/stripe/events', function () {
if ($cancelAtPeriodEnd) { if ($cancelAtPeriodEnd) {
// send_internal_notification('Subscription cancelled at period end for team: ' . $subscription->team->id); // send_internal_notification('Subscription cancelled at period end for team: ' . $subscription->team->id);
} else { } else {
send_internal_notification('Subscription resumed for team: ' . $subscription->team->id); send_internal_notification('customer.subscription.updated for customer: ' . $customerId);
} }
} }
break; break;
@ -905,9 +915,10 @@ Route::post('/payments/stripe/events', function () {
'stripe_invoice_paid' => false, 'stripe_invoice_paid' => false,
'stripe_trial_already_ended' => true, 'stripe_trial_already_ended' => true,
]); ]);
// send_internal_notification('Subscription cancelled: ' . $subscription->team->id); send_internal_notification('customer.subscription.deleted for customer: ' . $customerId);
break; break;
case 'customer.subscription.trial_will_end': case 'customer.subscription.trial_will_end':
// Not used for now
$customerId = data_get($data, 'customer'); $customerId = data_get($data, 'customer');
$subscription = Subscription::where('stripe_customer_id', $customerId)->firstOrFail(); $subscription = Subscription::where('stripe_customer_id', $customerId)->firstOrFail();
$team = data_get($subscription, 'team'); $team = data_get($subscription, 'team');
@ -929,7 +940,7 @@ Route::post('/payments/stripe/events', function () {
'stripe_invoice_paid' => false, 'stripe_invoice_paid' => false,
]); ]);
SubscriptionTrialEndedJob::dispatch($team); SubscriptionTrialEndedJob::dispatch($team);
send_internal_notification('Subscription paused for team: ' . $subscription->team->id); send_internal_notification('Subscription paused for customer: ' . $customerId);
break; break;
default: default:
// Unhandled event type // Unhandled event type

View File

@ -4,7 +4,7 @@
"version": "3.12.36" "version": "3.12.36"
}, },
"v4": { "v4": {
"version": "4.0.0-beta.209" "version": "4.0.0-beta.210"
} }
} }
} }