Class: DefraRubyMocks::GovpayController

Inherits:
ApplicationController show all
Defined in:
app/controllers/defra_ruby_mocks/govpay_controller.rb

Instance Method Summary collapse

Instance Method Details

#create_paymentObject



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'app/controllers/defra_ruby_mocks/govpay_controller.rb', line 8

def create_payment
  valid_create_params

  # Enqueue the payment callback to run after the controller responds
  DefraRubyMocks::GovpayPaymentCallbackJob.perform_later(params[:return_url])

  render json: GovpayCreatePaymentService.new.run(
    amount: params[:amount], description: params[:description], return_url: params[:return_url]
  )
rescue StandardError => e
  Rails.logger.error("MOCKS: Govpay payment creation error: #{e.message}")
  head 500
end

#create_refundObject



30
31
32
33
34
35
36
37
38
# File 'app/controllers/defra_ruby_mocks/govpay_controller.rb', line 30

def create_refund
  valid_create_refund_params
  render json: GovpayRequestRefundService.new.run(payment_id: params[:payment_id],
                                                  amount: params[:amount],
                                                  refund_amount_available: params[:refund_amount_available])
rescue StandardError => e
  Rails.logger.error("MOCKS: Govpay refund error: #{e.message}")
  head 500
end

#payment_detailsObject



22
23
24
25
26
27
28
# File 'app/controllers/defra_ruby_mocks/govpay_controller.rb', line 22

def payment_details
  valid_payment_id
  render json: GovpayGetPaymentService.new.run(payment_id: params[:payment_id])
rescue StandardError => e
  Rails.logger.error("MOCKS: Govpay payment details error: #{e.message}")
  head 422
end

#refund_detailsObject



40
41
42
43
44
45
# File 'app/controllers/defra_ruby_mocks/govpay_controller.rb', line 40

def refund_details
  render json: GovpayRefundDetailsService.new.run(payment_id: params[:payment_id], refund_id: params[:refund_id])
rescue StandardError => e
  Rails.logger.error("MOCKS: Govpay refund error: #{e.message}")
  head 500
end

#valid_create_paramsObject



47
48
49
# File 'app/controllers/defra_ruby_mocks/govpay_controller.rb', line 47

def valid_create_params
  params.require(i[amount description return_url])
end

#valid_create_refund_paramsObject

Raises:

  • (ArgumentError)


57
58
59
60
61
# File 'app/controllers/defra_ruby_mocks/govpay_controller.rb', line 57

def valid_create_refund_params
  valid_payment_id
  raise ArgumentError, "Invalid refund amount" unless params[:amount].present?
  raise ArgumentError, "Invalid refund amount available" unless params[:refund_amount_available].present?
end

#valid_payment_idObject

Raises:

  • (ArgumentError)


51
52
53
54
55
# File 'app/controllers/defra_ruby_mocks/govpay_controller.rb', line 51

def valid_payment_id
  return true if params[:payment_id].length > 20 && params[:payment_id].match(/\A[a-zA-Z0-9]*\z/)

  raise ArgumentError, "Invalid Govpay payment ID #{params[:payment_id]}"
end