Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve validation for billable shop #2

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion src/Http/Middleware/Billable.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,33 @@
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;

/**
* Responsible for ensuring the shop is being billed.
*/
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.
*
Expand All @@ -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(
Expand Down
48 changes: 33 additions & 15 deletions tests/Http/Middleware/BillableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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]);
Expand All @@ -40,49 +43,65 @@ 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]);
}

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]);
}

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]);
Expand All @@ -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);
Expand Down