Class: Pay::Braintree::Charge

Inherits:
Object
  • Object
show all
Defined in:
lib/pay/braintree/charge.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pay_charge) ⇒ Charge

Returns a new instance of Charge.



25
26
27
# File 'lib/pay/braintree/charge.rb', line 25

def initialize(pay_charge)
  @pay_charge = pay_charge
end

Instance Attribute Details

#pay_chargeObject (readonly)

Returns the value of attribute pay_charge.



4
5
6
# File 'lib/pay/braintree/charge.rb', line 4

def pay_charge
  @pay_charge
end

Class Method Details

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



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/pay/braintree/charge.rb', line 8

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

#chargeObject



29
30
31
32
33
# File 'lib/pay/braintree/charge.rb', line 29

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

#refund!(amount_to_refund) ⇒ Object



35
36
37
38
39
40
# File 'lib/pay/braintree/charge.rb', line 35

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