공개 (답변 중복 등록)/(문의 삭제 시 답변 잔존) 수정과 동형 결함 전수 해소.
- 문의 답변 단일성: 1차(피벗 is_answered) + 2차(게시판 리스너 duplicate 마커) 이중 방어,
경합 경로에서도 사유(reply_already_exists 422)가 보존되도록 훅 반환 계약 확장
- 질문·답변·피벗 생명주기 대칭: 삭제/복원/영구삭제 훅 연동 + 고아 데이터 백필 업그레이드 스텝
- 게시판별 답글 삭제 정책(cascade/block) 신설, 정렬 화이트리스트·per_page 게이트
(전량 조회 소비처 200/999 를 수용하는 상한 999 — 조였다가 관리자 화면 2곳 422 회귀 즉시 정정)
- 결제/본인인증 플러그인 검증 계약: FormRequest 15종 승격 + 컨트롤러 catch 정합.
거래 조회 tid 상한은 저장 컬럼(varchar 100) 기준 — 해외 CBT TID(40자) 수용
- onError 토스트 고정 문구 전수 해소(67건/50파일/15확장) — 서버 실패 사유를 그대로 표면화,
change-password 오변수(response→error) 교정
- 코어 플러그인 설정 검증 안내의 항목 표시명이 스키마 라벨(ko/en) 폴백에 덮여 요청 로케일
(ja) 라벨을 잃던 회귀 정정 — 번역 등록이 있으면 그것을 우선 (7.0.7 미출시 기능 자체 결함)
- 사전 결함 정리(Icon 등가표 27·darkmode 2·validated 통일), API 문서·CHANGELOG·ja 팩 동기
- develop(60652fdc0) 리베이스: getTable 정비와 병합 — 양측 의도 보존 (충돌 18파일)
168 lines
6.3 KiB
PHP
168 lines
6.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Plugins\Sirsoft\PayNicepayments\Controllers;
|
|
|
|
use App\Helpers\ResponseHelper;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\RateLimiter;
|
|
use Modules\Sirsoft\Ecommerce\Enums\PaymentStatusEnum;
|
|
use Modules\Sirsoft\Ecommerce\Models\Order;
|
|
use Modules\Sirsoft\Ecommerce\Services\OrderProcessingService;
|
|
use Plugins\Sirsoft\PayNicepayments\Concerns\RecordsPaymentWindowClosure;
|
|
use Plugins\Sirsoft\PayNicepayments\Http\Requests\PaymentCloseReportRequest;
|
|
|
|
class PaymentCloseReportController
|
|
{
|
|
use RecordsPaymentWindowClosure;
|
|
|
|
private const FAILURE_CODE = 'USER_CANCEL';
|
|
|
|
private const FAILURE_MESSAGE = '사용자가 나이스페이먼츠 결제창을 닫았습니다.';
|
|
|
|
public function __construct(
|
|
private readonly OrderProcessingService $orderService,
|
|
) {}
|
|
|
|
/**
|
|
* PC 결제창 닫힘 보고를 검증하고 결제 실패/취소 이력을 기록합니다.
|
|
*
|
|
* @param PaymentCloseReportRequest $request 결제창 닫힘 보고 요청
|
|
* @return JsonResponse 닫힘 보고 처리 결과
|
|
*/
|
|
public function store(PaymentCloseReportRequest $request): JsonResponse
|
|
{
|
|
$validated = $request->validated();
|
|
$oid = $validated['oid'];
|
|
$price = (int) $validated['price'];
|
|
|
|
$rateLimitKey = $this->rateLimitKey($request, $oid);
|
|
if (RateLimiter::tooManyAttempts($rateLimitKey, 20)) {
|
|
return ResponseHelper::error('common.failed', 429, [
|
|
'message' => ['Too many NicePayments payment close reports. Please try again later.'],
|
|
]);
|
|
}
|
|
RateLimiter::hit($rateLimitKey, 60);
|
|
|
|
$order = $this->orderService->findByOrderNumber($oid);
|
|
if (! $order) {
|
|
return ResponseHelper::error('common.failed', 404, [
|
|
'message' => ['Order not found.'],
|
|
]);
|
|
}
|
|
|
|
if (! $this->requestMatchesOrderBuyer($request, $order)) {
|
|
return ResponseHelper::error('common.failed', 403, [
|
|
'message' => ['Order buyer verification failed.'],
|
|
]);
|
|
}
|
|
|
|
$expectedPrice = $this->resolveExpectedPaymentPriceOrNull($order, 'close_report', [
|
|
'oid' => $oid,
|
|
'received_price' => $price,
|
|
'ip' => $request->ip(),
|
|
]);
|
|
if ($expectedPrice === null) {
|
|
return ResponseHelper::error('common.failed', 422, [
|
|
'message' => ['Payment currency is not chargeable.'],
|
|
]);
|
|
}
|
|
|
|
if ($price !== $expectedPrice) {
|
|
return ResponseHelper::error('common.failed', 422, [
|
|
'message' => ['Payment amount does not match the order amount.'],
|
|
]);
|
|
}
|
|
|
|
$closeReason = trim((string) ($validated['reason'] ?? ''));
|
|
$result = $this->recordCloseReportWithLock(
|
|
$oid,
|
|
$price,
|
|
$closeReason !== '' ? $closeReason : self::FAILURE_MESSAGE,
|
|
);
|
|
|
|
if (($result['status'] ?? null) === 'amount_mismatch') {
|
|
return ResponseHelper::error('common.failed', 422, [
|
|
'message' => ['Payment amount does not match the order amount.'],
|
|
]);
|
|
}
|
|
|
|
if (($result['status'] ?? null) === 'invalid_payment_currency') {
|
|
return ResponseHelper::error('common.failed', 422, [
|
|
'message' => ['Payment currency is not chargeable.'],
|
|
]);
|
|
}
|
|
|
|
if (($result['status'] ?? null) === 'ignored') {
|
|
return ResponseHelper::success('common.success', [
|
|
'status' => 'ignored',
|
|
'reason' => $result['reason'] ?? 'order_not_payable',
|
|
]);
|
|
}
|
|
|
|
return ResponseHelper::success('common.success', [
|
|
'status' => 'recorded',
|
|
]);
|
|
}
|
|
|
|
private function rateLimitKey(PaymentCloseReportRequest $request, string $oid): string
|
|
{
|
|
return 'sirsoft-pay_nicepayments:payment-close-report:'.sha1($request->ip().'|'.$oid);
|
|
}
|
|
|
|
/**
|
|
* @return array{status: string, reason?: string}
|
|
*/
|
|
private function recordCloseReportWithLock(string $oid, int $price, string $closeReason): array
|
|
{
|
|
return DB::transaction(function () use ($oid, $price, $closeReason): array {
|
|
// audit:allow controller-direct-data-access reason: PG 플러그인의 결제 레코드 직접 조회/기록 — ecommerce Repository 의존 시 모듈 버전 제약 연쇄(PaymentLimits 선례). Service/Repository 이관은 후속 백로그
|
|
$lockedOrder = Order::query()
|
|
->where('order_number', $oid)
|
|
->lockForUpdate()
|
|
->first();
|
|
|
|
if (! $lockedOrder || ! $lockedOrder->order_status->isBeforePayment()) {
|
|
return ['status' => 'ignored', 'reason' => 'order_not_payable'];
|
|
}
|
|
|
|
$payment = $lockedOrder->payment()->lockForUpdate()->first();
|
|
$lockedOrder->setRelation('payment', $payment);
|
|
|
|
// close-report 는 결제창이 승인/발급 단계로 넘어가기 전 READY 상태만 실패 처리한다.
|
|
// 카드 승인 콜백과 경쟁해 PAID 가 먼저 저장됐거나, 가상계좌 발급으로 WAITING_DEPOSIT 이
|
|
// 된 주문은 닫힘 보고가 뒤늦게 도착해도 주문/결제 상태를 덮으면 안 된다.
|
|
if ($payment?->isPaid()) {
|
|
return ['status' => 'ignored', 'reason' => 'payment_already_paid'];
|
|
}
|
|
if (! $payment || $payment->payment_status !== PaymentStatusEnum::READY) {
|
|
return ['status' => 'ignored', 'reason' => 'payment_not_ready'];
|
|
}
|
|
|
|
$expectedPrice = $this->resolveExpectedPaymentPriceOrNull($lockedOrder, 'close_report_locked', [
|
|
'oid' => $oid,
|
|
'received_price' => $price,
|
|
]);
|
|
if ($expectedPrice === null) {
|
|
return ['status' => 'invalid_payment_currency'];
|
|
}
|
|
|
|
if ($price !== $expectedPrice) {
|
|
return ['status' => 'amount_mismatch'];
|
|
}
|
|
|
|
$this->markPaymentWindowClosed(
|
|
$this->orderService,
|
|
$lockedOrder,
|
|
self::FAILURE_CODE,
|
|
self::FAILURE_MESSAGE,
|
|
$closeReason,
|
|
);
|
|
|
|
return ['status' => 'recorded'];
|
|
});
|
|
}
|
|
}
|