Files
sam-api/app/Http/Controllers/Api/V1/BarobillSmsController.php

132 lines
3.7 KiB
PHP
Raw Normal View History

<?php
namespace App\Http\Controllers\Api\V1;
use App\Helpers\ApiResponse;
use App\Http\Controllers\Controller;
use App\Models\Barobill\BarobillMember;
use App\Services\Barobill\BarobillSoapService;
use Illuminate\Http\Request;
class BarobillSmsController extends Controller
{
public function __construct(
private BarobillSoapService $soapService,
) {}
/**
* SMS 발송
*/
public function send(Request $request)
{
$data = $request->validate([
'from_number' => 'required|string',
'to_name' => 'required|string',
'to_number' => 'required|string',
'contents' => 'required|string',
'send_dt' => 'nullable|string',
'ref_key' => 'nullable|string',
]);
return ApiResponse::handle(function () use ($data) {
[$member, $corpNum] = $this->initSoap();
$result = $this->soapService->sendSMSMessage(
$corpNum,
$member->barobill_id,
$data['from_number'],
$data['to_name'],
$data['to_number'],
$data['contents'],
$data['send_dt'] ?? '',
$data['ref_key'] ?? ''
);
return $this->formatResult($result, 'send_key');
}, __('message.created'));
}
/**
* 발신번호 목록
*/
public function fromNumbers()
{
return ApiResponse::handle(function () {
[$member, $corpNum] = $this->initSoap();
$result = $this->soapService->getSMSFromNumbers($corpNum);
return $this->formatResult($result, 'from_numbers');
}, __('message.fetched'));
}
/**
* 발신번호 등록 여부 확인
*/
public function checkFromNumber(Request $request)
{
$data = $request->validate([
'from_number' => 'required|string',
]);
return ApiResponse::handle(function () use ($data) {
[$member, $corpNum] = $this->initSoap();
$result = $this->soapService->checkSMSFromNumber($corpNum, $data['from_number']);
return $this->formatResult($result, 'result');
}, __('message.fetched'));
}
/**
* SMS 전송 상태 조회
*/
public function sendState(string $sendKey)
{
return ApiResponse::handle(function () use ($sendKey) {
[$member, $corpNum] = $this->initSoap();
$result = $this->soapService->getSMSSendState($corpNum, $sendKey);
return $this->formatResult($result, 'state');
}, __('message.fetched'));
}
/**
* 바로빌 회원 조회 SOAP 초기화
*/
private function initSoap(): array
{
$member = $this->getMember();
if (! $member) {
throw new \Symfony\Component\HttpKernel\Exception\NotFoundHttpException(
__('error.barobill.member_not_found', [], 'ko') ?: '바로빌 회원사가 등록되어 있지 않습니다.'
);
}
$this->soapService->initForMember($member);
return [$member, $member->biz_no];
}
private function getMember(): ?BarobillMember
{
$tenantId = app('tenant_id');
return BarobillMember::withoutGlobalScopes()
->where('tenant_id', $tenantId)
->first();
}
private function formatResult(array $result, string $key): array
{
if (! ($result['success'] ?? false)) {
throw new \Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException(
$result['error'] ?? '바로빌 API 호출 실패'
);
}
return [$key => $result['data'] ?? null];
}
}