102 lines
3.3 KiB
PHP
102 lines
3.3 KiB
PHP
<?php
|
|
|
|
namespace App\Repositories\Payments;
|
|
|
|
use App\Models\Payments\GcPinOrder;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
final class GcPinOrderRepository
|
|
{
|
|
public function findByOidForUpdate(string $oid): ?GcPinOrder
|
|
{
|
|
return GcPinOrder::query()
|
|
->where('oid', $oid)
|
|
->lockForUpdate()
|
|
->first();
|
|
}
|
|
|
|
public function findByOid(string $oid): ?GcPinOrder
|
|
{
|
|
return GcPinOrder::query()->where('oid', $oid)->first();
|
|
}
|
|
|
|
public function markMethod(GcPinOrder $order, string $method): void
|
|
{
|
|
$order->pay_method = $method;
|
|
$order->save();
|
|
}
|
|
|
|
public function markVactIssued(GcPinOrder $order, string $tid, array $issued): void
|
|
{
|
|
if ($order->stat_pay === 'p') return; // 이미 paid면 무시(멱등)
|
|
$order->stat_pay = 'w';
|
|
$order->pg_tid = $tid ?: $order->pg_tid;
|
|
$order->ret_code = (string)($issued['RETURNCODE'] ?? '0000');
|
|
$order->ret_msg = (string)($issued['RETURNMSG'] ?? '');
|
|
$order->pay_data = $this->jsonSafe(array_merge($order->pay_data ?? [], ['vact'=>$issued]));
|
|
$order->ret_data = $this->jsonSafe(array_merge($order->ret_data ?? [], ['vact_issue'=>$issued]));
|
|
$order->save();
|
|
}
|
|
|
|
public function markPaid(GcPinOrder $order, string $tid, string $code, string $msg, array $payload): void
|
|
{
|
|
if ($order->stat_pay === 'p') return; // 멱등
|
|
|
|
$order->stat_pay = 'p';
|
|
$order->paid_at = now();
|
|
$order->pg_tid = $tid ?: $order->pg_tid;
|
|
$order->ret_code = $code;
|
|
$order->ret_msg = $msg;
|
|
|
|
$payload = $this->jsonSafe($payload);
|
|
$merged = array_merge($order->ret_data ?? [], $payload);
|
|
$order->ret_data = $this->jsonSafe($merged);
|
|
|
|
$order->save();
|
|
}
|
|
|
|
public function markCancelled(GcPinOrder $order, string $code, string $msg, array $payload = []): void
|
|
{
|
|
if ($order->stat_pay === 'p') return;
|
|
$order->stat_pay = 'c';
|
|
$order->cancelled_at = now();
|
|
$order->ret_code = $code;
|
|
$order->ret_msg = $msg;
|
|
$payload = $this->jsonSafe($payload);
|
|
$order->ret_data = $this->jsonSafe(array_merge($order->ret_data ?? [], $payload));
|
|
|
|
$order->save();
|
|
}
|
|
|
|
public function markFailed(GcPinOrder $order, string $code, string $msg, array $payload = []): void
|
|
{
|
|
if ($order->stat_pay === 'p') return;
|
|
$order->stat_pay = 'f';
|
|
$order->ret_code = $code;
|
|
$order->ret_msg = $msg;
|
|
$payload = $this->jsonSafe($payload);
|
|
$order->ret_data = $this->jsonSafe(array_merge($order->ret_data ?? [], $payload));
|
|
$order->save();
|
|
}
|
|
|
|
private function jsonSafe(mixed $v): mixed
|
|
{
|
|
if (is_array($v)) {
|
|
foreach ($v as $k => $vv) $v[$k] = $this->jsonSafe($vv);
|
|
return $v;
|
|
}
|
|
if (is_object($v)) return $this->jsonSafe((array)$v);
|
|
|
|
if (is_string($v)) {
|
|
if (function_exists('mb_check_encoding') && mb_check_encoding($v, 'UTF-8')) return $v;
|
|
$out = @iconv('EUC-KR', 'UTF-8//IGNORE', $v);
|
|
if ($out === false) $out = '';
|
|
if (function_exists('mb_check_encoding') && !mb_check_encoding($out, 'UTF-8')) {
|
|
$out = @iconv('UTF-8', 'UTF-8//IGNORE', $out) ?: '';
|
|
}
|
|
return $out;
|
|
}
|
|
return $v;
|
|
}
|
|
}
|