app/models/tang/subscription.rb in tang-0.1.0 vs app/models/tang/subscription.rb in tang-0.2.0

- old
+ new

@@ -46,36 +46,48 @@ before_save :check_for_upgrade after_save :handle_upgrade STATUSES = ['trialing', 'active', 'past_due', 'canceled', 'unpaid'] - def self.from_stripe(stripe_subscription, customer, plan) + def self.from_stripe(stripe_subscription) + customer = Tang.customer_class.find_by(stripe_id: stripe_subscription.customer) + plan = Plan.find_by(stripe_id: stripe_subscription.plan.id) + if customer.present? && plan.present? + subscription = Subscription.build(stripe_subscription, customer, plan) + subscription.update(coupon: nil, coupon_start: nil) if stripe_subscription.discount.nil? + return subscription + end + return nil + end + + def self.build(stripe_subscription, customer, plan) subscription = Subscription.find_or_create_by(stripe_id: stripe_subscription.id) do |s| s.customer = customer s.plan = plan s.application_fee_percent = stripe_subscription.application_fee_percent s.quantity = stripe_subscription.quantity # s.tax_percent = stripe_subscription.tax_percent # removed from api in favor of tax rates - s.trial_end = stripe_subscription.trial_end + s.trial_end = DateTime.strptime(stripe_subscription.trial_end.to_s, '%s') if stripe_subscription.trial_end.present? s.coupon = Coupon.find_by(stripe_id: stripe_subscription.discount.coupon.id) if stripe_subscription.discount.present? s.status = stripe_subscription.status end return subscription end def period_start - invoice = invoices.last + invoice = invoices.order(:date).last if invoice.present? - return invoice.period_start + # return invoice.period_start + return invoice.date end return created_at end def period_end - invoice = invoices.last - if invoice.present? - return invoice.period_end - end + # invoice = invoices.order(:period_start).last + # if invoice.present? + # return invoice.period_end + # end return plan.period_days_from(period_start) end def quantity self[:quantity] || 1