mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-07-09 03:14:30 -04:00
Working on subscriptions
This commit is contained in:
parent
498d8e4efc
commit
3a3ddc031c
@ -13,13 +13,14 @@ namespace App\Jobs\Cron;
|
||||
|
||||
use App\Libraries\MultiDB;
|
||||
use App\Models\Invoice;
|
||||
use App\Utils\Traits\SubscriptionHooker;
|
||||
use Illuminate\Foundation\Bus\Dispatchable;
|
||||
use Illuminate\Support\Carbon;
|
||||
|
||||
class SubscriptionCron
|
||||
{
|
||||
use Dispatchable;
|
||||
|
||||
use SubscriptionHooker;
|
||||
/**
|
||||
* Create a new job instance.
|
||||
*
|
||||
@ -38,12 +39,15 @@ class SubscriptionCron
|
||||
{
|
||||
|
||||
if (! config('ninja.db.multi_db_enabled')) {
|
||||
|
||||
$this->loopSubscriptions();
|
||||
|
||||
} else {
|
||||
//multiDB environment, need to
|
||||
foreach (MultiDB::$dbs as $db) {
|
||||
|
||||
MultiDB::setDB($db);
|
||||
|
||||
$this->loopSubscriptions();
|
||||
|
||||
}
|
||||
@ -56,7 +60,7 @@ class SubscriptionCron
|
||||
$invoices = Invoice::where('is_deleted', 0)
|
||||
->whereIn('status_id', [Invoice::STATUS_SENT, Invoice::STATUS_PARTIAL])
|
||||
->where('balance', '>', 0)
|
||||
->whereDate('due_date', '<=', now()->startOfDay())
|
||||
->whereDate('due_date', '<=', now()->addDay()->startOfDay())
|
||||
->whereNotNull('subscription_id')
|
||||
->cursor();
|
||||
|
||||
@ -65,18 +69,24 @@ class SubscriptionCron
|
||||
|
||||
$subscription = $invoice->subscription;
|
||||
|
||||
$body = [
|
||||
'context' => 'plan_expired',
|
||||
'client' => $invoice->client->hashed_id,
|
||||
'invoice' => $invoice->hashed_id,
|
||||
'subscription' => $subscription->hashed_id,
|
||||
];
|
||||
|
||||
$this->sendLoad($subscription, $body);
|
||||
//This will send the notification daily.
|
||||
//We'll need to handle this by performing some action on the invoice to either archive it or delete it?
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
/* Our daily cron should check
|
||||
|
||||
1. Is the subscription still in trial phase?
|
||||
2. Check the recurring invoice and its remaining_cycles to see whether we need to cancel or perform any other function.
|
||||
3. Any notifications that need to fire?
|
||||
*/
|
||||
private function processSubscription($client_subscription)
|
||||
private function handleWebhook($invoice, $subscription)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -166,6 +166,11 @@ class Invoice extends BaseModel
|
||||
return $this->belongsTo(User::class)->withTrashed();
|
||||
}
|
||||
|
||||
public function recurring_invoice()
|
||||
{
|
||||
return $this->belongsTo(RecurringInvoice::class)->withTrashed();
|
||||
}
|
||||
|
||||
public function assigned_user()
|
||||
{
|
||||
return $this->belongsTo(User::class, 'assigned_user_id', 'id')->withTrashed();
|
||||
|
@ -32,12 +32,14 @@ use App\Repositories\SubscriptionRepository;
|
||||
use App\Utils\Ninja;
|
||||
use App\Utils\Traits\CleanLineItems;
|
||||
use App\Utils\Traits\MakesHash;
|
||||
use App\Utils\Traits\SubscriptionHooker;
|
||||
use GuzzleHttp\RequestOptions;
|
||||
|
||||
class SubscriptionService
|
||||
{
|
||||
use MakesHash;
|
||||
use CleanLineItems;
|
||||
use SubscriptionHooker;
|
||||
|
||||
/** @var subscription */
|
||||
private $subscription;
|
||||
@ -231,29 +233,15 @@ class SubscriptionService
|
||||
'db' => $this->subscription->company->db,
|
||||
]);
|
||||
|
||||
$headers = [
|
||||
'Content-Type' => 'application/json',
|
||||
'X-Requested-With' => 'XMLHttpRequest',
|
||||
];
|
||||
|
||||
|
||||
$client = new \GuzzleHttp\Client(
|
||||
[
|
||||
'headers' => $headers,
|
||||
]);
|
||||
|
||||
try {
|
||||
$response = $client->{$this->subscription->webhook_configuration['post_purchase_rest_method']}($this->subscription->webhook_configuration['post_purchase_url'],[
|
||||
RequestOptions::JSON => ['body' => $body], RequestOptions::ALLOW_REDIRECTS => false
|
||||
]);
|
||||
}
|
||||
catch(\Exception $e)
|
||||
{
|
||||
$body = array_merge($body, ['exception' => $e->getMessage()]);
|
||||
}
|
||||
$response = $this->sendLoad($this->subscription, $body);
|
||||
|
||||
/* Append the response to the system logger body */
|
||||
if($response) {
|
||||
if(is_array($response)){
|
||||
|
||||
$body = $response;
|
||||
|
||||
}
|
||||
else {
|
||||
|
||||
$status = $response->getStatusCode();
|
||||
$response_body = $response->getBody();
|
||||
|
53
app/Utils/Traits/SubscriptionHooker.php
Normal file
53
app/Utils/Traits/SubscriptionHooker.php
Normal file
@ -0,0 +1,53 @@
|
||||
<?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\Utils\Traits;
|
||||
|
||||
use GuzzleHttp\RequestOptions;
|
||||
|
||||
/**
|
||||
* Class SubscriptionHooker.
|
||||
*/
|
||||
trait SubscriptionHooker
|
||||
{
|
||||
|
||||
public function sendLoad($subscription, $body)
|
||||
{
|
||||
|
||||
$headers = [
|
||||
'Content-Type' => 'application/json',
|
||||
'X-Requested-With' => 'XMLHttpRequest',
|
||||
];
|
||||
|
||||
if(count($subscription->webhook_configuration['post_purchase_headers']) >= 1)
|
||||
$headers = array_merge($headers, $subscription->webhook_configuration['post_purchase_headers']);
|
||||
|
||||
$client = new \GuzzleHttp\Client(
|
||||
[
|
||||
'headers' => $headers,
|
||||
]);
|
||||
|
||||
try {
|
||||
$response = $client->{$subscription->webhook_configuration['post_purchase_rest_method']}($subscription->webhook_configuration['post_purchase_url'],[
|
||||
RequestOptions::JSON => ['body' => $body], RequestOptions::ALLOW_REDIRECTS => false
|
||||
]);
|
||||
|
||||
return $response;
|
||||
}
|
||||
catch(\Exception $e)
|
||||
{
|
||||
$body = array_merge($body, ['exception' => $e->getMessage()]);
|
||||
return $body;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user