Bling.Customers (bling v0.4.0)

Module for interacting with Bling customers. Each method expects the first argument to be an ecto struct.

Link to this section Summary

Functions

Returns the stripe customer for the given database customer.

Creates and returns a url to the stripe billing portal.

Creates a setup intent for the given customer. Defaults to off_session usage.

Creates a customer in stripe from the given database customer. If to_stripe_params/1 is present on your Bling module, the map returned there will be merged with the params passed to stripe.

Creates a subscription in stripe and stores it in the database. Tries to confirm the payment immediately. If the payment requires further authentication or errors, the subscription will be marked as incomplete and you must use the payment intent to confirm the payment on your frontend.

Returns the defauly payment method for a customer, or nil. The payment method returned is a Bling.PaymentMethod struct.

Returns whether the customer is on a generic trial.

Returns whether the customer has a default payment method.

Returns all Stripe.Invoice structs for a customer.

Returns whether or not the customer is subscribed to a plan, and that plan is valid.

Returns whether or not the customer is subscribed to a specific price and the subscription is valid.

Returns whether or not the customer is subscribed to a specific product and the subscription is valid.

Fetches a single subscription for a customer by plan name.

Creates and returns a url to make a new subscription using stripe checkout.

Fetches all subscriptions for a customer.

Returns true if the customer is on a generic trial or if the specified subscription is on a trial.

Updates the customers default payment method with the provided payment_method_id.

Updates the customer's default payment method with the one stored in stripe.

Link to this section Functions

Link to this function

as_stripe_customer(customer)

Returns the stripe customer for the given database customer.

Link to this function

billing_portal_url(customer, opts \\ [])

Creates and returns a url to the stripe billing portal.

examples

Examples

url = billing_portal_url(customer, stripe: %{ return_url: "http://localhost:4000/users/settings" } })
Link to this function

create_setup_intent(customer, opts \\ [])

Creates a setup intent for the given customer. Defaults to off_session usage.

examples

Examples

intent = Bling.Customers.create_setup_intent(current_user)

# Can pass any valid Stripe.SetupIntent.create/1 params.
intent = Bling.Customers.create_setup_intent(current_user, stripe: %{ payment_method_types: ["card"] })
Link to this function

create_stripe_customer(customer, opts \\ [])

Creates a customer in stripe from the given database customer. If to_stripe_params/1 is present on your Bling module, the map returned there will be merged with the params passed to stripe.

examples

Examples

# Can pass any valid Stripe.customer.create/1 params.
customer = Bling.Customers.create_stripe_customer(current_user, stripe: %{ name: "Jane Doe" })
Link to this function

create_subscription(customer, opts \\ [])

Creates a subscription in stripe and stores it in the database. Tries to confirm the payment immediately. If the payment requires further authentication or errors, the subscription will be marked as incomplete and you must use the payment intent to confirm the payment on your frontend.

https://stripe.com/docs/billing/subscriptions/overview

examples

Examples

result =
  create_subscription(
    current_user,
    return_url: "http://localhost:4000/billing/user/1/finalize", # the route you configured during installation
    prices: [{price_id, quantity}],
    plan: "default", # can be omitted
    stripe: %{ coupon: "coupon_id" }, # any valid Stripe.Subscription.create/1 params
  )

case result do
  {:ok, subscription} -> # success, db subscription
  {:requires_action, payment_intent} -> # payment requires further authentication
  {:error, error} -> # Stripe.Error, card could have been declined
end
Link to this function

default_payment_method(customer)

Returns the defauly payment method for a customer, or nil. The payment method returned is a Bling.PaymentMethod struct.

Link to this function

generic_trial?(customer)

Returns whether the customer is on a generic trial.

Checks the trial_ends_at column on the customer.

Link to this function

has_default_payment_method?(customer)

Returns whether the customer has a default payment method.

Link to this function

invoices(customer, opts \\ [])

Returns all Stripe.Invoice structs for a customer.

examples

Examples

invoices = invoices(current_user)

# Can pass any valid Stripe.Invoice.list/1 params.
invocies = invoices(current_user, stripe: %{ limit: 10 })
Link to this function

subscribed?(customer, opts \\ [])

Returns whether or not the customer is subscribed to a plan, and that plan is valid.

examples

Examples

# checks if the customer is subscribed to the default plan
Bling.Customers.subscribed?(customer)

# checks if the customer is subscribed to a specific plan
Bling.Customers.subscribed?(customer, plan: "pro")
Link to this function

subscribed_to_price?(customer, price, opts \\ [])

Returns whether or not the customer is subscribed to a specific price and the subscription is valid.

examples

Examples

# checks if the customer is subscribed to a price on the default plan
Bling.Customers.subscribed_to_price?(customer, "price_123")

# checks if the customer is subscribed to a price on a specific plan
Bling.Customers.subscribed_to_price?(customer, "price_123", plan: "pro")
Link to this function

subscribed_to_product?(customer, product, opts \\ [])

Returns whether or not the customer is subscribed to a specific product and the subscription is valid.

examples

Examples

# checks if the customer is subscribed to a product on the default plan
Bling.Customers.subscribed_to_product?(customer, "prod_123")

# checks if the customer is subscribed to a product on a specific plan
Bling.Customers.subscribed_to_product?(customer, "prod_123", plan: "pro")
Link to this function

subscription(customer, opts \\ [])

Fetches a single subscription for a customer by plan name.

examples

Examples

# gets subscription with name "default"
subscription = Bling.Customers.subscription(customer)

# gets subscription with specific name
subscription = Bling.Customers.subscription(customer, plan: "pro")
Link to this function

subscription_checkout_url(customer, opts \\ [])

Creates and returns a url to make a new subscription using stripe checkout.

examples

Examples

url = subscription_checkout_url(customer,
  plan: "default",
  prices: [{price_id, quantity}]
  stripe: %{
    cancel_url: "http://localhost:4000/users/settings",
    success_url: "http://localhost:4000/users/settings"
})
Link to this function

subscriptions(customer)

Fetches all subscriptions for a customer.

Link to this function

trial?(customer, opts \\ [])

Returns true if the customer is on a generic trial or if the specified subscription is on a trial.

examples

Examples

# checks trial_ends_at on customer and "default" subscription plan
Bling.Customers.trial?(customer)

# checks trial_ends_at on customer and "swimming" subscription plan
Bling.Customers.trial?(customer, plan: "swimming")
Link to this function

update_default_payment_method(customer, payment_method_id)

Updates the customers default payment method with the provided payment_method_id.

Link to this function

update_default_payment_method_from_stripe(customer)

Updates the customer's default payment method with the one stored in stripe.