Skip to content

Beautiful Open-Source Form Builder

remove branding for subscribed users (#424)

Favour Olayinka68b610bc1550
diff --git a/app/Events/SubscriptionCreated.php b/app/Events/SubscriptionCreated.php
new file mode 100644
index 000000000..683b83add
--- /dev/null
+++ b/app/Events/SubscriptionCreated.php
@@ -0,0 +1,26 @@
+<?php
+
+namespace App\Events;
+
+use App\Models\Billing\Subscription;
+use Illuminate\Broadcasting\InteractsWithSockets;
+use Illuminate\Foundation\Events\Dispatchable;
+use Illuminate\Queue\SerializesModels;
+
+class SubscriptionCreated
+{
+ use Dispatchable;
+ use InteractsWithSockets;
+ use SerializesModels;
+
+ /**
+ * Create a new event instance.
+ *
+ * @return void
+ */
+
+ public function __construct(public Subscription $subscription)
+ {
+
+ }
+}
diff --git a/app/Listeners/HandleSubscriptionCreated.php b/app/Listeners/HandleSubscriptionCreated.php
new file mode 100644
index 000000000..00898808a
--- /dev/null
+++ b/app/Listeners/HandleSubscriptionCreated.php
@@ -0,0 +1,26 @@
+<?php
+
+namespace App\Listeners;
+
+use App\Events\SubscriptionCreated;
+
+class HandleSubscriptionCreated
+{
+ /**
+ * Handle the event.
+ *
+ * @param object $event
+ * @return void
+ */
+
+ public function handle(SubscriptionCreated $event)
+ {
+ $user = $event->subscription->user;
+
+ // Remove branding
+ $user->workspaces()->with('forms')->get()->each(function ($workspace) {
+ $workspace->forms()->update(['no_branding' => true]);
+ });
+
+ }
+}
diff --git a/app/Models/Billing/Subscription.php b/app/Models/Billing/Subscription.php
index e20d61339..8ef80e0b6 100644
--- a/app/Models/Billing/Subscription.php
+++ b/app/Models/Billing/Subscription.php
@@ -2,6 +2,7 @@
namespace App\Models\Billing;
+use App\Events\SubscriptionCreated;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Laravel\Cashier\Subscription as CashierSubscription;
@@ -9,6 +10,10 @@ class Subscription extends CashierSubscription
{
use HasFactory;
+ protected $dispatchesEvents = [
+ 'created' => SubscriptionCreated::class,
+ ];
+
public static function booted(): void
{
static::saved(function (Subscription $sub) {
diff --git a/app/Providers/EventServiceProvider.php b/app/Providers/EventServiceProvider.php
index a894023c0..ab6ba28db 100644
--- a/app/Providers/EventServiceProvider.php
+++ b/app/Providers/EventServiceProvider.php
@@ -5,9 +5,11 @@ namespace App\Providers;
use App\Events\Forms\FormSubmitted;
use App\Events\Models\FormCreated;
use App\Events\Models\FormIntegrationsEventCreated;
+use App\Events\SubscriptionCreated;
use App\Listeners\Forms\FormCreationConfirmation;
use App\Listeners\Forms\FormIntegrationsEventListener;
use App\Listeners\Forms\NotifyFormSubmission;
+use App\Listeners\HandleSubscriptionCreated;
use Illuminate\Auth\Events\Registered;
use Illuminate\Auth\Listeners\SendEmailVerificationNotification;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
@@ -32,6 +34,9 @@ class EventServiceProvider extends ServiceProvider
FormIntegrationsEventCreated::class => [
FormIntegrationsEventListener::class,
],
+ SubscriptionCreated::class => [
+ HandleSubscriptionCreated::class
+ ],
];
/**
diff --git a/client/pages/forms/[slug]/edit.vue b/client/pages/forms/[slug]/edit.vue
index 9b9a443ad..09a2a9970 100644
--- a/client/pages/forms/[slug]/edit.vue
+++ b/client/pages/forms/[slug]/edit.vue
@@ -69,6 +69,7 @@ function initUpdatedForm() {
watch(form, (form) => {
if (form?.value) {
initUpdatedForm()
+
}
})
diff --git a/client/pages/forms/create/index.vue b/client/pages/forms/create/index.vue
index bc35e4280..9979b7208 100644
--- a/client/pages/forms/create/index.vue
+++ b/client/pages/forms/create/index.vue
@@ -99,7 +99,7 @@ onMounted(() => {
formStore.loadAll(workspace.value.id)
}
- form.value = initForm({ workspace_id: workspace.value?.id }, true)
+ form.value = initForm({ workspace_id: workspace.value?.id, no_branding: workspace.value?.is_pro }, true)
formInitialHash.value = hash(JSON.stringify(form.value.data()))
if (route.query.template !== undefined && route.query.template) {
const template = templatesStore.getByKey(route.query.template)
diff --git a/tests/Feature/Forms/HideBrandingOnUpgradeTest.php b/tests/Feature/Forms/HideBrandingOnUpgradeTest.php
new file mode 100644
index 000000000..62b459f58
--- /dev/null
+++ b/tests/Feature/Forms/HideBrandingOnUpgradeTest.php
@@ -0,0 +1,35 @@
+<?php
+
+use Illuminate\Support\Str;
+
+it('can hide branding on upgrade', function () {
+ $user = $this->actingAsUser();
+ // Create workspaces and forms
+ for ($i = 0; $i < 3; $i++) {
+ $workspace = $this->createUserWorkspace($user);
+ for ($j = 0; $j < 3; $j++) {
+ $this->createForm($user, $workspace);
+ }
+ }
+
+ // Forms don't have branding removed when created
+ $forms = $user->workspaces()->with('forms')->get()->pluck('forms')->flatten();
+ $forms->each(function ($form) {
+ $this->assertEquals($form->no_branding, false);
+ });
+
+ // User subscribes
+ $user->subscriptions()->create([
+ 'name' => 'default',
+ 'stripe_id' => Str::random(),
+ 'stripe_status' => 'active',
+ 'stripe_price' => Str::random(),
+ 'quantity' => 1,
+ ]);
+
+ // Forms have branding removed after subscription
+ $forms = $user->workspaces()->with('forms')->get()->pluck('forms')->flatten();
+ $forms->each(function ($form) {
+ $this->assertEquals($form->no_branding, true);
+ });
+});