mobilepay 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ab2ecb74f00ea4ee578b918b4cf847729883292b
4
- data.tar.gz: 78c3295ec71aa6a5e3d38eda4e6098e1e10cb173
3
+ metadata.gz: 311d54f9494a6c32c4044c15826ba890985de747
4
+ data.tar.gz: 55f283e80713e74ca66dfcf91f32271db96b8556
5
5
  SHA512:
6
- metadata.gz: 2a1c19eb27c62f2d2eaa21a452d5da47686b417acbf33ed6d7302733d847934860fc6706216b7cfb2cf52ce9222391d63f6a729d844b9d0db4f823a37e437509
7
- data.tar.gz: a6be43d4de495cb1397ed951353a4044ca6006e907edbe3a16ac8b86b8c6ee776b78d0da3937e042df2cf551e469a40d5c15b82d8c5863a7e0593a7fe4fc2793
6
+ metadata.gz: 6cf5c513bd53f0a60180e9f064e23b339f6085421b3060b8c22dc87bfcf4510ad7852621052ec0952aaf42c3429e3cc5498889105837a652dc3804c614041ddf
7
+ data.tar.gz: 53beb0bc111da0df9b300ea755944dfe47792f0405a94c0ef2a2839ca25fa1a2b41c120a2c7b68b9ff0404c60797db2964121d2d2de6f066941b53bd516ea0e5
data/README.md CHANGED
@@ -32,10 +32,12 @@ Or install it yourself as:
32
32
 
33
33
  ```ruby
34
34
  require 'mobilepay'
35
- client = Mobilepay::Client.new merchant_id: 'merchant_id', subscription_key: 'subscription_key'
35
+ require 'jose'
36
+ client = Mobilepay::Client.new merchant_id: 'merchant_id', subscription_key: 'subscription_key', privatekey: OpenSSL::PKey::RSA.new(File.read('key.pvk'))
36
37
  ```
37
38
  subscription_key - Subscription Key for MobilePay, required
38
39
  merchant_id - Merchant ID, required
40
+ privatekey - Secret key for creating signature for requests, required
39
41
 
40
42
  ### Public Key
41
43
 
data/lib/mobilepay.rb CHANGED
@@ -3,4 +3,5 @@ require_relative 'mobilepay/client'
3
3
  require_relative 'mobilepay/security'
4
4
 
5
5
  module Mobilepay
6
+ class Failure < StandardError; end
6
7
  end
@@ -6,6 +6,7 @@ require_relative 'client/refund_amount'
6
6
  require_relative 'client/capture_amount'
7
7
  require_relative 'client/cancel_reservation'
8
8
  require_relative 'requests'
9
+ require_relative 'requests/generate_signature'
9
10
 
10
11
  module Mobilepay
11
12
  class Client
@@ -16,14 +17,14 @@ module Mobilepay
16
17
  include Mobilepay::Client::CaptureAmount
17
18
  include Mobilepay::Client::CancelReservation
18
19
  include Mobilepay::Requests
20
+ include Mobilepay::Requests::GenerateSignature
19
21
 
20
- class MobilePayFailure < StandardError; end
21
-
22
- attr_reader :merchant_id, :subscription_key, :base_uri
22
+ attr_reader :merchant_id, :subscription_key, :privatekey, :base_uri
23
23
 
24
24
  def initialize(args = {})
25
25
  @merchant_id = args[:merchant_id] || ''
26
26
  @subscription_key = args[:subscription_key] || ''
27
+ @privatekey = args[:privatekey]
27
28
  @base_uri = 'https://api.mobeco.dk/appswitch/api/v1'
28
29
  end
29
30
 
@@ -32,7 +33,7 @@ module Mobilepay
32
33
  def call(req, address, args = {})
33
34
  response = case req
34
35
  when :get, :put, :delete then http_request(req, address, args)
35
- else raise MobilePayFailure, 'Undefined type for call'
36
+ else raise Failure, 'Undefined type for call'
36
37
  end
37
38
  check_response(response)
38
39
  response
@@ -40,14 +41,15 @@ module Mobilepay
40
41
 
41
42
  def check_response(response)
42
43
  if response.code != '200'
43
- raise MobilePayFailure, JSON.parse(response.body)['message']
44
+ error_message = response.body.empty? ? response.code : JSON.parse(response.body)['message']
45
+ raise Failure, error_message
44
46
  end
45
47
  end
46
48
 
