Skip to content

Commit

Permalink
Merge pull request #61 from farayaz/60-update-tejarat-bajet
Browse files Browse the repository at this point in the history
update tejarat bajet
  • Loading branch information
mehrdadx10 authored Jul 10, 2024
2 parents 1de9216 + 72a6f83 commit e94410d
Showing 1 changed file with 36 additions and 18 deletions.
54 changes: 36 additions & 18 deletions src/Gateways/TejaratBajet.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@
final class TejaratBajet extends GatewayAbstract
{
protected $statuses = [
'invalid_client' => 'invalid_client: خطای سرویس گیرنده',
'TrackerAlreadyUsed' => 'کد پیگیری تکراری',
];

protected $requirements = [
'client_id',
'client_secret',
'username',
'password',
'sandbox',
];

Expand All @@ -33,13 +34,13 @@ public function request(
'Authorization' => 'Bearer ' . $this->authenticate(),
];

$url = '/customers/' . $nationalId . '/balance';
$url = 'customers/' . $nationalId . '/balance';
$result = $this->_request('get', $url, [], $headers);
if ($result['result']['balance'] < $amount) {
throw new LarapayException('باجت: عدم موجودی کافی: ' . number_format($result['result']['balance']));
throw new LarapayException('باجت: عدم موجودی کافی. موجودی: ' . number_format($result['result']['balance']) . ' ریال');
}

$url = '/customers/' . $nationalId . '/purchases/authorization?trackId=' . $id;
$url = 'customers/' . $nationalId . '/purchases/authorization?trackId=' . $id;
$data = [
'amount' => $amount,
'description' => $id,
Expand Down Expand Up @@ -75,7 +76,7 @@ public function verify(
];

// purchases
$url = '/customers/' . $nationalId . '/purchases?trackId=' . $id;
$url = 'customers/' . $nationalId . '/purchases?trackId=' . $id;
$data = [
'otp' => $params['otp'],
'amount' => $amount,
Expand All @@ -84,7 +85,7 @@ public function verify(
$this->_request('post', $url, $data, $headers);

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

return [
Expand All @@ -98,31 +99,48 @@ public function verify(

public function authenticate()
{
return Cache::remember(__CLASS__ . ':token', 3500, function () {
$data = [
'client_id' => $this->config['client_id'],
'client_secret' => $this->config['client_secret'],
'grant_type' => 'client_credentials',
'scope' => '',
];
$result = $this->_request('post', 'auth/token', $data);

return $result['result']['accessToken'];
});
if (Cache::get(__METHOD__)) {
return Cache::get(__METHOD__);
}
$data = [
'client_id' => $this->config['client_id'],
'client_secret' => $this->config['client_secret'],
'grant_type' => 'password',
'username' => $this->config['username'],
'password' => $this->config['password'],
];
$result = $this->_request('post', 'token', $data);
Cache::put(__METHOD__, $result['access_token'], $result['expires_in'] - 10);

return $result['access_token'];
}

private function _request($method, $url, array $data = [], array $headers = [])
{
$fullUrl = 'https://fct-api.stts.ir/api/' . ($this->config['sandbox'] ? 'vNext/' : 'v1/') . $url;
$as = 'asForm';
$fullUrl = 'https://setplus.stts.ir/';
if ($url != 'token') {
$fullUrl .= 'facilitycustomer/api/v1/';
$as = 'asJson';
}
$fullUrl .= $url;

try {
return Http::timeout(10)
->$as()
->withHeaders($headers)
->$method($fullUrl, $data)
->throw()
->json();
} catch (RequestException $e) {
$message = $e->getMessage();
$result = $e->response->json();
if (isset($result['error'])) {
$message = $this->translateStatus($result['error']);
}
if (isset($result['error_description'])) {
$message = $this->translateStatus($result['error_description']);
}
if (isset($result['title'])) {
$message = $this->translateStatus($result['title']);
}
Expand Down

0 comments on commit e94410d

Please sign in to comment.