View Source Glific.Partners.Billing (Glific v5.1.6)

We will use this as the main context interface for all billing subscriptions and the stripe interface.

Link to this section Summary

Functions

Standard changeset pattern we use for all data types

Create a billing record in glific, a billing customer in Stripe, given an organization

Create a billing record

Create a monthly subscription. Once the organization has entered a new payment card we create a subscription for it. We'll do updating the card in a separate function

create subscription on the basis of billing period

Create a quarterly subscription.

create subscription on the basis of billing period

Adding credit to customer in Stripe

fetches customer portal url of organization with billing status as active

Delete the billing record

Retrieve a billing record by clauses

Validate entered coupon code and return with coupon details

List of available billing period

A common function for making Stripe API calls with params that are not supported withing Stripity Stripe

Fetch the stripe id's

This is called on a regular schedule to update usage.

Fetch the stripe id's

Record the usage for a specific organization from start_date to end_date both dates inclusive

Stripe subscription created callback via webhooks. We are using this to update the prorate data with monthly billing.

Fetch the stripe tax rates

Update the billing record

Update organization and stripe customer with the current payment method as returned by stripe

Update the stripe customer details record

Update organization subscription plan

Update subscription details. We will also use this method while updating the details form webhook.

Update the usage record for all active subscriptions on a daily and weekly basis

Link to this section Types

@type t() :: %Glific.Partners.Billing{
  __meta__: Ecto.Schema.Metadata.t(),
  billing_period: String.t() | nil,
  currency: String.t() | nil,
  deduct_tds: boolean() | false,
  email: String.t() | nil,
  id: non_neg_integer() | nil,
  inserted_at: :utc_datetime | nil,
  is_active: boolean() | true,
  is_delinquent: boolean(),
  name: String.t() | nil,
  organization: term(),
  organization_id: term(),
  stripe_current_period_end: DateTime.t() | nil,
  stripe_current_period_start: DateTime.t() | nil,
  stripe_customer_id: String.t() | nil,
  stripe_last_usage_recorded: DateTime.t() | nil,
  stripe_payment_method_id: String.t() | nil,
  stripe_subscription_id: String.t() | nil,
  stripe_subscription_items: map(),
  stripe_subscription_status: String.t() | nil,
  tds_amount: float() | nil,
  updated_at: :utc_datetime | nil
}

Link to this section Functions

Link to this function

changeset(billing, attrs)

View Source
@spec changeset(t(), map()) :: Ecto.Changeset.t()

Standard changeset pattern we use for all data types

Link to this function

create(organization, attrs)

View Source
@spec create(Glific.Partners.Organization.t(), map()) ::
  {:ok, t()} | {:error, Ecto.Changeset.t() | String.t()}

Create a billing record in glific, a billing customer in Stripe, given an organization

Link to this function

create_billing(attrs \\ %{})

View Source
@spec create_billing(map()) :: {:ok, t()} | {:error, Ecto.Changeset.t()}

Create a billing record

Link to this function

create_monthly_subscription(organization, billing, params)

View Source
@spec create_monthly_subscription(Glific.Partners.Organization.t(), map(), map()) ::
  {:ok, Stripe.Subscription.t()} | {:pending, map()} | {:error, String.t()}

Create a monthly subscription. Once the organization has entered a new payment card we create a subscription for it. We'll do updating the card in a separate function

Link to this function

create_period_based_subscription(organization, billing, params)

View Source
@spec create_period_based_subscription(Glific.Partners.Organization.t(), map(), map()) ::
  {:ok, Stripe.Subscription.t()} | {:pending, map()} | {:error, String.t()}

create subscription on the basis of billing period

Link to this function

create_quarterly_subscription(organization, billing)

View Source
@spec create_quarterly_subscription(Glific.Partners.Organization.t(), map()) ::
  {:ok, Stripe.Subscription.t()} | {:pending, map()} | {:error, String.t()}

Create a quarterly subscription.

Link to this function

create_subscription(organization, params)

View Source
@spec create_subscription(Glific.Partners.Organization.t(), map()) ::
  {:ok, Stripe.Subscription.t()} | {:pending, map()} | {:error, String.t()}

create subscription on the basis of billing period

Link to this function

credit_customer(transaction)

View Source
@spec credit_customer(map()) :: any() | non_neg_integer()

Adding credit to customer in Stripe

Link to this function

customer_portal_link(billing)

View Source
@spec customer_portal_link(t()) :: {:ok, any()} | {:error, String.t()}

fetches customer portal url of organization with billing status as active

@spec delete_billing(t()) :: {:ok, t()} | {:error, Ecto.Changeset.t()}

Delete the billing record

@spec get_billing(map()) :: t() | nil

Retrieve a billing record by clauses

@spec get_promo_codes(any()) :: any()

Validate entered coupon code and return with coupon details

@spec list_billing_period() :: [String.t()]

List of available billing period

Link to this function

make_stripe_request(endpoint, method, params, opts \\ [])

View Source
@spec make_stripe_request(String.t(), atom(), map(), list()) :: any()

A common function for making Stripe API calls with params that are not supported withing Stripity Stripe

@spec monthly_stripe_ids() :: map()

Fetch the stripe id's

Link to this function

period_usage(record_date)

View Source
@spec period_usage(DateTime.t()) :: :ok

This is called on a regular schedule to update usage.

@spec quarterly_stripe_ids() :: map()

Fetch the stripe id's

Link to this function

record_usage(organization_id, start_date, end_date)

View Source
@spec record_usage(non_neg_integer(), DateTime.t(), DateTime.t()) :: :ok

Record the usage for a specific organization from start_date to end_date both dates inclusive

Link to this function

subscription_created_callback(subscription, org_id)

View Source
@spec subscription_created_callback(Stripe.Subscription.t(), non_neg_integer()) ::
  :ok | {:error, Stripe.Error.t()}

Stripe subscription created callback via webhooks. We are using this to update the prorate data with monthly billing.

@spec tax_rates() :: list()

Fetch the stripe tax rates

Link to this function

update_billing(billing, attrs)

View Source
@spec update_billing(t(), map()) :: {:ok, t()} | {:error, Ecto.Changeset.t()}

Update the billing record

Link to this function

update_payment_method(organization, stripe_payment_method_id)

View Source
@spec update_payment_method(Glific.Partners.Organization.t(), String.t()) ::
  {:ok, t()} | {:error, map()}

Update organization and stripe customer with the current payment method as returned by stripe

Link to this function

update_stripe_customer(billing, attrs)

View Source
@spec update_stripe_customer(t(), map()) :: {:ok, t()} | {:error, Stripe.Error.t()}

Update the stripe customer details record

Link to this function

update_subscription(billing, organization)

View Source
@spec update_subscription(t(), Glific.Partners.Organization.t()) ::
  Glific.Partners.Organization.t()

Update organization subscription plan

Link to this function

update_subscription_details(subscription, organization_id, billing)

View Source
@spec update_subscription_details(
  Stripe.Subscription.t(),
  non_neg_integer(),
  t() | nil
) ::
  {:ok, Stripe.Subscription.t()} | {:error, String.t()}

Update subscription details. We will also use this method while updating the details form webhook.

Link to this function

update_usage(organization_id, map)

View Source
@spec update_usage(non_neg_integer(), map()) :: :ok

Update the usage record for all active subscriptions on a daily and weekly basis