Module: Freemium::ManualBilling

Defined in:
lib/freemium/manual_billing.rb

Overview

adds manual billing functionality to the Subscription class

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



4
5
6
# File 'lib/freemium/manual_billing.rb', line 4

def self.included(base)
  base.extend ClassMethods
end

Instance Method Details

#charge!Object

charges this subscription. assumes, of course, that this module is mixed in to the Subscription model



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/freemium/manual_billing.rb', line 15

def charge!
  # Save the transaction immediately

  @transaction = gateway.charge(billing_key, self.installment_amount)
  self.transactions << @transaction
  self.last_transaction_at = Time.now # TODO this could probably now be inferred from the list of transactions
  self.last_transaction_success = @transaction.success?

  self.save(:validate => false)

  begin
    if @transaction.success?
      receive_payment!(@transaction)
    elsif !@transaction.subscription.in_grace?
      expire_after_grace!(@transaction)
    end
  rescue
  end

  @transaction
end

#installment_amount(options = {}) ⇒ Object

Override if you need to charge something different than the rate (ex: yearly billing option)



9
10
11
# File 'lib/freemium/manual_billing.rb', line 9

def installment_amount(options = {})
  self.rate(options)
end

#store_credit_card_offsiteObject



37
38
39
40
41
42
43
44
45
# File 'lib/freemium/manual_billing.rb', line 37

def store_credit_card_offsite
  if credit_card && credit_card.changed? && credit_card.valid?
    response = billing_key ? gateway.update(billing_key, credit_card, credit_card.address) : gateway.store(credit_card, credit_card.address)
    raise Freemium::CreditCardStorageError.new(response.message) unless response.success?
    self.billing_key = response.billing_key
    self.expire_on = nil if last_transaction_success
    self.credit_card.reload # to prevent needless subsequent store() calls
  end
end