Class: CreditCardVault
Overview
== Schema Information
Table name: credit_card_vaults
Database name: primary
id :integer not null, primary key
address_city :string
address_country :string
address_line1 :string
address_line1_check :string
address_line2 :string
address_state :string
address_zip_check :string
card_type :string(255)
consent_channel :string
consent_given_at :datetime
cvc_check :string
description :string
exp_month :integer
exp_year :integer
expiration_date :date
hidden :boolean
issuer_number :string
name :string
number :string(255) not null
stripe_detached_at :datetime
zip_code :string(10)
created_at :datetime
updated_at :datetime
creator_id :integer
customer_id :integer not null
updater_id :integer
vault_id :string
Indexes
customer_id_expiration_date (customer_id,expiration_date)
index_credit_card_vaults_on_vault_id (vault_id)
Foreign Keys
fk_rails_... (customer_id => parties.id)
Constant Summary
collapse
- CONSENT_CHANNELS =
Format: "channel" or "channel:Employee Name" for CRM-originated consent
%w[www crm_phone crm_manual].freeze
Models::Auditable::ALWAYS_IGNORED
Constants included
from Schedulable
Schedulable::SIMPLE_FORM_OPTIONS
Instance Attribute Summary collapse
#creator, #updater
Class Method Summary
collapse
Instance Method Summary
collapse
#all_skipped_columns, #audit_reference_data, #should_not_save_version, #stamp_record
ransackable_associations, ransackable_attributes, ransackable_scopes, ransortable_attributes, #to_relation
config
#after_commit
#publish_event
Instance Attribute Details
#address_line1 ⇒ Object
54
|
# File 'app/models/credit_card_vault.rb', line 54
validates :customer_id, :vault_id, :number, :exp_year, :exp_month, :address_line1, :zip_code, :address_state, presence: true
|
#address_state ⇒ Object
54
|
# File 'app/models/credit_card_vault.rb', line 54
validates :customer_id, :vault_id, :number, :exp_year, :exp_month, :address_line1, :zip_code, :address_state, presence: true
|
#card_token ⇒ Object
Returns the value of attribute card_token.
62
63
64
|
# File 'app/models/credit_card_vault.rb', line 62
def card_token
@card_token
end
|
#customer_id ⇒ Object
54
|
# File 'app/models/credit_card_vault.rb', line 54
validates :customer_id, :vault_id, :number, :exp_year, :exp_month, :address_line1, :zip_code, :address_state, presence: true
|
#exp_month ⇒ Object
54
|
# File 'app/models/credit_card_vault.rb', line 54
validates :customer_id, :vault_id, :number, :exp_year, :exp_month, :address_line1, :zip_code, :address_state, presence: true
|
#exp_year ⇒ Object
54
|
# File 'app/models/credit_card_vault.rb', line 54
validates :customer_id, :vault_id, :number, :exp_year, :exp_month, :address_line1, :zip_code, :address_state, presence: true
|
#number ⇒ Object
54
|
# File 'app/models/credit_card_vault.rb', line 54
validates :customer_id, :vault_id, :number, :exp_year, :exp_month, :address_line1, :zip_code, :address_state, presence: true
|
#vault_id ⇒ Object
54
|
# File 'app/models/credit_card_vault.rb', line 54
validates :customer_id, :vault_id, :number, :exp_year, :exp_month, :address_line1, :zip_code, :address_state, presence: true
|
#zip_code ⇒ Object
54
|
# File 'app/models/credit_card_vault.rb', line 54
validates :customer_id, :vault_id, :number, :exp_year, :exp_month, :address_line1, :zip_code, :address_state, presence: true
|
Class Method Details
.cleanup_invalid_values ⇒ Object
94
95
96
97
98
99
100
|
# File 'app/models/credit_card_vault.rb', line 94
def self.cleanup_invalid_values
CreditCardVault.where.not(zip_code: nil).find_each do |ccv|
new_zip = Heatwave::Normalizers.chain(ccv.zip_code, :strip, :no_undefined, :blank, :zip_or_postal_code)
ccv.update_column(:zip_code, new_zip) unless new_zip == ccv.zip_code
end
end
|
.consented ⇒ ActiveRecord::Relation<CreditCardVault>
A relation of CreditCardVaults that are consented. Active Record Scope
68
|
# File 'app/models/credit_card_vault.rb', line 68
scope :consented, -> { where.not(consent_given_at: nil) }
|
.detached ⇒ ActiveRecord::Relation<CreditCardVault>
A relation of CreditCardVaults that are detached. Active Record Scope
67
|
# File 'app/models/credit_card_vault.rb', line 67
scope :detached, -> { where.not(stripe_detached_at: nil) }
|
.expired ⇒ ActiveRecord::Relation<CreditCardVault>
A relation of CreditCardVaults that are expired. Active Record Scope
65
|
# File 'app/models/credit_card_vault.rb', line 65
scope :expired, -> { where(credit_card_vaults: { expiration_date: ..Date.current }) }
|
.hidden_without_consent ⇒ ActiveRecord::Relation<CreditCardVault>
A relation of CreditCardVaults that are hidden without consent. Active Record Scope
69
|
# File 'app/models/credit_card_vault.rb', line 69
scope :hidden_without_consent, -> { where(hidden: true).where(consent_given_at: nil) }
|
.sync_stripe_status!(customer) ⇒ Object
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
# File 'app/models/credit_card_vault.rb', line 71
def self.sync_stripe_status!(customer)
return if customer&.stripe_customer_id.blank?
currency = customer.catalog&.currency || 'USD'
stripe_pms = Payment::Apis::Stripe.list_payment_methods(
customer.stripe_customer_id, currency: currency
)
attached_pm_ids = stripe_pms.data.to_set(&:id)
customer.credit_card_vaults.where.not(vault_id: nil).find_each do |vault|
pm_id = vault.vault_id
next if pm_id.include?('|')
if attached_pm_ids.include?(pm_id)
vault.update_columns(stripe_detached_at: nil) if vault.stripe_detached_at.present?
elsif vault.stripe_detached_at.nil?
vault.update_columns(stripe_detached_at: Time.current)
end
end
rescue ::Stripe::StripeError => e
Rails.logger.warn("[CreditCardVault] Stripe sync failed for customer #{customer.id}: #{e.message}")
end
|
.valid ⇒ ActiveRecord::Relation<CreditCardVault>
A relation of CreditCardVaults that are valid. Active Record Scope
64
|
# File 'app/models/credit_card_vault.rb', line 64
scope :valid, -> { where('credit_card_vaults.expiration_date > ?', Date.current) }
|
.visible ⇒ ActiveRecord::Relation<CreditCardVault>
A relation of CreditCardVaults that are visible. Active Record Scope
66
|
# File 'app/models/credit_card_vault.rb', line 66
scope :visible, -> { where('hidden is null or hidden = false').where(stripe_detached_at: nil) }
|
Instance Method Details
#address_to_string ⇒ Object
110
111
112
|
# File 'app/models/credit_card_vault.rb', line 110
def address_to_string
[address_line1, address_line2, address_city, address_state, zip_code, address_country].compact.join(', ')
end
|
#consent_channel_label ⇒ Object
118
119
120
121
122
123
124
125
126
127
128
129
|
# File 'app/models/credit_card_vault.rb', line 118
def consent_channel_label
return nil if consent_channel.blank?
channel, person = consent_channel.split(':', 2)
label = case channel
when 'www' then 'Website'
when 'crm_phone' then 'Phone'
when 'crm_manual' then 'Manual entry'
else channel.humanize
end
person.present? ? "#{label} by #{person}" : label
end
|
#consented? ⇒ Boolean
114
115
116
|
# File 'app/models/credit_card_vault.rb', line 114
def consented?
consent_given_at.present?
end
|
56
|
# File 'app/models/credit_card_vault.rb', line 56
belongs_to :customer, optional: true
|
#expired? ⇒ Boolean
106
107
108
|
# File 'app/models/credit_card_vault.rb', line 106
def expired?
expiration_date <= Date.current
end
|
#grant_consent!(channel) ⇒ Object
131
132
133
|
# File 'app/models/credit_card_vault.rb', line 131
def grant_consent!(channel)
update!(consent_given_at: Time.current, consent_channel: channel, hidden: false)
end
|
#payments ⇒ ActiveRecord::Relation<Payment>
57
|
# File 'app/models/credit_card_vault.rb', line 57
has_many :payments, foreign_key: 'vault_id', primary_key: 'vault_id'
|
#set_expiration ⇒ Object
144
145
146
|
# File 'app/models/credit_card_vault.rb', line 144
def set_expiration
self.expiration_date = Date.new(exp_year, exp_month, -1) if exp_year.present? && exp_month.present?
end
|
#store_card(email = nil) ⇒ Object
135
136
137
138
139
140
|
# File 'app/models/credit_card_vault.rb', line 135
def store_card(email = nil)
return unless vault_id.nil?
res = Payment::Gateways::CreditCard.new.store(self, email)
res.success
end
|
#summary ⇒ Object
102
103
104
|
# File 'app/models/credit_card_vault.rb', line 102
def summary
"Type: #{card_type} | #: #{number} | Name: #{name} | Exp: #{exp_month}/#{exp_year}"
end
|