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



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

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

Instance Method Details

#amount_refunded_with_currencyObject



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

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

#amount_with_currencyObject



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

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

#charged_toObject



69
70
71
72
73
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
106
# File 'app/models/pay/charge.rb', line 69

def charged_to
  case payment_method_type
  when "card"
    "#{brand.titleize} (**** **** **** #{last4})"
  when "paypal"
    # Sometimes brand and email are missing (Stripe)
    brand ||= "PayPal"
    if email.present?
      brand + " (#{email})"
    else
      brand
    end

  # 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)


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

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

#partial_refund?Boolean

Returns:

  • (Boolean)


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

def partial_refund?
  refunded? && !full_refund?
end

#refunded?Boolean

Returns:

  • (Boolean)


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

def refunded?
  amount_refunded.to_i > 0
end

#sync!(**options) ⇒ Object



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

def sync!(**options)
  self.class.sync(processor_id, **options)
  reload
end