Skip to content

Instantly share code, notes, and snippets.

@rvibit
Created February 4, 2023 13:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rvibit/69ff50f010652ef840b10b55452f978c to your computer and use it in GitHub Desktop.
Save rvibit/69ff50f010652ef840b10b55452f978c to your computer and use it in GitHub Desktop.
vendor\kyon147\laravel-shopify\src\Http\Middleware\Billable.php for React SPA using laravel-shopify package
<?php
namespace Osiset\ShopifyApp\Http\Middleware;
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.
*
* @param Request $request The request object.
* @param \Closure $next The next action.
* @return mixed
*/
public function handle(Request $request, Closure $next)
{
if (Util::getShopifyConfig('billing_enabled') === true) {
/** @var $shop IShopModel */
$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(
Util::getShopifyConfig('route_names.billing'),
array_merge($request->input(), ['shop' => $shop->getDomain()->toNative()])
);
}
}
// Move on, everything's fine
return $next($request);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment