feat: 商城付费购买流 + 市场契约
- MarketPackage 模型/迁移:市场商品作为可购买实体(payable),复用 Payment 订单 - MarketPurchaseService:购买创建订单、已购判断、免费/付费门槛校验 - payment.paid 监听(核心注册,仅支付插件启用时)标记商品已购 - 市场契约:条目含 name/type/price(分)/download_url;插件市场 Tab 只显示 plugin、主题市场 Tab 只显示 theme;付费商品显示「购买」按钮与「已购买」徽章,未购买无法安装 - 新增主题市场 Tab(原仅插件页有市场) - 测试:购买/沙箱支付标记已购/重复购买/安装门槛/免费条目/支付插件禁用
This commit is contained in:
@@ -0,0 +1,114 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Blog\Services;
|
||||||
|
|
||||||
|
use App\Blog\Support\PluginManager;
|
||||||
|
use App\Models\MarketPackage;
|
||||||
|
use Plugins\Neatstudio\Payment\Models\Payment;
|
||||||
|
use Plugins\Neatstudio\Payment\Services\PaymentManager;
|
||||||
|
use RuntimeException;
|
||||||
|
|
||||||
|
class MarketPurchaseService
|
||||||
|
{
|
||||||
|
public function __construct(private PluginManager $plugins, private PaymentManager $payment)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 市场条目键:优先 item.name,缺省用下载文件名。
|
||||||
|
*/
|
||||||
|
public function itemKey(array $item): string
|
||||||
|
{
|
||||||
|
$name = $item['name'] ?? null;
|
||||||
|
|
||||||
|
if ($name) {
|
||||||
|
return (string) $name;
|
||||||
|
}
|
||||||
|
|
||||||
|
return basename((string) ($item['download_url'] ?? ''), '.zip') ?: uniqid('item-');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function itemType(array $item): string
|
||||||
|
{
|
||||||
|
return $item['type'] ?? 'plugin';
|
||||||
|
}
|
||||||
|
|
||||||
|
public function requiresPayment(array $item): bool
|
||||||
|
{
|
||||||
|
return (int) ($item['price'] ?? 0) > 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 购买市场商品:创建订单并返回(null = 已购买过,无需重复支付)。
|
||||||
|
*/
|
||||||
|
public function purchase(array $item): ?Payment
|
||||||
|
{
|
||||||
|
if (! $this->plugins->isEnabled('neatstudio.payment')) {
|
||||||
|
throw new RuntimeException('付费市场商品需要启用支付插件');
|
||||||
|
}
|
||||||
|
|
||||||
|
$price = (int) ($item['price'] ?? 0);
|
||||||
|
|
||||||
|
if ($price <= 0) {
|
||||||
|
throw new RuntimeException('该商品免费,无需购买');
|
||||||
|
}
|
||||||
|
|
||||||
|
$key = $this->itemKey($item);
|
||||||
|
$type = $this->itemType($item);
|
||||||
|
$user = auth()->user();
|
||||||
|
|
||||||
|
$package = MarketPackage::query()->firstOrCreate(
|
||||||
|
['item_key' => $key, 'item_type' => $type],
|
||||||
|
[
|
||||||
|
'title' => (string) ($item['title'] ?? $key),
|
||||||
|
'price' => $price,
|
||||||
|
'status' => 'pending',
|
||||||
|
'user_id' => $user?->id,
|
||||||
|
]
|
||||||
|
);
|
||||||
|
|
||||||
|
if ($package->isPaid()) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->payment->createOrder(
|
||||||
|
$user ?? $this->systemUser(),
|
||||||
|
'购买商品:'.$package->title,
|
||||||
|
$price,
|
||||||
|
$package,
|
||||||
|
'alipay'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function isPurchased(string $key, string $type = 'plugin'): bool
|
||||||
|
{
|
||||||
|
return MarketPackage::query()
|
||||||
|
->where('item_key', $key)
|
||||||
|
->where('item_type', $type)
|
||||||
|
->where('status', 'paid')
|
||||||
|
->exists();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 校验市场条目可否安装:免费直接装,付费需已购买。
|
||||||
|
*/
|
||||||
|
public function assertCanInstall(array $item): void
|
||||||
|
{
|
||||||
|
if ($this->requiresPayment($item) && ! $this->isPurchased($this->itemKey($item), $this->itemType($item))) {
|
||||||
|
throw new RuntimeException('该商品为付费商品,请先购买后再安装');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private function systemUser(): \App\Models\User
|
||||||
|
{
|
||||||
|
$user = \App\Models\User::query()->first();
|
||||||
|
|
||||||
|
if (! $user) {
|
||||||
|
throw new RuntimeException('无可用用户创建订单,请先登录');
|
||||||
|
}
|
||||||
|
|
||||||
|
return $user;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -29,7 +29,16 @@ class MarketplaceClient
|
|||||||
|
|
||||||
$response->throw();
|
$response->throw();
|
||||||
|
|
||||||
return $response->json('items', []);
|
$items = $response->json('items', []);
|
||||||
|
|
||||||
|
// 规范化市场契约:price 分、type(plugin/theme)、name
|
||||||
|
return array_map(function (array $item) {
|
||||||
|
$item['price'] = (int) ($item['price'] ?? 0);
|
||||||
|
$item['type'] = $item['type'] ?? 'plugin';
|
||||||
|
$item['name'] = $item['name'] ?? null;
|
||||||
|
|
||||||
|
return $item;
|
||||||
|
}, is_array($items) ? $items : []);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function download(string $url): string
|
public function download(string $url): string
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ declare(strict_types=1);
|
|||||||
namespace App\Filament\Pages;
|
namespace App\Filament\Pages;
|
||||||
|
|
||||||
use App\Blog\Services\MarketplaceClient;
|
use App\Blog\Services\MarketplaceClient;
|
||||||
|
use App\Blog\Services\MarketPurchaseService;
|
||||||
use App\Blog\Services\PackageInstaller;
|
use App\Blog\Services\PackageInstaller;
|
||||||
use App\Blog\Support\PluginManager;
|
use App\Blog\Support\PluginManager;
|
||||||
use Filament\Notifications\Notification;
|
use Filament\Notifications\Notification;
|
||||||
@@ -88,8 +89,12 @@ class Plugins extends Page
|
|||||||
public function loadMarket(): void
|
public function loadMarket(): void
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
$this->marketItems = app(MarketplaceClient::class)->items();
|
$items = app(MarketplaceClient::class)->items();
|
||||||
Notification::make()->title('市场已刷新:'.count($this->marketItems).' 个包')->success()->send();
|
$this->marketItems = array_values(array_filter(
|
||||||
|
$items,
|
||||||
|
fn (array $item) => ($item['type'] ?? 'plugin') === 'plugin'
|
||||||
|
));
|
||||||
|
Notification::make()->title('市场已刷新:'.count($this->marketItems).' 个插件')->success()->send();
|
||||||
} catch (\Throwable $e) {
|
} catch (\Throwable $e) {
|
||||||
Notification::make()->title('市场加载失败')->body($e->getMessage())->danger()->send();
|
Notification::make()->title('市场加载失败')->body($e->getMessage())->danger()->send();
|
||||||
}
|
}
|
||||||
@@ -104,6 +109,9 @@ class Plugins extends Page
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
// 付费商品需先购买
|
||||||
|
app(MarketPurchaseService::class)->assertCanInstall($item);
|
||||||
|
|
||||||
$result = app(PackageInstaller::class)->installFromMarket($item);
|
$result = app(PackageInstaller::class)->installFromMarket($item);
|
||||||
Notification::make()->title('安装成功:'.$result['key'])->success()->send();
|
Notification::make()->title('安装成功:'.$result['key'])->success()->send();
|
||||||
} catch (\Throwable $e) {
|
} catch (\Throwable $e) {
|
||||||
@@ -111,6 +119,43 @@ class Plugins extends Page
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function purchaseMarketItem(string $index)
|
||||||
|
{
|
||||||
|
$item = $this->marketItems[(int) $index] ?? null;
|
||||||
|
|
||||||
|
if (! $item) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
$payment = app(MarketPurchaseService::class)->purchase($item);
|
||||||
|
|
||||||
|
if (! $payment) {
|
||||||
|
Notification::make()->title('该商品已购买')->info()->send();
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
return redirect()->route('pay.checkout', $payment);
|
||||||
|
} catch (\Throwable $e) {
|
||||||
|
Notification::make()->title('购买失败')->body($e->getMessage())->danger()->send();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function isMarketPurchased(string $index): bool
|
||||||
|
{
|
||||||
|
$item = $this->marketItems[(int) $index] ?? null;
|
||||||
|
|
||||||
|
if (! $item) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return app(MarketPurchaseService::class)->isPurchased(
|
||||||
|
app(MarketPurchaseService::class)->itemKey($item),
|
||||||
|
app(MarketPurchaseService::class)->itemType($item)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
public function getMarketConfiguredProperty(): bool
|
public function getMarketConfiguredProperty(): bool
|
||||||
{
|
{
|
||||||
return (bool) config('market.url');
|
return (bool) config('market.url');
|
||||||
|
|||||||
@@ -5,6 +5,8 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace App\Filament\Pages;
|
namespace App\Filament\Pages;
|
||||||
|
|
||||||
|
use App\Blog\Services\MarketplaceClient;
|
||||||
|
use App\Blog\Services\MarketPurchaseService;
|
||||||
use App\Blog\Services\PackageInstaller;
|
use App\Blog\Services\PackageInstaller;
|
||||||
use App\Blog\Support\ThemeManager;
|
use App\Blog\Support\ThemeManager;
|
||||||
use Filament\Notifications\Notification;
|
use Filament\Notifications\Notification;
|
||||||
@@ -27,6 +29,8 @@ class Themes extends Page
|
|||||||
|
|
||||||
public $zip;
|
public $zip;
|
||||||
|
|
||||||
|
public array $marketItems = [];
|
||||||
|
|
||||||
public function getThemesProperty(): array
|
public function getThemesProperty(): array
|
||||||
{
|
{
|
||||||
return app(ThemeManager::class)->all();
|
return app(ThemeManager::class)->all();
|
||||||
@@ -67,4 +71,78 @@ class Themes extends Page
|
|||||||
Notification::make()->title('删除失败')->body($e->getMessage())->danger()->send();
|
Notification::make()->title('删除失败')->body($e->getMessage())->danger()->send();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function loadThemeMarket(): void
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
$items = app(MarketplaceClient::class)->items();
|
||||||
|
$this->marketItems = array_values(array_filter(
|
||||||
|
$items,
|
||||||
|
fn (array $item) => ($item['type'] ?? 'plugin') === 'theme'
|
||||||
|
));
|
||||||
|
Notification::make()->title('市场已刷新:'.count($this->marketItems).' 个主题')->success()->send();
|
||||||
|
} catch (\Throwable $e) {
|
||||||
|
Notification::make()->title('市场加载失败')->body($e->getMessage())->danger()->send();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function installFromThemeMarket(string $index): void
|
||||||
|
{
|
||||||
|
$item = $this->marketItems[(int) $index] ?? null;
|
||||||
|
|
||||||
|
if (! $item) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
app(MarketPurchaseService::class)->assertCanInstall($item);
|
||||||
|
|
||||||
|
$result = app(PackageInstaller::class)->installFromMarket($item);
|
||||||
|
Notification::make()->title('主题安装成功:'.$result['key'])->success()->send();
|
||||||
|
} catch (\Throwable $e) {
|
||||||
|
Notification::make()->title('安装失败')->body($e->getMessage())->danger()->send();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function purchaseThemeMarketItem(string $index)
|
||||||
|
{
|
||||||
|
$item = $this->marketItems[(int) $index] ?? null;
|
||||||
|
|
||||||
|
if (! $item) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
$payment = app(MarketPurchaseService::class)->purchase($item);
|
||||||
|
|
||||||
|
if (! $payment) {
|
||||||
|
Notification::make()->title('该商品已购买')->info()->send();
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
return redirect()->route('pay.checkout', $payment);
|
||||||
|
} catch (\Throwable $e) {
|
||||||
|
Notification::make()->title('购买失败')->body($e->getMessage())->danger()->send();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function isThemeMarketPurchased(string $index): bool
|
||||||
|
{
|
||||||
|
$item = $this->marketItems[(int) $index] ?? null;
|
||||||
|
|
||||||
|
if (! $item) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return app(MarketPurchaseService::class)->isPurchased(
|
||||||
|
app(MarketPurchaseService::class)->itemKey($item),
|
||||||
|
app(MarketPurchaseService::class)->itemType($item)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getMarketConfiguredProperty(): bool
|
||||||
|
{
|
||||||
|
return (bool) config('market.url');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,47 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Models;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||||
|
use Plugins\Neatstudio\Payment\Models\Payment;
|
||||||
|
|
||||||
|
class MarketPackage extends Model
|
||||||
|
{
|
||||||
|
protected $fillable = [
|
||||||
|
'item_key', 'item_type', 'title', 'price', 'status', 'payment_id', 'user_id',
|
||||||
|
];
|
||||||
|
|
||||||
|
public function payment(): BelongsTo
|
||||||
|
{
|
||||||
|
return $this->belongsTo(Payment::class);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function isPaid(): bool
|
||||||
|
{
|
||||||
|
return $this->status === 'paid';
|
||||||
|
}
|
||||||
|
|
||||||
|
public function markPaid(Payment $payment): void
|
||||||
|
{
|
||||||
|
$this->update([
|
||||||
|
'status' => 'paid',
|
||||||
|
'payment_id' => $payment->id,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 作为可购买实体(市场商品)的展示约定。
|
||||||
|
*/
|
||||||
|
public function payableLabel(): string
|
||||||
|
{
|
||||||
|
return '市场商品:'.$this->title;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function payableUrl(): ?string
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -19,6 +19,23 @@ class AppServiceProvider extends ServiceProvider
|
|||||||
//
|
//
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function registerMarketPurchaseListener(): void
|
||||||
|
{
|
||||||
|
$manager = app(\App\Blog\Support\PluginManager::class);
|
||||||
|
|
||||||
|
if (! $manager->isEnabled('neatstudio.payment')) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$manager->addAction('payment.paid', function (\Plugins\Neatstudio\Payment\Models\Payment $payment) {
|
||||||
|
$payable = $payment->payable;
|
||||||
|
|
||||||
|
if ($payable instanceof \App\Models\MarketPackage) {
|
||||||
|
$payable->markPaid($payment);
|
||||||
|
}
|
||||||
|
}, 10);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Bootstrap any application services.
|
* Bootstrap any application services.
|
||||||
*/
|
*/
|
||||||
@@ -29,6 +46,9 @@ class AppServiceProvider extends ServiceProvider
|
|||||||
$this->loadMigrationsFrom($path);
|
$this->loadMigrationsFrom($path);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 商城付费:支付成功标记市场商品已购(仅当支付插件启用时注册)
|
||||||
|
$this->registerMarketPurchaseListener();
|
||||||
|
|
||||||
// 前台页面缓存(匿名 GET,缓存含 SEO 注入后的最终 HTML)
|
// 前台页面缓存(匿名 GET,缓存含 SEO 注入后的最终 HTML)
|
||||||
app('router')->pushMiddlewareToGroup('web', \App\Blog\Support\PageCacheMiddleware::class);
|
app('router')->pushMiddlewareToGroup('web', \App\Blog\Support\PageCacheMiddleware::class);
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,32 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
|
return new class extends Migration
|
||||||
|
{
|
||||||
|
public function up(): void
|
||||||
|
{
|
||||||
|
Schema::create('market_packages', function (Blueprint $table) {
|
||||||
|
$table->id();
|
||||||
|
$table->string('item_key', 190);
|
||||||
|
$table->string('item_type', 20); // plugin / theme
|
||||||
|
$table->string('title', 255);
|
||||||
|
$table->unsignedBigInteger('price'); // 单位:分
|
||||||
|
$table->string('status', 20)->default('pending'); // pending / paid
|
||||||
|
$table->foreignId('payment_id')->nullable()->constrained()->nullOnDelete();
|
||||||
|
$table->foreignId('user_id')->nullable()->constrained()->nullOnDelete();
|
||||||
|
$table->timestamps();
|
||||||
|
|
||||||
|
$table->unique(['item_key', 'item_type']);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
Schema::dropIfExists('market_packages');
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -158,6 +158,31 @@ public function payableUrl(): ?string { return route('filament.admin.resources.
|
|||||||
- 后台「插件管理」→ 上传 ZIP 安装,或配置远程市场 `MARKET_URL`
|
- 后台「插件管理」→ 上传 ZIP 安装,或配置远程市场 `MARKET_URL`
|
||||||
- 内置插件(config/plugins.php enabled 列表)不可卸载,只能停用
|
- 内置插件(config/plugins.php enabled 列表)不可卸载,只能停用
|
||||||
|
|
||||||
|
## 远程市场契约
|
||||||
|
|
||||||
|
配置 `MARKET_URL` 后,后台「插件管理 / 主题管理」会请求 `{MARKET_URL}/items`(`market.token` 作为 Bearer Token),响应格式:
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"items": [
|
||||||
|
{
|
||||||
|
"name": "vendor.demo",
|
||||||
|
"type": "plugin",
|
||||||
|
"title": "演示插件",
|
||||||
|
"description": "描述",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"author": "作者",
|
||||||
|
"price": 9900,
|
||||||
|
"download_url": "https://example.com/vendor.demo.zip"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
- `name`:包键(插件为 `vendor.name`,主题为主题名),**购买授权按此键记录**
|
||||||
|
- `type`:`plugin` / `theme`——插件市场 Tab 只显示 plugin,主题市场 Tab 只显示 theme
|
||||||
|
- `price`:价格(分),`0` = 免费;`> 0` 显示「购买」按钮,走 `Payment` 订单(可 `payable` 为 `MarketPackage`),支付成功后标记已购,未购买无法安装
|
||||||
|
|
||||||
## 异步任务(配合 Workerman)
|
## 异步任务(配合 Workerman)
|
||||||
|
|
||||||
实现 `App\Blog\Jobs\AiJob` 接口并在 Workerman 常驻进程执行:
|
实现 `App\Blog\Jobs\AiJob` 接口并在 Workerman 常驻进程执行:
|
||||||
|
|||||||
@@ -66,19 +66,32 @@
|
|||||||
</div>
|
</div>
|
||||||
<div style="display:grid;grid-template-columns:repeat(auto-fill,minmax(300px,1fr));gap:1rem">
|
<div style="display:grid;grid-template-columns:repeat(auto-fill,minmax(300px,1fr));gap:1rem">
|
||||||
@forelse($marketItems as $index => $item)
|
@forelse($marketItems as $index => $item)
|
||||||
|
@php $marketPaid = $item['price'] > 0; @endphp
|
||||||
<x-filament::section :compact="true">
|
<x-filament::section :compact="true">
|
||||||
<div class="flex flex-col">
|
<div class="flex flex-col">
|
||||||
<x-filament::icon icon="heroicon-o-cloud-arrow-down" class="h-8 w-8 text-primary-600" />
|
<div class="flex items-start justify-between gap-3">
|
||||||
|
<x-filament::icon icon="heroicon-o-cloud-arrow-down" class="h-8 w-8 text-primary-600" />
|
||||||
|
@if($marketPaid)
|
||||||
|
<x-filament::badge color="warning">¥{{ number_format($item['price'] / 100, 2) }}</x-filament::badge>
|
||||||
|
@else
|
||||||
|
<x-filament::badge color="success">免费</x-filament::badge>
|
||||||
|
@endif
|
||||||
|
</div>
|
||||||
<h3 class="mt-3 text-base font-semibold">{{ $item['title'] ?? $item['name'] }}</h3>
|
<h3 class="mt-3 text-base font-semibold">{{ $item['title'] ?? $item['name'] }}</h3>
|
||||||
<p class="mt-1 flex-1 text-sm text-gray-500 dark:text-gray-400">{{ $item['description'] ?? '' }}</p>
|
<p class="mt-1 flex-1 text-sm text-gray-500 dark:text-gray-400">{{ $item['description'] ?? '' }}</p>
|
||||||
<p class="mt-2 text-xs text-gray-400">{{ $item['version'] ?? '' }} · {{ $item['author'] ?? '' }}</p>
|
<p class="mt-2 text-xs text-gray-400">{{ $item['version'] ?? '' }} · {{ $item['author'] ?? '' }}</p>
|
||||||
<div class="mt-4">
|
<div class="mt-4 flex flex-wrap gap-2">
|
||||||
<x-filament::button size="sm" color="primary" wire:click="installFromMarket({{ $index }})">安装</x-filament::button>
|
@if($marketPaid && ! $this->isMarketPurchased($index))
|
||||||
|
<x-filament::button size="sm" color="primary" wire:click="purchaseMarketItem({{ $index }})">购买</x-filament::button>
|
||||||
|
@elseif($marketPaid)
|
||||||
|
<x-filament::badge color="success">已购买</x-filament::badge>
|
||||||
|
@endif
|
||||||
|
<x-filament::button size="sm" color="gray" wire:click="installFromMarket({{ $index }})">安装</x-filament::button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</x-filament::section>
|
</x-filament::section>
|
||||||
@empty
|
@empty
|
||||||
<p class="text-sm text-gray-500">点击「刷新市场」拉取可用插件/主题</p>
|
<p class="text-sm text-gray-500">点击「刷新市场」拉取可用插件</p>
|
||||||
@endforelse
|
@endforelse
|
||||||
</div>
|
</div>
|
||||||
</x-filament::section>
|
</x-filament::section>
|
||||||
|
|||||||
@@ -64,4 +64,40 @@
|
|||||||
<x-filament::button type="submit" color="primary">安装</x-filament::button>
|
<x-filament::button type="submit" color="primary">安装</x-filament::button>
|
||||||
</form>
|
</form>
|
||||||
</x-filament::section>
|
</x-filament::section>
|
||||||
|
|
||||||
|
<x-filament::section heading="主题市场" :description="config('market.url') ?: '未配置远程市场(config/market.php 的 MARKET_URL),仅支持本地 ZIP 安装'">
|
||||||
|
<div class="mb-4">
|
||||||
|
<x-filament::button color="gray" wire:click="loadThemeMarket">刷新市场</x-filament::button>
|
||||||
|
</div>
|
||||||
|
<div style="display:grid;grid-template-columns:repeat(auto-fill,minmax(300px,1fr));gap:1rem">
|
||||||
|
@forelse($marketItems as $index => $item)
|
||||||
|
@php $marketPaid = $item['price'] > 0; @endphp
|
||||||
|
<x-filament::section :compact="true">
|
||||||
|
<div class="flex flex-col">
|
||||||
|
<div class="flex items-start justify-between gap-3">
|
||||||
|
<x-filament::icon icon="heroicon-o-swatch" class="h-8 w-8 text-primary-600" />
|
||||||
|
@if($marketPaid)
|
||||||
|
<x-filament::badge color="warning">¥{{ number_format($item['price'] / 100, 2) }}</x-filament::badge>
|
||||||
|
@else
|
||||||
|
<x-filament::badge color="success">免费</x-filament::badge>
|
||||||
|
@endif
|
||||||
|
</div>
|
||||||
|
<h3 class="mt-3 text-base font-semibold">{{ $item['title'] ?? $item['name'] }}</h3>
|
||||||
|
<p class="mt-1 flex-1 text-sm text-gray-500 dark:text-gray-400">{{ $item['description'] ?? '' }}</p>
|
||||||
|
<p class="mt-2 text-xs text-gray-400">{{ $item['version'] ?? '' }} · {{ $item['author'] ?? '' }}</p>
|
||||||
|
<div class="mt-4 flex flex-wrap gap-2">
|
||||||
|
@if($marketPaid && ! $this->isThemeMarketPurchased($index))
|
||||||
|
<x-filament::button size="sm" color="primary" wire:click="purchaseThemeMarketItem({{ $index }})">购买</x-filament::button>
|
||||||
|
@elseif($marketPaid)
|
||||||
|
<x-filament::badge color="success">已购买</x-filament::badge>
|
||||||
|
@endif
|
||||||
|
<x-filament::button size="sm" color="gray" wire:click="installFromThemeMarket({{ $index }})">安装</x-filament::button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</x-filament::section>
|
||||||
|
@empty
|
||||||
|
<p class="text-sm text-gray-500">点击「刷新市场」拉取可用主题</p>
|
||||||
|
@endforelse
|
||||||
|
</div>
|
||||||
|
</x-filament::section>
|
||||||
</x-filament-panels::page>
|
</x-filament-panels::page>
|
||||||
|
|||||||
@@ -0,0 +1,125 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Tests\Feature;
|
||||||
|
|
||||||
|
use App\Blog\Services\MarketPurchaseService;
|
||||||
|
use App\Models\MarketPackage;
|
||||||
|
use App\Models\Setting;
|
||||||
|
use App\Models\User;
|
||||||
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||||
|
use Plugins\Neatstudio\Payment\Models\Payment;
|
||||||
|
use RuntimeException;
|
||||||
|
use Tests\TestCase;
|
||||||
|
|
||||||
|
class MarketPurchaseTest extends TestCase
|
||||||
|
{
|
||||||
|
use RefreshDatabase;
|
||||||
|
|
||||||
|
private User $user;
|
||||||
|
|
||||||
|
private array $item = [
|
||||||
|
'name' => 'vendor.demo',
|
||||||
|
'type' => 'plugin',
|
||||||
|
'title' => '演示付费插件',
|
||||||
|
'description' => '演示',
|
||||||
|
'version' => '1.0.0',
|
||||||
|
'price' => 9900,
|
||||||
|
'download_url' => 'https://example.com/vendor.demo.zip',
|
||||||
|
];
|
||||||
|
|
||||||
|
protected function setUp(): void
|
||||||
|
{
|
||||||
|
parent::setUp();
|
||||||
|
|
||||||
|
$this->seed();
|
||||||
|
Setting::set('pay_sandbox', '1');
|
||||||
|
|
||||||
|
$this->user = User::create([
|
||||||
|
'name' => '买家',
|
||||||
|
'email' => 'buyer@test.com',
|
||||||
|
'password' => bcrypt('password'),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function service(): MarketPurchaseService
|
||||||
|
{
|
||||||
|
return app(MarketPurchaseService::class);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_purchase_creates_order_with_payable_package(): void
|
||||||
|
{
|
||||||
|
$this->actingAs($this->user);
|
||||||
|
|
||||||
|
$payment = $this->service()->purchase($this->item);
|
||||||
|
|
||||||
|
$this->assertNotNull($payment);
|
||||||
|
$this->assertInstanceOf(MarketPackage::class, $payment->payable);
|
||||||
|
$this->assertSame('pending', $payment->payable->status);
|
||||||
|
$this->assertSame(9900, $payment->amount);
|
||||||
|
$this->assertSame('市场商品:演示付费插件', $payment->payable_label);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_sandbox_payment_marks_package_paid(): void
|
||||||
|
{
|
||||||
|
$this->actingAs($this->user);
|
||||||
|
|
||||||
|
$payment = $this->service()->purchase($this->item);
|
||||||
|
$this->actingAs($this->user)->post('/pay/sandbox/'.$payment->order_no.'/confirm');
|
||||||
|
|
||||||
|
$package = MarketPackage::query()->first();
|
||||||
|
$this->assertTrue($package->isPaid());
|
||||||
|
$this->assertSame($payment->id, $package->payment_id);
|
||||||
|
$this->assertTrue($this->service()->isPurchased('vendor.demo', 'plugin'));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_purchase_returns_null_when_already_paid(): void
|
||||||
|
{
|
||||||
|
$this->actingAs($this->user);
|
||||||
|
|
||||||
|
$this->service()->purchase($this->item);
|
||||||
|
$package = MarketPackage::query()->first();
|
||||||
|
$package->markPaid($package->payment ?? Payment::first() ?? Payment::create([
|
||||||
|
'order_no' => 'X'.uniqid(),
|
||||||
|
'user_id' => $this->user->id,
|
||||||
|
'subject' => '占位',
|
||||||
|
'amount' => 0,
|
||||||
|
'status' => 'paid',
|
||||||
|
'paid_at' => now(),
|
||||||
|
]));
|
||||||
|
|
||||||
|
$this->assertNull($this->service()->purchase($this->item));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_assert_can_install_blocks_unpaid_paid_item(): void
|
||||||
|
{
|
||||||
|
$this->actingAs($this->user);
|
||||||
|
|
||||||
|
$this->expectException(RuntimeException::class);
|
||||||
|
$this->service()->assertCanInstall($this->item);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_free_item_needs_no_purchase(): void
|
||||||
|
{
|
||||||
|
$this->actingAs($this->user);
|
||||||
|
|
||||||
|
$free = ['name' => 'vendor.free', 'type' => 'plugin', 'title' => '免费插件', 'price' => 0];
|
||||||
|
|
||||||
|
$this->assertFalse($this->service()->requiresPayment($free));
|
||||||
|
$this->service()->assertCanInstall($free); // 不抛异常
|
||||||
|
|
||||||
|
$this->assertTrue(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_purchase_requires_payment_plugin(): void
|
||||||
|
{
|
||||||
|
$this->actingAs($this->user);
|
||||||
|
|
||||||
|
app(\App\Blog\Support\PluginManager::class)->disable('neatstudio.membership');
|
||||||
|
app(\App\Blog\Support\PluginManager::class)->disable('neatstudio.payment');
|
||||||
|
|
||||||
|
$this->expectException(RuntimeException::class);
|
||||||
|
$this->service()->purchase($this->item);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user