Class: Tang::CreateSubscription
- Inherits:
-
Object
- Object
- Tang::CreateSubscription
- Defined in:
- app/services/tang/create_subscription.rb
Class Method Summary collapse
- .call(plan, customer, token) ⇒ Object
- .create_stripe_customer(customer, token) ⇒ Object
- .create_stripe_subscription(customer, plan) ⇒ Object
- .finalize_customer(customer, subscription, stripe_card) ⇒ Object
- .update_stripe_customer(customer, token) ⇒ Object
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 47 |
# File 'app/services/tang/create_subscription.rb', line 3 def self.call(plan, customer, token) puts "CreateSubscription.call(#{plan}, #{customer}, #{token})" subscription = Subscription.new( plan: plan, customer: customer, coupon: customer.subscription_coupon ) return subscription if plan.nil? || customer.nil? # 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. return subscription if token.nil? && customer.stripe_id.blank? 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.) end subscription end |
.create_stripe_customer(customer, token) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'app/services/tang/create_subscription.rb', line 49 def self.create_stripe_customer(customer, token) puts "create_stripe_customer(#{customer}, #{token})" if customer.coupon.present? Stripe::Customer.create( source: token, email: customer.email, coupon: customer.coupon.stripe_id ) else Stripe::Customer.create( source: token, email: customer.email ) end end |
.create_stripe_subscription(customer, plan) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'app/services/tang/create_subscription.rb', line 76 def self.create_stripe_subscription(customer, plan) puts "create_stripe_subscription(#{customer}, #{plan})" if customer.subscription_coupon.present? Stripe::Subscription.create( customer: customer.stripe_id, plan: plan.stripe_id, coupon: customer.subscription_coupon.stripe_id ) else Stripe::Subscription.create( customer: customer.stripe_id, plan: plan.stripe_id ) end end |
.finalize_customer(customer, subscription, stripe_card) ⇒ Object
92 93 94 95 96 97 98 99 100 |
# File 'app/services/tang/create_subscription.rb', line 92 def self.finalize_customer(customer, subscription, stripe_card) puts "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
65 66 67 68 69 70 71 72 73 74 |
# File 'app/services/tang/create_subscription.rb', line 65 def self.update_stripe_customer(customer, token) puts "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 stripe_customer end |