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
-
.bank_name(bank_code) ⇒ String?
Get bank name from code.
-
.format_amount(amount) ⇒ String
Format amount to 2 decimal places.
-
.generate_request_id(prefix = 'REQ') ⇒ String
Generate a unique request ID.
-
.generate_timestamp(time = Time.now) ⇒ String
Generate ISO 8601 timestamp.
-
.mask_sensitive(data, visible_chars = 4) ⇒ String
Mask sensitive data for logging.
-
.mcc_description(mcc_code) ⇒ String?
Get MCC description.
-
.parse_date(date_string) ⇒ Date?
Parse date string.
-
.sanitize_string(str) ⇒ String
Sanitize string for API request.
-
.valid_account_number?(account_number) ⇒ Boolean
Validate account number format.
-
.valid_amount?(amount) ⇒ Boolean
Validate amount.
-
.valid_bank_code?(bank_code) ⇒ Boolean
Validate bank code.
-
.valid_date_format?(date_string) ⇒ Boolean
Validate date format (YYYY-MM-DD).
-
.valid_email?(email) ⇒ Boolean
Validate email format.
-
.valid_mcc_format?(mcc_code) ⇒ Boolean
Validate MCC code format.
-
.valid_phone_number?(phone_number) ⇒ Boolean
Validate phone number format (Bhutan).
Class Method Details
.bank_name(bank_code) ⇒ String?
Get bank name from code
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
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
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
18 19 20 |
# File 'lib/dk_payment_gateway/utils.rb', line 18 def self.(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
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
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
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
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
25 26 27 28 29 30 |
# File 'lib/dk_payment_gateway/utils.rb', line 25 def self.valid_account_number?(account_number) return false if account_number.nil? || account_number.empty? # Account numbers should be numeric and between 8-15 digits account_number.to_s.match?(/^\d{8,15}$/) end |
.valid_amount?(amount) ⇒ Boolean
Validate amount
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
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)
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
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
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)
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 |