Merge pull request #8077 from turbo124/v5-develop

Attach invoice documents to payment emails
This commit is contained in:
David Bomba 2022-12-15 17:00:09 +11:00 committed by GitHub
commit d7d9239a24
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 73 additions and 166 deletions

View File

@ -33,6 +33,8 @@ use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator; use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Str; use Illuminate\Support\Str;
use Illuminate\Validation\Rule; use Illuminate\Validation\Rule;
use Laracasts\Presenter\Exceptions\PresenterException;
use InvalidArgumentException;
use Livewire\Component; use Livewire\Component;
class BillingPortalPurchasev2 extends Component class BillingPortalPurchasev2 extends Component
@ -51,13 +53,6 @@ class BillingPortalPurchasev2 extends Component
*/ */
public $email; public $email;
/**
* Password model for user input.
*
* @var string
*/
public $password;
/** /**
* Instance of subscription. * Instance of subscription.
* *
@ -72,19 +67,6 @@ class BillingPortalPurchasev2 extends Component
*/ */
public $contact; public $contact;
/**
* Rules for validating the form.
*
* @var \string[][]
*/
// protected $rules = [
// 'email' => ['required', 'email'],
// 'data' => ['required', 'array'],
// 'data.*.recurring_qty' => ['required', 'between:100,1000'],
// 'data.*.optional_recurring_qty' => ['required', 'between:100,1000'],
// 'data.*.optional_qty' => ['required', 'between:100,1000'],
// ];
/** /**
* Id for CompanyGateway record. * Id for CompanyGateway record.
* *
@ -99,27 +81,10 @@ class BillingPortalPurchasev2 extends Component
*/ */
public $payment_method_id; public $payment_method_id;
/** /**
* List of steps that frontend form follows. * Array of front end variables for
* * the subscription
* @var array
*/ */
public $steps = [
'passed_email' => false,
'existing_user' => false,
'fetched_payment_methods' => false,
'fetched_client' => false,
'show_start_trial' => false,
'passwordless_login_sent' => false,
'started_payment' => false,
'discount_applied' => false,
'show_loading_bar' => false,
'not_eligible' => null,
'not_eligible_message' => null,
'payment_required' => true,
];
public $data = []; public $data = [];
/** /**
@ -157,18 +122,6 @@ class BillingPortalPurchasev2 extends Component
*/ */
public $request_data; public $request_data;
/**
* @var string
*/
public $price;
/**
* Disabled state of passwordless login button.
*
* @var bool
*/
public $passwordless_login_btn = false;
/** /**
* Instance of company. * Instance of company.
* *
@ -197,6 +150,7 @@ class BillingPortalPurchasev2 extends Component
public $payment_started = false; public $payment_started = false;
public $valid_coupon = false; public $valid_coupon = false;
public $payable_invoices = []; public $payable_invoices = [];
public $payment_confirmed = false;
public function mount() public function mount()
{ {
@ -249,7 +203,9 @@ class BillingPortalPurchasev2 extends Component
return $this; return $this;
} }
$contact = ClientContact::where('email', $this->email)->first(); $contact = ClientContact::where('email', $this->email)
->where('company_id', $this->subscription->company_id)
->first();
if($contact){ if($contact){
Auth::guard('contact')->loginUsingId($contact->id, true); Auth::guard('contact')->loginUsingId($contact->id, true);
@ -259,14 +215,10 @@ class BillingPortalPurchasev2 extends Component
$this->createClientContact(); $this->createClientContact();
} }
$this->authenticated = true;
$this->getPaymentMethods(); $this->getPaymentMethods();
} $this->authenticated = true;
$this->payment_started = true;
public function showClientRequiredFields()
{
} }
@ -303,7 +255,6 @@ class BillingPortalPurchasev2 extends Component
} }
/** /**
* Handle a coupon being entered into the checkout * Handle a coupon being entered into the checkout
*/ */
@ -455,6 +406,11 @@ class BillingPortalPurchasev2 extends Component
return $this; return $this;
} }
/**
* @return $this
* @throws PresenterException
* @throws InvalidArgumentException
*/
private function createClientContact() private function createClientContact()
{ {
@ -475,19 +431,26 @@ class BillingPortalPurchasev2 extends Component
$client = $client_repo->save($data, ClientFactory::create($company->id, $user->id)); $client = $client_repo->save($data, ClientFactory::create($company->id, $user->id));
$this->contact = $client->fresh()->contacts()->first(); $this->contact = $client->fresh()->contacts()->first();
Auth::guard('contact')->loginUsingId($this->contact->id, true); Auth::guard('contact')->loginUsingId($this->contact->id, true);
return $this; return $this;
} }
public function updated($propertyName) /**
* @param mixed $propertyName
*
* @return BillingPortalPurchasev2
*/
public function updated($propertyName) :self
{ {
if(in_array($propertyName, ['login','email'])) if(in_array($propertyName, ['login','email']))
return; return $this;
$this->buildBundle(); $this->buildBundle();
return $this;
} }
/** /**
@ -495,9 +458,10 @@ class BillingPortalPurchasev2 extends Component
* *
* @return $this * @return $this
*/ */
protected function getPaymentMethods(): self protected function getPaymentMethods() :self
{ {
$this->methods = $this->contact->client->service()->getPaymentMethods($this->float_amount_total); if($this->contact)
$this->methods = $this->contact->client->service()->getPaymentMethods($this->float_amount_total);
return $this; return $this;
} }
@ -511,6 +475,7 @@ class BillingPortalPurchasev2 extends Component
*/ */
public function handleMethodSelectingEvent($company_gateway_id, $gateway_type_id) public function handleMethodSelectingEvent($company_gateway_id, $gateway_type_id)
{ {
$this->payment_confirmed = true;
$this->company_gateway_id = $company_gateway_id; $this->company_gateway_id = $company_gateway_id;
$this->payment_method_id = $gateway_type_id; $this->payment_method_id = $gateway_type_id;
@ -525,7 +490,6 @@ class BillingPortalPurchasev2 extends Component
*/ */
public function handleBeforePaymentEvents() :void public function handleBeforePaymentEvents() :void
{ {
$this->payment_started = true;
$data = [ $data = [
'client_id' => $this->contact->client->id, 'client_id' => $this->contact->client->id,
@ -722,58 +686,6 @@ class BillingPortalPurchasev2 extends Component
* *
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*/ */
public function handleTrial()
{
return $this->subscription->service()->startTrial([
'email' => $this->email ?? $this->contact->email,
'quantity' => $this->quantity,
'contact_id' => $this->contact->id,
'client_id' => $this->contact->client->id,
]);
}
public function handlePaymentNotRequired()
{
$is_eligible = $this->subscription->service()->isEligible($this->contact);
if ($is_eligible['status_code'] != 200) {
$this->steps['not_eligible'] = true;
$this->steps['not_eligible_message'] = $is_eligible['message'];
$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,
]);
}
public function passwordlessLogin()
{
$this->passwordless_login_btn = true;
$contact = ClientContact::query()
->where('email', $this->email)
->where('company_id', $this->subscription->company_id)
->first();
$mailer = new NinjaMailerObject();
$mailer->mailable = new ContactPasswordlessLogin($this->email, $this->subscription->company, (string)route('client.subscription.purchase', $this->subscription->hashed_id) . '?coupon=' . $this->coupon);
$mailer->company = $this->subscription->company;
$mailer->settings = $this->subscription->company->settings;
$mailer->to_user = $contact;
NinjaMailerJob::dispatch($mailer);
$this->steps['passwordless_login_sent'] = true;
$this->passwordless_login_btn = false;
}
public function render() public function render()
{ {
@ -782,16 +694,10 @@ class BillingPortalPurchasev2 extends Component
} }
if ($this->contact instanceof ClientContact) { if ($this->contact instanceof ClientContact) {
$this->getPaymentMethods($this->contact); $this->getPaymentMethods();
} }
return render('components.livewire.billing-portal-purchasev2'); return render('components.livewire.billing-portal-purchasev2');
} }
// public function changeData()
// {
// nlog($this->data);
// }
} }

View File

@ -96,11 +96,17 @@ class PaymentEmailEngine extends BaseEmailEngine
$this->setAttachments([['file' => base64_encode($pdf), 'name' => $invoice->numberFormatter().'.pdf']]); $this->setAttachments([['file' => base64_encode($pdf), 'name' => $invoice->numberFormatter().'.pdf']]);
//attach invoice documents also to payments
if ($this->client->getSetting('document_email_attachment') !== false)
{
foreach ($invoice->documents as $document) {
$this->setAttachments([['path' => $document->filePath(), 'name' => $document->name, 'mime' => NULL, ]]);
}
}
}); });
// foreach ($this->payment->documents as $document) {
// $this->setAttachments([['path' => $document->filePath(), 'name' => $document->name, 'mime' => NULL, ]]);
// }
} }
return $this; return $this;

