Subscriptions v2 - OTP

This commit is contained in:
David Bomba 2022-12-10 16:31:31 +11:00
parent 21bec86fda
commit 660371cbf5
2 changed files with 123 additions and 112 deletions

View File

@ -194,6 +194,7 @@ class BillingPortalPurchasev2 extends Component
public $authenticated = false; public $authenticated = false;
public $login; public $login;
public $float_amount_total; public $float_amount_total;
public $payment_started = false;
public function mount() public function mount()
{ {
@ -205,7 +206,16 @@ class BillingPortalPurchasev2 extends Component
$this->data = []; $this->data = [];
$this->price = $this->subscription->price; $this->price = $this->subscription->price; // ?
$this->recurring_products = $this->subscription->service()->recurring_products();
$this->products = $this->subscription->service()->products();
$this->optional_recurring_products = $this->subscription->service()->optional_recurring_products();
$this->optional_products = $this->subscription->service()->optional_products();
$this->bundle = collect();
//every thing below is redundant
if (request()->query('coupon')) { if (request()->query('coupon')) {
$this->coupon = request()->query('coupon'); $this->coupon = request()->query('coupon');
@ -215,13 +225,6 @@ class BillingPortalPurchasev2 extends Component
$this->price = $this->subscription->promo_price; $this->price = $this->subscription->promo_price;
} }
$this->recurring_products = $this->subscription->service()->recurring_products();
$this->products = $this->subscription->service()->products();
$this->optional_recurring_products = $this->subscription->service()->optional_recurring_products();
$this->optional_products = $this->subscription->service()->optional_products();
$this->bundle = collect();
} }
public function loginValidation() public function loginValidation()
@ -238,13 +241,10 @@ class BillingPortalPurchasev2 extends Component
$code = Cache::get("subscriptions:otp:{$this->email}"); $code = Cache::get("subscriptions:otp:{$this->email}");
// $this->validateOnly('login', ['login' => 'required'], ['login' => ctrans('texts.invalid_code')]);
if($user_code != $code){ if($user_code != $code){
nlog($code);
nlog($user_code);
$errors = $this->getErrorBag(); $errors = $this->getErrorBag();
$errors->add('login', ctrans('texts.invalid_code')); $errors->add('login', ctrans('texts.invalid_code'));
return $this;
} }
$contact = ClientContact::where('email', $this->email)->first(); $contact = ClientContact::where('email', $this->email)->first();
@ -466,6 +466,99 @@ class BillingPortalPurchasev2 extends Component
} }
/**
* Fetching payment methods from the client.
*
* @return $this
*/
protected function getPaymentMethods(): self
{
$this->methods = $this->contact->client->service()->getPaymentMethods($this->float_amount_total);
return $this;
}
/**
* Middle method between selecting payment method &
* submitting the from to the backend.
*
* @param $company_gateway_id
* @param $gateway_type_id
*/
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();
}
/**
* Method to handle events before payments.
*
* @return void
*/
public function handleBeforePaymentEvents() :void
{
$this->payment_started = true;
// $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,
// // 'quantity' => $this->quantity,
// ];
// $is_eligible = $this->subscription->service()->isEligible($this->contact);
// if (is_array($is_eligible) && $is_eligible['message'] != 'Success') {
// $this->steps['not_eligible'] = true;
// $this->steps['not_eligible_message'] = $is_eligible['message'];
// $this->steps['show_loading_bar'] = false;
// return;
// }
// $this->invoice = $this->subscription
// ->service()
// ->createInvoice($data, $this->quantity)
// ->service()
// ->markSent()
// ->fillDefaults()
// ->adjustInventory()
// ->save();
// 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));
$this->emit('beforePaymentEventsCompleted');
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
public function rules() public function rules()
@ -597,103 +690,7 @@ class BillingPortalPurchasev2 extends Component
return $client->fresh()->contacts->first(); return $client->fresh()->contacts->first();
} }
/**
* Fetching payment methods from the client.
*
* @param ClientContact $contact
* @return $this
*/
protected function getPaymentMethods(): self
{
$this->methods = $this->contact->client->service()->getPaymentMethods($this->float_amount_total);
// if ($this->subscription->trial_enabled) {
// $this->heading_text = ctrans('texts.plan_trial');
// $this->steps['show_start_trial'] = true;
// return $this;
// }
// if ((int)$this->price == 0)
// $this->steps['payment_required'] = false;
// else
// $this->steps['fetched_payment_methods'] = true;
// $this->methods = $contact->client->service()->getPaymentMethods($this->price);
// $this->heading_text = ctrans('texts.payment_methods');
return $this;
}
/**
* Middle method between selecting payment method &
* submitting the from to the backend.
*
* @param $company_gateway_id
* @param $gateway_type_id
*/
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();
}
/**
* Method to handle events before payments.
*
* @return void
*/
public function handleBeforePaymentEvents()
{
$this->steps['started_payment'] = true;
$this->steps['show_loading_bar'] = true;
$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,
// 'quantity' => $this->quantity,
];
$is_eligible = $this->subscription->service()->isEligible($this->contact);
if (is_array($is_eligible) && $is_eligible['message'] != 'Success') {
$this->steps['not_eligible'] = true;
$this->steps['not_eligible_message'] = $is_eligible['message'];
$this->steps['show_loading_bar'] = false;
return;
}
$this->invoice = $this->subscription
->service()
->createInvoice($data, $this->quantity)
->service()
->markSent()
->fillDefaults()
->adjustInventory()
->save();
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));
$this->emit('beforePaymentEventsCompleted');
}
/** /**
* Proxy method for starting the trial. * Proxy method for starting the trial.

View File

@ -231,14 +231,14 @@
</div> </div>
@if($authenticated) @if($authenticated)
<div class=" mx-auto text-center mt-20 content-center"> <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')
{{ session('message') }} {{ session('message') }}
@endcomponent @endcomponent
@endif @endif
@if(count($methods) > 0) @if(count($methods) > 0 && !$payment_started)
<div class="mt-4"> <div class="mt-4">
@foreach($methods as $method) @foreach($methods as $method)
<button <button
@ -249,6 +249,20 @@
@endforeach @endforeach
</div> </div>
@endif @endif
@if($payment_started)
<div class="mt-4 container mx-auto flex w-full justify-center">
<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"
fill="none" viewBox="0 0 24 24">
<circle class="opacity-75" cx="12" cy="12" r="10" stroke="hsl(210, 70, 75)" stroke-linecap="round"
stroke-width="4" animation="dash 1.5s ease-in-out infinite"></circle>
<path class="opacity-75" fill="#fff"
d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path>
</svg>
</span>
</div>
@endif
</div> </div>
@endif @endif