You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
214 lines
9.0 KiB
PHP
214 lines
9.0 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Cloud;
|
|
|
|
use App\Models\CloudInvoicePayment;
|
|
use App\Models\CloudJob;
|
|
use App\Models\PaytaroOrder;
|
|
use App\Models\StripeOrder;
|
|
use App\Models\UsdtPaymentOrder;
|
|
use App\Models\YipayOrder;
|
|
|
|
/**
|
|
* Resolves a terminal payment-order creation fence from durable local evidence.
|
|
*
|
|
* This service never calls a payment gateway and never creates or retransmits
|
|
* an order. A signed callback resolves "applied" automatically elsewhere;
|
|
* the manual path is only an audited bookkeeping decision after an operator
|
|
* has checked the provider out of band.
|
|
*/
|
|
final class CloudPaymentEffectResolutionService
|
|
{
|
|
public static function handles(string $id): bool
|
|
{
|
|
if (preg_match('/^[1-9][0-9]{0,17}$/D', $id) !== 1) {
|
|
return false;
|
|
}
|
|
return CloudJob::where('id', (int) $id)
|
|
->where('kind', 'payment_create')
|
|
->where('execution_mode', 'local')->exists();
|
|
}
|
|
|
|
public static function resolve(array $actor, string $id,
|
|
array $input): array
|
|
{
|
|
if (!($actor['is_admin'] ?? false)
|
|
|| !($actor['is_super_admin'] ?? false)
|
|
|| !($actor['can_manage'] ?? false)) {
|
|
throw new CloudError('只有超级管理员可以确认支付订单影响', 403);
|
|
}
|
|
CloudAccess::manage($actor);
|
|
if (preg_match('/^[1-9][0-9]{0,17}$/D', $id) !== 1) {
|
|
throw new CloudError('编号无效', 400);
|
|
}
|
|
$resolution = $input['resolution'] ?? null;
|
|
$reason = is_string($input['reason'] ?? null)
|
|
? trim($input['reason']) : '';
|
|
if (!in_array($resolution, ['applied', 'not_applied'], true)
|
|
|| mb_strlen($reason) < 20 || mb_strlen($reason) > 500) {
|
|
throw new CloudError('支付影响结论或核验凭据无效', 400);
|
|
}
|
|
|
|
$audit = (new CloudJob())->getConnection()->transaction(
|
|
function () use ($actor, $id, $resolution, $reason): array {
|
|
$job = CloudJob::where('id', (int) $id)
|
|
->lockForUpdate()->first();
|
|
if ($job === null || (string) $job->kind !== 'payment_create'
|
|
|| (string) $job->execution_mode !== 'local'
|
|
|| (string) $job->phase !== 'terminal'
|
|
|| (string) $job->outcome !== 'failed'
|
|
|| !(bool) $job->effect_may_have_applied
|
|
|| (string) $job->effect_resolution !== 'unknown'
|
|
|| $job->invoice_id === null) {
|
|
throw new CloudError('支付任务不处于可确认的影响未知状态', 409);
|
|
}
|
|
$payment = CloudInvoicePayment::where(
|
|
'invoice_id', (int) $job->invoice_id
|
|
)->where('user_id', (int) $job->user_id)
|
|
->where('idempotency_key',
|
|
(string) $job->request_idempotency_key)
|
|
->lockForUpdate()->first();
|
|
|
|
if ($resolution === 'applied') {
|
|
if ($payment === null
|
|
|| !in_array((string) $payment->status,
|
|
['pending', 'paid_invoice', 'credited_balance'],
|
|
true)) {
|
|
throw new CloudError(
|
|
'本地没有可验证的支付订单结果,不能确认已应用',
|
|
409
|
|
);
|
|
}
|
|
self::assertAppliedProviderLedger($payment);
|
|
} else {
|
|
if ($payment !== null) {
|
|
if (!in_array((string) $payment->status,
|
|
['creating', 'creation_unknown', 'not_created'],
|
|
true)) {
|
|
throw new CloudError(
|
|
'本地支付订单已可用或已结算,不能确认未应用',
|
|
409
|
|
);
|
|
}
|
|
self::markProviderLedgerNotCreated($payment);
|
|
$payment->status = 'not_created';
|
|
$payment->updated_at = time();
|
|
$payment->save();
|
|
}
|
|
}
|
|
|
|
$now = time();
|
|
$job->effect_resolution = $resolution;
|
|
$job->effect_resolved_at = $now;
|
|
$job->effect_resolved_by = (int) ($actor['user_id'] ?? 0);
|
|
$job->effect_resolution_reason = $reason;
|
|
$job->updated_at = $now;
|
|
$job->save();
|
|
return [
|
|
'operation_id' => (string) $job->operation_id,
|
|
'checked_at' => $now,
|
|
'outcome' => $resolution,
|
|
'summary_code' => 'manual_provider_evidence',
|
|
'summary' => '已根据外部支付平台凭据记录人工结论',
|
|
'payment_id' => $payment === null
|
|
? null : (string) $payment->id,
|
|
'payment_status' => $payment === null
|
|
? null : (string) $payment->status,
|
|
];
|
|
},
|
|
3
|
|
);
|
|
error_log(sprintf(
|
|
'cloud admin resolved payment effect job_id=%d resolution=%s actor_id=%d reason_sha256=%s',
|
|
(int) $id,
|
|
(string) $resolution,
|
|
(int) ($actor['user_id'] ?? 0),
|
|
hash('sha256', $reason)
|
|
));
|
|
return $audit;
|
|
}
|
|
|
|
private static function assertAppliedProviderLedger(
|
|
CloudInvoicePayment $payment
|
|
): void {
|
|
$provider = (string) $payment->provider;
|
|
$orderNo = (string) $payment->provider_order_no;
|
|
if (str_starts_with($provider, 'yipay_')) {
|
|
$order = YipayOrder::where('order_no', $orderNo)
|
|
->lockForUpdate()->first();
|
|
if ($order === null || !in_array((string) $order->status,
|
|
['pending', 'paid', 'refunded'], true)
|
|
|| trim((string) $order->gateway_trade_no) === '') {
|
|
throw new CloudError('易支付本地订单不足以证明已应用', 409);
|
|
}
|
|
return;
|
|
}
|
|
if ($provider === 'paytaro') {
|
|
$order = PaytaroOrder::where('order_no', $orderNo)
|
|
->lockForUpdate()->first();
|
|
if ($order === null || !in_array((string) $order->status,
|
|
['pending', 'paid'], true)
|
|
|| trim((string) $order->gateway_trade_no) === '') {
|
|
throw new CloudError('Paytaro 本地订单不足以证明已应用', 409);
|
|
}
|
|
return;
|
|
}
|
|
if (str_starts_with($provider, 'stripe_')) {
|
|
$order = StripeOrder::where('order_no', $orderNo)
|
|
->lockForUpdate()->first();
|
|
if ($order === null || !in_array((string) $order->status,
|
|
['pending', 'paid'], true)
|
|
|| trim((string) $order->checkout_session_id) === '') {
|
|
throw new CloudError(
|
|
'Stripe 本地订单不足以证明已应用', 409
|
|
);
|
|
}
|
|
return;
|
|
}
|
|
if ($provider === 'usdt') {
|
|
$order = UsdtPaymentOrder::where('order_no', $orderNo)
|
|
->lockForUpdate()->first();
|
|
if ($order === null || !in_array((string) $order->status,
|
|
['pending', 'expired', 'paid'], true)) {
|
|
throw new CloudError('USDT 本地订单不足以证明已应用', 409);
|
|
}
|
|
return;
|
|
}
|
|
throw new CloudError('支付渠道账本无效', 409);
|
|
}
|
|
|
|
private static function markProviderLedgerNotCreated(
|
|
CloudInvoicePayment $payment
|
|
): void {
|
|
$provider = (string) $payment->provider;
|
|
$orderNo = (string) $payment->provider_order_no;
|
|
if (str_starts_with($provider, 'yipay_')) {
|
|
$order = YipayOrder::where('order_no', $orderNo)
|
|
->lockForUpdate()->first();
|
|
} elseif ($provider === 'paytaro') {
|
|
$order = PaytaroOrder::where('order_no', $orderNo)
|
|
->lockForUpdate()->first();
|
|
} elseif (str_starts_with($provider, 'stripe_')) {
|
|
$order = StripeOrder::where('order_no', $orderNo)
|
|
->lockForUpdate()->first();
|
|
if ($order !== null
|
|
&& trim((string) $order->checkout_session_id) !== '') {
|
|
throw new CloudError(
|
|
'Stripe Checkout 会话已经创建,不能确认未应用', 409
|
|
);
|
|
}
|
|
} elseif ($provider === 'usdt') {
|
|
throw new CloudError('USDT 订单在本地原子创建,不存在远程创建结果不明', 409);
|
|
} else {
|
|
throw new CloudError('支付渠道账本无效', 409);
|
|
}
|
|
if ($order === null || !in_array((string) $order->status,
|
|
['creating', 'creation_unknown', 'not_created'], true)) {
|
|
throw new CloudError('支付渠道订单不能确认为未创建', 409);
|
|
}
|
|
$order->status = 'not_created';
|
|
$order->updated_at = time();
|
|
$order->save();
|
|
}
|
|
}
|