|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Kaninstein\MultiAcquirerCheckout\Application\Services; |
| 4 | + |
| 5 | +use Illuminate\Contracts\Events\Dispatcher; |
| 6 | +use Kaninstein\MultiAcquirerCheckout\Infrastructure\Repositories\Contracts\PaymentRepositoryInterface; |
| 7 | + |
| 8 | +final readonly class PagarmeWebhookService |
| 9 | +{ |
| 10 | + public function __construct( |
| 11 | + private PaymentRepositoryInterface $payments, |
| 12 | + private Dispatcher $events, |
| 13 | + ) {} |
| 14 | + |
| 15 | + /** |
| 16 | + * @param array<string, mixed> $payload |
| 17 | + * @return array{status:string} |
| 18 | + */ |
| 19 | + public function handle(array $payload): array |
| 20 | + { |
| 21 | + $eventType = is_string($payload['type'] ?? null) ? (string) $payload['type'] : null; |
| 22 | + /** @var array<string,mixed> $data */ |
| 23 | + $data = is_array($payload['data'] ?? null) ? (array) $payload['data'] : $payload; |
| 24 | + |
| 25 | + if (! $eventType) { |
| 26 | + return ['status' => 'ignored']; |
| 27 | + } |
| 28 | + |
| 29 | + $gatewayTransactionId = $this->extractGatewayTransactionId($eventType, $data); |
| 30 | + if ($gatewayTransactionId === null) { |
| 31 | + return ['status' => 'ignored']; |
| 32 | + } |
| 33 | + |
| 34 | + $payment = $this->payments->findByGatewayTransactionId($gatewayTransactionId); |
| 35 | + if ($payment === null) { |
| 36 | + return ['status' => 'ignored']; |
| 37 | + } |
| 38 | + |
| 39 | + if ($payment->status->isFinal()) { |
| 40 | + return ['status' => 'ignored']; |
| 41 | + } |
| 42 | + |
| 43 | + $webhookMeta = [ |
| 44 | + 'last_event' => $eventType, |
| 45 | + 'received_at' => now()->toISOString(), |
| 46 | + ]; |
| 47 | + |
| 48 | + $payment->metadata = [ |
| 49 | + ...$payment->metadata, |
| 50 | + 'webhook' => $webhookMeta, |
| 51 | + ]; |
| 52 | + |
| 53 | + if ($eventType === 'charge.paid' || $eventType === 'charge.payment_succeeded' || $eventType === 'order.paid') { |
| 54 | + $payment->markPaid(); |
| 55 | + } elseif ($eventType === 'charge.pending' || $eventType === 'charge.waiting_payment' || $eventType === 'order.pending') { |
| 56 | + // Keep pending; only record metadata. |
| 57 | + } elseif ($eventType === 'charge.failed' || $eventType === 'charge.payment_failed') { |
| 58 | + $payment->fail($this->extractFailureReason($data) ?? 'Payment failed'); |
| 59 | + } elseif ($eventType === 'charge.refunded') { |
| 60 | + $payment->refund(); |
| 61 | + } elseif ($eventType === 'charge.canceled' || $eventType === 'order.canceled') { |
| 62 | + $payment->cancel(); |
| 63 | + } else { |
| 64 | + return ['status' => 'ignored']; |
| 65 | + } |
| 66 | + |
| 67 | + $this->payments->save($payment); |
| 68 | + |
| 69 | + if (config('multi-acquirer.events.dispatch_domain_events', true)) { |
| 70 | + foreach ($payment->releaseEvents() as $event) { |
| 71 | + $this->events->dispatch($event); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + return ['status' => 'success']; |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * @param array<string,mixed> $data |
| 80 | + */ |
| 81 | + private function extractFailureReason(array $data): ?string |
| 82 | + { |
| 83 | + $last = is_array($data['last_transaction'] ?? null) ? (array) $data['last_transaction'] : null; |
| 84 | + $gatewayResponse = $last && is_array($last['gateway_response'] ?? null) ? (array) $last['gateway_response'] : null; |
| 85 | + $errors = $gatewayResponse && is_array($gatewayResponse['errors'] ?? null) ? (array) $gatewayResponse['errors'] : []; |
| 86 | + $first = is_array($errors[0] ?? null) ? (array) $errors[0] : null; |
| 87 | + |
| 88 | + $message = $first['message'] ?? null; |
| 89 | + |
| 90 | + return is_string($message) && $message !== '' ? $message : null; |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * @param array<string,mixed> $data |
| 95 | + */ |
| 96 | + private function extractGatewayTransactionId(string $eventType, array $data): ?string |
| 97 | + { |
| 98 | + if (str_starts_with($eventType, 'charge.')) { |
| 99 | + $id = $data['id'] ?? null; |
| 100 | + return is_string($id) && $id !== '' ? $id : null; |
| 101 | + } |
| 102 | + |
| 103 | + if (str_starts_with($eventType, 'order.')) { |
| 104 | + $charges = is_array($data['charges'] ?? null) ? (array) $data['charges'] : []; |
| 105 | + $firstCharge = is_array($charges[0] ?? null) ? (array) $charges[0] : []; |
| 106 | + $id = $firstCharge['id'] ?? null; |
| 107 | + |
| 108 | + return is_string($id) && $id !== '' ? $id : null; |
| 109 | + } |
| 110 | + |
| 111 | + return null; |
| 112 | + } |
| 113 | +} |
| 114 | + |
0 commit comments