rbkmoney 0.0.1 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -15,3 +15,4 @@ spec/reports
15
15
  test/tmp
16
16
  test/version_tmp
17
17
  tmp
18
+ .DS_Store
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
- # Rbkmoney
1
+ # RBK Money
2
2
 
3
- TODO: Write a gem description
3
+ This gem helps to integrate RBK Money payments into Ruby on Rails applications.
4
4
 
5
5
  ## Installation
6
6
 
@@ -16,9 +16,59 @@ Or install it yourself as:
16
16
 
17
17
  $ gem install rbkmoney
18
18
 
19
+ And finally install `rbkmoney.yml` configuration file:
20
+
21
+ $ rails g rbkmoney:install
22
+
19
23
  ## Usage
20
24
 
21
- TODO: Write usage instructions here
25
+ 1. Fill in your `config/rbkmoney.yml` file with data.
26
+
27
+ 2. Create your RBK Money form as follows:
28
+
29
+ <%= rbkmoney_form_tag do %>
30
+ <%= rbkmoney_setup order_id: "<YOUR_ORDER_ID>",
31
+ item_name: "<ITEM_NAME>",
32
+ user_name: "<USER_NAME>",
33
+ user_email: "<USER_EMAIL>",
34
+ amount: 1,
35
+ success_url: "<CUSTOM_SUCCESS_URL>",
36
+ fail_url: "<CUSTOM_FAIL_URL>" %>
37
+
38
+ <%= submit_tag %>
39
+ <% end %>
40
+
41
+ 3. Add payment processing to your controller, e.g.:
42
+
43
+
44
+ class PaymentsController
45
+ def process
46
+ def payment
47
+ notification = RBKMoney::Notification.new(params)
48
+
49
+ if notification.acknowledge # check if payment is valid
50
+ if notification.accepted? # check if payment is accepted
51
+ if notification.completed? # check if payment is completed
52
+ order = Order.find(notification.order_id)
53
+ if order # check if order exists
54
+ if order.total == notification.amount # check payment amount
55
+ ...
56
+ head :ok and return
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
62
+
63
+ head :bad_request and return
64
+ end
65
+ end
66
+ end
67
+
68
+ 4. Add route for your new action, e.g.:
69
+
70
+ post 'payments/process' => 'payments#process'
71
+
22
72
 
23
73
  ## Contributing
24
74
 
@@ -0,0 +1,14 @@
1
+ module Rbkmoney
2
+ module Generators
3
+ class InstallGenerator < Rails::Generators::Base
4
+ desc << "Description:\n Copies configuration file to your application's config folder."
5
+
6
+ source_root File.expand_path('../templates', __FILE__)
7
+
8
+ def copy_files
9
+ puts "Installing config:"
10
+ copy_file "config/rbkmoney.yml", "config/rbkmoney.yml"
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,23 @@
1
+ development:
2
+ eshop_id: <YOUR_ESHOP_ID>
3
+ eshop_account: <YOUR_ESHOP_ACCOUNT>
4
+ currency: 'RUR'
5
+ success_url: <SUCCESS_URL>
6
+ fail_url: <FAIL_URL>
7
+ secret_key: <SECRET_KEY>
8
+
9
+ test:
10
+ eshop_id: <YOUR_ESHOP_ID>
11
+ eshop_account: <YOUR_ESHOP_ACCOUNT>
12
+ currency: 'RUR'
13
+ success_url: <SUCCESS_URL>
14
+ fail_url: <FAIL_URL>
15
+ secret_key: <SECRET_KEY>
16
+
17
+ production:
18
+ eshop_id: <YOUR_ESHOP_ID>
19
+ eshop_account: <YOUR_ESHOP_ACCOUNT>
20
+ currency: 'RUR'
21
+ success_url: <SUCCESS_URL>
22
+ fail_url: <FAIL_URL>
23
+ secret_key: <SECRET_KEY>
@@ -17,23 +17,27 @@ module RBKMoney
17
17
  end
