Skip to content

Commit 85c02bb

Browse files
Improve validation for billable shop
1 parent 24afabd commit 85c02bb

File tree

2 files changed

+53
-16
lines changed

2 files changed

+53
-16
lines changed

src/Http/Middleware/Billable.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,33 @@
55
use Closure;
66
use Illuminate\Http\Request;
77
use Illuminate\Support\Facades\Redirect;
8+
use Osiset\ShopifyApp\Contracts\Queries\Shop as ShopQuery;
89
use Osiset\ShopifyApp\Contracts\ShopModel as IShopModel;
10+
use Osiset\ShopifyApp\Objects\Values\ShopDomain;
911
use Osiset\ShopifyApp\Util;
1012

1113
/**
1214
* Responsible for ensuring the shop is being billed.
1315
*/
1416
class Billable
1517
{
18+
/**
19+
* The shop querier.
20+
*
21+
* @var ShopQuery
22+
*/
23+
protected $shopQuery;
24+
25+
/**
26+
* @param ShopQuery $shopQuery The shop querier.
27+
*
28+
* @return void
29+
*/
30+
public function __construct(ShopQuery $shopQuery)
31+
{
32+
$this->shopQuery = $shopQuery;
33+
}
34+
1635
/**
1736
* Checks if a shop has paid for access.
1837
*
@@ -25,7 +44,7 @@ public function handle(Request $request, Closure $next)
2544
{
2645
if (Util::getShopifyConfig('billing_enabled') === true) {
2746
/** @var $shop IShopModel */
28-
$shop = auth()->user();
47+
$shop = $this->shopQuery->getByDomain(ShopDomain::fromNative($request->get('shop')));
2948
if (! $shop->plan && ! $shop->isFreemium() && ! $shop->isGrandfathered()) {
3049
// They're not grandfathered in, and there is no charge or charge was declined... redirect to billing
3150
return Redirect::route(

tests/Http/Middleware/BillableTest.php

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Osiset\ShopifyApp\Test\Http\Middleware;
44

55
use Illuminate\Auth\AuthManager;
6+
use Illuminate\Support\Facades\Request;
67
use Osiset\ShopifyApp\Http\Middleware\Billable as BillableMiddleware;
78
use Osiset\ShopifyApp\Storage\Models\Charge;
89
use Osiset\ShopifyApp\Storage\Models\Plan;
@@ -25,13 +26,15 @@ public function setUp(): void
2526

2627
public function testEnabledBillingWithUnpaidShop(): void
2728
{
29+
$currentRequest = Request::instance();
30+
$newRequest = $currentRequest->duplicate(['shop' => 'mystore123.myshopify.com']);
31+
2832
// Enable billing and set a shop
29-
$shop = factory($this->model)->create();
30-
$this->auth->login($shop);
3133
$this->app['config']->set('shopify-app.billing_enabled', true);
34+
factory($this->model)->create(['name' => 'mystore123.myshopify.com']);
3235

3336
// Run the middleware
34-
$result = $this->runMiddleware(BillableMiddleware::class);
37+
$result = $this->runMiddleware(BillableMiddleware::class, $newRequest);
3538

3639
// Assert it was not called and redirect happened
3740
$this->assertFalse($result[0]);
@@ -40,49 +43,65 @@ public function testEnabledBillingWithUnpaidShop(): void
4043

4144
public function testEnabledBillingWithPaidShop(): void
4245
{
46+
$currentRequest = Request::instance();
47+
$newRequest = $currentRequest->duplicate(['shop' => 'mystore123.myshopify.com']);
48+
4349
// Enable billing and set a shop
50+
$this->app['config']->set('shopify-app.billing_enabled', true);
51+
4452
$plan = factory(Util::getShopifyConfig('models.plan', Plan::class))->states('type_recurring')->create();
4553
$shop = factory($this->model)->create([
54+
'name' => 'mystore123.myshopify.com',
4655
'plan_id' => $plan->getId()->toNative(),
4756
]);
57+
4858
factory(Util::getShopifyConfig('models.charge', Charge::class))->states('type_recurring')->create([
4959
'plan_id' => $plan->getId()->toNative(),
5060
'user_id' => $shop->getId()->toNative(),
5161
]);
5262

53-
$this->auth->login($shop);
54-
$this->app['config']->set('shopify-app.billing_enabled', true);
55-
5663
// Run the middleware
57-
$result = $this->runMiddleware(BillableMiddleware::class);
64+
$result = $this->runMiddleware(BillableMiddleware::class, $newRequest);
5865

5966
// Assert it was called
6067
$this->assertTrue($result[0]);
6168
}
6269

6370
public function testEnabledBillingWithGrandfatheredShop(): void
6471
{
72+
$currentRequest = Request::instance();
73+
$newRequest = $currentRequest->duplicate(['shop' => 'mystore123.myshopify.com']);
74+
6575
// Enable billing and set a shop
66-
$shop = factory($this->model)->states('grandfathered')->create();
67-
$this->auth->login($shop);
6876
$this->app['config']->set('shopify-app.billing_enabled', true);
77+
factory($this->model)
78+
->states('grandfathered')
79+
->create([
80+
'name' => 'mystore123.myshopify.com',
81+
]);
6982

7083
// Run the middleware
71-
$result = $this->runMiddleware(BillableMiddleware::class);
84+
$result = $this->runMiddleware(BillableMiddleware::class, $newRequest);
7285

7386
// Assert it was called
7487
$this->assertTrue($result[0]);
7588
}
7689

7790
public function testEnabledBillingWithFreemiumShop(): void
7891
{
92+
$currentRequest = Request::instance();
93+
$newRequest = $currentRequest->duplicate(['shop' => 'mystore123.myshopify.com']);
94+
7995
// Enable billing and set a shop
80-
$shop = factory($this->model)->states('freemium')->create();
81-
$this->auth->login($shop);
8296
$this->app['config']->set('shopify-app.billing_enabled', true);
97+
factory($this->model)
98+
->states('freemium')
99+
->create([
100+
'name' => 'mystore123.myshopify.com',
101+
]);
83102

84103
// Run the middleware
85-
$result = $this->runMiddleware(BillableMiddleware::class);
104+
$result = $this->runMiddleware(BillableMiddleware::class, $newRequest);
86105

87106
// Assert it was called
88107
$this->assertTrue($result[0]);
@@ -91,9 +110,8 @@ public function testEnabledBillingWithFreemiumShop(): void
91110
public function testDisabledBillingShouldPassOn(): void
92111
{
93112
// Ensure billing is disabled and set a shop
94-
$shop = factory($this->model)->create();
95-
$this->auth->login($shop);
96113
$this->app['config']->set('shopify-app.billing_enabled', false);
114+
factory($this->model)->create();
97115

98116
// Run the middleware
99117
$result = $this->runMiddleware(BillableMiddleware::class);

0 commit comments

Comments
 (0)