Skip to content

Commit

Permalink
Merge pull request #29 from farayaz/28-tejaratbajet-error-handling
Browse files Browse the repository at this point in the history
improve TejaratBajet error handling
  • Loading branch information
mehrdadx10 authored May 14, 2024
2 parents 3bab6be + ccdef7a commit 9396805
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 35 deletions.
3 changes: 0 additions & 3 deletions src/Gateways/Azkivam.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@ final class Azkivam extends GatewayAbstract
'51' => 'ازکی‌وام: Request data is not valid',
'59' => 'ازکی‌وام: Transaction not reversible',
'60' => 'ازکی‌وام: Transaction must be in verified state',

'token-mismatch' => 'عدم تطبیق توکن بازگشتی',
'connection-exception' => 'خطا ارتباطی با سرویس دهنده',
];

protected $requirements = ['merchant_id', 'api_key'];
Expand Down
5 changes: 4 additions & 1 deletion src/Gateways/GatewayAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,11 @@ protected function requirements()
}
}

protected function translateStatus(int|string $code = null)
protected function translateStatus(int|string|null $code = null)
{
$this->statuses['token-mismatch'] = 'عدم تطبیق توکن بازگشتی';
$this->statuses['connection-exception'] = 'خطا ارتباطی با سرویس دهنده';

return $this->statuses[$code] ?? $code;
}

Expand Down
52 changes: 21 additions & 31 deletions src/Gateways/TejaratBajet.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@
namespace Farayaz\Larapay\Gateways;

use Farayaz\Larapay\Exceptions\LarapayException;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\BadResponseException;
use Illuminate\Http\Client\ConnectionException;
use Illuminate\Http\Client\RequestException;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\View;

final class TejaratBajet extends GatewayAbstract
{
protected $statuses = [
'invalid_client' => 'invalid_client: خطای سرویس گیرنده',
'TrackerAlreadyUsed' => 'کد پیگیری تکراری',
];

protected $requirements = [
Expand All @@ -31,7 +34,7 @@ public function request(
];

$url = $this->_url('/customers/' . $nationalId . '/balance');
$result = $this->_request('GET', $url, [], $headers);
$result = $this->_request('get', $url, [], $headers);
if ($result['result']['balance'] < $amount) {
throw new LarapayException('عدم موجودی کافی: ' . number_format($result['result']['balance']));
}
Expand All @@ -41,7 +44,7 @@ public function request(
'amount' => $amount,
'description' => $id,
];
$this->_request('POST', $url, $data, $headers);
$this->_request('post', $url, $data, $headers);

return [
'token' => $id,
Expand Down Expand Up @@ -78,11 +81,11 @@ public function verify(
'amount' => $amount,
'description' => $id,
];
$this->_request('POST', $url, $data, $headers);
$this->_request('post', $url, $data, $headers);

// advice
$url = $this->_url('/customers/' . $nationalId . '/purchases/advice?trackId=' . $id);
$result = $this->_request('POST', $url, [], $headers);
$result = $this->_request('post', $url, [], $headers);

return [
'result' => $result['message'],
Expand All @@ -105,7 +108,7 @@ public function authenticate()
'scope' => '',
];

$result = $this->_request('POST', $url, $data);
$result = $this->_request('post', $url, $data);

return $result['result']['accessToken'];
});
Expand All @@ -120,32 +123,19 @@ private function _url($url)

private function _request($method, $url, array $data = [], array $headers = [])
{
$client = new Client;

try {
$response = $client->request(
$method,
$url,
[
'headers' => [
'Content-Type' => 'application/json',
'Accept' => 'application/json',
...$headers,
],
'json' => $data,
'timeout' => 10,
]
);

return json_decode($response->getBody(), true);
} catch (BadResponseException $e) {
$message = $e->getMessage();
if ($e->hasResponse()) {
$message = $e->getResponse()->getStatusCode() . ': ' . $e->getMessage();
$response = json_decode($e->getResponse()->getBody(), true);
$message = $response['detail'] ?? $response['title'] ?? $message;
}
return Http::timeout(10)
->withHeaders($headers)
->$method($url, $data)
->throw()
->json();
} catch (RequestException $e) {
$result = $e->response->json();
$message = $result['detail'] ?? $this->translateStatus($result['title']) ?? $e->getMessage();
$message = 'باجت: ' . $message;
throw new LarapayException($message);
} catch (ConnectionException $e) {
throw new LarapayException($this->translateStatus('connection-exception'));
}
}
}

0 comments on commit 9396805

Please sign in to comment.