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

164 lines
4.7 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;
use App\Models\BillingSubscription;
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;
use App\Repositories\InvoiceRepository;
2021-03-15 21:35:19 +01:00
2021-03-14 22:42:05 +01:00
class BillingSubscriptionService
{
/** @var BillingSubscription */
2021-03-14 22:42:05 +01:00
private $billing_subscription;
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')) {
return;
}
// 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-18 01:53:08 +01:00
// create client subscription record
//
// create recurring invoice if is_recurring
//
}
public function startTrial(array $data)
{
}
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-20 03:49:45 +01:00
$data['line_items'] = $this->createLineItems($data);
2021-03-17 03:21:06 +01:00
2021-03-15 21:35:19 +01:00
/*
If trial_enabled -> return early
-- what we need to know that we don't already
-- Has a promo code been entered, and does it match
-- Is this a recurring subscription
--
2021-03-15 21:35:19 +01:00
1. Is this a recurring product?
2. What is the quantity? ie is this a multi seat product ( does this mean we need this value stored in the client sub?)
*/
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-20 03:49:45 +01:00
private function createLineItems($data): array
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 03:49:45 +01:00
if(strlen($data['coupon']) >=1)
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-20 03:49:45 +01:00
private function createPromoLine($data)
{
2021-03-20 03:57:03 +01:00
$product = $this->billing_subscription->product;
$price = 0;
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');
$item->cost = $price;
$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-15 21:35:19 +01:00
private function convertInvoiceToRecurring()
{
//The first invoice is a plain invoice - the second is fired on the recurring schedule.
}
public function createClientSubscription($payment_hash, $recurring_invoice_id = null)
2021-03-14 22:42:05 +01:00
{
//create the client sub record
2021-03-15 21:35:19 +01:00
//?trial enabled?
$cs = new ClientSubscription();
$cs->subscription_id = $this->billing_subscription->id;
$cs->company_id = $this->billing_subscription->company_id;
// client_id
$cs->save();
2021-03-14 22:42:05 +01:00
}
public function triggerWebhook($payment_hash)
{
//hit the webhook to after a successful onboarding
}
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
}