mirror of
https://github.com/VueFileManager/vuefilemanager.git
synced 2026-05-13 16:55:01 +00:00
it_register_user_when_metered_billing_is_active
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
return [
|
||||
'driver' => env('SUBSCRIPTION_DRIVER', 'stripe'),
|
||||
|
||||
/*
|
||||
* Activate drivers to handle subscription
|
||||
*/
|
||||
'available_drivers' => [
|
||||
'paystack',
|
||||
'paypal',
|
||||
'stripe',
|
||||
],
|
||||
'metered_billing' => [
|
||||
'settlement_period' => 30,
|
||||
/*
|
||||
* Drivers which have native support for metered billing.
|
||||
* This native support doesn't use subscription package credit system, but prefer
|
||||
* auto-renew and maintain metered subscription on behalf of external service.
|
||||
*/
|
||||
'native_support' => [
|
||||
'stripe',
|
||||
],
|
||||
],
|
||||
'paystack' => [
|
||||
/*
|
||||
* List of allowed ip address to verify paystack webhook request
|
||||
*/
|
||||
'allowed_ips' => [
|
||||
'52.214.14.220',
|
||||
'52.49.173.169',
|
||||
'52.31.139.75',
|
||||
],
|
||||
],
|
||||
|
||||
/*
|
||||
* Get gateway credentials
|
||||
*/
|
||||
'credentials' => [
|
||||
'stripe' => [
|
||||
'secret' => env('STRIPE_SECRET_KEY'),
|
||||
'public_key' => env('STRIPE_PUBLIC_KEY'),
|
||||
],
|
||||
'paystack' => [
|
||||
'secret' => env('PAYSTACK_SECRET'),
|
||||
'public_key' => env('PAYSTACK_PUBLIC_KEY'),
|
||||
],
|
||||
'paypal' => [
|
||||
'id' => env('PAYPAL_CLIENT_ID'),
|
||||
'secret' => env('PAYPAL_CLIENT_SECRET'),
|
||||
'webhook_id' => env('PAYPAL_WEBHOOK_ID'),
|
||||
],
|
||||
],
|
||||
];
|
||||
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace App\Users\Actions;
|
||||
|
||||
use App\Users\Models\User;
|
||||
use VueFileManager\Subscription\Domain\Plans\Models\Plan;
|
||||
|
||||
class AutoSubscribeForMeteredBillingAction
|
||||
{
|
||||
public function __invoke(User $user)
|
||||
{
|
||||
// Get metered billing plan
|
||||
$plan = Plan::where('status', 'active')
|
||||
->where('type', 'metered')
|
||||
->first();
|
||||
|
||||
// TODO: add bonus
|
||||
|
||||
// Create user balance
|
||||
$user->balance()->create([
|
||||
'amount' => 0,
|
||||
'currency' => $plan->currency,
|
||||
]);
|
||||
|
||||
// Create user subscription
|
||||
$user->subscription()->create([
|
||||
'plan_id' => $plan->id,
|
||||
'name' => $plan->name,
|
||||
'status' => 'active',
|
||||
'renews_at' => now()->addDays(config('subscription.metered_billing.settlement_period')),
|
||||
'type' => 'pre-paid',
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -1,16 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace App\Users\Actions;
|
||||
|
||||
use App\Users\Models\User;
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Auth\Events\Registered;
|
||||
use Illuminate\Contracts\Auth\StatefulGuard;
|
||||
use VueFileManager\Subscription\Domain\Plans\Models\Plan;
|
||||
|
||||
class CreateNewUserAction extends Controller
|
||||
{
|
||||
public function __construct(
|
||||
protected StatefulGuard $guard
|
||||
) {
|
||||
protected StatefulGuard $guard,
|
||||
protected AutoSubscribeForMeteredBillingAction $autoSubscribeForMeteredBilling,
|
||||
)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -19,11 +23,11 @@ class CreateNewUserAction extends Controller
|
||||
public function __invoke($data)
|
||||
{
|
||||
$settings = get_settings([
|
||||
'registration', 'user_verification',
|
||||
'registration', 'user_verification', 'subscription_type',
|
||||
]);
|
||||
|
||||
// Check if account registration is enabled
|
||||
if (! intval($settings['registration'])) {
|
||||
if (!intval($settings['registration'])) {
|
||||
abort(401);
|
||||
}
|
||||
|
||||
@@ -34,22 +38,25 @@ class CreateNewUserAction extends Controller
|
||||
'email' => $data->email,
|
||||
]);
|
||||
|
||||
$user
|
||||
->settings()
|
||||
->create([
|
||||
'name' => $data->name,
|
||||
'avatar' => $data->avatar,
|
||||
]);
|
||||
$user->settings()->create([
|
||||
'name' => $data->name,
|
||||
'avatar' => $data->avatar,
|
||||
]);
|
||||
|
||||
// Subscribe user for metered billing
|
||||
if ($settings['subscription_type'] === 'metered') {
|
||||
($this->autoSubscribeForMeteredBilling)($user);
|
||||
}
|
||||
|
||||
// Mark as verified if verification is disabled
|
||||
if (! $data->password || ! intval($settings['user_verification'])) {
|
||||
if (!$data->password || !intval($settings['user_verification'])) {
|
||||
$user->markEmailAsVerified();
|
||||
}
|
||||
|
||||
event(new Registered($user));
|
||||
|
||||
// Log in if verification is disabled
|
||||
if (! $data->password || ! intval($settings['user_verification'])) {
|
||||
if (!$data->password || !intval($settings['user_verification'])) {
|
||||
$this->guard->login($user);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ use Tests\TestCase;
|
||||
use App\Users\Models\User;
|
||||
use Domain\Files\Models\File;
|
||||
use Domain\Sharing\Models\Share;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Domain\Settings\Models\Setting;
|
||||
|
||||
class MeteredBillingRestrictionsTest extends TestCase
|
||||
{
|
||||
@@ -13,8 +13,9 @@ class MeteredBillingRestrictionsTest extends TestCase
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
DB::table('settings')->insert([
|
||||
Setting::updateOrCreate([
|
||||
'name' => 'subscription_type',
|
||||
], [
|
||||
'value' => 'metered',
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
<?php
|
||||
namespace Tests\App\Socialite;
|
||||
|
||||
use DB;
|
||||
use Storage;
|
||||
use Tests\TestCase;
|
||||
use App\Users\Models\User;
|
||||
@@ -15,7 +14,7 @@ class SocialiteTest extends TestCase
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function it_socialite_redirect()
|
||||
public function socialite_redirect_user()
|
||||
{
|
||||
$response = $this->get('api/socialite/google/redirect');
|
||||
|
||||
@@ -25,19 +24,8 @@ class SocialiteTest extends TestCase
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function it_socialite_callback()
|
||||
public function socialite_execute_provider_callback()
|
||||
{
|
||||
// Set default settings
|
||||
DB::table('settings')->insert([
|
||||
[
|
||||
'name' => 'registration',
|
||||
'value' => 1,
|
||||
], [
|
||||
'name' => 'storage_default',
|
||||
'value' => 5,
|
||||
],
|
||||
]);
|
||||
|
||||
// Create fake image
|
||||
$fakeImage = UploadedFile::fake()
|
||||
->image('fake-avatar.jpg');
|
||||
|
||||
@@ -9,30 +9,28 @@ use Domain\Settings\Models\Setting;
|
||||
use Illuminate\Support\Facades\Password;
|
||||
use App\Users\Notifications\ResetPassword;
|
||||
use Illuminate\Auth\Notifications\VerifyEmail;
|
||||
use VueFileManager\Subscription\Domain\Plans\Models\Plan;
|
||||
|
||||
class SignFlowTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function it_register_user()
|
||||
public function it_create_user_from_register_form()
|
||||
{
|
||||
collect([
|
||||
[
|
||||
'name' => 'default_max_storage_amount',
|
||||
'value' => 12,
|
||||
],
|
||||
[
|
||||
'name' => 'registration',
|
||||
'value' => 1,
|
||||
],
|
||||
[
|
||||
'name' => 'user_verification',
|
||||
'value' => 1,
|
||||
],
|
||||
[
|
||||
'name' => 'default_max_storage_amount',
|
||||
'value' => 10,
|
||||
],
|
||||
])->each(function ($setting) {
|
||||
Setting::create([
|
||||
'name' => $setting['name'],
|
||||
Setting::updateOrCreate([
|
||||
'name' => $setting['name'],
|
||||
], [
|
||||
'value' => $setting['value'],
|
||||
]);
|
||||
});
|
||||
@@ -44,14 +42,17 @@ class SignFlowTest extends TestCase
|
||||
'name' => 'John Doe',
|
||||
])->assertStatus(201);
|
||||
|
||||
$this->assertDatabaseHas('users', [
|
||||
'email' => 'john@doe.com',
|
||||
'email_verified_at' => null,
|
||||
])->assertDatabaseHas('user_settings', [
|
||||
'name' => 'John Doe',
|
||||
])->assertDatabaseHas('user_limitations', [
|
||||
'max_storage_amount' => 12,
|
||||
]);
|
||||
$this
|
||||
->assertDatabaseHas('users', [
|
||||
'email' => 'john@doe.com',
|
||||
'email_verified_at' => null,
|
||||
])
|
||||
->assertDatabaseHas('user_settings', [
|
||||
'name' => 'John Doe',
|
||||
])
|
||||
->assertDatabaseHas('user_limitations', [
|
||||
'max_storage_amount' => 10,
|
||||
]);
|
||||
|
||||
Storage::disk('local')
|
||||
->assertExists('files/' . User::first()->id);
|
||||
@@ -59,13 +60,60 @@ class SignFlowTest extends TestCase
|
||||
Notification::assertTimesSent(1, VerifyEmail::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function it_register_user_when_metered_billing_is_active()
|
||||
{
|
||||
// Seed default settings
|
||||
Setting::updateOrCreate([
|
||||
'name' => 'subscription_type',
|
||||
], [
|
||||
'value' => 'metered',
|
||||
]);
|
||||
|
||||
// Create metered plan
|
||||
$plan = Plan::factory()
|
||||
->create([
|
||||
'status' => 'active',
|
||||
'type' => 'metered',
|
||||
'currency' => 'USD',
|
||||
]);
|
||||
|
||||
$this->postJson('api/register', [
|
||||
'email' => 'john@doe.com',
|
||||
'password' => 'SecretPassword',
|
||||
'password_confirmation' => 'SecretPassword',
|
||||
'name' => 'John Doe',
|
||||
])->assertStatus(201);
|
||||
|
||||
$this
|
||||
->assertDatabaseHas('users', [
|
||||
'email' => 'john@doe.com',
|
||||
])
|
||||
->assertDatabaseHas('subscriptions', [
|
||||
'status' => 'active',
|
||||
'name' => $plan->name,
|
||||
'ends_at' => null,
|
||||
'renews_at' => now()->addDays(config('subscription.metered_billing.settlement_period')),
|
||||
])
|
||||
->assertDatabaseHas('balances', [
|
||||
'currency' => 'USD',
|
||||
'amount' => 0,
|
||||
])
|
||||
->assertDatabaseHas('user_settings', [
|
||||
'name' => 'John Doe',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function it_try_register_when_registration_is_disabled()
|
||||
{
|
||||
Setting::create([
|
||||
Setting::updateOrCreate([
|
||||
'name' => 'registration',
|
||||
], [
|
||||
'value' => 0,
|
||||
]);
|
||||
|
||||
|
||||
@@ -46,6 +46,18 @@ abstract class TestCase extends BaseTestCase
|
||||
'name' => 'language',
|
||||
'value' => 'en',
|
||||
],
|
||||
[
|
||||
'name' => 'user_verification',
|
||||
'value' => 0,
|
||||
],
|
||||
[
|
||||
'name' => 'registration',
|
||||
'value' => 1,
|
||||
],
|
||||
[
|
||||
'name' => 'subscription_type',
|
||||
'value' => null,
|
||||
],
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user