88use Illuminate \Support \Facades \Redirect ;
99use Osiset \ShopifyApp \Contracts \ShopModel as IShopModel ;
1010use Osiset \ShopifyApp \Util ;
11- use RuntimeException ;
1211
1312/**
1413 * Responsible for ensuring the shop is being billed.
@@ -19,35 +18,47 @@ class Billable
1918 * Checks if a shop has paid for access.
2019 *
2120 * @param Request $request The request object.
22- * @param Closure $next The next action.
21+ * @param Closure $next The next action.
2322 *
24- *@throws Exception
23+ * @throws Exception
2524 *
2625 * @return mixed
2726 */
2827 public function handle (Request $ request , Closure $ next )
2928 {
30- if (! Util::isMPAApplication () ) {
31- throw new RuntimeException ( ' You cannot use Billable middleware with SPA mode ' );
29+ if (Util::getShopifyConfig ( ' billing_enabled ' ) !== true ) {
30+ return $ next ( $ request );
3231 }
3332
34- if (Util::getShopifyConfig ('billing_enabled ' ) === true ) {
35- /** @var $shop IShopModel */
36- $ shop = auth ()->user ();
37- if (!$ shop ->plan && !$ shop ->isFreemium () && !$ shop ->isGrandfathered ()) {
38- // They're not grandfathered in, and there is no charge or charge was declined... redirect to billing
39- return Redirect::route (
40- Util::getShopifyConfig ('route_names.billing ' ),
41- array_merge ($ request ->input (), [
42- 'shop ' => $ shop ->getDomain ()->toNative (),
43- 'host ' => $ request ->get ('host ' ),
44- 'locale ' => $ request ->get ('locale ' ),
45- ])
46- );
47- }
33+ // Proceed if we are on SPA mode & it's a non ajax request
34+ if (!Util::isMPAApplication () && ! $ request ->ajax ()) {
35+ return $ next ($ request );
4836 }
4937
50- // Move on, everything's fine
51- return $ next ($ request );
38+ /** @var $shop IShopModel */
39+ $ shop = auth ()->user ();
40+
41+ // if shop has plan or is on freemium or is grandfathered then move on with request
42+ if (! $ shop || $ shop ->plan || $ shop ->isFreemium () || $ shop ->isGrandfathered ()) {
43+ return $ next ($ request );
44+ }
45+
46+ $ args = [
47+ Util::getShopifyConfig ('route_names.billing ' ),
48+ array_merge ($ request ->input (), [
49+ 'shop ' => $ shop ->getDomain ()->toNative (),
50+ 'host ' => $ request ->get ('host ' ),
51+ 'locale ' => $ request ->get ('locale ' ),
52+ ]),
53+ ];
54+
55+ if ($ request ->ajax ()) {
56+ return response ()->json (
57+ ['forceRedirectUrl ' => route (...$ args )],
58+ 402
59+ );
60+ }
61+
62+ return Redirect::route (...$ args );
5263 }
5364}
0 commit comments