2024-02-08 19:55:35 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2022. Invoice Ninja LLC (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Livewire\BillingPortal;
|
|
|
|
|
|
|
|
use App\Libraries\MultiDB;
|
2024-02-29 17:36:32 +01:00
|
|
|
use App\Livewire\BillingPortal\Authentication\Login;
|
|
|
|
use App\Livewire\BillingPortal\Authentication\Register;
|
2024-02-28 17:55:37 +01:00
|
|
|
use App\Livewire\BillingPortal\Authentication\RegisterOrLogin;
|
2024-02-12 18:55:03 +01:00
|
|
|
use App\Livewire\BillingPortal\Cart\Cart;
|
2024-02-15 19:33:07 +01:00
|
|
|
use App\Livewire\BillingPortal\Payments\Methods;
|
2024-02-08 19:55:35 +01:00
|
|
|
use App\Models\Subscription;
|
|
|
|
use Livewire\Attributes\Computed;
|
|
|
|
use Livewire\Attributes\On;
|
|
|
|
use Livewire\Component;
|
2024-02-14 18:29:48 +01:00
|
|
|
use Illuminate\Support\Str;
|
2024-02-08 19:55:35 +01:00
|
|
|
|
|
|
|
class Purchase extends Component
|
|
|
|
{
|
|
|
|
public Subscription $subscription;
|
|
|
|
|
|
|
|
public string $db;
|
|
|
|
|
2024-02-09 17:53:38 +01:00
|
|
|
public array $request_data;
|
|
|
|
|
2024-02-12 18:55:03 +01:00
|
|
|
public string $hash;
|
|
|
|
|
2024-02-09 17:53:38 +01:00
|
|
|
public ?string $campaign;
|
|
|
|
|
|
|
|
//
|
|
|
|
|
2024-02-28 18:56:46 +01:00
|
|
|
public int $step = 0;
|
|
|
|
|
|
|
|
public string $id;
|
|
|
|
|
2024-02-28 17:55:37 +01:00
|
|
|
public static array $dependencies = [
|
2024-02-28 19:02:08 +01:00
|
|
|
Login::class => [
|
|
|
|
'id' => 'auth.login',
|
|
|
|
'dependencies' => [],
|
|
|
|
],
|
|
|
|
RegisterOrLogin::class => [
|
|
|
|
'id' => 'auth.login-or-register',
|
|
|
|
'dependencies' => [],
|
|
|
|
],
|
|
|
|
Register::class => [
|
|
|
|
'id' => 'auth.register',
|
|
|
|
'dependencies' => [],
|
|
|
|
],
|
|
|
|
Cart::class => [
|
|
|
|
'id' => 'cart',
|
|
|
|
'dependencies' => [],
|
|
|
|
],
|
|
|
|
Methods::class => [
|
|
|
|
'id' => 'methods',
|
|
|
|
'dependencies' => [Login::class, RegisterOrLogin::class, Register::class, RFF::class],
|
|
|
|
],
|
|
|
|
RFF::class => [
|
|
|
|
'id' => 'rff',
|
|
|
|
'dependencies' => [Login::class, RegisterOrLogin::class, Register::class],
|
|
|
|
],
|
2024-02-29 17:36:32 +01:00
|
|
|
Submit::class => [
|
|
|
|
'id' => 'submit',
|
|
|
|
'dependencies' => [Methods::class],
|
|
|
|
],
|
2024-02-28 17:55:37 +01:00
|
|
|
];
|
|
|
|
|
2024-02-29 17:36:32 +01:00
|
|
|
public array $steps = [];
|
2024-02-08 19:55:35 +01:00
|
|
|
|
|
|
|
public array $context = [];
|
|
|
|
|
|
|
|
#[On('purchase.context')]
|
2024-02-12 18:55:03 +01:00
|
|
|
public function handleContext(string $property, $value): self
|
2024-02-08 19:55:35 +01:00
|
|
|
{
|
2024-02-16 17:42:26 +01:00
|
|
|
$clone = $this->context;
|
|
|
|
|
2024-02-14 18:29:48 +01:00
|
|
|
data_set($this->context, $property, $value);
|
2024-02-12 18:55:03 +01:00
|
|
|
|
|
|
|
// The following may not be needed, as we can pass arround $context.
|
|
|
|
// cache()->set($this->hash, $this->context);
|
|
|
|
|
2024-02-16 17:42:26 +01:00
|
|
|
if ($clone !== $this->context) {
|
|
|
|
$this->id = Str::uuid();
|
|
|
|
}
|
2024-02-14 18:29:48 +01:00
|
|
|
|
2024-02-12 18:55:03 +01:00
|
|
|
return $this;
|
2024-02-08 19:55:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[On('purchase.next')]
|
|
|
|
public function handleNext(): void
|
|
|
|
{
|
2024-02-29 17:36:32 +01:00
|
|
|
if (count($this->steps) >= 1 && $this->step < count($this->steps) - 1) {
|
2024-02-08 19:55:35 +01:00
|
|
|
$this->step++;
|
2024-02-29 17:36:32 +01:00
|
|
|
$this->id = Str::uuid();
|
2024-02-08 19:55:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[On('purchase.forward')]
|
|
|
|
public function handleForward(string $component): void
|
|
|
|
{
|
|
|
|
$this->step = array_search($component, $this->steps);
|
2024-02-14 18:29:48 +01:00
|
|
|
|
|
|
|
$this->id = Str::uuid();
|
2024-02-08 19:55:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[Computed()]
|
|
|
|
public function component(): string
|
|
|
|
{
|
|
|
|
return $this->steps[$this->step];
|
|
|
|
}
|
|
|
|
|
2024-02-16 17:42:26 +01:00
|
|
|
#[Computed()]
|
|
|
|
public function componentUniqueId(): string
|
|
|
|
{
|
|
|
|
return "purchase-{$this->id}";
|
|
|
|
}
|
|
|
|
|
|
|
|
#[Computed()]
|
|
|
|
public function summaryUniqueId(): string
|
|
|
|
{
|
|
|
|
return "summary-{$this->id}";
|
|
|
|
}
|
|
|
|
|
2024-02-29 17:36:32 +01:00
|
|
|
public static function defaultSteps() {
|
|
|
|
return [
|
|
|
|
Setup::class,
|
|
|
|
Cart::class,
|
|
|
|
RegisterOrLogin::class,
|
|
|
|
Methods::class,
|
|
|
|
Submit::class,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2024-02-08 19:55:35 +01:00
|
|
|
public function mount()
|
|
|
|
{
|
2024-02-29 17:36:32 +01:00
|
|
|
$classes = collect(self::$dependencies)->mapWithKeys(fn($dependency, $class) => [$dependency['id'] => $class])->toArray();
|
|
|
|
|
|
|
|
if ($this->subscription->steps) {
|
|
|
|
$steps = collect(explode(',', $this->subscription->steps))
|
|
|
|
->map(fn($step) => $classes[$step])
|
|
|
|
->toArray();
|
|
|
|
|
|
|
|
$this->steps = [
|
|
|
|
Setup::class,
|
|
|
|
...$steps,
|
|
|
|
];
|
|
|
|
} else {
|
|
|
|
$this->steps = self::defaultSteps();
|
|
|
|
}
|
|
|
|
|
2024-02-14 18:29:48 +01:00
|
|
|
$this->id = Str::uuid();
|
|
|
|
|
2024-02-08 19:55:35 +01:00
|
|
|
MultiDB::setDb($this->db);
|
|
|
|
|
2024-02-12 18:55:03 +01:00
|
|
|
$this
|
|
|
|
->handleContext('hash', $this->hash)
|
|
|
|
->handleContext('quantity', 1)
|
|
|
|
->handleContext('request_data', $this->request_data)
|
|
|
|
->handleContext('campaign', $this->campaign);
|
2024-02-08 19:55:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function render()
|
|
|
|
{
|
|
|
|
return view('billing-portal.v3.purchase');
|
|
|
|
}
|
|
|
|
}
|