Skip to content

Commit

Permalink
remove ApiClient
Browse files Browse the repository at this point in the history
  • Loading branch information
hbhossein committed Oct 9, 2023
1 parent 76d7f81 commit 46f7b35
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 206 deletions.
14 changes: 13 additions & 1 deletion src/Facades/Moadian.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@

namespace Jooyeshgar\Moadian\Facades;

/**
* @method static string getNonce(int $validity = 30)
* @method static Jooyeshgar\Moadian\Http\Response getServerInfo()
* @method static Jooyeshgar\Moadian\Http\Response getFiscalInfo()
* @method static Jooyeshgar\Moadian\Http\Response inquiryByUid(string $uid, string $start = '', string $end = '')
* @method static Jooyeshgar\Moadian\Http\Response inquiryByReferenceNumbers(string $referenceId, string $start = '', string $end = '')
* @method static Jooyeshgar\Moadian\Http\Response getEconomicCodeInformation(string $taxID)
* @method static Jooyeshgar\Moadian\Http\Response sendInvoice(Invoice $invoice)
*
* @see \Jooyeshgar\Moadian\Moadian
*/

use Illuminate\Support\Facades\Facade;

class Moadian extends Facade
Expand All @@ -10,4 +22,4 @@ protected static function getFacadeAccessor()
{
return 'Jooyeshgar\Moadian\Moadian';
}
}
}
125 changes: 98 additions & 27 deletions src/Moadian.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,64 +3,135 @@

use GuzzleHttp\Client;
use Jooyeshgar\Moadian\Exceptions\MoadianException;
use Jooyeshgar\Moadian\Services\ApiClient;
use Ramsey\Uuid\Uuid;
use Jooyeshgar\Moadian\Http\EconomicCodeInformation;
use Jooyeshgar\Moadian\Http\FiscalInfo;
use Jooyeshgar\Moadian\Http\GetNonce;
use Jooyeshgar\Moadian\Http\InquiryByReferenceNumber;
use Jooyeshgar\Moadian\Http\InquiryByUid;
use Jooyeshgar\Moadian\Http\Request;
use Jooyeshgar\Moadian\Http\Response;
use Jooyeshgar\Moadian\Http\SendInvoice;
use Jooyeshgar\Moadian\Http\ServerInfo;
use Jooyeshgar\Moadian\Services\EncryptionService;
use Jooyeshgar\Moadian\Services\SignatureService;

class Moadian
{
protected $username;
private Client $client;
private SignatureService $signer;
private EncryptionService $encryptor;
private Response $response;

protected $privateKey;

protected ApiClient $client;
public function __construct($privateKey, $certificate, $baseUri ='https://tp.tax.gov.ir/requestsmanager/api/v2/')
{
$this->client = new Client([
'base_uri' => $baseUri,
'headers' => ['Content-Type' => 'application/json'],
]);
$this->signer = new SignatureService($privateKey, $certificate);
$this->encryptor = new EncryptionService();
$this->response = new Response();
}

public function __construct($username, $privateKey, $baseUri ='https://tp.tax.gov.ir/')
/**
* Sends a request to the API server.
*
* @param Request $request The request to send.
* @return mixed The response from API server.
*/
public function sendRequest(Request $request)
{
$this->username = $username;
$this->privateKey = $privateKey;
$this->client = new ApiClient($username, $privateKey, $baseUri);
$request->prepare($this->signer, $this->encryptor);

$httpResp = $this->client->request($request->method, $request->path, [
'body' => json_encode($request->getBody()),
'headers' => $request->getHeaders(),
'query' => $request->getParams()
]);

return $this->response->setResponse($httpResp);
}

public function getToken()
public function getNonce(int $validity = 30)
{
return $this->client->getToken();
$request = new GetNonce($validity);

$response = $this->sendRequest($request);

if($response->isSuccessful()){
$result = $response->getBody();
return $result['nonce'];
}

throw new MoadianException('Unable to retrieve Token');
}

public function getServerInfo()
{
$response = $this->client->getServerInfo();

return $response;
$request = new ServerInfo();
return $this->sendRequest($request);
}

public function getFiscalInfo()
{
$response = $this->client->getFiscalInfo();

return $response;
$request = new FiscalInfo();
return $this->sendRequest($request);
}

public function inquiryByUid(array $uids)
/**
* Inquiry invoice with reference uuid.
*
* @param string $uid
* @param string $start Optional. start time e.g 2023-05-14T00:00:00.000000000+03:30
* @param string $end Optional. end time e.g 2023-05-14T23:59:59.123456789+03:30
*/
public function inquiryByUid(string $uid, string $start = '', string $end = '')
{
$response = $this->client->inquiryByUid($uids);
return $response;
$request = new InquiryByUid($uid, $start, $end);
return $this->sendRequest($request);
}

public function inquiryByReferenceNumbers(array $refNums)
/**
* Inquiry invoice with reference ID.
*
* @param string $referenceId
* @param string $start Optional. start time e.g 2023-05-14T00:00:00.000000000+03:30
* @param string $end Optional. end time e.g 2023-05-14T23:59:59.123456789+03:30
*/
public function inquiryByReferenceNumbers(string $referenceId, string $start = '', string $end = '')
{
$response = $this->client->inquiryByReferenceNumbers($refNums);
return $response;
$request = new InquiryByReferenceNumber($referenceId, $start, $end);
return $this->sendRequest($request);
}

public function getEconomicCodeInformation(string $taxID)
{
if (strlen($taxID) < 9 || strlen($taxID) >= 12)
throw new MoadianException('$taxID must be between 10 and 11 digits');

return $this->client->getEconomicCodeInformation($taxID);
$request = new EconomicCodeInformation($taxID);
return $this->sendRequest($request);
}

public function sendInvoice(Invoice $invoice)
{
$request = new SendInvoice($invoice);
$this->requirePublicKey();
return $this->sendRequest($request);
}

public function sendInvoice(Invoice $moadianInvoice){
return $this->client->sendInvoice($moadianInvoice);
private function requirePublicKey()
{
if (empty($this->encryptor->publicKey)) {
$serverInfo = $this->getServerInfo();

if ($serverInfo->isSuccessful()) {
$info = $serverInfo->getBody();
$this->encryptor->KeyId = $info['publicKeys'][0]['id'];

$pem = chunk_split($info['publicKeys'][0]['key'], 64, "\n");
$this->encryptor->publicKey = "-----BEGIN PUBLIC KEY-----\n" . $pem . "-----END PUBLIC KEY-----\n";
}
}
}
}
178 changes: 0 additions & 178 deletions src/Services/ApiClient.php

This file was deleted.

0 comments on commit 46f7b35

Please sign in to comment.