trust_pay_rails 0.1.0

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: bcbb1d0f7e318548f2839f8cbeb9618d138c2b5f
4
+ data.tar.gz: 8d19b3935f8de119068e6e8f8bcdb2212607c3f8
5
+ SHA512:
6
+ metadata.gz: 373acf59ec1430047bbf6e1740d8198f1f20f659a3093f6a5ec12ba2efd1cf19a32e45a41366003bf4d761ab05e73281c93bdbaecaf1481ffcc8a4e812c4f149
7
+ data.tar.gz: 139aa1beb5aeda7d5fd646559037229a84d683ea575dc4021789eb0841fbb269efe6583d3c66f79122540af8f638c6067bb0bdb4a97b3ed785bb157ec8382aab
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.1
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in trust_pay_rails.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Michal Olah
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,88 @@
1
+ # TrustPayRails
2
+
3
+ A simple implementation of the [Trustpay](http://www.trustpay.eu/)
4
+ payment platform to integrate into Rails apps.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'trust_pay_rails'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install trust_pay_rails
21
+
22
+ ## Usage
23
+
24
+ To configure the gem you can provide the following environment
25
+ variables:
26
+
27
+ ``` bash
28
+ TRUSTPAY_ENV - [testing|production|fake_bank_url] select an environment
29
+ TRUSTPAY_KEY - your identifier received after registering with Trustpay
30
+ TRUSTPAY_KEY - your key received after registering with TrustPay
31
+ ```
32
+
33
+ You can also configure the gem with an initializer:
34
+
35
+ ``` ruby
36
+ TrustPayRails.environment = (ENV['TRUSTPAY_ENV'] || :testing).to_sym
37
+ TrustPayRails.aid = ENV['TRUSTPAY_AID']
38
+ TrustPayRails.key = ENV['TRUSTPAY_KEY']
39
+ ```
40
+
41
+ After configuring the gem you can create a TrustPay form using one of
42
+ the provided view helpers.
43
+
44
+ ``` erb
45
+ <%= trust_pay_form(amt: '123.45',
46
+ cur: 'EUR',
47
+ ref: '1234567890',
48
+ rurl: trust_pay_return_url,
49
+ curl: trust_pay_cancel_url,
50
+ eurl: trust_pay_error_url,
51
+ nurl: trust_pay_notification_url,
52
+ dsc: 'a description no longer than 200 chars',
53
+ lng: I18n.locale)
54
+ %>
55
+
56
+
57
+ <%= trust_pay_card_form(amt: '123.45',
58
+ cur: 'EUR',
59
+ ref: '1234567890',
60
+ rurl: trust_pay_return_url,
61
+ curl: trust_pay_cancel_url,
62
+ eurl: trust_pay_error_url,
63
+ nurl: trust_pay_notification_url,
64
+ dsc: 'a description no longer than 200 chars',
65
+ lng: I18n.locale)
66
+ %>
67
+ ```
68
+
69
+ To generate the TrustPay notification urls use the provided generator:
70
+
71
+ ``` bash
72
+ rails g trust_pay_rails:routes
73
+ ```
74
+
75
+
76
+ ## Development
77
+
78
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
79
+
80
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
81
+
82
+ ## Contributing
83
+
84
+ 1. Fork it ( https://github.com/[my-github-username]/trust_pay_rails/fork )
85
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
86
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
87
+ 4. Push to the branch (`git push origin my-new-feature`)
88
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ begin
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
9
+ rescue LoadError
10
+ # no rspec available
11
+ end
12
+
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "trust_pay_rails"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,19 @@
1
+ module TrustPayRails
2
+ module Generators
3
+ class RoutesGenerator < Rails::Generators::Base
4
+ source_root File.expand_path("../templates", __FILE__)
5
+
6
+ def generate_routes
7
+ route "get '/trust_pay/notification' => 'trust_pay#notification', as: :trust_pay_notification"
8
+ route "get '/trust_pay/return' => 'trust_pay#return', as: :trust_pay_return"
9
+ route "get '/trust_pay/cancel' => 'trust_pay#cancel', as: :trust_pay_cancel"
10
+ route "get '/trust_pay/error' => 'trust_pay#error', as: :trust_pay_error"
11
+ end
12
+
13
+ def generate_controller
14
+ copy_file "trust_pay_controller.rb", "app/controllers/trust_pay_controller.rb"
15
+ end
16
+ end
17
+ end
18
+ end
19
+
@@ -0,0 +1,33 @@
1
+ class TrustPayController < ApplicationController
2
+
3
+ def notification
4
+ if TrustPayRails::Signature.signature_match?(trust_pay_params)
5
+ # TODO: store payment
6
+ head :ok
7
+ else
8
+ head :unprocessable_entity
9
+ end
10
+ end
11
+
12
+ def return
13
+ # TODO: process user return
14
+
15
+ redirect_to root_url, notice: [t('.flash_message')]
16
+ end
17
+
18
+ def cancel
19
+ redirect_to root_url, notice: [t('.flash_message')]
20
+ end
21
+
22
+ def error
23
+ # TODO: process error
24
+
25
+ redirect_to root_url, notice: [t('.flash_message')]
26
+ end
27
+
28
+ private
29
+
30
+ def trust_pay_params
31
+ @trust_pay_params ||= Hash[params.map{|k, v| [k.downcase.to_sym, v]}]
32
+ end
33
+ end
@@ -0,0 +1,9 @@
1
+ require 'trust_pay_rails/view_helpers'
2
+
3
+ module TrustPayRails
4
+ class Railtie < Rails::Railtie
5
+ initializer "trust_pay_rails.view_helpers" do
6
+ ActionView::Base.send :include, ViewHelpers
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,38 @@
1
+ require 'openssl'
2
+ require 'active_support/core_ext/hash/slice'
3
+
4
+ module TrustPayRails
5
+ class Signature
6
+ def self.sign(data={})
7
+ new(TrustPayRails.key).sign(data)
8
+ end
9
+
10
+ def self.signature_match?(data)
11
+ new(TrustPayRails.key).signature_match?(data)
12
+ end
13
+
14
+ def initialize(key)
15
+ @key = key
16
+ end
17
+
18
+ # FIXME: use ruby 2.1 required arguments when we switch
19
+ def sign(data={})
20
+ hmac_sha_256(@key, data.slice(:aid, :typ, :amt, :cur, :ref, :res, :tid, :oid, :tss).values.join).upcase
21
+ end
22
+
23
+ def signature_match?(data={})
24
+ sign(data) == data[:sig]
25
+ end
26
+
27
+ private
28
+
29
+ def hmac_sha_256(key, data)
30
+ unpack_hex_string(OpenSSL::HMAC.digest(OpenSSL::Digest.new('sha256'), key, data))
31
+ end
32
+
33
+ def unpack_hex_string(data)
34
+ data.unpack("H*").first
35
+ end
36
+ end
37
+ end
38
+
@@ -0,0 +1,3 @@
1
+ module TrustPayRails
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,34 @@
1
+ module TrustPayRails
2
+ module ViewHelpers
3
+
4
+ def trust_pay_form(attributes={})
5
+ form_tag TrustPayRails.bank_transfer_url do
6
+ form_elements = trust_pay_form_elements(attributes)
7
+ form_elements << submit_tag('Pay with bank transfer')
8
+
9
+ form_elements.join.html_safe
10
+ end
11
+ end
12
+
13
+ def trust_pay_card_form(attributes={})
14
+ form_tag TrustPayRails.card_transfer_url do
15
+ form_elements = trust_pay_form_elements(attributes)
16
+ form_elements << submit_tag('Pay with card')
17
+
18
+ form_elements.join.html_safe
19
+ end
20
+ end
21
+
22
+ private
23
+
24
+ def trust_pay_form_elements(attributes={})
25
+ form_elements = attributes.merge!(aid: TrustPayRails.aid).map do |name, value|
26
+ hidden_field_tag name, value
27
+ end
28
+ form_elements << hidden_field_tag(:sig, TrustPayRails::Signature.sign(attributes))
29
+
30
+ form_elements
31
+ end
32
+
33
+ end
34
+ end
@@ -0,0 +1,37 @@
1
+ require "trust_pay_rails/version"
2
+ require "trust_pay_rails/signature"
3
+
4
+ require 'trust_pay_rails/railtie' if defined?(Rails)
5
+
6
+ module TrustPayRails
7
+ @environment = (ENV['TRUSTPAY_ENV'] || :testing).to_sym
8
+ @aid = ENV['TRUSTPAY_AID']
9
+ @key = ENV['TRUSTPAY_KEY']
10
+ class << self
11
+ attr_accessor :environment, :fake_bank_url, :fake_card_url, :aid, :key
12
+ end
13
+
14
+ def self.bank_transfer_url
15
+ case @environment
16
+ when :testing
17
+ 'https://test.trustpay.eu/mapi/pay.aspx'
18
+ when :production
19
+ 'https://ib.trustpay.eu/mapi/pay.aspx'
20
+ when :fake
21
+ @fake_bank_url
22
+ end
23
+ end
24
+
25
+ def self.card_transfer_url
26
+ case @environment
27
+ when :testing
28
+ # this won't actually work because trustpay does not offec car payment
29
+ # testing.
30
+ 'https://test.trustpay.eu/mapi/cardpayments.aspx'
31
+ when :production
32
+ 'https://ib.trustpay.eu/mapi/cardpayments.aspx'
33
+ when :fake
34
+ @fake_card_url
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'trust_pay_rails/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "trust_pay_rails"
8
+ spec.version = TrustPayRails::VERSION
9
+ spec.authors = ["Michal Olah"]
10
+ spec.email = ["olahmichal@gmail.com"]
11
+
12
+ spec.summary = %q{A Ruby client to the TrustPay payment platform.}
13
+ spec.homepage = "https://github.com/Learn2Codesk/trust_pay_rails"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.8"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec"
24
+ spec.add_development_dependency "rails"
25
+
26
+ spec.add_dependency 'activesupport'
27
+ end
metadata ADDED
@@ -0,0 +1,132 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: trust_pay_rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Michal Olah
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-04-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.8'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.8'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rails
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: activesupport
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description:
84
+ email:
85
+ - olahmichal@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - ".travis.yml"
93
+ - CODE_OF_CONDUCT.md
94
+ - Gemfile
95
+ - LICENSE.txt
96
+ - README.md
97
+ - Rakefile
98
+ - bin/console
99
+ - bin/setup
100
+ - lib/generators/trust_pay_rails/routes_generator.rb
101
+ - lib/generators/trust_pay_rails/templates/trust_pay_controller.rb
102
+ - lib/trust_pay_rails.rb
103
+ - lib/trust_pay_rails/railtie.rb
104
+ - lib/trust_pay_rails/signature.rb
105
+ - lib/trust_pay_rails/version.rb
106
+ - lib/trust_pay_rails/view_helpers.rb
107
+ - trust_pay_rails.gemspec
108
+ homepage: https://github.com/Learn2Codesk/trust_pay_rails
109
+ licenses:
110
+ - MIT
111
+ metadata: {}
112
+ post_install_message:
113
+ rdoc_options: []
114
+ require_paths:
115
+ - lib
116
+ required_ruby_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ required_rubygems_version: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ requirements: []
127
+ rubyforge_project:
128
+ rubygems_version: 2.4.5
129
+ signing_key:
130
+ specification_version: 4
131
+ summary: A Ruby client to the TrustPay payment platform.
132
+ test_files: []