1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-15 07:33:04 +01:00
invoiceninja/app/Services/Subscription/SubscriptionService.php

1528 lines
58 KiB
PHP
Raw Normal View History

2021-03-14 22:42:05 +01:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2024-04-12 06:15:41 +02:00
* @copyright Copyright (c) 2024. Invoice Ninja LLC (https://invoiceninja.com)
2021-03-14 22:42:05 +01:00
*
2021-06-16 08:58:16 +02:00
* @license https://www.elastic.co/licensing/elastic-license
2021-03-14 22:42:05 +01:00
*/
namespace App\Services\Subscription;
2021-03-14 22:42:05 +01:00
2023-03-18 08:24:56 +01:00
use App\DataMapper\InvoiceItem;
use App\Factory\CreditFactory;
use App\Factory\InvoiceFactory;
use App\Factory\PaymentFactory;
use App\Factory\RecurringInvoiceFactory;
use App\Jobs\Mail\NinjaMailer;
use App\Jobs\Mail\NinjaMailerJob;
use App\Jobs\Mail\NinjaMailerObject;
use App\Jobs\Util\SystemLogger;
use App\Libraries\MultiDB;
use App\Mail\RecurringInvoice\ClientContactRequestCancellationObject;
2021-04-05 14:37:25 +02:00
use App\Models\Client;
2023-03-18 08:24:56 +01:00
use App\Models\ClientContact;
2021-06-28 14:05:34 +02:00
use App\Models\Credit;
2021-03-23 13:17:28 +01:00
use App\Models\Invoice;
2023-03-09 10:40:49 +01:00
use App\Models\License;
2021-03-17 16:12:25 +01:00
use App\Models\PaymentHash;
use App\Models\PaymentType;
2023-03-18 08:24:56 +01:00
use App\Models\Product;
2023-03-09 10:40:49 +01:00
use App\Models\RecurringInvoice;
2023-03-18 08:24:56 +01:00
use App\Models\Subscription;
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;
use App\Repositories\PaymentRepository;
2023-03-09 10:40:49 +01:00
use App\Repositories\RecurringInvoiceRepository;
2023-03-18 08:24:56 +01:00
use App\Repositories\SubscriptionRepository;
use App\Services\Email\Email;
use App\Services\Email\EmailObject;
use App\Utils\Traits\CleanLineItems;
use App\Utils\Traits\MakesHash;
2022-03-04 04:01:09 +01:00
use App\Utils\Traits\Notifications\UserNotifies;
2023-03-18 08:24:56 +01:00
use App\Utils\Traits\SubscriptionHooker;
use Carbon\Carbon;
2021-09-08 03:04:50 +02:00
use Illuminate\Contracts\Container\BindingResolutionException;
2023-03-18 08:24:56 +01:00
use Illuminate\Support\Str;
2024-02-16 08:33:45 +01:00
use Illuminate\Mail\Mailables\Address;
2021-03-15 21:35:19 +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;
2022-03-04 04:01:09 +01:00
use UserNotifies;
2021-03-21 06:35:09 +01:00
/** @var subscription */
private $subscription;
2021-03-14 22:42:05 +01:00
2023-03-10 03:01:32 +01:00
public const WHITE_LABEL = 4316;
2023-03-09 10:40:49 +01:00
private float $credit_payments = 0;
public function __construct(Subscription $subscription)
2021-03-14 22:42:05 +01:00
{
$this->subscription = $subscription;
2021-03-14 22:42:05 +01:00
}
/*
Performs the initial purchase of a
2021-04-10 06:07:08 +02:00
one time or recurring product
*/
2021-03-18 01:53:08 +01:00
public function completePurchase(PaymentHash $payment_hash)
{
if (!property_exists($payment_hash->data, 'billing_context')) {
throw new \Exception("Illegal entrypoint into method, payload must contain billing context");
2021-03-18 10:57:55 +01:00
}
2023-02-16 02:36:09 +01:00
if ($payment_hash->data->billing_context->context == 'change_plan') {
2021-04-14 04:40:16 +02:00
return $this->handlePlanChange($payment_hash);
2021-04-16 05:58:14 +02:00
}
2021-04-14 04:40:16 +02:00
2023-03-09 10:40:49 +01:00
if ($payment_hash->data->billing_context->context == 'whitelabel') {
return $this->handleWhiteLabelPurchase($payment_hash);
}
2021-03-26 23:05:37 +01:00
// if we have a recurring product - then generate a recurring invoice
2024-01-14 05:05:00 +01:00
if (strlen($this->subscription->recurring_product_ids) >= 1) {
2023-02-16 02:36:09 +01:00
if (isset($payment_hash->data->billing_context->bundle)) {
2022-12-15 02:16:50 +01:00
$recurring_invoice = $this->convertInvoiceToRecurringBundle($payment_hash->payment->client_id, $payment_hash->data->billing_context->bundle);
2023-02-16 02:36:09 +01:00
} else {
2022-12-15 02:16:50 +01:00
$recurring_invoice = $this->convertInvoiceToRecurring($payment_hash->payment->client_id);
2023-02-16 02:36:09 +01:00
}
2022-12-15 02:16:50 +01:00
2021-04-01 04:24:05 +02:00
$recurring_invoice_repo = new RecurringInvoiceRepository();
2021-04-01 03:58:12 +02:00
$recurring_invoice = $recurring_invoice_repo->save([], $recurring_invoice);
$recurring_invoice->auto_bill = $this->subscription->auto_bill;
2024-01-14 05:05:00 +01:00
2021-04-01 03:58:12 +02:00
/* Start the recurring service */
$recurring_invoice->service()
->start()
->save();
//update the invoice and attach to the recurring invoice!!!!!
2023-01-23 08:40:04 +01:00
$invoice = Invoice::withTrashed()->find($payment_hash->fee_invoice_id);
$invoice->recurring_id = $recurring_invoice->id;
2022-12-23 01:33:14 +01:00
$invoice->is_proforma = false;
$invoice->save();
2021-04-01 03:58:12 +02:00
//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,
2022-12-19 02:38:03 +01:00
'contact' => auth()->guard('contact')->user() ? auth()->guard('contact')->user()->hashed_id : $recurring_invoice->client->contacts()->whereNotNull('email')->first()->hashed_id,
2021-12-01 06:22:22 +01:00
'account_key' => $recurring_invoice->client->custom_value2,
2021-04-04 14:14:33 +02:00
];
2023-04-29 08:04:42 +02:00
if (property_exists($payment_hash->data->billing_context, 'campaign')) {
$context['campaign'] = $payment_hash->data->billing_context->campaign;
2023-04-29 04:32:57 +02:00
}
2023-03-18 08:24:56 +01:00
$response = $this->triggerWebhook($context);
2023-03-01 01:16:18 +01:00
return $this->handleRedirect('/client/recurring_invoices/'.$recurring_invoice->hashed_id);
2023-02-16 02:36:09 +01:00
} else {
2022-03-03 23:05:21 +01:00
$invoice = Invoice::withTrashed()->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-12-01 06:22:22 +01:00
'account_key' => $invoice->client->custom_value2,
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
/* 06-04-2022 */
/* We may not be in a state where the user is present */
2024-06-28 07:16:08 +02:00
if (auth()->guard('contact')->user()) {
2023-03-01 01:16:18 +01:00
return $this->handleRedirect('/client/invoices/'.$this->encodePrimaryKey($payment_hash->fee_invoice_id));
2023-02-16 02:36:09 +01:00
}
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-12-01 06:22:22 +01:00
'account_key' => $contact->client->custom_value2,
2021-04-06 08:07:35 +02:00
];
2021-04-06 09:55:18 +02:00
$response = $this->triggerWebhook($context);
2021-04-07 13:05:09 +02:00
return $response;
2021-04-06 08:07:35 +02:00
}
2023-03-09 10:40:49 +01:00
private function handleWhiteLabelPurchase(PaymentHash $payment_hash): bool
{
//send license to the user.
$invoice = $payment_hash->fee_invoice;
2023-03-20 10:17:04 +01:00
$license_key = "v5_".Str::uuid()->toString();
2023-03-18 08:24:56 +01:00
$invoice->footer = ctrans('texts.white_label_body', ['license_key' => $license_key]);
2023-03-09 23:22:16 +01:00
$recurring_invoice = $this->convertInvoiceToRecurring($payment_hash->payment->client_id);
2024-01-14 05:05:00 +01:00
2023-03-09 23:22:16 +01:00
$recurring_invoice_repo = new RecurringInvoiceRepository();
$recurring_invoice = $recurring_invoice_repo->save([], $recurring_invoice);
$recurring_invoice->auto_bill = $this->subscription->auto_bill;
2024-01-14 05:05:00 +01:00
2023-03-09 23:22:16 +01:00
/* Start the recurring service */
$recurring_invoice->service()
->start()
->save();
//update the invoice and attach to the recurring invoice!!!!!
$invoice->recurring_id = $recurring_invoice->id;
$invoice->is_proforma = false;
2023-09-05 03:54:05 +02:00
// $invoice->service()->deletePdf();
2023-03-09 23:22:16 +01:00
$invoice->save();
2023-03-09 10:40:49 +01:00
$contact = $invoice->client->contacts()->whereNotNull('email')->first();
2024-01-14 05:05:00 +01:00
$license = new License();
2023-03-09 10:40:49 +01:00
$license->license_key = $license_key;
$license->email = $contact ? $contact->email : ' ';
$license->first_name = $contact ? $contact->first_name : ' ';
$license->last_name = $contact ? $contact->last_name : ' ';
$license->is_claimed = 1;
2024-06-28 07:16:08 +02:00
$license->transaction_reference = $payment_hash?->payment?->transaction_reference ?: ' '; //@phpstan-ignore-line
2023-03-09 10:40:49 +01:00
$license->product_id = self::WHITE_LABEL;
2023-03-09 23:22:16 +01:00
$license->recurring_invoice_id = $recurring_invoice->id;
2023-03-09 10:40:49 +01:00
$license->save();
2023-03-09 23:22:16 +01:00
$invitation = $invoice->invitations()->first();
2023-03-10 03:01:32 +01:00
2024-01-14 05:05:00 +01:00
$email_object = new EmailObject();
2024-02-16 08:33:45 +01:00
$email_object->to = [new Address($contact->email, $contact->present()->name())];
2023-03-09 10:40:49 +01:00
$email_object->subject = ctrans('texts.white_label_link') . " " .ctrans('texts.payment_subject');
2023-03-18 08:24:56 +01:00
$email_object->body = ctrans('texts.white_label_body', ['license_key' => $license_key]);
2023-03-09 10:40:49 +01:00
$email_object->client_id = $invoice->client_id;
$email_object->client_contact_id = $contact->id;
2023-08-08 11:44:52 +02:00
$email_object->invitation_key = $invitation->key;
2023-03-09 23:22:16 +01:00
$email_object->invitation_id = $invitation->id;
2023-03-09 10:40:49 +01:00
$email_object->entity_id = $invoice->id;
$email_object->entity_class = Invoice::class;
$email_object->user_id = $invoice->user_id;
Email::dispatch($email_object, $invoice->company);
return true;
}
/* 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)
2023-10-13 23:34:34 +02:00
{
2021-03-22 21:09:42 +01:00
// Redirects from here work just fine. Livewire will respect it.
2023-10-13 23:34:34 +02:00
$client_contact = ClientContact::find($this->decodePrimaryKey($data['contact_id']));
2021-03-22 21:09:42 +01:00
2023-10-26 04:57:44 +02:00
if(is_string($data['client_id'])) {
2023-10-13 23:42:09 +02:00
$data['client_id'] = $this->decodePrimaryKey($data['client_id']);
2023-10-26 04:57:44 +02:00
}
2024-01-14 05:05:00 +01:00
2023-02-16 02:36:09 +01:00
if (!$this->subscription->trial_enabled) {
return new \Exception("Trials are disabled for this product");
2023-02-16 02:36:09 +01:00
}
2021-03-29 04:14:55 +02:00
//create recurring invoice with start date = trial_duration + 1 day
$recurring_invoice_repo = new RecurringInvoiceRepository();
2023-02-16 02:36:09 +01:00
if (isset($data['bundle'])) {
$recurring_invoice = $this->convertInvoiceToRecurringBundle($client_contact->client_id, $data['bundle']->map(function ($bundle) {
return (object) $bundle;
}));
} else {
2022-12-16 06:21:14 +01:00
$recurring_invoice = $this->convertInvoiceToRecurring($client_contact->client_id);
2023-02-16 02:36:09 +01:00
}
2024-01-14 05:05:00 +01:00
2021-03-29 12:19:30 +02:00
$recurring_invoice->next_send_date = now()->addSeconds($this->subscription->trial_duration);
2022-06-15 06:38:22 +02:00
$recurring_invoice->next_send_date_client = now()->addSeconds($this->subscription->trial_duration);
2021-03-29 12:19:30 +02:00
$recurring_invoice->backup = 'is_trial';
2023-02-16 02:36:09 +01:00
if (array_key_exists('coupon', $data) && ($data['coupon'] == $this->subscription->promo_code) && $this->subscription->promo_discount > 0) {
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;
2023-10-13 23:42:09 +02:00
} elseif (strlen($this->subscription->promo_code ?? '') == 0 && $this->subscription->promo_discount > 0) {
2021-11-17 19:53:01 +01:00
$recurring_invoice->discount = $this->subscription->promo_discount;
$recurring_invoice->is_amount_discount = $this->subscription->is_amount_discount;
}
2021-03-29 12:19:30 +02:00
$recurring_invoice = $recurring_invoice_repo->save($data, $recurring_invoice);
2021-03-29 05:49:29 +02:00
/* Start the recurring service */
$recurring_invoice->service()
->start()
->save();
2023-02-16 02:36:09 +01:00
$context = [
'context' => 'trial',
'recurring_invoice' => $recurring_invoice->hashed_id,
'client' => $recurring_invoice->client->hashed_id,
'subscription' => $this->subscription->hashed_id,
'account_key' => $recurring_invoice->client->custom_value2,
];
2021-04-04 14:14:33 +02:00
//execute any webhooks
2021-04-06 09:55:18 +02:00
$response = $this->triggerWebhook($context);
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
}
/**
* 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
*/
2024-01-14 05:05:00 +01:00
public function calculateUpgradePriceV2(RecurringInvoice $recurring_invoice, Subscription $target): ?float
{
$outstanding_credit = 0;
$use_credit_setting = $recurring_invoice->client->getSetting('use_credits_payment');
$last_invoice = Invoice::query()
->where('recurring_id', $recurring_invoice->id)
->where('is_deleted', 0)
->where('status_id', Invoice::STATUS_PAID)
->first();
$refund = $this->calculateProRataRefundForSubscription($last_invoice);
2023-02-16 02:36:09 +01:00
if ($use_credit_setting != 'off') {
$outstanding_credit = Credit::query()
->where('client_id', $recurring_invoice->client_id)
->whereIn('status_id', [Credit::STATUS_SENT,Credit::STATUS_PARTIAL])
->where('is_deleted', 0)
->where('balance', '>', 0)
->sum('balance');
}
nlog("{$target->price} - {$refund} - {$outstanding_credit}");
2022-12-23 01:33:14 +01:00
return $target->price - $refund - $outstanding_credit;
}
/**
* 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
* @deprecated in favour of calculateUpgradePriceV2
* @return float
*/
2024-01-14 05:05:00 +01:00
public function calculateUpgradePrice(RecurringInvoice $recurring_invoice, Subscription $target): ?float
2021-04-10 06:07:08 +02:00
{
//calculate based on daily prices
2021-04-10 06:07:08 +02:00
$current_amount = $recurring_invoice->amount;
$currency_frequency = $recurring_invoice->frequency_id;
$outstanding = Invoice::query()
->where('recurring_id', $recurring_invoice->id)
->where('is_deleted', 0)
2023-02-16 02:36:09 +01:00
->where('is_proforma', 0)
->whereIn('status_id', [Invoice::STATUS_SENT, Invoice::STATUS_PARTIAL])
->where('balance', '>', 0);
2021-04-10 06:07:08 +02:00
$outstanding_amounts = $outstanding->sum('balance');
2023-08-06 09:35:19 +02:00
$outstanding_invoice = Invoice::query()->where('client_id', $recurring_invoice->client_id)
2021-04-14 11:55:43 +02:00
->where('is_deleted', 0)
2023-02-16 02:36:09 +01:00
->where('is_proforma', 0)
->where('subscription_id', $this->subscription->id)
2021-04-14 11:55:43 +02:00
->orderBy('id', 'desc')
->first();
2021-04-10 06:07:08 +02:00
2023-02-16 02:36:09 +01:00
//sometimes the last document could be a credit if the user is paying for their service with credits.
if (!$outstanding_invoice) {
2023-08-06 09:35:19 +02:00
$outstanding_invoice = Credit::query()->where('subscription_id', $this->subscription->id)
2023-02-16 02:36:09 +01:00
->where('client_id', $recurring_invoice->client_id)
->where('is_proforma', 0)
->where('is_deleted', 0)
->orderBy('id', 'desc')
->first();
2021-06-28 14:05:34 +02:00
}
2021-10-22 01:55:58 +02:00
//need to ensure at this point that a refund is appropriate!!
//28-02-2022
2023-02-16 02:36:09 +01:00
if ($recurring_invoice->invoices()->count() == 0) {
return $target->price;
2023-02-16 02:36:09 +01:00
} elseif ($outstanding->count() == 0) {
2021-04-10 06:07:08 +02:00
//nothing outstanding
2021-06-27 13:55:15 +02:00
return $target->price - $this->calculateProRataRefundForSubscription($outstanding_invoice);
2023-02-16 02:36:09 +01:00
} elseif ($outstanding->count() == 1) {
2021-04-10 06:07:08 +02:00
//user has multiple amounts outstanding
2021-06-27 13:55:15 +02:00
return $target->price - $this->calculateProRataRefundForSubscription($outstanding_invoice);
2023-02-16 02:36:09 +01: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-11-24 04:31:05 +01:00
return $target->price;
2021-04-10 06:07:08 +02:00
}
2021-11-24 04:31:05 +01:00
return $target->price;
2021-04-10 06:07:08 +02:00
}
2021-06-27 13:55:15 +02:00
/**
* We refund unused days left.
*
* @param Invoice $invoice
2023-02-16 02:36:09 +01:00
*
2021-06-27 13:55:15 +02:00
* @return float
*/
2024-01-14 05:05:00 +01:00
private function calculateProRataRefundForSubscription($invoice): float
2021-06-27 13:55:15 +02:00
{
2023-02-16 02:36:09 +01:00
if (!$invoice || !$invoice->date || $invoice->status_id != Invoice::STATUS_PAID) {
2021-06-27 13:55:15 +02:00
return 0;
2023-02-16 02:36:09 +01:00
}
2021-06-27 13:55:15 +02:00
/*Remove previous refunds from the calculation of the amount*/
2023-02-16 02:36:09 +01:00
$invoice->line_items = collect($invoice->line_items)->filter(function ($item) {
if ($item->product_key == ctrans("texts.refund")) {
return false;
}
return true;
})->toArray();
$amount = $invoice->calc()->getTotal();
2021-06-27 13:55:15 +02:00
$start_date = Carbon::parse($invoice->date);
$current_date = now();
2024-06-07 01:15:19 +02:00
$days_of_subscription_used = intval(abs($start_date->diffInDays($current_date)));
2021-06-27 13:55:15 +02:00
$days_in_frequency = $this->getDaysInFrequency();
2024-01-14 05:05:00 +01:00
$pro_rata_refund = round((($days_in_frequency - $days_of_subscription_used) / $days_in_frequency) * $amount, 2);
2021-06-27 13:55:15 +02:00
2022-12-23 01:33:14 +01:00
return max(0, $pro_rata_refund);
2023-02-16 02:36:09 +01:00
}
2021-06-27 13:55:15 +02:00
2021-04-14 04:40:16 +02:00
/**
* We refund unused days left.
*
* @param Invoice $invoice
* @return float
2021-04-14 04:40:16 +02:00
*/
2024-01-14 05:05:00 +01:00
private function calculateProRataRefund($invoice, $subscription = null): float
2021-04-10 06:07:08 +02:00
{
2023-02-16 02:36:09 +01:00
if (!$invoice || !$invoice->date) {
return 0;
2023-02-16 02:36:09 +01:00
}
2021-04-10 06:07:08 +02:00
$start_date = Carbon::parse($invoice->date);
$current_date = now();
2024-06-07 01:15:19 +02:00
$days_of_subscription_used = intval(abs($start_date->diffInDays($current_date)));
2021-04-14 04:40:16 +02:00
2023-03-18 08:24:56 +01:00
if ($subscription) {
$days_in_frequency = $subscription->service()->getDaysInFrequency();
2023-03-18 08:24:56 +01:00
} else {
$days_in_frequency = $this->getDaysInFrequency();
2023-03-18 08:24:56 +01:00
}
2021-04-14 04:40:16 +02:00
2023-02-16 02:36:09 +01:00
if ($days_of_subscription_used >= $days_in_frequency) {
2021-11-15 23:59:55 +01:00
return 0;
2023-02-16 02:36:09 +01:00
}
2021-11-15 23:59:55 +01:00
2024-01-14 05:05:00 +01: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
*
* @param Invoice $invoice
* @return array
2021-04-15 04:28:31 +02:00
*/
2024-01-14 05:05:00 +01:00
private function calculateProRataRefundItems($invoice, $is_credit = false): array
2021-04-15 03:03:12 +02:00
{
2023-02-16 02:36:09 +01:00
if (!$invoice) {
2021-06-29 08:05:03 +02:00
return [];
2023-02-16 02:36:09 +01:00
}
2021-06-29 08:05:03 +02:00
$handle_discount = false;
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
2024-06-07 01:15:19 +02:00
$days_of_subscription_used = intval(abs($start_date->diffInDays($current_date)));
2021-04-15 03:03:12 +02:00
2021-11-15 23:59:55 +01:00
$days_in_frequency = $invoice->subscription->service()->getDaysInFrequency();
2021-04-15 03:03:12 +02:00
2024-01-14 05:05:00 +01:00
$ratio = ($days_in_frequency - $days_of_subscription_used) / $days_in_frequency;
2021-04-15 03:03:12 +02:00
$line_items = [];
//Handle when we are refunding a discounted invoice. Need to consider the
//total discount and also the line item discount.
2023-02-16 02:36:09 +01:00
if ($invoice->discount > 0) {
$handle_discount = true;
2023-02-16 02:36:09 +01:00
}
2023-02-16 02:36:09 +01:00
foreach ($invoice->line_items as $item) {
if ($item->product_key != ctrans('texts.refund') && ($item->type_id == "1" || $item->type_id == "2")) {
$discount_ratio = 1;
2023-02-16 02:36:09 +01:00
if ($handle_discount) {
$discount_ratio = $this->calculateDiscountRatio($invoice);
2023-02-16 02:36:09 +01:00
}
2024-01-14 05:05:00 +01:00
$item->cost = ($item->cost * $ratio * $multiplier * $discount_ratio);
2021-04-15 03:03:12 +02:00
$item->product_key = ctrans('texts.refund');
$item->notes = ctrans('texts.refund') . ": ". $item->notes;
2021-04-15 03:03:12 +02:00
$line_items[] = $item;
}
}
return $line_items;
2021-04-14 04:40:16 +02:00
}
/**
* We only charge for the used days
*
* @param Invoice $invoice
* @return float
*/
2024-01-14 05:05:00 +01:00
public function calculateDiscountRatio($invoice): float
{
2023-02-16 02:36:09 +01:00
if ($invoice->is_amount_discount) {
return $invoice->discount / ($invoice->amount + $invoice->discount);
2023-02-16 02:36:09 +01:00
} else {
return $invoice->discount / 100;
2023-02-16 02:36:09 +01:00
}
}
2021-04-14 04:40:16 +02:00
/**
* We only charge for the used days
*
* @param Invoice $invoice
* @return float
2021-04-14 04:40:16 +02:00
*/
2024-01-14 05:05:00 +01:00
private function calculateProRataCharge($invoice): float
2021-04-14 04:40:16 +02:00
{
$start_date = Carbon::parse($invoice->date);
$current_date = now();
2024-06-07 01:15:19 +02:00
$days_to_charge = intval(abs($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-06-02 10:55:33 +02:00
nlog("days to charge = {$days_to_charge} days in frequency = {$days_in_frequency}");
2021-04-14 11:55:43 +02:00
2024-01-14 05:05:00 +01:00
$pro_rata_charge = round(($days_to_charge / $days_in_frequency) * $invoice->amount, 2);
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
/**
* This entry point assumes the user does not have to make a
* payment for the service.
2023-02-16 02:36:09 +01:00
*
* In this case, we generate a credit note for the old service
* Generate a new invoice for the new service
2023-02-16 02:36:09 +01:00
* Apply credits to the invoice
*
* @param array $data
*/
public function createChangePlanCreditV2($data)
{
/* Init vars */
$recurring_invoice = $data['recurring_invoice'];
$old_subscription = $data['subscription'];
$target_subscription = $data['target'];
$pro_rata_charge_amount = 0;
$pro_rata_refund_amount = 0;
$is_credit = false;
2022-12-23 01:33:14 +01:00
$credit = false;
/* Get last invoice */
2023-08-06 09:03:12 +02:00
$last_invoice = Invoice::query()->where('subscription_id', $recurring_invoice->subscription_id)
->where('client_id', $recurring_invoice->client_id)
2023-02-16 02:36:09 +01:00
->where('is_proforma', 0)
->where('is_deleted', 0)
->whereIn('status_id', [Invoice::STATUS_SENT, Invoice::STATUS_PARTIAL, Invoice::STATUS_PAID])
->withTrashed()
->orderBy('id', 'desc')
->first();
//if last payment was created from a credit, do not generate a new credit, refund the old one.
2024-01-14 05:05:00 +01:00
2023-02-16 02:36:09 +01:00
if ($last_invoice) {
$last_invoice->payments->each(function ($payment) {
$payment->credits()->where('is_deleted', 0)->each(function ($credit) {
$this->credit_payments += $credit->pivot->sum('amount');
});
});
$invoice_repo = new InvoiceRepository();
$invoice_repo->delete($last_invoice);
$payment_repo = new PaymentRepository(new CreditRepository());
2023-02-16 02:36:09 +01:00
$last_invoice->payments->each(function ($payment) use ($payment_repo) {
$payment_repo->delete($payment);
});
}
//if there are existing credit payments, then we refund directly to the credit.
2023-02-16 02:36:09 +01:00
if ($this->calculateProRataRefundForSubscription($last_invoice) > 0 && $this->credit_payments == 0) {
$credit = $this->createCredit($last_invoice, $target_subscription, false);
2023-02-16 02:36:09 +01:00
}
$new_recurring_invoice = $this->createNewRecurringInvoice($recurring_invoice);
$invoice = $this->changePlanInvoice($target_subscription, $recurring_invoice->client_id);
$invoice->recurring_id = $new_recurring_invoice->id;
$invoice->is_proforma = false;
$invoice->save();
$payment = PaymentFactory::create($invoice->company_id, $invoice->user_id, $invoice->client_id);
$payment->type_id = PaymentType::CREDIT;
$payment->client_id = $invoice->client_id;
$payment->is_manual = true;
$payment->save();
$payment->service()->applyNumber()->applyCreditsToInvoice($invoice);
2023-02-16 02:36:09 +01:00
$context = [
'context' => 'change_plan',
'recurring_invoice' => $new_recurring_invoice->hashed_id,
'credit' => $credit ? $credit->hashed_id : null,
'client' => $new_recurring_invoice->client->hashed_id,
'subscription' => $target_subscription->hashed_id,
'contact' => auth()->guard('contact')->user()->hashed_id,
'account_key' => $new_recurring_invoice->client->custom_value2,
];
2023-02-16 02:36:09 +01:00
$response = $this->triggerWebhook($context);
2023-02-16 02:36:09 +01:00
return '/client/recurring_invoices/'.$new_recurring_invoice->hashed_id;
}
/**
* When downgrading, we may need to create
* a credit
*
* @deprecated in favour of createChangePlanCreditV2
* @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-07-15 13:00:24 +02:00
$is_credit = false;
2021-04-14 04:40:16 +02:00
2023-08-06 09:35:19 +02:00
$last_invoice = Invoice::query()->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
2023-02-16 02:36:09 +01:00
if ($recurring_invoice->invoices()->count() == 0) {
2022-02-28 22:39:30 +01:00
$pro_rata_refund_amount = 0;
2023-02-16 02:36:09 +01:00
} elseif (!$last_invoice) {
2021-07-15 13:00:24 +02:00
$is_credit = true;
2023-08-06 09:35:19 +02:00
$last_invoice = Credit::query()->where('subscription_id', $recurring_invoice->subscription_id)
2021-07-15 13:00:24 +02:00
->where('client_id', $recurring_invoice->client_id)
->where('is_deleted', 0)
->withTrashed()
->orderBy('id', 'desc')
2023-02-16 02:36:09 +01:00
->first();
2021-07-15 13:00:24 +02:00
$pro_rata_refund_amount = $this->calculateProRataRefund($last_invoice, $old_subscription);
2023-02-16 02:36:09 +01:00
} elseif ($last_invoice->balance > 0) {
2023-08-08 11:44:52 +02:00
$pro_rata_charge_amount = $this->calculateProRataCharge($last_invoice);
2021-04-14 13:26:45 +02:00
nlog("pro rata charge = {$pro_rata_charge_amount}");
2023-02-16 02:36:09 +01:00
} else {
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-11-15 23:59:55 +01:00
$credit = false;
2021-10-22 01:55:58 +02:00
/* Only generate a credit if the previous invoice was paid in full. */
2023-02-16 02:36:09 +01:00
if ($last_invoice && $last_invoice->balance == 0) {
2021-10-22 01:55:58 +02:00
$credit = $this->createCredit($last_invoice, $target_subscription, $is_credit);
2023-02-16 02:36:09 +01:00
}
$new_recurring_invoice = $this->createNewRecurringInvoice($recurring_invoice);
2021-04-15 05:40:03 +02:00
2023-02-16 02:36:09 +01:00
$context = [
'context' => 'change_plan',
'recurring_invoice' => $new_recurring_invoice->hashed_id,
'credit' => $credit ? $credit->hashed_id : null,
'client' => $new_recurring_invoice->client->hashed_id,
'subscription' => $target_subscription->hashed_id,
'contact' => auth()->guard('contact')->user()->hashed_id,
'account_key' => $new_recurring_invoice->client->custom_value2,
];
2021-04-15 05:40:03 +02:00
2023-02-16 02:36:09 +01:00
$response = $this->triggerWebhook($context);
2021-04-15 05:40:03 +02:00
2023-02-16 02:36:09 +01:00
nlog($response);
2021-04-15 04:28:31 +02:00
2023-02-16 02:36:09 +01:00
if ($credit) {
return '/client/credits/'.$credit->hashed_id;
} else {
return '/client/credits';
}
2021-04-15 04:28:31 +02:00
}
2021-06-27 13:55:15 +02:00
public function changePlanPaymentCheck($data)
{
$recurring_invoice = $data['recurring_invoice'];
$old_subscription = $data['subscription'];
$target_subscription = $data['target'];
$pro_rata_charge_amount = 0;
$pro_rata_refund_amount = 0;
2023-08-06 09:35:19 +02:00
$last_invoice = Invoice::query()->where('subscription_id', $recurring_invoice->subscription_id)
2021-06-27 13:55:15 +02:00
->where('client_id', $recurring_invoice->client_id)
2023-02-16 02:36:09 +01:00
->where('is_proforma', 0)
2021-06-27 13:55:15 +02:00
->where('is_deleted', 0)
->withTrashed()
->orderBy('id', 'desc')
->first();
2023-02-16 02:36:09 +01:00
if (!$last_invoice) {
2021-06-29 08:05:03 +02:00
return true;
2023-02-16 02:36:09 +01:00
}
2021-06-27 13:55:15 +02:00
2023-02-16 02:36:09 +01:00
if ($last_invoice->balance > 0) {
2023-08-08 11:44:52 +02:00
$pro_rata_charge_amount = $this->calculateProRataCharge($last_invoice);
2021-06-27 13:55:15 +02:00
nlog("pro rata charge = {$pro_rata_charge_amount}");
2023-02-16 02:36:09 +01:00
} else {
2021-06-27 13:55:15 +02:00
$pro_rata_refund_amount = $this->calculateProRataRefund($last_invoice, $old_subscription) * -1;
nlog("pro rata refund = {$pro_rata_refund_amount}");
}
2023-01-19 02:16:51 +01:00
nlog("{$pro_rata_refund_amount} + {$pro_rata_charge_amount} + {$this->subscription->price}");
2024-01-14 05:05:00 +01:00
2021-06-27 13:55:15 +02:00
$total_payable = $pro_rata_refund_amount + $pro_rata_charge_amount + $this->subscription->price;
2023-02-16 02:36:09 +01:00
if ($total_payable > 0) {
2021-06-27 13:55:15 +02:00
return true;
2023-02-16 02:36:09 +01:00
}
2021-06-27 13:55:15 +02:00
return false;
}
/**
* When changing plans, we need to generate a pro rata invoice
*
2024-02-08 08:42:38 +01:00
* @param array $data{recurring_invoice: RecurringInvoice, subscription: Subscription, target: Subscription}
* @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;
2023-08-06 09:35:19 +02:00
$last_invoice = Invoice::query()->where('subscription_id', $recurring_invoice->subscription_id)
2021-04-15 04:28:31 +02:00
->where('client_id', $recurring_invoice->client_id)
->where('is_deleted', 0)
2023-02-16 02:36:09 +01:00
->where('is_proforma', 0)
2021-04-15 04:28:31 +02:00
->withTrashed()
->orderBy('id', 'desc')
->first();
2021-04-15 04:28:31 +02:00
2023-02-16 02:36:09 +01:00
if (!$last_invoice) {
2021-06-29 08:05:03 +02:00
//do nothing
2023-02-16 02:36:09 +01:00
} elseif ($last_invoice->balance > 0) {
2023-01-12 04:21:54 +01:00
$last_invoice = null;
2024-01-14 05:05:00 +01:00
// $pro_rata_charge_amount = $this->calculateProRataCharge($last_invoice, $old_subscription);
// nlog("pro rata charge = {$pro_rata_charge_amount}");
2023-02-16 02:36:09 +01: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-07-14 01:31:01 +02:00
return $this->proRataInvoice($last_invoice, $target_subscription, $recurring_invoice->client_id);
2021-04-08 12:38:31 +02:00
}
2021-04-14 04:40:16 +02:00
/**
* Response from payment service on
* return from a plan change
*
* @param PaymentHash $payment_hash
2021-04-14 04:40:16 +02:00
*/
private function handlePlanChange($payment_hash)
{
2021-07-15 13:00:24 +02:00
nlog("handle plan change");
2021-07-15 06:35:22 +02:00
2023-08-08 11:44:52 +02:00
$old_recurring_invoice = RecurringInvoice::query()->find($this->decodePrimaryKey($payment_hash->data->billing_context->recurring_invoice));
2021-04-14 04:40:16 +02:00
2023-02-16 02:36:09 +01:00
if (!$old_recurring_invoice) {
2021-11-15 23:59:55 +01:00
return $this->handleRedirect('/client/recurring_invoices/');
2023-02-16 02:36:09 +01:00
}
2021-11-15 23:59:55 +01:00
$recurring_invoice = $this->createNewRecurringInvoice($old_recurring_invoice);
2021-04-14 04:40:16 +02:00
//update the invoice and attach to the recurring invoice!!!!!
2023-08-08 11:44:52 +02:00
$invoice = Invoice::query()->find($payment_hash->fee_invoice_id);
$invoice->recurring_id = $recurring_invoice->id;
$invoice->is_proforma = false;
$invoice->save();
2023-06-30 00:54:33 +02:00
// 29-06-2023 handle webhooks for payment intent - user may not be present.
2024-01-14 05:05:00 +01:00
2021-07-15 13:00:24 +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,
2023-06-30 00:54:33 +02:00
'contact' => auth()->guard('contact')->user()?->hashed_id ?? $recurring_invoice->client->contacts()->first()->hashed_id,
2021-12-01 06:22:22 +01:00
'account_key' => $recurring_invoice->client->custom_value2,
2021-07-15 13:00:24 +02:00
];
2021-04-14 04:40:16 +02:00
2021-04-16 05:58:14 +02:00
2021-07-15 13:00:24 +02:00
$response = $this->triggerWebhook($context);
2021-04-14 04:40:16 +02:00
2021-07-15 13:00:24 +02:00
nlog($response);
2021-04-14 04:40:16 +02:00
2021-07-15 13:00:24 +02:00
return $this->handleRedirect('/client/recurring_invoices/'.$recurring_invoice->hashed_id);
2021-04-14 04:40:16 +02:00
}
/**
* Creates a new recurring invoice when changing
* plans
*
* @param RecurringInvoice $old_recurring_invoice
* @return RecurringInvoice
*/
2024-01-14 05:05:00 +01:00
public function createNewRecurringInvoice($old_recurring_invoice): RecurringInvoice
2021-04-14 04:40:16 +02:00
{
$old_recurring_invoice->service()->stop()->save();
2021-04-15 05:40:03 +02:00
$recurring_invoice_repo = new RecurringInvoiceRepository();
2021-10-22 01:55:58 +02:00
$recurring_invoice_repo->delete($old_recurring_invoice);
2021-04-15 05:40:03 +02:00
2023-02-16 02:36:09 +01:00
$recurring_invoice = $this->convertInvoiceToRecurring($old_recurring_invoice->client_id);
$recurring_invoice = $recurring_invoice_repo->save([], $recurring_invoice);
$recurring_invoice->next_send_date = now()->format('Y-m-d');
$recurring_invoice->next_send_date_client = now()->format('Y-m-d');
$recurring_invoice->next_send_date = $recurring_invoice->nextSendDate();
$recurring_invoice->next_send_date_client = $recurring_invoice->nextSendDateClient();
2021-04-15 05:40:03 +02:00
2023-02-16 02:36:09 +01:00
/* Start the recurring service */
$recurring_invoice->service()
->start()
->save();
2023-02-16 02:36:09 +01:00
return $recurring_invoice;
}
/**
* Creates a credit note if the plan change requires
*
* @param Invoice $last_invoice
* @param Subscription $target
* @return Credit
*/
2021-07-15 13:00:24 +02:00
private function createCredit($last_invoice, $target, $is_credit = false)
2021-04-15 04:28:31 +02:00
{
2021-07-15 13:00:24 +02:00
$last_invoice_is_credit = $is_credit ? false : true;
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->status_id = Credit::STATUS_SENT;
2021-04-15 04:28:31 +02:00
$credit->date = now()->format('Y-m-d');
$credit->subscription_id = $this->subscription->id;
$credit->discount = $last_invoice->discount;
$credit->is_amount_discount = $last_invoice->is_amount_discount;
2024-01-14 05:05:00 +01:00
$credit->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-07-14 01:31:01 +02:00
private function proRataInvoice($last_invoice, $target, $client_id)
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');
2021-07-15 13:00:24 +02:00
$invoice->subscription_id = $target->id;
2021-04-14 13:26:45 +02:00
2021-04-16 00:58:57 +02:00
$invoice->line_items = array_merge($subscription_repo->generateLineItems($target), $this->calculateProRataRefundItems($last_invoice));
2022-12-23 01:33:14 +01:00
$invoice->is_proforma = true;
2021-04-14 13:26:45 +02:00
2021-04-14 04:40:16 +02:00
$data = [
2021-07-14 01:31:01 +02:00
'client_id' => $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();
2022-12-12 05:27:22 +01:00
}
/**
* When changing plans we need to generate a pro rata
* invoice which takes into account any credits.
*
* @param Subscription $target
* @return Invoice
*/
private function changePlanInvoice($target, $client_id)
{
$subscription_repo = new SubscriptionRepository();
$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 = $target->id;
$invoice->line_items = $subscription_repo->generateLineItems($target);
2022-12-23 01:33:14 +01:00
$invoice->is_proforma = true;
$data = [
'client_id' => $client_id,
'quantity' => 1,
'date' => now()->format('Y-m-d'),
];
$invoice_repo->save($data, $invoice)
->service()
->markSent()
->fillDefaults()
->save();
2023-10-26 04:57:44 +02:00
if($invoice->fresh()->balance == 0) {
$invoice->service()->markPaid()->save();
}
return $invoice->fresh();
}
2022-12-12 06:00:39 +01:00
public function createInvoiceV2($bundle, $client_id, $valid_coupon = false)
2022-12-12 05:27:22 +01:00
{
$invoice_repo = new InvoiceRepository();
$subscription_repo = new SubscriptionRepository();
$invoice = InvoiceFactory::create($this->subscription->company_id, $this->subscription->user_id);
$invoice->subscription_id = $this->subscription->id;
2022-12-12 06:00:39 +01:00
$invoice->client_id = $client_id;
$invoice->is_proforma = true;
2023-02-16 02:36:09 +01:00
$invoice->number = "####" . ctrans('texts.subscription') . "_" . now()->format('Y-m-d') . "_" . rand(0, 100000);
$line_items = $bundle->map(function ($item) {
2024-01-14 05:05:00 +01:00
$line_item = new InvoiceItem();
2022-12-12 06:00:39 +01:00
$line_item->product_key = $item['product_key'];
2022-12-15 02:16:50 +01:00
$line_item->quantity = (float)$item['qty'];
$line_item->cost = (float)$item['unit_cost'];
2022-12-12 06:00:39 +01:00
$line_item->notes = $item['description'];
2022-12-12 05:27:22 +01:00
2022-12-12 06:00:39 +01:00
return $line_item;
2022-12-12 05:27:22 +01:00
})->toArray();
$invoice->line_items = $line_items;
2024-01-14 05:05:00 +01:00
2023-02-16 02:36:09 +01:00
if ($valid_coupon) {
2022-12-12 06:00:39 +01:00
$invoice->discount = $this->subscription->promo_discount;
$invoice->is_amount_discount = $this->subscription->is_amount_discount;
}
2022-12-12 05:27:22 +01:00
2022-12-12 06:00:39 +01:00
return $invoice_repo->save([], $invoice);
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-04-16 00:32:38 +02:00
*/
2022-07-06 09:12:29 +02:00
public function createInvoice($data, $quantity = 1): ?\App\Models\Invoice
2021-03-14 22:42:05 +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();
2022-07-06 09:12:29 +02:00
$subscription_repo->quantity = $quantity;
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;
$invoice->is_proforma = true;
2021-03-27 23:15:12 +01:00
2024-01-14 05:05:00 +01:00
if (strlen($data['coupon']) >= 1 && ($data['coupon'] == $this->subscription->promo_code) && $this->subscription->promo_discount > 0) {
2021-03-27 23:15:12 +01:00
$invoice->discount = $this->subscription->promo_discount;
$invoice->is_amount_discount = $this->subscription->is_amount_discount;
2023-02-16 02:36:09 +01:00
} elseif (strlen($this->subscription->promo_code) == 0 && $this->subscription->promo_discount > 0) {
2021-11-17 19:53:01 +01:00
$invoice->discount = $this->subscription->promo_discount;
$invoice->is_amount_discount = $this->subscription->is_amount_discount;
}
2021-03-26 22:55:04 +01:00
return $invoice_repo->save($data, $invoice);
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
*
2021-04-16 00:32:38 +02:00
* @param int $client_id The Client Id
* @return RecurringInvoice
2021-04-16 00:32:38 +02:00
*/
2024-01-14 05:05:00 +01:00
public function convertInvoiceToRecurring($client_id): RecurringInvoice
2021-03-15 21:35:19 +01:00
{
2021-11-22 02:53:40 +01:00
MultiDB::setDb($this->subscription->company->db);
2024-01-14 05:05:00 +01:00
2021-11-21 21:20:53 +01:00
$client = Client::withTrashed()->find($client_id);
2021-10-12 11:54:52 +02: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);
$recurring_invoice->client_id = $client_id;
2021-11-15 23:59:55 +01:00
$recurring_invoice->line_items = $subscription_repo->generateLineItems($this->subscription, true, false);
2021-04-01 03:58:12 +02:00
$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;
2021-10-12 11:54:52 +02:00
$recurring_invoice->auto_bill = $client->getSetting('auto_bill');
$recurring_invoice->auto_bill_enabled = $this->setAutoBillFlag($recurring_invoice->auto_bill);
$recurring_invoice->due_date_days = 'terms';
2021-11-16 04:14:15 +01:00
$recurring_invoice->next_send_date = now()->format('Y-m-d');
2022-06-15 06:38:22 +02:00
$recurring_invoice->next_send_date_client = now()->format('Y-m-d');
2021-11-16 04:14:15 +01:00
$recurring_invoice->next_send_date = $recurring_invoice->nextSendDate();
2022-06-15 06:38:22 +02:00
$recurring_invoice->next_send_date_client = $recurring_invoice->nextSendDateClient();
2021-04-01 03:58:12 +02:00
return $recurring_invoice;
2021-03-15 21:35:19 +01:00
}
2022-12-15 02:16:50 +01:00
/**
* Generates a recurring invoice based on
* the specifications of the subscription USING BUNDLE
*
* @param int $client_id The Client Id
* @return RecurringInvoice
*/
2024-01-14 05:05:00 +01:00
public function convertInvoiceToRecurringBundle($client_id, $bundle): RecurringInvoice
2022-12-15 02:16:50 +01:00
{
MultiDB::setDb($this->subscription->company->db);
2024-01-14 05:05:00 +01:00
2022-12-15 02:16:50 +01:00
$client = Client::withTrashed()->find($client_id);
$subscription_repo = new SubscriptionRepository();
$recurring_invoice = RecurringInvoiceFactory::create($this->subscription->company_id, $this->subscription->user_id);
$recurring_invoice->client_id = $client_id;
$recurring_invoice->line_items = $subscription_repo->generateBundleLineItems($bundle, true, false);
$recurring_invoice->subscription_id = $this->subscription->id;
$recurring_invoice->frequency_id = $this->subscription->frequency_id ?: RecurringInvoice::FREQUENCY_MONTHLY;
2024-02-08 08:42:38 +01:00
$recurring_invoice->date = now()->addSeconds($client->timezone_offset());
2022-12-15 02:16:50 +01:00
$recurring_invoice->remaining_cycles = -1;
$recurring_invoice->auto_bill = $client->getSetting('auto_bill');
$recurring_invoice->auto_bill_enabled = $this->setAutoBillFlag($recurring_invoice->auto_bill);
$recurring_invoice->due_date_days = 'terms';
2024-02-08 08:42:38 +01:00
$recurring_invoice->next_send_date = now()->addSeconds($client->timezone_offset())->format('Y-m-d');
2022-12-15 02:16:50 +01:00
$recurring_invoice->next_send_date_client = now()->format('Y-m-d');
$recurring_invoice->next_send_date = $recurring_invoice->nextSendDate();
$recurring_invoice->next_send_date_client = $recurring_invoice->nextSendDateClient();
return $recurring_invoice;
}
2023-03-17 02:49:08 +01:00
private function setAutoBillFlag($auto_bill): bool
2021-10-12 11:54:52 +02:00
{
if ($auto_bill == 'always' || $auto_bill == 'optout') {
return true;
}
return false;
}
2021-04-16 00:32:38 +02:00
/**
* Hit a 3rd party API if defined in the subscription
*
* @param array $context
2021-04-16 00:32:38 +02:00
*/
2021-04-04 14:14:33 +02:00
public function triggerWebhook($context)
2021-03-14 22:42:05 +01:00
{
2024-06-27 06:57:15 +02:00
if (empty($this->subscription->webhook_configuration['post_purchase_url']) || is_null($this->subscription->webhook_configuration['post_purchase_url']) || strlen($this->subscription->webhook_configuration['post_purchase_url']) < 1) { //@phpstan-ignore-line
2021-06-03 23:43:22 +02:00
return ["message" => "Success", "status_code" => 200];
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, [
'db' => $this->subscription->company->db,
]);
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 */
2023-02-16 02:36:09 +01:00
if (is_array($response)) {
2021-04-07 10:06:50 +02:00
$body = $response;
2023-02-16 02:36:09 +01:00
} else {
2021-06-03 23:43:22 +02:00
$body = $response->getStatusCode();
}
2023-08-06 09:35:19 +02:00
$client = Client::query()->where('id', $this->decodePrimaryKey($body['client']))->withTrashed()->first();
2021-03-21 06:35:09 +01:00
2023-02-16 02:36:09 +01:00
SystemLogger::dispatch(
$body,
SystemLog::CATEGORY_WEBHOOK,
SystemLog::EVENT_WEBHOOK_RESPONSE,
SystemLog::TYPE_WEBHOOK_RESPONSE,
$client,
$client->company,
);
2024-01-14 05:05:00 +01:00
2021-07-15 03:06:14 +02:00
nlog("ready to fire back");
2021-03-21 06:35:09 +01:00
2023-02-16 02:36:09 +01:00
if (is_array($body)) {
return $response;
} else {
return ['message' => 'There was a problem encountered with the webhook', 'status_code' => 500];
}
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
/**
* Get the single charge products for the
2021-04-08 04:43:31 +02:00
* subscription
*
2021-04-08 04:43:31 +02:00
*/
public function products()
{
2023-02-16 02:36:09 +01:00
if (!$this->subscription->product_ids) {
2021-08-11 01:16:35 +02:00
return collect();
2023-02-16 02:36:09 +01:00
}
2021-08-11 01:16:35 +02:00
$keys = $this->transformKeys(explode(",", $this->subscription->product_ids));
2023-02-16 02:36:09 +01:00
if (is_array($keys)) {
2023-08-06 09:03:12 +02:00
return Product::query()->whereIn('id', $keys)->get();
2023-02-16 02:36:09 +01:00
} else {
2023-08-06 09:35:19 +02:00
return Product::query()->where('id', $keys)->get();
2023-02-16 02:36:09 +01:00
}
}
2021-04-08 04:43:31 +02:00
/**
* Get the recurring products for the
2021-04-08 04:43:31 +02:00
* subscription
*
2021-04-08 04:43:31 +02:00
*/
2021-03-31 02:14:19 +02:00
public function recurring_products()
{
2023-02-16 02:36:09 +01:00
if (!$this->subscription->recurring_product_ids) {
2021-08-11 01:16:35 +02:00
return collect();
2023-02-16 02:36:09 +01:00
}
2021-08-11 01:16:35 +02:00
$keys = $this->transformKeys(explode(",", $this->subscription->recurring_product_ids));
2023-02-16 02:36:09 +01:00
if (is_array($keys)) {
2023-08-06 09:03:12 +02:00
return Product::query()->whereIn('id', $keys)->get();
2023-02-16 02:36:09 +01:00
} else {
2023-08-06 09:35:19 +02:00
return Product::query()->where('id', $keys)->get();
2021-08-11 01:16:35 +02:00
}
}
2021-04-07 14:23:14 +02:00
2022-12-01 07:05:27 +01:00
/* OPTIONAL PRODUCTS*/
/**
* Get the single charge products for the
* subscription
*
*/
public function optional_products()
{
2023-02-16 02:36:09 +01:00
if (!$this->subscription->optional_product_ids) {
2022-12-01 07:05:27 +01:00
return collect();
2023-02-16 02:36:09 +01:00
}
2022-12-01 07:05:27 +01:00
$keys = $this->transformKeys(explode(",", $this->subscription->optional_product_ids));
2023-02-16 02:36:09 +01:00
if (is_array($keys)) {
2023-08-06 09:03:12 +02:00
return Product::query()->whereIn('id', $keys)->get();
2023-02-16 02:36:09 +01:00
} else {
2023-08-06 09:35:19 +02:00
return Product::query()->where('id', $keys)->get();
2023-02-16 02:36:09 +01:00
}
2022-12-01 07:05:27 +01:00
}
/**
* Get the recurring products for the
* subscription
*
*/
public function optional_recurring_products()
{
2023-02-16 02:36:09 +01:00
if (!$this->subscription->optional_recurring_product_ids) {
2022-12-01 07:05:27 +01:00
return collect();
2023-02-16 02:36:09 +01:00
}
2022-12-01 07:05:27 +01:00
$keys = $this->transformKeys(explode(",", $this->subscription->optional_recurring_product_ids));
2023-02-16 02:36:09 +01:00
if (is_array($keys)) {
2023-08-06 09:03:12 +02:00
return Product::query()->whereIn('id', $keys)->get();
2023-02-16 02:36:09 +01:00
} else {
2023-08-06 09:35:19 +02:00
return Product::query()->where('id', $keys)->get();
2022-12-01 07:05:27 +01:00
}
}
2021-04-07 14:23:14 +02:00
/**
* Get available upgrades & downgrades for the plan.
*
2023-08-08 11:44:52 +02:00
* @return \Illuminate\Database\Eloquent\Collection
2021-04-07 14:23:14 +02:00
*/
public function getPlans()
{
return Subscription::query()
2021-08-10 07:17:48 +02:00
->where('company_id', $this->subscription->company_id)
->where('group_id', $this->subscription->group_id)
->where('id', '!=', $this->subscription->id)
->get();
2021-04-07 14:23:14 +02:00
}
2021-04-16 00:32:38 +02:00
/**
* Handle the cancellation of a subscription
*
* @param RecurringInvoice $recurring_invoice
*
2021-04-16 00:32:38 +02:00
*/
public function handleCancellation(RecurringInvoice $recurring_invoice)
2021-04-07 14:23:14 +02:00
{
2022-11-26 22:12:37 +01:00
$invoice_start_date = false;
$refund_end_date = false;
$gateway_refund_attempted = false;
//only refund if they are in the refund window.
2023-08-06 09:35:19 +02:00
$outstanding_invoice = Invoice::query()->where('subscription_id', $this->subscription->id)
->where('client_id', $recurring_invoice->client_id)
->where('status_id', Invoice::STATUS_PAID)
->where('is_deleted', 0)
2023-02-16 02:36:09 +01:00
->where('is_proforma', 0)
->where('balance', 0)
->orderBy('id', 'desc')
->first();
2023-02-16 02:36:09 +01:00
if ($outstanding_invoice) {
2022-11-26 22:12:37 +01:00
$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-16 00:32:38 +02:00
/* Refund only if we are in the window - and there is nothing outstanding on the invoice */
2023-02-16 02:36:09 +01:00
if ($refund_end_date && $refund_end_date->greaterThan(now())) {
if ($outstanding_invoice->payments()->exists()) {
$payment = $outstanding_invoice->payments()->first();
$data = [
'id' => $payment->id,
'gateway_refund' => $outstanding_invoice->amount >= 1 ? true : false,
'send_email' => true,
'email_receipt',
'invoices' => [
['invoice_id' => $outstanding_invoice->id, 'amount' => $outstanding_invoice->amount],
],
];
2021-04-15 07:46:50 +02:00
$payment->refund($data);
$gateway_refund_attempted = true;
}
}
2023-02-16 02:36:09 +01:00
$context = [
'context' => 'cancellation',
'subscription' => $this->subscription->hashed_id,
'recurring_invoice' => $recurring_invoice->hashed_id,
'client' => $recurring_invoice->client->hashed_id,
'contact' => auth()->guard('contact')->user()->hashed_id,
'account_key' => $recurring_invoice->client->custom_value2,
];
2023-02-16 02:36:09 +01:00
$this->triggerWebhook($context);
2024-01-14 05:05:00 +01:00
$nmo = new NinjaMailerObject();
2023-02-16 02:36:09 +01:00
$nmo->mailable = (new NinjaMailer((new ClientContactRequestCancellationObject($recurring_invoice, auth()->guard('contact')->user(), $gateway_refund_attempted))->build()));
$nmo->company = $recurring_invoice->company;
$nmo->settings = $recurring_invoice->company->settings;
2024-01-14 05:05:00 +01:00
2023-02-16 02:36:09 +01:00
$recurring_invoice->company->company_users->each(function ($company_user) use ($nmo) {
$methods = $this->findCompanyUserNotificationType($company_user, ['recurring_cancellation', 'all_notifications']);
2022-03-04 04:01:09 +01:00
2023-02-16 02:36:09 +01:00
//if mail is a method type -fire mail!!
if (($key = array_search('mail', $methods)) !== false) {
unset($methods[$key]);
2022-03-04 04:01:09 +01:00
2023-02-16 02:36:09 +01:00
$nmo->to_user = $company_user->user;
NinjaMailerJob::dispatch($nmo);
}
});
2021-04-08 17:11:14 +02:00
2023-02-16 02:36:09 +01:00
return $this->handleRedirect('client/subscriptions');
2021-04-07 14:23:14 +02:00
}
2023-01-19 00:23:42 +01:00
/**
* Get the number of days in the currency frequency
2023-02-16 02:36:09 +01:00
*
2023-01-19 00:23:42 +01:00
* @return int Number of days
*/
2024-02-08 04:38:22 +01:00
public function getDaysInFrequency(): int
2021-04-10 06:53:16 +02:00
{
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:
2024-06-07 01:15:19 +02:00
return intval(abs(now()->diffInDays(now()->addWeeks(4))));
2021-04-14 11:55:43 +02:00
case RecurringInvoice::FREQUENCY_MONTHLY:
2024-06-07 01:15:19 +02:00
return intval(abs(now()->diffInDays(now()->addMonthNoOverflow())));
2021-04-14 11:55:43 +02:00
case RecurringInvoice::FREQUENCY_TWO_MONTHS:
2024-06-07 01:15:19 +02:00
return intval(abs(now()->diffInDays(now()->addMonthsNoOverflow(2))));
2021-04-14 11:55:43 +02:00
case RecurringInvoice::FREQUENCY_THREE_MONTHS:
2024-06-07 01:15:19 +02:00
return intval(abs(now()->diffInDays(now()->addMonthsNoOverflow(3))));
2021-04-14 11:55:43 +02:00
case RecurringInvoice::FREQUENCY_FOUR_MONTHS:
2024-06-07 01:15:19 +02:00
return intval(abs(now()->diffInDays(now()->addMonthsNoOverflow(4))));
2021-04-14 11:55:43 +02:00
case RecurringInvoice::FREQUENCY_SIX_MONTHS:
2024-06-07 01:15:19 +02:00
return intval(abs(now()->diffInDays(now()->addMonthsNoOverflow(6))));
2021-04-14 11:55:43 +02:00
case RecurringInvoice::FREQUENCY_ANNUALLY:
2024-06-07 01:15:19 +02:00
return intval(abs(now()->diffInDays(now()->addYear())));
2021-04-14 11:55:43 +02:00
case RecurringInvoice::FREQUENCY_TWO_YEARS:
2024-06-07 01:15:19 +02:00
return intval(abs(now()->diffInDays(now()->addYears(2))));
2021-04-14 11:55:43 +02:00
case RecurringInvoice::FREQUENCY_THREE_YEARS:
2024-06-07 01:15:19 +02:00
return intval(abs(now()->diffInDays(now()->addYears(3))));
2021-04-10 06:53:16 +02:00
default:
return 0;
}
}
2023-01-19 00:23:42 +01:00
/**
* Get the next date by frequency_id
2023-02-16 02:36:09 +01:00
*
2023-01-19 00:23:42 +01:00
* @param Carbon $date The current carbon date
* @param int $frequency The frequncy_id of the subscription
2023-02-16 02:36:09 +01:00
*
2023-01-19 00:23:42 +01:00
* @return ?Carbon The next date carbon object
*/
2024-01-14 05:05:00 +01:00
public function getNextDateForFrequency($date, $frequency): ?Carbon
2021-10-21 06:08:46 +02:00
{
switch ($frequency) {
case RecurringInvoice::FREQUENCY_DAILY:
return $date->addDay();
case RecurringInvoice::FREQUENCY_WEEKLY:
return $date->addDays(7);
case RecurringInvoice::FREQUENCY_TWO_WEEKS:
return $date->addDays(13);
case RecurringInvoice::FREQUENCY_FOUR_WEEKS:
return $date->addWeeks(4);
case RecurringInvoice::FREQUENCY_MONTHLY:
return $date->addMonthNoOverflow();
case RecurringInvoice::FREQUENCY_TWO_MONTHS:
2023-08-07 00:23:13 +02:00
return $date->addMonthsNoOverflow(2);
2021-10-21 06:08:46 +02:00
case RecurringInvoice::FREQUENCY_THREE_MONTHS:
2023-08-07 00:23:13 +02:00
return $date->addMonthsNoOverflow(3);
2021-10-21 06:08:46 +02:00
case RecurringInvoice::FREQUENCY_FOUR_MONTHS:
2023-08-07 00:23:13 +02:00
return $date->addMonthsNoOverflow(4);
2021-10-21 06:08:46 +02:00
case RecurringInvoice::FREQUENCY_SIX_MONTHS:
2023-08-07 00:23:13 +02:00
return $date->addMonthsNoOverflow(6);
2021-10-21 06:08:46 +02:00
case RecurringInvoice::FREQUENCY_ANNUALLY:
return $date->addYear();
case RecurringInvoice::FREQUENCY_TWO_YEARS:
return $date->addYears(2);
case RecurringInvoice::FREQUENCY_THREE_YEARS:
return $date->addYears(3);
default:
2023-01-19 00:23:42 +01:00
return null;
2023-02-16 02:36:09 +01:00
}
2021-10-21 06:08:46 +02:00
}
/**
* Handle case where no payment is required
* @param Invoice $invoice The Invoice
* @param array $bundle The bundle array
* @param ClientContact $contact The Client Contact
2023-02-16 02:36:09 +01:00
*
* @return \Illuminate\Routing\Redirector|\Illuminate\Http\RedirectResponse
*/
public function handleNoPaymentFlow(Invoice $invoice, $bundle, ClientContact $contact)
{
if (strlen($this->subscription->recurring_product_ids) >= 1) {
2023-02-16 02:36:09 +01:00
$recurring_invoice = $this->convertInvoiceToRecurringBundle($contact->client_id, collect($bundle)->map(function ($bund) {
return (object) $bund;
}));
/* Start the recurring service */
$recurring_invoice->service()
->start()
->save();
$invoice->recurring_id = $recurring_invoice->id;
$invoice->save();
$context = [
'context' => 'recurring_purchase',
'recurring_invoice' => $recurring_invoice->hashed_id,
'invoice' => $invoice->hashed_id,
'client' => $recurring_invoice->client->hashed_id,
'subscription' => $this->subscription->hashed_id,
'contact' => $contact->hashed_id,
'redirect_url' => "/client/recurring_invoices/{$recurring_invoice->hashed_id}",
];
$this->triggerWebhook($context);
return $this->handleRedirect($context['redirect_url']);
}
$redirect_url = "/client/invoices/{$invoice->hashed_id}";
return $this->handleRedirect($redirect_url);
}
2021-04-14 06:41:04 +02:00
/**
* 'email' => $this->email ?? $this->contact->email,
* 'quantity' => $this->quantity,
* 'contact_id' => $this->contact->id,
*/
2021-04-14 06:41:04 +02:00
public function handleNoPaymentRequired(array $data)
{
$context = (new ZeroCostProduct($this->subscription, $data))->run();
// Forward payload to webhook
2023-02-16 02:36:09 +01:00
if (array_key_exists('context', $context)) {
2021-04-14 06:41:04 +02:00
$response = $this->triggerWebhook($context);
2023-02-16 02:36:09 +01:00
}
2021-04-14 06:41:04 +02:00
// Hit the redirect
return $this->handleRedirect($context['redirect_url']);
}
/**
* Handles redirecting the user
*/
private function handleRedirect($default_redirect)
2024-01-14 05:05:00 +01:00
{
if (array_key_exists('return_url', $this->subscription->webhook_configuration) && strlen($this->subscription->webhook_configuration['return_url']) >= 1) {
2024-01-01 00:59:25 +01:00
return method_exists(redirect(), "send") ? redirect($this->subscription->webhook_configuration['return_url'])->send() : redirect($this->subscription->webhook_configuration['return_url']);
2023-02-16 02:36:09 +01:00
}
2021-04-14 04:40:16 +02:00
2024-01-01 00:59:25 +01:00
return method_exists(redirect(), "send") ? redirect($default_redirect)->send() : redirect($default_redirect);
}
2021-09-08 03:04:50 +02:00
/**
2023-02-16 02:36:09 +01:00
* @param Invoice $invoice
* @return true
* @throws BindingResolutionException
2021-09-08 03:04:50 +02:00
*/
public function planPaid(Invoice $invoice)
{
2021-07-15 03:06:14 +02:00
$recurring_invoice_hashed_id = $invoice->recurring_invoice()->exists() ? $invoice->recurring_invoice->hashed_id : null;
2021-07-15 06:35:22 +02:00
2023-02-16 02:36:09 +01:00
$context = [
'context' => 'plan_paid',
'subscription' => $this->subscription->hashed_id,
'recurring_invoice' => $recurring_invoice_hashed_id,
'client' => $invoice->client->hashed_id,
2024-01-14 05:05:00 +01:00
'contact' => $invoice->client->primary_contact()->first() ? $invoice->client->primary_contact()->first()->hashed_id : $invoice->client->contacts->first()->hashed_id,
2023-02-16 02:36:09 +01:00
'invoice' => $invoice->hashed_id,
'account_key' => $invoice->client->custom_value2,
];
2021-07-15 03:06:14 +02:00
$response = $this->triggerWebhook($context);
2021-09-08 03:04:50 +02:00
nlog($response);
2024-01-14 05:05:00 +01:00
2021-07-15 03:06:14 +02:00
return true;
}
2021-03-14 22:42:05 +01:00
}