paymob_ruby 0.1.1 → 0.1.3

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 568a679887032b299bbf4111d8591f0bdc0a04914afc873ad6032c67ffa06d72
4
- data.tar.gz: e22125d83676040a2efc5b4c72ae00ed3108177201500b051d0b0e9123bd608c
3
+ metadata.gz: 24822d16f0b04315481dd8af2d517107854013214adf5132f2fc94b3dcd681c1
4
+ data.tar.gz: cad7e761eadcda83ff6decd31471d8b03f9a373b5d97c236c260ecbcce5649e6
5
5
  SHA512:
6
- metadata.gz: dd04ecd384ee403c933e1d954c895cb164b43f6704a693284a161230d0a4effc3592b28dbbc380a3d88d26ebfbc436e4af637d27bf4f027d795e9799a61d82fd
7
- data.tar.gz: 5ece42acdb5b91d10cb9da1bae86de4adf104d178200cce7916fe87448e6f25aeff4fe0bc88365d93691cf4c56459812bf88bb690f0978e3f654456c3593fe4e
6
+ metadata.gz: 77b525f1ed5b776d363a912f456eb8b38e7de4b34c6ec34ef8b4b226f8c6a9082b04a683e3e21a9bf1361e699f298b60d7b6eec080d78dc3ddb45ef741490ae3
7
+ data.tar.gz: 566567124158c1af278e874fae4056e2f15285e57264c2b73cf995542a6b94e0fc498143fc166aa00c528434ee1af9ec125035868b5f3f1f91054efa554ede7a
data/Gemfile.lock CHANGED
@@ -1,8 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- paymob_accept_ruby (0.1.0)
5
- activesupport (~> 7.0)
4
+ paymob_ruby (0.1.2)
6
5
  faraday (~> 2.7.5)
7
6
 
8
7
  GEM
@@ -74,6 +73,8 @@ GEM
74
73
  i18n (1.14.1)
75
74
  concurrent-ruby (~> 1.0)
76
75
  json (2.6.3)
76
+ json-schema (4.1.1)
77
+ addressable (>= 2.8)
77
78
  listen (3.8.0)
78
79
  rb-fsevent (~> 0.10, >= 0.10.3)
79
80
  rb-inotify (~> 0.9, >= 0.9.10)
@@ -180,7 +181,8 @@ DEPENDENCIES
180
181
  fuubar (~> 2.5.1)
181
182
  generator_spec
182
183
  guard-rspec
183
- paymob_accept_ruby!
184
+ json-schema
185
+ paymob_ruby!
184
186
  rake (~> 13.0)
185
187
  rspec (~> 3.0)
186
188
  rubocop (~> 1.52.0)
data/README.md CHANGED
@@ -18,12 +18,61 @@ Next, you need to run the generator:
18
18
 
19
19
  ## Usage
20
20
 
21
+ ### Configuration
22
+
23
+ Configure the gem with your configuration
24
+
21
25
  ```ruby
22
26
  PaymobRuby.configure do |config|
23
27
  config.api_key = "api_key"
28
+ config.hmac_key = "hmac_key"
24
29
  end
25
30
  ```
26
31
 
