Skip to content

Instantly share code, notes, and snippets.

@smitmartijn
Created August 29, 2025 13:01
Show Gist options
  • Select an option

  • Save smitmartijn/3d9bed0d983c1d5fffacc18e4f76bfa0 to your computer and use it in GitHub Desktop.

Select an option

Save smitmartijn/3d9bed0d983c1d5fffacc18e4f76bfa0 to your computer and use it in GitHub Desktop.
<?php
namespace App\Listeners;
use Laravel\Paddle\Events\WebhookReceived;
use App\Models\User;
use Laravel\Paddle\Cashier;
use Illuminate\Support\Facades\Log;
class HandleWebhookReceived
{
/**
* Handle the event.
*/
public function handle(WebhookReceived $event): void
{
$payload = $event->payload;
// Only handle transaction.completed events
if ($payload['event_type'] !== 'transaction.completed') {
return;
}
$transaction = $payload['data'];
Log::info('WebhookReceived: Processing transaction.completed for user creation', [
'transaction_id' => $transaction['id'],
'customer_id' => $transaction['customer_id'] ?? 'unknown'
]);
try {
$customerId = $transaction['customer_id'];
// We need to call the Paddle API to get the customers' email
$customerResponse = Cashier::api('GET', "customers/{$customerId}");
if (!isset($customerResponse['data'])) {
Log::error("Failed to fetch customer data for ID: {$customerId}", [
'transaction_id' => $transaction['id']
]);
return;
}
$customerData = $customerResponse['data'];
$customerEmail = $customerData['email'];
// Check if user already exists
$existingUser = User::where('email', $customerEmail)->first();
if ($existingUser) {
Log::info('User already exists, checking for Paddle customer record', [
'user_id' => $existingUser->id,
'email' => $customerEmail,
'paddle_customer_id' => $customerId
]);
// Ensure the user has the Paddle customer ID set
if (!$existingUser->paddle_id) {
$existingUser->update(['paddle_id' => $customerId]);
Log::info('Updated existing user with Paddle customer ID', [
'user_id' => $existingUser->id,
'paddle_customer_id' => $customerId
]);
}
return;
}
// Create new user for guest checkout
$user = User::create([
'name' => $customerData['name'] ?? explode('@', $customerEmail)[0],
'email' => $customerEmail,
'email_verified_at' => now(),
'paddle_id' => $customerId,
'password' => bcrypt(str()->random(32)) // Random password since they'll reset it
]);
$user->createAsCustomer();
Log::info('Created new user from guest checkout', [
'user_id' => $user->id,
'email' => $customerEmail,
'paddle_customer_id' => $customerId,
'transaction_id' => $transaction['id']
]);
} catch (\Exception $e) {
Log::error('Failed to process WebhookReceived for user creation', [
'error' => $e->getMessage(),
'transaction_id' => $transaction['id'] ?? 'unknown',
'customer_id' => $customerId ?? 'unknown'
]);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment