| 1 |
<?php |
| 2 |
|
| 3 |
namespace SyncBasalam\Services\Hamsalam; |
| 4 |
|
| 5 |
use SyncBasalam\Admin\Settings\SettingsConfig; |
| 6 |
use SyncBasalam\Admin\Settings\SettingsManager; |
| 7 |
use SyncBasalam\Config\Endpoints; |
| 8 |
use SyncBasalam\Services\ApiServiceManager; |
| 9 |
|
| 10 |
defined('ABSPATH') || exit; |
| 11 |
|
| 12 |
class FetchHamsalamBusinessId |
| 13 |
{ |
| 14 |
private $url; |
| 15 |
private $settings; |
| 16 |
private $hamsalmToken; |
| 17 |
|
| 18 |
public function __construct() |
| 19 |
{ |
| 20 |
$this->settings = syncBasalamSettings()->getSettings(); |
| 21 |
$this->url = Endpoints::HAMSALAM_BUSINESSES; |
| 22 |
$this->hamsalmToken = SettingsManager::getSettings(SettingsConfig::HAMSALAM_TOKEN); |
| 23 |
} |
| 24 |
|
| 25 |
public function fetch() |
| 26 |
{ |
| 27 |
try { |
| 28 |
$apiService = syncBasalamContainer()->get(ApiServiceManager::class); |
| 29 |
|
| 30 |
$header = ['Authorization' => 'Bearer ' . $this->hamsalmToken]; |
| 31 |
|
| 32 |
$response = $apiService->get($this->url, $header); |
| 33 |
|
| 34 |
if (!$response || !isset($response['body'])) return ('خطا در دریافت اطلاعات از ه� |
| 35 |
سلا� |
| 36 |
'); |
| 37 |
|
| 38 |
$businesses = json_decode($response['body'], true); |
| 39 |
|
| 40 |
$domain = get_site_url(); |
| 41 |
$vendorId = $this->settings[SettingsConfig::VENDOR_ID]; |
| 42 |
$businessId = null; |
| 43 |
|
| 44 |
if (!isset($businesses['data']) || !is_array($businesses['data'])) return null; |
| 45 |
|
| 46 |
foreach ($businesses['data'] as $business) { |
| 47 |
if ($business['platform'] == 'wordpress' && $domain == $business['domain'] && $vendorId == $business['vendor_id']) { |
| 48 |
$businessId = $business['id']; |
| 49 |
break; |
| 50 |
} |
| 51 |
} |
| 52 |
if ($businessId) { |
| 53 |
$data = [SettingsConfig::HAMSALAM_BUSINESS_ID => $businessId]; |
| 54 |
|
| 55 |
SettingsManager::updateSettings($data); |
| 56 |
return $businessId; |
| 57 |
} |
| 58 |
|
| 59 |
return null; |
| 60 |
} catch (\Exception) { |
| 61 |
return null; |
| 62 |
} |
| 63 |
} |
| 64 |
} |
| 65 |
|