Class: Spree::VpagoPaymentsController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/spree/vpago_payments_controller.rb

Instance Method Summary collapse

Instance Method Details

#access_deniedObject



89
90
91
92
93
94
# File 'app/controllers/spree/vpago_payments_controller.rb', line 89

def access_denied
  respond_to do |format|
    format.html { render file: Rails.public_path.join('422.html'), status: :not_found, layout: false }
    format.json { render json: { status: :unauthorized }, status: :unauthorized }
  end
end

#checkoutObject

GET

Raises:

  • (ActiveRecord::RecordNotFound)


12
13
14
15
16
17
18
19
# File 'app/controllers/spree/vpago_payments_controller.rb', line 12

def checkout
  @payment = Vpago::PaymentFinder.new(params.permit!.to_h).find_and_verify
  raise ActiveRecord::RecordNotFound unless @payment.present?

  return redirect_to @payment.processing_url, allow_other_host: true unless @payment.checkout?

  @order = @payment.order
end

#process_paymentObject

POST



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'app/controllers/spree/vpago_payments_controller.rb', line 39

def process_payment
  return render json: { status: :ok }, status: :ok if request.method != 'POST'

  return_params = sanitize_return_params
  @payment = Vpago::PaymentFinder.new(return_params).find_and_verify
  return render_not_found unless @payment.present?

  unless @payment.order.paid?
    Vpago::PaymentProcessorJob.perform_later(
      payment_number: @payment.number
    )
  end

  render json: { status: :ok }, status: :ok
rescue StandardError => e
  Rails.logger.error("Failed to enqueued payment processor job: #{params} #{e.message}")
  render json: { status: :internal_server_error, message: 'Failed to enqueue payment processor job' }, status: :internal_server_error
end

#processingObject

GET

Raises:

  • (ActiveRecord::RecordNotFound)


22
23
24
25
26
27
# File 'app/controllers/spree/vpago_payments_controller.rb', line 22

def processing
  @payment = Vpago::PaymentFinder.new(params.permit!.to_h).find_and_verify
  raise ActiveRecord::RecordNotFound unless @payment.present?

  @order = @payment.order
end

#render_not_foundObject



82
83
84
85
86
87
# File 'app/controllers/spree/vpago_payments_controller.rb', line 82

def render_not_found
  respond_to do |format|
    format.html { render file: Rails.public_path.join('404.html'), status: :not_found, layout: false }
    format.json { render json: { status: :not_found }, status: :not_found }
  end
end

#sanitize_return_paramsObject



73
74
75
76
77
78
79
80
# File 'app/controllers/spree/vpago_payments_controller.rb', line 73

def sanitize_return_params
  sanitized_params = params.permit!.to_h

  # In ABA case, it returns params in side return params.
  sanitized_params.merge!(JSON.parse(sanitized_params.delete(:return_params))) if sanitized_params[:return_params].present?

  sanitized_params
end

#successObject

GET

Raises:

  • (ActiveRecord::RecordNotFound)


30
31
32
33
34
35
36
# File 'app/controllers/spree/vpago_payments_controller.rb', line 30

def success
  @payment = Vpago::PaymentFinder.new(params.permit!.to_h).find_and_verify
  raise ActiveRecord::RecordNotFound unless @payment.present?

  @order = @payment.order
  raise CanCan::AccessDenied unless @order.completed?
end

#true_money_process_paymentObject

POST



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'app/controllers/spree/vpago_payments_controller.rb', line 59

def true_money_process_payment
  return render json: { status: { code: '000001', message: 'success' }, data: nil }, status: :ok if request.method != 'POST'

  @payment = Spree::Payment.find_by(number: params.dig(:data, :external_ref_id))
  return render_not_found unless @payment

  Vpago::PaymentProcessorJob.perform_later(payment_number: @payment.number) unless @payment.order.paid?

  render json: { status: { code: '000001', message: 'success' }, data: nil }, status: :ok
rescue StandardError => e
  Rails.logger.error("Payment error: #{e.message}")
  render json: { status: :internal_server_error, message: 'Failed to enqueue payment processor job' }, status: :internal_server_error
end