diff --git a/app/Jobs/Company/UpdateCompanyLedgerWithInvoice.php b/app/Jobs/Company/UpdateCompanyLedgerWithInvoice.php index 75d096d21c74..eb9f117bd768 100644 --- a/app/Jobs/Company/UpdateCompanyLedgerWithInvoice.php +++ b/app/Jobs/Company/UpdateCompanyLedgerWithInvoice.php @@ -46,6 +46,8 @@ class UpdateCompanyLedgerWithInvoice */ public function handle() { + \Log::error('in update company ledger with invoice'); + $balance = 0; $ledger = CompanyLedger::whereClientId($this->invoice->client_id) @@ -56,7 +58,9 @@ class UpdateCompanyLedgerWithInvoice if($ledger) $balance = $ledger->balance; -\Log::error("adjusting balance {$balance}"); + $adjustment = $balance + $this->adjustment; + + \Log::error("adjusting balance {$balance} to {$adjustment}"); $company_ledger = CompanyLedgerFactory::create($this->invoice->company_id, $this->invoice->user_id); $company_ledger->client_id = $this->invoice->client_id; diff --git a/app/Jobs/Company/UpdateCompanyLedgerWithPayment.php b/app/Jobs/Company/UpdateCompanyLedgerWithPayment.php index 02cb93cbd124..fcea4f42d8af 100644 --- a/app/Jobs/Company/UpdateCompanyLedgerWithPayment.php +++ b/app/Jobs/Company/UpdateCompanyLedgerWithPayment.php @@ -52,8 +52,6 @@ class UpdateCompanyLedgerWithPayment { $balance = 0; - $this->adjustment = $this->adjustment * -1; - /* Get the last record for the client and set the current balance*/ $ledger = CompanyLedger::whereClientId($this->payment->client_id) ->whereCompanyId($this->payment->company_id) diff --git a/app/Listeners/Invoice/UpdateInvoicePayment.php b/app/Listeners/Invoice/UpdateInvoicePayment.php index a0b0fbfb8c40..2d315bdf6fdf 100644 --- a/app/Listeners/Invoice/UpdateInvoicePayment.php +++ b/app/Listeners/Invoice/UpdateInvoicePayment.php @@ -12,6 +12,7 @@ namespace App\Listeners\Invoice; use App\Jobs\Company\UpdateCompanyLedgerWithInvoice; +use App\Jobs\Company\UpdateCompanyLedgerWithPayment; use App\Models\SystemLog; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Queue\InteractsWithQueue; @@ -43,15 +44,17 @@ class UpdateInvoicePayment implements ShouldQueue /* Simplest scenario*/ if($invoices_total == $payment->amount) { - - $invoices->each(function ($invoice){ + \Log::error("invoice totals match payment amount"); + $invoices->each(function ($invoice) use($payment){ + //$invoice->updateBalance($invoice->balance*-1); + //UpdateCompanyLedgerWithInvoice::dispatchNow($invoice, ($invoice->balance*-1)); + UpdateCompanyLedgerWithPayment::dispatchNow($payment, ($invoice->balance*-1)); $invoice->updateBalance($invoice->balance*-1); - UpdateCompanyLedgerWithInvoice::dispatchNow($invoice, ($invoice->balance*-1)); }); } else { - + \Log::error("invoice totals don't match, search for partials"); $total = 0; foreach($invoice as $invoice) @@ -62,6 +65,7 @@ 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 */ @@ -72,7 +76,7 @@ class UpdateInvoicePayment implements ShouldQueue //paid } else { - + \Log::error("no matches, fail"); $data = [ 'payment' => $payment, 'invoices' => $invoices, diff --git a/app/Models/Invoice.php b/app/Models/Invoice.php index 15cfbc874c19..57e081518e02 100644 --- a/app/Models/Invoice.php +++ b/app/Models/Invoice.php @@ -307,6 +307,7 @@ class Invoice extends BaseModel */ public function updateBalance($balance_adjustment) { + \Log::error('updating invoice balance'); if ($this->is_deleted) return; @@ -319,7 +320,6 @@ class Invoice extends BaseModel $this->status_id = self::STATUS_PAID; $this->save(); - - + \Log::error('finished updatingoice balance'); } } \ No newline at end of file diff --git a/app/PaymentDrivers/StripePaymentDriver.php b/app/PaymentDrivers/StripePaymentDriver.php index 9cc6c6312f17..ee0520894247 100644 --- a/app/PaymentDrivers/StripePaymentDriver.php +++ b/app/PaymentDrivers/StripePaymentDriver.php @@ -11,12 +11,14 @@ namespace App\PaymentDrivers; +use App\Events\Payment\PaymentWasCreated; use App\Factory\PaymentFactory; use App\Models\ClientGatewayToken; use App\Models\GatewayType; use App\Models\Invoice; use App\Models\Payment; use App\Models\PaymentType; +use App\Models\SystemLog; use App\Utils\Traits\MakesHash; use Illuminate\Http\Request; use Illuminate\Support\Carbon; @@ -345,19 +347,20 @@ class StripePaymentDriver extends BasePaymentDriver if(!$payment_type) $payment_type = PaymentType::CREDIT_CARD_OTHER; - $payment = PaymentFactory::create($this->client->company->id, $this->client->user->id); - $payment->client_id = $this->client->id; - $payment->client_contact_id = $client_contact->id; - $payment->company_gateway_id = $this->company_gateway->id; - $payment->status_id = Payment::STATUS_COMPLETED; - $payment->amount = $this->convertFromStripeAmount($server_response->amount, $this->client->currency->precision); - $payment->payment_date = Carbon::now(); - $payment->payment_type_id = $payment_type; - $payment->transaction_reference = $payment_method; - $payment->save(); + $data = [ + 'payment_method' => $payment_method, + 'payment_type' => $payment_type, + 'amount' => $server_response->amount, + ]; + + /* Create payment*/ + $payment = $this->createPayment($data); + + /* Link invoices to payment*/ $this->attachInvoices($payment, $hashed_ids); + event(new PaymentWasCreated($payment)); /** * Move this into an event @@ -371,9 +374,50 @@ class StripePaymentDriver extends BasePaymentDriver }); - return redirect()->route('client.payments.show', ['id' => $this->encodePrimaryKey($payment->id)]); + return redirect()->route('client.payments.show', ['payment' => $this->encodePrimaryKey($payment->id)]); } + else + { + /*Fail and log*/ + + $log = [ + 'server_response' => $server_response, + 'invoices' => $invoices, + ]; + + $sl = [ + 'company_id' => $this->client->company->id, + 'client_id' => $this->client->id, + 'user_id' => $this->client->user_id, + 'log' => $log, + 'category_id' => SystemLog::GATEWAY_RESPONSE, + 'event_id' => SystemLog::GATEWAY_FAILURE, + ]; + + SystemLog::create($sl); + + throw new Exception("Failed to process payment", 1); + + } + + } + + public function createPayment($data) + { + + $payment = parent::createPayment($data); + + $client_contact = $this->getContact(); + $client_contact_id = $client_contact ? $client_contact->id : null; + + $payment->amount = $this->convertFromStripeAmount($data['amount'], $this->client->currency->precision); + $payment->payment_type_id = $data['payment_type']; + $payment->transaction_reference = $data['payment_method']; + $payment->client_contact_id = $client_contact_id; + $payment->save(); + + return $payment; } diff --git a/database/seeds/RandomDataSeeder.php b/database/seeds/RandomDataSeeder.php index 1a527be7fe5b..e46427293b4c 100644 --- a/database/seeds/RandomDataSeeder.php +++ b/database/seeds/RandomDataSeeder.php @@ -6,6 +6,7 @@ use App\DataMapper\DefaultSettings; use App\Events\Invoice\InvoiceWasMarkedSent; use App\Events\Invoice\InvoiceWasUpdated; use App\Helpers\Invoice\InvoiceCalc; +use App\Jobs\Company\UpdateCompanyLedgerWithInvoice; use App\Listeners\Invoice\CreateInvoiceInvitation; use App\Models\Account; use App\Models\Client; @@ -122,6 +123,8 @@ class RandomDataSeeder extends Seeder $invoice->save(); event(new CreateInvoiceInvitation($invoice)); + + UpdateCompanyLedgerWithInvoice::dispatchNow($invoice, $invoice->balance); $invoice_repo->markSent($invoice);