From ecf7f66cfeac58b4c56b765419bc3de5309cc9b8 Mon Sep 17 00:00:00 2001 From: = Date: Wed, 14 Jul 2021 21:20:41 +1000 Subject: [PATCH] Working on actions when a subscription invoice is paid --- app/Listeners/Invoice/InvoicePaidActivity.php | 5 +++++ app/Models/Invoice.php | 2 +- app/Services/Subscription/SubscriptionService.php | 13 +++++++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/app/Listeners/Invoice/InvoicePaidActivity.php b/app/Listeners/Invoice/InvoicePaidActivity.php index 7991ad3b5b0f..e42fe41a006d 100644 --- a/app/Listeners/Invoice/InvoicePaidActivity.php +++ b/app/Listeners/Invoice/InvoicePaidActivity.php @@ -51,6 +51,11 @@ class InvoicePaidActivity implements ShouldQueue $this->activity_repo->save($fields, $event->invoice, $event->event_vars); + if($event->invoice->subscription()->exists()) + { + $event->invoice->subscription->service()->planPaid($event->invoice); + } + try { $event->invoice->service()->touchPdf(); } catch (\Exception $e) { diff --git a/app/Models/Invoice.php b/app/Models/Invoice.php index 3a375016dc06..75c3e314a86f 100644 --- a/app/Models/Invoice.php +++ b/app/Models/Invoice.php @@ -165,7 +165,7 @@ class Invoice extends BaseModel public function recurring_invoice() { - return $this->belongsTo(RecurringInvoice::class)->withTrashed(); + return $this->belongsTo(RecurringInvoice::class, 'recurring_id', 'id')->withTrashed(); } public function assigned_user() diff --git a/app/Services/Subscription/SubscriptionService.php b/app/Services/Subscription/SubscriptionService.php index 963bb93217a8..aeca8d1b3535 100644 --- a/app/Services/Subscription/SubscriptionService.php +++ b/app/Services/Subscription/SubscriptionService.php @@ -906,4 +906,17 @@ nlog("handle plan change"); return redirect($default_redirect); } + + public function planPaid($invoice) + { + $context = [ + 'context' => 'plan_paid', + 'subscription' => $this->subscription->hashed_id, + 'recurring_invoice' => $invoice->recurring_invoice->hashed_id, + 'client' => $invoice->client->hashed_id, + 'contact' => $invoice->client->primary_contact()->first()->hashed_id, + ]; + + $this->triggerWebhook($context); + } }