Module: SpreeStripeSubscriptions::UserDecorator

Defined in:
app/models/spree_stripe_subscriptions/user_decorator.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.prepended(base) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'app/models/spree_stripe_subscriptions/user_decorator.rb', line 5

def self.prepended(base)
  base.has_many :stripe_customers,
                class_name: 'Spree::StripeCustomer',
                foreign_key: :user_id,
                inverse_of: :user,
                dependent: :restrict_with_error
  base.has_many :stripe_subscriptions,
                class_name: 'Spree::StripeSubscription',
                foreign_key: :user_id,
                inverse_of: :user,
                dependent: :restrict_with_error
  base.has_many :stripe_subscription_events,
                class_name: 'Spree::StripeSubscriptionEvent',
                foreign_key: :user_id,
                inverse_of: :user,
                dependent: :restrict_with_error
  base.has_many :stripe_invoices,
                class_name: 'Spree::StripeInvoice',
                foreign_key: :user_id,
                inverse_of: :user,
                dependent: :restrict_with_error
end

Instance Method Details

#find_or_create_stripe_customerObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/models/spree_stripe_subscriptions/user_decorator.rb', line 33

def find_or_create_stripe_customer
  current_customer = stripe_customers.last
  stripe_customer = current_customer&.stripe_customer
  return stripe_customer if stripe_customer

  stripe_customer = Stripe::Customer.create(
    email: email,
    name: probable_name,
    metadata: { user_id: id }
  )
  stripe_customers.create(stripe_customer_id: stripe_customer.id)

  stripe_customer
end

#probable_nameObject



28
29
30
31
# File 'app/models/spree_stripe_subscriptions/user_decorator.rb', line 28

def probable_name
  name = email[/[^@]+/]
  name.split('.').map(&:capitalize).join(' ')
end