From 3a3ddc031cdc47ff74aa7eb27fb153dbdd619894 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Wed, 7 Apr 2021 18:06:50 +1000 Subject: [PATCH] Working on subscriptions --- app/Jobs/Cron/SubscriptionCron.php | 26 ++++++--- app/Models/Invoice.php | 5 ++ .../Subscription/SubscriptionService.php | 30 ++++------- app/Utils/Traits/SubscriptionHooker.php | 53 +++++++++++++++++++ 4 files changed, 85 insertions(+), 29 deletions(-) create mode 100644 app/Utils/Traits/SubscriptionHooker.php diff --git a/app/Jobs/Cron/SubscriptionCron.php b/app/Jobs/Cron/SubscriptionCron.php index 00ed302a2fbf..8c031007398a 100644 --- a/app/Jobs/Cron/SubscriptionCron.php +++ b/app/Jobs/Cron/SubscriptionCron.php @@ -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) { } + } diff --git a/app/Models/Invoice.php b/app/Models/Invoice.php index d48e401d4aa0..48342fd68aa8 100644 --- a/app/Models/Invoice.php +++ b/app/Models/Invoice.php @@ -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(); diff --git a/app/Services/Subscription/SubscriptionService.php b/app/Services/Subscription/SubscriptionService.php index dc99c7519e87..3ac417114ca3 100644 --- a/app/Services/Subscription/SubscriptionService.php +++ b/app/Services/Subscription/SubscriptionService.php @@ -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(); diff --git a/app/Utils/Traits/SubscriptionHooker.php b/app/Utils/Traits/SubscriptionHooker.php new file mode 100644 index 000000000000..ced93bbb2f21 --- /dev/null +++ b/app/Utils/Traits/SubscriptionHooker.php @@ -0,0 +1,53 @@ + '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; + } + + } +} + \ No newline at end of file