Module: TenantBilling

Extended by:
ActiveSupport::Concern
Included in:
Tenant
Defined in:
app/models/concerns/tenant_billing.rb

Overview

THIS IS DEAD CODE AS WE WILL NOT BE USING STRIPE FOR THIS BILLING

Constant Summary collapse

BASE_FEE_PRICE_ID =

Sandbox prices, they will later be fetched from the database once we create tables for them Work account

'price_1PaJ7AGLcIwYtoWf72dHnsBK'
MANAGED_COMPANIES_PRICE_ID =
'price_1PaJMLGLcIwYtoWftzGuIlAu'
MANAGED_EMPLOYEES_PRICE_ID =
'price_1PaJJnGLcIwYtoWf3ktoNaUl'

Instance Method Summary collapse

Instance Method Details

#billing_portal_urlObject



105
106
107
108
109
110
111
# File 'app/models/concerns/tenant_billing.rb', line 105

def billing_portal_url
  portal = Stripe::BillingPortal::Session.create({
                                                   customer: stripe_customer_id
                                                   # return_url: "#{AppMode.payroll.web_uri}"
                                                 })
  portal.url
end

#checkout_session_urlObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'app/models/concerns/tenant_billing.rb', line 50

def checkout_session_url
  stripe_customer = find_or_create_stripe_customer

  checkout_session = Stripe::Checkout::Session.create(
    line_items: [
      {
        price: BASE_FEE_PRICE_ID,
        quantity: 1
      },
      {
        price: MANAGED_COMPANIES_PRICE_ID
      },
      {
        price: MANAGED_EMPLOYEES_PRICE_ID
      }
    ],
    customer: stripe_customer.id,
    success_url: "#{AppMode.payroll.web_uri}/onboard/payroll/confirmation?session_id={CHECKOUT_SESSION_ID}",
    cancel_url: "#{AppMode.payroll.web_uri}/onboard/payroll/company_details?tenant_code=#{tenant_code}",
    payment_method_types: ['card'],
    mode: 'subscription',
    automatic_tax: {
      enabled: true
    },
    customer_update: {
      address: 'auto',
      shipping: 'auto'
    }
  )

  checkout_session.url
end

#due_invoicesObject



83
84
85
# File 'app/models/concerns/tenant_billing.rb', line 83

def due_invoices
  @due_invoices ||= Stripe::Invoice.list(customer: stripe_customer_id, status: 'open').data
end

#find_or_create_stripe_customerObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/models/concerns/tenant_billing.rb', line 6

def find_or_create_stripe_customer
  customer_fields = {
    name: tenant_name,
    email: bill_to_email,
    phone: bill_to_phone_no
    # Uncomment when the address becomes valid by using google autocomplete
    # or something to ensure stripe receives a valid address
    # address: {
    #   city: bill_to_city,
    #   country: country.try(:description),
    #   line1: bill_to_address_line1,
    #   line2: bill_to_address_line2,
    #   postal_code: bill_to_postal_code,
    #   state: bill_to_province.try(:description),
    # },
    # shipping: {
    #   address: {
    #     city: deliver_to_city,
    #     country: country.try(:description),
    #     line1: deliver_to_address_line1,
    #     line2: deliver_to_address_line2,
    #     postal_code: deliver_to_postal_code,
    #     state: deliver_to_province.try(:description),
    #   },
    #   name: tenant_name,
    #   phone: deliver_to_phone_no,
    # }
  }
  if stripe_customer_id
    Stripe::Customer.update(stripe_customer_id, customer_fields)
  else
    stripe_customer = Stripe::Customer.create(customer_fields)

    update!(stripe_customer_id: stripe_customer.id)

    stripe_customer
  end
end

#redirect_to_stripe_urlObject



99
100
101
102
103
# File 'app/models/concerns/tenant_billing.rb', line 99

def redirect_to_stripe_url
  return checkout_session_url if should_checkout?

  billing_portal_url if should_manage?
end

#should_checkout?Boolean

Returns:

  • (Boolean)


87
88
89
90
91
# File 'app/models/concerns/tenant_billing.rb', line 87

def should_checkout?
  return true unless stripe_customer_id

  due_invoices.length != 0
end

#should_manage?Boolean

Returns:

  • (Boolean)


93
94
95
96
97
# File 'app/models/concerns/tenant_billing.rb', line 93

def should_manage?
  return false unless stripe_customer_id

  due_invoices.length > 0 && due_invoices.any? { |sub| sub.status != 'active' }
end