Merge pull request #5440 from turbo124/v5-develop

Refund subscriptions
This commit is contained in:
David Bomba 2021-04-16 07:59:16 +10:00 committed by GitHub
commit 999e211104
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
17 changed files with 372 additions and 147 deletions

View File

@ -229,8 +229,8 @@ class CreateSingleAccount extends Command
'company_id' => $company->id, 'company_id' => $company->id,
'product_key' => 'enterprise_plan', 'product_key' => 'enterprise_plan',
'notes' => 'The Enterprise Plan', 'notes' => 'The Enterprise Plan',
'cost' => 10, 'cost' => 14,
'price' => 10, 'price' => 14,
'quantity' => 1, 'quantity' => 1,
]); ]);

View File

@ -35,7 +35,6 @@ class SubscriptionPlanSwitchController extends Controller
$amount = $recurring_invoice->subscription $amount = $recurring_invoice->subscription
->service() ->service()
->calculateUpgradePrice($recurring_invoice, $target); ->calculateUpgradePrice($recurring_invoice, $target);
/** /**
* *
* Null value here is a proxy for * Null value here is a proxy for

View File

@ -592,7 +592,7 @@ class PaymentController extends BaseController
$this->payment_repo->restore($payment); $this->payment_repo->restore($payment);
if (! $bulk) { if (! $bulk) {
return $this->listResponse($payment); return $this->itemResponse($payment);
} }
break; break;
@ -600,7 +600,7 @@ class PaymentController extends BaseController
$this->payment_repo->archive($payment); $this->payment_repo->archive($payment);
if (! $bulk) { if (! $bulk) {
return $this->listResponse($payment); return $this->itemResponse($payment);
} }
// code... // code...
break; break;
@ -608,12 +608,24 @@ class PaymentController extends BaseController
$this->payment_repo->delete($payment); $this->payment_repo->delete($payment);
if (! $bulk) { if (! $bulk) {
return $this->listResponse($payment); return $this->itemResponse($payment);
} }
// code... // code...
break; break;
case 'email': case 'email':
//dispatch email to queue //dispatch email to queue
$this->payment->service()->sendEmail();
if (! $bulk) {
return $this->itemResponse($payment);
}
break;
case 'email_receipt':
$this->payment->service()->sendEmail();
if (! $bulk) {
return $this->itemResponse($payment);
}
break; break;
default: default:
@ -671,6 +683,8 @@ class PaymentController extends BaseController
{ {
$payment = $request->payment(); $payment = $request->payment();
// nlog($request->all());
$payment = $payment->refund($request->all()); $payment = $payment->refund($request->all());
return $this->itemResponse($payment); return $this->itemResponse($payment);

View File

@ -85,7 +85,7 @@ class SelfUpdateController extends BaseController
Artisan::call('clear-compiled'); Artisan::call('clear-compiled');
Artisan::call('cache:clear'); Artisan::call('cache:clear');
Artisan::call('debugbar:clear'); // Artisan::call('debugbar:clear');
Artisan::call('route:clear'); Artisan::call('route:clear');
Artisan::call('view:clear'); Artisan::call('view:clear');
Artisan::call('config:clear'); Artisan::call('config:clear');

View File

@ -279,8 +279,8 @@ class TaskController extends BaseController
$task = $this->task_repo->save($request->all(), $task); $task = $this->task_repo->save($request->all(), $task);
// if($task->status_order != $old_task->status_order) if($task->status_order != $old_task->status_order)
// $this->task_repo->sortStatuses($old_task, $task); $this->task_repo->sortStatuses($old_task, $task);
event(new TaskWasUpdated($task, $task->company, Ninja::eventVars(auth()->user()->id))); event(new TaskWasUpdated($task, $task->company, Ninja::eventVars(auth()->user()->id)));

View File

@ -330,9 +330,9 @@ class BillingPortalPurchase extends Component
$is_eligible = $this->subscription->service()->isEligible($this->contact); $is_eligible = $this->subscription->service()->isEligible($this->contact);
if (is_array($is_eligible)) { if ($is_eligible['exception']['message'] != 'Success') {
$this->steps['not_eligible'] = true; $this->steps['not_eligible'] = true;
$this->steps['not_eligible_message'] = $is_eligible['exception']; $this->steps['not_eligible_message'] = $is_eligible['exception']['message'];
$this->steps['show_loading_bar'] = false; $this->steps['show_loading_bar'] = false;
return; return;

View File

@ -25,7 +25,7 @@ class RecurringInvoiceCancellation extends Component
public function processCancellation() public function processCancellation()
{ {
if ($this->invoice->subscription) { if ($this->invoice->subscription) {
return $this->invoice->subscription->service()->handleCancellation(); return $this->invoice->subscription->service()->handleCancellation($this->invoice);
} }
return redirect()->route('client.recurring_invoices.request_cancellation', ['recurring_invoice' => $this->invoice->hashed_id]); return redirect()->route('client.recurring_invoices.request_cancellation', ['recurring_invoice' => $this->invoice->hashed_id]);

View File

@ -82,6 +82,7 @@ class SubscriptionPlanSwitch extends Component
public function handleBeforePaymentEvents(): void public function handleBeforePaymentEvents(): void
{ {
$this->state['show_loading_bar'] = true; $this->state['show_loading_bar'] = true;
$this->state['invoice'] = $this->target->service()->createChangePlanInvoice([ $this->state['invoice'] = $this->target->service()->createChangePlanInvoice([
@ -121,6 +122,18 @@ class SubscriptionPlanSwitch extends Component
$this->handleBeforePaymentEvents(); $this->handleBeforePaymentEvents();
} }
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()
{ {
return render('components.livewire.subscription-plan-switch'); return render('components.livewire.subscription-plan-switch');

View File

@ -56,7 +56,7 @@ class UpdateSubscriptionRequest extends Request
'allow_plan_changes' => ['sometimes'], 'allow_plan_changes' => ['sometimes'],
'refund_period' => ['sometimes'], 'refund_period' => ['sometimes'],
'webhook_configuration' => ['array'], 'webhook_configuration' => ['array'],
'name' => ['required', Rule::unique('subscriptions')->where('company_id', auth()->user()->company()->id)->ignore($this->subscription->id)] 'name' => ['sometimes', Rule::unique('subscriptions')->where('company_id', auth()->user()->company()->id)->ignore($this->subscription->id)]
]; ];
return $this->globalRules($rules); return $this->globalRules($rules);

View File

@ -45,10 +45,10 @@ class CreateCompanyTaskStatuses
public function handle() public function handle()
{ {
$task_statuses = [ $task_statuses = [
['name' => ctrans('texts.backlog'), 'company_id' => $this->company->id, 'user_id' => $this->user->id, 'created_at' => now(), 'updated_at' => now()], ['name' => ctrans('texts.backlog'), 'company_id' => $this->company->id, 'user_id' => $this->user->id, 'created_at' => now(), 'updated_at' => now(), 'status_order' => 1],
['name' => ctrans('texts.ready_to_do'), 'company_id' => $this->company->id, 'user_id' => $this->user->id, 'created_at' => now(), 'updated_at' => now()], ['name' => ctrans('texts.ready_to_do'), 'company_id' => $this->company->id, 'user_id' => $this->user->id, 'created_at' => now(), 'updated_at' => now(), 'status_order' => 2],
['name' => ctrans('texts.in_progress'), 'company_id' => $this->company->id, 'user_id' => $this->user->id, 'created_at' => now(), 'updated_at' => now()], ['name' => ctrans('texts.in_progress'), 'company_id' => $this->company->id, 'user_id' => $this->user->id, 'created_at' => now(), 'updated_at' => now(), 'status_order' => 3],
['name' => ctrans('texts.done'), 'company_id' => $this->company->id, 'user_id' => $this->user->id, 'created_at' => now(), 'updated_at' => now()], ['name' => ctrans('texts.done'), 'company_id' => $this->company->id, 'user_id' => $this->user->id, 'created_at' => now(), 'updated_at' => now(), 'status_order' => 4],
]; ];

View File

@ -45,12 +45,29 @@ class Webhook extends BaseModel
public static $valid_events = [ public static $valid_events = [
self::EVENT_CREATE_CLIENT, self::EVENT_CREATE_CLIENT,
self::EVENT_CREATE_PAYMENT,
self::EVENT_CREATE_QUOTE,
self::EVENT_CREATE_INVOICE, self::EVENT_CREATE_INVOICE,
self::EVENT_CREATE_QUOTE,
self::EVENT_CREATE_PAYMENT,
self::EVENT_CREATE_VENDOR, self::EVENT_CREATE_VENDOR,
self::EVENT_UPDATE_QUOTE,
self::EVENT_DELETE_QUOTE,
self::EVENT_UPDATE_INVOICE,
self::EVENT_DELETE_INVOICE,
self::EVENT_UPDATE_CLIENT,
self::EVENT_DELETE_CLIENT,
self::EVENT_DELETE_PAYMENT,
self::EVENT_UPDATE_VENDOR,
self::EVENT_DELETE_VENDOR,
self::EVENT_CREATE_EXPENSE, self::EVENT_CREATE_EXPENSE,
self::EVENT_UPDATE_EXPENSE,
self::EVENT_DELETE_EXPENSE,
self::EVENT_CREATE_TASK, self::EVENT_CREATE_TASK,
self::EVENT_UPDATE_TASK,
self::EVENT_DELETE_TASK,
self::EVENT_APPROVE_QUOTE,
self::EVENT_LATE_INVOICE,
self::EVENT_EXPIRED_QUOTE,
self::EVENT_REMIND_INVOICE,
]; ];
protected $fillable = [ protected $fillable = [

View File

@ -291,6 +291,10 @@ class BaseRepository
/* Apply entity number */ /* Apply entity number */
$model = $model->service()->applyNumber()->save(); $model = $model->service()->applyNumber()->save();
/* Handle attempts where the deposit is greater than the amount/balance of the invoice */
if((int)$model->balance != 0 && $model->partial > $model->amount)
$model->partial = min($model->amount, $model->balance);
/* Update product details if necessary */ /* Update product details if necessary */
if ($model->company->update_products) if ($model->company->update_products)
UpdateOrCreateProduct::dispatch($model->line_items, $model, $model->company); UpdateOrCreateProduct::dispatch($model->line_items, $model, $model->company);

