Class: Pay::FakeProcessor::Billable

Inherits:
Object
  • Object
show all
Defined in:
lib/pay/fake_processor/billable.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pay_customer) ⇒ Billable

Returns a new instance of Billable.


13
14
15
# File 'lib/pay/fake_processor/billable.rb', line 13

def initialize(pay_customer)
  @pay_customer = pay_customer
end

Instance Attribute Details

#pay_customerObject (readonly)

Returns the value of attribute pay_customer.


4
5
6
# File 'lib/pay/fake_processor/billable.rb', line 4

def pay_customer
  @pay_customer
end

Instance Method Details

#add_payment_method(payment_method_id, default: false) ⇒ Object


62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/pay/fake_processor/billable.rb', line 62

def add_payment_method(payment_method_id, default: false)
  # Make to generate a processor_id
  customer

  pay_payment_method = pay_customer.payment_methods.create!(
    processor_id: NanoId.generate,
    default: default,
    type: :card,
    data: {
      brand: "Fake",
      last4: 1234,
      exp_month: Date.today.month,
      exp_year: Date.today.year
    }
  )

  pay_customer.reload_default_payment_method if default
  pay_payment_method
end

#charge(amount, options = {}) ⇒ Object


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/pay/fake_processor/billable.rb', line 26

def charge(amount, options = {})
  # Make to generate a processor_id
  customer

  attributes = options.merge(
    processor_id: NanoId.generate,
    amount: amount,
    data: {
      payment_method_type: :card,
      brand: "Fake",
      last4: 1234,
      exp_month: Date.today.month,
      exp_year: Date.today.year
    }
  )
  pay_customer.charges.create!(attributes)
end

#customerObject


17
18
19
20
# File 'lib/pay/fake_processor/billable.rb', line 17

def customer
  pay_customer.update!(processor_id: NanoId.generate) unless processor_id?
  pay_customer
end

#processor_subscription(subscription_id, options = {}) ⇒ Object


82
83
84
# File 'lib/pay/fake_processor/billable.rb', line 82

def processor_subscription(subscription_id, options = {})
  pay_customer.subscriptions.find_by(processor_id: subscription_id)
end

#subscribe(name: Pay.default_product_name, plan: Pay.default_plan_name, **options) ⇒ Object


44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/pay/fake_processor/billable.rb', line 44

def subscribe(name: Pay.default_product_name, plan: Pay.default_plan_name, **options)
  # Make to generate a processor_id
  customer
  attributes = options.merge(
    processor_id: NanoId.generate,
    name: name,
    processor_plan: plan,
    status: :active,
    quantity: options.fetch(:quantity, 1)
  )

  if (trial_period_days = attributes.delete(:trial_period_days))
    attributes[:trial_ends_at] = trial_period_days.to_i.days.from_now
  end

  pay_customer.subscriptions.create!(attributes)
end

#trial_end_date(subscription) ⇒ Object


86
87
88
# File 'lib/pay/fake_processor/billable.rb', line 86

def trial_end_date(subscription)
  Date.today
end

#update_customer!Object


22
23
24
# File 'lib/pay/fake_processor/billable.rb', line 22

def update_customer!
  # pass
end