|
| 1 | +# Postgresed [WIP] : Teams::Account |
| 2 | +require 'stripe' |
| 3 | + |
| 4 | +module Deprecated |
| 5 | + class Account |
| 6 | + include Mongoid::Document |
| 7 | + include Mongoid::Timestamps |
| 8 | + |
| 9 | + embedded_in :team |
| 10 | + |
| 11 | + field :stripe_card_token |
| 12 | + field :stripe_customer_token |
| 13 | + field :admin_id |
| 14 | + field :trial_end, default: nil |
| 15 | + field :plan_ids, type: Array, default: [] |
| 16 | + |
| 17 | + attr_protected :stripe_customer_token, :admin_id |
| 18 | + |
| 19 | + validate :stripe_customer_token, presence: true |
| 20 | + validate :stripe_card_token, presence: true |
| 21 | + validate :admin_id, :payer_is_team_admin |
| 22 | + |
| 23 | + def payer_is_team_admin |
| 24 | + if admin_id.nil? #or !team.admin?(admin) |
| 25 | + errors.add(:admin_id, "must be team admin to create an account") |
| 26 | + end |
| 27 | + end |
| 28 | + |
| 29 | + def subscribe_to!(plan, force=false) |
| 30 | + self.plan_ids = [plan.id] |
| 31 | + if force || update_on_stripe(plan) |
| 32 | + update_job_post_budget(plan) |
| 33 | + self.team.premium = true unless plan.free? |
| 34 | + self.team.analytics = plan.analytics |
| 35 | + self.team.upgraded_at = Time.now |
| 36 | + end |
| 37 | + team.save! |
| 38 | + end |
| 39 | + |
| 40 | + def save_with_payment(plan=nil) |
| 41 | + if valid? |
| 42 | + create_customer unless plan.try(:one_time?) |
| 43 | + subscribe_to!(plan) unless plan.nil? |
| 44 | + team.save! |
| 45 | + return true |
| 46 | + else |
| 47 | + return false |
| 48 | + end |
| 49 | + rescue Stripe::CardError => e |
| 50 | + # Honeybadger.notify(e) if Rails.env.production? |
| 51 | + Rails.logger.error "Stripe error while creating customer: #{e.message}" if ENV['DEBUG'] |
| 52 | + errors.add :base, e.message |
| 53 | + return false |
| 54 | + rescue Stripe::InvalidRequestError => e |
| 55 | + # Honeybadger.notify(e) if Rails.env.production? |
| 56 | + Rails.logger.error "Stripe error while creating customer: #{e.message}" if ENV['DEBUG'] |
| 57 | + errors.add :base, "There was a problem with your credit card." |
| 58 | + # throw e if Rails.env.development? |
| 59 | + return false |
| 60 | + end |
| 61 | + |
| 62 | + def customer |
| 63 | + Stripe::Customer.retrieve(self.stripe_customer_token) |
| 64 | + end |
| 65 | + |
| 66 | + def admin |
| 67 | + User.find(self.admin_id) |
| 68 | + end |
| 69 | + |
| 70 | + def create_customer |
| 71 | + new_customer = find_or_create_customer |
| 72 | + self.stripe_customer_token = new_customer.id |
| 73 | + end |
| 74 | + |
| 75 | + def find_or_create_customer |
| 76 | + if self.stripe_customer_token |
| 77 | + customer |
| 78 | + else |
| 79 | + Stripe::Customer.create(description: "#{admin.email} for #{self.team.name}", card: stripe_card_token) |
| 80 | + end |
| 81 | + end |
| 82 | + |
| 83 | + def update_on_stripe(plan) |
| 84 | + if plan.subscription? |
| 85 | + update_subscription_on_stripe!(plan) |
| 86 | + else |
| 87 | + charge_on_stripe!(plan) |
| 88 | + end |
| 89 | + end |
| 90 | + |
| 91 | + def update_subscription_on_stripe!(plan) |
| 92 | + customer && customer.update_subscription(plan: plan.stripe_plan_id, trial_end: self.trial_end) |
| 93 | + end |
| 94 | + |
| 95 | + def charge_on_stripe!(plan) |
| 96 | + Stripe::Charge.create( |
| 97 | + amount: plan.amount, |
| 98 | + currency: plan.currency, |
| 99 | + card: self.stripe_card_token, |
| 100 | + description: plan.name |
| 101 | + ) |
| 102 | + end |
| 103 | + |
| 104 | + def update_job_post_budget(plan) |
| 105 | + if plan.free? |
| 106 | + team.paid_job_posts = 0 |
| 107 | + team.monthly_subscription = false |
| 108 | + else |
| 109 | + team.valid_jobs = true |
| 110 | + |
| 111 | + if plan.subscription? |
| 112 | + team.monthly_subscription = true |
| 113 | + else |
| 114 | + team.paid_job_posts += 1 |
| 115 | + team.monthly_subscription = false |
| 116 | + end |
| 117 | + end |
| 118 | + end |
| 119 | + |
| 120 | + def suspend! |
| 121 | + team.premium = false |
| 122 | + team.analytics = false |
| 123 | + team.paid_job_posts = 0 |
| 124 | + team.monthly_subscription = false |
| 125 | + team.valid_jobs = false |
| 126 | + team.save |
| 127 | + team.jobs.map { |job| job.deactivate! } |
| 128 | + end |
| 129 | + |
| 130 | + def add_analytics |
| 131 | + team.analytics = true |
| 132 | + end |
| 133 | + |
| 134 | + def send_invoice(invoice_id) |
| 135 | + NotifierMailer.invoice(self.team.id, nil, invoice_id).deliver |
| 136 | + end |
| 137 | + |
| 138 | + def send_invoice_for(time = Time.now) |
| 139 | + NotifierMailer.invoice(self.team.id, time.to_i).deliver |
| 140 | + end |
| 141 | + |
| 142 | + def invoice_for(time) |
| 143 | + months_ago = ((Time.now.beginning_of_month-time)/1.month).round |
| 144 | + invoices(months_ago).last.to_hash.with_indifferent_access |
| 145 | + end |
| 146 | + |
| 147 | + def invoices(count = 100) |
| 148 | + Stripe::Invoice.all( |
| 149 | + customer: self.stripe_customer_token, |
| 150 | + count: count |
| 151 | + ).data |
| 152 | + end |
| 153 | + |
| 154 | + def current_plan |
| 155 | + Plan.find(self.plan_ids.first) unless self.plan_ids.blank? |
| 156 | + end |
| 157 | + end |
| 158 | +end |
0 commit comments