Class: Hws::PaymentOperationsDemo::VirtualAccount

Inherits:
Object
  • Object
show all
Defined in:
lib/hws/payment_operations_demo/virtual_account.rb

Overview

:nodoc:

Constant Summary collapse

HYPTO_VA_CONNECTOR_ID =
'Hws::Connectors::Hypto::VirtualAccount'
HYPTO_PAYOUT_CONNECTOR_ID =
'Hws::Connectors::Hypto::Payout'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(instrument, store) ⇒ VirtualAccount

Returns a new instance of VirtualAccount.



60
61
62
63
# File 'lib/hws/payment_operations_demo/virtual_account.rb', line 60

def initialize(instrument, store)
  @instrument = instrument
  @store = store
end

Instance Attribute Details

#instrumentObject

Returns the value of attribute instrument.



5
6
7
# File 'lib/hws/payment_operations_demo/virtual_account.rb', line 5

def instrument
  @instrument
end

#storeObject

Returns the value of attribute store.



5
6
7
# File 'lib/hws/payment_operations_demo/virtual_account.rb', line 5

def store
  @store
end

Class Method Details

.create(name:, description: nil) ⇒ Object

Hws::PaymentOperationsDemo::VirtualAccount.create(name: ‘name’)



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/hws/payment_operations_demo/virtual_account.rb', line 23

def self.create(name:, description: nil)
  va_instr_config = Hws::Instruments::Models::InstrumentConfig.find_by(connector_id: HYPTO_VA_CONNECTOR_ID)
  if va_instr_config.nil?
    Rails.logger.error "cannot find instrument_config for connector_id #{HYPTO_VA_CONNECTOR_ID}"
    raise Hws::PaymentOperationsDemo::Exceptions::EntityNotFoundError, "InstrumentConfig [#{HYPTO_VA_CONNECTOR_ID}] not found"
  end
  instrument, store = ActiveRecord::Base.transaction do
    instrument = va_instr_config.create_instrument
    Rails.logger.info "Instrument [#{instrument.id}] created"

    store = TransactionalValueStore.create(name: name, description: description)
    Rails.logger.info "Store [#{store.store_id}] created"

    Hws::PaymentOperationsDemo::Models::InstrumentResourceStore.create(
      store_id: store.store_id, instrument_id: instrument.id
    )
    Rails.logger.info 'InstrumentResourceStore entry created'

    [instrument, store]
  end

  VirtualAccount.new(instrument, store)
end

.fetch_transaction_status(reference_number) ⇒ Object



139
140
141
142
143
144
145
146
# File 'lib/hws/payment_operations_demo/virtual_account.rb', line 139

def self.fetch_transaction_status(reference_number)
  instrument = TransactionalValueStore.get_instrument_for_entry(reference_number)
  return if instrument.nil?

  resp = instrument.execute(action: :status, options: { reference_number: reference_number })
  Rails.logger.info "Fetch_transaction_status: response - #{resp}"
  TransactionalValueStore.update_txn_status(reference_number, resp.status, { bank_ref_id: resp.bank_ref_num })
end

.funds_received_webhook(va_num:, amount:, payment_type:, txn_time: Time.now, status:, bank_ref_id:, beneficiary:, remitter:) ⇒ Object



124
125
126
127
128
129
130
131
132
133
# File 'lib/hws/payment_operations_demo/virtual_account.rb', line 124

def self.funds_received_webhook(va_num:, amount:, payment_type:, txn_time: Time.now, status:, bank_ref_id:, beneficiary:, remitter:)
   = self.of(va_num)
  .store.deposit(
    amount: amount,
    tags: {
      mutable_tags: { status: status, bank_ref_id: bank_ref_id },
      immutable_tags: { pymt_type: payment_type, txn_time: txn_time, beneficiary: beneficiary, remitter: remitter }
    }
  )
end

.of(va_num) ⇒ Object

Hws::PaymentOperationsDemo::VirtualAccount.of(‘HYPTOUAT70809083139849’)



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/hws/payment_operations_demo/virtual_account.rb', line 48

