adyen_hpp_hmac_calculator 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2b1be53b37437d64f8bc5ec0f8db578c8d29fb57
4
+ data.tar.gz: 50e9d526eab72dadcae12d8306f32fa3b8c472cd
5
+ SHA512:
6
+ metadata.gz: 70f5dd042cfa42531e944f3f426dcfd635f66d706c0ef9bb9f8745b8019a8020f6630695e6f82c0a9bedc1b7771139a0459158b2bc86d50f8f1d37e2f273eb7c
7
+ data.tar.gz: 31aea3e4151af05e585a5384a491290035c19b99d1251aed531bf8f9ef2223fe4847abfee927f4a1fac7c087893641c38cf6337ad0a80a7796f8c2384c80e416
@@ -0,0 +1,61 @@
1
+ require 'base64'
2
+
3
+ # Calculates merchant signature for Adyen payment serivce (http://www.adyen.com).
4
+ # It is based on documentation from:
5
+ # https://docs.adyen.com/pages/viewpage.action?pageId=5376964
6
+ #
7
+ # @example Calculating merchant signature using AdyenHppHmacCalculator::calculate:
8
+ # hmac_key = "4468D9782DEF54FCD706C9100C71EC43932B1EBC2ACF6BA0560C05AAA7550C48"
9
+ # params = {
10
+ # "merchantAccount" => "TestMerchant",
11
+ # "currencyCode" => "EUR",
12
+ # "paymentAmount" => "199",
13
+ # "sessionValidity" => "2015-06-25T10:31:06Z",
14
+ # "shipBeforeDate" => "2015-07-01",
15
+ # "shopperLocale" => "en_GB",
16
+ # "merchantReference" => "SKINTEST-1435226439255",
17
+ # "skinCode" => "X7hsNDWp"
18
+ # }
19
+ # AdyenHppHmacCalculator.calculate(hamc_key, params) #=> "GJ1asjR5VmkvihDJxCd8yE2DGYOKwWwJCBiV3R51NFg="
20
+ class AdyenHppHmacCalculator
21
+ def initialize hmac_key
22
+ @hmac_key = hmac_key.clone.freeze
23
+ end
24
+
25
+ # Calculates merchant signature for specified params using hmac key specified on initialization
26
+ #
27
+ # @param params [Hash] key/value pairs to calculate merchant signature
28
+ # @return [String] HMAC SHA-256 Base64 signature encoded
29
+ def calculate params
30
+ merchant_string = generate_merchant_string params
31
+ hmac = hmac_calculator.calculate merchant_string
32
+ base64_encode hmac
33
+ end
34
+
35
+ # Calculates merchant signatre for specified hmac key and params
36
+ # https://docs.adyen.com/pages/viewpage.action?pageId=5376964
37
+ #
38
+ # @param hmac_key [String] key for HMAC algorithm
39
+ # @param params [Hash] key/value pairs to calculate merchant signature
40
+ # @return [String] HMAC SHA-256 Base64 signature encoded
41
+ def self.calculate hmac_key, params
42
+ self.new(hmac_key).calculate params
43
+ end
44
+
45
+ private
46
+
47
+ def generate_merchant_string params
48
+ MerchantStringGenerator.generate params
49
+ end
50
+
51
+ def hmac_calculator
52
+ @hmac_calculator ||= HmacCalculator.new @hmac_key
53
+ end
54
+
55
+ def base64_encode string
56
+ Base64.strict_encode64 string
57
+ end
58
+ end
59
+
60
+ require 'adyen_hpp_hmac_calculator/merchant_string_generator'
61
+ require 'adyen_hpp_hmac_calculator/hmac_calculator'
@@ -0,0 +1,23 @@
1
+ require 'openssl'
2
+
3
+ class AdyenHppHmacCalculator::HmacCalculator
4
+ DIGEST = OpenSSL::Digest.new('sha256')
5
+
6
+ def initialize hmac_key
7
+ @hmac_key_hex = string_to_hex hmac_key
8
+ end
9
+
10
+ # Calculates HMAC SHA-256 from string
11
+ #
12
+ # @param string [String] string to calculate HMAC SHA-256
13
+ # @return [String] HMAC SHA-256 string
14
+ def calculate string
15
+ OpenSSL::HMAC.digest(DIGEST, @hmac_key_hex, string)
16
+ end
17
+
18
+ private
19
+
20
+ def string_to_hex string
21
+ Array(string).pack('H*')
22
+ end
23
+ end
@@ -0,0 +1,49 @@
1
+ module AdyenHppHmacCalculator::MerchantStringGenerator
2
+ # Generates concatenated key/value paris
3
+ #
4
+ # @param params [Hash] key/value pairs to generate the signing string
5
+ # @return [String] concatenated key/value pairs
6
+ def self.generate params
7
+ pairs = create_pairs params
8
+ remove_unnecessary_keys pairs
9
+ sort_key_value_pairs_by_key pairs
10
+ delimit_pairs pairs
11
+ concatenate_pairs pairs
12
+ end
13
+
14
+ private
15
+
16
+ def self.create_pairs params
17
+ params.map{ |key, value| [key.to_s.dup, value.to_s.dup] }
18
+ end
19
+
20
+ def self.remove_unnecessary_keys pairs
21
+ pairs.reject! { |key, _| ignored_key? key }
22
+ end
23
+
24
+ def self.ignored_key? key
25
+ key == 'sig' or key == 'merchantSig' or key.start_with? 'ignore.'
26
+ end
27
+
28
+ def self.sort_key_value_pairs_by_key pairs
29
+ pairs.sort_by!{ |key, _| key }
30
+ end
31
+
32
+ def self.delimit_pairs pairs
33
+ pairs.each do |key, value|
34
+ escape! key
35
+ escape! value
36
+ end
37
+ end
38
+
39
+ def self.escape! string
40
+ string.gsub!('\\'){ '\\\\' }
41
+ string.gsub!(':'){ '\:' }
42
+ end
43
+
44
+ def self.concatenate_pairs pairs
45
+ keys = pairs.collect(&:first)
46
+ values = pairs.collect(&:last)
47
+ (keys + values).join(':')
48
+ end
49
+ end
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: adyen_hpp_hmac_calculator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Krzysztof Zielonka
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-10-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '10.4'
20
+ - - "~>"
21
+ - !ruby/object:Gem::Version
22
+ version: 10.4.2
23
+ type: :development
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '10.4'
30
+ - - "~>"
31
+ - !ruby/object:Gem::Version
32
+ version: 10.4.2
33
+ - !ruby/object:Gem::Dependency
34
+ name: yard
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: 0.8.7
40
+ - - "~>"
41
+ - !ruby/object:Gem::Version
42
+ version: 0.8.7.6
43
+ type: :development
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - "~>"
48
+ - !ruby/object:Gem::Version
49
+ version: 0.8.7
50
+ - - "~>"
51
+ - !ruby/object:Gem::Version
52
+ version: 0.8.7.6
53
+ description: Merchant signature calculator for Adyen (https://www.adyen.com) payments
54
+ service. It is implementation of HPP HMAC (SHA-256) algorithm from Adyen documentation
55
+ (https://docs.adyen.com/display/TD/HPP+HMAC+calculation).
56
+ email: aknolezik@gmail.com
57
+ executables: []
58
+ extensions: []
59
+ extra_rdoc_files: []
60
+ files:
61
+ - lib/adyen_hpp_hmac_calculator.rb
62
+ - lib/adyen_hpp_hmac_calculator/hmac_calculator.rb
63
+ - lib/adyen_hpp_hmac_calculator/merchant_string_generator.rb
64
+ homepage: https://rubygems.org/gems/adyen_hpp_hmac_calculator
65
+ licenses:
66
+ - MIT
67
+ metadata: {}
68
+ post_install_message:
69
+ rdoc_options: []
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ requirements: []
83
+ rubyforge_project:
84
+ rubygems_version: 2.4.5
85
+ signing_key:
86
+ specification_version: 4
87
+ summary: Adyen HPP HMAC calculator
88
+ test_files: []
89
+ has_rdoc: