trangshopeeaffiliate/app/Services/Converters/ManualAffiliateConverter.php
2026-07-11 22:56:22 +07:00

52 lines
1.6 KiB
PHP

<?php
namespace App\Services\Converters;
use App\DTOs\AffiliateResult;
use App\Interfaces\AffiliateConverterInterface;
use App\Services\SettingService;
class ManualAffiliateConverter implements AffiliateConverterInterface
{
protected SettingService $settingService;
public function __construct(SettingService $settingService)
{
$this->settingService = $settingService;
}
public function convert(string $url, ?string $code = null): AffiliateResult
{
$startTime = microtime(true);
$affiliateId = $this->settingService->get('affiliate_id');
if (!$affiliateId) {
return new AffiliateResult(
isSuccess: false,
originalUrl: $url,
errorMessage: 'Vui lòng thiết lập Affiliate ID trong phần Settings để sử dụng chuyển đổi thủ công.',
executionTime: microtime(true) - $startTime,
converterType: 'manual'
);
}
// URL encode the original URL
$encodedLink = urlencode($url);
// Construct the standard manual Shopee redirect URL
$affiliateUrl = "https://s.shopee.vn/an_redir?origin_link={$encodedLink}&affiliate_id={$affiliateId}";
if ($code) {
$affiliateUrl .= "&sub_id={$code}";
}
return new AffiliateResult(
isSuccess: true,
originalUrl: $url,
affiliateUrl: $affiliateUrl,
executionTime: microtime(true) - $startTime,
sessionUsed: 'manual_id_' . $affiliateId,
converterType: 'manual'
);
}
}