feat(integrations): resolve payment context from events
This commit is contained in:
@@ -2,12 +2,12 @@
|
||||
|
||||
namespace App\Shared\Integration\Services;
|
||||
|
||||
use App\Domains\Core\Client\Models\Client;
|
||||
use App\Domains\Commerce\Purchase\Models\Purchase;
|
||||
use App\Domains\Commerce\Purchase\Models\TelepagosPayment;
|
||||
use App\Domains\Commerce\Purchase\Models\TelepagosQr;
|
||||
use App\Domains\Commerce\Purchase\Services\CheckoutService;
|
||||
use App\Domains\Commerce\Purchase\Services\DniDistanceService;
|
||||
use App\Domains\Core\Client\Models\Client;
|
||||
use Exception;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
@@ -75,9 +75,30 @@ class TelepagosWebhookService
|
||||
|
||||
$dni = substr($cuit, 2, -1);
|
||||
|
||||
$tenantCodes = $client->tenants()->pluck('codigo');
|
||||
$eligiblePurchases = Purchase::query()
|
||||
->whereIn('tenant_codigo', $tenantCodes)
|
||||
->where(function (Builder $purchases) use ($client): void {
|
||||
$purchases
|
||||
->whereHas('event', function (Builder $events) use ($client): void {
|
||||
$events
|
||||
->where('client_id', $client->id)
|
||||
->orWhere(function (Builder $fallback) use ($client): void {
|
||||
$fallback
|
||||
->whereNull('client_id')
|
||||
->whereHas(
|
||||
'tenant',
|
||||
fn (Builder $tenants): Builder => $tenants->where('client_id', $client->id),
|
||||
);
|
||||
});
|
||||
})
|
||||
->orWhere(function (Builder $legacy) use ($client): void {
|
||||
$legacy
|
||||
->whereNull('event_id')
|
||||
->whereHas(
|
||||
'tenant',
|
||||
fn (Builder $tenants): Builder => $tenants->where('client_id', $client->id),
|
||||
);
|
||||
});
|
||||
})
|
||||
->whereIn('status', [
|
||||
Purchase::STATUS_CREATED,
|
||||
Purchase::STATUS_PENDING_PAYMENT,
|
||||
@@ -170,7 +191,7 @@ class TelepagosWebhookService
|
||||
return;
|
||||
}
|
||||
|
||||
if (! $client->tenants()->where('codigo', $compra->tenant_codigo)->exists()) {
|
||||
if (! $this->purchaseBelongsToClient($compra, $client)) {
|
||||
Log::channel('telepagos')->warning('Telepagos webhook: Purchase does not belong to client.', [
|
||||
'client_code' => $client->code,
|
||||
'cashin_id' => $cashinId,
|
||||
@@ -244,6 +265,16 @@ class TelepagosWebhookService
|
||||
return number_format((float) $amount, 2, '.', '');
|
||||
}
|
||||
|
||||
private function purchaseBelongsToClient(Purchase $purchase, Client $client): bool
|
||||
{
|
||||
$purchase->loadMissing(['event.client', 'event.tenant.client', 'tenant.client']);
|
||||
|
||||
$effectiveClient = $purchase->event?->effectiveClient()
|
||||
?? $purchase->tenant?->client;
|
||||
|
||||
return $effectiveClient?->is($client) ?? false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Builder<Purchase> $eligiblePurchases
|
||||
* @return Collection<int, Purchase>
|
||||
|
||||
Reference in New Issue
Block a user