added it_get_setup_intent, it_upgrade_plan, it_cancel_subscription test

This commit is contained in:
Peter Papp
2021-03-04 12:48:51 +01:00
parent b1c15bb6a7
commit 69b72d24a9
15 changed files with 253 additions and 86 deletions
+3 -4
View File
@@ -2,10 +2,10 @@
namespace App\Console\Commands;
use App\Page;
use App\Models\Page;
use App\Services\SetupService;
use App\Setting;
use App\User;
use App\Models\Setting;
use App\Models\User;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Hash;
use Faker;
@@ -66,7 +66,6 @@ class SetupDevEnvironment extends Command
{
$user = User::forceCreate([
'role' => 'admin',
'name' => 'John Doe',
'email' => 'john@doe.com',
'password' => Hash::make('secret'),
]);
@@ -7,6 +7,7 @@ use App\Http\Requests\Subscription\StoreUpgradeAccountRequest;
use App\Http\Resources\UserSubscription;
use App\Http\Tools\Demo;
use App\Invoice;
use App\Models\User;
use App\Services\StripeService;
use Auth;
use Cartalyst\Stripe\Exception\CardErrorException;
@@ -14,6 +15,7 @@ use Illuminate\Contracts\Routing\ResponseFactory;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Laravel\Cashier\Exceptions\IncompletePayment;
use Laravel\Cashier\Subscription;
use Symfony\Component\HttpKernel\Exception\HttpException;
class SubscriptionController extends Controller
@@ -34,11 +36,12 @@ class SubscriptionController extends Controller
*
* @return \Stripe\SetupIntent
*/
public function stripe_setup_intent()
public function setup_intent()
{
$user = Auth::user();
return $this->stripe->getSetupIntent($user);
return $this->stripe
->getSetupIntent(
Auth::user()
);
}
/**
@@ -50,7 +53,7 @@ class SubscriptionController extends Controller
{
$user = Auth::user();
if (! $user->subscription('main')) {
if (!$user->subscription('main')) {
return abort(204, 'User don\'t have any subscription');
}
@@ -113,7 +116,7 @@ class SubscriptionController extends Controller
*/
public function cancel()
{
$user = Auth::user();
$user = User::find(Auth::id());
// Check if is demo
if (is_demo($user->id)) {
+18 -10
View File
@@ -3,9 +3,8 @@
namespace App\Http\Controllers;
use App\Services\StripeService;
use App\Setting;
use App\User;
use Illuminate\Http\Request;
use App\Models\Setting;
use App\Models\User;
use Laravel\Cashier\Http\Controllers\WebhookController as CashierController;
@@ -33,13 +32,19 @@ class WebhookController extends CashierController
}
// Get user
$user = User::where('stripe_id', $payload['data']['object']['customer'])->firstOrFail();
$user = User::where('stripe_id', $payload['data']['object']['customer'])
->firstOrFail();
// Get default storage capacity
$default_storage = Setting::where('name', 'storage_default')->first();
$default_storage = Setting::where('name', 'storage_default')
->first();
// Update storage capacity
$user->settings()->update(['storage_capacity' => $default_storage->value]);
$user
->settings()
->update([
'storage_capacity' => $default_storage->value
]);
return $this->successMethod();
}
@@ -53,15 +58,18 @@ class WebhookController extends CashierController
public function handleInvoicePaymentSucceeded($payload)
{
// Get user
$user = User::where('stripe_id', $payload['data']['object']['customer'])->firstOrFail();
$user = User::where('stripe_id', $payload['data']['object']['customer'])
->firstOrFail();
// Get requested plan
$plan = $this->stripe->getPlan($user->subscription('main')->stripe_plan);
// Update user storage limit
$user->settings()->update([
'storage_capacity' => $plan['product']['metadata']['capacity']
]);
$user
->settings()
->update([
'storage_capacity' => $plan['product']['metadata']['capacity']
]);
return $this->successMethod();
}
+7
View File
@@ -21,5 +21,12 @@ class Page extends Model
'visibility',
];
public $fillable = [
'slug',
'title',
'visibility',
'content',
];
public $timestamps = false;
}
+7 -7
View File
@@ -173,13 +173,13 @@ class User extends Authenticatable
public function setBilling($billing)
{
$this->settings()->update([
'billing_address' => $billing['billing_address'],
'billing_city' => $billing['billing_city'],
'billing_country' => $billing['billing_country'],
'billing_name' => $billing['billing_name'],
'billing_phone_number' => $billing['billing_phone_number'],
'billing_postal_code' => $billing['billing_postal_code'],
'billing_state' => $billing['billing_state'],
'address' => $billing['billing_address'],
'city' => $billing['billing_city'],
'country' => $billing['billing_country'],
'name' => $billing['billing_name'],
'phone_number' => $billing['billing_phone_number'],
'postal_code' => $billing['billing_postal_code'],
'state' => $billing['billing_state'],
]);
return $this->settings;
+10 -23
View File
@@ -4,7 +4,6 @@
namespace App\Services;
use App\Models\User;
use Artisan;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use Laravel\Cashier\Exceptions\IncompletePayment;
@@ -22,18 +21,6 @@ class StripeService
$this->stripe = Stripe::make(config('cashier.secret'), '2020-03-02');
}
/**
* Get Stripe account details
*
* @return mixed
*/
public function getAccountDetails()
{
$account = $this->stripe->account()->details();
return $account;
}
/**
* Get setup intent
*
@@ -55,9 +42,9 @@ class StripeService
*/
public function getTaxRates()
{
$tax_rates = $this->stripe->taxRates()->all();
return $tax_rates['data'];
return $this->stripe
->taxRates()
->all()['data'];
}
/**
@@ -171,14 +158,14 @@ class StripeService
public function updateCustomerDetails($user)
{
$user->updateStripeCustomer([
'name' => $user->settings->billing_name,
'phone' => $user->settings->billing_phone_number,
'name' => $user->settings->name,
'phone' => $user->settings->phone_number,
'address' => [
'line1' => $user->settings->billing_address,
'city' => $user->settings->billing_city,
'country' => $user->settings->billing_country,
'postal_code' => $user->settings->billing_postal_code,
'state' => $user->settings->billing_state,
'line1' => $user->settings->address,
'city' => $user->settings->city,
'country' => $user->settings->country,
'postal_code' => $user->settings->postal_code,
'state' => $user->settings->state,
]
]);
}