giftcon_dev/app/Providers/Danal/Clients/DanalCpcgiClient.php

68 lines
2.2 KiB
PHP

<?php
namespace App\Providers\Danal\Clients;
use App\Support\Danal\DanalAes256CbcHex;
use App\Support\Danal\Nvp;
use App\Support\Danal\EucKr;
use Illuminate\Support\Facades\Http;
final class DanalCpcgiClient
{
public function __construct(
private readonly DanalAes256CbcHex $aes,
private readonly Nvp $nvp,
private readonly EucKr $euc,
) {}
/**
* CPCGI POST 규칙:
* CPID=...&DATA=urlencode(base64(AES256CBC( key=urlencode(value)&... )))
* 응답은 NVP, DATA가 있으면 다시 decrypt -> NVP
*/
public function call(string $url, string $outerCpid, array $reqData, string $hexKey, string $hexIv): array
{
$sendData = [];
foreach ($reqData as $k => $v) {
$sendData[$k] = is_string($v) ? $this->euc->toEuc($v) : $v;
}
$plain = $this->nvp->build($sendData);
$enc = $this->aes->encrypt($plain, $hexKey, $hexIv);
$payload = 'CPID=' . $outerCpid . '&DATA=' . urlencode($enc);
$httpOpt = config('danal.http');
$res = Http::withOptions([
'verify' => (bool)($httpOpt['verify_ssl'] ?? true),
'connect_timeout' => (float)($httpOpt['connect_timeout'] ?? 5),
])
->withHeaders([
'Content-Type' => 'application/x-www-form-urlencoded; charset=euc-kr',
])
->timeout((int)($httpOpt['timeout'] ?? 30))
->withBody($payload, 'application/x-www-form-urlencoded; charset=euc-kr')
->post($url);
$body = (string)$res->body();
$map = $this->nvp->parse($body);
// 응답이 DATA를 포함하면 decrypt 후 재파싱
if (isset($map['DATA']) && is_string($map['DATA']) && $map['DATA'] !== '') {
$dec = $this->aes->decrypt($map['DATA'], $hexKey, $hexIv);
$map = $this->nvp->parse($dec);
}
// 메시지는 EUC-KR일 수 있으니 UTF-8 변환
return $this->euc->mapToUtf8($map);
}
public function decryptReturnParams(string $returnParams, string $hexKey, string $hexIv): array
{
$dec = $this->aes->decrypt($returnParams, $hexKey, $hexIv);
$map = $this->nvp->parse($dec);
return $this->euc->mapToUtf8($map);
}
}