paynow_ruby 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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 2a8672ca12aeec21238f090b036f2e12018a2212ba76ec71cbdec2e46726682b
4
+ data.tar.gz: 737dc993c43ccccbfd42e3c438070ced1746e7c8680aee162f5d85f815dc07ad
5
+ SHA512:
6
+ metadata.gz: b2a5089bdb305ce428f6b01ef582da435499d26b65bfd49c9d5cf85f04c872987d3c278ad099035c1af8092073aaac00d6633dbb0e74671100ae69d963810973
7
+ data.tar.gz: 90fe740f43504f0483208ae8246409233023e92769d7c257be63a446d269d5149858a9db45588392389f9fb71e2b5f4052d4584c58fc613d790df5d1a72f295d
data/README.md ADDED
@@ -0,0 +1 @@
1
+ # paynow_ruby
data/lib/paynow.rb ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'paynow/configuration'
4
+ require 'paynow/paynow_client'
5
+ require 'paynow/digest'
6
+ require 'paynow/payment'
7
+ require 'paynow/idempotency_key_builder'
8
+ require 'paynow/request_builder'
9
+ require 'paynow/gateway'
10
+ require 'paynow/model/behaviour'
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Paynow
4
+ # Centralized configuration for the paynow_ruby gem
5
+ class Configuration
6
+ def self.configure
7
+ yield self
8
+ end
9
+
10
+ class << self
11
+ attr_accessor :host, :api_key, :api_version, :signature_key, :user_agent
12
+ end
13
+
14
+ def self.request_builder
15
+ Paynow::RequestBuilder
16
+ end
17
+
18
+ def self.digest
19
+ Paynow::Digest
20
+ end
21
+
22
+ def self.api_client
23
+ Paynow::PaynowClient
24
+ end
25
+
26
+ def self.idempotency_key_builder
27
+ Paynow::IdempotencyKeyBuilder
28
+ end
29
+
30
+ def self.gateway
31
+ Paynow::Gateway
32
+ end
33
+
34
+ def self.payment
35
+ Paynow::Payment
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'openssl'
4
+
5
+ module Paynow
6
+ # Digest of request body for the integrity verification
7
+ class Digest
8
+ attr_reader :data
9
+
10
+ def self.hmac(data)
11
+ new(data).hmac
12
+ end
13
+
14
+ def initialize(data)
15
+ @data = data
16
+ end
17
+
18
+ def hmac
19
+ return unless data
20
+
21
+ digest = OpenSSL::HMAC.digest('SHA256', key, data)
22
+
23
+ Base64.encode64(digest).strip
24
+ end
25
+
26
+ private
27
+
28
+ def key
29
+ Paynow::Configuration.signature_key
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Paynow
4
+ # Resonsible for building a payment from request and response
5
+ class Gateway
6
+ attr_reader :params
7
+
8
+ def self.create_payment(*args)
9
+ new(*args).create_payment
10
+ end
11
+
12
+ def initialize(params)
13
+ @params = params
14
+ end
15
+
16
+ def create_payment
17
+ response = api_client.create_payment(params)
18
+
19
+ parsed_response_body = JSON.parse(response.body)
20
+
21
+ payment.new(
22
+ request_attributes.merge(
23
+ response: response,
24
+ payment_id: parsed_response_body['paymentId'],
25
+ status: parsed_response_body['status'],
26
+ redirect_url: parsed_response_body['redirectUrl']
27
+ )
28
+ )
29
+ end
30
+
31
+ private
32
+
33
+ def request_attributes
34
+ params.slice(:amount, :external_id, :description, :buyer)
35
+ end
36
+
37
+ def api_client
38
+ Paynow::Configuration.api_client
39
+ end
40
+
41
+ def payment
42
+ Paynow::Configuration.payment
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'securerandom'
4
+
5
+ module Paynow
6
+ # Builds uniq key using UUID to be used in the request header
7
+ class IdempotencyKeyBuilder
8
+ def self.build
9
+ new.build
10
+ end
11
+
12
+ def build
13
+ SecureRandom.uuid
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Paynow
4
+ module Model
5
+ # Adds functionaliy needed to represent Paynow payments
6
+ module Behaviour
7
+ def self.included(base)
8
+ base.enum status: %i[created pending confirmed rejected error]
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Paynow
4
+ # Value object with information about the payment
5
+ class Payment
6
+ attr_reader :amount,
7
+ :buyer,
8
+ :description,
9
+ :payment_id,
10
+ :status,
11
+ :redirect_url,
12
+ :response,
13
+ :external_id
14
+
15
+ def self.create(*args)
16
+ Paynow::Configuration.gateway.create_payment(*args)
17
+ end
18
+
19
+ def initialize(attributes)
20
+ @response = attributes[:response]
21
+ @amount = attributes[:amount]
22
+ @description = attributes[:description]
23
+ @buyer = attributes[:buyer]
24
+ @payment_id = attributes[:payment_id]
25
+ @status = attributes[:status]
26
+ @redirect_url = attributes[:redirect_url]
27
+ @external_id = attributes[:external_id]
28
+ end
29
+
30
+ def created?
31
+ response.code == '201' && status == 'NEW'
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,66 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'net/http'
4
+
5
+ module Paynow
6
+ # Net::HTTP wraper for consuming the Paynow API
7
+ class PaynowClient
8
+ attr_reader :body
9
+
10
+ def self.create_payment(body)
11
+ new(body).create_payment
12
+ end
13
+
14
+ def initialize(body)
15
+ @body = body
16
+ end
17
+
18
+ def create_payment
19
+ uri = URI("https://#{host}/v1/payments")
20
+
21
+ Net::HTTP.post(uri, json_body, headers)
22
+ end
23
+
24
+ private
25
+
26
+ def headers
27
+ {
28
+ 'Api-Key': api_key,
29
+ 'Signature': signature,
30
+ 'Idempotency-Key': idempotency_key,
31
+ 'Api-Version': api_version,
32
+ 'Accept': 'application/json',
33
+ 'Content-Type': 'application/json',
34
+ 'User-Agent': user_agent,
35
+ }
36
+ end
37
+
38
+ def json_body
39
+ Paynow::Configuration.request_builder.build(body)
40
+ end
41
+
42
+ def idempotency_key
43
+ Paynow::Configuration.idempotency_key_builder.build
44
+ end
45
+
46
+ def host
47
+ Paynow::Configuration.host
48
+ end
49
+
50
+ def api_key
51
+ Paynow::Configuration.api_key
52
+ end
53
+
54
+ def api_version
55
+ Paynow::Configuration.api_version
56
+ end
57
+
58
+ def signature
59
+ Paynow::Configuration.digest.hmac(json_body)
60
+ end
61
+
62
+ def user_agent
63
+ Paynow::Configuration.user_agent
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'json'
4
+ require 'base64'
5
+
6
+ module Paynow
7
+ # JSON body builder
8
+ class RequestBuilder
9
+ attr_reader :body
10
+
11
+ def self.build(body)
12
+ new(body).build
13
+ end
14
+
15
+ def initialize(body)
16
+ @body = body
17
+ end
18
+
19
+ def build
20
+ body.transform_keys(&camelize_proc).to_json
21
+ end
22
+
23
+ private
24
+
25
+ def camelize_proc
26
+ proc do |key|
27
+ key.to_s.split('_').reduce do |accumulator, value|
28
+ if accumulator
29
+ accumulator + value[0].upcase + value[1..(value.length - 1)]
30
+ else
31
+ value
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: paynow_ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Piotr Wald
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-02-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: pry
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 3.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: 3.10.0
41
+ description:
42
+ email:
43
+ - valdpiotr@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - README.md
49
+ - lib/paynow.rb
50
+ - lib/paynow/configuration.rb
51
+ - lib/paynow/digest.rb
52
+ - lib/paynow/gateway.rb
53
+ - lib/paynow/idempotency_key_builder.rb
54
+ - lib/paynow/model/behaviour.rb
55
+ - lib/paynow/payment.rb
56
+ - lib/paynow/paynow_client.rb
57
+ - lib/paynow/request_builder.rb
58
+ homepage: https://github.com/PiotrWald/paynow_ruby
59
+ licenses:
60
+ - MIT
61
+ metadata: {}
62
+ post_install_message:
63
+ rdoc_options: []
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">"
69
+ - !ruby/object:Gem::Version
70
+ version: '2.4'
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ requirements: []
77
+ rubygems_version: 3.1.4
78
+ signing_key:
79
+ specification_version: 4
80
+ summary: ''
81
+ test_files: []