View File

@ -229,6 +229,6 @@ class BrowserPay implements MethodInterface
$domain = config('ninja.app_url'); $domain = config('ninja.app_url');
} }
return str_replace('https://', '', $domain); return str_replace(['https://', '/public'], '', $domain);
} }
} }

View File

@ -8,28 +8,27 @@
</h1> </h1>
</div> </div>
@if(isset($invoice))
<div class="flex items-center mt-4 text-sm">
<form action="{{ route('client.payments.process', ['hash' => $hash, 'sidebar' => 'hidden']) }}"
method="post"
id="payment-method-form">
@csrf
@if(isset($invoice)) @if($invoice instanceof \App\Models\Invoice)
<div class="flex items-center mt-4 text-sm"> <input type="hidden" name="invoices[]" value="{{ $invoice->hashed_id }}">
<form action="{{ route('client.payments.process', ['hash' => $hash, 'sidebar' => 'hidden']) }}" <input type="hidden" name="payable_invoices[0][amount]"
method="post" value="{{ $invoice->partial > 0 ? \App\Utils\Number::formatValue($invoice->partial, $invoice->client->currency()) : \App\Utils\Number::formatValue($invoice->balance, $invoice->client->currency()) }}">
id="payment-method-form"> <input type="hidden" name="payable_invoices[0][invoice_id]"
@csrf value="{{ $invoice->hashed_id }}">
@endif
@if($invoice instanceof \App\Models\Invoice) <input type="hidden" name="action" value="payment">
<input type="hidden" name="invoices[]" value="{{ $invoice->hashed_id }}"> <input type="hidden" name="company_gateway_id" value="{{ $company_gateway_id }}"/>
<input type="hidden" name="payable_invoices[0][amount]" <input type="hidden" name="payment_method_id" value="{{ $payment_method_id }}"/>
value="{{ $invoice->partial > 0 ? \App\Utils\Number::formatValue($invoice->partial, $invoice->client->currency()) : \App\Utils\Number::formatValue($invoice->balance, $invoice->client->currency()) }}"> </form>
<input type="hidden" name="payable_invoices[0][invoice_id]" </div>
value="{{ $invoice->hashed_id }}"> @endif
@endif
<input type="hidden" name="action" value="payment">
<input type="hidden" name="company_gateway_id" value="{{ $company_gateway_id }}"/>
<input type="hidden" name="payment_method_id" value="{{ $payment_method_id }}"/>
</form>
</div>
@endif
<form wire:submit.prevent="submit"> <form wire:submit.prevent="submit">
<!-- Recurring Plan Products--> <!-- Recurring Plan Products-->
@ -65,7 +64,6 @@
@endfor @endfor
</select> </select>
</div> </div>
@endif @endif
</div> </div>
@error("data.{$index}.recurring_qty") @error("data.{$index}.recurring_qty")
@ -83,7 +81,7 @@
@foreach($products as $product) @foreach($products as $product)
<li class="flex py-6"> <li class="flex py-6">
@if(filter_var($product->custom_value1, FILTER_VALIDATE_URL)) @if(filter_var($product->custom_value1, FILTER_VALIDATE_URL))
<div class="h-24 w-24 flex-shrink-0 overflow-hidden rounded-md border border-gray-200"> <div class="h-24 w-24 flex-shrink-0 overflow-hidden rounded-md border border-gray-200 mr-2">
<img src="{{$product->custom_value1}}" alt="" class="h-full w-full object-cover object-center"> <img src="{{$product->custom_value1}}" alt="" class="h-full w-full object-cover object-center">
</div> </div>
@endif @endif
@ -121,18 +119,17 @@
@if(!empty($subscription->optional_recurring_product_ids)) @if(!empty($subscription->optional_recurring_product_ids))
@foreach($optional_recurring_products as $index => $product) @foreach($optional_recurring_products as $index => $product)
<li class="flex py-6"> <li class="flex py-6">
@if(filter_var($product->custom_value1, FILTER_VALIDATE_URL)) @if(filter_var($product->custom_value1, FILTER_VALIDATE_URL))
<div class="h-24 w-24 flex-shrink-0 overflow-hidden rounded-md border border-gray-200"> <div class="h-24 w-24 flex-shrink-0 overflow-hidden rounded-md border border-gray-200 mr-2">
<img src="{{$product->custom_value1}}" alt="" class="h-full w-full object-cover object-center"> <img src="{{$product->custom_value1}}" alt="" class="h-full w-full object-cover object-center">
</div> </div>
@endif @endif
<div class="ml-0 flex flex-1 flex-col"> <div class="ml-0 flex flex-1 flex-col">
<div> <div>
<div class="flex justify-between text-base font-medium text-gray-900"> <div class="flex justify-between text-base font-medium text-gray-900">
<h3>{!! nl2br($product->notes) !!}</h3> <h3>{!! nl2br($product->notes) !!}</h3>
<p class="ml-0">{{ \App\Utils\Number::formatMoney($product->price, $subscription->company) }} </p> <p class="ml-0">{{ \App\Utils\Number::formatMoney($product->price, $subscription->company) }} / {{ App\Models\RecurringInvoice::frequencyForKey($subscription->frequency_id) }}</p>
</div> </div>
</div> </div>
<div class="flex justify-between text-sm mt-1"> <div class="flex justify-between text-sm mt-1">
@if(is_numeric($product->custom_value2)) @if(is_numeric($product->custom_value2))
@ -149,7 +146,7 @@
@endif @endif
> >
<option value="0" selected="selected">0</option> <option value="0" selected="selected">0</option>
@for ($i = 1; $i <= ($subscription->use_inventory_management ? min($product->in_stock_quantity,$product->custom_value2) : $product->custom_value2); $i++) @for ($i = 1; $i <= ($subscription->use_inventory_management ? min($product->in_stock_quantity, max(100,$product->custom_value2)) : max(100,$product->custom_value2)); $i++)
<option value="{{$i}}">{{$i}}</option> <option value="{{$i}}">{{$i}}</option>
@endfor @endfor
</select> </select>
@ -164,7 +161,7 @@
@foreach($optional_products as $index => $product) @foreach($optional_products as $index => $product)
<li class="flex py-6"> <li class="flex py-6">
@if(filter_var($product->custom_value1, FILTER_VALIDATE_URL)) @if(filter_var($product->custom_value1, FILTER_VALIDATE_URL))
<div class="h-24 w-24 flex-shrink-0 overflow-hidden rounded-md border border-gray-200"> <div class="h-24 w-24 flex-shrink-0 overflow-hidden rounded-md border border-gray-200 mr-2">
<img src="{{$product->custom_value1}}" alt="" class="h-full w-full object-cover object-center"> <img src="{{$product->custom_value1}}" alt="" class="h-full w-full object-cover object-center">
</div> </div>
@endif @endif
@ -187,7 +184,7 @@
@endif @endif
<select wire:model.debounce.300ms="data.{{ $index }}.optional_qty" class="rounded-md border-gray-300 shadow-sm sm:text-sm"> <select wire:model.debounce.300ms="data.{{ $index }}.optional_qty" class="rounded-md border-gray-300 shadow-sm sm:text-sm">
<option value="0" selected="selected">0</option> <option value="0" selected="selected">0</option>
@for ($i = 1; $i <= ($subscription->use_inventory_management ? min($product->in_stock_quantity,$product->custom_value2) : $product->custom_value2); $i++) @for ($i = 1; $i <= ($subscription->use_inventory_management ? min($product->in_stock_quantity, min(100,$product->custom_value2)) : min(100,$product->custom_value2)); $i++)
<option value="{{$i}}">{{$i}}</option> <option value="{{$i}}">{{$i}}</option>
@endfor @endfor
</select> </select>
@ -253,8 +250,7 @@
<span>{{ $total }}</span> <span>{{ $total }}</span>
</div> </div>
@if($authenticated) <div class="mx-auto text-center mt-20 content-center" x-data="{open: @entangle('payment_started'), toggle: @entangle('payment_confirmed'), buttonDisabled: false}" x-show.important="open" x-transition>
<div class="mx-auto text-center mt-20 content-center">
<h2 class="text-2xl font-bold tracking-wide border-b-2 pb-4">{{ $heading_text ?? ctrans('texts.checkout') }}</h2> <h2 class="text-2xl font-bold tracking-wide border-b-2 pb-4">{{ $heading_text ?? ctrans('texts.checkout') }}</h2>
@if (session()->has('message')) @if (session()->has('message'))
@component('portal.ninja2020.components.message') @component('portal.ninja2020.components.message')
@ -262,9 +258,10 @@
@endcomponent @endcomponent
@endif @endif
@if(count($methods) > 0) @if(count($methods) > 0)
<div class="mt-4"> <div class="mt-4" x-show.important="!toggle" x-transition>
@foreach($methods as $method) @foreach($methods as $method)
<button <button
x-on:click="buttonDisabled = true" x-bind:disabled="buttonDisabled"
wire:click="handleMethodSelectingEvent('{{ $method['company_gateway_id'] }}', '{{ $method['gateway_type_id'] }}')" wire:click="handleMethodSelectingEvent('{{ $method['company_gateway_id'] }}', '{{ $method['gateway_type_id'] }}')"
class="relative -ml-px inline-flex items-center space-x-2 rounded border border-gray-300 bg-gray-50 px-4 py-2 text-sm font-medium text-gray-700 hover:bg-gray-100 focus:border-indigo-500 focus:outline-none focus:ring-1 focus:ring-indigo-500"> class="relative -ml-px inline-flex items-center space-x-2 rounded border border-gray-300 bg-gray-50 px-4 py-2 text-sm font-medium text-gray-700 hover:bg-gray-100 focus:border-indigo-500 focus:outline-none focus:ring-1 focus:ring-indigo-500">
{{ $method['label'] }} {{ $method['label'] }}
@ -273,8 +270,7 @@
</div> </div>
@endif @endif
@if($payment_started) <div class="mt-4 container mx-auto flex w-full justify-center" x-show.important="toggle" x-transition>
<div class="mt-4 container mx-auto flex w-full justify-center">
<span class=""> <span class="">
<svg class="animate-spin h-8 w-8 text-primary mx-auto justify-center w-full" xmlns="http://www.w3.org/2000/svg" <svg class="animate-spin h-8 w-8 text-primary mx-auto justify-center w-full" xmlns="http://www.w3.org/2000/svg"
fill="none" viewBox="0 0 24 24"> fill="none" viewBox="0 0 24 24">
@ -285,9 +281,8 @@
</svg> </svg>
</span> </span>
</div> </div>
@endif
</div> </div>
@endif
@if(!$email || $errors->has('email')) @if(!$email || $errors->has('email'))
<form wire:submit.prevent="handleEmail" class=""> <form wire:submit.prevent="handleEmail" class="">