Module: Reji::ManagesPaymentMethods

Extended by:
ActiveSupport::Concern
Included in:
Billable
Defined in:
lib/reji/concerns/manages_payment_methods.rb

Instance Method Summary collapse

Instance Method Details

#add_payment_method(payment_method) ⇒ Object

Add a payment method to the customer.



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/reji/concerns/manages_payment_methods.rb', line 38

def add_payment_method(payment_method)
  self.assert_customer_exists

  stripe_payment_method = self.resolve_stripe_payment_method(payment_method)

  if stripe_payment_method.customer != self.stripe_id
    stripe_payment_method = stripe_payment_method.attach(
      {:customer => self.stripe_id}, self.stripe_options
    )
  end

  PaymentMethod.new(self, stripe_payment_method)
end

#create_setup_intent(options = {}) ⇒ Object

Create a new SetupIntent instance.



8
9
10
# File 'lib/reji/concerns/manages_payment_methods.rb', line 8

def create_setup_intent(options = {})
  Stripe::SetupIntent.create(options, self.stripe_options)
end

#default_payment_methodObject

Get the default payment method for the entity.



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/reji/concerns/manages_payment_methods.rb', line 76

def default_payment_method
  return unless self.has_stripe_id

  customer = Stripe::Customer.retrieve({
    :id => self.stripe_id,
    :expand => [
      'invoice_settings.default_payment_method',
      'default_source',
    ]
  }, self.stripe_options)

  if customer.invoice_settings.default_payment_method
    return PaymentMethod.new(
      self,
      customer.invoice_settings.default_payment_method
    )
  end

  # If we can't find a payment method, try to return a legacy source...
  customer.default_source
end

#delete_payment_methodsObject

Deletes the entity’s payment methods.



149
150
151
152
153
# File 'lib/reji/concerns/manages_payment_methods.rb', line 149

def delete_payment_methods
  self.payment_methods.each { |payment_method| payment_method.delete }

  self.update_default_payment_method_from_stripe
end

#find_payment_method(payment_method) ⇒ Object

Find a PaymentMethod by ID.



156
157
158
159
160
161
162
163
164
165
166
# File 'lib/reji/concerns/manages_payment_methods.rb', line 156

def find_payment_method(payment_method)
  stripe_payment_method = nil

  begin
    stripe_payment_method = self.resolve_stripe_payment_method(payment_method)
  rescue => e
    #
  end

  stripe_payment_method ? PaymentMethod.new(self, stripe_payment_method) : nil
end

#has_default_payment_methodObject

Determines if the customer currently has a default payment method.



13
14
15
# File 'lib/reji/concerns/manages_payment_methods.rb', line 13

def has_default_payment_method
  ! self.card_brand.blank?
end

#has_payment_methodObject

Determines if the customer currently has at least one payment method.



18
19
20
# File 'lib/reji/concerns/manages_payment_methods.rb', line 18

def has_payment_method
  ! self.payment_methods.empty?
end

#payment_methods(parameters = {}) ⇒ Object

Get a collection of the entity’s payment methods.



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/reji/concerns/manages_payment_methods.rb', line 23

def payment_methods(parameters = {})
  return [] unless self.has_stripe_id

  parameters = {:limit => 24}.merge(parameters)

  # "type" is temporarily required by Stripe...
  payment_methods = Stripe::PaymentMethod.list(
    {customer: self.stripe_id, type: 'card'}.merge(parameters),
    self.stripe_options
  )

  payment_methods.data.map { |payment_method| PaymentMethod.new(self, payment_method) }
end

#remove_payment_method(payment_method) ⇒ Object

Remove a payment method from the customer.



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/reji/concerns/manages_payment_methods.rb', line 53

def remove_payment_method(payment_method)
  self.assert_customer_exists

  stripe_payment_method = self.resolve_stripe_payment_method(payment_method)

  return if stripe_payment_method.customer != self.stripe_id

  customer = self.as_stripe_customer

  default_payment_method = customer.invoice_settings.default_payment_method

  stripe_payment_method.detach({}, self.stripe_options)

  # If the payment method was the default payment method, we'll remove it manually...
  if stripe_payment_method.id == default_payment_method
    self.update({
      :card_brand => nil,
      :card_last_four => nil,
    })
  end
end

#update_default_payment_method(payment_method) ⇒ Object

Update customer’s default payment method.



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/reji/concerns/manages_payment_methods.rb', line 99

def update_default_payment_method(payment_method)
  self.assert_customer_exists

  customer = self.as_stripe_customer

  stripe_payment_method = self.resolve_stripe_payment_method(payment_method)

  # If the customer already has the payment method as their default, we can bail out
  # of the call now. We don't need to keep adding the same payment method to this
  # model's account every single time we go through this specific process call.
  return if stripe_payment_method.id == customer.invoice_settings.default_payment_method

  payment_method = self.add_payment_method(stripe_payment_method)

  customer.invoice_settings = {:default_payment_method => payment_method.id}

  customer.save

  # Next we will get the default payment method for this user so we can update the
  # payment method details on the record in the database. This will allow us to
  # show that information on the front-end when updating the payment methods.
  self.fill_payment_method_details(payment_method)
  self.save

  payment_method
end

#update_default_payment_method_from_stripeObject

Synchronises the customer’s default payment method from Stripe back into the database.



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/reji/concerns/manages_payment_methods.rb', line 127

def update_default_payment_method_from_stripe
  default_payment_method = self.default_payment_method

  if default_payment_method
    if default_payment_method.instance_of? PaymentMethod
      self.fill_payment_method_details(
        default_payment_method.as_stripe_payment_method
      ).save
    else
      self.fill_source_details(default_payment_method).save
    end
  else
    self.update({
      :card_brand => nil,
      :card_last_four => nil,
    })
  end

  self
end