<commit_before>defmodule CodeCorps.StripeService.StripeConnectPlanService do
  alias CodeCorps.{Project, Repo, StripeConnectPlan}
  alias CodeCorps.StripeService.Adapters.StripeConnectPlanAdapter
  alias CodeCorps.StripeService.Validators.ProjectCanEnableDonations

  @api Application.get_env(:code_corps, :stripe)

  def create(%{"project_id" => project_id} = attributes) do
    with {:ok, %Project{} = project} <- get_project(project_id) |> ProjectCanEnableDonations.validate,
         %{} = create_attributes     <- get_create_attributes(),
         connect_account_id          <- project.organization.stripe_connect_account.id_from_stripe,
         {:ok, plan}                 <- @api.Plan.create(create_attributes, connect_account: connect_account_id),
         {:ok, params}               <- StripeConnectPlanAdapter.to_params(plan, attributes)
    do
      %StripeConnectPlan{}
      |> StripeConnectPlan.create_changeset(params)
      |> Repo.insert
    else
      {:error, :project_not_ready} -> {:error, :project_not_ready}
      {:error, error} -> {:error, error}
      nil -> {:error, :not_found}
    end
  end

  defp get_create_attributes do
    %{
      amount: 1, # in cents
      currency: "usd",
      id: "month",
      interval: "month",
      name: "Monthly donation",
      statement_descriptor: "CODECORPS.ORG Monthly Donation"
    }
  end

  defp get_project(project_id) do
    Project
    |> Repo.get(project_id)
    |> Repo.preload([:donation_goals, {:organization, :stripe_connect_account}])
  end
end<commit_msg>Limit to 22 chars in statement descriptor<commit_after>defmodule CodeCorps.StripeService.StripeConnectPlanService do
  alias CodeCorps.{Project, Repo, StripeConnectPlan}
  alias CodeCorps.StripeService.Adapters.StripeConnectPlanAdapter
  alias CodeCorps.StripeService.Validators.ProjectCanEnableDonations

  @api Application.get_env(:code_corps, :stripe)

  def create(%{"project_id" => project_id} = attributes) do
    with {:ok, %Project{} = project} <- get_project(project_id) |> ProjectCanEnableDonations.validate,
         %{} = create_attributes     <- get_create_attributes(),
         connect_account_id          <- project.organization.stripe_connect_account.id_from_stripe,
         {:ok, plan}                 <- @api.Plan.create(create_attributes, connect_account: connect_account_id),
         {:ok, params}               <- StripeConnectPlanAdapter.to_params(plan, attributes)
    do
      %StripeConnectPlan{}
      |> StripeConnectPlan.create_changeset(params)
      |> Repo.insert
    else
      {:error, :project_not_ready} -> {:error, :project_not_ready}
      {:error, error} -> {:error, error}
      nil -> {:error, :not_found}
    end
  end

  defp get_create_attributes do
    %{
      amount: 1, # in cents
      currency: "usd",
      id: "month",
      interval: "month",
      name: "Monthly donation",
      statement_descriptor: "CODECORPS.ORG Donation" # No more than 22 chars
    }
  end

  defp get_project(project_id) do
    Project
    |> Repo.get(project_id)
    |> Repo.preload([:donation_goals, {:organization, :stripe_connect_account}])
  end
end
{'fraction_non_alphanumeric': 0.11235595390524968, 'fraction_numerical': 0.0019206145966709346, 'mean_word_length': 3.4138418079096047, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5623540', 'n_tokens_mistral': 1039, 'n_tokens_neox': 977, 'n_words': 183}