Class: Dscf::Payment::PaymentService

Inherits:
Object
  • Object
show all
Defined in:
app/services/dscf/payment/payment_service.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(payment_request, current_user = nil) ⇒ PaymentService

Returns a new instance of PaymentService.



5
6
7
8
# File 'app/services/dscf/payment/payment_service.rb', line 5

def initialize(payment_request, current_user = nil)
  @payment_request = payment_request
  @current_user = current_user
end

Instance Attribute Details

#current_userObject (readonly)

Returns the value of attribute current_user.



3
4
5
# File 'app/services/dscf/payment/payment_service.rb', line 3

def current_user
  @current_user
end

#payment_requestObject (readonly)

Returns the value of attribute payment_request.



3
4
5
# File 'app/services/dscf/payment/payment_service.rb', line 3

def payment_request
  @payment_request
end

Instance Method Details

#processObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/services/dscf/payment/payment_service.rb', line 10

def process
  validate_payment_request

  ActiveRecord::Base.transaction do
    payment = create_payment_record

    case payment_request.payment_type
    when "disbursement"
      process_disbursement(payment)
    when "transfer"
      process_transfer(payment)
    when "repayment"
      process_repayment(payment)
    else
      raise InvalidPaymentTypeError.new("Unknown payment type: #{payment_request.payment_type}")
    end

    mark_payment_completed(payment)
    payment
  end
rescue => e
  handle_payment_failure(e)
  raise
end