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
2 changes: 1 addition & 1 deletion lib/algora/admin/admin.ex
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ defmodule Algora.Admin do
with account_id when is_binary(account_id) <- Algora.config([:stripe, :test_account_id]),
{:ok, user} <- Repo.fetch_by(User, handle: user_handle),
{:ok, acct} <- Payments.create_account(user, "US"),
{:ok, stripe_acct} <- Stripe.Account.retrieve(account_id, []) do
{:ok, stripe_acct} <- Algora.PSP.Account.retrieve(account_id) do
Payments.update_account(acct, stripe_acct)
end
end
Expand Down
7 changes: 4 additions & 3 deletions lib/algora/bounties/bounties.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ defmodule Algora.Bounties do
alias Algora.Organizations.Member
alias Algora.Payments
alias Algora.Payments.Transaction
alias Algora.PSP
alias Algora.Repo
alias Algora.Util
alias Algora.Workspace
Expand Down Expand Up @@ -649,7 +650,7 @@ defmodule Algora.Bounties do
recipient: User.t()
]
) ::
{:ok, Stripe.Invoice.t()} | {:error, atom()}
{:ok, PSP.invoice()} | {:error, atom()}
def create_invoice(%{owner: owner, amount: amount}, opts \\ []) do
tx_group_id = Nanoid.generate()

