diff --git a/app/Factory/RecurringInvoiceToInvoiceFactory.php b/app/Factory/RecurringInvoiceToInvoiceFactory.php index 6a56902e93..8ddee75719 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/app/Http/Requests/CompanyGateway/StoreCompanyGatewayRequest.php b/app/Http/Requests/CompanyGateway/StoreCompanyGatewayRequest.php index 4af93dba89..8b4a9109e2 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/Jobs/Account/CreateAccount.php b/app/Jobs/Account/CreateAccount.php index 3c03138d04..9436ff420e 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(); } diff --git a/app/Listeners/Invoice/InvoicePaidActivity.php b/app/Listeners/Invoice/InvoicePaidActivity.php index 7991ad3b5b..65351ff34c 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; @@ -51,6 +52,12 @@ class InvoicePaidActivity implements ShouldQueue $this->activity_repo->save($fields, $event->invoice, $event->event_vars); + if($event->invoice->subscription()->exists()) + { + nlog("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 3a375016dc..75c3e314a8 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 963bb93217..2e0ed98573 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; @@ -906,4 +913,27 @@ nlog("handle plan change"); return redirect($default_redirect); } + + 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' => $recurring_invoice_hashed_id, + 'client' => $invoice->client->hashed_id, + 'contact' => $invoice->client->primary_contact()->first()->hashed_id, + ]; + + nlog($context); + + $response = $this->triggerWebhook($context); + + nlog($response); + + return true; + } } diff --git a/tests/Feature/RecurringInvoiceTest.php b/tests/Feature/RecurringInvoiceTest.php index ca2e84d439..746b3a7887 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); + } } diff --git a/tests/Unit/SettingsSaverTest.php b/tests/Unit/SettingsSaverTest.php new file mode 100644 index 0000000000..3f473efcb4 --- /dev/null +++ b/tests/Unit/SettingsSaverTest.php @@ -0,0 +1,39 @@ +checkAttribute($key, $value); + + $this->assertFalse($result); + + } +}