18
18
 
19
19
  def self.eshop_id
20
- config['eshop_id'].to_i
20
+ self.config['eshop_id'].to_i rescue nil
21
21
  end
22
22
 
23
23
  def self.eshop_account
24
- config['eshop_account']
24
+ self.config['eshop_account'] rescue nil
25
25
  end
26
26
 
27
27
  def self.currency
28
- config['currency']
28
+ self.config['currency'] rescue nil
29
29
  end
30
30
 
31
31
  def self.success_url
32
- config['success_url']
32
+ self.config['success_url'] rescue nil
33
33
  end
34
34
 
35
35
  def self.fail_url
36
- config['fail_url']
36
+ self.config['fail_url'] rescue nil
37
+ end
38
+
39
+ def self.secret_key
40
+ self.config['secret_key'] rescue nil
37
41
  end
38
42
 
39
43
  end
@@ -0,0 +1,50 @@
1
+ module RBKMoney
2
+ module Helpers
3
+ def rbkmoney_form_tag(url = RBKMoney::purchase_uri, options = {})
4
+ form_tag(url, options)
5
+ end
6
+
7
+ def rbkmoney_form_tag(url = RBKMoney::purchase_uri, options = {}, &block)
8
+ if block
9
+ form_tag(url, options, &block)
10
+ else
11
+ form_tag(url, options)
12
+ end
13
+ end
14
+
15
+ def rbkmoney_setup(options = {})
16
+ params = {
17
+ eshop_id: RBKMoney::eshop_id,
18
+ currency: RBKMoney::currency,
19
+ success_url: RBKMoney::success_url,
20
+ fail_url: RBKMoney::fail_url,
21
+ locale: 'ru',
22
+ custom: []
23
+ }.reject{|k,v| v.nil?}.merge(options)
24
+
25
+ form = []
26
+ form << tag(:input, :type => 'hidden', :name => 'eshopId', :value => params[:eshop_id])
27
+ form << tag(:input, :type => 'hidden', :name => 'orderId', :value => params[:order_id])
28
+ form << tag(:input, :type => 'hidden', :name => 'serviceName', :value => params[:item_name])
29
+ form << tag(:input, :type => 'hidden', :name => 'recipientAmount', :value => params[:amount])
30
+ form << tag(:input, :type => 'hidden', :name => 'recipientCurrency', :value => params[:currency])
31
+ form << tag(:input, :type => 'hidden', :name => 'successUrl', :value => params[:success_url])
32
+ form << tag(:input, :type => 'hidden', :name => 'failUrl', :value => params[:fail_url])
33
+ form << tag(:input, :type => 'hidden', :name => 'requestedLanguage', :value => params[:locale])
34
+ form << tag(:input, :type => 'hidden', :name => 'userName', :value => params[:user_name])
35
+ form << tag(:input, :type => 'hidden', :name => 'user_email', :value => params[:user_email])
36
+
37
+ params[:custom].each_with_index do |value, i|
38
+ form << tag(:input, :type => 'hidden', :name => "userField_#{i}", :value => value)
39
+ end
40
+
41
+ form.map(&:to_s).join("\n").html_safe
42
+ end
43
+ end
44
+
45
+ class Railtie < Rails::Railtie
46
+ initializer "rbkmoney.helpers" do
47
+ ActionView::Base.send :include, Helpers
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,94 @@
1
+ module RBKMoney
2
+ class Notification
3
+ attr_accessor :params
4
+
5
+ def initialize(p)
6
+ @params = p
7
+ end
8
+
9
+ def accepted?
10
+ completed? or status == "3"
11
+ end
12
+
13
+ def completed?
14
+ status == "5"
15
+ end
16
+
17
+ def amount
18
+ recipient_amount.to_f
19
+ end
20
+
21
+ def eshop_id
22
+ @params['eshopId']
23
+ end
24
+
25
+ def payment_id
26
+ @params['paymentId']
27
+ end
28
+
29
+ def order_id
30
+ @params['orderId']
31
+ end
32
+
33
+ def service_name
34
+ @params['serviceName']
35
+ end
36
+
37
+ def eshop_account
38
+ @params['eshopAccount']
39
+ end
40
+
41
+ def recipient_amount
42
+ @params['recipientAmount']
43
+ end
44
+
45
+ def currency
46
+ @params['recipientCurrency']
47
+ end
48
+
49
+ def status
50
+ @params['paymentStatus']
51
+ end
52
+
53
+ def user_name
54
+ @params['userName']
55
+ end
56
+
57
+ def user_email
58
+ @params['userEmail']
59
+ end
60
+
61
+ def payment_data
62
+ @params['paymentData']
63
+ end
64
+
65
+ def received_at
66
+ Time.parse payment_data
67
+ end
68
+
69
+ def secret_key
70
+ RBKMoney::secret_key
71
+ end
72
+
73
+ def received_hash
74
+ @params['hash']
75
+ end
76
+
77
+ def user_fields
78
+ @params.select{ |p| p =~ /userField_[0-9]+/ }.map{ |k, v| v }
79
+ end
80
+
81
+ def to_s
82
+ values = [:eshop_id, :order_id, :service_name, :recipient_amount, :currency, :status, :user_name, :user_email, :payment_data, :received_hash].map { |property| "#{property}: '#{self.send property}'" }.join(', ')
83
+ "<#{values}>"
84
+ end
85
+
86
+ def generated_hash
87
+ Digest::MD5.hexdigest([eshop_id, order_id, service_name, eshop_account, recipient_amount, currency, status, user_name, user_email, payment_data, secret_key].join('::'))
88
+ end
89
+
90
+ def acknowledge
91
+ received_hash == generated_hash
92
+ end
93
+ end
94
+ end
@@ -1,3 +1,3 @@
1
1
  module RBKMoney
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.1"
3
3
  end
