paymob_ruby 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
+ SHA256:
3
+ metadata.gz: 52b1f6acf063b140de9deb2bc110562cd0697c1b8960cca0a3a1093ad18420ce
4
+ data.tar.gz: 83958c28994ea5488bf23d6c10184139c224a471078811726473899d51662f23
5
+ SHA512:
6
+ metadata.gz: ab7868fa87491d7aa84582798d490c03cd54ac8dd002dbbfc0feb43dafc294282226d05e723ca0cb2a39ebd3c3c2b2376dc06197a746af8353e59bb0f5a49f4d
7
+ data.tar.gz: c2b16ddd7b2597c6a9f9d82b4d01534c257c827fd4fd36855d1d83142af124fb1228c65e177f01650a6526fca1e76fb6a8cd8f5e19ebe439dc195965d7484101
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,102 @@
1
+ inherit_from: .rubocop_todo.yml
2
+
3
+ require:
4
+ - rubocop-rails
5
+ - rubocop-performance
6
+
7
+ AllCops:
8
+ SuggestExtensions: false
9
+ NewCops: enable
10
+ Exclude:
11
+ - 'bin/**/*'
12
+ - 'spec/fixtures/**/*'
13
+ - 'Guardfile'
14
+ TargetRubyVersion: 3.1.2
15
+
16
+ Bundler/OrderedGems:
17
+ Enabled: false
18
+
19
+ Metrics/BlockLength:
20
+ Exclude:
21
+ - 'Rakefile'
22
+ - '**/*.rake'
23
+ - 'spec/**/*.rb'
24
+
25
+ Metrics/ModuleLength:
26
+ Exclude:
27
+ - 'spec/**/*.rb'
28
+
29
+ Style/FetchEnvVar:
30
+ Enabled: false
31
+
32
+ Metrics/ParameterLists:
33
+ CountKeywordArgs: false
34
+
35
+ Layout/LineLength:
36
+ Max: 180
37
+
38
+ Layout/ClassStructure:
39
+ Enabled: true
40
+
41
+ Lint/MissingSuper:
42
+ Enabled: false
43
+
44
+ Metrics/MethodLength:
45
+ Max: 30
46
+
47
+ Style/FormatStringToken:
48
+ Enabled: false
49
+
50
+ Style/IfUnlessModifier:
51
+ Enabled: false
52
+
53
+ Style/OptionalBooleanParameter:
54
+ Enabled: false
55
+
56
+ Style/RegexpLiteral:
57
+ AllowInnerSlashes: true
58
+
59
+ Layout/MultilineMethodCallIndentation:
60
+ EnforcedStyle: indented
61
+
62
+ Layout/FirstArrayElementIndentation:
63
+ EnforcedStyle: consistent
64
+
65
+ Layout/EndOfLine:
66
+ EnforcedStyle: lf
67
+
68
+ Rails:
69
+ Enabled: true
70
+
71
+ Rails/FilePath:
72
+ Enabled: false
73
+
74
+ Rails/SkipsModelValidations:
75
+ Enabled: false
76
+
77
+ Rails/HelperInstanceVariable:
78
+ Enabled: false
79
+
80
+ Rails/UnknownEnv:
81
+ Enabled: false
82
+
83
+ Rails/HttpPositionalArguments:
84
+ Enabled: false
85
+
86
+ HttpPositionalArguments:
87
+ Enabled: false
88
+
89
+ HasAndBelongsToMany:
90
+ Enabled: false
91
+
92
+ Style/Documentation:
93
+ Enabled: false
94
+
95
+ Style/FrozenStringLiteralComment:
96
+ Enabled: false
97
+
98
+ Style/StringLiterals:
99
+ EnforcedStyle: double_quotes
100
+
101
+ Naming/VariableNumber:
102
+ Enabled: false
data/.rubocop_todo.yml ADDED
@@ -0,0 +1,30 @@
1
+ # This configuration was generated by
2
+ # `rubocop --auto-gen-config`
3
+ # on 2023-06-01 20:03:10 UTC using RuboCop version 1.28.2.
4
+ # The point is for the user to remove these configuration records
5
+ # one by one as the offenses are removed from the code base.
6
+ # Note that changes in the inspected code, or installation of new
7
+ # versions of RuboCop, may require this file to be generated again.
8
+
9
+ # Offense count: 1
10
+ # This cop supports safe auto-correction (--auto-correct).
11
+ # Configuration parameters: Include.
12
+ # Include: **/*.gemspec
13
+ Gemspec/RequireMFA:
14
+ Exclude:
15
+ - 'paymob_ruby.gemspec'
16
+
17
+ # Offense count: 1
18
+ # Configuration parameters: Include.
19
+ # Include: **/*.gemspec
20
+ Gemspec/RequiredRubyVersion:
21
+ Exclude:
22
+ - 'paymob_ruby.gemspec'
23
+
24
+ # Offense count: 2
25
+ # This cop supports safe auto-correction (--auto-correct).
26
+ # Configuration parameters: EnforcedStyle, AllowInnerSlashes.
27
+ # SupportedStyles: slashes, percent_r, mixed
28
+ Style/RegexpLiteral:
29
+ Exclude:
30
+ - 'paymob_ruby.gemspec'
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 3.1.3
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2023-06-01
4
+
5
+ - Initial release
data/Gemfile ADDED
@@ -0,0 +1,22 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in paymob_ruby.gemspec
4
+ gemspec
5
+
6
+ group :development, :test do
7
+ gem "dotenv-rails"
8
+ gem "fuubar", "~> 2.5.1"
9
+ gem "guard-rspec"
10
+ gem "rake", "~> 13.0"
11
+ gem "rspec", "~> 3.0"
12
+ gem "rubocop", "~> 1.52.0"
13
+ gem "rubocop-performance", "~> 1.18.0"
14
+ gem "rubocop-rails", "~> 2.19.1"
15
+ end
16
+
17
+ group :test do
18
+ gem "generator_spec"
19
+ gem "simplecov", "~> 0.22.0"
20
+ gem "vcr"
21
+ gem "webmock", "~> 3.18.1"
22
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,194 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ paymob_accept_ruby (0.1.0)
5
+ activesupport (~> 7.0)
6
+ faraday (~> 2.7.5)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ actionpack (7.0.5)
12
+ actionview (= 7.0.5)
13
+ activesupport (= 7.0.5)
14
+ rack (~> 2.0, >= 2.2.4)
15
+ rack-test (>= 0.6.3)
16
+ rails-dom-testing (~> 2.0)
17
+ rails-html-sanitizer (~> 1.0, >= 1.2.0)
18
+ actionview (7.0.5)
19
+ activesupport (= 7.0.5)
20
+ builder (~> 3.1)
21
+ erubi (~> 1.4)
22
+ rails-dom-testing (~> 2.0)
23
+ rails-html-sanitizer (~> 1.1, >= 1.2.0)
24
+ activesupport (7.0.5)
25
+ concurrent-ruby (~> 1.0, >= 1.0.2)
26
+ i18n (>= 1.6, < 2)
27
+ minitest (>= 5.1)
28
+ tzinfo (~> 2.0)
29
+ addressable (2.8.4)
30
+ public_suffix (>= 2.0.2, < 6.0)
31
+ ast (2.4.2)
32
+ base64 (0.1.1)
33
+ builder (3.2.4)
34
+ coderay (1.1.3)
35
+ concurrent-ruby (1.2.2)
36
+ crack (0.4.5)
37
+ rexml
38
+ crass (1.0.6)
39
+ diff-lcs (1.5.0)
40
+ docile (1.4.0)
41
+ dotenv (2.8.1)
42
+ dotenv-rails (2.8.1)
43
+ dotenv (= 2.8.1)
44
+ railties (>= 3.2)
45
+ erubi (1.12.0)
46
+ faraday (2.7.11)
47
+ base64
48
+ faraday-net_http (>= 2.0, < 3.1)
49
+ ruby2_keywords (>= 0.0.4)
50
+ faraday-net_http (3.0.2)
51
+ ffi (1.15.5)
52
+ formatador (1.1.0)
53
+ fuubar (2.5.1)
54
+ rspec-core (~> 3.0)
55
+ ruby-progressbar (~> 1.4)
56
+ generator_spec (0.9.4)
57
+ activesupport (>= 3.0.0)
58
+ railties (>= 3.0.0)
59
+ guard (2.18.0)
60
+ formatador (>= 0.2.4)
61
+ listen (>= 2.7, < 4.0)
62
+ lumberjack (>= 1.0.12, < 2.0)
63
+ nenv (~> 0.1)
64
+ notiffany (~> 0.0)
65
+ pry (>= 0.13.0)
66
+ shellany (~> 0.0)
67
+ thor (>= 0.18.1)
68
+ guard-compat (1.2.1)
69
+ guard-rspec (4.7.3)
70
+ guard (~> 2.1)
71
+ guard-compat (~> 1.1)
72
+ rspec (>= 2.99.0, < 4.0)
73
+ hashdiff (1.0.1)
74
+ i18n (1.14.1)
75
+ concurrent-ruby (~> 1.0)
76
+ json (2.6.3)
77
+ listen (3.8.0)
78
+ rb-fsevent (~> 0.10, >= 0.10.3)
79
+ rb-inotify (~> 0.9, >= 0.9.10)
80
+ loofah (2.21.3)
81
+ crass (~> 1.0.2)
82
+ nokogiri (>= 1.12.0)
83
+ lumberjack (1.2.8)
84
+ method_source (1.0.0)
85
+ minitest (5.18.0)
86
+ nenv (0.3.0)
87
+ nokogiri (1.15.2-arm64-darwin)
88
+ racc (~> 1.4)
89
+ notiffany (0.1.3)
90
+ nenv (~> 0.1)
91
+ shellany (~> 0.0)
92
+ parallel (1.23.0)
93
+ parser (3.2.2.1)
94
+ ast (~> 2.4.1)
95
+ pry (0.14.2)
96
+ coderay (~> 1.1)
97
+ method_source (~> 1.0)
98
+ public_suffix (5.0.1)
99
+ racc (1.6.2)
100
+ rack (2.2.7)
101
+ rack-test (2.1.0)
102
+ rack (>= 1.3)
103
+ rails-dom-testing (2.0.3)
104
+ activesupport (>= 4.2.0)
105
+ nokogiri (>= 1.6)
106
+ rails-html-sanitizer (1.6.0)
107
+ loofah (~> 2.21)
108
+ nokogiri (~> 1.14)
109
+ railties (7.0.5)
110
+ actionpack (= 7.0.5)
111
+ activesupport (= 7.0.5)
112
+ method_source
113
+ rake (>= 12.2)
114
+ thor (~> 1.0)
115
+ zeitwerk (~> 2.5)
116
+ rainbow (3.1.1)
117
+ rake (13.0.6)
118
+ rb-fsevent (0.11.2)
119
+ rb-inotify (0.10.1)
120
+ ffi (~> 1.0)
121
+ regexp_parser (2.8.0)
122
+ rexml (3.2.5)
123
+ rspec (3.12.0)
124
+ rspec-core (~> 3.12.0)
125
+ rspec-expectations (~> 3.12.0)
126
+ rspec-mocks (~> 3.12.0)
127
+ rspec-core (3.12.2)
128
+ rspec-support (~> 3.12.0)
129
+ rspec-expectations (3.12.3)
130
+ diff-lcs (>= 1.2.0, < 2.0)
131
+ rspec-support (~> 3.12.0)
132
+ rspec-mocks (3.12.5)
133
+ diff-lcs (>= 1.2.0, < 2.0)
134
+ rspec-support (~> 3.12.0)
135
+ rspec-support (3.12.0)
136
+ rubocop (1.52.0)
137
+ json (~> 2.3)
138
+ parallel (~> 1.10)
139
+ parser (>= 3.2.0.0)
140
+ rainbow (>= 2.2.2, < 4.0)
141
+ regexp_parser (>= 1.8, < 3.0)
142
+ rexml (>= 3.2.5, < 4.0)
143
+ rubocop-ast (>= 1.28.0, < 2.0)
144
+ ruby-progressbar (~> 1.7)
145
+ unicode-display_width (>= 2.4.0, < 3.0)
146
+ rubocop-ast (1.29.0)
147
+ parser (>= 3.2.1.0)
148
+ rubocop-performance (1.18.0)
149
+ rubocop (>= 1.7.0, < 2.0)
150
+ rubocop-ast (>= 0.4.0)
151
+ rubocop-rails (2.19.1)
152
+ activesupport (>= 4.2.0)
153
+ rack (>= 1.1)
154
+ rubocop (>= 1.33.0, < 2.0)
155
+ ruby-progressbar (1.13.0)
156
+ ruby2_keywords (0.0.5)
157
+ shellany (0.0.1)
158
+ simplecov (0.22.0)
159
+ docile (~> 1.1)
160
+ simplecov-html (~> 0.11)
161
+ simplecov_json_formatter (~> 0.1)
162
+ simplecov-html (0.12.3)
163
+ simplecov_json_formatter (0.1.4)
164
+ thor (1.2.2)
165
+ tzinfo (2.0.6)
166
+ concurrent-ruby (~> 1.0)
167
+ unicode-display_width (2.4.2)
168
+ vcr (6.2.0)
169
+ webmock (3.18.1)
170
+ addressable (>= 2.8.0)
171
+ crack (>= 0.3.2)
172
+ hashdiff (>= 0.4.0, < 2.0.0)
173
+ zeitwerk (2.6.8)
174
+
175
+ PLATFORMS
176
+ arm64-darwin-21
177
+
178
+ DEPENDENCIES
179
+ dotenv-rails
180
+ fuubar (~> 2.5.1)
181
+ generator_spec
182
+ guard-rspec
183
+ paymob_accept_ruby!
184
+ rake (~> 13.0)
185
+ rspec (~> 3.0)
186
+ rubocop (~> 1.52.0)
187
+ rubocop-performance (~> 1.18.0)
188
+ rubocop-rails (~> 2.19.1)
189
+ simplecov (~> 0.22.0)
190
+ vcr
191
+ webmock (~> 3.18.1)
192
+
193
+ BUNDLED WITH
194
+ 2.3.26
data/Guardfile ADDED
@@ -0,0 +1,42 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ ## Uncomment and set this to only include directories you want to watch
5
+ directories(%w[lib config spec] \
6
+ .select { |d| Dir.exist?(d) ? d : UI.warning("Directory #{d} does not exist") })
7
+
8
+ ## Note: if you are using the `directories` clause above and you are not
9
+ ## watching the project directory ('.'), then you will want to move
10
+ ## the Guardfile to a watched dir and symlink it back, e.g.
11
+ #
12
+ # $ mkdir config
13
+ # $ mv Guardfile config/
14
+ # $ ln -s config/Guardfile .
15
+ #
16
+ # and, you'll have to watch "config/Guardfile" instead of "Guardfile"
17
+
18
+ # NOTE: The cmd option is now required due to the increasing number of ways
19
+ # rspec may be run, below are examples of the most common uses.
20
+ # * bundler: 'bundle exec rspec'
21
+ # * bundler binstubs: 'bin/rspec'
22
+ # * spring: 'bin/rspec' (This will use spring if running and you have
23
+ # installed the spring binstubs per the docs)
24
+ # * zeus: 'zeus rspec' (requires the server to be started separately)
25
+ # * 'just' rspec: 'rspec'
26
+
27
+ guard :rspec, cmd: "bundle exec rspec" do
28
+ require "guard/rspec/dsl"
29
+ dsl = Guard::RSpec::Dsl.new(self)
30
+
31
+ # Feel free to open issues for suggestions and improvements
32
+
33
+ # RSpec files
34
+ rspec = dsl.rspec
35
+ watch(rspec.spec_helper) { rspec.spec_dir }
36
+ watch(rspec.spec_support) { rspec.spec_dir }
37
+ watch(rspec.spec_files)
38
+
39
+ # Ruby files
40
+ ruby = dsl.ruby
41
+ dsl.watch_spec_files_for(ruby.lib_files)
42
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Joe Kaldas
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,33 @@
1
+ # Paymob
2
+
3
+ Unofficial Paymob Ruby gem for [Paymob](https://paymob.com/)
4
+
5
+ ## Installation
6
+
7
+ Install the gem and add to the application's Gemfile by executing:
8
+
9
+ $ bundle add paymob
10
+
11
+ If bundler is not being used to manage dependencies, install the gem by executing:
12
+
13
+ $ gem install paymob
14
+
15
+ Next, you need to run the generator:
16
+
17
+ $ rails generate paymob:install
18
+
19
+ ## Usage
20
+
21
+ ```ruby
22
+ PaymobRuby.configure do |config|
23
+ config.api_key = "api_key"
24
+ end
25
+ ```
26
+
27
+ ## Contributing
28
+
29
+ Bug reports and pull requests are welcome.
30
+
31
+ ## License
32
+
33
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+
5
+ require "rake"
6
+ begin
7
+ require "bundler/setup"
8
+ Bundler::GemHelper.install_tasks
9
+ rescue LoadError
10
+ puts "although not required, bundler is recommended for running the tests"
11
+ end
12
+
13
+ task default: :spec
14
+ require "rspec/core/rake_task"
15
+ RSpec::Core::RakeTask.new(:spec)
16
+
17
+ require "rubocop/rake_task"
18
+ RuboCop::RakeTask.new do |task|
19
+ task.requires << "rubocop-performance"
20
+ task.requires << "rubocop-rspec"
21
+ end
@@ -0,0 +1,5 @@
1
+ require "paymob_ruby"
2
+
3
+ PaymobRuby.configure do |config|
4
+ config.api_key = "your_api_key"
5
+ end
@@ -0,0 +1,15 @@
1
+ require "rails/generators"
2
+
3
+ module PaymobRuby
4
+ module Generators
5
+ class InstallGenerator < Rails::Generators::Base
6
+ source_root File.expand_path("templates", __dir__)
7
+
8
+ desc "Creates a Paymob initializers file."
9
+
10
+ def copy_initializer
11
+ copy_file "paymob.rb", "config/initializers/paymob.rb"
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,5 @@
1
+ require "paymob_ruby"
2
+
3
+ PaymobRuby.configure do |config|
4
+ config.api_key = "your_api_key"
5
+ end
@@ -0,0 +1,24 @@
1
+ module PaymobRuby
2
+ class ApplicationService
3
+ Response = Struct.new(:success?, :payload, :error) do
4
+ def failure?
5
+ !success?
6
+ end
7
+ end
8
+
9
+ def self.call(...)
10
+ service = new
11
+ service.call(...)
12
+ rescue StandardError => e
13
+ service.failure(e)
14
+ end
15
+
16
+ def success(payload = nil)
17
+ Response.new(true, payload)
18
+ end
19
+
20
+ def failure(exception)
21
+ Response.new(false, nil, exception)
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,13 @@
1
+ module PaymobRuby
2
+ class Login < ApplicationService
3
+ def call
4
+ url = "#{BASE_URI}/auth/tokens"
5
+ response = ::Faraday.post(url, { api_key: PaymobRuby.api_key }.to_json, "Content-Type" => "application/json")
6
+
7
+ raise AuthenticationError.new("Invalid API key", http_status: response.status, http_body: response.body) unless response.success?
8
+
9
+ response_json = JSON.parse(response.body)
10
+ success(response_json["token"])
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,29 @@
1
+ module PaymobRuby
2
+ class PayCard < ApplicationService
3
+ def call(user:, amount:, integration_id:, iframe_id:, commission_fees: 0)
4
+ @iframe_id = iframe_id
5
+ result = PaymobRuby::PaymentToken.call(
6
+ integration_id:,
7
+ user:,
8
+ amount:,
9
+ commission_fees:
10
+ ).payload
11
+ @payment_token = result[:payment_token]
12
+
13
+ valid_url?
14
+
15
+ success({ payment_link: }.merge(result))
16
+ end
17
+
18
+ private
19
+
20
+ def valid_url?
21
+ response = ::Faraday.get(payment_link)
22
+ raise InvalidRequestError.new("Invalid iframe ID", :iframe_id, http_status: response.status, http_body: response.body) unless response.success?
23
+ end
24
+
25
+ def payment_link
26
+ @payment_link ||= "#{BASE_URI}/acceptance/iframes/#{@iframe_id}?payment_token=#{@payment_token}"
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,38 @@
1
+ module PaymobRuby
2
+ class PayToken < ApplicationService
3
+ def call(user:, amount:, integration_id:, cc_token:, commission_fees: 0)
4
+ @cc_token = cc_token
5
+ result = PaymobRuby::PaymentToken.call(
6
+ integration_id:,
7
+ user:,
8
+ amount:,
9
+ commission_fees:
10
+ ).payload
11
+ @payment_token = result[:payment_token]
12
+
13
+ response = pay!
14
+
15
+ success({ response: }.merge(result))
16
+ end
17
+
18
+ private
19
+
20
+ def pay!
21
+ response = Faraday.post(
22
+ "#{BASE_URI}/acceptance/payments/pay",
23
+ {
24
+ source: {
25
+ subtype: "TOKEN",
26
+ identifier: @cc_token
27
+ },
28
+ payment_token: @payment_token
29
+ }.to_json,
30
+ "Content-Type" => "application/json"
31
+ )
32
+
33
+ raise APIError.new("Something went wrong", http_status: response.status, http_body: response.body) unless response.success?
34
+
35
+ JSON.parse(response.body)
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,112 @@
1
+ module PaymobRuby
2
+ class PaymentToken < ApplicationService
3
+ def call(user:, amount:, integration_id:, commission_fees: 0)
4
+ @login_token = PaymobRuby::Login.call.payload
5
+ @user = user
6
+ @amount = amount.round
7
+ @integration_id = integration_id
8
+ @commission_fees = commission_fees
9
+
10
+ sanity_checks!
11
+
12
+ # Paymob order id
13
+ @order_id = order_response["id"]
14
+
15
+ success({ payment_token: payment_key_response["token"], order_id: @order_id })
16
+ end
17
+
18
+ private
19
+
20
+ def sanity_checks! # rubocop:disable Metrics/AbcSize
21
+ raise InvalidRequestError.new("Amount must be positive", :amount) unless @amount.positive?
22
+ raise InvalidRequestError.new("Commission fees can't be negative", :commission_fees) if @commission_fees.negative?
23
+ raise InvalidRequestError.new("First name is missing", :user) if first_name.blank?
24
+ raise InvalidRequestError.new("Last name is missing", :user) if last_name.blank?
25
+ raise InvalidRequestError.new("Email is missing", :user) if email.blank?
26
+ raise InvalidRequestError.new("Phone number is missing", :user) if phone_number.blank?
27
+ end
28
+
29
+ def order_response
30
+ @order_response ||= Faraday.post(
31
+ "#{BASE_URI}/ecommerce/orders",
32
+ order_body.to_json,
33
+ "Content-Type": "application/json"
34
+ )
35
+
36
+ raise APIError.new("Something went wrong", http_status: @order_response.status, http_body: @order_response.body) unless @order_response.success?
37
+
38
+ JSON.parse(@order_response.body)
39
+ end
40
+
41
+ def payment_key_response
42
+ @payment_key_response ||= Faraday.post(
43
+ "#{BASE_URI}/acceptance/payment_keys",
44
+ payment_key_body.to_json,
45
+ "Content-Type": "application/json"
46
+ )
47
+
48
+ unless @payment_key_response.success?
49
+ raise InvalidRequestError.new("Invalid integration ID", :integration_id, http_status: @payment_key_response.status, http_body: @payment_key_response.body)
50
+ end
51
+
52
+ JSON.parse(@payment_key_response.body)
53
+ end
54
+
55
+ def first_name
56
+ @first_name ||= @user[:first_name]
57
+ end
58
+
59
+ def last_name
60
+ @last_name ||= @user[:last_name]
61
+ end
62
+
63
+ def phone_number
64
+ @phone_number ||= @user[:phone_number]
65
+ end
66
+
67
+ def email
68
+ @email ||= @user[:email]
69
+ end
70
+
71
+ def order_body
72
+ {
73
+ auth_token: @login_token,
74
+ delivery_needed: false,
75
+ amount_cents: (@amount * 100).to_i,
76
+ currency: "EGP",
77
+ items: [],
78
+ commission_fees: @commission_fees
79
+ }
80
+ end
81
+
82
+ def payment_key_body
83
+ {
84
+ auth_token: @login_token,
85
+ amount_cents: (@amount * 100).to_i,
86
+ expiration: 36_000,
87
+ order_id: @order_id,
88
+ billing_data: address_info,
89
+ currency: "EGP",
90
+ card_integration_id: @integration_id
91
+ }
92
+ end
93
+
94
+ def address_info
95
+ {
96
+ first_name:,
97
+ last_name:,
98
+ phone_number:,
99
+ email:,
100
+ street: "NA",
101
+ building: "NA",
102
+ floor: "NA",
103
+ apartment: "NA",
104
+ postal_code: "NA",
105
+ city: "NA",
106
+ state: "NA",
107
+ country: "EG",
108
+ shipping_method: "PKG"
109
+ }
110
+ end
111
+ end
112
+ end
@@ -0,0 +1,78 @@
1
+ module PaymobRuby
2
+ # PaymobError is the base error from which all other more specific Paymob
3
+ # errors derive.
4
+ class PaymobError < StandardError
5
+ attr_reader :message, :error, :http_body, :http_headers, :http_status, :json_body
6
+
7
+ # Initializes a PaymobError.
8
+ def initialize(message = nil, http_status: nil, http_body: nil)
9
+ @message = message
10
+ @http_status = http_status
11
+ @http_body = http_body
12
+ begin
13
+ @json_body = JSON.parse(http_body)
14
+ rescue StandardError
15
+ @json_body = nil
16
+ end
17
+ end
18
+
19
+ def to_s
20
+ status_string = @http_status.nil? ? "" : "(Status #{@http_status}) "
21
+ "#{status_string}#{@message}"
22
+ end
23
+ end
24
+
25
+ # AuthenticationError is raised when invalid credentials are used to connect
26
+ # to Paymob's servers.
27
+ class AuthenticationError < PaymobError
28
+ end
29
+
30
+ # APIConnectionError is raised in the event that the SDK can't connect to
31
+ # Paymob's servers. That can be for a variety of different reasons from a
32
+ # downed network to a bad TLS certificate.
33
+ class APIConnectionError < PaymobError
34
+ end
35
+
36
+ # APIError is a generic error that may be raised in cases where none of the
37
+ # other named errors cover the problem. It could also be raised in the case
38
+ # that a new error has been introduced in the API, but this version of the
39
+ # Ruby SDK doesn't know how to handle it.
40
+ class APIError < PaymobError
41
+ end
42
+
43
+ # InvalidRequestError is raised when a request is initiated with invalid
44
+ # parameters.
45
+ class InvalidRequestError < PaymobError
46
+ attr_accessor :param
47
+
48
+ def initialize(message, param, http_status: nil, http_body: nil)
49
+ super(message, http_status:, http_body:)
50
+ @param = param
51
+ end
52
+ end
53
+
54
+ # SignatureVerificationError is raised when the signature verification for a
55
+ # webhook fails
56
+ class SignatureVerificationError < PaymobError
57
+ attr_accessor :sig_header
58
+
59
+ def initialize(message, sig_header, http_body: nil)
60
+ super(message, http_body:)
61
+ @sig_header = sig_header
62
+ end
63
+ 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
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PaymobRuby
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "faraday"
4
+ require "active_support"
5
+
6
+ require_relative "paymob_ruby/version"
7
+
8
+ # API resource support classes
9
+ require "paymob_ruby/errors"
10
+
11
+ # 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"
17
+
18
+ module PaymobRuby
19
+ BASE_URI = "https://accept.paymobsolutions.com/api"
20
+
21
+ class << self
22
+ attr_accessor :api_key
23
+ end
24
+
25
+ def self.configure
26
+ yield self
27
+ end
28
+ end
data/sig/paymob.rbs ADDED
@@ -0,0 +1,4 @@
1
+ module PaymobRuby
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: paymob_ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Joe Kaldas
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-09-18 00:00:00.000000000 Z
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'
27
+ - !ruby/object:Gem::Dependency
28
+ name: faraday
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 2.7.5
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 2.7.5
41
+ description:
42
+ email:
43
+ - joekaldas8@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".rspec"
49
+ - ".rubocop.yml"
50
+ - ".rubocop_todo.yml"
51
+ - ".ruby-version"
52
+ - CHANGELOG.md
53
+ - Gemfile
54
+ - Gemfile.lock
55
+ - Guardfile
56
+ - LICENSE.txt
57
+ - README.md
58
+ - Rakefile
59
+ - config/initializers/paymob.rb
60
+ - lib/generators/paymob_ruby/install_generator.rb
61
+ - lib/generators/paymob_ruby/templates/paymob.rb
62
+ - lib/paymob_ruby.rb
63
+ - lib/paymob_ruby/actions/application_service.rb
64
+ - lib/paymob_ruby/actions/login.rb
65
+ - lib/paymob_ruby/actions/pay_card.rb
66
+ - lib/paymob_ruby/actions/pay_token.rb
67
+ - lib/paymob_ruby/actions/payment_token.rb
68
+ - lib/paymob_ruby/errors.rb
69
+ - lib/paymob_ruby/version.rb
70
+ - sig/paymob.rbs
71
+ homepage: https://github.com/JoeKaldas/paymob
72
+ licenses:
73
+ - MIT
74
+ metadata:
75
+ allowed_push_host: https://rubygems.org
76
+ homepage_uri: https://github.com/JoeKaldas/paymob
77
+ source_code_uri: https://github.com/JoeKaldas/paymob
78
+ rubygems_mfa_required: 'true'
79
+ post_install_message:
80
+ rdoc_options: []
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: 3.1.3
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ requirements: []
94
+ rubygems_version: 3.3.26
95
+ signing_key:
96
+ specification_version: 4
97
+ summary: Ruby bindings for the Paymob API.
98
+ test_files: []