Module: DkPaymentGateway::Utils

Defined in:
lib/dk_payment_gateway/utils.rb

Constant Summary collapse

BANK_CODES =

Bank codes mapping

{
  '1010' => 'Bank of Bhutan',
  '1040' => 'Bhutan National Bank',
  '1060' => 'Digital Kidu',
  '1070' => 'Druk PNB Bank',
  '1080' => 'T Bank'
}.freeze
MCC_CODES =

Common MCC codes

{
  '5411' => 'Grocery Stores, Supermarkets',
  '5812' => 'Eating Places, Restaurants',
  '5999' => 'Miscellaneous and Specialty Retail Stores',
  '5814' => 'Fast Food Restaurants',
  '5912' => 'Drug Stores and Pharmacies',
  '5311' => 'Department Stores',
  '5541' => 'Service Stations',
  '5732' => 'Electronics Stores',
  '5942' => 'Book Stores',
  '5945' => 'Hobby, Toy, and Game Shops'
}.freeze

Class Method Summary collapse

Class Method Details

.bank_name(bank_code) ⇒ String?

Get bank name from code

Parameters:

  • bank_code (String)

    Bank code

Returns:

  • (String, nil)

    Bank name or nil if not found



121
122
123
# File 'lib/dk_payment_gateway/utils.rb', line 121

def self.bank_name(bank_code)
  BANK_CODES[bank_code.to_s]
end

.format_amount(amount) ⇒ String

Format amount to 2 decimal places

Parameters:

  • amount (Numeric)

    Amount to format

Returns:

  • (String)

    Formatted amount



63
64
65
# File 'lib/dk_payment_gateway/utils.rb', line 63

def self.format_amount(amount)
  format('%.2f', amount.to_f)
end

.generate_request_id(prefix = 'REQ') ⇒ String

Generate a unique request ID

Parameters:

  • prefix (String) (defaults to: 'REQ')

    Optional prefix for the request ID

Returns:

  • (String)

    Unique request ID



11
12
13
# File 'lib/dk_payment_gateway/utils.rb', line 11

def self.generate_request_id(prefix = 'REQ')
  "#{prefix}_#{Time.now.to_i}_#{SecureRandom.hex(6)}"
end

.generate_timestamp(time = Time.now) ⇒ String

Generate ISO 8601 timestamp

Parameters:

  • time (Time) (defaults to: Time.now)

    Optional time object (defaults to current time)

Returns:

  • (String)

    ISO 8601 formatted timestamp



18
19
20
# File 'lib/dk_payment_gateway/utils.rb', line 18

def self.generate_timestamp(time = Time.now)
  time.utc.strftime('%Y-%m-%dT%H:%M:%SZ')
end

.mask_sensitive(data, visible_chars = 4) ⇒ String

Mask sensitive data for logging

Parameters:

  • data (String)

    Sensitive data to mask

  • visible_chars (Integer) (defaults to: 4)

    Number of characters to show at start and end

Returns:

  • (String)

    Masked string



100
101
102
103
104
105
106
107
# File 'lib/dk_payment_gateway/utils.rb', line 100

def self.mask_sensitive(data, visible_chars = 4)
  return '' if data.nil? || data.empty?

  data_str = data.to_s
  return data_str if data_str.length <= visible_chars * 2

  "#{data_str[0...visible_chars]}#{'*' * (data_str.length - visible_chars * 2)}#{data_str[-visible_chars..]}"
end

.mcc_description(mcc_code) ⇒ String?

Get MCC description

Parameters:

  • mcc_code (String)

    MCC code

Returns:

  • (String, nil)

    MCC description or nil if not found



149
150
151
# File 'lib/dk_payment_gateway/utils.rb', line 149

def self.mcc_description(mcc_code)
  MCC_CODES[mcc_code.to_s]
end

.parse_date(date_string) ⇒ Date?

Parse date string

