Class: Spree::BankPayments::ReferenceGenerator

Inherits:
Object
  • Object
show all
Defined in:
app/services/spree/bank_payments/reference_generator.rb

Defined Under Namespace

Classes: ExhaustedError

Constant Summary collapse

ALPHABET =

Crockford base32: excludes I, L, O and U so handwritten and mistyped references stay unambiguous.

'0123456789ABCDEFGHJKMNPQRSTVWXYZ'.freeze
LENGTH =
6
MAX_ATTEMPTS =
10

Instance Method Summary collapse

Constructor Details

#initialize(payment_method:) ⇒ ReferenceGenerator

Returns a new instance of ReferenceGenerator.



12
13
14
# File 'app/services/spree/bank_payments/reference_generator.rb', line 12

def initialize(payment_method:)
  @payment_method = payment_method
end

Instance Method Details

#generateObject

Raises:



16
17
18
19
20
21
22
23
24
# File 'app/services/spree/bank_payments/reference_generator.rb', line 16

def generate
  MAX_ATTEMPTS.times do
    candidate = "#{@payment_method.preferred_reference_prefix}#{random_code}"
    return candidate unless taken?(candidate)
  end

  raise ExhaustedError,
        "could not generate a unique reference after #{MAX_ATTEMPTS} attempts"
end