| 1 |
<?php |
| 2 |
|
| 3 |
namespace SyncBasalam\Services\Products; |
| 4 |
|
| 5 |
use SyncBasalam\Config\Endpoints; |
| 6 |
use SyncBasalam\Services\ApiServiceManager; |
| 7 |
|
| 8 |
defined('ABSPATH') || exit; |
| 9 |
class FetchCommission |
| 10 |
{ |
| 11 |
public static function fetchCategoryCommission($categoryIds) |
| 12 |
{ |
| 13 |
$apiservice = syncBasalamContainer()->get(ApiServiceManager::class); |
| 14 |
$queryParams = []; |
| 15 |
|
| 16 |
if (isset($categoryIds[0]) && is_numeric($categoryIds[0])) { |
| 17 |
$queryParams[] = "product.category.level1=" . intval($categoryIds[0]); |
| 18 |
} |
| 19 |
if (isset($categoryIds[1]) && is_numeric($categoryIds[1])) { |
| 20 |
$queryParams[] = "product.category.level2=" . intval($categoryIds[1]); |
| 21 |
} |
| 22 |
if (isset($categoryIds[2]) && is_numeric($categoryIds[2])) { |
| 23 |
$queryParams[] = "product.category.level3=" . intval($categoryIds[2]); |
| 24 |
} |
| 25 |
|
| 26 |
if (empty($queryParams)) return false; |
| 27 |
|
| 28 |
$url = Endpoints::COMMISSION . '?' . implode("&", $queryParams); |
| 29 |
|
| 30 |
try { |
| 31 |
$result = $apiservice->get($url); |
| 32 |
} catch (\Exception $e) { |
| 33 |
return 0; |
| 34 |
} |
| 35 |
|
| 36 |
$decodedBody = json_decode($result['body'], true); |
| 37 |
|
| 38 |
$commissionPercent = $decodedBody['commission_data']['commission_percent']; |
| 39 |
|
| 40 |
if ($commissionPercent) return $commissionPercent; |
| 41 |
return 0; |
| 42 |
} |
| 43 |
} |
| 44 |
|