module Paysto

Constants

RXP
VERSION

Public Class Methods

balance() click to toggle source

Your current balance in Paysto.

# File lib/paysto/base.rb, line 47
def balance
  https_request_to @@urls[:balance], base_params
end
currencies() click to toggle source

List of available pay methods according to your tariff plan.

# File lib/paysto/base.rb, line 42
def currencies
  https_request_to @@urls[:currencies], base_params
end
get_payment_type(invoice_id, time = Time.zone.now) click to toggle source

Returns payment type or 'common' by default. invoice_id – invoice ID of payment. time – estimated payment execution time.

# File lib/paysto/base.rb, line 67
def get_payment_type(invoice_id, time = Time.zone.now)
  _payments = get_payments(time.utc - 30.minutes, time.utc + 5.minutes)
  if _payments.present?
    p = _payments.select{ |_p| _p[2].eql?(invoice_id.to_s) }.first
    _type = p[7] if p
  end
  _type || 'common'
end
get_payments(from, to) click to toggle source

Returns array of payments with details between dates. from – from date. to – to date.

# File lib/paysto/base.rb, line 54
def get_payments(from, to)
  p = { 'PAYSTO_SHOP_ID' => @@id,
        'FROM'           => from.strftime('%Y%m%d%H%M'),
        'TO'             => to.strftime('%Y%m%d%H%M') }
  p.merge!('PAYSTO_MD5'  => generate_md5(p))

  res = https_request_to(@@urls[:payments_list], p)
  CSV.parse(res)
end
invoice_class() click to toggle source

Invoice class.

# File lib/paysto/base.rb, line 32
def invoice_class
  @@invoice_class_name.constantize
end
invoice_notification_class() click to toggle source

InvoiceNotification class.

# File lib/paysto/base.rb, line 37
def invoice_notification_class
  @@invoice_notification_class_name.constantize
end
invoice_valid?(invoice) click to toggle source

Check whether the invoice is valid.

# File lib/paysto/base.rb, line 77
def invoice_valid?(invoice)
  invoice && invoice.send("#{@@payment_class_name.underscore}_id").blank? && invoice.paid_at.blank?
end
ip_valid?(ip) click to toggle source

Check whether the IP is permitted.

# File lib/paysto/base.rb, line 82
def ip_valid?(ip)
  @@ips.include?(ip)
end
md5_valid?(p) click to toggle source

Check whether the MD5 sign is valid.

# File lib/paysto/base.rb, line 87
def md5_valid?(p)
  hash = p.except('action', 'controller', 'PAYSTO_MD5')
  generate_md5(hash) == p['PAYSTO_MD5']
end
pay_till() click to toggle source

Timestamp string in Paysto format for payments expiration.

# File lib/paysto/base.rb, line 93
def pay_till
  (Time.zone.now + @@expiration).utc.strftime('%Y%m%d%H%M')
end
payment_class() click to toggle source

Payment class.

# File lib/paysto/base.rb, line 27
def payment_class
  @@payment_class_name.constantize
end
real_amount(str) click to toggle source

Real income value without Paysto tax for any amount which you want to calculate. str – amount string or numeric, does not matter.

# File lib/paysto/base.rb, line 99
def real_amount(str)
  amount = str.to_f
  _tax = amount * @@tax
  _tax = @@min_tax if _tax < @@min_tax
  amount - _tax
end
setup() { |self| ... } click to toggle source

Configuring module.

# File lib/paysto/base.rb, line 22
def setup
  yield self
end

Private Class Methods

base_params() click to toggle source

Base set of params for requests.

# File lib/paysto/base.rb, line 122
def base_params
  p = { 'PAYSTO_SHOP_ID' => @@id }
  p.merge!('PAYSTO_MD5'  => generate_md5(p))
end
generate_md5(p = {}, upcase = true) click to toggle source

Generating MD5 sign according with received params.

# File lib/paysto/base.rb, line 109
def generate_md5(p = {}, upcase = true)
  md5 = Digest::MD5.hexdigest(md5_string(p))
  upcase ? md5.upcase : md5
end
https_request_to(url, params) click to toggle source

Performing an HTTPS request.

# File lib/paysto/base.rb, line 128
def https_request_to(url, params)
  uri = URI.parse(url)
  req = Net::HTTP::Post.new(uri.path)
  req.set_form_data(params)
  session = Net::HTTP.new(uri.hostname, 443)
  session.use_ssl = true
  res = session.start do |http|
    http.request(req)
  end
  res.body.force_encoding('UTF-8')
end
md5_string(p = {}) click to toggle source

Final data string to gererane MD5.

# File lib/paysto/base.rb, line 115
def md5_string(p = {})
  str = p.to_a.sort_by{ |pair| pair.first.downcase }.map{ |pair| pair.join('=') }.join('&')
  str_with_secret = [str, @@secret].join('&')
  str_with_secret.gsub(RXP, '')
end