Sha256: 7219ed340b75b97089f6221cf9a5344ea8cd7917cab953f42ddb7edab86a10f1

Contents?: true

Size: 1.8 KB

Versions: 4

Compression:

Stored size: 1.8 KB

Contents

# frozen_string_literal: true

module Fawry
  class FawryCallback
    include Utils

    attr_reader :callback_params, :fawry_secure_key, :options

    def initialize(callback_params, opts)
      @callback_params = callback_params
      @fawry_secure_key = Fawry.configuration.fawry_secure_key || ENV.fetch('FAWRY_SECURE_KEY')
      @options = opts
    end

    def parse
      verify_callback_signature!
      build_callback

      self
    end

    private

    # Adds keys from fawry API response as methods
    # on FawryCallback instance that return the value
    # of each key
    #
    # type => type
    # referenceNumber => reference_number
    # merchantRefNumber => merchant_ref_number
    # expirationTime => expiration_time
    # statusCode => status_code
    # statusDescription => status_description
    #
    # fawry_callback = FawryCallback.new(callback_params, fawry_secure_key)
    # fawry_callback.order_status => PAID
    # fawry_callback.fawry_ref_number => 1234567
    def build_callback
      enrich_object(callback_params)
    end

    def verify_callback_signature!
      raise InvalidSignatureError, 'Invalid Signature' unless signature == callback_params[:messageSignature]
    end

    # rubocop:disable Metrics/AbcSize
    def signature
      Digest::SHA256.hexdigest("#{callback_params[:fawryRefNumber]}#{callback_params[:merchantRefNum]}"\
                               "#{format('%<paymentAmount>.2f', paymentAmount: callback_params[:paymentAmount])}"\
                               "#{format('%<orderAmount>.2f', orderAmount: callback_params[:orderAmount])}"\
                               "#{callback_params[:orderStatus]}#{callback_params[:paymentMethod]}"\
                               "#{callback_params[:paymentRefrenceNumber]}#{fawry_secure_key}")
    end
    # rubocop:enable Metrics/AbcSize
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
fawry-1.4.1 lib/fawry/fawry_callback.rb
fawry-1.4.0 lib/fawry/fawry_callback.rb
fawry-1.3.1 lib/fawry/fawry_callback.rb
fawry-1.3.0 lib/fawry/fawry_callback.rb