Skip to content
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
28 changes: 16 additions & 12 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\User;
use App\Notifications\LicenseKeyGenerated;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
Expand All @@ -12,41 +13,44 @@
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Notification;

class CreateAnystackLicenseJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

public function __construct(
public string $email,
public User $user,
public Subscription $subscription,
public ?string $firstName = null,
public ?string $lastName = null,
) {}

public function handle(): void
{
$contact = $this->createContact();
if (! $this->user->anystack_contact_id) {
$contact = $this->createContact();

$license = $this->createLicense($contact['id']);
$this->user->anystack_contact_id = $contact['id'];
$this->user->save();
}

Cache::put($this->email.'.license_key', $license['key'], now()->addDay());
$license = $this->createLicense($this->user->anystack_contact_id);

Notification::route('mail', $this->email)
->notify(new LicenseKeyGenerated(
$license['key'],
$this->subscription,
$this->firstName
));
Cache::put($this->user->email.'.license_key', $license['key'], now()->addDay());

$this->user->notify(new LicenseKeyGenerated(
$license['key'],
$this->subscription,
$this->firstName
));
}

private function createContact(): array
{
$data = collect([
'first_name' => $this->firstName,
'last_name' => $this->lastName,
'email' => $this->email,
'email' => $this->user->email,
])
->filter()
->all();
Expand Down
5 changes: 5 additions & 0 deletions app/Jobs/CreateUserFromStripeCustomer.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Str;
use Laravel\Cashier\Cashier;
use Stripe\Customer;
Expand Down Expand Up @@ -39,6 +40,10 @@ public function handle(): void
return;
}

Validator::validate(['email' => $this->customer->email], [
'email' => 'required|email|max:255',
]);

$user = new User;
$user->name = $this->customer->name;
$user->email = $this->customer->email;
Expand Down
6 changes: 4 additions & 2 deletions app/Jobs/HandleCustomerSubscriptionCreatedJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Jobs;

use App\Models\User;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
Expand All @@ -27,9 +28,10 @@ public function handle(): void
return;
}

/** @var User $user */
$user = Cashier::findBillable($stripeSubscription->customer);

