Class: Faker::Stripe

Inherits:
Base
  • Object
show all
Defined in:
lib/faker/default/stripe.rb

Constant Summary

Constants inherited from Base

Base::LLetters, Base::Letters, Base::NOT_GIVEN, Base::Numbers, Base::ULetters

Class Method Summary collapse

Methods inherited from Base

bothify, disable_enforce_available_locales, fetch, fetch_all, flexible, letterify, method_missing, numerify, parse, rand, rand_in_range, regexify, resolve, respond_to_missing?, sample, shuffle, translate, unique, with_locale

Class Method Details

.ccv(legacy_card_type = NOT_GIVEN, card_type: nil) ⇒ String

Produces a random ccv number.

Examples:

Faker::Stripe.ccv #=> "123"
Faker::Stripe.ccv(card_type: "amex") #=> "1234"

Parameters:

  • card_type (String) (defaults to: nil)

    Specific valid card type.

Returns:



133
134
135
136
137
138
139
# File 'lib/faker/default/stripe.rb', line 133

def ccv(legacy_card_type = NOT_GIVEN, card_type: nil)
  warn_for_deprecated_arguments do |keywords|
    keywords << :card_type if legacy_card_type != NOT_GIVEN
  end

  (card_type.to_s == 'amex' ? rand_in_range(1000, 9999) : rand_in_range(100, 999)).to_s
end

.invalid_card(legacy_card_error = NOT_GIVEN, card_error: nil) ⇒ String

Produces a random invalid card number.

Examples:

Faker::Stripe.invalid_card #=> "4000000000000002"
Faker::Stripe.invalid_card(card_error: "addressZipFail") #=> "4000000000000010"

Returns:



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/faker/default/stripe.rb', line 76

def invalid_card(legacy_card_error = NOT_GIVEN, card_error: nil)
  warn_for_deprecated_arguments do |keywords|
    keywords << :card_error if legacy_card_error != NOT_GIVEN
  end

  invalid_cards = translate('faker.stripe.invalid_cards').keys

  if card_error.nil?
    card_error = sample(invalid_cards).to_s
  else
    unless invalid_cards.include?(card_error.to_sym)
      raise ArgumentError,
            "Invalid credit cards argument can be left blank or include #{invalid_cards.join(', ')}"
    end
  end

  fetch('stripe.invalid_cards.' + card_error)
end

.monthString

Produces a random month in two digits format.

Examples:

Faker::Stripe.month #=> "10"

Returns:



104
105
106
# File 'lib/faker/default/stripe.rb', line 104

def month
  format('%02d', rand_in_range(1, 12))
end

.valid_card(legacy_card_type = NOT_GIVEN, card_type: nil) ⇒ String

Produces a random valid card number.

Examples:

Faker::Stripe.valid_card #=> "4242424242424242"
Faker::Stripe.valid_card(card_type: "visa_debit") #=> "4000056655665556"

Parameters:

  • card_type (String) (defaults to: nil)

    Specific valid card type.

Returns:



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/faker/default/stripe.rb', line 17

def valid_card(legacy_card_type = NOT_GIVEN, card_type: nil)
  warn_for_deprecated_arguments do |keywords|
    keywords << :card_type if legacy_card_type != NOT_GIVEN
  end

  valid_cards = translate('faker.stripe.valid_cards').keys

  if card_type.nil?
    card_type = sample(valid_cards).to_s
  else
    unless valid_cards.include?(card_type.to_sym)
      raise ArgumentError,
            "Valid credit cards argument can be left blank or include #{valid_cards.join(', ')}"
    end
  end

  fetch('stripe.valid_cards.' + card_type)
end

.valid_token(legacy_card_type = NOT_GIVEN, card_type: nil) ⇒ String

Produces a random valid Stripe token.

Examples:

Faker::Stripe.valid_token #=> "tok_visa"
Faker::Stripe.valid_token(card_type: "mc_debit") #=> "tok_mastercard_debit"

Parameters:

  • card_type (String) (defaults to: nil)

    Specific valid card type.

Returns:



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/faker/default/stripe.rb', line 47

def valid_token(legacy_card_type = NOT_GIVEN, card_type: nil)
  warn_for_deprecated_arguments do |keywords|
    keywords << :card_type if legacy_card_type != NOT_GIVEN
  end

  valid_tokens = translate('faker.stripe.valid_tokens').keys

  if card_type.nil?
    card_type = sample(valid_tokens).to_s
  else
    unless valid_tokens.include?(card_type.to_sym)
      raise ArgumentError,
            "Valid credit cards argument can be left blank or include #{valid_tokens.join(', ')}"
    end
  end

  fetch('stripe.valid_tokens.' + card_type)
end

.yearString

Produces a random year that is always in the future.

Examples:

Faker::Stripe.year #=> "2018" # This will always be a year in the future

Returns:



117
118
119
120
# File 'lib/faker/default/stripe.rb', line 117

def year
  start_year = ::Time.new.year + 1
  rand_in_range(start_year, start_year + 5).to_s
end