1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-11 05:32:39 +01:00
invoiceninja/app/Services/BillingSubscription/BillingSubscriptionService.php

260 lines
8.2 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
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\Services\BillingSubscription;
2021-03-17 03:21:06 +01:00
use App\DataMapper\InvoiceItem;
use App\Factory\InvoiceFactory;
2021-03-21 06:35:09 +01:00
use App\Jobs\Util\SystemLogger;
use App\Models\BillingSubscription;
use App\Models\ClientContact;
2021-03-15 21:35:19 +01:00
use App\Models\ClientSubscription;
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-21 06:35:09 +01:00
use App\Models\SystemLog;
2021-03-17 03:21:06 +01:00
use App\Repositories\InvoiceRepository;
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;
use GuzzleHttp\RequestOptions;
2021-03-15 21:35:19 +01:00
2021-03-14 22:42:05 +01:00
class BillingSubscriptionService
{
2021-03-21 06:35:09 +01:00
use MakesHash;
2021-03-22 21:09:42 +01:00
use CleanLineItems;
2021-03-21 06:35:09 +01:00
/** @var billing_subscription */
2021-03-14 22:42:05 +01:00
private $billing_subscription;
/** @var client_subscription */
2021-03-21 06:35:09 +01:00
private $client_subscription;
2021-03-14 22:42:05 +01:00
public function __construct(BillingSubscription $billing_subscription)
{
$this->billing_subscription = $billing_subscription;
}
2021-03-18 01:53:08 +01:00
public function completePurchase(PaymentHash $payment_hash)
{
2021-03-18 10:57:55 +01:00
if (!property_exists($payment_hash, 'billing_context')) {
throw new \Exception("Illegal entrypoint into method, payload must contain billing context");
2021-03-18 10:57:55 +01:00
}
// At this point we have some state carried from the billing page
// to this, available as $payment_hash->data->billing_context. Make something awesome ⭐
2021-03-22 21:09:42 +01:00
// create client subscription record
2021-03-18 01:53:08 +01:00
//
// create recurring invoice if is_recurring
2021-03-22 21:09:42 +01:00
//
2021-03-18 01:53:08 +01:00
}
/**
'email' => $this->email ?? $this->contact->email,
'quantity' => $this->quantity,
'contact_id' => $this->contact->id,
*/
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.
if(!$this->billing_subscription->trial_enabled)
return new \Exception("Trials are disabled for this product");
$contact = ClientContact::with('client')->find($data['contact_id']);
$cs = new ClientSubscription();
$cs->subscription_id = $this->billing_subscription->id;
$cs->company_id = $this->billing_subscription->company_id;
$cs->trial_started = time();
$cs->trial_duration = time() + $this->billing_subscription->trial_duration;
$cs->quantity = $data['quantity'];
$cs->client_id = $contact->client->id;
$cs->save();
$this->client_subscription = $cs;
//execute any webhooks
//if we have a defined return url redirect now
//else we provide a default redirect
if(strlen($this->billing_subscription->webhook_configuration->post_purchase_url) >=1)
return redirect($this->billing_subscription->webhook_configuration->post_purchase_url);
2021-03-18 01:53:08 +01:00
return redirect('/client/subscription/'.$cs->hashed_id);
2021-03-18 01:53:08 +01:00
}
public function createInvoice($data): ?\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-22 21:09:42 +01:00
$data['line_items'] = $this->cleanItems($this->createLineItems($data));
2021-03-17 03:21:06 +01:00
return $invoice_repo->save($data, InvoiceFactory::create($this->billing_subscription->company_id, $this->billing_subscription->user_id));
2021-03-17 03:21:06 +01:00
}
2021-03-21 10:26:30 +01:00
/**
* Creates the required line items for the invoice
* for the billing subscription.
*/
2021-03-20 03:49:45 +01:00
private function createLineItems($data): array
2021-03-17 03:21:06 +01:00
{
2021-03-17 03:21:06 +01:00
$line_items = [];
2021-03-17 03:21:06 +01:00
$product = $this->billing_subscription->product;
$item = new InvoiceItem;
2021-03-20 03:49:45 +01:00
$item->quantity = $data['quantity'];
2021-03-17 03:21:06 +01:00
$item->product_key = $product->product_key;
$item->notes = $product->notes;
$item->cost = $product->price;
2021-03-20 03:49:45 +01:00
$item->tax_rate1 = $product->tax_rate1 ?: 0;
$item->tax_name1 = $product->tax_name1 ?: '';
$item->tax_rate2 = $product->tax_rate2 ?: 0;
$item->tax_name2 = $product->tax_name2 ?: '';
$item->tax_rate3 = $product->tax_rate3 ?: 0;
$item->tax_name3 = $product->tax_name3 ?: '';
$item->custom_value1 = $product->custom_value1 ?: '';
$item->custom_value2 = $product->custom_value2 ?: '';
$item->custom_value3 = $product->custom_value3 ?: '';
$item->custom_value4 = $product->custom_value4 ?: '';
2021-03-17 03:21:06 +01:00
//$item->type_id need to switch whether the subscription is a service or product
$line_items[] = $item;
//do we have a promocode? enter this as a line item.
2021-03-20 04:39:30 +01:00
if(strlen($data['coupon']) >=1 && ($data['coupon'] == $this->billing_subscription->promo_code) && $this->billing_subscription->promo_discount > 0)
2021-03-20 03:57:03 +01:00
$line_items[] = $this->createPromoLine($data);
2021-03-17 03:21:06 +01:00
return $line_items;
2021-03-14 22:42:05 +01:00
}
2021-03-21 10:26:30 +01:00
/**
* If a coupon is entered (and is valid)
* then we apply the coupon discount with a line item.
*/
2021-03-20 03:49:45 +01:00
private function createPromoLine($data)
{
2021-03-20 03:57:03 +01:00
$product = $this->billing_subscription->product;
2021-03-20 04:39:30 +01:00
$discounted_amount = 0;
$discount = 0;
$amount = $data['quantity'] * $product->cost;
2021-03-20 03:57:03 +01:00
2021-03-20 04:39:30 +01:00
if ($this->billing_subscription->is_amount_discount == true) {
$discount = $this->billing_subscription->promo_discount;
}
else {
$discount = round($amount * ($this->billing_subscription->promo_discount / 100), 2);
}
$discounted_amount = $amount - $discount;
2021-03-20 03:49:45 +01:00
$item = new InvoiceItem;
2021-03-20 03:57:03 +01:00
$item->quantity = 1;
$item->product_key = ctrans('texts.promo_code');
$item->notes = ctrans('texts.promo_code');
2021-03-20 04:39:30 +01:00
$item->cost = $discounted_amount;
2021-03-20 03:57:03 +01:00
$item->tax_rate1 = $product->tax_rate1 ?: 0;
$item->tax_name1 = $product->tax_name1 ?: '';
$item->tax_rate2 = $product->tax_rate2 ?: 0;
$item->tax_name2 = $product->tax_name2 ?: '';
$item->tax_rate3 = $product->tax_rate3 ?: 0;
$item->tax_name3 = $product->tax_name3 ?: '';
return $item;
2021-03-20 03:49:45 +01:00
}
2021-03-21 10:26:30 +01:00
private function convertInvoiceToRecurring($payment_hash)
2021-03-15 21:35:19 +01:00
{
//The first invoice is a plain invoice - the second is fired on the recurring schedule.
}
2021-03-21 10:26:30 +01:00
public function createClientSubscription($payment_hash)
2021-03-14 22:42:05 +01:00
{
2021-03-21 10:26:30 +01:00
//is this a recurring or one off subscription.
2021-03-15 21:35:19 +01:00
$cs = new ClientSubscription();
$cs->subscription_id = $this->billing_subscription->id;
$cs->company_id = $this->billing_subscription->company_id;
2021-03-21 10:26:30 +01:00
//if a payment has been made
//$cs->invoice_id = xx
//if is_recurring
//create recurring invoice from invoice
$recurring_invoice = $this->convertInvoiceToRecurring($payment_hash);
$recurring_invoice->frequency_id = $this->billing_subscription->frequency_id;
$recurring_invoice->next_send_date = $recurring_invoice->nextDateByFrequency(now()->format('Y-m-d'));
//$cs->recurring_invoice_id = $recurring_invoice->id;
//?set the recurring invoice as active - set the date here also based on the frequency?
//$cs->quantity = xx
// client_id
//$cs->client_id = xx
2021-03-15 21:35:19 +01:00
$cs->save();
2021-03-21 06:35:09 +01:00
$this->client_subscription = $cs;
2021-03-14 22:42:05 +01:00
}
public function triggerWebhook($payment_hash)
{
//hit the webhook to after a successful onboarding
2021-03-21 06:35:09 +01:00
//$client = xxxxxxx
//todo webhook
$body = [
'billing_subscription' => $this->billing_subscription,
'client_subscription' => $this->client_subscription,
// 'client' => $client->toArray(),
];
$client = new \GuzzleHttp\Client(['headers' => $this->billing_subscription->webhook_configuration->post_purchase_headers]);
$response = $client->{$this->billing_subscription->webhook_configuration->post_purchase_rest_method}($this->billing_subscription->post_purchase_url,[
RequestOptions::JSON => ['body' => $body]
]);
SystemLogger::dispatch(
$body,
SystemLog::CATEGORY_WEBHOOK,
SystemLog::EVENT_WEBHOOK_RESPONSE,
SystemLog::TYPE_WEBHOOK_RESPONSE,
//$client,
);
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-03-14 22:42:05 +01:00
}