This commit is contained in:
Andras Bacsai 2023-08-15 16:28:38 +02:00
parent 56161e8e0d
commit 878db64878

View File

@ -175,127 +175,122 @@
} }
}); });
if (is_cloud()) { Route::get('/waitlist/confirm', function () {
Route::get('/test', function () { $email = request()->get('email');
return 'OK'; $confirmation_code = request()->get('confirmation_code');
}); ray($email, $confirmation_code);
Route::get('/waitlist/confirm', function () { try {
$email = request()->get('email'); $found = Waitlist::where('uuid', $confirmation_code)->where('email', $email)->first();
$confirmation_code = request()->get('confirmation_code'); if ($found && !$found->verified && $found->created_at > now()->subMinutes(config('constants.waitlist.confirmation_valid_for_minutes'))) {
ray($email, $confirmation_code); $found->verified = true;
try { $found->save();
$found = Waitlist::where('uuid', $confirmation_code)->where('email', $email)->first(); return 'Thank you for confirming your email address. We will notify you when you are next in line.';
if ($found && !$found->verified && $found->created_at > now()->subMinutes(config('constants.waitlist.confirmation_valid_for_minutes'))) { }
$found->verified = true; return redirect()->route('dashboard');
$found->save(); } catch (error) {
return 'Thank you for confirming your email address. We will notify you when you are next in line.'; return redirect()->route('dashboard');
} }
return redirect()->route('dashboard');
} catch (error) { })->name('webhooks.waitlist.confirm');
return redirect()->route('dashboard'); Route::get('/waitlist/cancel', function () {
$email = request()->get('email');
$confirmation_code = request()->get('confirmation_code');
try {
$found = Waitlist::where('uuid', $confirmation_code)->where('email', $email)->first();
if ($found && !$found->verified) {
$found->delete();
return 'Your email address has been removed from the waitlist.';
}
return redirect()->route('dashboard');
} catch (error) {
return redirect()->route('dashboard');
}
})->name('webhooks.waitlist.cancel');
Route::post('/payments/events', function () {
try {
$secret = config('coolify.lemon_squeezy_webhook_secret');
$payload = request()->collect();
$hash = hash_hmac('sha256', $payload, $secret);
$signature = request()->header('X-Signature');
if (!hash_equals($hash, $signature)) {
return response('Invalid signature.', 400);
} }
})->name('webhooks.waitlist.confirm'); $webhook = Webhook::create([
Route::get('/waitlist/cancel', function () { 'type' => 'lemonsqueezy',
$email = request()->get('email'); 'payload' => $payload,
$confirmation_code = request()->get('confirmation_code'); ]);
try { $event = data_get($payload, 'meta.event_name');
$found = Waitlist::where('uuid', $confirmation_code)->where('email', $email)->first(); ray('Subscription event: ' . $event);
if ($found && !$found->verified) { $email = data_get($payload, 'data.attributes.user_email');
$found->delete(); $team_id = data_get($payload, 'meta.custom_data.team_id');
return 'Your email address has been removed from the waitlist.'; if (is_null($team_id) || empty($team_id)) {
} throw new Exception('No team_id found in webhook payload.');
return redirect()->route('dashboard');
} catch (error) {
return redirect()->route('dashboard');
} }
$subscription_id = data_get($payload, 'data.id');
})->name('webhooks.waitlist.cancel'); $order_id = data_get($payload, 'data.attributes.order_id');
Route::post('/payments/events', function () { $product_id = data_get($payload, 'data.attributes.product_id');
try { $variant_id = data_get($payload, 'data.attributes.variant_id');
$secret = config('coolify.lemon_squeezy_webhook_secret'); $variant_name = data_get($payload, 'data.attributes.variant_name');
$payload = request()->collect(); $customer_id = data_get($payload, 'data.attributes.customer_id');
$hash = hash_hmac('sha256', $payload, $secret); $status = data_get($payload, 'data.attributes.status');
$signature = request()->header('X-Signature'); $trial_ends_at = data_get($payload, 'data.attributes.trial_ends_at');
$renews_at = data_get($payload, 'data.attributes.renews_at');
if (!hash_equals($hash, $signature)) { $ends_at = data_get($payload, 'data.attributes.ends_at');
return response('Invalid signature.', 400); $update_payment_method = data_get($payload, 'data.attributes.urls.update_payment_method');
} $team = Team::find($team_id);
$found = $team->members->where('email', $email)->first();
$webhook = Webhook::create([ if (!$found->isAdmin()) {
'type' => 'lemonsqueezy', throw new Exception("User {$email} is not an admin or owner of team {$team->id}.");
'payload' => $payload, }
]); switch ($event) {
$event = data_get($payload, 'meta.event_name'); case 'subscription_created':
ray('Subscription event: ' . $event); case 'subscription_updated':
$email = data_get($payload, 'data.attributes.user_email'); case 'subscription_resumed':
$team_id = data_get($payload, 'meta.custom_data.team_id'); case 'subscription_unpaused':
if (is_null($team_id) || empty($team_id)) { $subscription = Subscription::updateOrCreate([
throw new Exception('No team_id found in webhook payload.'); 'team_id' => $team_id,
} ], [
$subscription_id = data_get($payload, 'data.id'); 'lemon_subscription_id' => $subscription_id,
$order_id = data_get($payload, 'data.attributes.order_id'); 'lemon_customer_id' => $customer_id,
$product_id = data_get($payload, 'data.attributes.product_id'); 'lemon_order_id' => $order_id,
$variant_id = data_get($payload, 'data.attributes.variant_id'); 'lemon_product_id' => $product_id,
$variant_name = data_get($payload, 'data.attributes.variant_name'); 'lemon_variant_id' => $variant_id,
$customer_id = data_get($payload, 'data.attributes.customer_id'); 'lemon_status' => $status,
$status = data_get($payload, 'data.attributes.status'); 'lemon_variant_name' => $variant_name,
$trial_ends_at = data_get($payload, 'data.attributes.trial_ends_at'); 'lemon_trial_ends_at' => $trial_ends_at,
$renews_at = data_get($payload, 'data.attributes.renews_at'); 'lemon_renews_at' => $renews_at,
$ends_at = data_get($payload, 'data.attributes.ends_at'); 'lemon_ends_at' => $ends_at,
$update_payment_method = data_get($payload, 'data.attributes.urls.update_payment_method'); 'lemon_update_payment_menthod_url' => $update_payment_method,
$team = Team::find($team_id); ]);
$found = $team->members->where('email', $email)->first(); break;
if (!$found->isAdmin()) { case 'subscription_cancelled':
throw new Exception("User {$email} is not an admin or owner of team {$team->id}."); case 'subscription_paused':
} case 'subscription_expired':
switch ($event) { $subscription = Subscription::where('team_id', $team_id)->where('lemon_order_id', $order_id)->first();
case 'subscription_created': if ($subscription) {
case 'subscription_updated': $subscription->update([
case 'subscription_resumed':
case 'subscription_unpaused':
$subscription = Subscription::updateOrCreate([
'team_id' => $team_id,
], [
'lemon_subscription_id' => $subscription_id,
'lemon_customer_id' => $customer_id,
'lemon_order_id' => $order_id,
'lemon_product_id' => $product_id,
'lemon_variant_id' => $variant_id,
'lemon_status' => $status, 'lemon_status' => $status,
'lemon_variant_name' => $variant_name,
'lemon_trial_ends_at' => $trial_ends_at, 'lemon_trial_ends_at' => $trial_ends_at,
'lemon_renews_at' => $renews_at, 'lemon_renews_at' => $renews_at,
'lemon_ends_at' => $ends_at, 'lemon_ends_at' => $ends_at,
'lemon_update_payment_menthod_url' => $update_payment_method, 'lemon_update_payment_menthod_url' => $update_payment_method,
]); ]);
break; }
case 'subscription_cancelled': break;
case 'subscription_paused':
case 'subscription_expired':
$subscription = Subscription::where('team_id', $team_id)->where('lemon_order_id', $order_id)->first();
if ($subscription) {
$subscription->update([
'lemon_status' => $status,
'lemon_trial_ends_at' => $trial_ends_at,
'lemon_renews_at' => $renews_at,
'lemon_ends_at' => $ends_at,
'lemon_update_payment_menthod_url' => $update_payment_method,
]);
}
break;
}
$webhook->update([
'status' => 'success',
]);
} catch (Exception $e) {
ray($e->getMessage());
$webhook->update([
'status' => 'failed',
'failure_reason' => $e->getMessage(),
]);
} finally {
return response('OK');
} }
}); $webhook->update([
} 'status' => 'success',
]);
} catch (Exception $e) {
ray($e->getMessage());
$webhook->update([
'status' => 'failed',
'failure_reason' => $e->getMessage(),
]);
} finally {
return response('OK');
}
});