Move atomic operations to Jobs instead of listeners

This commit is contained in:
David Bomba 2019-10-03 22:02:31 +10:00
parent 724cccacfc
commit 87d61262ca
4 changed files with 150 additions and 6 deletions

View File

@ -63,11 +63,9 @@ class MarkInvoicePaid implements ShouldQueue
$payment->save();
/* Update Invoice balance */
//$invoice = ApplyPaymentToInvoice::dispatchNow($payment, $this->invoice);
event(new PaymentWasCreated($payment));
UpdateCompanyLedgerWithPayment::dispatchNow($payment, ($payment->amount*-1));
$this->invoice->updateBalance(($payment->amount*-1));
return $this->invoice;
}

View File

@ -0,0 +1,142 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com)
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2019. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\Jobs\Invoice;
use App\Jobs\Company\UpdateCompanyLedgerWithInvoice;
use App\Jobs\Company\UpdateCompanyLedgerWithPayment;
use App\Models\Payment;
use App\Models\SystemLog;
use App\Utils\Traits\SystemLogTrait;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
class UpdateInvoicePayment implements ShouldQueue
{
use SystemLogTrait, Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public $payment;
/**
* Create the event listener.
*
* @return void
*/
public function __construct(Payment $payment)
{
$this->payment = $payment;
}
/**
* Handle the event.
*
* @param object $event
* @return void
*/
public function handle()
{
$invoices = $this->payment->invoices();
$invoices_total = $invoices->sum('balance');
/* Simplest scenario*/
if($invoices_total == $this->payment->amount)
{
$invoices->each(function ($invoice){
UpdateCompanyLedgerWithPayment::dispatchNow($this->payment, ($invoice->balance*-1));
$invoice->clearPartial();
$invoice->updateBalance($invoice->balance*-1);
});
}
else {
$total = 0;
foreach($invoice as $invoice)
{
if($invoice->hasPartial())
$total += $invoice->partial;
else
$total += $invoice->balance;
Log::error("total = {$total}");
}
/* test if there is a batch of partial invoices that have been paid */
if($this->payment->amount == $total)
{
$invoices->each(function ($invoice){
if($invoice->hasPartial()) {
UpdateCompanyLedgerWithPayment::dispatchNow($this->payment, ($invoice->partial*-1));
$invoice->updateBalance($invoice->partial*-1);
$invoice->clearPartial();
$invoice->setDueDate();
//todo do we need to mark it as a partial?
}
else
{
UpdateCompanyLedgerWithPayment::dispatchNow($this->payment, ($invoice->balance*-1));
$invoice->clearPartial();
$invoice->updateBalance($invoice->balance*-1);
}
});
}
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');
}
}
}
}
/*
$this->payment = $event->payment;
$invoice = $this->payment->invoice;
$adjustment = $this->payment->amount * -1;
$partial = max(0, $invoice->partial - $this->payment->amount);
$invoice->updateBalances($adjustment, $partial);
$invoice->updatePaidStatus(true);
// store a backup of the invoice
$activity = Activity::wherePaymentId($this->payment->id)
->whereActivityTypeId(ACTIVITY_TYPE_CREATE_PAYMENT)
->first();
$activity->json_backup = $invoice->hidePrivateFields()->toJSON();
$activity->save();
if ($invoice->balance == 0 && $this->payment->account->auto_archive_invoice) {
$invoiceRepo = app('App\Ninja\Repositories\InvoiceRepository');
$invoiceRepo->archive($invoice);
}
*/

View File

@ -12,6 +12,7 @@
namespace App\PaymentDrivers;
use App\Events\Payment\PaymentWasCreated;
use App\Jobs\Invoice\UpdateInvoicePayment;
use App\Models\ClientGatewayToken;
use App\Models\GatewayType;
use App\Models\Payment;
@ -138,6 +139,8 @@ class PayPalExpressPaymentDriver extends BasePaymentDriver
$this->attachInvoices($payment, $request->input('hashed_ids'));
event(new PaymentWasCreated($payment));
UpdateInvoicePayment::dispatchNow($payment);
return redirect()->route('client.payments.show', ['payment'=>$this->encodePrimaryKey($payment->id)]);

View File

@ -13,6 +13,7 @@ namespace App\PaymentDrivers;
use App\Events\Payment\PaymentWasCreated;
use App\Factory\PaymentFactory;
use App\Jobs\Invoice\UpdateInvoicePayment;
use App\Models\ClientGatewayToken;
use App\Models\GatewayType;
use App\Models\Invoice;
@ -362,6 +363,8 @@ class StripePaymentDriver extends BasePaymentDriver
event(new PaymentWasCreated($payment));
UpdateInvoicePayment::dispatchNow($payment);
return redirect()->route('client.payments.show', ['payment' => $this->encodePrimaryKey($payment->id)]);
}
@ -369,12 +372,10 @@ class StripePaymentDriver extends BasePaymentDriver
{
/*Fail and log*/
$log = [
$this->sysLog([
'server_response' => $server_response,
'invoices' => $invoices,
];
$this->sysLog($log);
]);
throw new Exception("Failed to process payment", 1);