Skip to content

Commit

Permalink
fix: Switching Stripe.Session to Stripe.Checkout.Session.
Browse files Browse the repository at this point in the history
  • Loading branch information
LuchoTurtle committed Oct 11, 2023
1 parent 9235da0 commit 830051e
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 14 deletions.
12 changes: 6 additions & 6 deletions example.md
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,7 @@ defmodule AppWeb.CheckoutSessionController do
automatic_tax: %{enabled: true}
}

{:ok, session} = Stripe.Session.create(params)
{:ok, session} = Stripe.Checkout.Session.create(params)

conn
|> put_status(303)
Expand All @@ -572,7 +572,7 @@ end

Let's analyze the code.

We are using `Stripe.session.create/1`
We are using `Stripe.Checkout.Session.create/1`
to create a [`Session`](https://stripe.com/docs/api/checkout/sessions)
in Stripe.
We need to pass a few required parameters.
Expand Down Expand Up @@ -666,7 +666,7 @@ and add the following piece of code:

```elixir
def success(conn, %{"session_id" => session_id}) do
case Stripe.Session.retrieve(session_id) do
case Stripe.Checkout.Session.retrieve(session_id) do
{:ok, _session} ->
render(conn, :success, layout: false)

Expand All @@ -685,7 +685,7 @@ and add the following piece of code:

def cancel(conn, %{"session_id" => session_id}) do

case Stripe.Session.retrieve(session_id) do
case Stripe.Checkout.Session.retrieve(session_id) do
{:ok, _session} ->
render(conn, :cancel, layout: false)

Expand Down Expand Up @@ -721,7 +721,7 @@ we check if the `session_id` is valid
by retrieving it from `Stripe`.

```elixir
Stripe.Session.retrieve(session_id)
Stripe.Checkout.Session.retrieve(session_id)
```

If it is successful, we render a page confirming the payment.
Expand Down Expand Up @@ -997,7 +997,7 @@ Change the handler, so it looks like the following:
```elixir
def success(conn, %{"session_id" => session_id}) do

case Stripe.Session.retrieve(session_id) do
case Stripe.Checkout.Session.retrieve(session_id) do
{:ok, session} ->

person_id = conn.assigns.person.id
Expand Down
2 changes: 1 addition & 1 deletion lib/app_web/controllers/checkout_session_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ defmodule AppWeb.CheckoutSessionController do
automatic_tax: %{enabled: true}
}

{:ok, session} = Stripe.Session.create(params)
{:ok, session} = Stripe.Checkout.Session.create(params)

conn
|> put_status(303)
Expand Down
4 changes: 2 additions & 2 deletions lib/app_web/controllers/page_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ defmodule AppWeb.PageController do
end

def success(conn, %{"session_id" => session_id}) do
case Stripe.Session.retrieve(session_id) do
case Stripe.Checkout.Session.retrieve(session_id) do
{:ok, session} ->
person_id = conn.assigns.person.id

Expand All @@ -32,7 +32,7 @@ defmodule AppWeb.PageController do
end

def cancel(conn, %{"session_id" => session_id}) do
case Stripe.Session.retrieve(session_id) do
case Stripe.Checkout.Session.retrieve(session_id) do
{:ok, _session} ->
render(conn, :cancel, layout: false)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ defmodule AppWeb.CheckoutSessionControllerTest do
conn = setup_conn_with_user(conn)
url_to_be_redirected_to = "www.session_url.com"

with_mock Stripe.Session,
with_mock Stripe.Checkout.Session,
create: fn _params -> {:ok, %{url: url_to_be_redirected_to}} end do
conn = post(conn, ~p"/purchase/checkout-session", %{})

Expand Down
8 changes: 4 additions & 4 deletions test/app_web/controllers/page_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ defmodule AppWeb.PageControllerTest do
test "get success with loggedin user with session_id", %{conn: conn} do
conn = setup_conn_with_user(conn)

with_mock Stripe.Session,
with_mock Stripe.Checkout.Session,
retrieve: fn _session_id -> {:ok, %{customer: 1}} end do
conn = get(conn, ~p"/purchase/success?session_id=123")

Expand All @@ -33,7 +33,7 @@ defmodule AppWeb.PageControllerTest do
%{conn: conn} do
conn = setup_conn_with_user(conn)

with_mock Stripe.Session, retrieve: fn _session_id -> {:error, nil} end do
with_mock Stripe.Checkout.Session, retrieve: fn _session_id -> {:error, nil} end do
conn = get(conn, ~p"/purchase/success?session_id=123")
assert redirected_to(conn, 303) == ~p"/"
end
Expand All @@ -48,7 +48,7 @@ defmodule AppWeb.PageControllerTest do
test "get cancel with loggedin user with session_id", %{conn: conn} do
conn = setup_conn_with_user(conn)

with_mock Stripe.Session,
with_mock Stripe.Checkout.Session,
retrieve: fn _session_id -> {:ok, %{customer: 1}} end do
conn = get(conn, ~p"/purchase/cancel?session_id=123")

Expand All @@ -60,7 +60,7 @@ defmodule AppWeb.PageControllerTest do
%{conn: conn} do
conn = setup_conn_with_user(conn)

with_mock Stripe.Session, retrieve: fn _session_id -> {:error, nil} end do
with_mock Stripe.Checkout.Session, retrieve: fn _session_id -> {:error, nil} end do
conn = get(conn, ~p"/purchase/cancel?session_id=123")
assert redirected_to(conn, 303) == ~p"/"
end
Expand Down

0 comments on commit 830051e

Please sign in to comment.