47
49
  def check_args(args)
48
50
  args.each do |arg_name, value|
49
51
  if value.nil? || !value.is_a?(String)
50
- raise MobilePayFailure, "Invalid argument '#{arg_name}', must be string"
52
+ raise Failure, "Invalid argument '#{arg_name}', must be string"
51
53
  end
52
54
  end
53
55
  end
@@ -6,9 +6,9 @@ module Mobilepay
6
6
  # Cancels a specific reservation
7
7
  def cancel_reservation(args = {})
8
8
  check_args(order_id: args[:order_id])
9
- response = call(:delete, "/reservations/merchants/#{merchant_id}/orders/#{args[:order_id]}", { body: '{}' })
9
+ response = call(:delete, "/reservations/merchants/#{merchant_id}/orders/#{args[:order_id]}", { body: '' })
10
10
  JSON.parse(response.body)
11
- rescue MobilePayFailure => ex
11
+ rescue Failure => ex
12
12
  return { error: ex.message }
13
13
  end
14
14
 
@@ -6,9 +6,9 @@ module Mobilepay
6
6
  # Captures a previously reserved amount, either in full or partially
7
7
  def capture_amount(args = {})
8
8
  check_args(order_id: args[:order_id])
9
- response = call(:put, "/reservations/merchants/#{merchant_id}/orders/#{args[:order_id]}", { body: args[:body] || '{}' })
9
+ response = call(:put, "/reservations/merchants/#{merchant_id}/orders/#{args[:order_id]}", { body: args[:body] || '' })
10
10
  JSON.parse(response.body)
11
- rescue MobilePayFailure => ex
11
+ rescue Failure => ex
12
12
  return { error: ex.message }
13
13
  end
14
14
 
@@ -6,9 +6,9 @@ module Mobilepay
6
6
  # Gets the status of a given order
7
7
  def payment_status(args = {})
8
8
  check_args(order_id: args[:order_id])
9
- response = call(:get, "/merchants/#{merchant_id}/orders/#{args[:order_id]}", { body: '{}' })
9
+ response = call(:get, "/merchants/#{merchant_id}/orders/#{args[:order_id]}", { body: '' })
10
10
  JSON.parse(response.body)
11
- rescue MobilePayFailure => ex
11
+ rescue Failure => ex
12
12
  return { error: ex.message }
13
13
  end
14
14
 
@@ -6,9 +6,9 @@ module Mobilepay
6
6
  # Gets the transactions for a given order
7
7
  def payment_transactions(args = {})
8
8
  check_args(order_id: args[:order_id])
9
- response = call(:get, "/merchants/#{merchant_id}/orders/#{args[:order_id]}/transactions", { body: '{}' })
9
+ response = call(:get, "/merchants/#{merchant_id}/orders/#{args[:order_id]}/transactions", { body: '' })
10
10
  JSON.parse(response.body)
11
- rescue MobilePayFailure => ex
11
+ rescue Failure => ex
12
12
  return { error: ex.message }
13
13
  end
14
14
 
@@ -8,7 +8,7 @@ module Mobilepay
8
8
  check_args(order_id: args[:order_id])
9
9
  response = call(:put, "/merchants/#{merchant_id}/orders/#{args[:order_id]}", { body: args[:body] || '{}' })
10
10
  JSON.parse(response.body)
11
- rescue MobilePayFailure => ex
11
+ rescue Failure => ex
12
12
  return { error: ex.message }
13
13
  end
14
14
 
@@ -10,7 +10,7 @@ module Mobilepay
10
10
  address += "?customerId=#{args[:customer_id]}" if args[:customer_id]
11
11
  response = call(:get, address, { body: '{}' })
12
12
  JSON.parse(response.body)
13
- rescue MobilePayFailure => ex
13
+ rescue Failure => ex
14
14
  return { error: ex.message }
15
15
  end
16
16
 
@@ -27,6 +27,7 @@ module Mobilepay
27
27
  def generate_headers(req, body)
28
28
  req['Content-Type'] = 'application/json'
29
29
  req['Ocp-Apim-Subscription-Key'] = subscription_key
30
+ req['AuthenticationSignature'] = generate_signature(req) unless privatekey.nil?
30
31
  req.body = body
31
32
  req
32
33
  end
