2021-03-14 22:42:05 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @license https://opensource.org/licenses/AAL
|
|
|
|
*/
|
|
|
|
|
2021-03-25 11:55:59 +01:00
|
|
|
namespace App\Services\Subscription;
|
2021-03-14 22:42:05 +01:00
|
|
|
|
2021-03-17 03:21:06 +01:00
|
|
|
use App\DataMapper\InvoiceItem;
|
2021-04-15 04:28:31 +02:00
|
|
|
use App\Factory\CreditFactory;
|
2021-03-17 03:21:06 +01:00
|
|
|
use App\Factory\InvoiceFactory;
|
2021-03-23 13:17:28 +01:00
|
|
|
use App\Factory\InvoiceToRecurringInvoiceFactory;
|
2021-03-29 04:14:55 +02:00
|
|
|
use App\Factory\RecurringInvoiceFactory;
|
2021-04-05 14:37:25 +02:00
|
|
|
use App\Jobs\Util\SubscriptionWebhookHandler;
|
2021-03-21 06:35:09 +01:00
|
|
|
use App\Jobs\Util\SystemLogger;
|
2021-04-05 14:37:25 +02:00
|
|
|
use App\Models\Client;
|
2021-03-23 04:26:21 +01:00
|
|
|
use App\Models\ClientContact;
|
2021-03-23 13:17:28 +01:00
|
|
|
use App\Models\Invoice;
|
2021-03-17 16:12:25 +01:00
|
|
|
use App\Models\PaymentHash;
|
2021-03-17 03:21:06 +01:00
|
|
|
use App\Models\Product;
|
2021-03-29 12:43:42 +02:00
|
|
|
use App\Models\RecurringInvoice;
|
2021-03-26 22:55:04 +01:00
|
|
|
use App\Models\Subscription;
|
2021-03-21 06:35:09 +01:00
|
|
|
use App\Models\SystemLog;
|
2021-04-15 04:28:31 +02:00
|
|
|
use App\Repositories\CreditRepository;
|
2021-03-17 03:21:06 +01:00
|
|
|
use App\Repositories\InvoiceRepository;
|
2021-03-29 04:14:55 +02:00
|
|
|
use App\Repositories\RecurringInvoiceRepository;
|
2021-03-26 22:55:04 +01:00
|
|
|
use App\Repositories\SubscriptionRepository;
|
2021-04-14 06:41:04 +02:00
|
|
|
use App\Services\Subscription\ZeroCostProduct;
|
2021-04-04 14:14:33 +02:00
|
|
|
use App\Utils\Ninja;
|
2021-03-22 21:09:42 +01:00
|
|
|
use App\Utils\Traits\CleanLineItems;
|
2021-03-21 06:35:09 +01:00
|
|
|
use App\Utils\Traits\MakesHash;
|
2021-04-07 10:06:50 +02:00
|
|
|
use App\Utils\Traits\SubscriptionHooker;
|
2021-04-10 06:07:08 +02:00
|
|
|
use Carbon\Carbon;
|
2021-03-21 06:35:09 +01:00
|
|
|
use GuzzleHttp\RequestOptions;
|
2021-03-15 21:35:19 +01:00
|
|
|
|
2021-03-25 11:55:59 +01:00
|
|
|
class SubscriptionService
|
2021-03-14 22:42:05 +01:00
|
|
|
{
|
2021-03-21 06:35:09 +01:00
|
|
|
use MakesHash;
|
2021-03-22 21:09:42 +01:00
|
|
|
use CleanLineItems;
|
2021-04-07 10:06:50 +02:00
|
|
|
use SubscriptionHooker;
|
2021-03-21 06:35:09 +01:00
|
|
|
|
2021-03-25 11:55:59 +01:00
|
|
|
/** @var subscription */
|
|
|
|
private $subscription;
|
2021-03-14 22:42:05 +01:00
|
|
|
|
2021-03-25 11:55:59 +01:00
|
|
|
public function __construct(Subscription $subscription)
|
2021-03-14 22:42:05 +01:00
|
|
|
{
|
2021-03-25 11:55:59 +01:00
|
|
|
$this->subscription = $subscription;
|
2021-03-14 22:42:05 +01:00
|
|
|
}
|
|
|
|
|
2021-04-10 06:07:08 +02:00
|
|
|
/*
|
|
|
|
Performs the initial purchase of a
|
|
|
|
one time or recurring product
|
|
|
|
*/
|
2021-03-18 01:53:08 +01:00
|
|
|
public function completePurchase(PaymentHash $payment_hash)
|
|
|
|
{
|
|
|
|
|
2021-03-24 10:14:30 +01:00
|
|
|
if (!property_exists($payment_hash->data, 'billing_context')) {
|
2021-03-23 04:26:21 +01:00
|
|
|
throw new \Exception("Illegal entrypoint into method, payload must contain billing context");
|
2021-03-18 10:57:55 +01:00
|
|
|
}
|
|
|
|
|
2021-04-14 04:40:16 +02:00
|
|
|
if($payment_hash->data->billing_context->context == 'change_plan') {
|
|
|
|
return $this->handlePlanChange($payment_hash);
|
2021-04-16 05:58:14 +02:00
|
|
|
}
|
2021-04-14 04:40:16 +02:00
|
|
|
|
2021-03-26 23:05:37 +01:00
|
|
|
// if we have a recurring product - then generate a recurring invoice
|
2021-04-01 03:58:12 +02:00
|
|
|
if(strlen($this->subscription->recurring_product_ids) >=1){
|
2021-03-18 01:53:08 +01:00
|
|
|
|
2021-04-01 03:58:12 +02:00
|
|
|
$recurring_invoice = $this->convertInvoiceToRecurring($payment_hash->payment->client_id);
|
2021-04-01 04:24:05 +02:00
|
|
|
$recurring_invoice_repo = new RecurringInvoiceRepository();
|
|
|
|
|
2021-04-01 03:58:12 +02:00
|
|
|
$recurring_invoice->next_send_date = now();
|
|
|
|
$recurring_invoice = $recurring_invoice_repo->save([], $recurring_invoice);
|
|
|
|
$recurring_invoice->next_send_date = $recurring_invoice->nextSendDate();
|
|
|
|
|
|
|
|
/* Start the recurring service */
|
|
|
|
$recurring_invoice->service()
|
|
|
|
->start()
|
|
|
|
->save();
|
|
|
|
|
|
|
|
//execute any webhooks
|
2021-04-04 14:14:33 +02:00
|
|
|
|
|
|
|
$context = [
|
|
|
|
'context' => 'recurring_purchase',
|
|
|
|
'recurring_invoice' => $recurring_invoice->hashed_id,
|
2021-04-05 01:08:16 +02:00
|
|
|
'invoice' => $this->encodePrimaryKey($payment_hash->fee_invoice_id),
|
2021-04-05 01:21:54 +02:00
|
|
|
'client' => $recurring_invoice->client->hashed_id,
|
|
|
|
'subscription' => $this->subscription->hashed_id,
|
2021-04-14 04:40:16 +02:00
|
|
|
'contact' => auth('contact')->user()->hashed_id,
|
2021-04-04 14:14:33 +02:00
|
|
|
];
|
|
|
|
|
2021-04-07 00:56:36 +02:00
|
|
|
$response = $this->triggerWebhook($context);
|
|
|
|
|
2021-04-14 06:41:04 +02:00
|
|
|
// nlog($response);
|
2021-04-01 03:58:12 +02:00
|
|
|
|
2021-04-14 06:41:04 +02:00
|
|
|
$this->handleRedirect('/client/recurring_invoices/'.$recurring_invoice->hashed_id);
|
2021-04-01 03:58:12 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-04-05 01:21:54 +02:00
|
|
|
$invoice = Invoice::find($payment_hash->fee_invoice_id);
|
2021-04-01 03:58:12 +02:00
|
|
|
|
2021-04-04 14:14:33 +02:00
|
|
|
$context = [
|
|
|
|
'context' => 'single_purchase',
|
|
|
|
'invoice' => $this->encodePrimaryKey($payment_hash->fee_invoice_id),
|
2021-04-05 01:21:54 +02:00
|
|
|
'client' => $invoice->client->hashed_id,
|
|
|
|
'subscription' => $this->subscription->hashed_id,
|
2021-04-04 14:14:33 +02:00
|
|
|
];
|
|
|
|
|
2021-04-01 03:58:12 +02:00
|
|
|
//execute any webhooks
|
2021-04-04 14:14:33 +02:00
|
|
|
$this->triggerWebhook($context);
|
2021-03-18 01:53:08 +01:00
|
|
|
|
2021-04-14 06:41:04 +02:00
|
|
|
$this->handleRedirect('/client/invoices/'.$this->encodePrimaryKey($payment_hash->fee_invoice_id));
|
2021-04-01 03:58:12 +02:00
|
|
|
|
|
|
|
}
|
2021-03-18 01:53:08 +01:00
|
|
|
}
|
|
|
|
|
2021-04-06 09:55:18 +02:00
|
|
|
/* Hits the client endpoint to determine whether the user is able to access this subscription */
|
2021-04-06 08:07:35 +02:00
|
|
|
public function isEligible($contact)
|
|
|
|
{
|
2021-04-06 09:55:18 +02:00
|
|
|
$context = [
|
2021-04-06 08:07:35 +02:00
|
|
|
'context' => 'is_eligible',
|
|
|
|
'subscription' => $this->subscription->hashed_id,
|
|
|
|
'contact' => $contact->hashed_id,
|
2021-04-07 14:23:14 +02:00
|
|
|
'contact_email' => $contact->email,
|
|
|
|
'client' => $contact->client->hashed_id,
|
2021-04-06 08:07:35 +02:00
|
|
|
];
|
2021-04-06 09:55:18 +02:00
|
|
|
|
|
|
|
$response = $this->triggerWebhook($context);
|
2021-04-16 04:11:04 +02:00
|
|
|
|
2021-04-07 13:05:09 +02:00
|
|
|
return $response;
|
2021-04-06 08:07:35 +02:00
|
|
|
}
|
|
|
|
|
2021-04-07 13:26:50 +02:00
|
|
|
/* Starts the process to create a trial
|
2021-04-06 09:55:18 +02:00
|
|
|
- we create a recurring invoice, which is has its next_send_date as now() + trial_duration
|
|
|
|
- we then hit the client API end point to advise the trial payload
|
|
|
|
- we then return the user to either a predefined user endpoint, OR we return the user to the recurring invoice page.
|
|
|
|
*/
|
2021-03-18 01:53:08 +01:00
|
|
|
public function startTrial(array $data)
|
|
|
|
{
|
2021-03-22 21:09:42 +01:00
|
|
|
// Redirects from here work just fine. Livewire will respect it.
|
2021-03-29 12:19:30 +02:00
|
|
|
$client_contact = ClientContact::find($data['contact_id']);
|
2021-03-22 21:09:42 +01:00
|
|
|
|
2021-03-25 11:55:59 +01:00
|
|
|
if(!$this->subscription->trial_enabled)
|
2021-03-23 04:26:21 +01:00
|
|
|
return new \Exception("Trials are disabled for this product");
|
|
|
|
|
2021-03-29 04:14:55 +02:00
|
|
|
//create recurring invoice with start date = trial_duration + 1 day
|
|
|
|
$recurring_invoice_repo = new RecurringInvoiceRepository();
|
|
|
|
|
2021-04-01 03:58:12 +02:00
|
|
|
$recurring_invoice = $this->convertInvoiceToRecurring($client_contact->client_id);
|
2021-03-29 12:19:30 +02:00
|
|
|
$recurring_invoice->next_send_date = now()->addSeconds($this->subscription->trial_duration);
|
|
|
|
$recurring_invoice->backup = 'is_trial';
|
2021-03-23 04:26:21 +01:00
|
|
|
|
2021-04-01 16:10:26 +02:00
|
|
|
if(array_key_exists('coupon', $data) && ($data['coupon'] == $this->subscription->promo_code) && $this->subscription->promo_discount > 0)
|
2021-03-29 04:14:55 +02:00
|
|
|
{
|
2021-03-29 05:49:29 +02:00
|
|
|
$recurring_invoice->discount = $this->subscription->promo_discount;
|
|
|
|
$recurring_invoice->is_amount_discount = $this->subscription->is_amount_discount;
|
2021-04-01 16:10:26 +02:00
|
|
|
}
|
2021-03-23 04:26:21 +01:00
|
|
|
|
2021-03-29 12:19:30 +02:00
|
|
|
$recurring_invoice = $recurring_invoice_repo->save($data, $recurring_invoice);
|
2021-04-01 16:10:26 +02:00
|
|
|
|
2021-03-29 05:49:29 +02:00
|
|
|
/* Start the recurring service */
|
|
|
|
$recurring_invoice->service()
|
|
|
|
->start()
|
|
|
|
->save();
|
2021-03-23 04:26:21 +01:00
|
|
|
|
2021-04-04 14:14:33 +02:00
|
|
|
$context = [
|
|
|
|
'context' => 'trial',
|
|
|
|
'recurring_invoice' => $recurring_invoice->hashed_id,
|
2021-04-05 01:21:54 +02:00
|
|
|
'client' => $recurring_invoice->client->hashed_id,
|
|
|
|
'subscription' => $this->subscription->hashed_id,
|
2021-04-04 14:14:33 +02:00
|
|
|
];
|
|
|
|
|
2021-03-23 04:26:21 +01:00
|
|
|
//execute any webhooks
|
2021-04-06 09:55:18 +02:00
|
|
|
$response = $this->triggerWebhook($context);
|
2021-03-23 04:26:21 +01:00
|
|
|
|
2021-04-15 04:28:31 +02:00
|
|
|
return $this->handleRedirect('/client/recurring_invoices/'.$recurring_invoice->hashed_id);
|
2021-03-18 01:53:08 +01:00
|
|
|
}
|
|
|
|
|
2021-04-16 04:11:04 +02:00
|
|
|
/**
|
|
|
|
* Returns an upgrade price when moving between plans
|
|
|
|
*
|
|
|
|
* However we only allow people to move between plans
|
|
|
|
* if their account is in good standing.
|
|
|
|
*
|
|
|
|
* @param RecurringInvoice $recurring_invoice
|
|
|
|
* @param Subscription $target
|
|
|
|
*
|
|
|
|
* @return float
|
|
|
|
*/
|
2021-04-12 06:36:51 +02:00
|
|
|
public function calculateUpgradePrice(RecurringInvoice $recurring_invoice, Subscription $target) :?float
|
2021-04-10 06:07:08 +02:00
|
|
|
{
|
|
|
|
//calculate based on daily prices
|
|
|
|
|
|
|
|
$current_amount = $recurring_invoice->amount;
|
|
|
|
$currency_frequency = $recurring_invoice->frequency_id;
|
|
|
|
|
2021-04-13 11:34:59 +02:00
|
|
|
$outstanding = $recurring_invoice->invoices()
|
2021-04-10 06:07:08 +02:00
|
|
|
->where('is_deleted', 0)
|
|
|
|
->whereIn('status_id', [Invoice::STATUS_SENT, Invoice::STATUS_PARTIAL])
|
|
|
|
->where('balance', '>', 0);
|
|
|
|
|
|
|
|
$outstanding_amounts = $outstanding->sum('balance');
|
2021-04-16 04:11:04 +02:00
|
|
|
|
2021-04-14 11:55:43 +02:00
|
|
|
$outstanding_invoice = Invoice::where('subscription_id', $this->subscription->id)
|
2021-04-14 13:26:45 +02:00
|
|
|
->where('client_id', $recurring_invoice->client_id)
|
2021-04-14 11:55:43 +02:00
|
|
|
->where('is_deleted', 0)
|
|
|
|
->orderBy('id', 'desc')
|
|
|
|
->first();
|
2021-04-10 06:07:08 +02:00
|
|
|
|
|
|
|
if ($outstanding->count() == 0){
|
|
|
|
//nothing outstanding
|
2021-04-14 11:55:43 +02:00
|
|
|
return $target->price - $this->calculateProRataRefund($outstanding_invoice);
|
2021-04-10 06:07:08 +02:00
|
|
|
}
|
2021-04-11 05:46:40 +02:00
|
|
|
elseif ($outstanding->count() == 1){
|
2021-04-10 06:07:08 +02:00
|
|
|
//user has multiple amounts outstanding
|
2021-04-14 11:55:43 +02:00
|
|
|
return $target->price - $this->calculateProRataRefund($outstanding_invoice);
|
2021-04-10 06:07:08 +02:00
|
|
|
}
|
2021-04-12 06:36:51 +02:00
|
|
|
elseif ($outstanding->count() > 1) {
|
2021-04-10 06:07:08 +02:00
|
|
|
//user is changing plan mid frequency cycle
|
2021-04-11 05:46:40 +02:00
|
|
|
//we cannot handle this if there are more than one invoice outstanding.
|
2021-04-12 06:36:51 +02:00
|
|
|
return null;
|
2021-04-10 06:07:08 +02:00
|
|
|
}
|
|
|
|
|
2021-04-12 06:36:51 +02:00
|
|
|
return null;
|
|
|
|
|
2021-04-10 06:07:08 +02:00
|
|
|
}
|
|
|
|
|
2021-04-14 04:40:16 +02:00
|
|
|
/**
|
|
|
|
* We refund unused days left.
|
|
|
|
*
|
|
|
|
* @param Invoice $invoice
|
|
|
|
* @return float
|
|
|
|
*/
|
2021-04-12 06:36:51 +02:00
|
|
|
private function calculateProRataRefund($invoice) :float
|
2021-04-10 06:07:08 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
$start_date = Carbon::parse($invoice->date);
|
|
|
|
|
|
|
|
$current_date = now();
|
|
|
|
|
2021-04-14 13:26:45 +02:00
|
|
|
$days_of_subscription_used = $start_date->diffInDays($current_date);
|
2021-04-14 04:40:16 +02:00
|
|
|
|
|
|
|
$days_in_frequency = $this->getDaysInFrequency();
|
|
|
|
|
2021-04-14 13:26:45 +02:00
|
|
|
$pro_rata_refund = round((($days_in_frequency - $days_of_subscription_used)/$days_in_frequency) * $invoice->amount ,2);
|
2021-04-15 03:03:12 +02:00
|
|
|
|
|
|
|
return $pro_rata_refund;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-04-15 04:28:31 +02:00
|
|
|
/**
|
|
|
|
* Returns refundable set of line items
|
|
|
|
* transformed for direct injection into
|
|
|
|
* the invoice
|
2021-04-16 04:11:04 +02:00
|
|
|
*
|
2021-04-15 04:28:31 +02:00
|
|
|
* @param Invoice $invoice
|
|
|
|
* @return array
|
|
|
|
*/
|
2021-04-15 05:40:03 +02:00
|
|
|
private function calculateProRataRefundItems($invoice, $is_credit = false) :array
|
2021-04-15 03:03:12 +02:00
|
|
|
{
|
2021-04-15 05:40:03 +02:00
|
|
|
/* depending on whether we are creating an invoice or a credit*/
|
|
|
|
$multiplier = $is_credit ? 1 : -1;
|
|
|
|
|
2021-04-15 03:03:12 +02:00
|
|
|
$start_date = Carbon::parse($invoice->date);
|
2021-04-14 13:26:45 +02:00
|
|
|
|
2021-04-15 03:03:12 +02:00
|
|
|
$current_date = now();
|
2021-04-14 11:55:43 +02:00
|
|
|
|
2021-04-15 03:03:12 +02:00
|
|
|
$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'))
|
|
|
|
{
|
|
|
|
|
2021-04-15 05:40:03 +02:00
|
|
|
$item->cost = ($item->cost*$ratio*$multiplier);
|
2021-04-15 03:03:12 +02:00
|
|
|
$item->product_key = ctrans('texts.refund');
|
|
|
|
$item->notes = ctrans('texts.refund') . ": ". $item->notes;
|
|
|
|
|
|
|
|
|
|
|
|
$line_items[] = $item;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $line_items;
|
2021-04-14 13:26:45 +02:00
|
|
|
|
2021-04-14 04:40:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* We only charge for the used days
|
|
|
|
*
|
|
|
|
* @param Invoice $invoice
|
|
|
|
* @return float
|
|
|
|
*/
|
|
|
|
private function calculateProRataCharge($invoice) :float
|
|
|
|
{
|
|
|
|
|
|
|
|
$start_date = Carbon::parse($invoice->date);
|
|
|
|
|
|
|
|
$current_date = now();
|
|
|
|
|
2021-04-14 11:55:43 +02:00
|
|
|
$days_to_charge = $start_date->diffInDays($current_date);
|
2021-04-14 04:40:16 +02:00
|
|
|
|
2021-04-10 06:53:16 +02:00
|
|
|
$days_in_frequency = $this->getDaysInFrequency();
|
|
|
|
|
2021-04-15 04:28:31 +02:00
|
|
|
nlog("days to charge = {$days_to_charge} fays in frequency = {$days_in_frequency}");
|
2021-04-14 11:55:43 +02:00
|
|
|
|
|
|
|
$pro_rata_charge = round(($days_to_charge/$days_in_frequency) * $invoice->amount ,2);
|
2021-04-10 06:53:16 +02:00
|
|
|
|
2021-04-14 11:55:43 +02:00
|
|
|
nlog("pro rata charge = {$pro_rata_charge}");
|
|
|
|
|
|
|
|
return $pro_rata_charge;
|
2021-04-10 06:07:08 +02:00
|
|
|
}
|
2021-04-01 03:58:12 +02:00
|
|
|
|
2021-04-16 04:11:04 +02:00
|
|
|
/**
|
|
|
|
* When downgrading, we may need to create
|
|
|
|
* a credit
|
|
|
|
*
|
|
|
|
* @param array $data
|
|
|
|
*/
|
2021-04-15 04:28:31 +02:00
|
|
|
public function createChangePlanCredit($data)
|
2021-04-08 12:38:31 +02:00
|
|
|
{
|
2021-04-14 04:40:16 +02:00
|
|
|
$recurring_invoice = $data['recurring_invoice'];
|
2021-04-15 03:03:12 +02:00
|
|
|
$old_subscription = $data['subscription'];
|
2021-04-15 04:28:31 +02:00
|
|
|
$target_subscription = $data['target'];
|
|
|
|
|
2021-04-14 04:40:16 +02:00
|
|
|
$pro_rata_charge_amount = 0;
|
|
|
|
$pro_rata_refund_amount = 0;
|
|
|
|
|
2021-04-15 03:03:12 +02:00
|
|
|
$last_invoice = Invoice::where('subscription_id', $recurring_invoice->subscription_id)
|
2021-04-14 13:26:45 +02:00
|
|
|
->where('client_id', $recurring_invoice->client_id)
|
2021-04-14 04:40:16 +02:00
|
|
|
->where('is_deleted', 0)
|
2021-04-15 03:03:12 +02:00
|
|
|
->withTrashed()
|
2021-04-14 04:40:16 +02:00
|
|
|
->orderBy('id', 'desc')
|
|
|
|
->first();
|
2021-04-15 04:28:31 +02:00
|
|
|
|
2021-04-14 13:26:45 +02:00
|
|
|
if($last_invoice->balance > 0)
|
2021-04-14 04:40:16 +02:00
|
|
|
{
|
2021-04-15 03:03:12 +02:00
|
|
|
$pro_rata_charge_amount = $this->calculateProRataCharge($last_invoice, $old_subscription);
|
2021-04-14 13:26:45 +02:00
|
|
|
nlog("pro rata charge = {$pro_rata_charge_amount}");
|
2021-04-14 04:40:16 +02:00
|
|
|
}
|
|
|
|
else
|
2021-04-13 11:34:59 +02:00
|
|
|
{
|
2021-04-15 03:03:12 +02:00
|
|
|
$pro_rata_refund_amount = $this->calculateProRataRefund($last_invoice, $old_subscription) * -1;
|
2021-04-14 13:26:45 +02:00
|
|
|
nlog("pro rata refund = {$pro_rata_refund_amount}");
|
2021-04-14 04:40:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$total_payable = $pro_rata_refund_amount + $pro_rata_charge_amount + $this->subscription->price;
|
|
|
|
|
2021-04-14 13:26:45 +02:00
|
|
|
nlog("total payable = {$total_payable}");
|
|
|
|
|
2021-04-16 00:58:57 +02:00
|
|
|
$credit = $this->createCredit($last_invoice, $target_subscription);
|
2021-04-15 04:28:31 +02:00
|
|
|
|
2021-04-15 07:36:50 +02:00
|
|
|
$new_recurring_invoice = $this->createNewRecurringInvoice($recurring_invoice);
|
2021-04-15 05:40:03 +02:00
|
|
|
|
|
|
|
$context = [
|
|
|
|
'context' => 'change_plan',
|
2021-04-15 07:36:50 +02:00
|
|
|
'recurring_invoice' => $new_recurring_invoice->hashed_id,
|
2021-04-15 05:40:03 +02:00
|
|
|
'credit' => $credit->hashed_id,
|
2021-04-15 07:36:50 +02:00
|
|
|
'client' => $new_recurring_invoice->client->hashed_id,
|
2021-04-15 05:40:03 +02:00
|
|
|
'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);
|
2021-04-15 04:28:31 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-04-16 04:11:04 +02:00
|
|
|
/**
|
|
|
|
* When changing plans, we need to generate a pro rata invoice
|
|
|
|
*
|
|
|
|
* @param array $data
|
|
|
|
* @return Invoice
|
|
|
|
*/
|
2021-04-15 04:28:31 +02:00
|
|
|
public function createChangePlanInvoice($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)
|
2021-04-14 04:40:16 +02:00
|
|
|
{
|
2021-04-15 04:28:31 +02:00
|
|
|
$pro_rata_charge_amount = $this->calculateProRataCharge($last_invoice, $old_subscription);
|
|
|
|
nlog("pro rata charge = {$pro_rata_charge_amount}");
|
2021-04-14 04:40:16 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-04-15 04:28:31 +02:00
|
|
|
$pro_rata_refund_amount = $this->calculateProRataRefund($last_invoice, $old_subscription) * -1;
|
|
|
|
nlog("pro rata refund = {$pro_rata_refund_amount}");
|
2021-04-13 11:34:59 +02:00
|
|
|
}
|
|
|
|
|
2021-04-15 04:28:31 +02:00
|
|
|
$total_payable = $pro_rata_refund_amount + $pro_rata_charge_amount + $this->subscription->price;
|
|
|
|
|
2021-04-16 00:32:38 +02:00
|
|
|
return $this->proRataInvoice($last_invoice, $target_subscription);
|
2021-04-15 04:28:31 +02:00
|
|
|
|
2021-04-08 12:38:31 +02:00
|
|
|
}
|
|
|
|
|
2021-04-14 04:40:16 +02:00
|
|
|
/**
|
2021-04-16 04:11:04 +02:00
|
|
|
* Response from payment service on
|
|
|
|
* return from a plan change
|
2021-04-14 04:40:16 +02:00
|
|
|
*
|
2021-04-16 04:11:04 +02:00
|
|
|
* @param PaymentHash $payment_hash
|
2021-04-14 04:40:16 +02:00
|
|
|
*/
|
|
|
|
private function handlePlanChange($payment_hash)
|
|
|
|
{
|
2021-04-16 05:58:14 +02:00
|
|
|
|
2021-04-15 07:36:50 +02:00
|
|
|
$old_recurring_invoice = RecurringInvoice::find($payment_hash->data->billing_context->recurring_invoice);
|
2021-04-14 04:40:16 +02:00
|
|
|
|
2021-04-15 07:36:50 +02:00
|
|
|
$recurring_invoice = $this->createNewRecurringInvoice($old_recurring_invoice);
|
2021-04-14 04:40:16 +02:00
|
|
|
|
|
|
|
$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,
|
|
|
|
];
|
|
|
|
|
2021-04-16 05:58:14 +02:00
|
|
|
|
2021-04-14 04:40:16 +02:00
|
|
|
$response = $this->triggerWebhook($context);
|
|
|
|
|
|
|
|
nlog($response);
|
|
|
|
|
2021-04-15 05:40:03 +02:00
|
|
|
return $this->handleRedirect('/client/recurring_invoices/'.$recurring_invoice->hashed_id);
|
2021-04-14 04:40:16 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-04-16 04:11:04 +02:00
|
|
|
/**
|
|
|
|
* Creates a new recurring invoice when changing
|
|
|
|
* plans
|
|
|
|
*
|
|
|
|
* @param RecurringInvoice $old_recurring_invoice
|
|
|
|
* @return RecurringInvoice
|
|
|
|
*/
|
2021-04-15 07:36:50 +02:00
|
|
|
private function createNewRecurringInvoice($old_recurring_invoice) :RecurringInvoice
|
2021-04-14 04:40:16 +02:00
|
|
|
{
|
2021-04-15 07:36:50 +02:00
|
|
|
|
|
|
|
$old_recurring_invoice->service()->stop()->save();
|
2021-04-15 05:40:03 +02:00
|
|
|
|
|
|
|
$recurring_invoice_repo = new RecurringInvoiceRepository();
|
2021-04-16 05:58:14 +02:00
|
|
|
$recurring_invoice_repo->archive($old_recurring_invoice);
|
2021-04-15 05:40:03 +02:00
|
|
|
|
2021-04-15 07:36:50 +02:00
|
|
|
$recurring_invoice = $this->convertInvoiceToRecurring($old_recurring_invoice->client_id);
|
2021-04-15 05:40:03 +02:00
|
|
|
$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();
|
|
|
|
|
2021-04-15 07:36:50 +02:00
|
|
|
return $recurring_invoice;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-04-16 04:11:04 +02:00
|
|
|
/**
|
|
|
|
* Handle a plan change where no payment is required
|
|
|
|
*
|
|
|
|
* @param array $data
|
|
|
|
*/
|
2021-04-15 07:36:50 +02:00
|
|
|
public function handlePlanChangeNoPayment($data)
|
|
|
|
{
|
2021-04-16 04:11:04 +02:00
|
|
|
|
2021-04-15 07:36:50 +02:00
|
|
|
$recurring_invoice = $this->createNewRecurringInvoice($data['recurring_invoice']);
|
|
|
|
|
2021-04-15 05:40:03 +02:00
|
|
|
$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);
|
|
|
|
|
2021-04-16 04:11:04 +02:00
|
|
|
// nlog($response);
|
2021-04-14 04:40:16 +02:00
|
|
|
|
2021-04-15 05:40:03 +02:00
|
|
|
return $this->handleRedirect('/client/recurring_invoices/'.$recurring_invoice->hashed_id);
|
2021-04-14 04:40:16 +02:00
|
|
|
}
|
|
|
|
|
2021-04-16 04:11:04 +02:00
|
|
|
/**
|
|
|
|
* Creates a credit note if the plan change requires
|
|
|
|
*
|
|
|
|
* @param Invoice $last_invoice
|
|
|
|
* @param Subscription $target
|
|
|
|
* @return Credit
|
|
|
|
*/
|
2021-04-16 00:58:57 +02:00
|
|
|
private function createCredit($last_invoice, $target)
|
2021-04-15 04:28:31 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
$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);
|
|
|
|
|
2021-04-15 05:40:03 +02:00
|
|
|
$credit->line_items = array_merge($line_items, $this->calculateProRataRefundItems($last_invoice, true));
|
2021-04-15 04:28:31 +02:00
|
|
|
|
|
|
|
$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();
|
|
|
|
|
|
|
|
}
|
2021-04-16 00:58:57 +02:00
|
|
|
|
2021-04-14 04:40:16 +02:00
|
|
|
/**
|
2021-04-16 00:58:57 +02:00
|
|
|
* When changing plans we need to generate a pro rata
|
|
|
|
* invoice which takes into account any credits.
|
|
|
|
*
|
|
|
|
* @param Invoice $last_invoice
|
|
|
|
* @param Subscription $target
|
|
|
|
* @return Invoice
|
2021-04-14 04:40:16 +02:00
|
|
|
*/
|
2021-04-16 00:32:38 +02:00
|
|
|
private function proRataInvoice($last_invoice, $target)
|
2021-04-14 04:40:16 +02:00
|
|
|
{
|
|
|
|
$subscription_repo = new SubscriptionRepository();
|
|
|
|
$invoice_repo = new InvoiceRepository();
|
|
|
|
|
2021-04-14 13:26:45 +02:00
|
|
|
$invoice = InvoiceFactory::create($this->subscription->company_id, $this->subscription->user_id);
|
|
|
|
$invoice->date = now()->format('Y-m-d');
|
|
|
|
$invoice->subscription_id = $this->subscription->id;
|
|
|
|
|
2021-04-16 00:58:57 +02:00
|
|
|
$invoice->line_items = array_merge($subscription_repo->generateLineItems($target), $this->calculateProRataRefundItems($last_invoice));
|
2021-04-14 13:26:45 +02:00
|
|
|
|
2021-04-14 04:40:16 +02:00
|
|
|
$data = [
|
2021-04-14 13:26:45 +02:00
|
|
|
'client_id' => $last_invoice->client_id,
|
2021-04-14 04:40:16 +02:00
|
|
|
'quantity' => 1,
|
|
|
|
'date' => now()->format('Y-m-d'),
|
|
|
|
];
|
|
|
|
|
2021-04-16 00:32:38 +02:00
|
|
|
return $invoice_repo->save($data, $invoice)
|
|
|
|
->service()
|
|
|
|
->markSent()
|
|
|
|
->fillDefaults()
|
|
|
|
->save();
|
2021-04-14 04:40:16 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-04-16 00:32:38 +02:00
|
|
|
/**
|
|
|
|
* Generates the first invoice when a subscription is purchased
|
|
|
|
*
|
|
|
|
* @param array $data
|
|
|
|
* @return Invoice
|
|
|
|
*/
|
2021-03-17 12:06:58 +01:00
|
|
|
public function createInvoice($data): ?\App\Models\Invoice
|
2021-03-14 22:42:05 +01:00
|
|
|
{
|
2021-03-23 04:26:21 +01:00
|
|
|
|
2021-03-17 03:21:06 +01:00
|
|
|
$invoice_repo = new InvoiceRepository();
|
2021-03-26 22:55:04 +01:00
|
|
|
$subscription_repo = new SubscriptionRepository();
|
2021-03-17 03:21:06 +01:00
|
|
|
|
2021-03-26 22:55:04 +01:00
|
|
|
$invoice = InvoiceFactory::create($this->subscription->company_id, $this->subscription->user_id);
|
|
|
|
$invoice->line_items = $subscription_repo->generateLineItems($this->subscription);
|
2021-03-27 22:45:46 +01:00
|
|
|
$invoice->subscription_id = $this->subscription->id;
|
2021-03-27 23:15:12 +01:00
|
|
|
|
2021-04-01 16:10:26 +02:00
|
|
|
if(strlen($data['coupon']) >=1 && ($data['coupon'] == $this->subscription->promo_code) && $this->subscription->promo_discount > 0)
|
2021-03-26 22:55:04 +01:00
|
|
|
{
|
2021-03-27 23:15:12 +01:00
|
|
|
$invoice->discount = $this->subscription->promo_discount;
|
|
|
|
$invoice->is_amount_discount = $this->subscription->is_amount_discount;
|
2021-03-20 04:39:30 +01:00
|
|
|
}
|
|
|
|
|
2021-03-26 22:55:04 +01:00
|
|
|
return $invoice_repo->save($data, $invoice);
|
2021-03-20 03:57:03 +01:00
|
|
|
|
2021-03-20 03:49:45 +01:00
|
|
|
}
|
|
|
|
|
2021-04-16 00:32:38 +02:00
|
|
|
/**
|
|
|
|
* Generates a recurring invoice based on
|
|
|
|
* the specifications of the subscription
|
|
|
|
*
|
|
|
|
* @param int $client_id The Client Id
|
|
|
|
* @return RecurringInvoice
|
|
|
|
*/
|
2021-04-14 06:41:04 +02:00
|
|
|
public function convertInvoiceToRecurring($client_id) :RecurringInvoice
|
2021-03-15 21:35:19 +01:00
|
|
|
{
|
2021-04-01 03:58:12 +02:00
|
|
|
|
|
|
|
$subscription_repo = new SubscriptionRepository();
|
|
|
|
|
|
|
|
$recurring_invoice = RecurringInvoiceFactory::create($this->subscription->company_id, $this->subscription->user_id);
|
2021-04-01 16:10:26 +02:00
|
|
|
$recurring_invoice->client_id = $client_id;
|
2021-04-01 03:58:12 +02:00
|
|
|
$recurring_invoice->line_items = $subscription_repo->generateLineItems($this->subscription, true);
|
|
|
|
$recurring_invoice->subscription_id = $this->subscription->id;
|
|
|
|
$recurring_invoice->frequency_id = $this->subscription->frequency_id ?: RecurringInvoice::FREQUENCY_MONTHLY;
|
|
|
|
$recurring_invoice->date = now();
|
|
|
|
$recurring_invoice->remaining_cycles = -1;
|
|
|
|
|
|
|
|
return $recurring_invoice;
|
2021-03-15 21:35:19 +01:00
|
|
|
}
|
|
|
|
|
2021-04-16 00:32:38 +02:00
|
|
|
/**
|
|
|
|
* Hit a 3rd party API if defined in the subscription
|
|
|
|
*
|
|
|
|
* @param array $context
|
|
|
|
*/
|
2021-04-04 14:14:33 +02:00
|
|
|
public function triggerWebhook($context)
|
2021-03-14 22:42:05 +01:00
|
|
|
{
|
2021-04-06 08:07:35 +02:00
|
|
|
/* If no webhooks have been set, then just return gracefully */
|
2021-04-06 09:11:58 +02:00
|
|
|
if(!array_key_exists('post_purchase_url', $this->subscription->webhook_configuration) || !array_key_exists('post_purchase_rest_method', $this->subscription->webhook_configuration)) {
|
2021-04-06 09:55:18 +02:00
|
|
|
return true;
|
2021-04-06 08:07:35 +02:00
|
|
|
}
|
|
|
|
|
2021-04-06 09:55:18 +02:00
|
|
|
$response = false;
|
2021-03-23 13:17:28 +01:00
|
|
|
|
2021-04-05 04:42:49 +02:00
|
|
|
$body = array_merge($context, [
|
2021-04-07 13:26:50 +02:00
|
|
|
'company_key' => $this->subscription->company->company_key,
|
2021-04-05 04:42:49 +02:00
|
|
|
'account_key' => $this->subscription->company->account->key,
|
|
|
|
'db' => $this->subscription->company->db,
|
2021-04-07 13:26:50 +02:00
|
|
|
]);
|
2021-04-04 14:47:59 +02:00
|
|
|
|
2021-04-07 10:06:50 +02:00
|
|
|
$response = $this->sendLoad($this->subscription, $body);
|
2021-03-21 06:35:09 +01:00
|
|
|
|
2021-04-07 10:06:50 +02:00
|
|
|
/* Append the response to the system logger body */
|
|
|
|
if(is_array($response)){
|
2021-04-05 04:42:49 +02:00
|
|
|
|
2021-04-07 10:06:50 +02:00
|
|
|
$body = $response;
|
2021-04-07 13:26:50 +02:00
|
|
|
|
2021-04-05 04:42:49 +02:00
|
|
|
}
|
2021-04-07 10:06:50 +02:00
|
|
|
else {
|
2021-04-07 13:26:50 +02:00
|
|
|
|
2021-04-06 09:14:30 +02:00
|
|
|
$status = $response->getStatusCode();
|
2021-04-14 06:41:04 +02:00
|
|
|
|
|
|
|
//$response_body = $response->getReasonPhrase();
|
2021-04-06 09:14:30 +02:00
|
|
|
$body = array_merge($body, ['status' => $status, 'response_body' => $response_body]);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-04-05 14:37:25 +02:00
|
|
|
$client = \App\Models\Client::find($this->decodePrimaryKey($body['client']));
|
2021-03-21 06:35:09 +01:00
|
|
|
|
2021-04-05 14:37:25 +02:00
|
|
|
SystemLogger::dispatch(
|
|
|
|
$body,
|
|
|
|
SystemLog::CATEGORY_WEBHOOK,
|
|
|
|
SystemLog::EVENT_WEBHOOK_RESPONSE,
|
|
|
|
SystemLog::TYPE_WEBHOOK_RESPONSE,
|
|
|
|
$client,
|
|
|
|
);
|
2021-03-21 06:35:09 +01:00
|
|
|
|
2021-04-06 09:55:18 +02:00
|
|
|
return $response;
|
|
|
|
|
2021-03-14 22:42:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function fireNotifications()
|
|
|
|
{
|
|
|
|
//scan for any notification we are required to send
|
|
|
|
}
|
2021-03-17 16:12:25 +01:00
|
|
|
|
2021-04-08 04:43:31 +02:00
|
|
|
/**
|
2021-04-08 16:53:54 +02:00
|
|
|
* Get the single charge products for the
|
2021-04-08 04:43:31 +02:00
|
|
|
* subscription
|
2021-04-08 16:53:54 +02:00
|
|
|
*
|
2021-04-08 04:43:31 +02:00
|
|
|
* @return ?Product Collection
|
|
|
|
*/
|
2021-03-26 10:20:37 +01:00
|
|
|
public function products()
|
|
|
|
{
|
2021-03-26 10:38:51 +01:00
|
|
|
return Product::whereIn('id', $this->transformKeys(explode(",", $this->subscription->product_ids)))->get();
|
2021-03-26 10:20:37 +01:00
|
|
|
}
|
|
|
|
|
2021-04-08 04:43:31 +02:00
|
|
|
/**
|
2021-04-08 16:53:54 +02:00
|
|
|
* Get the recurring products for the
|
2021-04-08 04:43:31 +02:00
|
|
|
* subscription
|
2021-04-08 16:53:54 +02:00
|
|
|
*
|
2021-04-08 04:43:31 +02:00
|
|
|
* @return ?Product Collection
|
|
|
|
*/
|
2021-03-31 02:14:19 +02:00
|
|
|
public function recurring_products()
|
2021-03-26 10:20:37 +01:00
|
|
|
{
|
2021-03-26 10:38:51 +01:00
|
|
|
return Product::whereIn('id', $this->transformKeys(explode(",", $this->subscription->recurring_product_ids)))->get();
|
2021-03-26 10:20:37 +01:00
|
|
|
}
|
2021-04-07 14:23:14 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get available upgrades & downgrades for the plan.
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection
|
|
|
|
*/
|
|
|
|
public function getPlans()
|
|
|
|
{
|
|
|
|
return Subscription::query()
|
|
|
|
->where('company_id', $this->subscription->company_id)
|
|
|
|
->where('group_id', $this->subscription->group_id)
|
|
|
|
->where('id', '!=', $this->subscription->id)
|
|
|
|
->get();
|
|
|
|
}
|
|
|
|
|
2021-04-16 00:32:38 +02:00
|
|
|
/**
|
|
|
|
* Handle the cancellation of a subscription
|
|
|
|
*
|
|
|
|
* @param RecurringInvoice $recurring_invoice
|
|
|
|
*
|
|
|
|
*/
|
2021-04-15 07:36:50 +02:00
|
|
|
public function handleCancellation(RecurringInvoice $recurring_invoice)
|
2021-04-07 14:23:14 +02:00
|
|
|
{
|
2021-04-15 07:36:50 +02:00
|
|
|
|
|
|
|
//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();
|
2021-04-15 07:46:50 +02:00
|
|
|
$recurring_invoice_repo->archive($recurring_invoice);
|
2021-04-15 07:36:50 +02:00
|
|
|
|
2021-04-16 00:32:38 +02:00
|
|
|
/* Refund only if we are in the window - and there is nothing outstanding on the invoice */
|
2021-04-15 07:36:50 +02:00
|
|
|
if($refund_end_date->greaterThan(now()) && (int)$outstanding_invoice->balance == 0)
|
|
|
|
{
|
2021-04-16 00:32:38 +02:00
|
|
|
|
2021-04-15 07:36:50 +02:00
|
|
|
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],
|
|
|
|
],
|
|
|
|
|
|
|
|
];
|
|
|
|
|
2021-04-15 07:46:50 +02:00
|
|
|
$payment->refund($data);
|
2021-04-15 07:36:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$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');
|
2021-04-08 17:11:14 +02:00
|
|
|
|
2021-04-07 14:23:14 +02:00
|
|
|
}
|
2021-04-08 16:53:54 +02:00
|
|
|
|
2021-04-10 06:53:16 +02:00
|
|
|
private function getDaysInFrequency()
|
|
|
|
{
|
|
|
|
|
|
|
|
switch ($this->subscription->frequency_id) {
|
2021-04-14 11:55:43 +02:00
|
|
|
case RecurringInvoice::FREQUENCY_DAILY:
|
2021-04-10 06:53:16 +02:00
|
|
|
return 1;
|
2021-04-14 11:55:43 +02:00
|
|
|
case RecurringInvoice::FREQUENCY_WEEKLY:
|
2021-04-10 06:53:16 +02:00
|
|
|
return 7;
|
2021-04-14 11:55:43 +02:00
|
|
|
case RecurringInvoice::FREQUENCY_TWO_WEEKS:
|
2021-04-10 06:53:16 +02:00
|
|
|
return 14;
|
2021-04-14 11:55:43 +02:00
|
|
|
case RecurringInvoice::FREQUENCY_FOUR_WEEKS:
|
2021-04-10 06:53:16 +02:00
|
|
|
return now()->diffInDays(now()->addWeeks(4));
|
2021-04-14 11:55:43 +02:00
|
|
|
case RecurringInvoice::FREQUENCY_MONTHLY:
|
2021-04-10 06:53:16 +02:00
|
|
|
return now()->diffInDays(now()->addMonthNoOverflow());
|
2021-04-14 11:55:43 +02:00
|
|
|
case RecurringInvoice::FREQUENCY_TWO_MONTHS:
|
2021-04-10 06:53:16 +02:00
|
|
|
return now()->diffInDays(now()->addMonthNoOverflow(2));
|
2021-04-14 11:55:43 +02:00
|
|
|
case RecurringInvoice::FREQUENCY_THREE_MONTHS:
|
2021-04-10 06:53:16 +02:00
|
|
|
return now()->diffInDays(now()->addMonthNoOverflow(3));
|
2021-04-14 11:55:43 +02:00
|
|
|
case RecurringInvoice::FREQUENCY_FOUR_MONTHS:
|
2021-04-10 06:53:16 +02:00
|
|
|
return now()->diffInDays(now()->addMonthNoOverflow(4));
|
2021-04-14 11:55:43 +02:00
|
|
|
case RecurringInvoice::FREQUENCY_SIX_MONTHS:
|
2021-04-10 06:53:16 +02:00
|
|
|
return now()->diffInDays(now()->addMonthNoOverflow(6));
|
2021-04-14 11:55:43 +02:00
|
|
|
case RecurringInvoice::FREQUENCY_ANNUALLY:
|
2021-04-10 06:53:16 +02:00
|
|
|
return now()->diffInDays(now()->addYear());
|
2021-04-14 11:55:43 +02:00
|
|
|
case RecurringInvoice::FREQUENCY_TWO_YEARS:
|
2021-04-10 06:53:16 +02:00
|
|
|
return now()->diffInDays(now()->addYears(2));
|
2021-04-14 11:55:43 +02:00
|
|
|
case RecurringInvoice::FREQUENCY_THREE_YEARS:
|
2021-04-10 06:53:16 +02:00
|
|
|
return now()->diffInDays(now()->addYears(3));
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2021-04-14 06:41:04 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 'email' => $this->email ?? $this->contact->email,
|
|
|
|
* 'quantity' => $this->quantity,
|
|
|
|
* 'contact_id' => $this->contact->id,
|
|
|
|
*/
|
|
|
|
public function handleNoPaymentRequired(array $data)
|
|
|
|
{
|
|
|
|
|
|
|
|
$context = (new ZeroCostProduct($this->subscription, $data))->run();
|
|
|
|
|
|
|
|
// Forward payload to webhook
|
|
|
|
if(array_key_exists('context', $context))
|
|
|
|
$response = $this->triggerWebhook($context);
|
|
|
|
|
|
|
|
// Hit the redirect
|
|
|
|
return $this->handleRedirect($context['redirect_url']);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles redirecting the user
|
|
|
|
*/
|
|
|
|
private function handleRedirect($default_redirect)
|
|
|
|
{
|
2021-04-14 04:40:16 +02:00
|
|
|
|
2021-04-14 06:41:04 +02:00
|
|
|
if(array_key_exists('return_url', $this->subscription->webhook_configuration) && strlen($this->subscription->webhook_configuration['return_url']) >=1)
|
|
|
|
return redirect($this->subscription->webhook_configuration['return_url']);
|
2021-04-14 04:40:16 +02:00
|
|
|
|
2021-04-14 06:41:04 +02:00
|
|
|
return redirect($default_redirect);
|
|
|
|
}
|
2021-03-14 22:42:05 +01:00
|
|
|
}
|