Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions lib/algora/bounties/bounties.ex
Original file line number Diff line number Diff line change
Expand Up @@ -429,8 +429,12 @@ defmodule Algora.Bounties do

@spec notify_bounty(%{owner: User.t(), bounty: Bounty.t()}, opts :: []) ::
{:ok, nil} | {:error, atom()}
def notify_bounty(%{owner: _owner, bounty: bounty}, _opts) do
Algora.Admin.alert("Notify bounty: #{inspect(bounty)}", :error)
def notify_bounty(%{owner: owner, bounty: bounty}, _opts) do
Algora.Admin.alert(
"New contract offer: #{AlgoraWeb.Endpoint.url()}/#{owner.handle}/contracts/#{bounty.id}",
:critical
)

{:ok, nil}
end

Expand Down Expand Up @@ -906,11 +910,9 @@ defmodule Algora.Bounties do
end
end

def calculate_contract_amount(amount), do: Money.mult!(amount, Decimal.new("1.13"))

def final_contract_amount(:marketplace, amount), do: amount

def final_contract_amount(:bring_your_own, amount), do: calculate_contract_amount(amount)
def final_contract_amount(:bring_your_own, amount), do: Money.mult!(amount, Decimal.new("1.13"))

@spec create_payment_session(
%{owner: User.t(), amount: Money.t(), description: String.t()},
Expand Down
80 changes: 79 additions & 1 deletion lib/algora/payments/payments.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ defmodule Algora.Payments do
alias Algora.Bounties
alias Algora.Bounties.Bounty
alias Algora.Bounties.Claim
alias Algora.Bounties.Jobs.PromptPayoutConnect
alias Algora.Bounties.Tip
alias Algora.Jobs.JobPosting
alias Algora.MoneyUtils
Expand Down Expand Up @@ -737,7 +738,7 @@ defmodule Algora.Payments do

{:error, :no_active_account} ->
case %{credit_id: credit.id}
|> Bounties.Jobs.PromptPayoutConnect.new()
|> PromptPayoutConnect.new()
|> Oban.insert() do
{:ok, _job} -> {:cont, :ok}
error -> {:halt, error}
Expand All @@ -761,4 +762,81 @@ defmodule Algora.Payments do
end
end)
end

def process_release(
%Stripe.Charge{id: charge_id, captured: true, payment_intent: payment_intent_id},
group_id,
amount,
recipient
) do
Repo.transact(fn ->
tx = Repo.get_by(Transaction, group_id: group_id, type: :charge, status: :succeeded)

user = Repo.get_by(User, id: tx.user_id)
bounty = Repo.get_by(Bounty, id: tx.bounty_id)

Algora.Admin.alert(
"Release #{amount} escrow to #{recipient.handle} for #{AlgoraWeb.Endpoint.url()}/#{user.handle}/contracts/#{bounty.id}",
:critical
)

debit_id = Nanoid.generate()
credit_id = Nanoid.generate()

with {:ok, debit0} <- Repo.fetch_by(Transaction, group_id: group_id, type: :debit, status: :requires_release),
{:ok, _} <-
debit0
|> change(%{
net_amount: Money.sub!(debit0.net_amount, amount),
gross_amount: Money.sub!(debit0.gross_amount, amount)
})
|> Repo.update(),
{:ok, credit0} <- Repo.fetch_by(Transaction, group_id: group_id, type: :credit, status: :requires_release),
{:ok, _} <-
credit0
|> change(%{
net_amount: Money.add!(credit0.net_amount, amount),
gross_amount: Money.add!(credit0.gross_amount, amount)
})
|> Repo.update(),
{:ok, _debit} <-
Repo.insert(%Transaction{
id: debit_id,
provider: "stripe",
provider_id: charge_id,
provider_charge_id: charge_id,
provider_payment_intent_id: payment_intent_id,
type: :debit,
status: :succeeded,
succeeded_at: DateTime.utc_now(),
bounty_id: tx.bounty_id,
user_id: tx.user_id,
gross_amount: amount,
net_amount: amount,
total_fee: Money.zero(:USD),
linked_transaction_id: credit_id,
group_id: group_id
}),
{:ok, _credit} <-
Repo.insert(%Transaction{
id: credit_id,
provider: "stripe",
provider_id: charge_id,
provider_charge_id: charge_id,
provider_payment_intent_id: payment_intent_id,
type: :credit,
status: :initialized,
succeeded_at: DateTime.utc_now(),
bounty_id: tx.bounty_id,
user_id: recipient.id,
gross_amount: amount,
net_amount: amount,
total_fee: Money.zero(:USD),
linked_transaction_id: debit_id,
group_id: group_id
}) do
{:ok, nil}
end
end)
end
end
4 changes: 3 additions & 1 deletion lib/algora/settings/settings.ex
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,16 @@ defmodule Algora.Settings do
projects = Accounts.list_contributed_projects(user, limit: 2)
avatar_url = profile["avatar_url"] || user.avatar_url
hourly_rate = match["hourly_rate"] || profile["hourly_rate"]
hours_per_week = match["hours_per_week"] || profile["hours_per_week"] || user.hours_per_week

[
%{
user: %{user | avatar_url: avatar_url},
projects: projects,
badge_variant: match["badge_variant"],
badge_text: match["badge_text"],
hourly_rate: if(hourly_rate, do: Money.new(:USD, hourly_rate, no_fraction_if_integer: true))
hourly_rate: if(hourly_rate, do: Money.new(:USD, hourly_rate, no_fraction_if_integer: true)),
hours_per_week: hours_per_week
}
]
else
Expand Down
4 changes: 1 addition & 3 deletions lib/algora_web/forms/contract_form.ex
Original file line number Diff line number Diff line change
Expand Up @@ -225,9 +225,7 @@ defmodule AlgoraWeb.Forms.ContractForm do
</div>
</dt>
<dd class="font-display font-semibold tabular-nums text-lg">
{Money.to_string!(
Bounties.calculate_contract_amount(get_change(@form.source, :amount))
)}
{Money.to_string!(get_change(@form.source, :amount))}
</dd>
</div>
</dl>
Expand Down
Loading