8
8
use Illuminate \Support \Facades \Redirect ;
9
9
use Osiset \ShopifyApp \Contracts \ShopModel as IShopModel ;
10
10
use Osiset \ShopifyApp \Util ;
11
- use RuntimeException ;
12
11
13
12
/**
14
13
* Responsible for ensuring the shop is being billed.
@@ -19,35 +18,47 @@ class Billable
19
18
* Checks if a shop has paid for access.
20
19
*
21
20
* @param Request $request The request object.
22
- * @param Closure $next The next action.
21
+ * @param Closure $next The next action.
23
22
*
24
- *@throws Exception
23
+ * @throws Exception
25
24
*
26
25
* @return mixed
27
26
*/
28
27
public function handle (Request $ request , Closure $ next )
29
28
{
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 );
32
31
}
33
32
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 );
48
36
}
49
37
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 );
52
63
}
53
64
}
0 commit comments