Module: SpreeStripe::CreditCardDecorator
- Defined in:
- app/models/spree_stripe/credit_card_decorator.rb
Overview
Backports Stripe's credit-card fingerprint dedup onto Spree::CreditCard for spree < 5.6.0, which doesn't ship it. spree >= 5.6.0 provides the column, scope, and validation in core, so this is only prepended (below) when core lacks it. (Wallet metadata accessors already live in core since 5.4, so they don't need backporting here.)
Class Method Summary collapse
Class Method Details
.prepended(base) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'app/models/spree_stripe/credit_card_decorator.rb', line 8 def self.prepended(base) # Matches saved cards by the gateway's stable card fingerprint plus expiry, # used to reuse an existing card instead of saving a duplicate when a gateway # issues a fresh payment method id for the same physical card. # # @param fingerprint [String] gateway card fingerprint # @param month [Integer, String] expiry month # @param year [Integer, String] expiry year base.scope :by_fingerprint, ->(fingerprint, month, year) { cards_table = Spree::CreditCard.table_name where(fingerprint: fingerprint). where("CAST(#{cards_table}.month AS DECIMAL) = ?", month). where("CAST(#{cards_table}.year AS DECIMAL) = ?", year) } # Prevents saving the same physical card (same gateway fingerprint + expiry) # twice for a user and payment method. Skipped when the gateway does not # provide a fingerprint. base.validate :fingerprint_not_duplicated, if: -> { fingerprint.present? } end |