2023-08-17 13:19:37 +00:00
|
|
|
<?php
|
|
|
|
|
2023-12-07 18:06:32 +00:00
|
|
|
namespace App\Livewire\Subscription;
|
2023-08-17 13:19:37 +00:00
|
|
|
|
|
|
|
use Illuminate\Support\Facades\Http;
|
|
|
|
use Livewire\Component;
|
|
|
|
|
|
|
|
class Actions extends Component
|
|
|
|
{
|
|
|
|
public function cancel()
|
|
|
|
{
|
|
|
|
try {
|
2023-08-22 15:44:49 +00:00
|
|
|
$subscription_id = currentTeam()->subscription->lemon_subscription_id;
|
2023-08-17 13:19:37 +00:00
|
|
|
if (!$subscription_id) {
|
|
|
|
throw new \Exception('No subscription found');
|
|
|
|
}
|
|
|
|
$response = Http::withHeaders([
|
|
|
|
'Accept' => 'application/vnd.api+json',
|
|
|
|
'Content-Type' => 'application/vnd.api+json',
|
2023-08-23 14:40:59 +00:00
|
|
|
'Authorization' => 'Bearer ' . config('subscription.lemon_squeezy_api_key'),
|
2023-08-17 13:19:37 +00:00
|
|
|
])->delete('https://api.lemonsqueezy.com/v1/subscriptions/' . $subscription_id);
|
|
|
|
$json = $response->json();
|
|
|
|
if ($response->failed()) {
|
|
|
|
$error = data_get($json, 'errors.0.status');
|
|
|
|
if ($error === '404') {
|
|
|
|
throw new \Exception('Subscription not found.');
|
|
|
|
}
|
|
|
|
throw new \Exception(data_get($json, 'errors.0.title', 'Something went wrong. Please try again later.'));
|
|
|
|
} else {
|
2023-12-07 18:06:32 +00:00
|
|
|
$this->dispatch('success', 'Subscription cancelled successfully. Reloading in 5s.');
|
|
|
|
$this->dispatch('reloadWindow', 5000);
|
2023-08-17 13:19:37 +00:00
|
|
|
}
|
2023-09-11 15:36:30 +00:00
|
|
|
} catch (\Throwable $e) {
|
2023-09-15 13:34:25 +00:00
|
|
|
return handleError($e, $this);
|
2023-08-17 13:19:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
public function resume()
|
|
|
|
{
|
|
|
|
try {
|
2023-08-22 15:44:49 +00:00
|
|
|
$subscription_id = currentTeam()->subscription->lemon_subscription_id;
|
2023-08-17 13:19:37 +00:00
|
|
|
if (!$subscription_id) {
|
|
|
|
throw new \Exception('No subscription found');
|
|
|
|
}
|
|
|
|
$response = Http::withHeaders([
|
|
|
|
'Accept' => 'application/vnd.api+json',
|
|
|
|
'Content-Type' => 'application/vnd.api+json',
|
2023-08-23 14:40:59 +00:00
|
|
|
'Authorization' => 'Bearer ' . config('subscription.lemon_squeezy_api_key'),
|
2023-08-17 13:19:37 +00:00
|
|
|
])->patch('https://api.lemonsqueezy.com/v1/subscriptions/' . $subscription_id, [
|
|
|
|
'data' => [
|
|
|
|
'type' => 'subscriptions',
|
|
|
|
'id' => $subscription_id,
|
|
|
|
'attributes' => [
|
|
|
|
'cancelled' => false,
|
|
|
|
],
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
$json = $response->json();
|
|
|
|
if ($response->failed()) {
|
|
|
|
$error = data_get($json, 'errors.0.status');
|
|
|
|
if ($error === '404') {
|
|
|
|
throw new \Exception('Subscription not found.');
|
|
|
|
}
|
|
|
|
throw new \Exception(data_get($json, 'errors.0.title', 'Something went wrong. Please try again later.'));
|
|
|
|
} else {
|
2023-12-07 18:06:32 +00:00
|
|
|
$this->dispatch('success', 'Subscription resumed successfully. Reloading in 5s.');
|
|
|
|
$this->dispatch('reloadWindow', 5000);
|
2023-08-17 13:19:37 +00:00
|
|
|
}
|
2023-09-11 15:36:30 +00:00
|
|
|
} catch (\Throwable $e) {
|
2023-09-15 13:34:25 +00:00
|
|
|
return handleError($e, $this);
|
2023-08-17 13:19:37 +00:00
|
|
|
}
|
|
|
|
}
|
2023-08-24 14:14:09 +00:00
|
|
|
public function stripeCustomerPortal() {
|
|
|
|
$session = getStripeCustomerPortalSession(currentTeam());
|
|
|
|
redirect($session->url);
|
|
|
|
}
|
2023-08-17 13:19:37 +00:00
|
|
|
}
|