updgrade and downgrade

This commit is contained in:
David Bomba 2021-04-15 13:40:03 +10:00
parent bb6a1c6b27
commit 7c8bf53951
3 changed files with 92 additions and 33 deletions

View File

@ -85,26 +85,12 @@ class SubscriptionPlanSwitch extends Component
$this->state['show_loading_bar'] = true; $this->state['show_loading_bar'] = true;
$change_cost = $this->target->service()->calculateUpgradePrice($this->recurring_invoice, $this->target);
if($change_cost > 0)
{
$this->state['invoice'] = $this->target->service()->createChangePlanInvoice([ $this->state['invoice'] = $this->target->service()->createChangePlanInvoice([
'recurring_invoice' => $this->recurring_invoice, 'recurring_invoice' => $this->recurring_invoice,
'subscription' => $this->subscription, 'subscription' => $this->subscription,
'target' => $this->target, 'target' => $this->target,
'hash' => $this->hash, 'hash' => $this->hash,
]); ]);
}
else
{
$this->state['credit'] = $this->target->service()->createChangePlanCredit([
'recurring_invoice' => $this->recurring_invoice,
'subscription' => $this->subscription,
'target' => $this->target,
'hash' => $this->hash,
]);
}
Cache::put($this->hash, [ Cache::put($this->hash, [
'subscription_id' => $this->target->id, 'subscription_id' => $this->target->id,
@ -139,6 +125,13 @@ class SubscriptionPlanSwitch extends Component
public function handlePaymentNotRequired() public function handlePaymentNotRequired()
{ {
return $this->target->service()->createChangePlanCredit([
'recurring_invoice' => $this->recurring_invoice,
'subscription' => $this->subscription,
'target' => $this->target,
'hash' => $this->hash,
]);
} }
public function render() public function render()

View File

@ -250,9 +250,11 @@ class SubscriptionService
* @param Invoice $invoice * @param Invoice $invoice
* @return array * @return array
*/ */
private function calculateProRataRefundItems($invoice) :array private function calculateProRataRefundItems($invoice, $is_credit = false) :array
{ {
/* depending on whether we are creating an invoice or a credit*/
$multiplier = $is_credit ? 1 : -1;
$start_date = Carbon::parse($invoice->date); $start_date = Carbon::parse($invoice->date);
$current_date = now(); $current_date = now();
@ -271,7 +273,7 @@ class SubscriptionService
if($item->product_key != ctrans('texts.refund')) if($item->product_key != ctrans('texts.refund'))
{ {
$item->cost = ($item->cost*$ratio*-1); $item->cost = ($item->cost*$ratio*$multiplier);
$item->product_key = ctrans('texts.refund'); $item->product_key = ctrans('texts.refund');
$item->notes = ctrans('texts.refund') . ": ". $item->notes; $item->notes = ctrans('texts.refund') . ": ". $item->notes;
@ -343,8 +345,40 @@ class SubscriptionService
nlog("total payable = {$total_payable}"); nlog("total payable = {$total_payable}");
return $this->createCredit($pro_rata_refund_amount, $last_invoice, $target_subscription, $old_subscription); $credit = $this->createCredit($pro_rata_refund_amount, $last_invoice, $target_subscription, $old_subscription);
///////////////////////////
$old_subscription_recurring_invoice = $recurring_invoice;
$old_subscription_recurring_invoice->service()->stop()->save();
$recurring_invoice_repo = new RecurringInvoiceRepository();
$recurring_invoice_repo->archive($old_subscription_recurring_invoice);
$recurring_invoice = $this->convertInvoiceToRecurring($recurring_invoice->client_id);
$recurring_invoice = $recurring_invoice_repo->save([], $recurring_invoice);
$recurring_invoice->next_send_date = now();
$recurring_invoice->next_send_date = $recurring_invoice->nextSendDate();
/* Start the recurring service */
$recurring_invoice->service()
->start()
->save();
////////////////////////////
$context = [
'context' => 'change_plan',
'recurring_invoice' => $recurring_invoice->hashed_id,
'credit' => $credit->hashed_id,
'client' => $recurring_invoice->client->hashed_id,
'subscription' => $target_subscription->hashed_id,
'contact' => auth('contact')->user()->hashed_id,
];
$response = $this->triggerWebhook($context);
nlog($response);
return $this->handleRedirect('/client/credits/'.$credit->hashed_id);
} }
@ -424,16 +458,49 @@ class SubscriptionService
nlog($response); nlog($response);
if(array_key_exists('post_purchase_url', $this->subscription->webhook_configuration) && strlen($this->subscription->webhook_configuration['post_purchase_url']) >=1) return $this->handleRedirect('/client/recurring_invoices/'.$recurring_invoice->hashed_id);
return redirect($this->subscription->webhook_configuration['post_purchase_url']);
return redirect('/client/recurring_invoices/'.$recurring_invoice->hashed_id);
} }
public function handlePlanChangeNoPayment() public function handlePlanChangeNoPayment($data)
{ {
/*
'recurring_invoice' => $this->recurring_invoice,
'subscription' => $this->subscription,
'target' => $this->target,
'hash' => $this->hash,
*/
$old_subscription_recurring_invoice = $data['recurring_invoice'];
$old_subscription_recurring_invoice->service()->stop()->save();
$recurring_invoice_repo = new RecurringInvoiceRepository();
$recurring_invoice_repo->archive($old_subscription_recurring_invoice);
$recurring_invoice = $this->convertInvoiceToRecurring($old_subscription_recurring_invoice->client_id);
$recurring_invoice = $recurring_invoice_repo->save([], $recurring_invoice);
$recurring_invoice->next_send_date = now();
$recurring_invoice->next_send_date = $recurring_invoice->nextSendDate();
/* Start the recurring service */
$recurring_invoice->service()
->start()
->save();
$context = [
'context' => 'change_plan',
'recurring_invoice' => $recurring_invoice->hashed_id,
'invoice' => $this->encodePrimaryKey($payment_hash->fee_invoice_id),
'client' => $recurring_invoice->client->hashed_id,
'subscription' => $this->subscription->hashed_id,
'contact' => auth('contact')->user()->hashed_id,
];
$response = $this->triggerWebhook($context);
nlog($response);
return $this->handleRedirect('/client/recurring_invoices/'.$recurring_invoice->hashed_id);
} }
private function createCredit($refund_amount, $last_invoice, $target, $old_subscription) private function createCredit($refund_amount, $last_invoice, $target, $old_subscription)
@ -448,7 +515,7 @@ class SubscriptionService
$line_items = $subscription_repo->generateLineItems($target); $line_items = $subscription_repo->generateLineItems($target);
$credit->line_items = array_merge($line_items, $this->calculateProRataRefundItems($last_invoice)); $credit->line_items = array_merge($line_items, $this->calculateProRataRefundItems($last_invoice, true));
$data = [ $data = [
'client_id' => $last_invoice->client_id, 'client_id' => $last_invoice->client_id,

View File

@ -1,6 +1,9 @@
<div class="grid grid-cols-12 gap-8 mt-8"> <div class="grid grid-cols-12 gap-8 mt-8">
<div class="col-span-12 md:col-span-5 md:col-start-4 px-4 py-5"> <div class="col-span-12 md:col-span-5 md:col-start-4 px-4 py-5">
<!-- Total price --> <!-- Total price -->
@if(isset($state['invoice']))
<div class="relative mt-8"> <div class="relative mt-8">
<div class="absolute inset-0 flex items-center"> <div class="absolute inset-0 flex items-center">
<div class="w-full border-t border-gray-300"></div> <div class="w-full border-t border-gray-300"></div>
@ -15,7 +18,6 @@
</div> </div>
</div> </div>
@if($state['invoice'])
<form action="{{ route('client.payments.process', ['hash' => $hash, 'sidebar' => 'hidden']) }}" <form action="{{ route('client.payments.process', ['hash' => $hash, 'sidebar' => 'hidden']) }}"
method="post" id="payment-method-form"> method="post" id="payment-method-form">
@csrf @csrf
@ -32,14 +34,6 @@
<input type="hidden" name="company_gateway_id" value="{{ $state['company_gateway_id'] }}"/> <input type="hidden" name="company_gateway_id" value="{{ $state['company_gateway_id'] }}"/>
<input type="hidden" name="payment_method_id" value="{{ $state['payment_method_id'] }}"/> <input type="hidden" name="payment_method_id" value="{{ $state['payment_method_id'] }}"/>
</form> </form>
@elseif($state['credit'])
<form wire:submit.prevent="handlePaymentNotRequired" class="mt-8">
@csrf
<button class="px-3 py-2 border rounded mr-4 hover:border-blue-600">
{{ ctrans('texts.click_to_continue') }}
</button>
</form>
@endif
<!-- Payment methods --> <!-- Payment methods -->
<div class="mt-8 flex flex-col items-center"> <div class="mt-8 flex flex-col items-center">
@ -68,5 +62,10 @@
@endif @endif
</div> </div>
</div> </div>
@elseif($amount < 0)
<button wire:click="handlePaymentNotRequired"class="px-3 py-2 border rounded mr-4 hover:border-blue-600">
{{ ctrans('texts.click_to_continue') }}
</button>
@endif
</div> </div>
</div> </div>