Module: OmiseHelper

Overview

Helper module for Omise payment gateway configuration

This module provides methods to configure Omise API keys based on the country where the payment is being processed. Each country (Thailand, Singapore, Malaysia) has its own Omise account with separate API credentials.

Examples:

Usage in a service

class PaymentService
  include OmiseHelper

  def process_payment(restaurant)
    configure_omise_keys(restaurant.country.alpha3)
    # Now Omise.api_key and Omise.vault_key are set for the country
    charge = ::Omise::Charge.create(...)
  end
end

Instance Method Summary collapse

Instance Method Details

#configure_omise_keys(country_code) ⇒ void

Note:

If an invalid country code is provided, it defaults to Thailand (THA)

This method returns an undefined value.

Configure Omise API keys based on country code

This method sets the global Omise configuration to use the appropriate API credentials for the specified country. It reads credentials from environment variables in the format:

  • OMISE_SKEY_COUNTRY_CODE (secret key)

  • OMISE_VAULT_KEY_COUNTRY_CODE (public/vault key)

Examples:

Configure for Malaysia

configure_omise_keys('MYS')
# Sets Omise.api_key = ENV['OMISE_SKEY_MYS']
# Sets Omise.vault_key = ENV['OMISE_VAULT_KEY_MYS']

Configure for Singapore

configure_omise_keys('SGP')
# Sets Omise.api_key = ENV['OMISE_SKEY_SGP']
# Sets Omise.vault_key = ENV['OMISE_VAULT_KEY_SGP']

Parameters:

  • country_code (String)

    The ISO 3166-1 alpha-3 country code Supported values: 'THA' (Thailand), 'SGP' (Singapore), 'MYS' (Malaysia)



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'app/helpers/omise_helper.rb', line 52

def configure_omise_keys(country_code)
  # validate country code
  # default to TH if country code is not in the list
  country_code = ApiV5::Constants::COUNTRY_CODE_TH if ApiV5::Constants::COUNTRY_CODES.exclude?(country_code)

  # Handle Malaysia Omise keys with dual fallback mechanism to Singapore
  if country_code == ApiV5::Constants::COUNTRY_CODE_MY
    flag_enabled = Flipper.enabled?(:use_omise_malaysia_keys)

    # Fallback 1: Feature flag disabled - intentional fallback to Singapore
    if flag_enabled
      # Fallback 2: Feature flag enabled but keys missing - configuration issue
      api_key = Figaro.env.send("OMISE_SKEY_#{country_code}!")
      vault_key = Figaro.env.send("OMISE_VAULT_KEY_#{country_code}!")

      if api_key.blank? || vault_key.blank?
        error_context = {
          requested_country: country_code,
          fallback_reason: 'keys_not_configured',
          missing_api_key: api_key.blank?,
          missing_vault_key: vault_key.blank?,
          flipper_flag_enabled: true,
          fallback_country: ApiV5::Constants::COUNTRY_CODE_SG,
          action_required: 'Configure OMISE_SKEY_MYS and OMISE_VAULT_KEY_MYS environment variables',
        }

        BUSINESS_LOGGER.warn('Malaysia Omise keys missing despite feature flag enabled, falling back to Singapore',
                             error_context)

        APMErrorHandler.report(
          RuntimeError.new('Malaysia Omise keys not configured'),
          error_context,
        )

        country_code = ApiV5::Constants::COUNTRY_CODE_SG
      end
    else
      BUSINESS_LOGGER.info('Malaysia Omise keys feature flag disabled, using Singapore keys',
                           requested_country: country_code,
                           fallback_reason: 'feature_flag_disabled',
                           flipper_flag: 'use_omise_malaysia_keys',
                           fallback_country: ApiV5::Constants::COUNTRY_CODE_SG)
      country_code = ApiV5::Constants::COUNTRY_CODE_SG
    end
  end

  api_key = Figaro.env.send("OMISE_SKEY_#{country_code}!")
  vault_key = Figaro.env.send("OMISE_VAULT_KEY_#{country_code}!")

  ::Omise.api_key = api_key
  ::Omise.vault_key = vault_key
end

#omise_public_keyString

Get the current Omise public key (vault key)

Returns:

  • (String)

    The configured Omise vault/public key



24
25
26
# File 'app/helpers/omise_helper.rb', line 24

def omise_public_key
  ::Omise.vault_key
end