Module: Billable
- Defined in:
- lib/billable.rb
Class Method Summary collapse
Instance Method Summary collapse
- #apply_coupon(coupon) ⇒ Object
- #as_stripe_customer ⇒ Object
- #charge(amount, options = {}) ⇒ Object
- #create_as_stripe_customer(token, options = {}) ⇒ Object
- #download_invoice(id, data, storage_path = nil) ⇒ Object
- #find_invoice(id) ⇒ Object
- #find_invoice_or_fail(id) ⇒ Object
- #get_subscription(subscription = 'default') ⇒ Object
- #has_stripe_id ⇒ Object
- #has_subscription_on_trial?(subscription) ⇒ Boolean
- #invoice ⇒ Object
- #invoice_for(description, amount, options = {}) ⇒ Object
- #invoices(include_pending = false, parameters = {}) ⇒ Object
- #invoices_including_pending(parameters = []) ⇒ Object
- #new_subscription(subscription, plan, *args) ⇒ Object
- #on_generic_trial? ⇒ Boolean
- #on_plan(plan) ⇒ Object
- #on_trial?(subscription = 'default', plan = nil) ⇒ Boolean
- #preferred_currency ⇒ Object
- #refund(charge, options = {}) ⇒ Object
- #subscribed?(subscription = 'default', plan = nil) ⇒ Boolean
- #subscribed_to_plan?(subscription = 'default', plan) ⇒ Boolean
- #subscription(name) ⇒ Object
- #tax_percentage ⇒ Object
- #upcoming_invoice ⇒ Object
- #update_card(token) ⇒ Object
Class Method Details
.included(base) ⇒ Object
5 6 7 8 9 10 |
# File 'lib/billable.rb', line 5 def self.included(base) base.class_eval do has_many :subscriptions Stripe.api_key = ENV["STRIPE_API_KEY"] end end |
Instance Method Details
#apply_coupon(coupon) ⇒ Object
163 164 165 166 167 |
# File 'lib/billable.rb', line 163 def apply_coupon(coupon) customer = as_stripe_customer customer.coupon = coupon customer.save end |
#as_stripe_customer ⇒ Object
193 194 195 |
# File 'lib/billable.rb', line 193 def as_stripe_customer Stripe::Customer.retrieve(stripe_id) end |
#charge(amount, options = {}) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/billable.rb', line 12 def charge(amount, = {}) .merge!({ currency: preferred_currency }) [:amount] = amount if !.key?('source') && stripe_id [:customer] = stripe_id end if !.key?('source') && !.key?('customer') raise 'No payment source provided.' end Stripe::Charge.create() end |
#create_as_stripe_customer(token, options = {}) ⇒ Object
184 185 186 187 188 189 190 191 |
# File 'lib/billable.rb', line 184 def create_as_stripe_customer(token, = {}) .merge!({ email: email }) customer = Stripe::Customer.create() self.stripe_id = customer.id self.save update_card(token) unless token.nil? customer end |
#download_invoice(id, data, storage_path = nil) ⇒ Object
118 119 120 |
# File 'lib/billable.rb', line 118 def download_invoice(id, data, storage_path = nil) # Coming Soon end |
#find_invoice(id) ⇒ Object
105 106 107 108 109 110 |
# File 'lib/billable.rb', line 105 def find_invoice(id) stripe_invoice = Stripe::Invoice.retrieve(id) Invoice.new(self, stripe_invoice) rescue false end |
#find_invoice_or_fail(id) ⇒ Object
112 113 114 115 116 |
# File 'lib/billable.rb', line 112 def find_invoice_or_fail(id) invoice = find_invoice(id) raise 'Invoice not found' if invoice.nil? invoice end |
#get_subscription(subscription = 'default') ⇒ Object
82 83 84 85 |
# File 'lib/billable.rb', line 82 def get_subscription(subscription = 'default') subscription_in_db = subscriptions.order(created_at: :desc).first subscription_in_db if subscription_in_db.name === subscription end |
#has_stripe_id ⇒ Object
180 181 182 |
# File 'lib/billable.rb', line 180 def has_stripe_id !!stripe_id end |
#has_subscription_on_trial?(subscription) ⇒ Boolean
62 63 64 |
# File 'lib/billable.rb', line 62 def has_subscription_on_trial?(subscription) subscription && subscription.on_trial end |
#invoice ⇒ Object
91 92 93 94 95 |
# File 'lib/billable.rb', line 91 def invoice Stripe::Invoice.create(customer: stripe_id).pay rescue false end |
#invoice_for(description, amount, options = {}) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/billable.rb', line 32 def invoice_for(description, amount, = {}) if !stripe_id raise 'User is not a customer. See the create_as_stripe_customer method.' end .merge!({ customer: stripe_id, amount: amount, currency: preferred_currency, description: description }) Stripe::InvoiceItem.create() end |
#invoices(include_pending = false, parameters = {}) ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/billable.rb', line 122 def invoices(include_pending = false, parameters = {}) invoices = [] parameters.merge!({ limit: 24 }) stripe_invoices = as_stripe_customer.invoices(parameters) unless stripe_invoices.nil? stripe_invoices.data.each do |invoice| if invoice.paid || include_pending invoices << Invoice.new(self, invoice) end end end invoices end |
#invoices_including_pending(parameters = []) ⇒ Object
138 139 140 |
# File 'lib/billable.rb', line 138 def invoices_including_pending(parameters = []) invoices(true, parameters) end |
#new_subscription(subscription, plan, *args) ⇒ Object
47 48 49 |
# File 'lib/billable.rb', line 47 def new_subscription(subscription, plan, *args) SubscriptionBuilder.new(self, subscription, plan, *args) end |
#on_generic_trial? ⇒ Boolean
66 67 68 |
# File 'lib/billable.rb', line 66 def on_generic_trial? trial_ends_at && DateTime.now < trial_ends_at.to_datetime end |
#on_plan(plan) ⇒ Object
175 176 177 178 |
# File 'lib/billable.rb', line 175 def on_plan(plan) subscription = subscriptions.first subscription.stripe_plan === plan && subscription.valid end |
#on_trial?(subscription = 'default', plan = nil) ⇒ Boolean
51 52 53 54 55 56 57 58 59 60 |
# File 'lib/billable.rb', line 51 def on_trial?(subscription = 'default', plan = nil) return true if on_generic_trial? subscription = get_subscription(subscription) if plan.nil? has_subscription_on_trial?(subscription) else has_subscription_on_trial?(subscription) && stripe_plan === plan end end |
#preferred_currency ⇒ Object
197 198 199 |
# File 'lib/billable.rb', line 197 def preferred_currency BankTeller::uses_currency end |
#refund(charge, options = {}) ⇒ Object
27 28 29 30 |
# File 'lib/billable.rb', line 27 def refund(charge, = {}) [:charge] = charge Stripe::Refund.create() end |
#subscribed?(subscription = 'default', plan = nil) ⇒ Boolean
70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/billable.rb', line 70 def subscribed?(subscription = 'default', plan = nil) subscription = get_subscription(subscription) if subscription.nil? false elsif plan.nil? subscription.valid else subscription.valid && stripe_plan === plan end end |
#subscribed_to_plan?(subscription = 'default', plan) ⇒ Boolean
169 170 171 172 173 |
# File 'lib/billable.rb', line 169 def subscribed_to_plan?(subscription = 'default', plan) subscription = get_subscription(subscription) return false unless subscription || subscription.valid subscription.stripe_plan === plan end |
#subscription(name) ⇒ Object
87 88 89 |
# File 'lib/billable.rb', line 87 def subscription(name) get_subscription(name) end |
#tax_percentage ⇒ Object
201 202 203 |
# File 'lib/billable.rb', line 201 def tax_percentage 0 end |
#upcoming_invoice ⇒ Object
97 98 99 100 101 102 103 |
# File 'lib/billable.rb', line 97 def upcoming_invoice args = { customer: stripe_id } stripe_invoice = Stripe::Invoice.upcoming(args) Invoice.new(self, stripe_invoice) rescue false end |
#update_card(token) ⇒ Object
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/billable.rb', line 142 def update_card(token) customer = as_stripe_customer token = Stripe::Token.retrieve(token) return if token.card.id === customer.default_source card = customer.sources.create(source: token) customer.default_source = card.id customer.save if customer.default_source source = customer.sources.retrieve(customer.default_source) end if source self.card_brand = source.brand self.card_last_four = source.last4 end self.save end |