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
4 changes: 1 addition & 3 deletions lib/algora/contracts/contracts.ex
Original file line number Diff line number Diff line change
Expand Up @@ -472,9 +472,7 @@ defmodule Algora.Contracts do
defp maybe_pay_invoice(contract, invoice, txs) do
pm_id = contract.client.customer.default_payment_method.provider_id

case Invoice.pay(invoice.id, %{off_session: true, payment_method: pm_id}, %{
idempotency_key: "contract-#{contract.id}"
}) do
case Invoice.pay(invoice.id, %{off_session: true, payment_method: pm_id}) do
{:ok, stripe_invoice} ->
if stripe_invoice.paid, do: release_funds(contract, stripe_invoice, txs)
{:ok, stripe_invoice}
Expand Down
9 changes: 3 additions & 6 deletions lib/algora/psp/psp.ex
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,14 @@ defmodule Algora.PSP do
}
def create(params, opts), do: Algora.PSP.client(__MODULE__).create(params, Keyword.new(opts))

@spec pay(Stripe.id() | t, params, options) :: {:ok, t} | {:error, Algora.PSP.error()}
@spec pay(Stripe.id() | t, params) :: {:ok, t} | {:error, Algora.PSP.error()}
when params:
%{
optional(:off_session) => boolean,
optional(:payment_method) => String.t()
}
| %{},
options: %{
:idempotency_key => String.t()
}
def pay(invoice_id, params, opts), do: Algora.PSP.client(__MODULE__).pay(invoice_id, params, Keyword.new(opts))
| %{}
def pay(invoice_id, params), do: Algora.PSP.client(__MODULE__).pay(invoice_id, params)

def retrieve(id), do: Algora.PSP.client(__MODULE__).retrieve(id)
def retrieve(id, opts), do: Algora.PSP.client(__MODULE__).retrieve(id, Keyword.new(opts))
Expand Down
20 changes: 4 additions & 16 deletions lib/algora_web/controllers/webhooks/github_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -208,14 +208,12 @@ defmodule AlgoraWeb.Webhooks.GithubController do

autopay_result =
if autopayable_bounty do
idempotency_key = "bounty-#{autopayable_bounty.id}"

with {:ok, invoice} <-
Bounties.create_invoice(
%{
owner: autopayable_bounty.owner,
amount: autopayable_bounty.amount,
idempotency_key: idempotency_key
idempotency_key: "bounty-#{autopayable_bounty.id}"
},
ticket_ref: %{
owner: payload["repository"]["owner"]["login"],
Expand All @@ -231,8 +229,7 @@ defmodule AlgoraWeb.Webhooks.GithubController do
%{
payment_method: autopayable_bounty.owner.customer.default_payment_method.provider_id,
off_session: true
},
%{idempotency_key: idempotency_key}
}
) do
Algora.Admin.alert(
"Autopay successful (#{autopayable_bounty.owner.name} - #{autopayable_bounty.amount}).",
Expand Down Expand Up @@ -439,8 +436,6 @@ defmodule AlgoraWeb.Webhooks.GithubController do
not is_nil(amount) and
autopay_cooldown_expired?.()

idempotency_key = "tip-#{recipient}-#{webhook.delivery}"

autopay_result =
if autopayable? do
with {:ok, owner} <- Accounts.fetch_user_by(id: installation.connected_user_id),
Expand All @@ -458,21 +453,14 @@ defmodule AlgoraWeb.Webhooks.GithubController do
%{
owner: owner,
amount: amount,
idempotency_key: idempotency_key
idempotency_key: "invoice-#{recipient.provider_login}-#{webhook.delivery}"
},
ticket_ref: ticket_ref,
tip_id: tip.id,
recipient: recipient
),
{:ok, _invoice} <-
Invoice.pay(
invoice,
%{
payment_method: customer.default_payment_method.provider_id,
off_session: true
},
%{idempotency_key: idempotency_key}
) do
Invoice.pay(invoice, %{payment_method: customer.default_payment_method.provider_id, off_session: true}) do
Algora.Admin.alert(
"Autopay successful (#{payload["repository"]["full_name"]}##{ticket_ref.number} - #{amount}).",
:info
Expand Down
10 changes: 1 addition & 9 deletions test/algora/bounties_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -197,15 +197,7 @@ defmodule Algora.BountiesTest do
claims: [claim]
)

assert {:ok, _invoice} =
PSP.Invoice.pay(
invoice,
%{
payment_method: payment_method.provider_id,
off_session: true
},
%{idempotency_key: "bounty-#{bounty.id}"}
)
assert {:ok, _invoice} = PSP.Invoice.pay(invoice, %{payment_method: payment_method.provider_id, off_session: true})

charge = Repo.one!(from t in Transaction, where: t.type == :charge)
assert Money.equal?(charge.net_amount, amount)
Expand Down
4 changes: 2 additions & 2 deletions test/support/stripe_mock.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ defmodule Algora.Support.StripeMock do
}}
end

def pay(_invoice_id, %{payment_method: "pm_card_declined"}, _opts) do
def pay(_invoice_id, %{payment_method: "pm_card_declined"}) do
{:error,
%Stripe.Error{
source: :stripe,
Expand All @@ -21,7 +21,7 @@ defmodule Algora.Support.StripeMock do
}}
end

def pay(invoice_id, _params, _opts) do
def pay(invoice_id, _params) do
{:ok, %Stripe.Invoice{id: invoice_id, paid: true, status: "paid"}}
end
end
Expand Down