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


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

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


60
61
62
# File 'app/models/pay/charge.rb', line 60

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

Instance Method Details

#amount_refunded_with_currencyObject


97
98
99
# File 'app/models/pay/charge.rb', line 97

def amount_refunded_with_currency
  Pay::Currency.format(amount_refunded, currency: currency)
end

#amount_with_currencyObject


93
94
95
# File 'app/models/pay/charge.rb', line 93

def amount_with_currency
  Pay::Currency.format(amount, currency: currency)
end

#captured?Boolean

Returns:

  • (Boolean)

72
73
74
# File 'app/models/pay/charge.rb', line 72

def captured?
  amount_captured > 0
end

#charged_toObject


101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'app/models/pay/charge.rb', line 101

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)

85
86
87
# File 'app/models/pay/charge.rb', line 85

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

#line_itemsObject


134
135
136
# File 'app/models/pay/charge.rb', line 134

def line_items
  Array.wrap(super)
end

#partial_refund?Boolean

Returns:

  • (Boolean)

89
90
91
# File 'app/models/pay/charge.rb', line 89

def partial_refund?
  refunded? && !full_refund?
end

#payment_processorObject


64
65
66
# File 'app/models/pay/charge.rb', line 64

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

#processor_chargeObject


68
69
70
# File 'app/models/pay/charge.rb', line 68

def processor_charge
  payment_processor.charge
end

#refund!(refund_amount = nil) ⇒ Object


76
77
78
79
# File 'app/models/pay/charge.rb', line 76

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

#refunded?Boolean

Returns:

  • (Boolean)

81
82
83
# File 'app/models/pay/charge.rb', line 81

def refunded?
  amount_refunded.to_i > 0
end