Class: Tang::CreateSubscription

Inherits:
Object
  • Object
show all
Defined in:
app/services/tang/create_subscription.rb

Class Method Summary collapse

Class Method Details

.call(plan, customer, token) ⇒ Object



3
4
5
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
44
45
46
# File 'app/services/tang/create_subscription.rb', line 3

def self.call(plan, customer, token)
  subscription = Subscription.new(
    plan: plan, 
    customer: customer, 
    coupon: customer.subscription_coupon
  )
  return subscription if plan.nil? || customer.nil?

  # TODO: Check for token presence. 
  # A nil token will throw an error when calling create_stripe_subscription 
  # because the customer does not have a payment method.

  begin
    if customer.stripe_id.blank?
      # Create a new subscription and customer
      stripe_customer = create_stripe_customer(customer, token)
      customer.stripe_id = stripe_customer.id
    else
      # Update the payment method
      stripe_customer = update_stripe_customer(customer, token)
    end

    # Subscribe
    stripe_sub = create_stripe_subscription(customer, plan)

    # Save the subscription
    subscription.stripe_id = stripe_sub.id
    subscription.trial_end = DateTime.strptime(stripe_sub.trial_end.to_s, '%s') if stripe_sub.trial_end.present?
    subscription.activate!

    # Save the payment method
    stripe_card = Stripe::Customer.retrieve_source(
      stripe_customer.id,
      stripe_customer.default_source,
    )

    # Finalize customer with subscription and payment method
    finalize_customer(customer, subscription, stripe_card)

  rescue Stripe::StripeError => e
    subscription.errors.add(:base, :invalid, message: e.message)
  end
  return subscription
end

.create_stripe_customer(customer, token) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'app/services/tang/create_subscription.rb', line 48

def self.create_stripe_customer(customer, token)
  if customer.coupon.present?
    stripe_customer = Stripe::Customer.create(
      source: token,
      email: customer.email,
      coupon: customer.coupon.stripe_id
    )
  else
    stripe_customer = Stripe::Customer.create(
      source: token,
      email: customer.email
    )
  end
  return stripe_customer
end

.create_stripe_subscription(customer, plan) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'app/services/tang/create_subscription.rb', line 74

def self.create_stripe_subscription(customer, plan)
  if customer.subscription_coupon.present?
    stripe_sub = Stripe::Subscription.create(
      customer: customer.stripe_id,
      plan: plan.stripe_id,
      coupon: customer.subscription_coupon.stripe_id
    )
  else
    stripe_sub = Stripe::Subscription.create(
      customer: customer.stripe_id,
      plan: plan.stripe_id
    )
  end
  return stripe_sub
end

.finalize_customer(customer, subscription, stripe_card) ⇒ Object



90
91
92
93
94
95
96
97
# File 'app/services/tang/create_subscription.rb', line 90

def self.finalize_customer(customer, subscription, stripe_card)
  # Remove temporary coupon
  customer.subscription_coupon = nil
  # Save subscription data to customer
  customer.update_subscription_end(subscription)
  # Save the payment method
  customer.update_card_from_stripe(stripe_card)
end

.update_stripe_customer(customer, token) ⇒ Object



64
65
66
67
68
69
70
71
72
# File 'app/services/tang/create_subscription.rb', line 64

def self.update_stripe_customer(customer, token)
  # Update the payment method
  stripe_customer = Stripe::Customer.retrieve(customer.stripe_id)
  if token.present?
    stripe_customer.source = token
    stripe_customer.save
  end
  return stripe_customer
end