|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Controllers\Admin;
|
|
|
|
|
|
|
|
|
|
use App\Controllers\AdminController;
|
|
|
|
|
use App\Services\Auth;
|
|
|
|
|
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 = [];
|
|
|
|
|
$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));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} 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('table_error', $tableError)
|
|
|
|
|
->assign('rate_saved', $request->getParam('rate_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');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function filters($request): array
|
|
|
|
|
{
|
|
|
|
|
$asset = $request->getParam('asset', 'CNY');
|
|
|
|
|
return [in_array($asset, PaymentReconciliation::ASSETS, true) ? $asset : 'CNY',
|
|
|
|
|
$request->getParam('review') === '1'];
|
|
|
|
|
}
|
|
|
|
|
}
|