From c1bb20dec047f2a0cc94654e0da09b2d98ff56d0 Mon Sep 17 00:00:00 2001 From: Deon George Date: Sat, 13 May 2023 23:51:27 +1000 Subject: [PATCH] Add event to process webhook payments --- app/Events/ProviderPaymentCreated.php | 40 ++++++++++++++++++++++++ app/Jobs/AccountingPaymentSync.php | 7 ++++- app/Listeners/ProviderPaymentCreated.php | 40 ++++++++++++++++++++++++ app/Models/Scopes/SiteScope.php | 3 +- app/Providers/EventServiceProvider.php | 6 ++++ composer.lock | 6 ++-- 6 files changed, 97 insertions(+), 5 deletions(-) create mode 100644 app/Events/ProviderPaymentCreated.php create mode 100644 app/Listeners/ProviderPaymentCreated.php diff --git a/app/Events/ProviderPaymentCreated.php b/app/Events/ProviderPaymentCreated.php new file mode 100644 index 0000000..3c9e22d --- /dev/null +++ b/app/Events/ProviderPaymentCreated.php @@ -0,0 +1,40 @@ +provider = $provider; + $this->paymentData = $paymentData; + } + + /** + * Get the channels the event should broadcast on. + * + * @return \Illuminate\Broadcasting\Channel|array + */ + public function broadcastOn() + { + return new PrivateChannel('channel-name'); + } +} diff --git a/app/Jobs/AccountingPaymentSync.php b/app/Jobs/AccountingPaymentSync.php index 2700017..b486080 100644 --- a/app/Jobs/AccountingPaymentSync.php +++ b/app/Jobs/AccountingPaymentSync.php @@ -9,9 +9,10 @@ use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\SerializesModels; +use Illuminate\Support\Facades\Config; use Illuminate\Support\Facades\Log; -use App\Models\{Account,Invoice,Payment,PaymentItem,ProviderToken}; +use App\Models\{Account,Invoice,Payment,PaymentItem,ProviderToken,Site}; use Intuit\Models\Payment as PaymentModel; /** @@ -49,6 +50,10 @@ class AccountingPaymentSync implements ShouldQueue */ public function handle() { + // @todo Can this be automatically determined? + $site = Site::findOrFail($this->to->site_id); + Config::set('site',$site); + // See if we are already linked if (($x=$this->to->provider->payments->where('pivot.ref',$this->pmi->id))->count() === 1) { $o = $x->pop(); diff --git a/app/Listeners/ProviderPaymentCreated.php b/app/Listeners/ProviderPaymentCreated.php new file mode 100644 index 0000000..901b3c7 --- /dev/null +++ b/app/Listeners/ProviderPaymentCreated.php @@ -0,0 +1,40 @@ +provider)->singleOrFail(); + if (! ($to=$so->token($uo))) + abort(500,sprintf('Unknown Tokens for [%s]',$uo->email)); + + $api = $to->API(); + $acc = $api->getPayment($event->paymentData['id']); + + Job::dispatch($to,$acc); + } +} \ No newline at end of file diff --git a/app/Models/Scopes/SiteScope.php b/app/Models/Scopes/SiteScope.php index 54e4727..dd5ea18 100644 --- a/app/Models/Scopes/SiteScope.php +++ b/app/Models/Scopes/SiteScope.php @@ -21,7 +21,8 @@ class SiteScope implements Scope ! collect($builder->getQuery()->wheres)->pluck('column')->contains(function($item) { return preg_match('/^(.*[^.]\.)?site_id/',$item); }), function($q) use ($model) { - return $q->where($model->getTable().'.site_id',config('site')->site_id); + // @todo Remove this override "?? 1" it was put in place to retry failed AccountingPaymentSync + return $q->where($model->getTable().'.site_id',config('site')->site_id ?? 1); } ); } diff --git a/app/Providers/EventServiceProvider.php b/app/Providers/EventServiceProvider.php index 4acb030..d8696e0 100644 --- a/app/Providers/EventServiceProvider.php +++ b/app/Providers/EventServiceProvider.php @@ -5,6 +5,8 @@ namespace App\Providers; use Illuminate\Support\Facades\Event; use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider; +use App\Listeners\ProviderPaymentCreated; + class EventServiceProvider extends ServiceProvider { /** @@ -16,6 +18,10 @@ class EventServiceProvider extends ServiceProvider 'Illuminate\Mail\Events\MessageSent' => [ 'App\Listeners\LogSentMessage', ], + + \App\Events\ProviderPaymentCreated::class => [ + ProviderPaymentCreated::class, + ], ]; /** diff --git a/composer.lock b/composer.lock index fec2a3f..6913fca 100644 --- a/composer.lock +++ b/composer.lock @@ -3681,11 +3681,11 @@ }, { "name": "leenooks/intuit", - "version": "0.1.5", + "version": "0.1.6", "source": { "type": "git", "url": "https://dev.dege.au/leenooks/intuit", - "reference": "dd8250900895daf911230db10177e40a98530f9f" + "reference": "600dfec536041b318f3a496da9e42f8fb8d02b64" }, "require": { "jenssegers/model": "^1.5" @@ -3715,7 +3715,7 @@ "laravel", "leenooks" ], - "time": "2023-05-13T11:58:49+00:00" + "time": "2023-05-13T13:46:04+00:00" }, { "name": "leenooks/laravel",