Skip to content

Commit 0bfea37

Browse files
committed
feat: Adding fetching user to check payment status. #1
1 parent 990444e commit 0bfea37

File tree

4 files changed

+30
-6
lines changed

4 files changed

+30
-6
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1164,6 +1164,10 @@ defmodule UsersTable do
11641164
Pockets.put(@table, person_id, %{stripe_id: stripe_id, status: status})
11651165
end
11661166

1167+
def fetch_user(person_id) do
1168+
Pockets.get(@table, person_id)
1169+
end
1170+
11671171
end
11681172
```
11691173

@@ -1182,6 +1186,8 @@ If the file already exists, we open the file.
11821186
- `create_user/3` receives a `stripe_id`, `person_id` and `status`
11831187
(pertaining to whether the payment has been made or not)
11841188
and creates a user object.
1189+
- `fetch_user/1` retrieves the persisted user
1190+
according to the given` person_id`.
11851191

11861192
Let's make use of some of these functions.
11871193
We want to setup the `DETS` table on the process startup.
@@ -1199,6 +1205,12 @@ UsersTable.init()
11991205

12001206
Awesome!
12011207

1208+
1209+
1210+
# todo
1211+
ver todo no checkout_session_controller.ex
1212+
remove swoosh, mailer, postgres, ecto
1213+
12021214
# Thanks!
12031215

12041216
Thanks for learning about payment processing with us!

lib/app/users.ex

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,8 @@ defmodule UsersTable do
2121
Pockets.put(@table, person_id, %{stripe_id: stripe_id, status: status})
2222
end
2323

24+
def fetch_user(person_id) do
25+
Pockets.get(@table, person_id)
26+
end
27+
2428
end

lib/app_web/controllers/app_controller.ex

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,14 @@ defmodule AppWeb.AppController do
22
use AppWeb, :controller
33

44
def home(conn, _params) do
5-
# The home page is often custom made,
6-
# so skip the default app layout.
7-
render(conn, :app, layout: false)
5+
6+
person_id = conn.assigns.person.id
7+
case UsersTable.fetch_user(person_id) do
8+
nil ->
9+
conn |> redirect(to: ~p"/")
10+
11+
_ ->
12+
render(conn, :app, layout: false)
13+
end
814
end
915
end

lib/app_web/controllers/page_controller.ex

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,17 @@ defmodule AppWeb.PageController do
22
use AppWeb, :controller
33

44
def home(conn, _params) do
5-
# The home page is often custom made,
6-
# so skip the default app layout.
75
render(conn, :home, layout: false)
86
end
97

108
def success(conn, %{"session_id" => session_id}) do
119

1210
case Stripe.Session.retrieve(session_id) do
13-
{:ok, _session} ->
11+
{:ok, session} ->
12+
13+
person_id = conn.assigns.person.id
14+
UsersTable.create_user(%{person_id: person_id, stripe_id: session.customer, status: true})
15+
1416
render(conn, :success, layout: false)
1517

1618
{:error, _error} ->

0 commit comments

Comments
 (0)