def self.of(va_num)
  instrument = Hws::Instruments::Models::Instrument.find_by(external_identifier: va_num)
  raise Hws::PaymentOperationsDemo::Exceptions::EntityNotFoundError, 'Instrument not found' if instrument.nil?

  store = TransactionalValueStore.load(
    Hws::PaymentOperationsDemo::Models::InstrumentResourceStore
      .find_by(instrument_id: instrument.id).store_id
  )

  VirtualAccount.new(instrument, store)
end

.record_txn_status_change(reference_number, status, bank_ref_num) ⇒ Object



135
136
137
# File 'lib/hws/payment_operations_demo/virtual_account.rb', line 135

def self.record_txn_status_change(reference_number, status, bank_ref_num)
  TransactionalValueStore.update_txn_status(reference_number, status, { bank_ref_id: bank_ref_num })
end

Instance Method Details

#activateObject



86
87
88
# File 'lib/hws/payment_operations_demo/virtual_account.rb', line 86

def activate
  @instrument.execute(action: __method__, options: { va_num: @instrument.external_identifier })
end

#as_jsonObject



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/hws/payment_operations_demo/virtual_account.rb', line 10

def as_json
  {
    name: @store.store[:name],
    description: @store.store[:description],
    va_info: {
      id: self.instrument.value['id'],
      account_number: self.instrument.value['va_num'],
      account_ifsc: self.instrument.value['account_ifsc']
    }
  }
end

#balanceObject

fetch_balance



95
96
97
# File 'lib/hws/payment_operations_demo/virtual_account.rb', line 95

def balance
  @store.balance
end

#deactivateObject



90
91
92
# File 'lib/hws/payment_operations_demo/virtual_account.rb', line 90

def deactivate
  @instrument.execute(action: __method__, options: { va_num: @instrument.external_identifier })
end

#payout_instrumentObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/hws/payment_operations_demo/virtual_account.rb', line 65

def payout_instrument
  return @payout_instrument if @payout_instrument.present?

  Rails.logger.warn 'Payout instrument not configured. Trying to auto configure...'
  # @payout_instrument ||= instrument
  payout_i_c = Hws::Instruments::Models::InstrumentConfig.where(connector_id: HYPTO_PAYOUT_CONNECTOR_ID, connector_credentials: @instrument.instrument_config.connector_credentials).first
  if payout_i_c.nil?
    Rails.logger.error 'Cannot find a matching instrument config. Unable to auto configure'
    raise 'Cannot find a matching instrument config. Unable to auto configure'
  end
  payout_instruments = payout_i_c.instruments
  @payout_instrument = if payout_instruments.empty?
                         Rails.logger.warn 'No payout instrument found. Creating one'
                         payout_i_c.create_instrument
                       else
                         payout_instruments.first
                       end

  @payout_instrument
end

#transfer_funds(amount:, payment_type:, beneficiary:) ⇒ Object

send_funds



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/hws/payment_operations_demo/virtual_account.rb', line 100

def transfer_funds(amount:, payment_type:, beneficiary:)
  instrument = self.payout_instrument

  entry_id = self.store.withdraw(
    amount: amount,
    tags: {
      mutable_tags: { status: 'PENDING' },
      immutable_tags: { pymt_type: payment_type, beneficiary: beneficiary, instrument_id: instrument.id }
    }
  )
  begin
    payload = { amount: amount, reference_number: entry_id, payment_type: payment_type, beneficiary: beneficiary }.with_indifferent_access
    if payment_type == 'UPI' && beneficiary.key?('upi_id')
      payout_instrument.execute(action: :send_to_upi_id, options: payload)
    else # NEFT, IMPS, RTGS or UPI to bank account number
      payout_instrument.execute(action: :send_to_bank_account, options: payload)
    end
  rescue StandardError => e
    Rails.logger.error e
    self.store.update_txn_status(entry_id, 'FAILED') if entry_id.present?
    raise e
  end
end