@@ -8,7 +8,7 @@ Gem::Specification.new do |gem|
8
8
  gem.version = RBKMoney::VERSION
9
9
  gem.authors = ["Maksim Gladkov"]
10
10
  gem.email = ["maksim.gladkov@gmail.com"]
11
- gem.description = %q{This gem helps to integrate RBK Money payments into Ruby on Rails applications.}
11
+ gem.description = %q{This gem helps to integrate RBK Money payments into Ruby on Rails applications}
12
12
  gem.summary = %q{RBK Money wrapper gem for Ruby on Rails}
13
13
  gem.homepage = ""
14
14
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rbkmoney
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,9 +9,9 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-11-27 00:00:00.000000000 Z
12
+ date: 2014-02-26 00:00:00.000000000 Z
13
13
  dependencies: []
14
- description: This gem helps to integrate RBK Money payments into Ruby on Rails applications.
14
+ description: This gem helps to integrate RBK Money payments into Ruby on Rails applications
15
15
  email:
16
16
  - maksim.gladkov@gmail.com
17
17
  executables: []
@@ -23,7 +23,11 @@ files:
23
23
  - LICENSE.txt
24
24
  - README.md
25
25
  - Rakefile
26
+ - lib/generators/rbkmoney/install_generator.rb
27
+ - lib/generators/rbkmoney/templates/config/rbkmoney.yml
26
28
  - lib/rbkmoney.rb
29
+ - lib/rbkmoney/helper.rb
30
+ - lib/rbkmoney/notification.rb
27
31
  - lib/rbkmoney/version.rb
28
32
  - rbkmoney.gemspec
29
33
  homepage: ''
@@ -51,3 +55,4 @@ signing_key:
51
55
  specification_version: 3
52
56
  summary: RBK Money wrapper gem for Ruby on Rails
53
57
  test_files: []
58
+ has_rdoc: