diff --git a/src/Http/Middleware/Billable.php b/src/Http/Middleware/Billable.php index 72a9db39..72d2c56e 100644 --- a/src/Http/Middleware/Billable.php +++ b/src/Http/Middleware/Billable.php @@ -5,7 +5,9 @@ use Closure; use Illuminate\Http\Request; use Illuminate\Support\Facades\Redirect; +use Osiset\ShopifyApp\Contracts\Queries\Shop as ShopQuery; use Osiset\ShopifyApp\Contracts\ShopModel as IShopModel; +use Osiset\ShopifyApp\Objects\Values\ShopDomain; use Osiset\ShopifyApp\Util; /** @@ -13,6 +15,23 @@ */ class Billable { + /** + * The shop querier. + * + * @var ShopQuery + */ + protected $shopQuery; + + /** + * @param ShopQuery $shopQuery The shop querier. + * + * @return void + */ + public function __construct(ShopQuery $shopQuery) + { + $this->shopQuery = $shopQuery; + } + /** * Checks if a shop has paid for access. * @@ -25,7 +44,7 @@ public function handle(Request $request, Closure $next) { if (Util::getShopifyConfig('billing_enabled') === true) { /** @var $shop IShopModel */ - $shop = auth()->user(); + $shop = $this->shopQuery->getByDomain(ShopDomain::fromNative($request->get('shop'))); if (! $shop->plan && ! $shop->isFreemium() && ! $shop->isGrandfathered()) { // They're not grandfathered in, and there is no charge or charge was declined... redirect to billing return Redirect::route( diff --git a/tests/Http/Middleware/BillableTest.php b/tests/Http/Middleware/BillableTest.php index ff3de70b..92088944 100644 --- a/tests/Http/Middleware/BillableTest.php +++ b/tests/Http/Middleware/BillableTest.php @@ -3,6 +3,7 @@ namespace Osiset\ShopifyApp\Test\Http\Middleware; use Illuminate\Auth\AuthManager; +use Illuminate\Support\Facades\Request; use Osiset\ShopifyApp\Http\Middleware\Billable as BillableMiddleware; use Osiset\ShopifyApp\Storage\Models\Charge; use Osiset\ShopifyApp\Storage\Models\Plan; @@ -25,13 +26,15 @@ public function setUp(): void public function testEnabledBillingWithUnpaidShop(): void { + $currentRequest = Request::instance(); + $newRequest = $currentRequest->duplicate(['shop' => 'mystore123.myshopify.com']); + // Enable billing and set a shop - $shop = factory($this->model)->create(); - $this->auth->login($shop); $this->app['config']->set('shopify-app.billing_enabled', true); + factory($this->model)->create(['name' => 'mystore123.myshopify.com']); // Run the middleware - $result = $this->runMiddleware(BillableMiddleware::class); + $result = $this->runMiddleware(BillableMiddleware::class, $newRequest); // Assert it was not called and redirect happened $this->assertFalse($result[0]); @@ -40,21 +43,25 @@ public function testEnabledBillingWithUnpaidShop(): void public function testEnabledBillingWithPaidShop(): void { + $currentRequest = Request::instance(); + $newRequest = $currentRequest->duplicate(['shop' => 'mystore123.myshopify.com']); + // Enable billing and set a shop + $this->app['config']->set('shopify-app.billing_enabled', true); + $plan = factory(Util::getShopifyConfig('models.plan', Plan::class))->states('type_recurring')->create(); $shop = factory($this->model)->create([ + 'name' => 'mystore123.myshopify.com', 'plan_id' => $plan->getId()->toNative(), ]); + factory(Util::getShopifyConfig('models.charge', Charge::class))->states('type_recurring')->create([ 'plan_id' => $plan->getId()->toNative(), 'user_id' => $shop->getId()->toNative(), ]); - $this->auth->login($shop); - $this->app['config']->set('shopify-app.billing_enabled', true); - // Run the middleware - $result = $this->runMiddleware(BillableMiddleware::class); + $result = $this->runMiddleware(BillableMiddleware::class, $newRequest); // Assert it was called $this->assertTrue($result[0]); @@ -62,13 +69,19 @@ public function testEnabledBillingWithPaidShop(): void public function testEnabledBillingWithGrandfatheredShop(): void { + $currentRequest = Request::instance(); + $newRequest = $currentRequest->duplicate(['shop' => 'mystore123.myshopify.com']); + // Enable billing and set a shop - $shop = factory($this->model)->states('grandfathered')->create(); - $this->auth->login($shop); $this->app['config']->set('shopify-app.billing_enabled', true); + factory($this->model) + ->states('grandfathered') + ->create([ + 'name' => 'mystore123.myshopify.com', + ]); // Run the middleware - $result = $this->runMiddleware(BillableMiddleware::class); + $result = $this->runMiddleware(BillableMiddleware::class, $newRequest); // Assert it was called $this->assertTrue($result[0]); @@ -76,13 +89,19 @@ public function testEnabledBillingWithGrandfatheredShop(): void public function testEnabledBillingWithFreemiumShop(): void { + $currentRequest = Request::instance(); + $newRequest = $currentRequest->duplicate(['shop' => 'mystore123.myshopify.com']); + // Enable billing and set a shop - $shop = factory($this->model)->states('freemium')->create(); - $this->auth->login($shop); $this->app['config']->set('shopify-app.billing_enabled', true); + factory($this->model) + ->states('freemium') + ->create([ + 'name' => 'mystore123.myshopify.com', + ]); // Run the middleware - $result = $this->runMiddleware(BillableMiddleware::class); + $result = $this->runMiddleware(BillableMiddleware::class, $newRequest); // Assert it was called $this->assertTrue($result[0]); @@ -91,9 +110,8 @@ public function testEnabledBillingWithFreemiumShop(): void public function testDisabledBillingShouldPassOn(): void { // Ensure billing is disabled and set a shop - $shop = factory($this->model)->create(); - $this->auth->login($shop); $this->app['config']->set('shopify-app.billing_enabled', false); + factory($this->model)->create(); // Run the middleware $result = $this->runMiddleware(BillableMiddleware::class);