-
-
Notifications
You must be signed in to change notification settings - Fork 108
Expand file tree
/
Copy pathViewSubscriptionItem.php
More file actions
115 lines (105 loc) · 4.89 KB
/
ViewSubscriptionItem.php
File metadata and controls
115 lines (105 loc) · 4.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php
namespace App\Filament\Resources\SubscriptionItemResource\Pages;
use App\Enums\Subscription;
use App\Enums\Subscription as SubscriptionEnum;
use App\Filament\Resources\SubscriptionItemResource;
use App\Filament\Resources\SubscriptionResource;
use App\Filament\Resources\UserResource;
use App\Jobs\UpsertLicenseFromAnystackLicense;
use App\Models\License;
use App\Services\Anystack\Anystack;
use Filament\Actions;
use Filament\Forms\Components\TextInput;
use Filament\Infolists\Components;
use Filament\Notifications\Notification;
use Filament\Resources\Pages\ViewRecord;
use Filament\Schemas\Components\Section;
use Filament\Schemas\Schema;
use Laravel\Cashier\SubscriptionItem;
/**
* @property ?SubscriptionItem $record
*/
class ViewSubscriptionItem extends ViewRecord
{
protected static string $resource = SubscriptionItemResource::class;
protected function getHeaderActions(): array
{
return [
Actions\Action::make('import_from_anystack')
->label('Import Related License')
->icon('heroicon-o-arrow-down-tray')
->color('primary')
->form([
TextInput::make('anystack_id')
->label('Anystack License UUID')
->placeholder('Enter the Anystack license UUID')
->required()
->uuid()
->helperText('Paste the license UUID from Anystack to import it into the system.'),
])
->action(function (array $data): void {
try {
$response = Anystack::api()
->license($data['anystack_id'], Subscription::Mini->anystackProductId()) // any plan's product id will work
->retrieve();
$licenseData = $response->json('data');
dispatch_sync(new UpsertLicenseFromAnystackLicense($licenseData));
$license = License::where('anystack_id', $data['anystack_id'])->firstOrFail();
$license->update(['subscription_item_id' => $this->record->id]);
Notification::make()
->title('License Imported')
->body('The license was imported.')
->success()
->send();
} catch (\Exception $e) {
Notification::make()
->title('Error importing license')
->body('Failed to import license from Anystack: '.$e->getMessage())
->danger()
->send();
}
}),
Actions\Action::make('viewOnStripe')
->label('View on Stripe')
->color('gray')
->icon('heroicon-o-arrow-top-right-on-square')
->url(fn () => 'https://dashboard.stripe.com/subscriptions/'.$this->record->subscription->stripe_id)
->openUrlInNewTab(),
];
}
public function infolist(Schema $schema): Schema
{
return $schema
->schema([
Section::make('Subscription Item Details')
->schema([
Components\TextEntry::make('subscription.id')
->label('Subscription ID')
->url(fn ($record) => SubscriptionResource::getUrl('view', ['record' => $record->subscription_id])),
Components\TextEntry::make('subscription.user.email')
->label('User')
->url(fn ($record) => UserResource::getUrl('edit', ['record' => $record->subscription_id])),
Components\TextEntry::make('stripe_id')
->label('Stripe ID')
->copyable(),
Components\TextEntry::make('stripe_product')
->copyable(),
Components\TextEntry::make('stripe_price')
->label('Plan')
->formatStateUsing(function ($state) {
try {
$plan = SubscriptionEnum::fromStripePriceId($state);
return $plan->name().' ('.$state.')';
} catch (\Exception $e) {
return $state;
}
}),
Components\TextEntry::make('quantity'),
Components\TextEntry::make('created_at')
->dateTime(),
Components\TextEntry::make('updated_at')
->dateTime(),
])->columns(2),
]);
}
}