Class: Pay::Charge

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.find_by_processor_and_id(processor, processor_id) ⇒ Object



41
42
43
# File 'app/models/pay/charge.rb', line 41

def self.find_by_processor_and_id(processor, processor_id)
  joins(:customer).find_by(processor_id: processor_id, pay_customers: {processor: processor})
end

.pay_processor_for(name) ⇒ Object



45
46
47
# File 'app/models/pay/charge.rb', line 45

def self.pay_processor_for(name)
  "Pay::#{name.to_s.classify}::Charge".constantize
end

Instance Method Details

#charged_toObject



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'app/models/pay/charge.rb', line 74

def charged_to
  case payment_method_type
  when "card"
    "#{brand.titleize} (**** **** **** #{last4})"
  when "paypal"
    "#{brand} (#{email})"

  # Braintree
  when "venmo"
    "#{brand.titleize} #{username}"
  when "us_bank_account"
    "#{bank} #{last4}"

  # Stripe
  when "acss_debit"
    "#{bank} #{last4}"
  when "eps", "fpx", "ideal", "p24"
    bank

  when "au_becs_debit"
    "BECS Debit #{last4}"

  when "bacs_debit"
    "Bacs Debit #{last4}"

  when "sepa_debit"
    "SEPA Debit #{last4}"

  else
    payment_method_type&.titleize
  end
end

#full_refund?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'app/models/pay/charge.rb', line 66

def full_refund?
  refunded? && amount == amount_refunded
end

#partial_refund?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'app/models/pay/charge.rb', line 70

def partial_refund?
  refunded? && !full_refund?
end

#payment_processorObject



49
50
51
# File 'app/models/pay/charge.rb', line 49

def payment_processor
  @payment_processor ||= self.class.pay_processor_for(customer.processor).new(self)
end

#processor_chargeObject



53
54
55
# File 'app/models/pay/charge.rb', line 53

def processor_charge
  payment_processor.charge
end

#refund!(refund_amount = nil) ⇒ Object



57
58
59
60
# File 'app/models/pay/charge.rb', line 57

def refund!(refund_amount = nil)
  refund_amount ||= amount
  payment_processor.refund!(refund_amount)
end

#refunded?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'app/models/pay/charge.rb', line 62

def refunded?
  amount_refunded.to_i > 0
end