Refactor payment events

This commit is contained in:
David Bomba 2019-10-04 16:22:22 +10:00
parent 3900529e83
commit a83099dad8
5 changed files with 37 additions and 14 deletions

View File

@ -46,12 +46,12 @@ class UpdateInvoicePayment implements ShouldQueue
public function handle() public function handle()
{ {
$invoices = $this->payment->invoices(); $invoices = $this->payment->invoices()->get();
$invoices_total = $invoices->sum('balance'); $invoices_total = $invoices->sum('balance');
/* Simplest scenario*/ /* Simplest scenario*/
if($invoices_total == $this->payment->amount) if(strval($invoices_total) === strval($this->payment->amount))
{ {
$invoices->each(function ($invoice){ $invoices->each(function ($invoice){
@ -66,7 +66,8 @@ class UpdateInvoicePayment implements ShouldQueue
$total = 0; $total = 0;
foreach($invoice as $invoice)
foreach($invoices as $invoice)
{ {
if($invoice->hasPartial()) if($invoice->hasPartial())
@ -74,7 +75,6 @@ class UpdateInvoicePayment implements ShouldQueue
else else
$total += $invoice->balance; $total += $invoice->balance;
Log::error("total = {$total}");
} }
/* test if there is a batch of partial invoices that have been paid */ /* test if there is a batch of partial invoices that have been paid */
@ -103,15 +103,16 @@ class UpdateInvoicePayment implements ShouldQueue
} }
else { else {
/*
$this->sysLog([ $this->sysLog([
'payment' => $this->payment, 'payment' => $this->payment,
'invoices' => $invoices, 'invoices' => $invoices,
'invoices_total' => $invoices_total, 'invoices_total' => $invoices_total,
'payment_amount' => $this->payment->amount, 'payment_amount' => $this->payment->amount,
'partial_check_amount' => $total, 'partial_check_amount' => $total,
], SystemLog::GATEWAY_RESPONSE, SystemLog::PAYMENT_RECONCILIATION_FAILURE); ], SystemLog::GATEWAY_RESPONSE, SystemLog::PAYMENT_RECONCILIATION_FAILURE, $this->payment->client);
*/
throw new Exception('payment amount does not match invoice totals'); throw new \Exception("payment amount {$this->payment->amount} does not match invoice totals {$invoices_total}");
} }

View File

@ -110,7 +110,7 @@ class PayPalExpressPaymentDriver extends BasePaymentDriver
'data' => $data '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() 'server_response' => $response->getData()
]); ]);
throw new Exception($response->getMessage()); throw new \Exception($response->getMessage());
} }
$payment = $this->createPayment($response->getData()); $payment = $this->createPayment($response->getData());

View File

@ -377,7 +377,7 @@ class StripePaymentDriver extends BasePaymentDriver
'invoices' => $invoices, '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) if(!$customer)
throw new Exception('Unable to create gateway customer'); throw new \Exception('Unable to create gateway customer');
return $customer; return $customer;
} }

View File

@ -11,6 +11,7 @@
namespace App\Utils\Traits; namespace App\Utils\Traits;
use App\Models\Client;
use App\Models\SystemLog; use App\Models\SystemLog;
/** /**
@ -20,9 +21,12 @@ use App\Models\SystemLog;
trait SystemLogTrait 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 = [ $sl = [
'client_id' => $this->client->id, 'client_id' => $this->client->id,
'company_id' => $this->client->company->id, 'company_id' => $this->client->company->id,

View File

@ -5,8 +5,10 @@ use App\DataMapper\CompanySettings;
use App\DataMapper\DefaultSettings; use App\DataMapper\DefaultSettings;
use App\Events\Invoice\InvoiceWasMarkedSent; use App\Events\Invoice\InvoiceWasMarkedSent;
use App\Events\Invoice\InvoiceWasUpdated; use App\Events\Invoice\InvoiceWasUpdated;
use App\Events\Payment\PaymentWasCreated;
use App\Helpers\Invoice\InvoiceCalc; use App\Helpers\Invoice\InvoiceCalc;
use App\Jobs\Company\UpdateCompanyLedgerWithInvoice; use App\Jobs\Company\UpdateCompanyLedgerWithInvoice;
use App\Jobs\Invoice\UpdateInvoicePayment;
use App\Listeners\Invoice\CreateInvoiceInvitation; use App\Listeners\Invoice\CreateInvoiceInvitation;
use App\Models\Account; use App\Models\Account;
use App\Models\Client; use App\Models\Client;
@ -16,6 +18,7 @@ use App\Models\CompanyToken;
use App\Models\GatewayType; use App\Models\GatewayType;
use App\Models\GroupSetting; use App\Models\GroupSetting;
use App\Models\Invoice; use App\Models\Invoice;
use App\Models\PaymentType;
use App\Models\User; use App\Models\User;
use App\Models\UserAccount; use App\Models\UserAccount;
use App\Repositories\InvoiceRepository; use App\Repositories\InvoiceRepository;
@ -114,7 +117,7 @@ class RandomDataSeeder extends Seeder
$invoices = Invoice::all(); $invoices = Invoice::all();
$invoice_repo = new InvoiceRepository(); $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); $invoice_calc = new InvoiceCalc($invoice, $invoice->settings);
@ -129,6 +132,22 @@ class RandomDataSeeder extends Seeder
$invoice_repo->markSent($invoice); $invoice_repo->markSent($invoice);
event(new InvoiceWasMarkedSent($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 */ /** 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, '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(); $clients = Client::all();