Expand Down Expand Up @@ -685,15 +686,15 @@ defmodule Algora.Bounties do
}),
{:ok, customer} <- Payments.fetch_or_create_customer(owner),
{:ok, invoice} <-
Algora.Stripe.Invoice.create(%{
PSP.Invoice.create(%{
auto_advance: false,
customer: customer.provider_id
}),
{:ok, _line_items} <-
line_items
|> Enum.map(&LineItem.to_invoice_item(&1, invoice, customer))
|> Enum.reduce_while({:ok, []}, fn params, {:ok, acc} ->
case Algora.Stripe.Invoiceitem.create(params) do
case PSP.Invoiceitem.create(params) do
{:ok, item} -> {:cont, {:ok, [item | acc]}}
{:error, error} -> {:halt, {:error, error}}
end
Expand Down
10 changes: 5 additions & 5 deletions lib/algora/contracts/contracts.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ defmodule Algora.Contracts do
alias Algora.Payments
alias Algora.Payments.Account
alias Algora.Payments.Transaction
alias Algora.PSP.Invoice
alias Algora.Repo
alias Algora.Stripe
alias Algora.Util

require Algora.SQL
Expand Down Expand Up @@ -395,7 +395,7 @@ defmodule Algora.Contracts do
defp maybe_generate_invoice(contract, charge) do
invoice_params = %{auto_advance: false, customer: contract.client.customer.provider_id}

with {:ok, invoice} <- Stripe.Invoice.create(invoice_params),
with {:ok, invoice} <- Invoice.create(invoice_params),
{:ok, _line_items} <- create_line_items(contract, invoice, charge.line_items) do
{:ok, invoice}
end
Expand Down Expand Up @@ -442,7 +442,7 @@ defmodule Algora.Contracts do

defp create_line_items(contract, invoice, line_items) do
Enum.reduce_while(line_items, {:ok, []}, fn line_item, {:ok, acc} ->
case Stripe.Invoiceitem.create(%{
case Algora.PSP.Invoiceitem.create(%{
invoice: invoice.id,
customer: contract.client.customer.provider_id,
amount: MoneyUtils.to_minor_units(line_item.amount),
Expand All @@ -460,7 +460,7 @@ defmodule Algora.Contracts do
defp maybe_pay_invoice(contract, invoice, txs) do
pm_id = contract.client.customer.default_payment_method.provider_id

case Stripe.Invoice.pay(invoice.id, %{off_session: true, payment_method: pm_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 All @@ -482,7 +482,7 @@ defmodule Algora.Contracts do
defp transfer_funds(contract, %Transaction{type: :transfer} = transaction) when transaction.status != :succeeded do
with {:ok, account} <- Repo.fetch_by(Account, user_id: transaction.user_id),
{:ok, stripe_transfer} <-
Stripe.Transfer.create(%{
Algora.PSP.Transfer.create(%{
amount: MoneyUtils.to_minor_units(transaction.net_amount),
currency: to_string(transaction.net_amount.currency),
destination: account.provider_id
Expand Down
47 changes: 0 additions & 47 deletions lib/algora/integrations/stripe/stripe.ex

This file was deleted.

60 changes: 30 additions & 30 deletions lib/algora/payments/payments.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ defmodule Algora.Payments do
alias Algora.Payments.Jobs
alias Algora.Payments.PaymentMethod
alias Algora.Payments.Transaction
alias Algora.PSP
alias Algora.Repo
alias Algora.Stripe.ConnectCountries
alias Algora.Util

require Logger
Expand All @@ -28,12 +28,12 @@ defmodule Algora.Payments do
end

@spec create_stripe_session(
line_items :: [Stripe.Session.line_item_data()],
payment_intent_data :: Stripe.Session.payment_intent_data()
line_items :: [PSP.Session.line_item_data()],
payment_intent_data :: PSP.Session.payment_intent_data()
) ::
{:ok, Stripe.Session.t()} | {:error, Stripe.Error.t()}
{:ok, PSP.session()} | {:error, PSP.error()}
def create_stripe_session(line_items, payment_intent_data) do
Algora.Stripe.Session.create(%{
PSP.Session.create(%{
mode: "payment",
billing_address_collection: "required",
line_items: line_items,
Expand All @@ -59,7 +59,7 @@ defmodule Algora.Payments do
end

def get_provider_fee_from_invoice(%{id: id}) do
case Stripe.Invoice.retrieve(id, expand: ["charge.balance_transaction"]) do
case PSP.Invoice.retrieve(id, expand: ["charge.balance_transaction"]) do
{:ok, invoice} ->
get_provider_fee_from_balance_transaction(invoice.charge.balance_transaction)

Expand All @@ -71,7 +71,7 @@ defmodule Algora.Payments do
# TODO: This is not used anymore
def get_provider_fee_from_payment_intent(pi) do
with [ch] <- pi.charges.data,
{:ok, txn} <- Stripe.BalanceTransaction.retrieve(ch.balance_transaction) do
{:ok, txn} <- PSP.BalanceTransaction.retrieve(ch.balance_transaction) do
get_provider_fee_from_balance_transaction(txn)
else
_ -> nil
Expand Down Expand Up @@ -153,7 +153,7 @@ defmodule Algora.Payments do
end

@spec fetch_or_create_customer(user :: User.t()) ::
{:ok, Customer.t()} | {:error, Ecto.Changeset.t()} | {:error, Stripe.Error.t()}
{:ok, Customer.t()} | {:error, Ecto.Changeset.t()} | {:error, PSP.error()}
def fetch_or_create_customer(user) do
case fetch_customer_by(user_id: user.id) do
{:ok, customer} -> {:ok, customer}
Expand All @@ -162,9 +162,9 @@ defmodule Algora.Payments do
end

@spec create_customer(user :: User.t()) ::
{:ok, Customer.t()} | {:error, Ecto.Changeset.t()} | {:error, Stripe.Error.t()}
{:ok, Customer.t()} | {:error, Ecto.Changeset.t()} | {:error, PSP.error()}
def create_customer(user) do
with {:ok, stripe_customer} <- Stripe.Customer.create(%{name: user.name}) do
with {:ok, stripe_customer} <- PSP.Customer.create(%{name: user.name}) do
%Customer{}
|> Customer.changeset(%{
provider: "stripe",
Expand All @@ -177,7 +177,7 @@ defmodule Algora.Payments do
end
end

@spec create_payment_method(customer :: Customer.t(), payment_method :: Stripe.PaymentMethod.t()) ::
@spec create_payment_method(customer :: Customer.t(), payment_method :: PSP.payment_method()) ::
{:ok, PaymentMethod.t()} | {:error, Ecto.Changeset.t()}
def create_payment_method(customer, payment_method) do
%PaymentMethod{}
Expand All @@ -193,9 +193,9 @@ defmodule Algora.Payments do
end

@spec create_stripe_setup_session(customer :: Customer.t(), success_url :: String.t(), cancel_url :: String.t()) ::
{:ok, Stripe.Session.t()} | {:error, Stripe.Error.t()}
{:ok, PSP.session()} | {:error, PSP.error()}
def create_stripe_setup_session(customer, success_url, cancel_url) do
Stripe.Session.create(%{
PSP.Session.create(%{
billing_address_collection: "required",
mode: "setup",
payment_method_types: ["card"],
Expand Down Expand Up @@ -223,7 +223,7 @@ defmodule Algora.Payments do
@spec create_account(user :: User.t(), country :: String.t()) ::
{:ok, Account.t()} | {:error, Ecto.Changeset.t()}
def create_account(user, country) do
type = ConnectCountries.account_type(country)
type = PSP.ConnectCountries.account_type(country)

with {:ok, stripe_account} <- create_stripe_account(%{country: country, type: type}) do
attrs = %{
Expand All @@ -242,18 +242,18 @@ defmodule Algora.Payments do
end

@spec create_stripe_account(attrs :: map()) ::
{:ok, Stripe.Account.t()} | {:error, Stripe.Error.t()}
{:ok, PSP.account()} | {:error, PSP.error()}
defp create_stripe_account(%{country: country, type: type}) do
case Stripe.Account.create(%{country: country, type: to_string(type)}) do
case PSP.Account.create(%{country: country, type: to_string(type)}) do
{:ok, account} -> {:ok, account}
{:error, _reason} -> Stripe.Account.create(%{type: to_string(type)})
{:error, _reason} -> PSP.Account.create(%{type: to_string(type)})
end
end

@spec create_account_link(account :: Account.t(), base_url :: String.t()) ::
{:ok, Stripe.AccountLink.t()} | {:error, Stripe.Error.t()}
{:ok, PSP.account_link()} | {:error, PSP.error()}
def create_account_link(account, base_url) do
Stripe.AccountLink.create(%{
PSP.AccountLink.create(%{
account: account.provider_id,
refresh_url: "#{base_url}/callbacks/stripe/refresh",
return_url: "#{base_url}/callbacks/stripe/return",
Expand All @@ -262,12 +262,12 @@ defmodule Algora.Payments do
end

@spec create_login_link(account :: Account.t()) ::
{:ok, Stripe.LoginLink.t()} | {:error, Stripe.Error.t()}
{:ok, PSP.login_link()} | {:error, PSP.error()}
def create_login_link(account) do
Stripe.LoginLink.create(account.provider_id, %{})
PSP.LoginLink.create(account.provider_id)
end

@spec update_account(account :: Account.t(), stripe_account :: Stripe.Account.t()) ::
@spec update_account(account :: Account.t(), stripe_account :: PSP.account()) ::
{:ok, Account.t()} | {:error, Ecto.Changeset.t()}
def update_account(account, stripe_account) do
account
Expand All @@ -288,10 +288,10 @@ defmodule Algora.Payments do
end

@spec refresh_stripe_account(user :: User.t()) ::
{:ok, Account.t()} | {:error, Ecto.Changeset.t()} | {:error, :not_found} | {:error, Stripe.Error.t()}
{:ok, Account.t()} | {:error, Ecto.Changeset.t()} | {:error, :not_found} | {:error, PSP.error()}
def refresh_stripe_account(user) do
with {:ok, account} <- fetch_account(user),
{:ok, stripe_account} <- Stripe.Account.retrieve(account.provider_id, []),
{:ok, stripe_account} <- PSP.Account.retrieve(account.provider_id),
{:ok, updated_account} <- update_account(account, stripe_account) do
user = Accounts.get_user(account.user_id)

Expand All @@ -303,25 +303,25 @@ defmodule Algora.Payments do
end
end

@spec get_service_agreement(account :: Stripe.Account.t()) :: String.t()
@spec get_service_agreement(account :: PSP.account()) :: String.t()
defp get_service_agreement(%{tos_acceptance: %{service_agreement: agreement}} = _account) when not is_nil(agreement) do
agreement
end

@spec get_service_agreement(account :: Stripe.Account.t()) :: String.t()
@spec get_service_agreement(account :: PSP.account()) :: String.t()
defp get_service_agreement(%{capabilities: capabilities}) do
if is_nil(capabilities[:card_payments]), do: "recipient", else: "full"
end

@spec delete_account(account :: Account.t()) :: {:ok, Account.t()} | {:error, Ecto.Changeset.t()}
def delete_account(account) do
with {:ok, _stripe_account} <- Stripe.Account.delete(account.provider_id) do
with {:ok, _stripe_account} <- PSP.Account.delete(account.provider_id) do
Repo.delete(account)
end
end

@spec execute_pending_transfer(credit_id :: String.t()) ::
{:ok, Stripe.Transfer.t()} | {:error, :not_found} | {:error, :duplicate_transfer_attempt}
{:ok, PSP.transfer()} | {:error, :not_found} | {:error, :duplicate_transfer_attempt}
def execute_pending_transfer(credit_id) do
with {:ok, credit} <- Repo.fetch_by(Transaction, id: credit_id, type: :credit, status: :succeeded) do
transfers =
Expand Down Expand Up @@ -391,7 +391,7 @@ defmodule Algora.Payments do
end
end

@spec initialize_and_execute_transfer(credit :: Transaction.t()) :: {:ok, Stripe.Transfer.t()} | {:error, term()}
@spec initialize_and_execute_transfer(credit :: Transaction.t()) :: {:ok, PSP.transfer()} | {:error, term()}
defp initialize_and_execute_transfer(%Transaction{} = credit) do
case fetch_active_account(credit.user_id) do
{:ok, account} ->
Expand Down Expand Up @@ -452,7 +452,7 @@ defmodule Algora.Payments do
|> Map.merge(if charge && charge.provider_id, do: %{source_transaction: charge.provider_id}, else: %{})

# TODO: provide idempotency key
case Algora.Stripe.Transfer.create(transfer_params) do
case PSP.Transfer.create(transfer_params) do
{:ok, transfer} ->
# it's fine if this fails since we'll receive a webhook
transaction
Expand Down
3 changes: 1 addition & 2 deletions lib/algora/payments/schemas/account.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ defmodule Algora.Payments.Account do
use Algora.Schema

alias Algora.Activities.Activity
alias Algora.Stripe

@derive {Inspect, except: [:provider_meta]}
typed_schema "accounts" do
Expand Down Expand Up @@ -60,7 +59,7 @@ defmodule Algora.Payments.Account do
:user_id
])
|> validate_inclusion(:type, [:standard, :express])
|> validate_inclusion(:country, Stripe.ConnectCountries.list_codes())
|> validate_inclusion(:country, Algora.PSP.ConnectCountries.list_codes())
|> foreign_key_constraint(:user_id)
|> generate_id()
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule Algora.Stripe.ConnectCountries do
defmodule Algora.PSP.ConnectCountries do
@moduledoc false

@spec list() :: [{String.t(), String.t()}]
Expand Down
Loading