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)
  assert_customer_exists

  stripe_payment_method = resolve_stripe_payment_method(payment_method)

  if stripe_payment_method.customer != stripe_id
    stripe_payment_method = stripe_payment_method.attach(
      { customer: stripe_id }, 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, stripe_options)
end

#default_payment_methodObject

Get the default payment method for the entity.



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/reji/concerns/manages_payment_methods.rb', line 71

def default_payment_method
  return unless stripe_id?

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

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

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

#default_payment_method?Boolean

Determines if the customer currently has a default payment method.

Returns:

  • (Boolean)


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

def default_payment_method?
  card_brand.present?
end

#delete_payment_methodsObject

Deletes the entity’s payment methods.



134
135
136
137
138
# File 'lib/reji/concerns/manages_payment_methods.rb', line 134

def delete_payment_methods
  payment_methods.each(&:delete)

  update_default_payment_method_from_stripe
end

#find_payment_method(payment_method) ⇒ Object

Find a PaymentMethod by ID.



141
142
143
144
145
146
147
148
149
150
151
# File 'lib/reji/concerns/manages_payment_methods.rb', line 141

def find_payment_method(payment_method)
  stripe_payment_method = nil

  begin
    stripe_payment_method = resolve_stripe_payment_method(payment_method)
  rescue StandardError => _e
    #
  end

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

#payment_method?Boolean

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

Returns:

  • (Boolean)


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

def payment_method?
  !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 stripe_id?

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

  # "type" is temporarily required by Stripe...
  payment_methods = Stripe::PaymentMethod.list(
    { customer: stripe_id, type: 'card' }.merge(parameters),
    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
# File 'lib/reji/concerns/manages_payment_methods.rb', line 53

def remove_payment_method(payment_method)
  assert_customer_exists

  stripe_payment_method = resolve_stripe_payment_method(payment_method)

  return if stripe_payment_method.customer != stripe_id

  customer = as_stripe_customer

  default_payment_method = customer.invoice_settings.default_payment_method

  stripe_payment_method.detach({}, stripe_options)

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

#update_default_payment_method(payment_method) ⇒ Object

Update customer’s default payment method.



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/reji/concerns/manages_payment_methods.rb', line 89

def update_default_payment_method(payment_method)
  assert_customer_exists

  customer = as_stripe_customer

  stripe_payment_method = 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 = 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.
  fill_payment_method_details(payment_method)
  save

  payment_method
end

#update_default_payment_method_from_stripeObject

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



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/reji/concerns/manages_payment_methods.rb', line 117

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
      fill_payment_method_details(default_payment_method.as_stripe_payment_method).save
    else
      fill_source_details(default_payment_method).save
    end
  else
    update({ card_brand: nil, card_last_four: nil })
  end

  self
end