| 1 |
<?php |
| 2 |
|
| 3 |
namespace SyncBasalam\Admin\Settings; |
| 4 |
|
| 5 |
use SyncBasalam\Config\Endpoints; |
| 6 |
use SyncBasalam\Services\ApiServiceManager; |
| 7 |
|
| 8 |
defined('ABSPATH') || exit; |
| 9 |
|
| 10 |
class OAuthManager |
| 11 |
{ |
| 12 |
public function getOauthData() |
| 13 |
{ |
| 14 |
$oauthDataUrl = apply_filters('sync_basalam_oauth_data_url', Endpoints::HAMSALAM_OAUTH_DATA); |
| 15 |
$defaultClientId = apply_filters('sync_basalam_oauth_default_client_id', 779); |
| 16 |
$defaultRedirectUri = apply_filters('sync_basalam_oauth_default_redirect_uri', Endpoints::HAMSALAM_OAUTH_TOKEN); |
| 17 |
|
| 18 |
try { |
| 19 |
$apiservice = syncBasalamContainer()->get(ApiServiceManager::class); |
| 20 |
$request = $apiservice->get($oauthDataUrl); |
| 21 |
$clientId = $request['body']['client_id'] ?? $defaultClientId; |
| 22 |
$redirectUri = $request['body']['redirect_uri'] ?? $defaultRedirectUri; |
| 23 |
} catch (\Throwable $th) { |
| 24 |
$clientId = $defaultClientId; |
| 25 |
$redirectUri = $defaultRedirectUri; |
| 26 |
} |
| 27 |
|
| 28 |
return [ |
| 29 |
'client_id' => $clientId, |
| 30 |
'redirect_uri' => $redirectUri, |
| 31 |
]; |
| 32 |
} |
| 33 |
|
| 34 |
public static function saveOauthData() |
| 35 |
{ |
| 36 |
$isVendor = isset($_GET['is_vendor']) ? sanitize_text_field(wp_unslash($_GET['is_vendor'])) : true; |
| 37 |
$vendorId = isset($_GET['vendor_id']) ? sanitize_text_field(intval($_GET['vendor_id'])) : null; |
| 38 |
$hamsalamToken = isset($_GET['hamsalam_token']) ? sanitize_text_field(wp_unslash($_GET['hamsalam_token'])) : null; |
| 39 |
$hamsalamBusinessId = isset($_GET['hamsalam_business_id']) ? sanitize_text_field(wp_unslash($_GET['hamsalam_business_id'])) : null; |
| 40 |
$accessToken = isset($_GET['access_token']) ? sanitize_text_field(wp_unslash($_GET['access_token'])) : null; |
| 41 |
$refreshToken = isset($_GET['refresh_token']) ? sanitize_text_field(wp_unslash($_GET['refresh_token'])) : null; |
| 42 |
$expiresIn = isset($_GET['expires_in']) ? sanitize_text_field(intval($_GET['expires_in'])) : null; |
| 43 |
|
| 44 |
// Allow pro version to handle custom fields |
| 45 |
$extraData = apply_filters('sync_basalam_oauth_save_extra_data', []); |
| 46 |
|
| 47 |
if ($isVendor == 'false') { |
| 48 |
$data = [SettingsConfig::IS_VENDOR => false]; |
| 49 |
$data = apply_filters('sync_basalam_oauth_non_vendor_data', $data, $vendorId, $accessToken, $refreshToken, $extraData); |
| 50 |
SettingsManager::updateSettings($data); |
| 51 |
return true; |
| 52 |
} |
| 53 |
|
| 54 |
$data = [ |
| 55 |
SettingsConfig::VENDOR_ID => $vendorId, |
| 56 |
SettingsConfig::IS_VENDOR => $isVendor, |
| 57 |
SettingsConfig::TOKEN => $accessToken, |
| 58 |
SettingsConfig::REFRESH_TOKEN => $refreshToken, |
| 59 |
SettingsConfig::HAMSALAM_TOKEN => $hamsalamToken, |
| 60 |
SettingsConfig::HAMSALAM_BUSINESS_ID => $hamsalamBusinessId, |
| 61 |
SettingsConfig::EXPIRE_TOKEN_TIME => $expiresIn, |
| 62 |
]; |
| 63 |
|
| 64 |
$data = array_merge($data, $extraData); |
| 65 |
|
| 66 |
SettingsManager::updateSettings($data); |
| 67 |
|
| 68 |
return true; |
| 69 |
} |
| 70 |
|
| 71 |
public function getOAuthUrls() |
| 72 |
{ |
| 73 |
$oauthData = $this->getOauthData(); |
| 74 |
$siteUrl = get_site_url(); |
| 75 |
|
| 76 |
$scopes = apply_filters('sync_basalam_oauth_scopes', "vendor.product.write vendor.parcel.write customer.profile.read vendor.profile.read vendor.parcel.read vendor.profile.write customer.chat.read customer.chat.write"); |
| 77 |
|
| 78 |
return [ |
| 79 |
'redirect_uri' => $oauthData['redirect_uri'], |
| 80 |
'url_req_token' => Endpoints::oauthLoginUrl( |
| 81 |
$oauthData['client_id'], |
| 82 |
$scopes, |
| 83 |
$oauthData['redirect_uri'], |
| 84 |
$siteUrl |
| 85 |
), |
| 86 |
]; |
| 87 |
} |
| 88 |
} |
| 89 |
|