|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Controllers\Admin;
|
|
|
|
|
|
|
|
|
|
use App\Controllers\AdminController;
|
|
|
|
|
use App\Models\CloudInvoicePayment;
|
|
|
|
|
use App\Services\AdminPermission;
|
|
|
|
|
use App\Services\Auth;
|
|
|
|
|
use App\Services\Payment\PaymentClosureService;
|
|
|
|
|
use App\Services\Payment\PaymentRateService;
|
|
|
|
|
use App\Services\Payment\PaymentReconciliation;
|
|
|
|
|
|
|
|
|
|
final class PaymentReconciliationController extends AdminController
|
|
|
|
|
{
|
|
|
|
|
public function index($request, $response, $args)
|
|
|
|
|
{
|
|
|
|
|
[$asset, $review] = $this->filters($request);
|
|
|
|
|
$totals = $rows = $recentClosures = [];
|
|
|
|
|
$tableError = false;
|
|
|
|
|
$orders = null;
|
|
|
|
|
try {
|
|
|
|
|
$totals = PaymentReconciliation::totals();
|
|
|
|
|
if ($asset !== 'USD') {
|
|
|
|
|
$orders = PaymentReconciliation::quotedOrders($asset, $review)
|
|
|
|
|
->orderByDesc('received_at')->orderByDesc('order_no')->paginate(30);
|
|
|
|
|
$orders->setPath('/admin/payment-reconciliation');
|
|
|
|
|
$orders->appends(['asset' => $asset, 'review' => $review ? '1' : '0']);
|
|
|
|
|
$refunds = PaymentReconciliation::refundRecords($orders->pluck('order_no')->all());
|
|
|
|
|
foreach ($orders as $quote) {
|
|
|
|
|
$rows[] = PaymentReconciliation::record($quote, $refunds->get($quote->order_no));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (AdminPermission::isSuperAdmin()) {
|
|
|
|
|
$recentClosures = CloudInvoicePayment::whereNotNull('provider_closed_at')->orderByDesc('id')
|
|
|
|
|
->limit(20)->get(['provider', 'provider_order_no', 'provider_closed_at',
|
|
|
|
|
'provider_closed_by', 'provider_close_evidence'])->map(static fn ($payment) => [
|
|
|
|
|
'provider' => $payment->provider,
|
|
|
|
|
'order' => $payment->provider_order_no,
|
|
|
|
|
'time' => gmdate('Y-m-d H:i:s', (int) $payment->provider_closed_at),
|
|
|
|
|
'actor' => $payment->provider_closed_by,
|
|
|
|
|
'evidence' => $payment->provider_close_evidence,
|
|
|
|
|
])->all();
|
|
|
|
|
}
|
|
|
|
|
} catch (\Illuminate\Database\QueryException) {
|
|
|
|
|
$tableError = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$rates = [];
|
|
|
|
|
foreach (['USD/CNY', 'USDT/USD'] as $pair) {
|
|
|
|
|
$rate = PaymentRateService::status($pair);
|
|
|
|
|
$times = [
|
|
|
|
|
'source_at' => $rate['source_at'], 'fetched_at' => $rate['fetched_at'],
|
|
|
|
|
'valid_until' => $rate['current']['valid_until'] ?? null, 'attempted_at' => $rate['attempted_at'],
|
|
|
|
|
];
|
|
|
|
|
foreach ($times as $key => $seconds) {
|
|
|
|
|
$rate[$key . '_display'] = $seconds === null ? '—' : gmdate('Y-m-d H:i:s', (int) $seconds);
|
|
|
|
|
}
|
|
|
|
|
$rates[$pair] = $rate;
|
|
|
|
|
}
|
|
|
|
|
$this->setViewTheme('venix');
|
|
|
|
|
return $this->view()
|
|
|
|
|
->assign('rates', $rates)->assign('totals', $totals)
|
|
|
|
|
->assign('asset', $asset)->assign('review', $review)
|
|
|
|
|
->assign('quote_rows', $rows)->assign('orders', $orders)
|
|
|
|
|
->assign('recent_closures', $recentClosures)
|
|
|
|
|
->assign('table_error', $tableError)
|
|
|
|
|
->assign('rate_saved', $request->getParam('rate_saved') === '1')
|
|
|
|
|
->assign('closure_saved', $request->getParam('closure_saved') === '1')
|
|
|
|
|
->display('admin/payment-reconciliation/index.tpl');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function overrideRate($request, $response, $args)
|
|
|
|
|
{
|
|
|
|
|
$payload = $request->getParsedBody();
|
|
|
|
|
$payload = is_array($payload) ? $payload : [];
|
|
|
|
|
$minutes = filter_var($payload['minutes'] ?? null, FILTER_VALIDATE_INT, [
|
|
|
|
|
'options' => ['min_range' => 1, 'max_range' => 4320],
|
|
|
|
|
]);
|
|
|
|
|
try {
|
|
|
|
|
if ($minutes === false
|
|
|
|
|
|| !is_string($payload['pair'] ?? null)
|
|
|
|
|
|| !is_string($payload['rate'] ?? null) || !is_string($payload['reason'] ?? null)) {
|
|
|
|
|
throw new \InvalidArgumentException('请填写有效的币对、汇率、有效分钟数和理由');
|
|
|
|
|
}
|
|
|
|
|
PaymentRateService::override($payload['pair'], trim($payload['rate']),
|
|
|
|
|
time() + $minutes * 60, (int) Auth::getUser()->id, $payload['reason']);
|
|
|
|
|
} catch (\InvalidArgumentException $error) {
|
|
|
|
|
return $response->withJson(['ret' => 0, 'message' => $error->getMessage()], 422);
|
|
|
|
|
}
|
|
|
|
|
return $response->withRedirect('/admin/payment-reconciliation?rate_saved=1', 303);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function export($request, $response, $args)
|
|
|
|
|
{
|
|
|
|
|
[$asset, $review] = $this->filters($request);
|
|
|
|
|
$file = tmpfile();
|
|
|
|
|
if ($file === false) {
|
|
|
|
|
throw new \RuntimeException('无法创建对账导出');
|
|
|
|
|
}
|
|
|
|
|
PaymentReconciliation::csv($file, $asset, $review);
|
|
|
|
|
rewind($file);
|
|
|
|
|
return $response->withBody(new \Slim\Http\Stream($file))
|
|
|
|
|
->withHeader('Content-Type', 'text/csv; charset=utf-8')
|
|
|
|
|
->withHeader('Content-Disposition', 'attachment; filename="payments-' . $asset . '.csv"')
|
|
|
|
|
->withHeader('Cache-Control', 'no-store, private');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function confirmClosure($request, $response, $args)
|
|
|
|
|
{
|
|
|
|
|
$payload = $request->getParsedBody();
|
|
|
|
|
try {
|
|
|
|
|
if (!is_array($payload) || ($payload['confirm'] ?? '') !== 'closed'
|
|
|
|
|
|| !is_string($payload['provider'] ?? null) || !is_string($payload['order_no'] ?? null)
|
|
|
|
|
|| !is_string($payload['evidence'] ?? null)) {
|
|
|
|
|
throw new \DomainException('请先核实商户订单已关闭且未收款');
|
|
|
|
|
}
|
|
|
|
|
PaymentClosureService::confirm($payload['provider'], trim($payload['order_no']),
|
|
|
|
|
(int) Auth::getUser()->id, $payload['evidence']);
|
|
|
|
|
} catch (\DomainException $error) {
|
|
|
|
|
return $response->withJson(['ret' => 0, 'message' => $error->getMessage()], 422);
|
|
|
|
|
}
|
|
|
|
|
return $response->withRedirect('/admin/payment-reconciliation?closure_saved=1', 303);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function filters($request): array
|
|
|
|
|
{
|
|
|
|
|
$asset = $request->getParam('asset', 'CNY');
|
|
|
|
|
return [in_array($asset, PaymentReconciliation::ASSETS, true) ? $asset : 'CNY',
|
|
|
|
|
$request->getParam('review') === '1'];
|
|
|
|
|
}
|
|
|
|
|
}
|