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

284 lines
10 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\Subscription;
2021-03-14 22:42:05 +01:00
2021-03-17 03:21:06 +01:00
use App\DataMapper\InvoiceItem;
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-03-21 06:35:09 +01:00
use App\Jobs\Util\SystemLogger;
use App\Models\ClientContact;
2021-03-15 21:35:19 +01:00
use App\Models\ClientSubscription;
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-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-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;
use GuzzleHttp\RequestOptions;
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-03-21 06:35:09 +01:00
/** @var subscription */
private $subscription;
2021-03-14 22:42:05 +01:00
/** @var client_subscription */
2021-03-26 22:55:04 +01:00
// private $client_subscription;
2021-03-21 06:35:09 +01:00
public function __construct(Subscription $subscription)
2021-03-14 22:42:05 +01:00
{
$this->subscription = $subscription;
2021-03-14 22:42:05 +01:00
}
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
}
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-04 14:14:33 +02:00
];
$this->triggerWebhook($context);
2021-04-01 03:58:12 +02:00
if(array_key_exists('post_purchase_url', $this->subscription->webhook_configuration) && strlen($this->subscription->webhook_configuration['post_purchase_url']) >=1)
return redirect($this->subscription->webhook_configuration['post_purchase_url']);
return redirect('/client/recurring_invoices/'.$recurring_invoice->hashed_id);
}
else
{
2021-04-04 14:14:33 +02:00
$context = [
'context' => 'single_purchase',
'invoice' => $this->encodePrimaryKey($payment_hash->fee_invoice_id),
];
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-01 03:58:12 +02:00
if(array_key_exists('post_purchase_url', $this->subscription->webhook_configuration) && strlen($this->subscription->webhook_configuration['post_purchase_url']) >=1)
return redirect($this->subscription->webhook_configuration['post_purchase_url']);
return redirect('/client/invoices/'.$this->encodePrimaryKey($payment_hash->fee_invoice_id));
}
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.
2021-03-29 12:19:30 +02:00
$client_contact = ClientContact::find($data['contact_id']);
2021-03-22 21:09:42 +01:00
if(!$this->subscription->trial_enabled)
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';
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-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();
2021-04-04 14:14:33 +02:00
$context = [
'context' => 'trial',
'recurring_invoice' => $recurring_invoice->hashed_id,
];
//execute any webhooks
2021-04-04 14:14:33 +02:00
$this->triggerWebhook($context);
2021-03-30 11:41:58 +02:00
if(array_key_exists('post_purchase_url', $this->subscription->webhook_configuration) && strlen($this->subscription->webhook_configuration['post_purchase_url']) >=1)
return redirect($this->subscription->webhook_configuration['post_purchase_url']);
2021-03-18 01:53:08 +01:00
2021-03-29 12:19:30 +02:00
return redirect('/client/recurring_invoices/'.$recurring_invoice->hashed_id);
2021-03-18 01:53:08 +01:00
}
2021-04-01 03:58:12 +02: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-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
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-03-26 22:55:04 +01:00
2021-04-01 03:58:12 +02:00
private function convertInvoiceToRecurring($client_id)
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);
$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-03-26 22:55:04 +01:00
// @deprecated due to change in architecture
2021-03-21 10:26:30 +01:00
2021-03-26 22:55:04 +01:00
// public function createClientSubscription($payment_hash)
// {
2021-03-26 22:55:04 +01:00
// //is this a recurring or one off subscription.
2021-03-15 21:35:19 +01:00
2021-03-26 22:55:04 +01:00
// $cs = new ClientSubscription();
// $cs->subscription_id = $this->subscription->id;
// $cs->company_id = $this->subscription->company_id;
2021-03-21 10:26:30 +01:00
2021-03-26 22:55:04 +01:00
// $cs->invoice_id = $payment_hash->billing_context->invoice_id;
// $cs->client_id = $payment_hash->billing_context->client_id;
// $cs->quantity = $payment_hash->billing_context->quantity;
2021-03-21 10:26:30 +01:00
2021-03-26 22:55:04 +01:00
// //if is_recurring
// //create recurring invoice from invoice
// if($this->subscription->is_recurring)
// {
// $recurring_invoice = $this->convertInvoiceToRecurring($payment_hash);
// $recurring_invoice->frequency_id = $this->subscription->frequency_id;
// $recurring_invoice->next_send_date = $recurring_invoice->nextDateByFrequency(now()->format('Y-m-d'));
// $recurring_invoice->save();
// $cs->recurring_invoice_id = $recurring_invoice->id;
2021-03-21 10:26:30 +01:00
2021-03-26 22:55:04 +01:00
// //?set the recurring invoice as active - set the date here also based on the frequency?
// $recurring_invoice->service()->start();
// }
2021-03-21 10:26:30 +01:00
2021-03-21 06:35:09 +01:00
2021-03-26 22:55:04 +01:00
// $cs->save();
2021-03-21 06:35:09 +01:00
2021-03-26 22:55:04 +01:00
// $this->client_subscription = $cs;
// }
2021-03-14 22:42:05 +01:00
2021-03-26 23:05:37 +01:00
//@todo - need refactor
2021-04-04 14:14:33 +02:00
public function triggerWebhook($context)
2021-03-14 22:42:05 +01:00
{
2021-04-04 14:14:33 +02:00
//context = 'trial, recurring_purchase, single_purchase'
2021-03-14 22:42:05 +01:00
//hit the webhook to after a successful onboarding
2021-03-23 13:17:28 +01:00
2021-04-04 14:14:33 +02:00
$body = [
2021-04-05 00:37:01 +02:00
'subscription' => $this->subscription->hashed_id,
'client' => $this->client_subscription->client->hashed_id,
2021-04-04 14:14:33 +02:00
];
2021-03-21 06:35:09 +01:00
2021-04-04 14:14:33 +02:00
$body = array_merge($body, $context);
2021-04-04 14:47:59 +02:00
2021-04-04 14:14:33 +02:00
if(Ninja::isHosted())
{
$hosted = [
'company' => $this->subscription->company,
];
$body = array_merge($body, $hosted);
}
2021-03-21 06:35:09 +01:00
2021-04-04 14:14:33 +02:00
$client = new \GuzzleHttp\Client(
[
'headers' => $this->subscription->webhook_configuration['post_purchase_headers']
]);
2021-03-21 06:35:09 +01:00
2021-04-04 14:47:59 +02:00
$response = $client->{$this->subscription->webhook_configuration['post_purchase_rest_method']}($this->subscription['post_purchase_url'],[
2021-04-04 14:14:33 +02:00
RequestOptions::JSON => ['body' => $body]
]);
2021-03-21 06:35:09 +01:00
2021-03-26 23:05:37 +01:00
// SystemLogger::dispatch(
// $body,
// SystemLog::CATEGORY_WEBHOOK,
// SystemLog::EVENT_WEBHOOK_RESPONSE,
// SystemLog::TYPE_WEBHOOK_RESPONSE,
// $this->client_subscription->client,
// );
2021-03-21 06:35:09 +01:00
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
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-31 02:14:19 +02:00
public function recurring_products()
{
2021-03-26 10:38:51 +01:00
return Product::whereIn('id', $this->transformKeys(explode(",", $this->subscription->recurring_product_ids)))->get();
}
2021-03-14 22:42:05 +01:00
}