Sha256: 7fd5208dc48a383d648b74b1e4e3c7ea464fcac93c4d567b3b2d36ddf88955df

Contents?: true

Size: 1.5 KB

Versions: 1

Compression:

Stored size: 1.5 KB

Contents

module Unitpay
  class Service
    EXTRA_OPTIONS = [:locale, :hideHint, :hideBackUrl, :hideOrderCost, :hideMenu, :hideDesc, :hideOtherMethods]
    URL = 'https://unitpay.ru/pay'

    def initialize(public_key, secret_key, currency = 'RUB')
      @public_key, @secret_key, @currency = public_key, secret_key, currency
    end

    def payment_url(sum, account, desc, options = {})
      URI.escape(url(sum, account, desc, options))
    end

    def payment_params(sum, account, desc, options = {})
      main_params(sum, account, desc, options[:use_sign]).merge(extra_params(options))
    end

    def valid_sign?(current_sign, sum, account, desc)
      current_sign == calculate_sign(sum, account, desc)
    end

    private

    attr_reader :public_key, :secret_key, :currency

    def calculate_sign(sum, account, desc)
      Digest::MD5.hexdigest( [account, currency, desc, sum, secret_key].join )
    end

    def main_params(sum, account, desc, use_sign)
      use_sign = true if use_sign.nil?
      sign = use_sign ? { sign: calculate_sign(sum, account, desc) } : {}

      {
        sum: sum,
        account: account,
        desc: desc,
        currency: currency
      }.merge(sign)
    end

    def extra_params(options)
      options.select { |key, _| EXTRA_OPTIONS.include?(key) }
    end

    def url(sum, account, desc, options)
      "#{ URL }/#{ public_key }?#{ to_query(payment_params(sum, account, desc, options)) }"
    end

    def to_query(hash)
      hash.map { |k, v| "#{k}=#{v}" }.join('&')
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
unitpay-0.0.1 lib/unitpay/service.rb