Skip to content

Commit cc34bcd

Browse files
committed
impl payment flow
1 parent d01552c commit cc34bcd

File tree

3 files changed

+51
-18
lines changed

3 files changed

+51
-18
lines changed

lib/algora/jobs/jobs.ex

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ defmodule Algora.Jobs do
4848
defp maybe_limit(query, nil), do: query
4949
defp maybe_limit(query, limit), do: limit(query, ^limit)
5050

51-
@spec create_payment_session(JobPosting.t(), Money.t()) ::
51+
@spec create_payment_session(User.t() | nil, JobPosting.t(), Money.t()) ::
5252
{:ok, String.t()} | {:error, atom()}
53-
def create_payment_session(job_posting, amount) do
53+
def create_payment_session(user, job_posting, amount) do
5454
line_items = [
5555
%LineItem{
5656
amount: amount,
@@ -66,20 +66,17 @@ defmodule Algora.Jobs do
6666
gross_amount = LineItem.gross_amount(line_items)
6767
group_id = Nanoid.generate()
6868

69+
job_posting = Repo.preload(job_posting, :user)
70+
6971
Repo.transact(fn ->
70-
with {:ok, user} <-
71-
Accounts.get_or_register_user(job_posting.email, %{
72-
type: :organization,
73-
display_name: job_posting.company_name
74-
}),
75-
{:ok, _charge} <-
72+
with {:ok, _charge} <-
7673
%Transaction{}
7774
|> change(%{
7875
id: Nanoid.generate(),
7976
provider: "stripe",
8077
type: :charge,
8178
status: :initialized,
82-
user_id: user.id,
79+
user_id: if(user, do: user.id),
8380
job_id: job_posting.id,
8481
gross_amount: gross_amount,
8582
net_amount: gross_amount,
@@ -101,8 +98,9 @@ defmodule Algora.Jobs do
10198
description: "Job posting - #{job_posting.company_name}",
10299
metadata: %{"version" => Payments.metadata_version(), "group_id" => group_id}
103100
},
104-
success_url: "#{AlgoraWeb.Endpoint.url()}/jobs?status=paid",
105-
cancel_url: "#{AlgoraWeb.Endpoint.url()}/jobs?status=canceled"
101+
success_url:
102+
"#{AlgoraWeb.Endpoint.url()}/#{job_posting.user.handle}/jobs/#{job_posting.id}/applicants?status=paid",
103+
cancel_url: "#{AlgoraWeb.Endpoint.url()}/#{job_posting.user.handle}/jobs/#{job_posting.id}/applicants"
106104
) do
107105
{:ok, session.url}
108106
end

lib/algora/payments/payments.ex

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ defmodule Algora.Payments do
3535
end
3636

3737
@spec create_stripe_session(
38-
user :: User.t(),
38+
user :: User.t() | nil,
3939
line_items :: [PSP.Session.line_item_data()],
4040
payment_intent_data :: PSP.Session.payment_intent_data(),
4141
opts :: [
@@ -44,7 +44,29 @@ defmodule Algora.Payments do
4444
]
4545
) ::
4646
{:ok, PSP.session()} | {:error, PSP.error()}
47-
def create_stripe_session(user, line_items, payment_intent_data, opts \\ []) do
47+
def create_stripe_session(user, line_items, payment_intent_data, opts \\ [])
48+
49+
def create_stripe_session(nil, line_items, payment_intent_data, opts) do
50+
opts = %{
51+
mode: "payment",
52+
billing_address_collection: "required",
53+
line_items: line_items,
54+
success_url: opts[:success_url] || "#{AlgoraWeb.Endpoint.url()}/payment/success",
55+
cancel_url: opts[:cancel_url] || "#{AlgoraWeb.Endpoint.url()}/payment/canceled",
56+
payment_intent_data: payment_intent_data
57+
}
58+
59+
opts =
60+
if payment_intent_data[:capture_method] == :manual do
61+
opts
62+
else
63+
Map.put(opts, :invoice_creation, %{enabled: true})
64+
end
65+
66+
PSP.Session.create(opts)
67+
end
68+
69+
def create_stripe_session(user, line_items, payment_intent_data, opts) do
4870
with {:ok, customer} <- fetch_or_create_customer(user) do
4971
opts = %{
5072
mode: "payment",
@@ -657,10 +679,16 @@ defmodule Algora.Payments do
657679
Repo.update_all(from(c in Claim, where: c.id in ^claim_ids), set: [status: :approved])
658680

659681
{_, job_postings} =
660-
Repo.update_all(from(j in JobPosting, where: j.id in ^job_ids, select: j), set: [status: :processing])
682+
Repo.update_all(from(j in JobPosting, where: j.id in ^job_ids, select: j), set: [status: :active])
683+
684+
job_postings = Repo.preload(job_postings, :user)
685+
686+
Repo.update_all(from(u in User, where: u.id in ^Enum.map(job_postings, & &1.user_id)),
687+
set: [hiring_subscription: :active]
688+
)
661689

662690
for job <- job_postings do
663-
Algora.Admin.alert("Job payment received! #{job.company_name} #{job.email} #{job.url}", :info)
691+
Algora.Admin.alert("Job payment received! #{job.company_name} #{job.email} #{job.url}", :critical)
664692
end
665693

666694
auto_txs =

lib/algora_web/live/org/job_live.ex

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,14 @@ defmodule AlgoraWeb.Org.JobLive do
100100
end
101101

102102
@impl true
103-
def handle_params(%{"tab" => tab}, _uri, socket) do
103+
def handle_params(%{"tab" => tab} = params, _uri, socket) do
104+
socket =
105+
if params["status"] == "paid" do
106+
put_flash(socket, :info, "Your annual subscription has been activated!")
107+
else
108+
socket
109+
end
110+
104111
{:noreply, assign(socket, :current_tab, tab)}
105112
end
106113

@@ -661,9 +668,9 @@ defmodule AlgoraWeb.Org.JobLive do
661668

662669
@impl true
663670
def handle_event("process_payment", %{"payment" => %{"payment_type" => "stripe"}}, socket) do
664-
# Mock data for demonstration
665671
case Jobs.create_payment_session(
666-
%{socket.assigns.job | email: socket.assigns.current_user.email},
672+
socket.assigns.current_user,
673+
socket.assigns.job,
667674
socket.assigns.current_org.subscription_price
668675
) do
669676
{:ok, url} ->

0 commit comments

Comments
 (0)