Class: Dscf::Banking::VoucherService

Inherits:
BaseTransactionService show all
Defined in:
app/services/dscf/banking/voucher_service.rb

Instance Attribute Summary collapse

Attributes inherited from BaseTransactionService

#errors

Instance Method Summary collapse

Methods inherited from BaseTransactionService

#failure?, #success?

Constructor Details

#initializeVoucherService

Returns a new instance of VoucherService.



6
7
8
9
10
# File 'app/services/dscf/banking/voucher_service.rb', line 6

def initialize
  super
  @voucher = nil
  @redemption = nil
end

Instance Attribute Details

#redemptionObject (readonly)

Returns the value of attribute redemption.



4
5
6
# File 'app/services/dscf/banking/voucher_service.rb', line 4

def redemption
  @redemption
end

#voucherObject (readonly)

Returns the value of attribute voucher.



4
5
6
# File 'app/services/dscf/banking/voucher_service.rb', line 4

def voucher
  @voucher
end

Instance Method Details

#cancel_voucher(code:) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'app/services/dscf/banking/voucher_service.rb', line 113

def cancel_voucher(code:)
  @voucher = Dscf::Banking::Voucher.find_by(code: code.to_s.strip.upcase)

  unless @voucher
    add_error("Voucher not found")
    return self
  end

  if @voucher.redeemed? || @voucher.cancelled?
    add_error("Voucher cannot be cancelled")
    return self
  end

  ActiveRecord::Base.transaction do
    @voucher.lock!
    refundable = @voucher.remaining_amount.to_f
    @voucher.update!(status: :cancelled, remaining_amount: 0)

    if refundable.positive?
      create_voucher_transaction(
        debit_account: (@voucher.currency),
        credit_account: @voucher.,
        amount: refundable,
        description: "Voucher cancellation #{@voucher.code}"
      )
    end
  end

  self
end

#expire_due_vouchersObject



168
169
170
171
172
173
174
# File 'app/services/dscf/banking/voucher_service.rb', line 168

def expire_due_vouchers
  Dscf::Banking::Voucher.active_now.find_each do |voucher|
    expire_voucher(voucher) if voucher.expired?
  end

  self
end

#expire_voucher(voucher) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'app/services/dscf/banking/voucher_service.rb', line 144

def expire_voucher(voucher)
  return self unless voucher

  ActiveRecord::Base.transaction do
    voucher.lock!
    return self unless voucher.active?
    return self unless voucher.expired?

    refundable = voucher.remaining_amount.to_f
    voucher.update!(status: :expired, remaining_amount: 0)

    if refundable.positive?
      create_voucher_transaction(
        debit_account: (voucher.currency),
        credit_account: voucher.,
        amount: refundable,
        description: "Voucher expiration #{voucher.code}"
      )
    end
  end

  self
end

#generate_voucher(parent_account:, amount:, recipient_mobile:, recipient_name: nil, message: nil, expires_at: nil) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'app/services/dscf/banking/voucher_service.rb', line 12

def generate_voucher(parent_account:, amount:, recipient_mobile:, recipient_name: nil, message: nil, expires_at: nil)
  amount = amount.to_f
  return self unless validate_generate_params(, amount, recipient_mobile)

  expires_at ||= default_expiration

   = (.currency)
  return self unless success?

  voucher_code = generate_code

  ActiveRecord::Base.transaction do
    transaction = create_voucher_transaction(
      debit_account: ,
      credit_account: ,
      amount: amount,
      description: "Voucher issuance #{voucher_code}"
    )

    return self unless success?

    @voucher = Dscf::Banking::Voucher.create(
      parent_account: ,
      amount: amount,
      remaining_amount: amount,
      currency: .currency,
      recipient_mobile: recipient_mobile,
      recipient_name: recipient_name,
      message: message,
      expires_at: expires_at,
      status: :active,
      code: voucher_code
    )

    unless @voucher.persisted?
      @voucher.errors.full_messages.each { |msg| add_error(msg) }
      raise ActiveRecord::Rollback
    end

    transaction&.update(description: "Voucher issuance #{@voucher.code}")
  end

  self
end

#redeem_voucher(code:, amount:, recipient_mobile: nil, destination_account: nil) ⇒ Object



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
104
105
106
107
108
109
110
111
# File 'app/services/dscf/banking/voucher_service.rb', line 57

def redeem_voucher(code:, amount:, recipient_mobile: nil, destination_account: nil)
  amount = amount.to_f
  code = code.to_s.strip.upcase
  @voucher = Dscf::Banking::Voucher.find_by(code: code)

  unless @voucher
    add_error("Voucher not found")
    return self
  end

  return self unless validate_redeem_params(amount)
  return self unless validate_voucher_state(@voucher, amount)
  return self unless ensure_voucher_settlement_funds(@voucher.currency, amount)

  if 
    return self unless (, "Destination account")
    unless .currency == @voucher.currency
      add_error("Currency mismatch between voucher and destination account")
      return self
    end
  end

  ActiveRecord::Base.transaction do
    @voucher.lock!
    remaining_after = @voucher.remaining_amount.to_f - amount

    @redemption = Dscf::Banking::VoucherRedemption.create(
      voucher: @voucher,
      amount: amount,
      currency: @voucher.currency,
      recipient_mobile: recipient_mobile.presence || "N/A",
      destination_account: ,
      redeemed_at: Time.current
    )

    unless @redemption.persisted?
      @redemption.errors.full_messages.each { |msg| add_error(msg) }
      raise ActiveRecord::Rollback
    end

    update_voucher_balance_and_status(@voucher, remaining_after)
    return self unless success?

    if 
      create_voucher_transaction(
        debit_account: (@voucher.currency),
        credit_account: ,
        amount: amount,
        description: "Voucher redemption #{@voucher.code}"
      )
    end
  end

  self
end