Skip to content

Store licenses #133

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
May 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions app/Jobs/CreateAnystackLicenseJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Jobs;

use App\Enums\Subscription;
use App\Models\License;
use App\Models\User;
use App\Notifications\LicenseKeyGenerated;
use Illuminate\Bus\Queueable;
Expand All @@ -11,7 +12,6 @@
use Illuminate\Http\Client\PendingRequest;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Http;

class CreateAnystackLicenseJob implements ShouldQueue
Expand All @@ -21,6 +21,7 @@ class CreateAnystackLicenseJob implements ShouldQueue
public function __construct(
public User $user,
public Subscription $subscription,
public ?string $subscriptionItemId = null,
public ?string $firstName = null,
public ?string $lastName = null,
) {}
Expand All @@ -34,12 +35,20 @@ public function handle(): void
$this->user->save();
}

$license = $this->createLicense($this->user->anystack_contact_id);
$licenseData = $this->createLicense($this->user->anystack_contact_id);

Cache::put($this->user->email.'.license_key', $license['key'], now()->addDay());
$license = License::create([
'user_id' => $this->user->id,
'subscription_item_id' => $this->subscriptionItemId,
'policy_name' => $this->subscription->value,
'key' => $licenseData['key'],
'expires_at' => $licenseData['expires_at'],
'created_at' => $licenseData['created_at'],
'updated_at' => $licenseData['updated_at'],
]);

$this->user->notify(new LicenseKeyGenerated(
$license['key'],
$license->key,
$this->subscription,
$this->firstName
));
Expand Down
6 changes: 6 additions & 0 deletions app/Jobs/HandleCustomerSubscriptionCreatedJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Illuminate\Queue\SerializesModels;
use Laravel\Cashier\Cashier;
use Laravel\Cashier\Events\WebhookHandled;
use Laravel\Cashier\SubscriptionItem;
use Stripe\Subscription;

class HandleCustomerSubscriptionCreatedJob implements ShouldQueue
Expand Down Expand Up @@ -38,6 +39,10 @@ public function handle(): void
}

$subscriptionPlan = \App\Enums\Subscription::fromStripeSubscription($stripeSubscription);
$cashierSubscriptionItemId = SubscriptionItem::query()
->where('stripe_id', $stripeSubscription->items->first()->id)
->first()
->id;

$nameParts = explode(' ', $user->name ?? '', 2);
$firstName = $nameParts[0] ?: null;
Expand All @@ -46,6 +51,7 @@ public function handle(): void
dispatch(new CreateAnystackLicenseJob(
$user,
$subscriptionPlan,
$cashierSubscriptionItemId,
$firstName,
$lastName,
));
Expand Down
18 changes: 14 additions & 4 deletions app/Livewire/OrderSuccess.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace App\Livewire;

use App\Enums\Subscription;
use Illuminate\Support\Facades\Cache;
use App\Models\User;
use Laravel\Cashier\Cashier;
use Livewire\Attributes\Layout;
use Livewire\Attributes\Title;
Expand Down Expand Up @@ -63,11 +63,21 @@ private function loadLicenseKey(): ?string
return null;
}

if ($licenseKey = Cache::get($this->email.'.license_key')) {
session()->put($this->sessionKey('license_key'), $licenseKey);
$user = User::where('email', $this->email)->first();

if (! $user) {
return null;
}

return $licenseKey;
$license = $user->licenses()->latest()->first();

if (! $license) {
return null;
}

session()->put($this->sessionKey('license_key'), $license->key);

return $license->key;
}

private function loadSubscription(): ?Subscription
Expand Down
35 changes: 35 additions & 0 deletions app/Models/License.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Laravel\Cashier\SubscriptionItem;

class License extends Model
{
use HasFactory;

protected $guarded = [];

protected $casts = [
'expires_at' => 'datetime',
];

/**
* @return BelongsTo<User>
*/
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}

/**
* @return BelongsTo<SubscriptionItem>
*/
public function subscriptionItem(): BelongsTo
{
return $this->belongsTo(SubscriptionItem::class);
}
}
9 changes: 9 additions & 0 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

// use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Cashier\Billable;
Expand All @@ -24,4 +25,12 @@ class User extends Authenticatable
'email_verified_at' => 'datetime',
'password' => 'hashed',
];

/**
* @return HasMany<License>
*/
public function licenses(): HasMany
{
return $this->hasMany(License::class);
}
}
31 changes: 31 additions & 0 deletions database/factories/LicenseFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Database\Factories;

use App\Enums\Subscription;
use App\Models\License;
use App\Models\User;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Facades\Date;
use Laravel\Cashier\SubscriptionItem;

/**
* @extends Factory<License>
*/
class LicenseFactory extends Factory
{
protected $model = License::class;

public function definition(): array
{
return [
'user_id' => User::factory(),
'subscription_item_id' => SubscriptionItem::factory(),
'policy_name' => fake()->randomElement(Subscription::cases())->value,
'key' => fake()->uuid(),
'created_at' => fake()->dateTimeBetween('-1 year', 'now'),
'updated_at' => fn (array $attrs) => $attrs['created_at'],
'expires_at' => fn (array $attrs) => Date::parse($attrs['created_at'])->addYear(),
];
}
}
35 changes: 35 additions & 0 deletions database/migrations/2025_05_14_013051_create_licenses_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('licenses', function (Blueprint $table) {
$table->id();
$table->foreignId('user_id');
$table->foreignId('subscription_item_id')->nullable();
$table->string('policy_name');
$table->string('key');
$table->timestamp('expires_at')->nullable();
$table->timestamps();

$table->index('user_id');
$table->index('subscription_item_id');
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('licenses');
}
};
4 changes: 2 additions & 2 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
<env name="APP_ENV" value="testing"/>
<env name="BCRYPT_ROUNDS" value="4"/>
<env name="CACHE_DRIVER" value="array"/>
<!-- <env name="DB_CONNECTION" value="sqlite"/> -->
<!-- <env name="DB_DATABASE" value=":memory:"/> -->
<env name="DB_CONNECTION" value="sqlite"/>
<env name="DB_DATABASE" value=":memory:"/>
<env name="MAIL_MAILER" value="array"/>
<env name="QUEUE_CONNECTION" value="sync"/>
<env name="SESSION_DRIVER" value="array"/>
Expand Down
Loading