From 326f14c4fc06e44ca9ef926b8da89490a32bad0f Mon Sep 17 00:00:00 2001 From: sajad mahmoodifar Date: Fri, 18 Sep 2026 20:11:43 +0330 Subject: [PATCH] fix(zibal): use verification failure exceptions --- CHANGELOG.md | 2 ++ src/Drivers/Zibal/Zibal.php | 4 ++-- tests/Drivers/ZibalTest.php | 17 ++++++++++++++++- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 48ffabe..3d9cfc2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -67,6 +67,8 @@ Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) princip coverage badge. ### Fixed +- **Zibal:** verification HTTP errors and unsuccessful gateway results throw `InvalidPaymentException` instead of + `PurchaseFailedException`, so the documented verification error handler catches them. - `Payment::resetCallbackUrl()` puts the callbackUrl of the configuration back, the way its name and its docblock always promised. It used to set the callbackUrl to `null` instead, which left the driver without one, so the only way back to the configured callbackUrl was to select the driver again with `via()`. A driver that has no callbackUrl in diff --git a/src/Drivers/Zibal/Zibal.php b/src/Drivers/Zibal/Zibal.php index 12bfd46..9e94a5e 100644 --- a/src/Drivers/Zibal/Zibal.php +++ b/src/Drivers/Zibal/Zibal.php @@ -168,7 +168,7 @@ public function verify(): ReceiptInterface if ($response->getStatusCode() !== 200) { // connection error $message = $body['message'] ?? 'خطا در هنگام وریفای تراکنش رخ داده است.'; - throw new PurchaseFailedException($message, (int) $response->getStatusCode()); + throw new InvalidPaymentException($message, (int) $response->getStatusCode()); } if ($body['result'] == 201) { // transaction has been verified before @@ -177,7 +177,7 @@ public function verify(): ReceiptInterface if ($body['result'] != 100) { // gateway errors - throw new PurchaseFailedException($this->translateStatus($body['result']), $body['result']); + throw new InvalidPaymentException($this->translateStatus($body['result']), $body['result']); } return new Receipt('Zibal', $body['refNumber'])->detail($body); diff --git a/tests/Drivers/ZibalTest.php b/tests/Drivers/ZibalTest.php index c0b3264..ab6f87a 100644 --- a/tests/Drivers/ZibalTest.php +++ b/tests/Drivers/ZibalTest.php @@ -221,8 +221,23 @@ public function testVerifyFailsWhenTheGatewayReportsAnError(): void $driver = $this->driver(); $this->fakeHttp($driver, [$this->jsonResponse(['result' => 202])]); - $this->expectException(PurchaseFailedException::class); + $this->expectException(InvalidPaymentException::class); $this->expectExceptionMessage('سفارش پرداخت نشده یا ناموفق بوده است.'); + $this->expectExceptionCode(202); + + $driver->verify(); + } + + public function testVerifyFailsWhenTheGatewayIsNotReachable(): void + { + $this->fakeRequest(['success' => 1, 'trackId' => 1234567]); + + $driver = $this->driver(); + $this->fakeHttp($driver, [$this->jsonResponse(['message' => 'service unavailable'], 503)]); + + $this->expectException(InvalidPaymentException::class); + $this->expectExceptionMessage('service unavailable'); + $this->expectExceptionCode(503); $driver->verify(); }