diff --git a/app/Jobs/Invoice/UpdateInvoicePayment.php b/app/Jobs/Invoice/UpdateInvoicePayment.php index e486e5fbce73..a53f6d5a8991 100644 --- a/app/Jobs/Invoice/UpdateInvoicePayment.php +++ b/app/Jobs/Invoice/UpdateInvoicePayment.php @@ -46,12 +46,12 @@ class UpdateInvoicePayment implements ShouldQueue public function handle() { - $invoices = $this->payment->invoices(); + $invoices = $this->payment->invoices()->get(); $invoices_total = $invoices->sum('balance'); /* Simplest scenario*/ - if($invoices_total == $this->payment->amount) + if(strval($invoices_total) === strval($this->payment->amount)) { $invoices->each(function ($invoice){ @@ -66,7 +66,8 @@ class UpdateInvoicePayment implements ShouldQueue $total = 0; - foreach($invoice as $invoice) + + foreach($invoices as $invoice) { if($invoice->hasPartial()) @@ -74,7 +75,6 @@ class UpdateInvoicePayment implements ShouldQueue else $total += $invoice->balance; - Log::error("total = {$total}"); } /* test if there is a batch of partial invoices that have been paid */ @@ -103,15 +103,16 @@ class UpdateInvoicePayment implements ShouldQueue } else { +/* $this->sysLog([ 'payment' => $this->payment, 'invoices' => $invoices, 'invoices_total' => $invoices_total, 'payment_amount' => $this->payment->amount, 'partial_check_amount' => $total, - ], SystemLog::GATEWAY_RESPONSE, SystemLog::PAYMENT_RECONCILIATION_FAILURE); - - throw new Exception('payment amount does not match invoice totals'); + ], SystemLog::GATEWAY_RESPONSE, SystemLog::PAYMENT_RECONCILIATION_FAILURE, $this->payment->client); +*/ + throw new \Exception("payment amount {$this->payment->amount} does not match invoice totals {$invoices_total}"); } diff --git a/app/PaymentDrivers/PayPalExpressPaymentDriver.php b/app/PaymentDrivers/PayPalExpressPaymentDriver.php index 43c6aa704ff1..a00fd4a26f25 100644 --- a/app/PaymentDrivers/PayPalExpressPaymentDriver.php +++ b/app/PaymentDrivers/PayPalExpressPaymentDriver.php @@ -110,7 +110,7 @@ class PayPalExpressPaymentDriver extends BasePaymentDriver 'data' => $data ]); - throw new Exception("Error Processing Payment", 1); + throw new \Exception("Error Processing Payment", 1); } } @@ -132,7 +132,7 @@ class PayPalExpressPaymentDriver extends BasePaymentDriver 'server_response' => $response->getData() ]); - throw new Exception($response->getMessage()); + throw new \Exception($response->getMessage()); } $payment = $this->createPayment($response->getData()); diff --git a/app/PaymentDrivers/StripePaymentDriver.php b/app/PaymentDrivers/StripePaymentDriver.php index 78146cf8f42a..c65a1316bf2a 100644 --- a/app/PaymentDrivers/StripePaymentDriver.php +++ b/app/PaymentDrivers/StripePaymentDriver.php @@ -377,7 +377,7 @@ class StripePaymentDriver extends BasePaymentDriver 'invoices' => $invoices, ]); - throw new Exception("Failed to process payment", 1); + throw new \Exception("Failed to process payment", 1); } @@ -484,7 +484,7 @@ class StripePaymentDriver extends BasePaymentDriver } if(!$customer) - throw new Exception('Unable to create gateway customer'); + throw new \Exception('Unable to create gateway customer'); return $customer; } diff --git a/app/Utils/Traits/SystemLogTrait.php b/app/Utils/Traits/SystemLogTrait.php index 5b3fd65707db..3604bea3755a 100644 --- a/app/Utils/Traits/SystemLogTrait.php +++ b/app/Utils/Traits/SystemLogTrait.php @@ -11,6 +11,7 @@ namespace App\Utils\Traits; +use App\Models\Client; use App\Models\SystemLog; /** @@ -20,9 +21,12 @@ use App\Models\SystemLog; trait SystemLogTrait { - public function sysLog($log, $category_id = SystemLog::GATEWAY_RESPONSE, $event_id = SystemLog::GATEWAY_FAILURE) + public function sysLog($log, $category_id = SystemLog::GATEWAY_RESPONSE, $event_id = SystemLog::GATEWAY_FAILURE, Client $client = null) { + if($client != null) + $this->client = $client; + $sl = [ 'client_id' => $this->client->id, 'company_id' => $this->client->company->id, diff --git a/database/seeds/RandomDataSeeder.php b/database/seeds/RandomDataSeeder.php index e46427293b4c..c8db0649dc1a 100644 --- a/database/seeds/RandomDataSeeder.php +++ b/database/seeds/RandomDataSeeder.php @@ -5,8 +5,10 @@ use App\DataMapper\CompanySettings; use App\DataMapper\DefaultSettings; use App\Events\Invoice\InvoiceWasMarkedSent; use App\Events\Invoice\InvoiceWasUpdated; +use App\Events\Payment\PaymentWasCreated; use App\Helpers\Invoice\InvoiceCalc; use App\Jobs\Company\UpdateCompanyLedgerWithInvoice; +use App\Jobs\Invoice\UpdateInvoicePayment; use App\Listeners\Invoice\CreateInvoiceInvitation; use App\Models\Account; use App\Models\Client; @@ -16,6 +18,7 @@ use App\Models\CompanyToken; use App\Models\GatewayType; use App\Models\GroupSetting; use App\Models\Invoice; +use App\Models\PaymentType; use App\Models\User; use App\Models\UserAccount; use App\Repositories\InvoiceRepository; @@ -114,7 +117,7 @@ class RandomDataSeeder extends Seeder $invoices = Invoice::all(); $invoice_repo = new InvoiceRepository(); - $invoices->each(function ($invoice) use($invoice_repo){ + $invoices->each(function ($invoice) use($invoice_repo, $user, $company, $client){ $invoice_calc = new InvoiceCalc($invoice, $invoice->settings); @@ -129,6 +132,22 @@ class RandomDataSeeder extends Seeder $invoice_repo->markSent($invoice); event(new InvoiceWasMarkedSent($invoice)); + + $payment = App\Models\Payment::create([ + 'user_id' => $user->id, + 'company_id' => $company->id, + 'client_id' => $client->id, + 'amount' => $invoice->balance, + 'transaction_reference' => rand(0,500), + 'payment_type_id' => PaymentType::CREDIT_CARD_OTHER, + ]); + + $payment->invoices()->save($invoice); + + event(new PaymentWasCreated($payment)); + + UpdateInvoicePayment::dispatchNow($payment); + }); /** Recurring Invoice Factory */ @@ -136,7 +155,6 @@ class RandomDataSeeder extends Seeder // factory(\App\Models\Payment::class,20)->create(['user_id' => $user->id, 'company_id' => $company->id, 'client_id' => $client->id, 'settings' => ClientSettings::buildClientSettings($company->settings, $client->settings)]); - factory(\App\Models\Payment::class,20)->create(['user_id' => $user->id, 'company_id' => $company->id, 'client_id' => $client->id]); $clients = Client::all();