added crypto pm and activation key system

This commit is contained in:
Gitea
2025-05-16 11:24:08 +05:30
parent 23b5a45d0b
commit 93515e7845
11 changed files with 454 additions and 13 deletions

View File

@@ -35,7 +35,10 @@ class Dashboard extends Component
$user = auth()->user();
$userId = $user->id;
if ($user->subscribed()) {
$subscription = $user->subscriptions()->where(['stripe_status' => 'active'])->orderByDesc('updated_at')->first();
$subscription = $user->subscriptions()
//->where(['stripe_status' => 'active'])
->orderByDesc('updated_at')
->first();
if ($subscription !== null) {
$subscriptionId = $subscription->stripe_id;
$cacheKey = "stripe_check_executed_user_{$userId}_{$subscriptionId}";
@@ -74,7 +77,7 @@ class Dashboard extends Component
]);
}
}
Cache::put($cacheKey, true, now()->addHour());
Cache::put($cacheKey, true, now()->addMinute());
} catch (Exception $exception) {
\Log::error($exception->getMessage());
}
@@ -176,7 +179,7 @@ class Dashboard extends Component
$userPriceID = $result['items'][0]['stripe_price'];
$subscriptionEnd = $result['ends_at'];
$planName = null; // Default value if not found
$planName = null;
foreach (config('app.plans') as $plan) {
if ($plan['pricing_id'] === $userPriceID) {

View File

@@ -2,11 +2,14 @@
namespace App\Livewire\Dashboard;
use App\Models\ActivationKey;
use App\Models\Plan;
use Livewire\Component;
class Pricing extends Component
{
public $plans;
public $activation_key;
public function mount(): void
{
@@ -18,6 +21,70 @@ class Pricing extends Component
$this->redirect(route('checkout', $pricing_id));
}
public function activateKey(): void
{
$this->validate([
'activation_key' => 'required|alpha_num|max:30',
], [
'activation_key.required' => 'You must enter an activation key.',
'activation_key.alpha_num' => 'The activation key may only contain letters and numbers (no special characters).',
'activation_key.max' => 'The activation key must not exceed 30 characters.',
]);
$trimmedKey = trim($this->activation_key);
$activation = ActivationKey::where('activation_key', $trimmedKey)
->where('is_activated', false)
->first();
if ($activation) {
if ($activation->price_id !== null) {
$result = $this->addSubscription($activation->price_id);
}
if ($result === true) {
$activation->is_activated = true;
$activation->user_id = auth()->id();
$activation->save();
session()->flash('success', 'Activation key is valid and has been activated. Refresh page to see changes.');
$this->reset('activation_key');
} else {
session()->flash('error', 'Something went wrong. Kindly drop a mail at contact@zemail.me to activate your subscription manually.');
}
} else {
session()->flash('error', 'Invalid or already activated key.');
}
}
private function addSubscription($price_id): bool
{
try {
$plan = Plan::where('pricing_id', $price_id)->firstOrFail();
$user = auth()->user();
$user->createOrGetStripeCustomer();
$user->updateStripeCustomer([
'address' => [
'postal_code' => '10001',
'country' => 'US',
],
'name' => $user->name,
'email' => $user->email,
]);
$user->creditBalance($plan->price * 100, 'Premium Top-up for plan: ' . $plan->name);
$balance = $user->balance();
$user->newSubscription('default', $plan->pricing_id)->create();
if ($plan->monthly_billing == 1) {
$ends_at = now()->addMonth();
} else {
$ends_at = now()->addYear();
}
$user->subscription('default')->cancelAt($ends_at);
return true;
} catch (\Exception $e) {
\Log::error($e->getMessage());
return false;
}
}
public function render()
{
return view('livewire.dashboard.pricing');