@@ -0,0 +1,22 @@
1
+ require 'digest/sha1'
2
+ require 'base64'
3
+ require 'jose'
4
+
5
+ module Mobilepay
6
+ module Requests
7
+ module GenerateSignature
8
+
9
+ # Generate Authentication Signature
10
+ def generate_signature(request)
11
+ payload = (request.uri.to_s + request.body.to_s).force_encoding('UTF-8')
12
+ payload_sha1 = Digest::SHA1.hexdigest(payload)
13
+ payload_base64 = Base64.encode64(payload_sha1)
14
+
15
+ jwk_rs256 = JOSE::JWK.generate_key([:rsa, 1024])
16
+ jwk_rs256.kty.key = privatekey
17
+ JOSE::JWS.sign(jwk_rs256, payload_base64, { "alg" => "RS256" }).compact
18
+ end
19
+
20
+ end
21
+ end
22
+ end
@@ -1,18 +1,19 @@
1
1
  require 'json'
2
2
  require_relative 'security/public_key'
3
3
  require_relative 'requests'
4
+ require_relative 'requests/generate_signature'
4
5
 
5
6
  module Mobilepay
6
7
  class Security
7
8
  include Mobilepay::Security::PublicKey
8
9
  include Mobilepay::Requests
10
+ include Mobilepay::Requests::GenerateSignature
9
11
 
10
- class SecurityFailure < StandardError; end
11
-
12
- attr_reader :subscription_key, :base_uri
12
+ attr_reader :subscription_key, :privatekey, :base_uri
13
13
 
14
14
  def initialize(args = {})
15
15
  @subscription_key = args[:subscription_key] || ''
16
+ @privatekey = nil
16
17
  @base_uri = 'https://api.mobeco.dk/merchantsecurity/api'
17
18
  end
18
19
 
@@ -26,7 +27,8 @@ module Mobilepay
26
27
 
27
28
  def check_response(response)
28
29
  if response.code != '200'
29
- raise SecurityFailure, JSON.parse(response.body)['message']
30
+ error_message = response.body.empty? ? response.code : JSON.parse(response.body)['message']
31
+ raise Failure, error_message
30
32
  end
31
33
  end
32
34
  end
@@ -7,7 +7,7 @@ module Mobilepay
7
7
  def public_key
8
8
  response = call
9
9
  JSON.parse(response.body)
10
- rescue SecurityFailure => ex
10
+ rescue Failure => ex
11
11
  return { error: ex.message }
12
12
  end
13
13
 
@@ -1,3 +1,3 @@
1
1
  module Mobilepay
2
- VERSION = '1.0.0'
2
+ VERSION = '1.1.0'
3
3
  end
data/mobilepay.gemspec CHANGED
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
10
10
  spec.email = ['kortirso@gmail.com']
11
11
 
12
12
  spec.summary = 'Gem for interaction with MobilePay API'
13
- spec.description = 'Gem for interaction with MobilePay API'
13
+ spec.description = 'Actions with payments in MobilePay system'
14
14
  spec.homepage = 'https://github.com/kortirso/mobilepay'
15
15
  spec.license = 'MIT'
16
16
 
@@ -23,5 +23,5 @@ Gem::Specification.new do |spec|
23
23
 
24
24
  spec.add_development_dependency 'bundler', '~> 1.14'
25
25
  spec.add_development_dependency 'rake', '~> 10.0'
26
- spec.add_development_dependency 'rspec', '~> 3.0'
26
+ spec.add_development_dependency 'jose', '~> 1.1'
27
27
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mobilepay
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Anton Bogdanov
@@ -39,20 +39,20 @@ dependencies:
39
39
  - !ruby/object:Gem::Version
40
40
  version: '10.0'
41
41
  - !ruby/object:Gem::Dependency
42
- name: rspec
42
+ name: jose
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '3.0'
47
+ version: '1.1'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: '3.0'
55
- description: Gem for interaction with MobilePay API
54
+ version: '1.1'
55
+ description: Actions with payments in MobilePay system
56
56
  email:
57
57
  - kortirso@gmail.com
58
58
  executables: []
@@ -77,6 +77,7 @@ files:
77
77
  - lib/mobilepay/client/refund_amount.rb
78
78
  - lib/mobilepay/client/reservations.rb
79
79
  - lib/mobilepay/requests.rb
80
+ - lib/mobilepay/requests/generate_signature.rb
80
81
  - lib/mobilepay/security.rb
81
82
  - lib/mobilepay/security/public_key.rb
82
83
  - lib/mobilepay/version.rb