Parameters:

  • date_string (String)

    Date string in YYYY-MM-DD format

Returns:

  • (Date, nil)

    Parsed date or nil if invalid



79
80
81
82
83
84
85
# File 'lib/dk_payment_gateway/utils.rb', line 79

def self.parse_date(date_string)
  return nil unless valid_date_format?(date_string)

  Date.parse(date_string)
rescue ArgumentError
  nil
end

.sanitize_string(str) ⇒ String

Sanitize string for API request

Parameters:

  • str (String)

    String to sanitize

Returns:

  • (String)

    Sanitized string



90
91
92
93
94
# File 'lib/dk_payment_gateway/utils.rb', line 90

def self.sanitize_string(str)
  return '' if str.nil?

  str.to_s.strip
end

.valid_account_number?(account_number) ⇒ Boolean

Validate account number format

Parameters:

  • account_number (String)

    Account number to validate

Returns:

  • (Boolean)

    True if valid format



25
26
27
28
29
30
# File 'lib/dk_payment_gateway/utils.rb', line 25

def self.()
  return false if .nil? || .empty?

  # Account numbers should be numeric and between 8-15 digits
  .to_s.match?(/^\d{8,15}$/)
end

.valid_amount?(amount) ⇒ Boolean

Validate amount

Parameters:

  • amount (Numeric)

    Amount to validate

Returns:

  • (Boolean)

    True if valid



54
55
56
57
58
# File 'lib/dk_payment_gateway/utils.rb', line 54

def self.valid_amount?(amount)
  return false if amount.nil?

  amount.is_a?(Numeric) && amount >= 0
end

.valid_bank_code?(bank_code) ⇒ Boolean

Validate bank code

Parameters:

  • bank_code (String)

    Bank code to validate

Returns:

  • (Boolean)

    True if valid



128
129
130
# File 'lib/dk_payment_gateway/utils.rb', line 128

def self.valid_bank_code?(bank_code)
  BANK_CODES.key?(bank_code.to_s)
end

.valid_date_format?(date_string) ⇒ Boolean

Validate date format (YYYY-MM-DD)

Parameters:

  • date_string (String)

    Date string to validate

Returns:

  • (Boolean)

    True if valid format



70
71
72
73
74
# File 'lib/dk_payment_gateway/utils.rb', line 70

def self.valid_date_format?(date_string)
  return false if date_string.nil? || date_string.empty?

  date_string.to_s.match?(/^\d{4}-\d{2}-\d{2}$/)
end

.valid_email?(email) ⇒ Boolean

Validate email format

Parameters:

  • email (String)

    Email to validate

Returns:

  • (Boolean)

    True if valid format



45
46
47
48
49
# File 'lib/dk_payment_gateway/utils.rb', line 45

def self.valid_email?(email)
  return false if email.nil? || email.empty?

  email.to_s.match?(/\A[\w+\-.]+@[a-z\d-]+(\.[a-z\d-]+)*\.[a-z]+\z/i)
end

.valid_mcc_format?(mcc_code) ⇒ Boolean

Validate MCC code format

Parameters:

  • mcc_code (String)

    MCC code to validate

Returns:

  • (Boolean)

    True if valid format (4 digits)



156
157
158
# File 'lib/dk_payment_gateway/utils.rb', line 156

def self.valid_mcc_format?(mcc_code)
  mcc_code.to_s.match?(/^\d{4}$/)
end

.valid_phone_number?(phone_number) ⇒ Boolean

Validate phone number format (Bhutan)

Parameters:

  • phone_number (String)

    Phone number to validate

Returns:

  • (Boolean)

    True if valid format



35
36
37
38
39
40
# File 'lib/dk_payment_gateway/utils.rb', line 35

def self.valid_phone_number?(phone_number)
  return false if phone_number.nil? || phone_number.empty?

  # Bhutan phone numbers are typically 8 digits
  phone_number.to_s.match?(/^\d{8}$/)
end