feat(payment): display charge and credit currencies
parent
c6557c0cd1
commit
3c054cf5b3
@ -0,0 +1,98 @@
|
||||
<?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'];
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Payment;
|
||||
|
||||
final class PaymentRateUnavailable extends \InvalidArgumentException
|
||||
{
|
||||
public const MESSAGE = '当前支付汇率不可用,请选择其他付款方式';
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct(self::MESSAGE);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,91 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Payment;
|
||||
|
||||
use App\Models\PaymentCheckoutQuote;
|
||||
use App\Models\StripeOrder;
|
||||
use App\Models\YipayRefund;
|
||||
use App\Services\Cloud\CloudMoney;
|
||||
use Brick\Math\BigDecimal;
|
||||
|
||||
final class PaymentReconciliation
|
||||
{
|
||||
public const ASSETS = ['CNY', 'USDT', 'USD'];
|
||||
|
||||
public static function totals(): array
|
||||
{
|
||||
$rows = PaymentCheckoutQuote::query()->select('asset')
|
||||
->selectRaw("SUM(CASE WHEN status = 'accepted' THEN received_units ELSE 0 END) AS received")
|
||||
->selectRaw("SUM(CASE WHEN status = 'accepted' THEN credit_cents ELSE 0 END) AS credited")
|
||||
->selectRaw("SUM(CASE WHEN status = 'review' THEN 1 ELSE 0 END) AS needs_review")
|
||||
->groupBy('asset')->get()->keyBy('asset');
|
||||
$stripe = (string) StripeOrder::where('status', 'paid')->sum('amount');
|
||||
$totals = [];
|
||||
foreach (['CNY' => 2, 'USDT' => 6] as $asset => $scale) {
|
||||
$row = $rows->get($asset);
|
||||
$totals[$asset] = [
|
||||
'received' => (string) BigDecimal::ofUnscaledValue((string) ($row->received ?? 0), $scale),
|
||||
'credited' => (string) BigDecimal::ofUnscaledValue((string) ($row->credited ?? 0), 2),
|
||||
'review' => (int) ($row->needs_review ?? 0),
|
||||
];
|
||||
}
|
||||
$usd = (string) BigDecimal::of($stripe)->toScale(2);
|
||||
$totals['USD'] = ['received' => $usd, 'credited' => $usd, 'review' => 0];
|
||||
return $totals;
|
||||
}
|
||||
|
||||
public static function quotedOrders(string $asset, bool $review)
|
||||
{
|
||||
return PaymentCheckoutQuote::where('asset', $asset)
|
||||
->when($review, static fn ($query) => $query->where('status', 'review'));
|
||||
}
|
||||
|
||||
public static function record(PaymentCheckoutQuote $quote, ?YipayRefund $refund = null): array
|
||||
{
|
||||
// An unverified reported currency must never be counted as CNY or USDT.
|
||||
$received = $quote->received_units !== null && $quote->review_reason !== 'currency_mismatch'
|
||||
? PaymentCheckoutQuotes::formatUnits($quote->asset, $quote->received_units) : '';
|
||||
return [
|
||||
'order' => $quote->order_no,
|
||||
'provider' => $quote->provider,
|
||||
'asset' => $quote->asset,
|
||||
'status' => $quote->status,
|
||||
'reason' => $quote->review_reason ?? '',
|
||||
'usd_amount' => $quote->quote_version === 1 ? CloudMoney::decimal($quote->usd_cents) : '',
|
||||
'quoted' => $quote->quote_version === 1
|
||||
? PaymentCheckoutQuotes::formatUnits($quote->asset, $quote->expected_units) : '',
|
||||
'received' => $received,
|
||||
'credited' => $quote->credit_cents === null ? '' : CloudMoney::decimal($quote->credit_cents),
|
||||
'refund_status' => $refund->status ?? '',
|
||||
'refund_cny' => $refund->gateway_amount ?? '',
|
||||
'refund_usd' => $refund->amount ?? '',
|
||||
'time' => $quote->received_at ?? $quote->quoted_at,
|
||||
];
|
||||
}
|
||||
|
||||
public static function refundRecords(array $orders)
|
||||
{
|
||||
return YipayRefund::whereIn('order_no', $orders)->get()->keyBy('order_no');
|
||||
}
|
||||
|
||||
public static function csv($stream, string $asset, bool $review): void
|
||||
{
|
||||
fputcsv($stream, ['order', 'provider', 'asset', 'status', 'reason', 'usd_amount', 'quoted',
|
||||
'received', 'credited', 'refund_status', 'refund_cny', 'refund_usd', 'timestamp_utc'], escape: '');
|
||||
if ($asset === 'USD') {
|
||||
if (!$review) {
|
||||
foreach (StripeOrder::where('status', 'paid')->orderBy('id')->lazyById(500) as $order) {
|
||||
fputcsv($stream, [$order->order_no, 'stripe_' . $order->payment_method, 'USD', 'paid', '',
|
||||
$order->amount, $order->amount, $order->amount, $order->amount, '', '', '', $order->paid_at], escape: '');
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
self::quotedOrders($asset, $review)->chunkById(500, static function ($rows) use ($stream): void {
|
||||
$refunds = self::refundRecords($rows->pluck('order_no')->all());
|
||||
foreach ($rows as $quote) {
|
||||
fputcsv($stream, self::record($quote, $refunds->get($quote->order_no)), escape: '');
|
||||
}
|
||||
}, 'order_no');
|
||||
}
|
||||
}
|
||||
@ -1,2 +1,2 @@
|
||||
{* Shared Venix CSS/JS cache key. Increment whenever a shared browser asset changes. *}
|
||||
{assign var=venix_asset_version value='2026111801' scope='global'}
|
||||
{assign var=venix_asset_version value='2026112201' scope='global'}
|
||||
|
||||
Loading…
Reference in New Issue