1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 07:02:34 +01:00
invoiceninja/app/Http/Livewire/BillingPortalPurchase.php

516 lines
14 KiB
PHP
Raw Normal View History

2021-03-18 01:58:10 +01:00
<?php
2021-03-18 01:53:08 +01:00
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2022-04-27 05:20:41 +02:00
* @copyright Copyright (c) 2022. Invoice Ninja LLC (https://invoiceninja.com)
2021-03-18 01:53:08 +01:00
*
2021-06-16 08:58:16 +02:00
* @license https://www.elastic.co/licensing/elastic-license
2021-03-18 01:53:08 +01:00
*/
2021-03-18 01:58:10 +01:00
namespace App\Http\Livewire;
use App\Factory\ClientFactory;
2021-03-31 18:10:44 +02:00
use App\Jobs\Mail\NinjaMailerJob;
use App\Jobs\Mail\NinjaMailerObject;
2021-06-03 01:50:31 +02:00
use App\Libraries\MultiDB;
2021-03-31 18:10:44 +02:00
use App\Mail\ContactPasswordlessLogin;
use App\Models\Client;
2021-03-18 01:58:10 +01:00
use App\Models\ClientContact;
2021-03-18 14:14:10 +01:00
use App\Models\Invoice;
2021-06-03 01:50:31 +02:00
use App\Models\Subscription;
2021-03-18 01:58:10 +01:00
use App\Repositories\ClientContactRepository;
use App\Repositories\ClientRepository;
use Illuminate\Support\Facades\App;
2021-03-18 01:58:10 +01:00
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
2021-11-08 07:35:15 +01:00
use Illuminate\Support\Str;
use App\DataMapper\ClientSettings;
2021-03-18 01:58:10 +01:00
use Livewire\Component;
class BillingPortalPurchase extends Component
{
2021-03-18 14:14:10 +01:00
/**
* Random hash generated by backend to handle the tracking of state.
*
* @var string
*/
2021-03-18 01:58:10 +01:00
public $hash;
2021-03-18 14:14:10 +01:00
/**
* Top level text on the left side of billing page.
*
* @var string
*/
public $heading_text;
2021-03-18 01:58:10 +01:00
2021-03-18 14:14:10 +01:00
/**
* E-mail address model for user input.
*
* @var string
*/
2021-03-18 01:58:10 +01:00
public $email;
2021-03-18 14:14:10 +01:00
/**
* Password model for user input.
*
* @var string
*/
2021-03-18 01:58:10 +01:00
public $password;
2021-03-18 14:14:10 +01:00
/**
* Instance of subscription.
2021-03-18 14:14:10 +01:00
*
* @var Subscription
2021-03-18 14:14:10 +01:00
*/
public $subscription;
2021-03-18 01:58:10 +01:00
2021-03-18 14:14:10 +01:00
/**
* Instance of client contact.
*
* @var null|ClientContact
*/
2021-03-18 01:58:10 +01:00
public $contact;
2021-03-18 14:14:10 +01:00
/**
* Rules for validating the form.
*
* @var \string[][]
*/
2021-03-18 01:58:10 +01:00
protected $rules = [
'email' => ['required', 'email'],
];
2021-03-18 14:14:10 +01:00
/**
* Id for CompanyGateway record.
*
* @var string|integer
*/
2021-03-18 01:58:10 +01:00
public $company_gateway_id;
2021-03-18 14:14:10 +01:00
/**
* Id for GatewayType.
*
* @var string|integer
*/
2021-03-18 01:58:10 +01:00
public $payment_method_id;
2022-08-10 03:56:46 +02:00
private $user_coupon;
2021-03-18 14:14:10 +01:00
/**
* List of steps that frontend form follows.
*
* @var array
*/
2021-03-18 01:58:10 +01:00
public $steps = [
'passed_email' => false,
'existing_user' => false,
'fetched_payment_methods' => false,
'fetched_client' => false,
2021-03-18 14:14:10 +01:00
'show_start_trial' => false,
2021-03-31 18:10:44 +02:00
'passwordless_login_sent' => false,
'started_payment' => false,
2021-04-01 17:02:11 +02:00
'discount_applied' => false,
'show_loading_bar' => false,
'not_eligible' => null,
2021-04-10 13:56:29 +02:00
'not_eligible_message' => null,
2021-04-14 06:41:04 +02:00
'payment_required' => true,
2021-03-18 01:58:10 +01:00
];
2021-03-18 14:14:10 +01:00
/**
* List of payment methods fetched from client.
*
* @var array
*/
2021-03-18 01:58:10 +01:00
public $methods = [];
2021-03-18 14:14:10 +01:00
/**
* Instance of \App\Models\Invoice
*
* @var Invoice
*/
2021-03-18 01:58:10 +01:00
public $invoice;
2021-03-18 14:14:10 +01:00
/**
* Coupon model for user input
*
* @var string
*/
2021-03-18 01:58:10 +01:00
public $coupon;
2021-03-18 15:22:22 +01:00
/**
* Quantity for seats
*
* @var int
*/
2022-07-06 08:18:16 +02:00
public $quantity;
2021-03-18 15:22:22 +01:00
2021-03-18 15:41:26 +01:00
/**
* First-hit request data (queries, locales...).
*
* @var array
*/
public $request_data;
2021-03-29 15:50:36 +02:00
/**
* @var string
*/
public $price;
2021-03-31 18:10:44 +02:00
/**
* Disabled state of passwordless login button.
*
* @var bool
*/
public $passwordless_login_btn = false;
2021-06-03 01:50:31 +02:00
/**
* Instance of company.
*
* @var Company
*/
2021-06-03 03:43:14 +02:00
public $company;
2021-06-03 01:50:31 +02:00
/**
* Campaign reference.
*
* @var string|null
*/
public $campaign;
2021-03-29 15:50:36 +02:00
public function mount()
{
2021-06-03 01:50:31 +02:00
MultiDB::setDb($this->company->db);
2022-07-06 08:18:16 +02:00
$this->quantity = 1;
$this->price = $this->subscription->price;
2021-04-01 16:20:31 +02:00
if (request()->query('coupon')) {
$this->coupon = request()->query('coupon');
$this->handleCoupon();
}
2021-11-17 19:53:01 +01:00
elseif(strlen($this->subscription->promo_code) == 0 && $this->subscription->promo_discount > 0){
$this->price = $this->subscription->promo_price;
}
2021-03-29 15:50:36 +02:00
}
2021-03-18 14:14:10 +01:00
/**
* Handle user authentication
*
* @return $this|bool|void
*/
2021-03-18 01:58:10 +01:00
public function authenticate()
{
$this->validate();
2021-05-24 00:25:14 +02:00
$contact = ClientContact::where('email', $this->email)
->where('company_id', $this->subscription->company_id)
->first();
2021-03-18 01:58:10 +01:00
if ($contact && $this->steps['existing_user'] === false) {
return $this->steps['existing_user'] = true;
}
if ($contact && $this->steps['existing_user']) {
2021-05-24 00:25:14 +02:00
$attempt = Auth::guard('contact')->attempt(['email' => $this->email, 'password' => $this->password, 'company_id' => $this->subscription->company_id]);
2021-03-18 01:58:10 +01:00
return $attempt
? $this->getPaymentMethods($contact)
: session()->flash('message', 'These credentials do not match our records.');
}
$this->steps['existing_user'] = false;
$contact = $this->createBlankClient();
if ($contact && $contact instanceof ClientContact) {
$this->getPaymentMethods($contact);
}
}
2021-03-18 14:14:10 +01:00
/**
* Create a blank client. Used for new customers purchasing.
*
* @return mixed
* @throws \Laracasts\Presenter\Exceptions\PresenterException
*/
2021-03-18 01:58:10 +01:00
protected function createBlankClient()
{
$company = $this->subscription->company;
$user = $this->subscription->user;
2022-03-29 23:05:42 +02:00
$user->setCompany($company);
2021-03-18 01:58:10 +01:00
$client_repo = new ClientRepository(new ClientContactRepository());
$data = [
2021-06-29 11:46:40 +02:00
'name' => '',
2021-03-18 01:58:10 +01:00
'contacts' => [
['email' => $this->email],
],
2021-11-08 07:35:15 +01:00
'client_hash' => Str::random(40),
'settings' => ClientSettings::defaults(),
];
foreach ($this->request_data as $field => $value) {
if (in_array($field, Client::$subscriptions_fillable)) {
$data[$field] = $value;
}
if (in_array($field, ClientContact::$subscription_fillable)) {
$data['contacts'][0][$field] = $value;
}
}
if(array_key_exists('currency_id', $this->request_data)) {
$currency = Cache::get('currencies')->filter(function ($item){
return $item->id == $this->request_data['currency_id'];
})->first();
if($currency)
$data['settings']->currency_id = $currency->id;
}
if (array_key_exists('locale', $this->request_data)) {
$request = $this->request_data;
$record = Cache::get('languages')->filter(function ($item) use ($request) {
return $item->locale == $request['locale'];
})->first();
if ($record) {
$data['settings']['language_id'] = (string)$record->id;
}
}
$client = $client_repo->save($data, ClientFactory::create($company->id, $user->id));
2021-03-18 01:58:10 +01:00
2021-10-13 00:10:44 +02:00
return $client->fresh()->contacts->first();
2021-03-18 01:58:10 +01:00
}
2021-03-18 14:14:10 +01:00
/**
* Fetching payment methods from the client.
*
* @param ClientContact $contact
* @return $this
*/
2021-03-18 01:58:10 +01:00
protected function getPaymentMethods(ClientContact $contact): self
{
Auth::guard('contact')->loginUsingId($contact->id, true);
2021-03-29 12:25:29 +02:00
$this->contact = $contact;
if ($this->subscription->trial_enabled) {
2021-03-18 14:14:10 +01:00
$this->heading_text = ctrans('texts.plan_trial');
$this->steps['show_start_trial'] = true;
return $this;
}
2021-11-10 09:49:26 +01:00
if ((int)$this->price == 0)
2021-04-14 06:41:04 +02:00
$this->steps['payment_required'] = false;
else
$this->steps['fetched_payment_methods'] = true;
2021-03-18 01:58:10 +01:00
2021-04-07 18:08:26 +02:00
$this->methods = $contact->client->service()->getPaymentMethods($this->price);
2021-03-18 01:58:10 +01:00
2021-03-18 14:14:10 +01:00
$this->heading_text = ctrans('texts.payment_methods');
2021-03-18 01:58:10 +01:00
return $this;
}
2021-03-18 14:14:10 +01:00
/**
* Middle method between selecting payment method &
* submitting the from to the backend.
*
* @param $company_gateway_id
* @param $gateway_type_id
*/
2021-03-18 01:58:10 +01:00
public function handleMethodSelectingEvent($company_gateway_id, $gateway_type_id)
{
$this->company_gateway_id = $company_gateway_id;
$this->payment_method_id = $gateway_type_id;
$this->handleBeforePaymentEvents();
}
2021-03-18 14:14:10 +01:00
/**
* Method to handle events before payments.
*
* @return void
*/
2021-03-18 01:58:10 +01:00
public function handleBeforePaymentEvents()
{
$this->steps['started_payment'] = true;
$this->steps['show_loading_bar'] = true;
2021-03-18 01:58:10 +01:00
$data = [
'client_id' => $this->contact->client->id,
'date' => now()->format('Y-m-d'),
'invitations' => [[
'key' => '',
'client_contact_id' => $this->contact->hashed_id,
]],
'user_input_promo_code' => $this->coupon,
'coupon' => empty($this->subscription->promo_code) ? '' : $this->coupon,
2021-03-18 15:22:22 +01:00
'quantity' => $this->quantity,
2021-03-18 01:58:10 +01:00
];
$is_eligible = $this->subscription->service()->isEligible($this->contact);
2021-06-03 23:57:24 +02:00
if (is_array($is_eligible) && $is_eligible['message'] != 'Success') {
$this->steps['not_eligible'] = true;
2021-06-03 23:57:24 +02:00
$this->steps['not_eligible_message'] = $is_eligible['message'];
$this->steps['show_loading_bar'] = false;
return;
}
2021-11-25 01:16:47 +01:00
$this->invoice = $this->subscription
->service()
2022-07-06 09:12:29 +02:00
->createInvoice($data, $this->quantity)
2021-11-25 01:16:47 +01:00
->service()
->markSent()
->fillDefaults()
2022-06-08 12:40:26 +02:00
->adjustInventory()
2021-11-25 01:16:47 +01:00
->save();
2021-03-18 01:58:10 +01:00
Cache::put($this->hash, [
'subscription_id' => $this->subscription->id,
'email' => $this->email ?? $this->contact->email,
'client_id' => $this->contact->client->id,
'invoice_id' => $this->invoice->id,
'context' => 'purchase',
'campaign' => $this->campaign,
], now()->addMinutes(60));
2021-03-18 01:58:10 +01:00
$this->emit('beforePaymentEventsCompleted');
}
2021-03-18 14:14:10 +01:00
/**
* Proxy method for starting the trial.
*
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*/
public function handleTrial()
{
return $this->subscription->service()->startTrial([
2021-03-18 14:14:10 +01:00
'email' => $this->email ?? $this->contact->email,
2021-03-22 14:03:32 +01:00
'quantity' => $this->quantity,
'contact_id' => $this->contact->id,
2021-04-14 06:41:04 +02:00
'client_id' => $this->contact->client->id,
]);
}
public function handlePaymentNotRequired()
{
$is_eligible = $this->subscription->service()->isEligible($this->contact);
2021-06-02 10:55:33 +02:00
2021-06-03 23:43:22 +02:00
if ($is_eligible['status_code'] != 200) {
2021-04-14 06:41:04 +02:00
$this->steps['not_eligible'] = true;
2021-06-03 23:43:22 +02:00
$this->steps['not_eligible_message'] = $is_eligible['message'];
2021-04-14 06:41:04 +02:00
$this->steps['show_loading_bar'] = false;
return;
}
return $this->subscription->service()->handleNoPaymentRequired([
'email' => $this->email ?? $this->contact->email,
'quantity' => $this->quantity,
'contact_id' => $this->contact->id,
'client_id' => $this->contact->client->id,
'coupon' => $this->coupon,
2021-03-18 14:14:10 +01:00
]);
}
2021-03-18 02:00:01 +01:00
2021-03-18 15:22:22 +01:00
/**
* Update quantity property.
*
* @param string $option
* @return int
*/
public function updateQuantity(string $option): int
2021-03-18 01:58:10 +01:00
{
2022-08-10 03:56:46 +02:00
$this->handleCoupon();
2021-03-18 15:22:22 +01:00
if ($this->quantity == 1 && $option == 'decrement') {
2022-08-10 03:56:46 +02:00
$this->price = $this->price * 1;
2021-03-18 15:22:22 +01:00
return $this->quantity;
}
2022-08-10 03:56:46 +02:00
if ($this->quantity > $this->subscription->max_seats_limit && $option == 'increment') {
$this->price = $this->price * $this->subscription->max_seats_limit;
2021-03-18 15:22:22 +01:00
return $this->quantity;
}
2021-03-22 13:53:34 +01:00
if ($option == 'increment') {
$this->quantity++;
2022-08-10 03:56:46 +02:00
$this->price = $this->price * $this->quantity;
2022-07-06 08:18:16 +02:00
return $this->quantity;
2021-03-22 13:53:34 +01:00
}
2022-07-06 08:18:16 +02:00
$this->quantity--;
2022-08-10 03:56:46 +02:00
$this->price = $this->price * $this->quantity;
2021-03-22 13:53:34 +01:00
2022-07-06 08:18:16 +02:00
return $this->quantity;
2021-03-18 01:58:10 +01:00
}
2021-03-29 15:50:36 +02:00
public function handleCoupon()
{
2022-08-10 03:56:46 +02:00
if($this->steps['discount_applied']){
$this->price = $this->subscription->promo_price;
return;
}
2021-03-29 15:50:36 +02:00
if ($this->coupon == $this->subscription->promo_code) {
$this->price = $this->subscription->promo_price;
2022-08-10 03:56:46 +02:00
$this->quantity = 1;
2021-04-01 17:02:11 +02:00
$this->steps['discount_applied'] = true;
2021-03-29 15:50:36 +02:00
}
2022-08-10 03:56:46 +02:00
else
$this->price = $this->subscription->price;
2021-03-29 15:50:36 +02:00
}
2021-03-31 18:10:44 +02:00
public function passwordlessLogin()
{
$this->passwordless_login_btn = true;
$contact = ClientContact::query()
->where('email', $this->email)
2022-01-24 00:29:47 +01:00
->where('company_id', $this->subscription->company_id)
2021-03-31 18:10:44 +02:00
->first();
$mailer = new NinjaMailerObject();
2021-06-09 17:07:41 +02:00
$mailer->mailable = new ContactPasswordlessLogin($this->email, $this->subscription->company, (string)route('client.subscription.purchase', $this->subscription->hashed_id) . '?coupon=' . $this->coupon);
2021-03-31 18:10:44 +02:00
$mailer->company = $this->subscription->company;
$mailer->settings = $this->subscription->company->settings;
$mailer->to_user = $contact;
NinjaMailerJob::dispatchNow($mailer);
$this->steps['passwordless_login_sent'] = true;
$this->passwordless_login_btn = false;
}
2021-03-18 01:58:10 +01:00
public function render()
{
if (array_key_exists('email', $this->request_data)) {
$this->email = $this->request_data['email'];
}
2021-03-18 01:58:10 +01:00
if ($this->contact instanceof ClientContact) {
$this->getPaymentMethods($this->contact);
}
return render('components.livewire.billing-portal-purchase');
}
}