liqpay 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in liqpay.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,3 @@
1
+ # LiqPAY
2
+
3
+ This Ruby gem implements the [LiqPAY](http://liqpay.com) billing system API.
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
@@ -0,0 +1,24 @@
1
+ require 'digest/sha1'
2
+ require 'base64'
3
+
4
+ module Liqpay
5
+ class BaseOperation
6
+ attr_accessor :merchant_id, :merchant_signature
7
+
8
+ def initialize(options={})
9
+ options.replace(Liqpay.default_options.merge(options))
10
+
11
+ @merchant_id = options[:merchant_id]
12
+ @merchant_signature = options[:merchant_signature]
13
+ end
14
+
15
+ def signature
16
+ @signature ||= sign(xml, @merchant_signature)
17
+ end
18
+
19
+ private
20
+ def sign(xml, signature)
21
+ Base64.encode64(Digest::SHA1.digest(signature + xml + signature)).strip
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,72 @@
1
+ require 'base64'
2
+
3
+ module Liqpay
4
+ class Request < BaseOperation
5
+ attr_accessor :result_url, :server_url, :order_id, :amount, :currency, :description, :default_phone, :pay_way, :goods_id, :exp_time
6
+
7
+ def initialize(options={})
8
+ super(options)
9
+
10
+ @result_url = options[:result_url]
11
+ @server_url = options[:server_url]
12
+ @order_id = options[:order_id]
13
+ @amount = options[:amount]
14
+ @currency = options[:currency]
15
+ @description = options[:description]
16
+ @default_phone = options[:default_phone]
17
+ @pay_way = options[:pay_way]
18
+ @kamikaze = options[:kamikaze]
19
+ end
20
+
21
+ def encoded_xml
22
+ @encoded_xml ||= encode(xml)
23
+ end
24
+
25
+ def xml
26
+ @xml ||= make_xml
27
+ end
28
+
29
+
30
+ private
31
+ def encode(xml)
32
+ Base64.encode64(xml).gsub(/\s/,'')
33
+ end
34
+
35
+ def make_xml
36
+ validate! unless @kamikaze
37
+ Nokogiri::XML::Builder.new { |xml|
38
+ xml.request {
39
+ xml.version Liqpay::API_VERSION
40
+ xml.merchant_id merchant_id
41
+ xml.result_url result_url
42
+ xml.server_url server_url
43
+ xml.order_id order_id
44
+ xml.amount "%0.2f" % amount
45
+ xml.currency currency
46
+ xml.description description
47
+ xml.default_phone default_phone
48
+ xml.pay_way pay_way.is_a?(Array) ? pay_way.join(',') : pay_way
49
+ }
50
+ }.to_xml
51
+ end
52
+
53
+ def validate!
54
+
55
+ %w(merchant_id merchant_signature currency amount order_id).each do |required_field|
56
+ raise Liqpay::Exception.new(required_field + ' is a required field') unless self.send(required_field).to_s != ''
57
+ end
58
+
59
+ raise Liqpay::Exception.new('currency must be one of '+Liqpay::SUPPORTED_CURRENCIES.join(', ')) unless Liqpay::SUPPORTED_CURRENCIES.include?(currency)
60
+
61
+ begin
62
+ self.amount = Float(self.amount)
63
+ rescue ArgumentError, TypeError
64
+ raise Liqpay::Exception.new('amount must be a number')
65
+ end
66
+
67
+ raise Liqpay::Exception.new('goods_id must only contain digits') unless goods_id.to_s =~ /\A\d*\Z/
68
+
69
+ raise Liqpay::Exception.new('amount must be more than 0.01') unless amount > 0.01
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,37 @@
1
+ require 'base64'
2
+ require 'nokogiri'
3
+
4
+ module Liqpay
5
+ class Response < BaseOperation
6
+ attr_reader :encoded_xml, :signature, :xml
7
+
8
+ ATTRIBUTES = %w(merchant_id order_id amount currency description status code transaction_id pay_way sender_phone goods_id pays_count)
9
+ ATTRIBUTES.each do |attr|
10
+ attr_reader attr
11
+ end
12
+
13
+ def initialize(options = {})
14
+ super(options)
15
+
16
+ @encoded_xml = options[:operation_xml]
17
+ @signature = options[:signature]
18
+
19
+ decode!
20
+ end
21
+
22
+ private
23
+ def decode!
24
+ @xml = Base64.decode64(@encoded_xml)
25
+
26
+ if sign(@xml, @merchant_signature) != @signature
27
+ raise Liqpay::InvalidResponse
28
+ end
29
+
30
+ doc = Nokogiri.XML(@xml)
31
+
32
+ ATTRIBUTES.each do |attr|
33
+ self.instance_variable_set('@'+attr, doc.at(attr).try(:content))
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,3 @@
1
+ module Liqpay
2
+ VERSION = "0.0.1"
3
+ end
data/lib/liqpay.rb ADDED
@@ -0,0 +1,15 @@
1
+ require 'liqpay/version'
2
+ require 'liqpay/request'
3
+ require 'liqpay/response'
4
+
5
+ module Liqpay
6
+ API_VERSION = '1.2'
7
+ ENDPOINT_URL = 'https://www.liqpay.com/?do=clickNbuy'
8
+ SUPPORTED_CURRENCIES = %w(UAH USD EUR RUR)
9
+
10
+ @default_options = {}
11
+ class << self; attr_accessor :default_options; end
12
+
13
+ class Exception < ::Exception; end
14
+ class InvalidResponse < Exception; end
15
+ end
data/liqpay.gemspec ADDED
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "liqpay/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "liqpay"
7
+ s.version = Liqpay::VERSION
8
+ s.authors = ["Leonid Shevtsov"]
9
+ s.email = ["leonid@shevtsov.me"]
10
+ s.homepage = "http://leonid.shevtsov.me/en/liqpay"
11
+ s.summary = %q{LiqPAY billing API implementation in Ruby}
12
+ s.description = %q{LiqPAY billing API implementation in Ruby}
13
+
14
+ s.add_dependency 'nokogiri'
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+ end
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: liqpay
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Leonid Shevtsov
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-09-03 00:00:00 +03:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: nokogiri
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ description: LiqPAY billing API implementation in Ruby
36
+ email:
37
+ - leonid@shevtsov.me
38
+ executables: []
39
+
40
+ extensions: []
41
+
42
+ extra_rdoc_files: []
43
+
44
+ files:
45
+ - .gitignore
46
+ - Gemfile
47
+ - README.md
48
+ - Rakefile
49
+ - lib/liqpay.rb
50
+ - lib/liqpay/base_operation.rb
51
+ - lib/liqpay/request.rb
52
+ - lib/liqpay/response.rb
53
+ - lib/liqpay/version.rb
54
+ - liqpay.gemspec
55
+ has_rdoc: true
56
+ homepage: http://leonid.shevtsov.me/en/liqpay
57
+ licenses: []
58
+
59
+ post_install_message:
60
+ rdoc_options: []
61
+
62
+ require_paths:
63
+ - lib
64
+ required_ruby_version: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ hash: 3
70
+ segments:
71
+ - 0
72
+ version: "0"
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ hash: 3
79
+ segments:
80
+ - 0
81
+ version: "0"
82
+ requirements: []
83
+
84
+ rubyforge_project:
85
+ rubygems_version: 1.6.2
86
+ signing_key:
87
+ specification_version: 3
88
+ summary: LiqPAY billing API implementation in Ruby
89
+ test_files: []
90
+