Skip to content

Instantly share code, notes, and snippets.

@aloukissas
Created February 4, 2020 07:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aloukissas/6aa45179dd63b313a6d9e71eabf176d8 to your computer and use it in GitHub Desktop.
Save aloukissas/6aa45179dd63b313a6d9e71eabf176d8 to your computer and use it in GitHub Desktop.
def create_subscription(email, plan_id, payment_method_id) do
with %User{customer_id: nil, name: name} = user <-
Repo.get_by(User, email: email),
{:ok, %Stripe.Customer{id: customer_id}} <-
Stripe.Customer.create(%{
name: name,
email: email,
payment_method: payment_method_id,
invoice_settings: %{
default_payment_method: payment_method_id
}
}),
{:ok, %Stripe.Subscription{id: subscription_id}} <-
Stripe.Subscription.create(%{customer: customer_id, items: [%{plan: plan_id}]}),
{:ok, %User{}} <-
User.update(user, %{customer_id: customer_id, subscription_id: subscription_id}) do
{:ok, :subscription_created}
else
err ->
{:error, err}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment