Class: KPM::Account

Inherits:
Object
  • Object
show all
Defined in:
lib/kpm/account.rb

Constant Summary collapse

KILLBILL_HOST =

Killbill server

ENV['KILLBILL_HOST'] || '127.0.0.1'
KILLBILL_URL =
"http://#{KILLBILL_HOST}:8080"
KILLBILL_API_VERSION =
'1.0'
KILLBILL_USER =

USER/PWD

ENV['KILLBILL_USER'] || 'admin'
KILLBILL_PASSWORD =
ENV['KILLBILL_PASSWORD'] || 'password'
KILLBILL_API_KEY =

TENANT KEY

ENV['KILLBILL_API_KEY'] || 'bob'
KILLBILL_API_SECRET =
ENV['KILLBILL_API_SECRET'] || 'lazar'
TMP_DIR_PEFIX =

Temporary directory

'killbill'
TMP_DIR =
Dir.mktmpdir(TMP_DIR_PEFIX)
WHO =

Created By

'kpm_export_import'
SAFE_PAYMENT_METHOD =

safe payment method

'__EXTERNAL_PAYMENT__'
PLUGIN_NAME_COLUMN =
'plugin_name'
REMOVE_DATA_FROM =

fields to remove from the export files

{ accounts: i[name address1 address2 city state_or_province phone email],
account_history: i[name address1 address2 city state_or_province phone email] }.freeze
DATE_COLUMNS_TO_FIX =
%w[created_date updated_date processing_available_date effective_date
boot_date start_timestamp last_access_time payment_date original_created_date
last_sys_update_date charged_through_date bundle_start_date start_date catalog_effective_date reference_time].freeze
ROUND_TRIP_EXPORT_IMPORT_MAP =

round trip constants duplicate record

{ accounts: { id: :accounts_id, external_key: :accounts_id }, all: { account_id: :accounts_id },
account_history: { id: :account_history_id, external_key: :accounts_id, payment_method_id: :payment_methods_id },
account_emails: { id: :account_emails_id }, account_email_history: { id: :account_email_history_id },
subscription_events: { id: :subscription_events_id }, subscriptions: { id: :subscriptions_id },
bundles: { id: :bundles_id }, blocking_states: { id: :blocking_states_id, blockable_id: nil },
invoice_items: { id: :invoice_items_id, child_account_id: nil, invoice_id: :invoices_id, bundle_id: :bundles_id, subscription_id: :subscriptions_id },
invoices: { id: :invoices_id },
invoice_payments: { id: :invoice_payments_id, invoice_id: :invoices_id, payment_id: :payments_id },
invoice_parent_children: { id: :invoice_parent_children_id, parent_invoice_id: nil, child_invoice_id: nil, child_account_id: nil },
payment_attempts: { id: :payment_attempts_id, payment_method_id: :payment_methods_id, transaction_id: :payment_transactions_id },
payment_attempt_history: { id: :payment_attempt_history_id, payment_method_id: :payment_methods_id, transaction_id: :payment_transactions_id },
payment_methods: { id: :payment_methods_id, external_key: :generate }, payment_method_history: { id: :payment_method_history_id },
payments: { id: :payments_id, payment_method_id: :payment_methods_id },
payment_history: { id: :payment_history_id, payment_method_id: :payment_methods_id },
payment_transactions: { id: :payment_transactions_id, payment_id: :payments_id },
payment_transaction_history: { id: :payment_transaction_history_id, payment_id: :payments_id },
_invoice_payment_control_plugin_auto_pay_off: { payment_method_id: :payment_methods_id, payment_id: :payments_id },
rolled_up_usage: { id: :rolled_up_usage_id, subscription_id: :subscriptions_id, tracking_id: nil },
custom_fields: { id: :custom_fields_id }, custom_field_history: { id: :custom_field_history_id },
tag_definitions: { id: :tag_definitions_id }, tag_definition_history: { id: :tag_definition_history_id },
tags: { id: :tags_id, object_id: nil },
tag_history: { id: :tag_history_id, object_id: nil },
audit_log: { id: :audit_log_id } }.freeze
DELIMITERS =

delimeters to sniff

[',', '|'].freeze
DEFAULT_DELIMITER =
'|'
B64_REGEX =
%r{^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)?$}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(config_file = nil, killbill_api_credentials = nil, killbill_credentials = nil, killbill_url = nil, database_name = nil, database_credentials = nil, database_host = nil, database_port = nil, data_delimiter = nil, logger = nil) ⇒ Account



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

def initialize(config_file = nil, killbill_api_credentials = nil, killbill_credentials = nil, killbill_url = nil,
               database_name = nil, database_credentials = nil, database_host = nil, database_port = nil, data_delimiter = nil, logger = nil)
  @killbill_api_key = KILLBILL_API_KEY
  @killbill_api_secret = KILLBILL_API_SECRET
  @killbill_url = KILLBILL_URL
  @killbill_user = KILLBILL_USER
  @killbill_password = KILLBILL_PASSWORD
  @delimiter = data_delimiter || DEFAULT_DELIMITER
  @logger = logger
  @tables_id = {}

  set_killbill_options(killbill_api_credentials, killbill_credentials, killbill_url)

  database_credentials ||= [nil, nil]
  @database = Database.new(database_name, database_host, database_port, database_credentials[0], database_credentials[1], logger)

  load_config_from_file(config_file)
end

Instance Method Details

#export_data(account_id = nil) ⇒ Object

Raises:

  • (Interrupt)


95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/kpm/account.rb', line 95

def export_data( = nil)
  raise Interrupt, 'Need to specify an account id' if  == :export.to_s

  export_data = fetch_export_data()
  export_file = export(export_data)

  raise Interrupt, 'Account id not found' unless File.exist?(export_file)

  @logger.info "\e[32mData exported under #{export_file}\e[0m"

  export_file
end

#import_data(source_file, tenant_record_id, skip_payment_methods, round_trip_export_import = false, generate_record_id = false) ⇒ Object

Raises:

  • (Interrupt)


108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/kpm/account.rb', line 108

def import_data(source_file, tenant_record_id, skip_payment_methods, round_trip_export_import = false, generate_record_id = false)
  source_file = File.expand_path(source_file)

  @generate_record_id = generate_record_id
  @tenant_record_id = tenant_record_id
  @round_trip_export_import = round_trip_export_import

  raise Interrupt, 'Need to specify a file' if source_file == :import.to_s

  raise Interrupt, "File #{source_file} does not exist" unless File.exist?(source_file)

  @delimiter = sniff_delimiter(source_file) || @delimiter

  sanitize_and_import(source_file, skip_payment_methods)
end