feat: khởi tạo/huỷ domain Plesk qua job, gửi mail hoá đơn qua queue, đính kèm triển khai
- ProvisionDnsDomainJob/SuspendDnsDomainJob: tạo domain admin lên Plesk, huỷ = suspend (is_created=3) - Khách xem zone DNS được gán ở /dich-vu/domain, quản lý bản ghi A/AAAA/CNAME/TXT/MX/SRV - send-mail hoá đơn đẩy SendInvoiceMailJob, preview + sửa nội dung, ghi mail.order_id - invoice_attachments.type (payment_proof/deployment) - CacheService xoá theo SCAN pattern, Redis không persistent mặc định - Backfill transactions.public_code Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
30898ea46f
commit
94614d9cb3
@@ -122,7 +122,7 @@ class DomainOrderService
|
||||
|
||||
Transaction::create([
|
||||
'uuid' => (string) Str::uuid(),
|
||||
'public_code' => 'DOM-' . now()->format('Ymd') . '-' . strtoupper(Str::random(6)),
|
||||
'public_code' => Transaction::newPublicCode('DOM'),
|
||||
'trans' => $invoice->invoice_number,
|
||||
'amount' => $price,
|
||||
'customer_id' => $lockedCustomer->id,
|
||||
@@ -341,7 +341,7 @@ class DomainOrderService
|
||||
|
||||
Transaction::create([
|
||||
'uuid' => (string) Str::uuid(),
|
||||
'public_code' => 'DOM-REF-' . now()->format('Ymd') . '-' . strtoupper(Str::random(6)),
|
||||
'public_code' => Transaction::newPublicCode('DOM-REF'),
|
||||
'trans' => 'REF-' . $invoice->invoice_number,
|
||||
'amount' => $refundAmount,
|
||||
'customer_id' => $lockedCustomer->id,
|
||||
|
||||
@@ -223,7 +223,7 @@ class OrderQueryService
|
||||
'invoice_number' => $invoice?->invoice_number,
|
||||
'total' => (float) ($invoice?->total ?? 0),
|
||||
'status' => $invoice?->status,
|
||||
'payment_url' => url('/client/invoices/' . $invoice?->invoice_number . '/payment'),
|
||||
'payment_url' => $invoice?->clientPath(),
|
||||
],
|
||||
];
|
||||
}
|
||||
@@ -278,7 +278,7 @@ class OrderQueryService
|
||||
'invoice_number' => $invoice->invoice_number,
|
||||
'total' => (float) $invoice->total,
|
||||
'status' => $invoice->status,
|
||||
'payment_url' => url('/client/invoices/' . $invoice->invoice_number . '/payment'),
|
||||
'payment_url' => $invoice->clientPath(),
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
@@ -3,10 +3,11 @@
|
||||
namespace App\Services\Cache;
|
||||
|
||||
use Closure;
|
||||
use Illuminate\Cache\RedisStore;
|
||||
use Illuminate\Contracts\Cache\Repository as CacheRepository;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Facades\Redis;
|
||||
use Illuminate\Support\Str;
|
||||
use stdClass;
|
||||
|
||||
class CacheService implements CacheServiceInterface
|
||||
@@ -66,11 +67,6 @@ class CacheService implements CacheServiceInterface
|
||||
|
||||
public function clearByModule(string $module): void
|
||||
{
|
||||
if ($this->supportsTagging()) {
|
||||
Cache::tags([$module])->flush();
|
||||
return;
|
||||
}
|
||||
|
||||
$this->scanAndDelete($module);
|
||||
}
|
||||
|
||||
@@ -87,32 +83,24 @@ class CacheService implements CacheServiceInterface
|
||||
return Cache::store();
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete all keys for a module using SCAN (safe for production).
|
||||
*
|
||||
* KEYS is O(N) and blocks Redis; SCAN iterates in batches without blocking.
|
||||
* The pattern matches: {cache_prefix}:{module}:*
|
||||
*/
|
||||
private function scanAndDelete(string $module): void
|
||||
{
|
||||
$redis = Redis::connection('cache');
|
||||
$prefix = config('cache.prefix');
|
||||
$pattern = "{$prefix}:{$module}:*";
|
||||
$cursor = '0';
|
||||
$store = Cache::store()->getStore();
|
||||
if (! $store instanceof RedisStore) {
|
||||
return;
|
||||
}
|
||||
|
||||
$redis = $store->connection();
|
||||
$connectionPrefix = (string) config('database.redis.options.prefix');
|
||||
$pattern = $connectionPrefix . $store->getPrefix() . $module . ':*';
|
||||
$cursor = '0';
|
||||
|
||||
do {
|
||||
$result = $redis->scan($cursor, 'MATCH', $pattern, 'COUNT', 100);
|
||||
|
||||
if ($result === false) {
|
||||
break;
|
||||
}
|
||||
|
||||
[$cursor, $keys] = $result;
|
||||
[$cursor, $keys] = $redis->scan($cursor, ['match' => $pattern, 'count' => 500]) ?: ['0', []];
|
||||
|
||||
foreach ((array) $keys as $key) {
|
||||
$redis->del($key);
|
||||
$redis->del(Str::after($key, $connectionPrefix));
|
||||
}
|
||||
|
||||
} while ((string) $cursor !== '0');
|
||||
}
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ namespace App\Services\Customer;
|
||||
|
||||
use App\Models\Invoice;
|
||||
use App\Models\Order;
|
||||
use App\Enums\TransactionStatus;
|
||||
use App\Models\Transaction;
|
||||
use App\Repositories\Customer\CustomerRepositoryInterface;
|
||||
use App\Repositories\Invoice\InvoiceRepositoryInterface;
|
||||
@@ -11,6 +12,7 @@ use App\Repositories\Order\OrderRepositoryInterface;
|
||||
use App\Repositories\Transaction\TransactionRepositoryInterface;
|
||||
use App\Services\Customer\Concerns\ResolvesCustomer;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class CustomerBillingService
|
||||
{
|
||||
@@ -91,6 +93,36 @@ class CustomerBillingService
|
||||
return $transaction->refresh()->load('user');
|
||||
}
|
||||
|
||||
public function createTransaction(string $uuid, array $data, ?int $adminId): Transaction
|
||||
{
|
||||
$customer = $this->resolveCustomer($uuid);
|
||||
|
||||
$transaction = $this->transactionRepository->create([
|
||||
'uuid' => (string) Str::uuid(),
|
||||
'public_code' => Transaction::newPublicCode('ADM'),
|
||||
'trans' => $data['trans'] ?? null,
|
||||
'amount' => $data['amount'],
|
||||
'received_amount' => $data['amount'],
|
||||
'customer_id' => $customer->id,
|
||||
'user_id' => $adminId,
|
||||
'status' => $data['status'] ?? TransactionStatus::COMPLETED->value,
|
||||
'type' => 'admin_record',
|
||||
'note' => $data['note'] ?? null,
|
||||
]);
|
||||
|
||||
return $transaction->load('user');
|
||||
}
|
||||
|
||||
public function deleteTransaction(string $uuid, int $transactionId): void
|
||||
{
|
||||
$customer = $this->resolveCustomer($uuid);
|
||||
|
||||
$exists = Transaction::where('customer_id', $customer->id)->where('id', $transactionId)->exists();
|
||||
abort_if(!$exists, 404, 'Không tìm thấy giao dịch');
|
||||
|
||||
$this->transactionRepository->delete($transactionId);
|
||||
}
|
||||
|
||||
public function getOrders(string $uuid): Collection
|
||||
{
|
||||
$customer = $this->resolveCustomer($uuid);
|
||||
|
||||
@@ -294,6 +294,7 @@ class CustomerServiceProvisioningService
|
||||
|
||||
Transaction::create([
|
||||
'uuid' => (string) Str::uuid(),
|
||||
'public_code' => Transaction::newPublicCode('SVC'),
|
||||
'customer_id' => $customer->id,
|
||||
'user_id' => Auth::id(),
|
||||
'trans' => $order->order_num,
|
||||
|
||||
@@ -83,6 +83,7 @@ class CustomerWalletService
|
||||
{
|
||||
return Transaction::create([
|
||||
'uuid' => (string) Str::uuid(),
|
||||
'public_code' => Transaction::newPublicCode('ADM'),
|
||||
'trans' => 'ADMIN' . now()->format('YmdHis'),
|
||||
'amount' => $amount,
|
||||
'customer_id' => $customer->id,
|
||||
|
||||
@@ -4,6 +4,7 @@ namespace App\Services\Email;
|
||||
|
||||
use App\Jobs\Mail\SendTemplateEmailJob;
|
||||
use App\Models\Template;
|
||||
use App\Support\AutoEmailGate;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
|
||||
/**
|
||||
@@ -21,6 +22,10 @@ class EmailTemplateService
|
||||
*/
|
||||
public function send(string $scenario, string|array $to, array $data = []): void
|
||||
{
|
||||
if (!AutoEmailGate::allows($scenario)) {
|
||||
return;
|
||||
}
|
||||
|
||||
SendTemplateEmailJob::dispatch($scenario, is_array($to) ? $to : [$to], $data);
|
||||
}
|
||||
|
||||
|
||||
@@ -243,7 +243,7 @@ class InvoicePaymentService
|
||||
|
||||
Transaction::create([
|
||||
'uuid' => (string) Str::uuid(),
|
||||
'public_code' => 'INV-REF-' . now()->format('Ymd') . '-' . strtoupper(Str::random(6)),
|
||||
'public_code' => Transaction::newPublicCode('INV-REF'),
|
||||
'trans' => $invoice->invoice_number . '-REFUND',
|
||||
'type' => 'refund',
|
||||
'amount' => -$refundAmount,
|
||||
@@ -295,7 +295,7 @@ class InvoicePaymentService
|
||||
],
|
||||
[
|
||||
'uuid' => (string) Str::uuid(),
|
||||
'public_code' => 'INV-PAY-' . now()->format('Ymd') . '-' . strtoupper(Str::random(6)),
|
||||
'public_code' => Transaction::newPublicCode('INV-PAY'),
|
||||
'amount' => $amount,
|
||||
'received_amount' => $amount,
|
||||
'user_id' => $adminId,
|
||||
|
||||
@@ -24,6 +24,7 @@ use App\Repositories\InvoiceItem\InvoiceItemRepositoryInterface;
|
||||
use App\Repositories\Module\Hosting\HostingRepositoryInterface as ModuleHostingRepositoryInterface;
|
||||
use App\Repositories\Module\HostingServer\HostingServerRepositoryInterface;
|
||||
use App\Repositories\Order\OrderRepositoryInterface;
|
||||
use App\Support\AutoEmailGate;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
@@ -246,7 +247,7 @@ class OrderService
|
||||
],
|
||||
[
|
||||
'uuid' => (string) Str::uuid(),
|
||||
'public_code' => 'INV-PAY-' . now()->format('Ymd') . '-' . strtoupper(Str::random(6)),
|
||||
'public_code' => Transaction::newPublicCode('INV-PAY'),
|
||||
'amount' => $orderTotal,
|
||||
'user_id' => Auth::guard('admin')->id(),
|
||||
'status' => TransactionStatus::COMPLETED->value,
|
||||
@@ -258,15 +259,17 @@ class OrderService
|
||||
return $order;
|
||||
});
|
||||
|
||||
OrderCreatedJob::dispatch([
|
||||
'customer_email' => $customer->email,
|
||||
'client_name' => $customer->fullname,
|
||||
'order_num' => $orderData['order_num'],
|
||||
'invoice_num' => $invoiceData['invoice_number'],
|
||||
'order_total' => number_format($orderTotal, 0, ',', '.'),
|
||||
'company_name' => setting('general.company_name') ?: config('app.name'),
|
||||
'company_logo' => setting('general.company_logo') ?: '',
|
||||
]);
|
||||
if (AutoEmailGate::allows('order_created')) {
|
||||
OrderCreatedJob::dispatch([
|
||||
'customer_email' => $customer->email,
|
||||
'client_name' => $customer->fullname,
|
||||
'order_num' => $orderData['order_num'],
|
||||
'invoice_num' => $invoiceData['invoice_number'],
|
||||
'order_total' => number_format($orderTotal, 0, ',', '.'),
|
||||
'company_name' => setting('general.company_name') ?: config('app.name'),
|
||||
'company_logo' => setting('general.company_logo') ?: '',
|
||||
]);
|
||||
}
|
||||
|
||||
return $order;
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
namespace App\Services\Plesk;
|
||||
|
||||
use App\Models\DnsServer;
|
||||
use Illuminate\Http\Client\ConnectionException;
|
||||
use Illuminate\Http\Client\RequestException;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Str;
|
||||
@@ -83,6 +84,28 @@ class PleskDomainService
|
||||
return false;
|
||||
}
|
||||
|
||||
public function suspend(DnsServer $server, string $domain): bool
|
||||
{
|
||||
$this->lastError = null;
|
||||
|
||||
try {
|
||||
$response = $this->plesk->client($server, 30)
|
||||
->post($this->plesk->url($server, 'cli', '/subscription/call'), ['params' => ['--webspace-off', $domain]]);
|
||||
} catch (ConnectionException $e) {
|
||||
$this->lastError = $e->getMessage();
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($response->successful() && (int) $response->json('code', 1) === 0) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$this->lastError = trim((string) ($response->json('stderr') ?: $response->json('message') ?: $response->body()));
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function getDomainInfo(DnsServer $server, string $domain = ''): array|false
|
||||
{
|
||||
if (empty($domain)) {
|
||||
|
||||
Reference in New Issue
Block a user