Class: Pay::Braintree::PaymentMethod

Inherits:
PaymentMethod
  • Object
show all
Extended by:
Sync
Defined in:
app/models/pay/braintree/payment_method.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Sync

find_pay_customer, pay_processor, sync_with_retries

Class Method Details

.sync(id, object: nil) ⇒ Object



6
7
8
9
10
11
12
13
# File 'app/models/pay/braintree/payment_method.rb', line 6

def self.sync(id, object: nil)
  sync_with_retries do
    payment_method = object || Pay.braintree_gateway.payment_method.find(id)
    return unless (pay_customer = find_pay_customer(payment_method.customer_id))

    pay_customer.save_payment_method(payment_method, default: payment_method.default?)
  end
end

Instance Method Details

#detachObject

Remove payment method



29
30
31
32
33
# File 'app/models/pay/braintree/payment_method.rb', line 29

def detach
  result = gateway.payment_method.delete(processor_id)
  raise Pay::Braintree::Error, result unless result.success?
  result.success?
end

#make_default!Object

Sets payment method as default



16
17
18
19
20
21
22
23
24
25
26
# File 'app/models/pay/braintree/payment_method.rb', line 16

def make_default!
  return if default?

  result = gateway.customer.update(customer.processor_id, default_payment_method_token: processor_id)
  raise Pay::Braintree::Error, result unless result.success?

  customer.payment_methods.update_all(default: false)
  update!(default: true)

  result.success?
end