mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-06-03 01:34:35 -04:00
Refactoring subscriptions
This commit is contained in:
parent
986e7fee86
commit
aabe5683e1
@ -32,6 +32,7 @@ use App\Models\Expense;
|
|||||||
use App\Models\Product;
|
use App\Models\Product;
|
||||||
use App\Models\Project;
|
use App\Models\Project;
|
||||||
use App\Models\Quote;
|
use App\Models\Quote;
|
||||||
|
use App\Models\RecurringInvoice;
|
||||||
use App\Models\Task;
|
use App\Models\Task;
|
||||||
use App\Models\User;
|
use App\Models\User;
|
||||||
use App\Models\Vendor;
|
use App\Models\Vendor;
|
||||||
@ -255,6 +256,7 @@ class CreateSingleAccount extends Command
|
|||||||
$sub->recurring_product_ids = "{$p1->hashed_id}";
|
$sub->recurring_product_ids = "{$p1->hashed_id}";
|
||||||
$sub->webhook_configuration = $webhook_config;
|
$sub->webhook_configuration = $webhook_config;
|
||||||
$sub->allow_plan_changes = true;
|
$sub->allow_plan_changes = true;
|
||||||
|
$sub->frequency_id = RecurringInvoice::FREQUENCY_MONTHLY;
|
||||||
$sub->save();
|
$sub->save();
|
||||||
|
|
||||||
$sub = SubscriptionFactory::create($company->id, $user->id);
|
$sub = SubscriptionFactory::create($company->id, $user->id);
|
||||||
@ -263,6 +265,7 @@ class CreateSingleAccount extends Command
|
|||||||
$sub->recurring_product_ids = "{$p2->hashed_id}";
|
$sub->recurring_product_ids = "{$p2->hashed_id}";
|
||||||
$sub->webhook_configuration = $webhook_config;
|
$sub->webhook_configuration = $webhook_config;
|
||||||
$sub->allow_plan_changes = true;
|
$sub->allow_plan_changes = true;
|
||||||
|
$sub->frequency_id = RecurringInvoice::FREQUENCY_MONTHLY;
|
||||||
$sub->save();
|
$sub->save();
|
||||||
|
|
||||||
$sub = SubscriptionFactory::create($company->id, $user->id);
|
$sub = SubscriptionFactory::create($company->id, $user->id);
|
||||||
@ -271,6 +274,7 @@ class CreateSingleAccount extends Command
|
|||||||
$sub->recurring_product_ids = "{$p3->hashed_id}";
|
$sub->recurring_product_ids = "{$p3->hashed_id}";
|
||||||
$sub->webhook_configuration = $webhook_config;
|
$sub->webhook_configuration = $webhook_config;
|
||||||
$sub->allow_plan_changes = true;
|
$sub->allow_plan_changes = true;
|
||||||
|
$sub->frequency_id = RecurringInvoice::FREQUENCY_MONTHLY;
|
||||||
$sub->save();
|
$sub->save();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -112,6 +112,7 @@ class BillingPortalPurchase extends Component
|
|||||||
'show_loading_bar' => false,
|
'show_loading_bar' => false,
|
||||||
'not_eligible' => null,
|
'not_eligible' => null,
|
||||||
'not_eligible_message' => null,
|
'not_eligible_message' => null,
|
||||||
|
'payment_required' => true,
|
||||||
];
|
];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -270,6 +271,9 @@ class BillingPortalPurchase extends Component
|
|||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if((int)$this->subscription->price == 0)
|
||||||
|
$this->steps['payment_required'] = false;
|
||||||
|
else
|
||||||
$this->steps['fetched_payment_methods'] = true;
|
$this->steps['fetched_payment_methods'] = true;
|
||||||
|
|
||||||
$this->methods = $contact->client->service()->getPaymentMethods($this->price);
|
$this->methods = $contact->client->service()->getPaymentMethods($this->price);
|
||||||
@ -357,6 +361,30 @@ class BillingPortalPurchase extends Component
|
|||||||
'email' => $this->email ?? $this->contact->email,
|
'email' => $this->email ?? $this->contact->email,
|
||||||
'quantity' => $this->quantity,
|
'quantity' => $this->quantity,
|
||||||
'contact_id' => $this->contact->id,
|
'contact_id' => $this->contact->id,
|
||||||
|
'client_id' => $this->contact->client->id,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function handlePaymentNotRequired()
|
||||||
|
{
|
||||||
|
|
||||||
|
$is_eligible = $this->subscription->service()->isEligible($this->contact);
|
||||||
|
|
||||||
|
if ($is_eligible['status_code'] != 200) {
|
||||||
|
$this->steps['not_eligible'] = true;
|
||||||
|
$this->steps['not_eligible_message'] = $is_eligible['exception']['message'];
|
||||||
|
$this->steps['show_loading_bar'] = false;
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
return $this->subscription->service()->handleNoPaymentRequired([
|
||||||
|
'email' => $this->email ?? $this->contact->email,
|
||||||
|
'quantity' => $this->quantity,
|
||||||
|
'contact_id' => $this->contact->id,
|
||||||
|
'client_id' => $this->contact->client->id,
|
||||||
|
'coupon' => '',
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -105,10 +105,10 @@ class NinjaMailerJob implements ShouldQueue
|
|||||||
|
|
||||||
switch ($class) {
|
switch ($class) {
|
||||||
case Invoice::class:
|
case Invoice::class:
|
||||||
event(new InvoiceWasEmailedAndFailed($this->nmo->invitation, $this->nmo->company, $message, $this->nmo->reminder_template, Ninja::eventVars(auth()->user()->id)));
|
event(new InvoiceWasEmailedAndFailed($this->nmo->invitation, $this->nmo->company, $message, $this->nmo->reminder_template, Ninja::eventVars(auth()->user() ? auth()->user()->id : null)));
|
||||||
break;
|
break;
|
||||||
case Payment::class:
|
case Payment::class:
|
||||||
event(new PaymentWasEmailedAndFailed($this->nmo->entity, $this->nmo->company, $message, Ninja::eventVars(auth()->user()->id)));
|
event(new PaymentWasEmailedAndFailed($this->nmo->entity, $this->nmo->company, $message, Ninja::eventVars(auth()->user ? auth()->user()->id : null)));
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
# code...
|
# code...
|
||||||
|
@ -29,6 +29,7 @@ use App\Models\SystemLog;
|
|||||||
use App\Repositories\InvoiceRepository;
|
use App\Repositories\InvoiceRepository;
|
||||||
use App\Repositories\RecurringInvoiceRepository;
|
use App\Repositories\RecurringInvoiceRepository;
|
||||||
use App\Repositories\SubscriptionRepository;
|
use App\Repositories\SubscriptionRepository;
|
||||||
|
use App\Services\Subscription\ZeroCostProduct;
|
||||||
use App\Utils\Ninja;
|
use App\Utils\Ninja;
|
||||||
use App\Utils\Traits\CleanLineItems;
|
use App\Utils\Traits\CleanLineItems;
|
||||||
use App\Utils\Traits\MakesHash;
|
use App\Utils\Traits\MakesHash;
|
||||||
@ -93,12 +94,10 @@ class SubscriptionService
|
|||||||
|
|
||||||
$response = $this->triggerWebhook($context);
|
$response = $this->triggerWebhook($context);
|
||||||
|
|
||||||
nlog($response);
|
// nlog($response);
|
||||||
|
|
||||||
if(array_key_exists('post_purchase_url', $this->subscription->webhook_configuration) && strlen($this->subscription->webhook_configuration['post_purchase_url']) >=1)
|
$this->handleRedirect('/client/recurring_invoices/'.$recurring_invoice->hashed_id);
|
||||||
return redirect($this->subscription->webhook_configuration['post_purchase_url']);
|
|
||||||
|
|
||||||
return redirect('/client/recurring_invoices/'.$recurring_invoice->hashed_id);
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -114,10 +113,7 @@ class SubscriptionService
|
|||||||
//execute any webhooks
|
//execute any webhooks
|
||||||
$this->triggerWebhook($context);
|
$this->triggerWebhook($context);
|
||||||
|
|
||||||
if(array_key_exists('post_purchase_url', $this->subscription->webhook_configuration) && strlen($this->subscription->webhook_configuration['post_purchase_url']) >=1)
|
$this->handleRedirect('/client/invoices/'.$this->encodePrimaryKey($payment_hash->fee_invoice_id));
|
||||||
return redirect($this->subscription->webhook_configuration['post_purchase_url']);
|
|
||||||
|
|
||||||
return redirect('/client/invoices/'.$this->encodePrimaryKey($payment_hash->fee_invoice_id));
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -417,6 +413,7 @@ class SubscriptionService
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public function createInvoice($data): ?\App\Models\Invoice
|
public function createInvoice($data): ?\App\Models\Invoice
|
||||||
{
|
{
|
||||||
|
|
||||||
@ -438,7 +435,7 @@ class SubscriptionService
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private function convertInvoiceToRecurring($client_id) :RecurringInvoice
|
public function convertInvoiceToRecurring($client_id) :RecurringInvoice
|
||||||
{
|
{
|
||||||
|
|
||||||
$subscription_repo = new SubscriptionRepository();
|
$subscription_repo = new SubscriptionRepository();
|
||||||
@ -480,7 +477,8 @@ class SubscriptionService
|
|||||||
else {
|
else {
|
||||||
|
|
||||||
$status = $response->getStatusCode();
|
$status = $response->getStatusCode();
|
||||||
$response_body = $response->getBody();
|
|
||||||
|
//$response_body = $response->getReasonPhrase();
|
||||||
$body = array_merge($body, ['status' => $status, 'response_body' => $response_body]);
|
$body = array_merge($body, ['status' => $status, 'response_body' => $response_body]);
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -582,4 +580,34 @@ class SubscriptionService
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* '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)
|
||||||
|
{
|
||||||
|
|
||||||
|
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']);
|
||||||
|
|
||||||
|
return redirect($default_redirect);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
84
app/Services/Subscription/ZeroCostProduct.php
Normal file
84
app/Services/Subscription/ZeroCostProduct.php
Normal file
@ -0,0 +1,84 @@
|
|||||||
|
<?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;
|
||||||
|
|
||||||
|
use App\Models\Subscription;
|
||||||
|
use App\Repositories\RecurringInvoiceRepository;
|
||||||
|
use App\Services\AbstractService;
|
||||||
|
|
||||||
|
class ZeroCostProduct extends AbstractService
|
||||||
|
{
|
||||||
|
private $subscription;
|
||||||
|
|
||||||
|
private $data;
|
||||||
|
|
||||||
|
/**
|
||||||
|
$data = [
|
||||||
|
'email' => $this->email ?? $this->contact->email,
|
||||||
|
'quantity' => $this->quantity,
|
||||||
|
'contact_id' => $this->contact->id,
|
||||||
|
'client_id' => $this->contact->client->id,
|
||||||
|
];
|
||||||
|
*/
|
||||||
|
public function __construct(Subscription $subscription, array $data)
|
||||||
|
{
|
||||||
|
$this->subscription = $subscription;
|
||||||
|
|
||||||
|
$this->data = $data;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public function run()
|
||||||
|
{
|
||||||
|
//create a zero dollar invoice.
|
||||||
|
|
||||||
|
$invoice = $this->subscription->service()->createInvoice($this->data);
|
||||||
|
|
||||||
|
$invoice->service()
|
||||||
|
->markPaid()
|
||||||
|
->save();
|
||||||
|
|
||||||
|
$redirect_url = "/client/invoices/{$invoice->hashed_id}";
|
||||||
|
|
||||||
|
//create a recurring zero dollar invoice attached to this subscription.
|
||||||
|
|
||||||
|
if(strlen($this->subscription->recurring_product_ids) >=1){
|
||||||
|
|
||||||
|
$recurring_invoice = $this->subscription->service()->convertInvoiceToRecurring($this->data['client_id']);
|
||||||
|
$recurring_invoice_repo = new RecurringInvoiceRepository();
|
||||||
|
|
||||||
|
$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();
|
||||||
|
|
||||||
|
$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' => auth('contact')->user()->hashed_id,
|
||||||
|
'redirect_url' => "/client/recurring_invoices/{$recurring_invoice->hashed_id}",
|
||||||
|
];
|
||||||
|
|
||||||
|
return $context;
|
||||||
|
}
|
||||||
|
|
||||||
|
return ['redirect_url' => $redirect_url];
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -40,11 +40,13 @@ trait SubscriptionHooker
|
|||||||
RequestOptions::JSON => ['body' => $body], RequestOptions::ALLOW_REDIRECTS => false
|
RequestOptions::JSON => ['body' => $body], RequestOptions::ALLOW_REDIRECTS => false
|
||||||
]);
|
]);
|
||||||
|
|
||||||
return $response;
|
return array_merge($body, ['exception' => json_decode($response->getBody(),true), 'status_code' => $response->getStatusCode()]);
|
||||||
}
|
}
|
||||||
catch(\Exception $e)
|
catch(\Exception $e)
|
||||||
{
|
{
|
||||||
$body = array_merge($body, ['exception' => $e->getMessage()]);
|
//;
|
||||||
|
// dd($e);
|
||||||
|
$body = array_merge($body, ['exception' => ['message' => $e->getMessage(), 'status_code' => 500]]);
|
||||||
return $body;
|
return $body;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4210,7 +4210,7 @@ $LANG = array(
|
|||||||
'activity_83' => ':user deleted subscription :subscription',
|
'activity_83' => ':user deleted subscription :subscription',
|
||||||
'activity_84' => ':user restored subscription :subscription',
|
'activity_84' => ':user restored subscription :subscription',
|
||||||
'amount_greater_than_balance_v5' => 'The amount is greater than the invoice balance. You cannot overpay an invoice.',
|
'amount_greater_than_balance_v5' => 'The amount is greater than the invoice balance. You cannot overpay an invoice.',
|
||||||
|
'click_to_continue' => 'Click to continue',
|
||||||
);
|
);
|
||||||
|
|
||||||
return $LANG;
|
return $LANG;
|
||||||
|
@ -135,6 +135,13 @@
|
|||||||
</svg>
|
</svg>
|
||||||
@endif
|
@endif
|
||||||
</div>
|
</div>
|
||||||
|
@elseif(!$steps['payment_required'])
|
||||||
|
<form wire:submit.prevent="handlePaymentNotRequired" class="mt-8">
|
||||||
|
@csrf
|
||||||
|
<button class="px-3 py-2 border rounded mr-4 hover:border-blue-600">
|
||||||
|
{{ ctrans('texts.click_to_continue') }}
|
||||||
|
</button>
|
||||||
|
</form>
|
||||||
@elseif($steps['show_start_trial'])
|
@elseif($steps['show_start_trial'])
|
||||||
<form wire:submit.prevent="handleTrial" class="mt-8">
|
<form wire:submit.prevent="handleTrial" class="mt-8">
|
||||||
@csrf
|
@csrf
|
||||||
|
Loading…
x
Reference in New Issue
Block a user