Skip to content

Commit 1006864

Browse files
committed
move payment_guard show_plans callback to payment_controller render_plans callback
1 parent f2c91af commit 1006864

File tree

5 files changed

+39
-30
lines changed

5 files changed

+39
-30
lines changed

lib/shopifex/payment_guard.ex

+1-22
Original file line numberDiff line numberDiff line change
@@ -49,19 +49,9 @@ defmodule Shopifex.PaymentGuard do
4949
"""
5050
@callback get_plan(plan_id :: String.t() | pos_integer()) :: plan()
5151

52-
@doc """
53-
Display the available payment plans for the user to select.
54-
"""
55-
@callback show_plans(
56-
conn :: Plug.Conn.t(),
57-
guard_identifier :: String.t(),
58-
redirect_after :: String.t()
59-
) :: Plug.Conn.t()
60-
6152
@optional_callbacks grant_for_guard: 2,
6253
use_grant: 2,
6354
get_plan: 1,
64-
show_plans: 3,
6555
create_grant: 3
6656

6757
defmacro __using__(_opts) do
@@ -116,17 +106,6 @@ defmodule Shopifex.PaymentGuard do
116106
),
117107
do: Changeset.change(grant_changeset, %{remaining_usages: remaining_usages - 1})
118108

119-
@impl Shopifex.PaymentGuard
120-
def show_plans(conn, guard_identifier, redirect_after) do
121-
conn
122-
|> put_view(ShopifexWeb.PaymentView)
123-
|> put_layout({ShopifexWeb.LayoutView, "app.html"})
124-
|> render("show-plans.html",
125-
guard: guard_identifier,
126-
redirect_after: redirect_after
127-
)
128-
end
129-
130109
@impl Shopifex.PaymentGuard
131110
def create_grant(shop, plan, charge_id) do
132111
Shopifex.Shops.create_grant(%{
@@ -138,7 +117,7 @@ defmodule Shopifex.PaymentGuard do
138117
})
139118
end
140119

141-
defoverridable grant_for_guard: 2, use_grant: 2, show_plans: 3, get_plan: 1, create_grant: 3
120+
defoverridable grant_for_guard: 2, use_grant: 2, get_plan: 1, create_grant: 3
142121
end
143122
end
144123
end

lib/shopifex/plug/payment_guard.ex

+10-2
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,18 @@ defmodule Shopifex.Plug.PaymentGuard do
2121
"""
2222
require Logger
2323

24+
@router_helpers Module.concat([Application.compile_env!(:shopifex, :web_module), Router, Helpers])
25+
2426
def init(options) do
2527
# initialize options
2628
options
2729
end
2830

2931
@doc """
30-
This makes sure the shop in the session contains a payment which unlocks the guard
32+
This makes sure the shop in the session contains a payment which unlocks the guard.
33+
34+
If no payment is present which unlocks the guard, the conn will be redirected to your
35+
application's PaymentController.show_plans route.
3136
"""
3237
def call(conn, guard_identifier) do
3338
payment_guard = Application.fetch_env!(:shopifex, :payment_guard)
@@ -39,7 +44,10 @@ defmodule Shopifex.Plug.PaymentGuard do
3944
Logger.info("Payment guard blocked request")
4045
redirect_after = URI.encode_www_form("#{conn.request_path}?#{conn.query_string}")
4146

42-
payment_guard.show_plans(conn, guard_identifier, redirect_after)
47+
show_plans_url = @router_helpers.payment_path(conn, :show_plans, %{guard_identifier: guard_identifier, redirect_after: redirect_after})
48+
49+
conn
50+
|> Phoenix.Controller.redirect(to: show_plans_url)
4351
|> Plug.Conn.halt()
4452

4553
grant_for_guard ->

lib/shopifex_web/controllers/payment_controller.ex

+25-3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@ defmodule ShopifexWeb.PaymentController do
1717
```
1818
"""
1919

20+
21+
@doc """
22+
Display the available payment plans for the user to select.
23+
"""
24+
@callback render_plans(
25+
conn :: Plug.Conn.t(),
26+
guard_identifier :: String.t(),
27+
redirect_after :: String.t()
28+
) :: Plug.Conn.t()
29+
2030
@doc """
2131
An optional callback called after a payment is completed. By default, this function
2232
redirects the user to the app index within their Shopify admin panel.
@@ -38,7 +48,8 @@ defmodule ShopifexWeb.PaymentController do
3848
String.t()
3949
) ::
4050
Plug.Conn.t()
41-
@optional_callbacks after_payment: 5
51+
52+
@optional_callbacks render_plans: 3, after_payment: 5
4253

4354
defmacro __using__(_opts) do
4455
quote do
@@ -51,7 +62,7 @@ defmodule ShopifexWeb.PaymentController do
5162
path_prefix = Application.get_env(:shopifex, :path_prefix, "")
5263
default_redirect_after = path_prefix <> "/?token=" <> Guardian.Plug.current_token(conn)
5364

54-
payment_guard.show_plans(
65+
render_plans(
5566
conn,
5667
Map.get(params, "guard_identifier"),
5768
Map.get(params, "redirect_after", default_redirect_after)
@@ -198,13 +209,24 @@ defmodule ShopifexWeb.PaymentController do
198209
end
199210
end
200211

212+
@impl ShopifexWeb.PaymentController
213+
def render_plans(conn, guard_identifier, redirect_after) do
214+
conn
215+
|> put_view(ShopifexWeb.PaymentView)
216+
|> put_layout({ShopifexWeb.LayoutView, "app.html"})
217+
|> render("show-plans.html",
218+
guard: guard_identifier,
219+
redirect_after: redirect_after
220+
)
221+
end
222+
201223
@impl ShopifexWeb.PaymentController
202224
def after_payment(conn, shop, _plan, _grant, redirect_after) do
203225
api_key = Application.get_env(:shopifex, :api_key)
204226
redirect(conn, external: "https://#{shop.url}/admin/apps/#{api_key}#{redirect_after}")
205227
end
206228

207-
defoverridable after_payment: 5
229+
defoverridable render_plans: 3, after_payment: 5
208230
end
209231
end
210232
end

mix.exs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ defmodule Shopifex.MixProject do
44
def project do
55
[
66
app: :shopifex,
7-
version: "1.1.1",
7+
version: "2.0.0",
88
elixir: "~> 1.10",
99
start_permanent: Mix.env() == :prod,
1010
compilers: [:phoenix, :gettext] ++ Mix.compilers(),

test/plug/payment_guard_test.exs

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ defmodule Shopifex.Plug.PaymentGuardTest do
88

99
setup [:shop_in_session]
1010

11-
test "payment guard blocks pay-walled function and renders payment page", %{
11+
test "payment guard blocks pay-walled function and redirects to payment page", %{
1212
conn: conn
1313
} do
1414
conn = Shopifex.Plug.PaymentGuard.call(conn, "block")
1515

16-
assert html_response(conn, 200) =~ "Components.WrappedShowPlans"
16+
assert html_response(conn, 302) =~ "<html><body>You are being <a href=\"/payment/show-plans?guard_identifier=block"
1717
end
1818

1919
test "payment guard grants access pay-walled function and places guard payment in session", %{

0 commit comments

Comments
 (0)