id !== $userId) { throw new CloudError('账单不存在', 404); } // The invoice and its order/job/instance snapshot must agree. Holding // a shared lock prevents a concurrent payment transaction from // returning an old invoice status with newly-updated associations. $detail = (new CloudInvoice())->getConnection()->transaction( function () use ($invoiceId, $userId) { $invoice = CloudInvoice::where('id', $invoiceId) ->where('user_id', $userId) ->sharedLock() ->first(); if ($invoice === null) { throw new CloudError('账单不存在', 404); } return CloudInvoiceService::display($invoice); }, 3 ); return [ 'invoice' => $detail, 'payment_methods' => ($detail['payable'] ?? false) ? CloudInvoicePaymentService::methods($user) : [], 'balance_payment_allowed' => (int) $user->enable === 1 && !UserFeatureRestrictionService::isBalancePurchaseRestricted($user), 'balance' => (string) $user->money, 'csrf_token' => AdminCsrf::token(), 'user_id' => (string) $user->id, 'username' => (string) ($user->user_name ?: $user->email), 'admin' => $isAdmin, ]; } /** Return one owner-scoped, transactionally consistent polling snapshot. */ public static function statusForUser(int $invoiceId, int $userId): array { if ($invoiceId <= 0 || $userId <= 0) { throw new CloudError('账单不存在', 404); } return (new CloudInvoice())->getConnection()->transaction( function () use ($invoiceId, $userId) { $invoice = CloudInvoice::where('id', $invoiceId) ->where('user_id', $userId) ->sharedLock() ->first(['id', 'purpose', 'instance_id', 'order_id', 'user_id', 'due_at', 'status', 'paid_at', 'paid_via', 'created_at']); if ($invoice === null) { throw new CloudError('账单不存在', 404); } return CloudInvoiceService::status($invoice); }, 3 ); } }