Sha256: 449e9c2bb31f2de9d85a892993183735c6c7582e90414ca9472fd9e841c581a8
Contents?: true
Size: 1.86 KB
Versions: 3
Compression:
Stored size: 1.86 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, use_sign = true, currency = 'RUB') @public_key, @secret_key, @use_sign, @currency = public_key, secret_key, use_sign, 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).merge(extra_params(options)) end def params_for_widget(sum, account, desc) main_params(sum, account, desc).merge(publicKey: public_key) end def valid_sign?(current_sign, sum, account, desc) current_sign == calculate_sign(sum, account, desc) end def valid_notify_sign?(params) params[:sign] == calculate_notify_sign(params) end private attr_reader :public_key, :secret_key, :currency, :use_sign def calculate_sign(sum, account, desc) Digest::MD5.hexdigest( [account, currency, desc, sum, secret_key].join ) end def calculate_notify_sign(params) params.delete(:sign) values = Hash[ params.sort ].values + [ secret_key ] Digest::MD5.hexdigest(values.join) end def main_params(sum, account, desc) 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
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
unitpay-0.2 | lib/unitpay/service.rb |
unitpay-0.1 | lib/unitpay/service.rb |
unitpay-0.0.4 | lib/unitpay/service.rb |