Skip to content

Commit

Permalink
Fix customer creation
Browse files Browse the repository at this point in the history
  • Loading branch information
driesvints committed May 8, 2023
1 parent b524dee commit 29c1419
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ public function up(): void
$table->id();
$table->unsignedBigInteger('billable_id');
$table->string('billable_type');
$table->string('lemon_squeezy_id')->unique();
$table->string('lemon_squeezy_id')->nullable()->unique();
$table->timestamp('trial_ends_at')->nullable();
$table->timestamps();

$table->index(['billable_id', 'billable_type']);
$table->unique(['billable_id', 'billable_type']);
});
}
};
33 changes: 20 additions & 13 deletions src/Http/Controllers/WebhookController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@
use LemonSqueezy\Laravel\Subscription;
use Symfony\Component\HttpFoundation\Response;

class WebhookController extends Controller
/**
* @internal Not supported by any backwards compatibility promise. Please use events to react to webhooks.
*/
final class WebhookController extends Controller
{
public function __construct()
{
Expand Down Expand Up @@ -73,7 +76,11 @@ public function handleSubscriptionCreated(array $payload): void

$attributes = $payload['data']['attributes'];

$billable = $this->findOrCreateCustomer((string) $attributes['customer_id'], $custom);
$billable = $this->findOrCreateCustomer(
$custom['billable_id'],
(string) $custom['billable_type'],
(string) $attributes['customer_id']
);

$subscription = $billable->subscriptions()->create([
'type' => $custom['subscription_type'] ?? Subscription::DEFAULT_TYPE,
Expand All @@ -96,7 +103,7 @@ public function handleSubscriptionCreated(array $payload): void
SubscriptionCreated::dispatch($billable, $subscription, $payload);
}

protected function handleSubscriptionUpdated(array $payload): void
private function handleSubscriptionUpdated(array $payload): void
{
if (! $subscription = $this->findSubscription($payload['data']['id'])) {
return;
Expand All @@ -107,7 +114,7 @@ protected function handleSubscriptionUpdated(array $payload): void
SubscriptionUpdated::dispatch($subscription->billable, $subscription, $payload);
}

protected function handleSubscriptionCancelled(array $payload): void
private function handleSubscriptionCancelled(array $payload): void
{
if (! $subscription = $this->findSubscription($payload['data']['id'])) {
return;
Expand All @@ -118,7 +125,7 @@ protected function handleSubscriptionCancelled(array $payload): void
SubscriptionCancelled::dispatch($subscription->billable, $subscription, $payload);
}

protected function handleSubscriptionResumed(array $payload): void
private function handleSubscriptionResumed(array $payload): void
{
if (! $subscription = $this->findSubscription($payload['data']['id'])) {
return;
Expand All @@ -129,7 +136,7 @@ protected function handleSubscriptionResumed(array $payload): void
SubscriptionResumed::dispatch($subscription->billable, $subscription, $payload);
}

protected function handleSubscriptionExpired(array $payload): void
private function handleSubscriptionExpired(array $payload): void
{
if (! $subscription = $this->findSubscription($payload['data']['id'])) {
return;
Expand All @@ -140,7 +147,7 @@ protected function handleSubscriptionExpired(array $payload): void
SubscriptionExpired::dispatch($subscription->billable, $subscription, $payload);
}

protected function handleSubscriptionPaused(array $payload): void
private function handleSubscriptionPaused(array $payload): void
{
if (! $subscription = $this->findSubscription($payload['data']['id'])) {
return;
Expand All @@ -151,7 +158,7 @@ protected function handleSubscriptionPaused(array $payload): void
SubscriptionPaused::dispatch($subscription->billable, $subscription, $payload);
}

protected function handleSubscriptionUnpaused(array $payload): void
private function handleSubscriptionUnpaused(array $payload): void
{
if (! $subscription = $this->findSubscription($payload['data']['id'])) {
return;
Expand All @@ -165,17 +172,17 @@ protected function handleSubscriptionUnpaused(array $payload): void
/**
* @return \LemonSqueezy\Laravel\Billable
*/
protected function findOrCreateCustomer(string $customerId, array $custom)
private function findOrCreateCustomer(int|string $billableId, string $billableType, string $customerId)
{
return LemonSqueezy::$customerModel::firstOrCreate([
'lemon_squeezy_id' => $customerId,
'billable_id' => $billableId,
'billable_type' => $billableType,
], [
'billable_id' => $custom['billable_id'],
'billable_type' => $custom['billable_type'],
'lemon_squeezy_id' => $customerId,
])->billable;
}

protected function findSubscription(string $subscriptionId): ?Subscription
private function findSubscription(string $subscriptionId): ?Subscription
{
return LemonSqueezy::$subscriptionModel::firstWhere('lemon_squeezy_id', $subscriptionId);
}
Expand Down

0 comments on commit 29c1419

Please sign in to comment.