if (! $user || ! ($email = $user->email)) {
if (! $user || ! $user->email) {
$this->fail('Failed to find user from Stripe subscription customer.');

return;
Expand All @@ -42,7 +44,7 @@ public function handle(): void
$lastName = $nameParts[1] ?? null;

dispatch(new CreateAnystackLicenseJob(
$email,
$user,
$subscriptionPlan,
$firstName,
$lastName,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?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::table('users', function (Blueprint $table) {
$table->string('anystack_contact_id')->nullable()->index();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('users', function (Blueprint $table) {
$table->dropIndex([
'anystack_contact_id',
]);

$table->dropColumn([
'anystack_contact_id',
]);
});
}
};
43 changes: 31 additions & 12 deletions tests/Feature/Jobs/CreateAnystackLicenseJobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Enums\Subscription;
use App\Jobs\CreateAnystackLicenseJob;
use App\Models\User;
use App\Notifications\LicenseKeyGenerated;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Cache;
Expand Down Expand Up @@ -45,8 +46,13 @@ protected function setUp(): void
/** @test */
public function it_creates_contact_and_license_on_anystack()
{
$user = User::factory()->create([
'email' => 'test@example.com',
'name' => 'John Doe',
]);

$job = new CreateAnystackLicenseJob(
'test@example.com',
$user,
Subscription::Max,
'John',
'Doe'
Expand Down Expand Up @@ -79,8 +85,13 @@ public function it_creates_contact_and_license_on_anystack()
/** @test */
public function it_stores_license_key_in_cache()
{
$user = User::factory()->create([
'email' => 'test@example.com',
'name' => 'John Doe',
]);

$job = new CreateAnystackLicenseJob(
'test@example.com',
$user,
Subscription::Max,
'John',
'Doe'
Expand All @@ -94,20 +105,24 @@ public function it_stores_license_key_in_cache()
/** @test */
public function it_sends_license_key_notification()
{
$user = User::factory()->create([
'email' => 'test@example.com',
'name' => 'John Doe',
]);

$job = new CreateAnystackLicenseJob(
'test@example.com',
$user,
Subscription::Max,
'John',
'Doe'
);

$job->handle();

Notification::assertSentOnDemand(
LicenseKeyGenerated::class,
Notification::assertSentTo(
$user,
function (LicenseKeyGenerated $notification, array $channels, object $notifiable) {
return $notifiable->routes['mail'] === 'test@example.com' &&
$notification->licenseKey === 'test-license-key-12345' &&
return $notification->licenseKey === 'test-license-key-12345' &&
$notification->subscription === Subscription::Max &&
$notification->firstName === 'John';
}
Expand All @@ -117,9 +132,14 @@ function (LicenseKeyGenerated $notification, array $channels, object $notifiable
/** @test */
public function it_handles_missing_name_components()
{
$user = User::factory()->create([
'email' => 'test@example.com',
'name' => null,
]);

// Create and run the job with missing name components
$job = new CreateAnystackLicenseJob(
'test@example.com',
$user,
Subscription::Max,
);

Expand All @@ -135,11 +155,10 @@ public function it_handles_missing_name_components()
});

// Assert notification was sent with null firstName
Notification::assertSentOnDemand(
LicenseKeyGenerated::class,
Notification::assertSentTo(
$user,
function (LicenseKeyGenerated $notification, array $channels, object $notifiable) {
return $notifiable->routes['mail'] === 'test@example.com' &&
$notification->licenseKey === 'test-license-key-12345' &&
return $notification->licenseKey === 'test-license-key-12345' &&
$notification->subscription === Subscription::Max &&
$notification->firstName === null;
}
Expand Down
19 changes: 19 additions & 0 deletions tests/Feature/Jobs/CreateUserFromStripeCustomerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Hash;
use Illuminate\Validation\ValidationException;
use Stripe\Customer;
use Tests\TestCase;

Expand Down Expand Up @@ -96,4 +97,22 @@ public function it_handles_a_null_name_in_stripe_customer()
'stripe_id' => 'cus_noname123',
]);
}

/** @test */
public function it_fails_when_customer_has_no_email()
{
$customer = Customer::constructFrom([
'id' => 'cus_noemail123',
'name' => 'No Email',
'email' => '',
]);

$job = new CreateUserFromStripeCustomer($customer);

$this->expectException(ValidationException::class);

$job->handle();

$this->assertDatabaseCount('users', 0);
}
}
10 changes: 8 additions & 2 deletions tests/Feature/Jobs/HandleCustomerSubscriptionCreatedJobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use App\Jobs\CreateAnystackLicenseJob;
use App\Jobs\CreateUserFromStripeCustomer;
use App\Jobs\HandleCustomerSubscriptionCreatedJob;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Bus;
use Laravel\Cashier\Events\WebhookHandled;
Expand Down Expand Up @@ -38,7 +39,8 @@ public function it_dispatches_the_create_anystack_license_job_with_correct_data(
$job->handle();

Bus::assertDispatched(CreateAnystackLicenseJob::class, function (CreateAnystackLicenseJob $job) {
return $job->email === 'test@example.com' &&
return $job->user instanceof User &&
$job->user->email === 'test@example.com' &&
$job->subscription === Subscription::Max &&
$job->firstName === 'John' &&
$job->lastName === 'Doe';
Expand Down Expand Up @@ -99,7 +101,11 @@ public function it_fails_when_customer_has_no_email()

$this->mockStripeClient($mockCustomer);

dispatch_sync(new CreateUserFromStripeCustomer($mockCustomer));
User::factory()->create([
'stripe_id' => 'cus_S9dhoV2rJK2Auy',
'name' => 'John Doe',
'email' => '',
]);

Bus::fake();

Expand Down
4 changes: 3 additions & 1 deletion tests/Feature/Livewire/OrderSuccessTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ public function allLineItems()
}
};

$this->app->instance(StripeClient::class, $mockStripeClient);
$this->app->bind(StripeClient::class, function ($app, $parameters) use ($mockStripeClient) {
return $mockStripeClient;
});
}
}
4 changes: 2 additions & 2 deletions tests/Feature/StripePurchaseHandlingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ public function a_license_is_created_when_a_stripe_subscription_is_created()

$this->postJson('/stripe/webhook', $payload);

Bus::assertDispatched(CreateAnystackLicenseJob::class, function ($job) {
return $job->email === 'john@example.com' &&
Bus::assertDispatched(CreateAnystackLicenseJob::class, function (CreateAnystackLicenseJob $job) {
return $job->user->email === 'john@example.com' &&
$job->subscription === Subscription::Max &&
$job->firstName === 'John' &&
$job->lastName === 'Doe';
Expand Down