| 1 |
<?php |
| 2 |
|
| 3 |
namespace SyncBasalam\Services; |
| 4 |
|
| 5 |
use SyncBasalam\Admin\Settings\SettingsConfig; |
| 6 |
use SyncBasalam\Admin\Settings; |
| 7 |
use SyncBasalam\Config\Endpoints; |
| 8 |
use SyncBasalam\Logger\Logger; |
| 9 |
|
| 10 |
defined('ABSPATH') || exit; |
| 11 |
|
| 12 |
class VendorInfoService |
| 13 |
{ |
| 14 |
private $apiService; |
| 15 |
private $basalamToken; |
| 16 |
private $basalamVendorId; |
| 17 |
|
| 18 |
public function __construct() |
| 19 |
{ |
| 20 |
$this->apiService = syncBasalamContainer()->get(ApiServiceManager::class); |
| 21 |
$this->basalamToken = Settings::getSettings(SettingsConfig::TOKEN); |
| 22 |
$this->basalamVendorId = Settings::getSettings(SettingsConfig::VENDOR_ID); |
| 23 |
} |
| 24 |
|
| 25 |
public function FetchVendorInfo() |
| 26 |
{ |
| 27 |
// Credentials can change during an OAuth callback in the same request. |
| 28 |
$this->basalamToken = Settings::getSettings(SettingsConfig::TOKEN); |
| 29 |
$this->basalamVendorId = Settings::getSettings(SettingsConfig::VENDOR_ID); |
| 30 |
|
| 31 |
if (!$this->basalamToken || !$this->basalamVendorId) { |
| 32 |
return null; |
| 33 |
} |
| 34 |
|
| 35 |
try { |
| 36 |
$apiUrl = sprintf(Endpoints::VENDOR_INFO, $this->basalamVendorId); |
| 37 |
$FetchVendorInfo = $this->apiService->get($apiUrl, ['Authorization' => 'Bearer ' . $this->basalamToken]); |
| 38 |
|
| 39 |
if (!is_array($FetchVendorInfo) || !isset($FetchVendorInfo['body'])) { |
| 40 |
return null; |
| 41 |
} |
| 42 |
|
| 43 |
$vendorInfo = json_decode($FetchVendorInfo['body'], true); |
| 44 |
if (!is_array($vendorInfo)) { |
| 45 |
return null; |
| 46 |
} |
| 47 |
|
| 48 |
return $vendorInfo; |
| 49 |
} catch (\Exception $e) { |
| 50 |
Logger::error('خطا در دریافت اطلاعات فروشنده: ' . $e->getMessage()); |
| 51 |
return null; |
| 52 |
} |
| 53 |
} |
| 54 |
} |
| 55 |
|