Add event to process webhook payments

This commit is contained in:
Deon George 2023-05-13 23:51:27 +10:00
parent 12b63a506f
commit c1bb20dec0
6 changed files with 97 additions and 5 deletions

View File

@ -0,0 +1,40 @@
<?php
namespace App\Events;
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
class ProviderPaymentCreated
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public array $paymentData;
public string $provider;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct(string $provider,array $paymentData)
{
$this->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');
}
}

View File

@ -9,9 +9,10 @@ use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels; use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Log; 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; use Intuit\Models\Payment as PaymentModel;
/** /**
@ -49,6 +50,10 @@ class AccountingPaymentSync implements ShouldQueue
*/ */
public function handle() 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 // See if we are already linked
if (($x=$this->to->provider->payments->where('pivot.ref',$this->pmi->id))->count() === 1) { if (($x=$this->to->provider->payments->where('pivot.ref',$this->pmi->id))->count() === 1) {
$o = $x->pop(); $o = $x->pop();

View File

@ -0,0 +1,40 @@
<?php
namespace App\Listeners;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Log;
use App\Events\ProviderPaymentCreated as Event;
use App\Jobs\AccountingPaymentSync as Job;
use App\Models\{ProviderOauth,Site,User};
class ProviderPaymentCreated
{
private const LOGKEY = 'LPC';
/**
* Handle the event.
*
* @param Event $event
* @return void
*/
public function handle(Event $event)
{
$site = Site::findOrFail(1); // @todo This shouldnt be hard coded
Config::set('site',$site);
$uo = User::findOrFail(1); // @todo This shouldnt be hard coded
$so = ProviderOauth::where('name',$event->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);
}
}

View File

@ -21,7 +21,8 @@ class SiteScope implements Scope
! collect($builder->getQuery()->wheres)->pluck('column')->contains(function($item) { return preg_match('/^(.*[^.]\.)?site_id/',$item); }), ! collect($builder->getQuery()->wheres)->pluck('column')->contains(function($item) { return preg_match('/^(.*[^.]\.)?site_id/',$item); }),
function($q) use ($model) 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);
} }
); );
} }

View File

@ -5,6 +5,8 @@ namespace App\Providers;
use Illuminate\Support\Facades\Event; use Illuminate\Support\Facades\Event;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider; use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
use App\Listeners\ProviderPaymentCreated;
class EventServiceProvider extends ServiceProvider class EventServiceProvider extends ServiceProvider
{ {
/** /**
@ -16,6 +18,10 @@ class EventServiceProvider extends ServiceProvider
'Illuminate\Mail\Events\MessageSent' => [ 'Illuminate\Mail\Events\MessageSent' => [
'App\Listeners\LogSentMessage', 'App\Listeners\LogSentMessage',
], ],
\App\Events\ProviderPaymentCreated::class => [
ProviderPaymentCreated::class,
],
]; ];
/** /**

6
composer.lock generated
View File

@ -3681,11 +3681,11 @@
}, },
{ {
"name": "leenooks/intuit", "name": "leenooks/intuit",
"version": "0.1.5", "version": "0.1.6",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://dev.dege.au/leenooks/intuit", "url": "https://dev.dege.au/leenooks/intuit",
"reference": "dd8250900895daf911230db10177e40a98530f9f" "reference": "600dfec536041b318f3a496da9e42f8fb8d02b64"
}, },
"require": { "require": {
"jenssegers/model": "^1.5" "jenssegers/model": "^1.5"
@ -3715,7 +3715,7 @@
"laravel", "laravel",
"leenooks" "leenooks"
], ],
"time": "2023-05-13T11:58:49+00:00" "time": "2023-05-13T13:46:04+00:00"
}, },
{ {
"name": "leenooks/laravel", "name": "leenooks/laravel",