32
+ :bulb: You can get your API_KEY from Settings -> Account info -> API Key in your Paymob portal.
33
+
34
+ For reference on the internals & specifics of Paymob, please head to their official documentation [here](https://docs.paymob.com/)
35
+
36
+ Any API call will return an object with following methods:
37
+
38
+ ```ruby
39
+ result = PaymobRuby.doSomething
40
+ result.success?
41
+ result.failure?
42
+ result.payload
43
+ result.error
44
+ ```
45
+
46
+ ## Creating a payment:
47
+
48
+ ### Using iframe:
49
+
50
+ ```ruby
51
+ user = { first_name: "John", last_name: "Doe", email: "johndoe@test.com", phone_number: "012xxxxxxxxx" }
52
+ result = PaymobRuby::PayCard.call(user:, amount: 10, integration_id: 12345678, iframe_id: 123)
53
+ ```
54
+
55
+ ### Using motto:
56
+
57
+ ```ruby
58
+ user = { first_name: "John", last_name: "Doe", email: "johndoe@test.com", phone_number: "012xxxxxxxxx" }
59
+ result = PaymobRuby::PayToken.call(user:, amount: 10, integration_id: 12345678, token: "abc123")
60
+ ```
61
+
62
+ ### Calculating hash
63
+ ```ruby
64
+ PaymobRuby::Hmac.valid_signature?(paymob_response)
65
+ ```
66
+
67
+ ## Errors:
68
+ Errors could be one of the following:
69
+
70
+ ```ruby
71
+ AuthenticationError
72
+ InvalidRequestError (With `param` attribute)
73
+ APIError
74
+ ```
75
+
27
76
  ## Contributing
28
77
 
29
78
  Bug reports and pull requests are welcome.
data/Rakefile CHANGED
@@ -1,5 +1,3 @@
1
- # frozen_string_literal: true
2
-
3
1
  require "bundler/gem_tasks"
4
2
 
5
3
  require "rake"
@@ -2,4 +2,5 @@ require "paymob_ruby"
2
2
 
3
3
  PaymobRuby.configure do |config|
4
4
  config.api_key = "your_api_key"
5
+ config.hmac_key = "your_hmac_key"
5
6
  end
@@ -7,6 +7,7 @@ module PaymobRuby
7
7
  @integration_id = integration_id
8
8
  @commission_fees = commission_fees
9
9
 
10
+ user_valid?
10
11
  sanity_checks!
11
12
 
12
13
  # Paymob order id
@@ -0,0 +1,5 @@
1
+ module PaymobRuby
2
+ class Configuration
3
+ attr_accessor :api_key, :hmac_key
4
+ end
5
+ end
@@ -22,6 +22,9 @@ module PaymobRuby
22
22
  end
23
23
  end
24
24
 
25
+ # ConfigurationMissingError is raised when a configuration is missing
26
+ class ConfigurationMissingError < StandardError; end
27
+
25
28
  # AuthenticationError is raised when invalid credentials are used to connect
26
29
  # to Paymob's servers.
27
30
  class AuthenticationError < PaymobError
@@ -61,18 +64,4 @@ module PaymobRuby
61
64
  @sig_header = sig_header
62
65
  end
63
66
  end
64
-
65
- module OAuth
66
- # OAuthError is raised when the OAuth API returns an error.
67
- class OAuthError < PaymobError
68
- def initialize(description, http_status: nil, http_body: nil)
69
- super(description, http_status:, http_body:)
70
- end
71
- end
72
-
73
- # InvalidRequestError is raised when a code, refresh token, or grant type
74
- # parameter is not provided, but was required.
75
- class InvalidRequestError < OAuthError
76
- end
77
- end
78
67
  end
@@ -0,0 +1,20 @@
1
+ module PaymobRuby
2
+ module Hmac
3
+ FILTERED_TRANSACTION_KEYS = %w[amount_cents created_at currency error_occured has_parent_transaction id
4
+ integration_id is_3d_secure is_auth is_capture is_refunded is_standalone_payment
5
+ is_voided order.id owner pending
6
+ source_data.pansource_data.sub_type source_data.type success].freeze
7
+
8
+ class << self
9
+ def valid_signature?(paymob_response)
10
+ digest = ::OpenSSL::Digest.new("sha512")
11
+
12
+ concatenated_str = FILTERED_TRANSACTION_KEYS.map do |element|
13
+ paymob_response.dig("obj", *element.split("."))
14
+ end.join
15
+ secure_hash = ::OpenSSL::HMAC.hexdigest(digest, PaymobRuby.hmac_key, concatenated_str)
16
+ secure_hash == paymob_response["hmac"]
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,29 @@
1
+ module PaymobRuby
2
+ module JsonSchemas
3
+ module User
4
+ extend ActiveSupport::Concern
5
+
6
+ def user_valid?
7
+ JSON::Validator.validate!(customer_schema, @user)
8
+ rescue JSON::Schema::ValidationError => e
9
+ raise InvalidRequestError.new("Customer hash has the following error: #{e.message}", :user)
10
+ end
11
+
12
+ private
13
+
14
+ def customer_schema
15
+ {
16
+ type: "object",
17
+ "$schema": "http://json-schema.org/draft-04/schema",
18
+ properties: {
19
+ first_name: { type: "string" },
20
+ last_name: { type: "string" },
21
+ email: { type: "string" },
22
+ phone_number: { type: "string" }
23
+ },
24
+ required: %w[first_name last_name email phone_number]
25
+ }
26
+ end
27
+ end
28
+ end
29
+ end
@@ -1,5 +1,3 @@
1
- # frozen_string_literal: true
2
-
3
1
  module PaymobRuby
4
- VERSION = "0.1.1"
2
+ VERSION = "0.1.3".freeze
5
3
  end
data/lib/paymob_ruby.rb CHANGED
@@ -1,28 +1,35 @@
1
- # frozen_string_literal: true
2
-
3
- require "faraday"
4
1
  require "active_support"
2
+ require "faraday"
3
+ require "json-schema"
5
4
 
6
5
  require_relative "paymob_ruby/version"
7
6
 
8
7
  # API resource support classes
9
8
  require "paymob_ruby/errors"
9
+ require "paymob_ruby/configuration"
10
+ require "paymob_ruby/hmac"
10
11
 
11
12
  # API operations
12
- require "paymob_ruby/actions/application_service"
13
- require "paymob_ruby/actions/login"
14
- require "paymob_ruby/actions/payment_token"
15
- require "paymob_ruby/actions/pay_card"
16
- require "paymob_ruby/actions/pay_token"
13
+ require "paymob_ruby/api/application_service"
14
+ require "paymob_ruby/api/login"
15
+ require "paymob_ruby/api/payment_token"
16
+ require "paymob_ruby/api/pay_card"
17
+ require "paymob_ruby/api/pay_token"
18
+
19
+ require "paymob_ruby/json_schemas/user"
17
20
 
18
21
  module PaymobRuby
19
- BASE_URI = "https://accept.paymobsolutions.com/api"
22
+ BASE_URI = "https://accept.paymobsolutions.com/api".freeze
20
23
 
21
24
  class << self
22
- attr_accessor :api_key
23
- end
25
+ attr_accessor :api_key, :hmac_key
26
+
27
+ def configure
28
+ yield configuration
29
+ end
24
30
 
25
- def self.configure
26
- yield self
31
+ def configuration
32
+ @configuration ||= Configuration.new
33
+ end
27
34
  end
28
35
  end
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: paymob_ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joe Kaldas
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-09-18 00:00:00.000000000 Z
11
+ date: 2023-09-19 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '7.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '7.0'
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: faraday
15
29
  requirement: !ruby/object:Gem::Requirement
@@ -24,6 +38,20 @@ dependencies:
24
38
  - - "~>"
25
39
  - !ruby/object:Gem::Version
26
40
  version: 2.7.5
41
+ - !ruby/object:Gem::Dependency
42
+ name: json-schema
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 4.1.1
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 4.1.1
27
55
  description:
28
56
  email:
29
57
  - joekaldas8@gmail.com
@@ -42,16 +70,18 @@ files:
42
70
  - LICENSE.txt
43
71
  - README.md
44
72
  - Rakefile
45
- - config/initializers/paymob.rb
46
73
  - lib/generators/paymob_ruby/install_generator.rb
47
74
  - lib/generators/paymob_ruby/templates/paymob.rb
48
75
  - lib/paymob_ruby.rb
49
- - lib/paymob_ruby/actions/application_service.rb
50
- - lib/paymob_ruby/actions/login.rb
51
- - lib/paymob_ruby/actions/pay_card.rb
52
- - lib/paymob_ruby/actions/pay_token.rb
53
- - lib/paymob_ruby/actions/payment_token.rb
76
+ - lib/paymob_ruby/api/application_service.rb
77
+ - lib/paymob_ruby/api/login.rb
78
+ - lib/paymob_ruby/api/pay_card.rb
79
+ - lib/paymob_ruby/api/pay_token.rb
80
+ - lib/paymob_ruby/api/payment_token.rb
81
+ - lib/paymob_ruby/configuration.rb
54
82
  - lib/paymob_ruby/errors.rb
83
+ - lib/paymob_ruby/hmac.rb
84
+ - lib/paymob_ruby/json_schemas/user.rb
55
85
  - lib/paymob_ruby/version.rb
56
86
  - sig/paymob.rbs
57
87
  homepage: https://github.com/JoeKaldas/paymob
@@ -1,5 +0,0 @@
1
- require "paymob_ruby"
2
-
3
- PaymobRuby.configure do |config|
4
- config.api_key = "your_api_key"
5
- end
File without changes
File without changes
File without changes