fawry 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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 35a9a9bd2b1674d4f6df24e1b7a8fb187585906fed608c41991613f25f459214
4
+ data.tar.gz: f1c205a1f9d92c758df8fe13f0a4bcd0bc70ea22106ab380984db00bc1303f6a
5
+ SHA512:
6
+ metadata.gz: 7d3f2b4b9f111b8f967cbe2a67da12ed9c4683fd99a70e784d14963642b609594f4a2592782e5cfa0d92b8c16cbbe024d70fa99c150a74a0600656bab61057ed
7
+ data.tar.gz: f65d775062f6a683b855b7d3deb10d8fa99b4f05a4f365bcb6b86312f2375d506f6e7e50f87f91ce517c5878e6b707a1dafc6b873ff0a729f6a101193709e4d1
@@ -0,0 +1,13 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+
13
+ .byebug_history
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,6 @@
1
+ Metrics/LineLength:
2
+ Max: 120
3
+ Metrics/BlockLength:
4
+ ExcludedMethods: ['describe', 'context']
5
+ Style/Documentation:
6
+ Enabled: false
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.6.0
7
+ before_install: gem install bundler -v 2.0.2
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at amrrbakry17@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in fawry.gemspec
6
+ gemspec
@@ -0,0 +1,92 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ fawry (0.1.0)
5
+ dry-validation (~> 1.3, >= 1.3.1)
6
+ faraday (~> 0.17.0)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ addressable (2.7.0)
12
+ public_suffix (>= 2.0.2, < 5.0)
13
+ byebug (11.0.1)
14
+ concurrent-ruby (1.1.5)
15
+ crack (0.4.3)
16
+ safe_yaml (~> 1.0.0)
17
+ diff-lcs (1.3)
18
+ dry-configurable (0.8.3)
19
+ concurrent-ruby (~> 1.0)
20
+ dry-core (~> 0.4, >= 0.4.7)
21
+ dry-container (0.7.2)
22
+ concurrent-ruby (~> 1.0)
23
+ dry-configurable (~> 0.1, >= 0.1.3)
24
+ dry-core (0.4.9)
25
+ concurrent-ruby (~> 1.0)
26
+ dry-equalizer (0.2.2)
27
+ dry-inflector (0.2.0)
28
+ dry-initializer (3.0.1)
29
+ dry-logic (1.0.3)
30
+ concurrent-ruby (~> 1.0)
31
+ dry-core (~> 0.2)
32
+ dry-equalizer (~> 0.2)
33
+ dry-schema (1.4.1)
34
+ concurrent-ruby (~> 1.0)
35
+ dry-configurable (~> 0.8, >= 0.8.3)
36
+ dry-core (~> 0.4)
37
+ dry-equalizer (~> 0.2)
38
+ dry-initializer (~> 3.0)
39
+ dry-logic (~> 1.0)
40
+ dry-types (~> 1.2)
41
+ dry-types (1.2.0)
42
+ concurrent-ruby (~> 1.0)
43
+ dry-container (~> 0.3)
44
+ dry-core (~> 0.4, >= 0.4.4)
45
+ dry-equalizer (~> 0.2, >= 0.2.2)
46
+ dry-inflector (~> 0.1, >= 0.1.2)
47
+ dry-logic (~> 1.0, >= 1.0.2)
48
+ dry-validation (1.3.1)
49
+ concurrent-ruby (~> 1.0)
50
+ dry-container (~> 0.7, >= 0.7.1)
51
+ dry-core (~> 0.4)
52
+ dry-equalizer (~> 0.2)
53
+ dry-initializer (~> 3.0)
54
+ dry-schema (~> 1.0, >= 1.3.1)
55
+ faraday (0.17.0)
56
+ multipart-post (>= 1.2, < 3)
57
+ hashdiff (1.0.0)
58
+ multipart-post (2.1.1)
59
+ public_suffix (4.0.1)
60
+ rake (10.5.0)
61
+ rspec (3.9.0)
62
+ rspec-core (~> 3.9.0)
63
+ rspec-expectations (~> 3.9.0)
64
+ rspec-mocks (~> 3.9.0)
65
+ rspec-core (3.9.0)
66
+ rspec-support (~> 3.9.0)
67
+ rspec-expectations (3.9.0)
68
+ diff-lcs (>= 1.2.0, < 2.0)
69
+ rspec-support (~> 3.9.0)
70
+ rspec-mocks (3.9.0)
71
+ diff-lcs (>= 1.2.0, < 2.0)
72
+ rspec-support (~> 3.9.0)
73
+ rspec-support (3.9.0)
74
+ safe_yaml (1.0.5)
75
+ webmock (3.7.6)
76
+ addressable (>= 2.3.6)
77
+ crack (>= 0.3.2)
78
+ hashdiff (>= 0.4.0, < 2.0.0)
79
+
80
+ PLATFORMS
81
+ ruby
82
+
83
+ DEPENDENCIES
84
+ bundler (~> 2.0)
85
+ byebug (~> 11.0, >= 11.0.1)
86
+ fawry!
87
+ rake (~> 10.0)
88
+ rspec (~> 3.0)
89
+ webmock (~> 3.7, >= 3.7.6)
90
+
91
+ BUNDLED WITH
92
+ 2.0.2
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Amr Bakry
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.
@@ -0,0 +1,80 @@
1
+ # Fawry
2
+ A plug-and-play library that makes interfacing with Fawry's payment gateway API a breeze:
3
+ - [Charge customers](https://github.com/amrrbakry/fawry#charge-customers)
4
+ - [Refund customers](https://github.com/amrrbakry/fawry#refund-customers)
5
+ - Get payment status _(Not yet implemented)_
6
+ - Parse Fawry's service callback V2 _(Not yet implemented)_
7
+
8
+ _Fawry's production and sandbox environments are supported._
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ ```ruby
15
+ gem 'fawry'
16
+ ```
17
+
18
+ And then execute:
19
+
20
+ $ bundle
21
+
22
+ Or install it yourself as:
23
+
24
+ $ gem install fawry
25
+
26
+ ## Usage
27
+ ### Charge customers
28
+ ```ruby
29
+ params = { "merchant_code": 'merchant_code',
30
+ "merchant_ref_num": 'io5jxf3jp27kfh8m719arcqgw7izo7db',
31
+ "customer_profile_id": 'ocvsydvbu2gcp528wvl64i9z5srdalg5',
32
+ "customer_mobile": '012345678901',
33
+ "payment_method": 'PAYATFAWRY',
34
+ "currency_code": 'EGP',
35
+ "amount": 20,
36
+ "fawry_secure_key": 'fawry_secure_key',
37
+ "description": 'the charge request description',
38
+ "charge_items": [{ "item_id": 'fk3fn9flk8et9a5t9w3c5h3oc684ivho',
39
+ "description": 'desc', "price": 20, "quantity": 1 }] }
40
+
41
+ # use sandbox option to call Fawry's sandbox env
42
+ res = Fawry.charge(params, sandbox: true)
43
+ # => #<Fawry::FawryResponse:0x0000564257d0ea90 @type="ChargeResponse", @reference_number="931600239",
44
+ # @merchant_ref_number="io5jxf3jp27kfh8m719arcqgw7izo7db",
45
+ # @expiration_time=1573153206979, @status_code=200,
46
+ # @status_description="Operation done successfully">
47
+
48
+ res.success? # => true
49
+ res.reference_number # => 931600239
50
+ ```
51
+ ### Refund Customers
52
+ ```ruby
53
+ params = { "merchant_code": 'merchant_code',
54
+ "reference_number": '931337410',
55
+ "refund_amount": 20,
56
+ "fawry_secure_key": 'fawry_secure_key' }
57
+
58
+ res = Fawry.refund(params, sandbox: true)
59
+ # => #<Fawry::FawryResponse:0x0000564257d0ea90 @type="ResponseDataModel", @status_code=200,
60
+ # @status_description="Operation done successfully">
61
+
62
+ res.success? # => true
63
+ ```
64
+ ## Development
65
+
66
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
67
+
68
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
69
+
70
+ ## Contributing
71
+
72
+ Bug reports and pull requests are welcome on GitHub at https://github.com/amrrbakry/fawry. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
73
+
74
+ ## License
75
+
76
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
77
+
78
+ ## Code of Conduct
79
+
80
+ Everyone interacting in the Fawry project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/amrrbakry/fawry/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'fawry'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('lib', __dir__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'fawry/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'fawry'
9
+ spec.version = Fawry::VERSION
10
+ spec.authors = ['Amr Bakry']
11
+ spec.email = ['amrrbakry17@gmail.com']
12
+
13
+ spec.summary = "A library to interface with Fawry's payment gateway API (charge, refund, payment status,
14
+ service callback)."
15
+ spec.homepage = 'https://github.com/amrrbakry/fawry'
16
+ spec.license = 'MIT'
17
+
18
+ spec.metadata['homepage_uri'] = 'https://github.com/amrrbakry/fawry'
19
+ spec.metadata['source_code_uri'] = 'https://github.com/amrrbakry/fawry'
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
24
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
25
+ end
26
+ spec.bindir = 'exe'
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ['lib']
29
+
30
+ spec.add_dependency 'dry-validation', '~> 1.3', '>= 1.3.1'
31
+ spec.add_dependency 'faraday', '~> 0.17.0'
32
+
33
+ spec.add_development_dependency 'bundler', '~> 2.0'
34
+ spec.add_development_dependency 'byebug', '~> 11.0', '>= 11.0.1'
35
+ spec.add_development_dependency 'rake', '~> 10.0'
36
+ spec.add_development_dependency 'rspec', '~> 3.0'
37
+ spec.add_development_dependency 'webmock', '~> 3.7', '>= 3.7.6'
38
+ end
@@ -0,0 +1,85 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'fawry/version'
4
+ require 'fawry/connection'
5
+ require 'fawry/errors'
6
+ require 'fawry/fawry_request'
7
+ require 'fawry/fawry_response'
8
+ require 'fawry/requests/charge_request'
9
+ require 'fawry/requests/refund_request'
10
+ require 'fawry/contracts/charge_request_contract'
11
+ require 'fawry/contracts/refund_request_contract'
12
+
13
+ module Fawry
14
+ class << self
15
+ # Sends a charge request to Fawry API
16
+ # performs param validation and builds
17
+ # the request signature
18
+ #
19
+ # @param params [Hash] list of params to send to fawry
20
+ # required(:merchant_code).value(:string)
21
+ # required(:merchant_ref_num).value(:string)
22
+ # required(:customer_profile_id).value(:string)
23
+ # required(:amount).value(:decimal)
24
+ # required(:description).value(:string)
25
+ # required(:customer_mobile).value(:string)
26
+ # required(:fawry_secure_key).value(:string)
27
+ # required(:charge_items).array(:hash) do
28
+ # required(:item_id).value(:string)
29
+ # required(:description).value(:string)
30
+ # required(:price).value(:decimal)
31
+ # required(:quantity).value(:integer)
32
+ # end
33
+ # optional(:currency_code).value(:string)
34
+ # optional(:card_token).value(:string)
35
+ # optional(:customer_email).value(:string)
36
+ # optional(:payment_method).value(:string)
37
+ # optional(:payment_expiry).value(:integer)
38
+ #
39
+ # @param opts [Hash] list of options to
40
+ # configure the request
41
+ # @option opts :sandbox [Boolean] whether to
42
+ # send the request to fawry sandbox env or not
43
+ # false by default
44
+ # Example: `Fawry.charge(params, sandbox: true)`
45
+ #
46
+ # @raise [Fawry::InvalidFawryRequest] raised when one
47
+ # or more of the params are invalid. the message
48
+ # specifices which params and why are they invalid
49
+ #
50
+ # @return [Fawry::FawryResponse] an object that
51
+ # has Fawry API response keys as instance methods
52
+ # plus some convenience methods e.g. success?
53
+ def charge(params, opts = {})
54
+ FawryRequest.new('charge', params, opts).fire
55
+ end
56
+
57
+ # Sends a refund request to Fawry API
58
+ # performs param validation and builds
59
+ # the request signature
60
+ #
61
+ # @param params [Hash] list of params to send to fawry
62
+ # required(:merchant_code).value(:string)
63
+ # required(:reference_number).value(:string)
64
+ # required(:refund_amount).value(:decimal)
65
+ # required(:fawry_secure_key).value(:string)
66
+ # optional(:reason).value(:string)
67
+ #
68
+ # @param opts [Hash] list of options to
69
+ # configure the request
70
+ # @option opts :sandbox [Boolean] whether to
71
+ # send the request to fawry sandbox env or not
72
+ # false by default
73
+ #
74
+ # @raise [Fawry::InvalidFawryRequest] raised when one
75
+ # or more of the params are invalid. the message
76
+ # specifices which params and why are they invalid
77
+ #
78
+ # @return [Fawry::FawryResponse] an object that
79
+ # has Fawry API response keys as instance methods
80
+ # plus some convenience methods e.g. success?
81
+ def refund(params, opts = {})
82
+ FawryRequest.new('refund', params, opts).fire
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'faraday'
4
+ require 'json'
5
+
6
+ module Fawry
7
+ class Connection
8
+ FAWRY_BASE_URL = 'https://www.atfawry.com/ECommerceWeb/Fawry/payments/'
9
+
10
+ FAWRY_SANDBOX_BASE_URL = 'https://atfawry.fawrystaging.com//ECommerceWeb/Fawry/payments/'
11
+
12
+ class << self
13
+ def post(path, params, body, options)
14
+ conn = options[:sandbox] ? sandbox_connection : connection
15
+
16
+ conn.post(path) do |request|
17
+ request.params = params
18
+ request.body = body.to_json
19
+ end
20
+ end
21
+
22
+ private
23
+
24
+ def connection
25
+ @connection ||= Faraday.new(url: FAWRY_BASE_URL, headers: { 'Content-Type': 'application/json',
26
+ 'Accept': 'application/json' })
27
+ end
28
+
29
+ def sandbox_connection
30
+ @sandbox_connection ||= Faraday.new(url: FAWRY_SANDBOX_BASE_URL, headers: { 'Content-Type': 'application/json',
31
+ 'Accept': 'application/json' })
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'dry-validation'
4
+
5
+ module Fawry
6
+ module Contracts
7
+ class ChargeRequestContract < Dry::Validation::Contract
8
+ params do
9
+ required(:merchant_code).value(:string)
10
+ required(:merchant_ref_num).value(:string)
11
+ required(:customer_profile_id).value(:string)
12
+ required(:amount).value(:decimal)
13
+ required(:description).value(:string)
14
+ required(:customer_mobile).value(:string)
15
+ required(:fawry_secure_key).value(:string)
16
+ required(:charge_items).array(:hash) do
17
+ required(:item_id).value(:string)
18
+ required(:description).value(:string)
19
+ required(:price).value(:decimal)
20
+ required(:quantity).value(:integer)
21
+ end
22
+ optional(:currency_code).value(:string)
23
+ optional(:card_token).value(:string)
24
+ optional(:customer_email).value(:string)
25
+ optional(:payment_method).value(:string)
26
+ optional(:payment_expiry).value(:integer)
27
+ end
28
+
29
+ rule(:customer_email) do
30
+ unless /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i.match?(value)
31
+ key? && key.failure('has invalid format')
32
+ end
33
+ end
34
+
35
+ rule(:payment_method) do
36
+ if key? && value == 'CARD' && values[:card_token].nil?
37
+ key.failure('card_token is required when payment_method is CARD')
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'dry-validation'
4
+
5
+ module Fawry
6
+ module Contracts
7
+ class RefundRequestContract < Dry::Validation::Contract
8
+ params do
9
+ required(:merchant_code).value(:string)
10
+ required(:reference_number).value(:string)
11
+ required(:refund_amount).value(:decimal)
12
+ required(:fawry_secure_key).value(:string)
13
+ optional(:reason).value(:string)
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Fawry
4
+ class InvalidFawryRequest < StandardError; end
5
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Fawry
4
+ class FawryRequest
5
+ DEFAULT_OPTIONS = { sandbox: false }.freeze
6
+
7
+ attr_reader :action, :params, :request, :options
8
+
9
+ def initialize(action, params, opts)
10
+ @action = action
11
+ @params = params
12
+ @options = DEFAULT_OPTIONS.merge(opts)
13
+
14
+ build_request
15
+ end
16
+
17
+ private
18
+
19
+ def build_request
20
+ case action
21
+ when 'charge'
22
+ self.class.include Requests::ChargeRequest
23
+ validate_charge_params!
24
+ @request = build_charge_request
25
+ when 'refund'
26
+ self.class.include Requests::RefundRequest
27
+ validate_refund_params!
28
+ @request = build_refund_request
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Fawry
4
+ class FawryResponse
5
+ attr_reader :fawry_api_response
6
+
7
+ def initialize(fawry_api_response)
8
+ @fawry_api_response = fawry_api_response
9
+
10
+ build_response
11
+ end
12
+
13
+ def success?
14
+ status_code == 200
15
+ end
16
+
17
+ def failure?
18
+ !success?
19
+ end
20
+
21
+ def fawry_api_response_body
22
+ fawry_api_response
23
+ end
24
+
25
+ private
26
+
27
+ # Adds keys from fawry API response as methods
28
+ # on FawryResponse instance that return the value
29
+ # of each key
30
+ #
31
+ # type => type
32
+ # referenceNumber => reference_number
33
+ # merchantRefNumber => merchant_ref_number
34
+ # expirationTime => expiration_time
35
+ # statusCode => status_code
36
+ # statusDescription => status_description
37
+ #
38
+ # fawry_res = FawryResponse.new(response)
39
+ # fawry_res.status_code => 200
40
+ # fawry_res.reference_number => 1234567
41
+ def build_response
42
+ fawry_api_response.keys.each do |key|
43
+ method_name = key.split(/(?=[A-Z])/).map(&:downcase).join('_') # statusCode => status_code
44
+ instance_variable_set("@#{method_name}", fawry_api_response[key])
45
+ method_body = proc { instance_variable_get("@#{method_name}") }
46
+
47
+ self.class.public_send(:define_method, method_name, method_body)
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,67 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'digest'
4
+
5
+ module Fawry
6
+ module Requests
7
+ module ChargeRequest
8
+ DEFAULTS = { payment_method: 'PAYATFAWRY', currency_code: 'EGP' }.freeze
9
+
10
+ def fire
11
+ fawry_api_response = Connection.post(request[:path], request[:params], request[:body], request[:options])
12
+ response_body = JSON.parse(fawry_api_response.body)
13
+
14
+ FawryResponse.new(response_body)
15
+ end
16
+
17
+ private
18
+
19
+ def build_charge_request
20
+ {
21
+ path: 'charge',
22
+ params: {},
23
+ body: charge_request_transformed_params,
24
+ options: options
25
+ }
26
+ end
27
+
28
+ def request_params
29
+ @request_params ||= DEFAULTS.merge(params)
30
+ end
31
+
32
+ def charge_request_transformed_params
33
+ {
34
+ merchantCode: request_params[:merchant_code],
35
+ merchantRefNum: request_params[:merchant_ref_num],
36
+ customerProfileId: request_params[:customer_profile_id],
37
+ cardToken: request_params[:card_token],
38
+ customerMobile: request_params[:customer_mobile],
39
+ customerEmail: request_params[:customer_email],
40
+ paymentMethod: request_params[:payment_method],
41
+ amount: request_params[:amount],
42
+ description: request_params[:description],
43
+ paymentExpiry: request_params[:payment_expiry],
44
+ chargeItems: charge_items,
45
+ currencyCode: request_params[:currency_code],
46
+ signature: charge_request_signature
47
+ }.compact
48
+ end
49
+
50
+ def validate_charge_params!
51
+ contract = Contracts::ChargeRequestContract.new.call(request_params)
52
+ raise InvalidFawryRequest, contract.errors.to_h if contract.failure?
53
+ end
54
+
55
+ def charge_items
56
+ request_params[:charge_items].each { |hash| hash[:itemId] = hash.delete(:item_id) }
57
+ end
58
+
59
+ def charge_request_signature
60
+ Digest::SHA256.hexdigest("#{request_params[:merchant_code]}#{request_params[:merchant_ref_num]}"\
61
+ "#{request_params[:customer_profile_id]}#{request_params[:payment_method]}"\
62
+ "#{format('%<amount>.2f', amount: request_params[:amount])}"\
63
+ "#{request_params[:card_token]}#{request_params[:fawry_secure_key]}")
64
+ end
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'digest'
4
+
5
+ module Fawry
6
+ module Requests
7
+ module RefundRequest
8
+ def fire
9
+ fawry_api_response = Connection.post(request[:path], request[:params], request[:body], request[:options])
10
+ response_body = JSON.parse(fawry_api_response.body)
11
+
12
+ FawryResponse.new(response_body)
13
+ end
14
+
15
+ private
16
+
17
+ def build_refund_request
18
+ {
19
+ path: 'refund',
20
+ params: {},
21
+ body: refund_request_transformed_params,
22
+ options: options
23
+ }
24
+ end
25
+
26
+ def request_params
27
+ @request_params = params
28
+ end
29
+
30
+ def refund_request_transformed_params
31
+ {
32
+ merchantCode: request_params[:merchant_code],
33
+ referenceNumber: request_params[:reference_number],
34
+ refundAmount: request_params[:refund_amount],
35
+ reason: request_params[:reason],
36
+ signature: refund_request_signature
37
+ }.compact
38
+ end
39
+
40
+ def validate_refund_params!
41
+ contract = Contracts::RefundRequestContract.new.call(request_params)
42
+ raise InvalidFawryRequest, contract.errors.to_h if contract.failure?
43
+ end
44
+
45
+ def refund_request_signature
46
+ Digest::SHA256.hexdigest("#{request_params[:merchant_code]}#{request_params[:reference_number]}"\
47
+ "#{format('%<refund_amount>.2f', refund_amount: request_params[:refund_amount])}"\
48
+ "#{request_params[:reason]}#{request_params[:fawry_secure_key]}")
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Fawry
4
+ VERSION = '0.1.0'
5
+ end
metadata ADDED
@@ -0,0 +1,185 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fawry
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Amr Bakry
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-11-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: dry-validation
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 1.3.1
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '1.3'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 1.3.1
33
+ - !ruby/object:Gem::Dependency
34
+ name: faraday
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: 0.17.0
40
+ type: :runtime
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: 0.17.0
47
+ - !ruby/object:Gem::Dependency
48
+ name: bundler
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '2.0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '2.0'
61
+ - !ruby/object:Gem::Dependency
62
+ name: byebug
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '11.0'
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: 11.0.1
71
+ type: :development
72
+ prerelease: false
73
+ version_requirements: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - "~>"
76
+ - !ruby/object:Gem::Version
77
+ version: '11.0'
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: 11.0.1
81
+ - !ruby/object:Gem::Dependency
82
+ name: rake
83
+ requirement: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - "~>"
86
+ - !ruby/object:Gem::Version
87
+ version: '10.0'
88
+ type: :development
89
+ prerelease: false
90
+ version_requirements: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - "~>"
93
+ - !ruby/object:Gem::Version
94
+ version: '10.0'
95
+ - !ruby/object:Gem::Dependency
96
+ name: rspec
97
+ requirement: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - "~>"
100
+ - !ruby/object:Gem::Version
101
+ version: '3.0'
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - "~>"
107
+ - !ruby/object:Gem::Version
108
+ version: '3.0'
109
+ - !ruby/object:Gem::Dependency
110
+ name: webmock
111
+ requirement: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - "~>"
114
+ - !ruby/object:Gem::Version
115
+ version: '3.7'
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ version: 3.7.6
119
+ type: :development
120
+ prerelease: false
121
+ version_requirements: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - "~>"
124
+ - !ruby/object:Gem::Version
125
+ version: '3.7'
126
+ - - ">="
127
+ - !ruby/object:Gem::Version
128
+ version: 3.7.6
129
+ description:
130
+ email:
131
+ - amrrbakry17@gmail.com
132
+ executables: []
133
+ extensions: []
134
+ extra_rdoc_files: []
135
+ files:
136
+ - ".gitignore"
137
+ - ".rspec"
138
+ - ".rubocop.yml"
139
+ - ".travis.yml"
140
+ - CODE_OF_CONDUCT.md
141
+ - Gemfile
142
+ - Gemfile.lock
143
+ - LICENSE.txt
144
+ - README.md
145
+ - Rakefile
146
+ - bin/console
147
+ - bin/setup
148
+ - fawry.gemspec
149
+ - lib/fawry.rb
150
+ - lib/fawry/connection.rb
151
+ - lib/fawry/contracts/charge_request_contract.rb
152
+ - lib/fawry/contracts/refund_request_contract.rb
153
+ - lib/fawry/errors.rb
154
+ - lib/fawry/fawry_request.rb
155
+ - lib/fawry/fawry_response.rb
156
+ - lib/fawry/requests/charge_request.rb
157
+ - lib/fawry/requests/refund_request.rb
158
+ - lib/fawry/version.rb
159
+ homepage: https://github.com/amrrbakry/fawry
160
+ licenses:
161
+ - MIT
162
+ metadata:
163
+ homepage_uri: https://github.com/amrrbakry/fawry
164
+ source_code_uri: https://github.com/amrrbakry/fawry
165
+ post_install_message:
166
+ rdoc_options: []
167
+ require_paths:
168
+ - lib
169
+ required_ruby_version: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - ">="
172
+ - !ruby/object:Gem::Version
173
+ version: '0'
174
+ required_rubygems_version: !ruby/object:Gem::Requirement
175
+ requirements:
176
+ - - ">="
177
+ - !ruby/object:Gem::Version
178
+ version: '0'
179
+ requirements: []
180
+ rubygems_version: 3.0.1
181
+ signing_key:
182
+ specification_version: 4
183
+ summary: A library to interface with Fawry's payment gateway API (charge, refund,
184
+ payment status, service callback).
185
+ test_files: []