Class: Pay::Braintree::Charge

Inherits:
Charge show all
Defined in:
app/models/pay/braintree/charge.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Charge

#amount_refunded_with_currency, #amount_with_currency, #captured?, #charged_to, find_by_processor_and_id, #full_refund?, #line_items, #partial_refund?, #refunded?

Class Method Details

.sync(charge_id, object: nil, try: 0, retries: 1) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'app/models/pay/braintree/charge.rb', line 4

def self.sync(charge_id, object: nil, try: 0, retries: 1)
  object ||= Pay.braintree_gateway.transaction.find(charge_id)

  pay_customer = Pay::Customer.find_by(processor: :braintree, processor_id: object.customer_details.id)
  return unless pay_customer

  pay_customer.save_transaction(object)
rescue ActiveRecord::RecordInvalid, ActiveRecord::RecordNotUnique
  try += 1
  if try <= retries
    sleep 0.1
    retry
  else
    raise
  end
end

Instance Method Details

#api_recordObject



21
22
23
24
25
# File 'app/models/pay/braintree/charge.rb', line 21

def api_record
  Pay.braintree_gateway.transaction.find(processor_id)
rescue ::Braintree::Braintree::Error => e
  raise Pay::Braintree::Error, e
end

#refund!(amount_to_refund = nil) ⇒ Object



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

def refund!(amount_to_refund = nil)
  amount_to_refund ||= amount
  Pay.braintree_gateway.transaction.refund(processor_id, amount_to_refund / 100.0)
  update(amount_refunded: amount_to_refund)
rescue ::Braintree::BraintreeError => e
  raise Pay::Braintree::Error, e
end