From ecf7f66cfeac58b4c56b765419bc3de5309cc9b8 Mon Sep 17 00:00:00 2001 From: = Date: Wed, 14 Jul 2021 21:20:41 +1000 Subject: [PATCH 1/5] 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); + } } From a6146ee4686646a60acb59be83c9a23b17fe8e27 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Thu, 15 Jul 2021 09:23:38 +1000 Subject: [PATCH 2/5] Tests for settings --- tests/Unit/SettingsSaverTest.php | 39 ++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 tests/Unit/SettingsSaverTest.php diff --git a/tests/Unit/SettingsSaverTest.php b/tests/Unit/SettingsSaverTest.php new file mode 100644 index 000000000000..3f473efcb4fe --- /dev/null +++ b/tests/Unit/SettingsSaverTest.php @@ -0,0 +1,39 @@ +checkAttribute($key, $value); + + $this->assertFalse($result); + + } +} From 46bc8fa49520b2fd98357988b2c83d58ad7688e7 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Thu, 15 Jul 2021 11:06:14 +1000 Subject: [PATCH 3/5] Fixes for subscription webhooks --- .../StoreCompanyGatewayRequest.php | 31 +++++++++++-------- app/Listeners/Invoice/InvoicePaidActivity.php | 2 ++ .../Subscription/SubscriptionService.php | 21 +++++++++++-- 3 files changed, 39 insertions(+), 15 deletions(-) diff --git a/app/Http/Requests/CompanyGateway/StoreCompanyGatewayRequest.php b/app/Http/Requests/CompanyGateway/StoreCompanyGatewayRequest.php index 4af93dba894e..8b4a9109e25a 100644 --- a/app/Http/Requests/CompanyGateway/StoreCompanyGatewayRequest.php +++ b/app/Http/Requests/CompanyGateway/StoreCompanyGatewayRequest.php @@ -47,27 +47,32 @@ class StoreCompanyGatewayRequest extends Request $gateway = Gateway::where('key', $input['gateway_key'])->first(); - $default_gateway_fields = json_decode($gateway->fields); + if($gateway); + { - /*Force gateway properties */ - if (isset($input['config']) && is_object(json_decode($input['config']))) { + $default_gateway_fields = json_decode($gateway->fields); - foreach (json_decode($input['config']) as $key => $value) { + /*Force gateway properties */ + if (isset($input['config']) && is_object(json_decode($input['config']))) { - $default_gateway_fields->{$key} = $value; + foreach (json_decode($input['config']) as $key => $value) { + + $default_gateway_fields->{$key} = $value; + + } + + $input['config'] = json_encode($default_gateway_fields); } - $input['config'] = json_encode($default_gateway_fields); - + if (isset($input['config'])) + $input['config'] = encrypt($input['config']); + + if (isset($input['fees_and_limits'])) + $input['fees_and_limits'] = $this->cleanFeesAndLimits($input['fees_and_limits']); + } - if (isset($input['config'])) - $input['config'] = encrypt($input['config']); - - if (isset($input['fees_and_limits'])) - $input['fees_and_limits'] = $this->cleanFeesAndLimits($input['fees_and_limits']); - $this->replace($input); } diff --git a/app/Listeners/Invoice/InvoicePaidActivity.php b/app/Listeners/Invoice/InvoicePaidActivity.php index e42fe41a006d..65351ff34c6b 100644 --- a/app/Listeners/Invoice/InvoicePaidActivity.php +++ b/app/Listeners/Invoice/InvoicePaidActivity.php @@ -39,6 +39,7 @@ class InvoicePaidActivity implements ShouldQueue */ public function handle($event) { + MultiDB::setDb($event->company->db); $fields = new stdClass; @@ -53,6 +54,7 @@ class InvoicePaidActivity implements ShouldQueue if($event->invoice->subscription()->exists()) { + nlog("subscription exists"); $event->invoice->subscription->service()->planPaid($event->invoice); } diff --git a/app/Services/Subscription/SubscriptionService.php b/app/Services/Subscription/SubscriptionService.php index aeca8d1b3535..2e0ed985739b 100644 --- a/app/Services/Subscription/SubscriptionService.php +++ b/app/Services/Subscription/SubscriptionService.php @@ -695,10 +695,14 @@ nlog("handle plan change"); */ public function triggerWebhook($context) { + nlog("trigger webook"); + if (empty($this->subscription->webhook_configuration['post_purchase_url']) || is_null($this->subscription->webhook_configuration['post_purchase_url']) || strlen($this->subscription->webhook_configuration['post_purchase_url']) < 1) { return ["message" => "Success", "status_code" => 200]; } + nlog("past first if"); + $response = false; $body = array_merge($context, [ @@ -709,6 +713,8 @@ nlog("handle plan change"); $response = $this->sendLoad($this->subscription, $body); + nlog("after response"); + /* Append the response to the system logger body */ if(is_array($response)){ @@ -732,6 +738,7 @@ nlog("handle plan change"); $client->company, ); + nlog("ready to fire back"); if(is_array($body)) return $response; @@ -909,14 +916,24 @@ nlog("handle plan change"); public function planPaid($invoice) { + nlog("this is a plan that has been paid"); + + $recurring_invoice_hashed_id = $invoice->recurring_invoice()->exists() ? $invoice->recurring_invoice->hashed_id : null; + $context = [ 'context' => 'plan_paid', 'subscription' => $this->subscription->hashed_id, - 'recurring_invoice' => $invoice->recurring_invoice->hashed_id, + 'recurring_invoice' => $recurring_invoice_hashed_id, 'client' => $invoice->client->hashed_id, 'contact' => $invoice->client->primary_contact()->first()->hashed_id, ]; - $this->triggerWebhook($context); + nlog($context); + + $response = $this->triggerWebhook($context); + + nlog($response); + + return true; } } From 6a771de80c37e08bf769acd37cbbb53f635252cd Mon Sep 17 00:00:00 2001 From: David Bomba Date: Thu, 15 Jul 2021 11:31:11 +1000 Subject: [PATCH 4/5] Fixes for recurring invoice subscription ID passing --- .../RecurringInvoiceToInvoiceFactory.php | 2 + tests/Feature/RecurringInvoiceTest.php | 57 +++++++++++++++++++ 2 files changed, 59 insertions(+) diff --git a/app/Factory/RecurringInvoiceToInvoiceFactory.php b/app/Factory/RecurringInvoiceToInvoiceFactory.php index 6a56902e93d3..8ddee75719b7 100644 --- a/app/Factory/RecurringInvoiceToInvoiceFactory.php +++ b/app/Factory/RecurringInvoiceToInvoiceFactory.php @@ -38,6 +38,8 @@ class RecurringInvoiceToInvoiceFactory $invoice->tax_rate2 = $recurring_invoice->tax_rate2; $invoice->tax_name3 = $recurring_invoice->tax_name3; $invoice->tax_rate3 = $recurring_invoice->tax_rate3; + $invoice->total_taxes = $recurring_invoice->total_taxes; + $invoice->subscription_id = $recurring_invoice->subscription_id; $invoice->custom_value1 = $recurring_invoice->custom_value1; $invoice->custom_value2 = $recurring_invoice->custom_value2; $invoice->custom_value3 = $recurring_invoice->custom_value3; diff --git a/tests/Feature/RecurringInvoiceTest.php b/tests/Feature/RecurringInvoiceTest.php index ca2e84d439de..746b3a78878f 100644 --- a/tests/Feature/RecurringInvoiceTest.php +++ b/tests/Feature/RecurringInvoiceTest.php @@ -10,6 +10,8 @@ */ namespace Tests\Feature; +use App\Factory\InvoiceToRecurringInvoiceFactory; +use App\Factory\RecurringInvoiceToInvoiceFactory; use App\Models\Client; use App\Models\ClientContact; use App\Models\RecurringInvoice; @@ -155,4 +157,59 @@ class RecurringInvoiceTest extends TestCase $response->assertStatus(200); } + + public function testSubscriptionIdPassesToInvoice() + { + $recurring_invoice = InvoiceToRecurringInvoiceFactory::create($this->invoice); + $recurring_invoice->user_id = $this->user->id; + $recurring_invoice->next_send_date = \Carbon\Carbon::now()->addDays(10); + $recurring_invoice->status_id = RecurringInvoice::STATUS_ACTIVE; + $recurring_invoice->remaining_cycles = 2; + $recurring_invoice->next_send_date = \Carbon\Carbon::now()->addDays(10); + $recurring_invoice->save(); + + $recurring_invoice->number = $this->getNextRecurringInvoiceNumber($this->invoice->client, $this->invoice); + $recurring_invoice->subscription_id = 10; + $recurring_invoice->save(); + + $invoice = RecurringInvoiceToInvoiceFactory::create($recurring_invoice, $this->client); + + $this->assertEquals(10, $invoice->subscription_id); + } + + public function testSubscriptionIdPassesToInvoiceIfNull() + { + $recurring_invoice = InvoiceToRecurringInvoiceFactory::create($this->invoice); + $recurring_invoice->user_id = $this->user->id; + $recurring_invoice->next_send_date = \Carbon\Carbon::now()->addDays(10); + $recurring_invoice->status_id = RecurringInvoice::STATUS_ACTIVE; + $recurring_invoice->remaining_cycles = 2; + $recurring_invoice->next_send_date = \Carbon\Carbon::now()->addDays(10); + $recurring_invoice->save(); + + $recurring_invoice->number = $this->getNextRecurringInvoiceNumber($this->invoice->client, $this->invoice); + $recurring_invoice->save(); + + $invoice = RecurringInvoiceToInvoiceFactory::create($recurring_invoice, $this->client); + + $this->assertEquals(null, $invoice->subscription_id); + } + + public function testSubscriptionIdPassesToInvoiceIfNothingSet() + { + $recurring_invoice = InvoiceToRecurringInvoiceFactory::create($this->invoice); + $recurring_invoice->user_id = $this->user->id; + $recurring_invoice->next_send_date = \Carbon\Carbon::now()->addDays(10); + $recurring_invoice->status_id = RecurringInvoice::STATUS_ACTIVE; + $recurring_invoice->remaining_cycles = 2; + $recurring_invoice->next_send_date = \Carbon\Carbon::now()->addDays(10); + $recurring_invoice->save(); + + $recurring_invoice->number = $this->getNextRecurringInvoiceNumber($this->invoice->client, $this->invoice); + $recurring_invoice->save(); + + $invoice = RecurringInvoiceToInvoiceFactory::create($recurring_invoice, $this->client); + + $this->assertEquals(null, $invoice->subscription_id); + } } From a04f2be3b4750a183fbab2055f3eddf900726569 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Thu, 15 Jul 2021 13:03:24 +1000 Subject: [PATCH 5/5] Minor Fixes --- app/Jobs/Account/CreateAccount.php | 1 + 1 file changed, 1 insertion(+) diff --git a/app/Jobs/Account/CreateAccount.php b/app/Jobs/Account/CreateAccount.php index 3c03138d04d1..9436ff420e40 100644 --- a/app/Jobs/Account/CreateAccount.php +++ b/app/Jobs/Account/CreateAccount.php @@ -81,6 +81,7 @@ class CreateAccount { $sp794f3f->trial_started = now(); $sp794f3f->trial_plan = 'pro'; + $sp794f3f->plan = 'pro'; $sp794f3f->save(); }