class SolidusPaybright::CallbackValidator

Public Class Methods

new(params) click to toggle source

@param params [Hash] The Paybright callback params

# File lib/solidus_paybright/callback_validator.rb, line 4
def initialize(params)
  @params = params
end

Public Instance Methods

call() click to toggle source

@return [Array] The validation result and the error message

# File lib/solidus_paybright/callback_validator.rb, line 9
def call
  payment = Spree::Payment.find_by(id: @params[:x_reference])

  unless payment
    return [false, "Payment #{@params[:x_reference]} not found"]
  end

  if payment.completed?
    return [false, "Payment #{payment.id} is already in completed state"]
  end

  if payment.order.complete?
    return [false, "Order is already in complete state"]
  end

  if !signing_helper(payment).valid_params?(@params.to_hash)
    return [false, "Invalid parameters signature"]
  end

  if @params[:x_result].casecmp("completed") != 0
    return [false, "The contract was not completed (#{@params[:x_result]})"]
  end

  [true, ""]
end

Private Instance Methods

signing_helper(payment) click to toggle source
# File lib/solidus_paybright/callback_validator.rb, line 37
def signing_helper(payment)
  api_token = payment.payment_method.preferences.fetch(:api_token)
  SolidusPaybright::SigningHelper.new(api_token)
end