View File

@ -155,6 +155,10 @@ class PaymentRepository extends BaseRepository {
} }
if ( ! $is_existing_payment && ! $this->import_mode ) { if ( ! $is_existing_payment && ! $this->import_mode ) {
if ($payment->client->getSetting('client_manual_payment_notification'))
$payment->service()->sendEmail();
event( new PaymentWasCreated( $payment, $payment->company, Ninja::eventVars(auth()->user()->id) ) ); event( new PaymentWasCreated( $payment, $payment->company, Ninja::eventVars(auth()->user()->id) ) );
} }

View File

@ -12,6 +12,7 @@
namespace App\Services\Subscription; namespace App\Services\Subscription;
use App\DataMapper\InvoiceItem; use App\DataMapper\InvoiceItem;
use App\Factory\CreditFactory;
use App\Factory\InvoiceFactory; use App\Factory\InvoiceFactory;
use App\Factory\InvoiceToRecurringInvoiceFactory; use App\Factory\InvoiceToRecurringInvoiceFactory;
use App\Factory\RecurringInvoiceFactory; use App\Factory\RecurringInvoiceFactory;
@ -26,6 +27,7 @@ use App\Models\Product;
use App\Models\RecurringInvoice; use App\Models\RecurringInvoice;
use App\Models\Subscription; use App\Models\Subscription;
use App\Models\SystemLog; use App\Models\SystemLog;
use App\Repositories\CreditRepository;
use App\Repositories\InvoiceRepository; use App\Repositories\InvoiceRepository;
use App\Repositories\RecurringInvoiceRepository; use App\Repositories\RecurringInvoiceRepository;
use App\Repositories\SubscriptionRepository; use App\Repositories\SubscriptionRepository;
@ -130,7 +132,7 @@ class SubscriptionService
]; ];
$response = $this->triggerWebhook($context); $response = $this->triggerWebhook($context);
nlog($response); // nlog($response);
return $response; return $response;
} }
@ -177,11 +179,7 @@ class SubscriptionService
//execute any webhooks //execute any webhooks
$response = $this->triggerWebhook($context); $response = $this->triggerWebhook($context);
if(array_key_exists('return_url', $this->subscription->webhook_configuration) && strlen($this->subscription->webhook_configuration['return_url']) >=1){ return $this->handleRedirect('/client/recurring_invoices/'.$recurring_invoice->hashed_id);
return redirect($this->subscription->webhook_configuration['return_url']);
}
return redirect('/client/recurring_invoices/'.$recurring_invoice->hashed_id);
} }
public function calculateUpgradePrice(RecurringInvoice $recurring_invoice, Subscription $target) :?float public function calculateUpgradePrice(RecurringInvoice $recurring_invoice, Subscription $target) :?float
@ -198,15 +196,19 @@ class SubscriptionService
$outstanding_amounts = $outstanding->sum('balance'); $outstanding_amounts = $outstanding->sum('balance');
// $outstanding_invoices = $outstanding->get(); // $outstanding_invoices = $outstanding->get();
$outstanding_invoices = $outstanding; $outstanding_invoice = Invoice::where('subscription_id', $this->subscription->id)
->where('client_id', $recurring_invoice->client_id)
->where('is_deleted', 0)
->orderBy('id', 'desc')
->first();
if ($outstanding->count() == 0){ if ($outstanding->count() == 0){
//nothing outstanding //nothing outstanding
return $target->price; return $target->price - $this->calculateProRataRefund($outstanding_invoice);
} }
elseif ($outstanding->count() == 1){ elseif ($outstanding->count() == 1){
//user has multiple amounts outstanding //user has multiple amounts outstanding
return $target->price - $this->calculateProRataRefund($outstanding->first()); return $target->price - $this->calculateProRataRefund($outstanding_invoice);
} }
elseif ($outstanding->count() > 1) { elseif ($outstanding->count() > 1) {
//user is changing plan mid frequency cycle //user is changing plan mid frequency cycle
@ -231,15 +233,61 @@ class SubscriptionService
$current_date = now(); $current_date = now();
$days_to_refund = $start_date->diffInDays($current_date); $days_of_subscription_used = $start_date->diffInDays($current_date);
$days_in_frequency = $this->getDaysInFrequency(); $days_in_frequency = $this->getDaysInFrequency();
$pro_rata_refund = round((($days_in_frequency - $days_to_refund)/$days_in_frequency) * $invoice->amount ,2); $pro_rata_refund = round((($days_in_frequency - $days_of_subscription_used)/$days_in_frequency) * $invoice->amount ,2);
return $pro_rata_refund; return $pro_rata_refund;
} }
/**
* Returns refundable set of line items
* transformed for direct injection into
* the invoice
* @param Invoice $invoice
* @return 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);
$current_date = now();
$days_of_subscription_used = $start_date->diffInDays($current_date);
$days_in_frequency = $this->getDaysInFrequency();
$ratio = ($days_in_frequency - $days_of_subscription_used)/$days_in_frequency;
$line_items = [];
foreach($invoice->line_items as $item)
{
if($item->product_key != ctrans('texts.refund'))
{
$item->cost = ($item->cost*$ratio*$multiplier);
$item->product_key = ctrans('texts.refund');
$item->notes = ctrans('texts.refund') . ": ". $item->notes;
$line_items[] = $item;
}
}
return $line_items;
}
/** /**
* We only charge for the used days * We only charge for the used days
* *
@ -253,79 +301,105 @@ class SubscriptionService
$current_date = now(); $current_date = now();
$days_to_refund = $start_date->diffInDays($current_date); $days_to_charge = $start_date->diffInDays($current_date);
$days_in_frequency = $this->getDaysInFrequency(); $days_in_frequency = $this->getDaysInFrequency();
$pro_rata_refund = round(($days_to_refund/$days_in_frequency) * $invoice->amount ,2); nlog("days to charge = {$days_to_charge} fays in frequency = {$days_in_frequency}");
$pro_rata_charge = round(($days_to_charge/$days_in_frequency) * $invoice->amount ,2);
nlog("pro rata charge = {$pro_rata_charge}");
return $pro_rata_charge;
}
public function createChangePlanCredit($data)
{
$recurring_invoice = $data['recurring_invoice'];
$old_subscription = $data['subscription'];
$target_subscription = $data['target'];
$pro_rata_charge_amount = 0;
$pro_rata_refund_amount = 0;
$last_invoice = Invoice::where('subscription_id', $recurring_invoice->subscription_id)
->where('client_id', $recurring_invoice->client_id)
->where('is_deleted', 0)
->withTrashed()
->orderBy('id', 'desc')
->first();
if($last_invoice->balance > 0)
{
$pro_rata_charge_amount = $this->calculateProRataCharge($last_invoice, $old_subscription);
nlog("pro rata charge = {$pro_rata_charge_amount}");
}
else
{
$pro_rata_refund_amount = $this->calculateProRataRefund($last_invoice, $old_subscription) * -1;
nlog("pro rata refund = {$pro_rata_refund_amount}");
}
$total_payable = $pro_rata_refund_amount + $pro_rata_charge_amount + $this->subscription->price;
nlog("total payable = {$total_payable}");
$credit = $this->createCredit($pro_rata_refund_amount, $last_invoice, $target_subscription, $old_subscription);
$new_recurring_invoice = $this->createNewRecurringInvoice($recurring_invoice);
$context = [
'context' => 'change_plan',
'recurring_invoice' => $new_recurring_invoice->hashed_id,
'credit' => $credit->hashed_id,
'client' => $new_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);
return $pro_rata_refund;
} }
public function createChangePlanInvoice($data) public function createChangePlanInvoice($data)
{ {
$recurring_invoice = $data['recurring_invoice']; $recurring_invoice = $data['recurring_invoice'];
//Data array structure $old_subscription = $data['subscription'];
/** $target_subscription = $data['target'];
* [
* 'recurring_invoice' => RecurringInvoice::class,
* 'subscription' => Subscription::class,
* 'target' => Subscription::class
* ]
*/
// $outstanding_invoice = $recurring_invoice->invoices()
// ->where('is_deleted', 0)
// ->whereIn('status_id', [Invoice::STATUS_SENT, Invoice::STATUS_PARTIAL])
// ->where('balance', '>', 0)
// ->first();
$pro_rata_charge_amount = 0; $pro_rata_charge_amount = 0;
$pro_rata_refund_amount = 0; $pro_rata_refund_amount = 0;
// // We calculate the pro rata charge for this invoice. $last_invoice = Invoice::where('subscription_id', $recurring_invoice->subscription_id)
// if($outstanding_invoice) ->where('client_id', $recurring_invoice->client_id)
// {
// }
$last_invoice = $recurring_invoice->invoices()
->where('is_deleted', 0) ->where('is_deleted', 0)
->withTrashed()
->orderBy('id', 'desc') ->orderBy('id', 'desc')
->first(); ->first();
//$last_invoice may not be here! if($last_invoice->balance > 0)
if(!$last_invoice) {
$data = [
'client_id' => $recurring_invoice->client_id,
'coupon' => '',
];
return $this->createInvoice($data)->service()->markSent()->fillDefaults()->save();
}
else if($last_invoice->balance > 0)
{ {
$pro_rata_charge_amount = $this->calculateProRataCharge($last_invoice); $pro_rata_charge_amount = $this->calculateProRataCharge($last_invoice, $old_subscription);
nlog("pro rata charge = {$pro_rata_charge_amount}");
} }
else else
{ {
$pro_rata_refund_amount = $this->calculateProRataRefund($last_invoice) * -1; $pro_rata_refund_amount = $this->calculateProRataRefund($last_invoice, $old_subscription) * -1;
nlog("pro rata refund = {$pro_rata_refund_amount}");
} }
$total_payable = $pro_rata_refund_amount + $pro_rata_charge_amount + $this->subscription->price; $total_payable = $pro_rata_refund_amount + $pro_rata_charge_amount + $this->subscription->price;
if($total_payable > 0) nlog("total payable = {$total_payable}");
{
return $this->proRataInvoice($pro_rata_refund_amount, $data['subscription'], $data['target']);
}
else
{
//create credit
}
return Invoice::where('status_id', Invoice::STATUS_SENT)->first(); return $this->proRataInvoice($pro_rata_refund_amount, $last_invoice, $target_subscription, $old_subscription);
} }
/** /**
@ -335,25 +409,9 @@ class SubscriptionService
private function handlePlanChange($payment_hash) private function handlePlanChange($payment_hash)
{ {
//payment has been made. $old_recurring_invoice = RecurringInvoice::find($payment_hash->data->billing_context->recurring_invoice);
//
//new subscription starts today - delete old recurring invoice.
$old_subscription_recurring_invoice = RecurringInvoice::find($payment_hash->data->billing_context->recurring_invoice); $recurring_invoice = $this->createNewRecurringInvoice($old_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($payment_hash->payment->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 = [
'context' => 'change_plan', 'context' => 'change_plan',
@ -368,18 +426,82 @@ 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() private function createNewRecurringInvoice($old_recurring_invoice) :RecurringInvoice
{ {
$old_recurring_invoice->service()->stop()->save();
$recurring_invoice_repo = new RecurringInvoiceRepository();
$recurring_invoice_repo->archive($$old_recurring_invoice);
$recurring_invoice = $this->convertInvoiceToRecurring($old_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();
return $recurring_invoice;
} }
public function handlePlanChangeNoPayment($data)
{
/*
'recurring_invoice' => $this->recurring_invoice,
'subscription' => $this->subscription,
'target' => $this->target,
'hash' => $this->hash,
*/
$recurring_invoice = $this->createNewRecurringInvoice($data['recurring_invoice']);
$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)
{
$subscription_repo = new SubscriptionRepository();
$credit_repo = new CreditRepository();
$credit = CreditFactory::create($this->subscription->company_id, $this->subscription->user_id);
$credit->date = now()->format('Y-m-d');
$credit->subscription_id = $this->subscription->id;
$line_items = $subscription_repo->generateLineItems($target);
$credit->line_items = array_merge($line_items, $this->calculateProRataRefundItems($last_invoice, true));
$data = [
'client_id' => $last_invoice->client_id,
'quantity' => 1,
'date' => now()->format('Y-m-d'),
];
return $credit_repo->save($data, $credit)->service()->markSent()->fillDefaults()->save();
}
/** /**
* 'client_id' => 2, * 'client_id' => 2,
'date' => '2021-04-13', 'date' => '2021-04-13',
@ -388,23 +510,21 @@ class SubscriptionService
'coupon' => '', 'coupon' => '',
'quantity' => 1, 'quantity' => 1,
*/ */
private function proRataInvoice($refund_amount, $subscription, $target) private function proRataInvoice($refund_amount, $last_invoice, $target, $old_subscription)
{ {
$subscription_repo = new SubscriptionRepository(); $subscription_repo = new SubscriptionRepository();
$invoice_repo = new InvoiceRepository(); $invoice_repo = new InvoiceRepository();
$invoice = InvoiceFactory::create($this->subscription->company_id, $this->subscription->user_id);
$invoice->date = now()->format('Y-m-d');
$invoice->subscription_id = $this->subscription->id;
$line_items = $subscription_repo->generateLineItems($target); $line_items = $subscription_repo->generateLineItems($target);
$item = new InvoiceItem; $invoice->line_items = array_merge($line_items, $this->calculateProRataRefundItems($last_invoice));
$item->quantity = 1;
$item->product_key = ctrans('texts.refund');
$item->notes = ctrans('texts.refund') . ":" .$subscription->name;
$item->cost = $refund_amount;
$line_items[] = $item;
$data = [ $data = [
'client_id' => $subscription->client_id, 'client_id' => $last_invoice->client_id,
'quantity' => 1, 'quantity' => 1,
'date' => now()->format('Y-m-d'), 'date' => now()->format('Y-m-d'),
]; ];
@ -538,40 +658,91 @@ class SubscriptionService
->get(); ->get();
} }
public function handleCancellation() public function handleCancellation(RecurringInvoice $recurring_invoice)
{ {
dd('Cancelling using SubscriptionService'); //only allow cancellation of services that are paid up to date.
// $last_invoice =
//only refund if they are in the refund window.
$outstanding_invoice = Invoice::where('subscription_id', $this->subscription->id)
->where('client_id', $recurring_invoice->client_id)
->where('is_deleted', 0)
->orderBy('id', 'desc')
->first();
$invoice_start_date = Carbon::parse($outstanding_invoice->date);
$refund_end_date = $invoice_start_date->addSeconds($this->subscription->refund_period);
/* Stop the recurring invoice and archive */
$recurring_invoice->service()->stop()->save();
$recurring_invoice_repo = new RecurringInvoiceRepository();
$recurring_invoice_repo->archive($recurring_invoice);
if($refund_end_date->greaterThan(now()) && (int)$outstanding_invoice->balance == 0)
{
//we are in the refund window.
//
//$outstanding_invoice
if($outstanding_invoice->payments()->exists())
{
$payment = $outstanding_invoice->payments()->first();
$data = [
'id' => $payment->id,
'gateway_refund' => true,
'send_email' => true,
'invoices' => [
['invoice_id' => $outstanding_invoice->id, 'amount' => $outstanding_invoice->amount],
],
];
$payment->refund($data);
}
}
$context = [
'context' => 'cancellation',
'subscription' => $this->subscription->hashed_id,
'recurring_invoice' => $recurring_invoice->hashed_id,
'client' => $recurring_invoice->client->hashed_id,
'contact' => auth('contact')->user()->hashed_id,
];
$this->triggerWebhook($context);
return $this->handleRedirect('client/subscriptions');
// ..
} }
private function getDaysInFrequency() private function getDaysInFrequency()
{ {
switch ($this->subscription->frequency_id) { switch ($this->subscription->frequency_id) {
case self::FREQUENCY_DAILY: case RecurringInvoice::FREQUENCY_DAILY:
return 1; return 1;
case self::FREQUENCY_WEEKLY: case RecurringInvoice::FREQUENCY_WEEKLY:
return 7; return 7;
case self::FREQUENCY_TWO_WEEKS: case RecurringInvoice::FREQUENCY_TWO_WEEKS:
return 14; return 14;
case self::FREQUENCY_FOUR_WEEKS: case RecurringInvoice::FREQUENCY_FOUR_WEEKS:
return now()->diffInDays(now()->addWeeks(4)); return now()->diffInDays(now()->addWeeks(4));
case self::FREQUENCY_MONTHLY: case RecurringInvoice::FREQUENCY_MONTHLY:
return now()->diffInDays(now()->addMonthNoOverflow()); return now()->diffInDays(now()->addMonthNoOverflow());
case self::FREQUENCY_TWO_MONTHS: case RecurringInvoice::FREQUENCY_TWO_MONTHS:
return now()->diffInDays(now()->addMonthNoOverflow(2)); return now()->diffInDays(now()->addMonthNoOverflow(2));
case self::FREQUENCY_THREE_MONTHS: case RecurringInvoice::FREQUENCY_THREE_MONTHS:
return now()->diffInDays(now()->addMonthNoOverflow(3)); return now()->diffInDays(now()->addMonthNoOverflow(3));
case self::FREQUENCY_FOUR_MONTHS: case RecurringInvoice::FREQUENCY_FOUR_MONTHS:
return now()->diffInDays(now()->addMonthNoOverflow(4)); return now()->diffInDays(now()->addMonthNoOverflow(4));
case self::FREQUENCY_SIX_MONTHS: case RecurringInvoice::FREQUENCY_SIX_MONTHS:
return now()->diffInDays(now()->addMonthNoOverflow(6)); return now()->diffInDays(now()->addMonthNoOverflow(6));
case self::FREQUENCY_ANNUALLY: case RecurringInvoice::FREQUENCY_ANNUALLY:
return now()->diffInDays(now()->addYear()); return now()->diffInDays(now()->addYear());
case self::FREQUENCY_TWO_YEARS: case RecurringInvoice::FREQUENCY_TWO_YEARS:
return now()->diffInDays(now()->addYears(2)); return now()->diffInDays(now()->addYears(2));
case self::FREQUENCY_THREE_YEARS: case RecurringInvoice::FREQUENCY_THREE_YEARS:
return now()->diffInDays(now()->addYears(3)); return now()->diffInDays(now()->addYears(3));
default: default:
return 0; return 0;

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,7 +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>
@endif
<!-- Payment methods --> <!-- Payment methods -->
<div class="mt-8 flex flex-col items-center"> <div class="mt-8 flex flex-col items-center">
@ -61,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>

View File

@ -21,6 +21,7 @@ use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker; use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Support\Facades\Session; use Illuminate\Support\Facades\Session;
use Illuminate\Support\Str; use Illuminate\Support\Str;
use Illuminate\Validation\ValidationException;
use Tests\MockAccountData; use Tests\MockAccountData;
use Tests\TestCase; use Tests\TestCase;
@ -40,6 +41,8 @@ class SubscriptionApiTest extends TestCase
$this->makeTestData(); $this->makeTestData();
$this->withoutExceptionHandling();
Session::start(); Session::start();
$this->faker = \Faker\Factory::create(); $this->faker = \Faker\Factory::create();
@ -92,7 +95,6 @@ class SubscriptionApiTest extends TestCase
$product = Product::factory()->create([ $product = Product::factory()->create([
'company_id' => $this->company->id, 'company_id' => $this->company->id,
'user_id' => $this->user->id, 'user_id' => $this->user->id,
'frequency_id' => RecurringInvoice::FREQUENCY_MONTHLY,
]); ]);
$response1 = $this $response1 = $this
@ -101,24 +103,19 @@ class SubscriptionApiTest extends TestCase
->assertStatus(200) ->assertStatus(200)
->json(); ->json();
// try {
$response2 = $this $response2 = $this
->withHeaders(['X-API-SECRET' => config('ninja.api_secret'),'X-API-TOKEN' => $this->token]) ->withHeaders(['X-API-SECRET' => config('ninja.api_secret'),'X-API-TOKEN' => $this->token])
->put('/api/v1/subscriptions/' . $response1['data']['id'], ['allow_cancellation' => true]) ->put('/api/v1/subscriptions/' . $response1['data']['id'], ['allow_cancellation' => true])
->assertStatus(200) ->assertStatus(200)
->json(); ->json();
// }catch(ValidationException $e) {
// nlog($e->validator->getMessageBag());
// }
$this->assertNotEquals($response1['data']['allow_cancellation'], $response2['data']['allow_cancellation']); $this->assertNotEquals($response1['data']['allow_cancellation'], $response2['data']['allow_cancellation']);
} }
/*
TypeError : Argument 1 passed to App\Transformers\SubscriptionTransformer::transform() must be an instance of App\Models\Subscription, bool given, called in /var/www/html/vendor/league/fractal/src/Scope.php on line 407
/var/www/html/app/Transformers/SubscriptionTransformer.php:35
/var/www/html/vendor/league/fractal/src/Scope.php:407
/var/www/html/vendor/league/fractal/src/Scope.php:349
/var/www/html/vendor/league/fractal/src/Scope.php:235
/var/www/html/app/Http/Controllers/BaseController.php:395
/var/www/html/app/Http/Controllers/SubscriptionController.php:408
*/
public function testSubscriptionDeleted() public function testSubscriptionDeleted()
{ {