Sha256: 4b24c5b1be750a1fe247e2d7349ef13eec53891745ce49478c5b99c67085f45a

Contents?: true

Size: 1.06 KB

Versions: 1

Compression:

Stored size: 1.06 KB

Contents

module QiwiObserver
  class Webhook
    def initialize(key)
      raise ArgumentError, "Secret key must be set" unless key

      @key = key
    end

    def call(params)
      return WebhookResponse.new(success: true, body: params) if params.dig(:test) == true
      return WebhookResponse.new(success: true, body: params) if sign_correct?(params)

      WebhookResponse.new(success: false, body: params.dig(:payment, :txnId))
    end

    private

    def sign_correct?(params)
      sign_fields = concat_sign_fields(params)
      secret_key = Base64.decode64(@key)
      secure_hash = OpenSSL::HMAC.hexdigest("SHA256", secret_key, sign_fields)
      params[:hash] == secure_hash
    end

    def concat_sign_fields(params)
      first_part = params.dig(:payment, :sum, :currency)
      second_part = params.dig(:payment, :sum, :amount)
      third_part = params.dig(:payment, :type)
      fourth_part = params.dig(:payment, :account)
      fifth_part = params.dig(:payment, :txnId)
      [first_part, second_part, third_part, fourth_part, fifth_part].join("|")
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
qiwi_observer-0.2.1 lib/qiwi_observer/webhook/webhook.rb