fawry 1.0.0 → 1.3.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -17,6 +17,7 @@ module Fawry
17
17
  private
18
18
 
19
19
  # rubocop:disable Metrics/MethodLength
20
+ # rubocop:disable Metrics/AbcSize
20
21
  def build_request
21
22
  case action
22
23
  when 'charge'
@@ -31,7 +32,20 @@ module Fawry
31
32
  self.class.include Requests::PaymentStatusRequest
32
33
  validate_payment_status_params!
33
34
  @request = build_payment_status_request
35
+ when 'create_card_token'
36
+ self.class.include Requests::CreateCardTokenRequest
37
+ validate_card_token_params!
38
+ @request = build_create_card_token_request
39
+ when 'list_tokens'
40
+ self.class.include Requests::ListTokensRequest
41
+ validate_list_tokens_params!
42
+ @request = build_list_tokens_request
43
+ when 'delete_token'
44
+ self.class.include Requests::DeleteTokenRequest
45
+ validate_delete_token_params!
46
+ @request = build_delete_token_request
34
47
  end
48
+ # rubocop:enable Metrics/AbcSize
35
49
  # rubocop:enable Metrics/MethodLength
36
50
  end
37
51
  end
@@ -5,7 +5,7 @@ require 'digest'
5
5
  module Fawry
6
6
  module Requests
7
7
  module ChargeRequest
8
- DEFAULTS = { payment_method: 'PAYATFAWRY', currency_code: 'EGP' }.freeze
8
+ DEFAULTS = { payment_method: 'PAYATFAWRY', currency_code: 'EGP', language: 'ar-eg' }.freeze
9
9
 
10
10
  def fire_charge_request
11
11
  fawry_api_response = Connection.post(request[:path], request[:params], request[:body], request[:options])
@@ -18,7 +18,7 @@ module Fawry
18
18
 
19
19
  def build_charge_request
20
20
  {
21
- path: 'charge',
21
+ path: 'payments/charge',
22
22
  params: {},
23
23
  body: charge_request_transformed_params,
24
24
  options: options
@@ -45,6 +45,7 @@ module Fawry
45
45
  paymentExpiry: request_params[:payment_expiry],
46
46
  chargeItems: charge_items,
47
47
  currencyCode: request_params[:currency_code],
48
+ language: request_params[:language],
48
49
  signature: charge_request_signature
49
50
  }.compact
50
51
  end
@@ -52,11 +53,11 @@ module Fawry
52
53
  # rubocop:enable Metrics/AbcSize
53
54
 
54
55
  def fawry_merchant_code
55
- ENV.fetch('FAWRY_MERCHANT_CODE') { request_params[:merchant_code] }
56
+ Fawry.configuration.fawry_merchant_code || ENV.fetch('FAWRY_MERCHANT_CODE') { request_params[:merchant_code] }
56
57
  end
57
58
 
58
59
  def fawry_secure_key
59
- ENV.fetch('FAWRY_SECURE_KEY') { request_params[:fawry_secure_key] }
60
+ Fawry.configuration.fawry_secure_key || ENV.fetch('FAWRY_SECURE_KEY') { request_params[:fawry_secure_key] }
60
61
  end
61
62
 
62
63
  def validate_charge_params!
@@ -0,0 +1,57 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'digest'
4
+
5
+ module Fawry
6
+ module Requests
7
+ module CreateCardTokenRequest
8
+ def fire_create_card_token_request
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_create_card_token_request
18
+ {
19
+ path: 'cards/cardToken',
20
+ params: {},
21
+ body: create_card_token_request_transformed_params,
22
+ options: options
23
+ }
24
+ end
25
+
26
+ def create_card_token_request
27
+ @create_card_token_request ||= params
28
+ end
29
+
30
+ def create_card_token_request_transformed_params
31
+ {
32
+ merchantCode: fawry_merchant_code,
33
+ customerProfileId: create_card_token_request[:customer_profile_id],
34
+ customerMobile: create_card_token_request[:customer_mobile],
35
+ customerEmail: create_card_token_request[:customer_email],
36
+ cardNumber: create_card_token_request[:card_number],
37
+ expiryYear: create_card_token_request[:expiry_year],
38
+ expiryMonth: create_card_token_request[:expiry_month],
39
+ cvv: create_card_token_request[:cvv]
40
+ }.compact
41
+ end
42
+
43
+ def fawry_merchant_code
44
+ Fawry.configuration.fawry_merchant_code || ENV.fetch('FAWRY_MERCHANT_CODE') { request_params[:merchant_code] }
45
+ end
46
+
47
+ def fawry_secure_key
48
+ Fawry.configuration.fawry_secure_key || ENV.fetch('FAWRY_SECURE_KEY') { request_params[:fawry_secure_key] }
49
+ end
50
+
51
+ def validate_card_token_params!
52
+ contract = Contracts::CreateCardTokenRequestContract.new.call(create_card_token_request)
53
+ raise InvalidFawryRequestError, contract.errors.to_h if contract.failure?
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'digest'
4
+
5
+ module Fawry
6
+ module Requests
7
+ module DeleteTokenRequest
8
+ def fire_delete_token_request
9
+ fawry_api_response = Connection.delete(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_delete_token_request
18
+ {
19
+ path: 'cards/cardToken',
20
+ params: {},
21
+ body: delete_token_request_transformed_params,
22
+ options: options
23
+ }
24
+ end
25
+
26
+ def request_params
27
+ @request_params = params
28
+ end
29
+
30
+ def delete_token_request_transformed_params
31
+ {
32
+ merchantCode: fawry_merchant_code,
33
+ customerProfileId: request_params[:customer_profile_id],
34
+ signature: delete_token_request_signature,
35
+ cardToken: request_params[:card_token]
36
+ }.compact
37
+ end
38
+
39
+ def fawry_merchant_code
40
+ Fawry.configuration.fawry_merchant_code || ENV.fetch('FAWRY_MERCHANT_CODE') { request_params[:merchant_code] }
41
+ end
42
+
43
+ def fawry_secure_key
44
+ Fawry.configuration.fawry_secure_key || ENV.fetch('FAWRY_SECURE_KEY') { request_params[:fawry_secure_key] }
45
+ end
46
+
47
+ def card_token
48
+ request_params[:card_token]
49
+ end
50
+
51
+ def validate_delete_token_params!
52
+ contract = Contracts::ListTokensRequestContract.new.call(request_params)
53
+ raise InvalidFawryRequestError, contract.errors.to_h if contract.failure?
54
+ end
55
+
56
+ def delete_token_request_signature
57
+ Digest::SHA256.hexdigest("#{fawry_merchant_code}#{request_params[:customer_profile_id]}"\
58
+ "#{card_token}#{fawry_secure_key}")
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,57 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'digest'
4
+
5
+ module Fawry
6
+ module Requests
7
+ module ListTokensRequest
8
+ def fire_list_tokens_request
9
+ fawry_api_response = Connection.get(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_list_tokens_request
18
+ {
19
+ path: 'cards/cardToken',
20
+ params: list_tokens_request_transformed_params,
21
+ body: {},
22
+ options: options
23
+ }
24
+ end
25
+
26
+ def request_params
27
+ @request_params = params
28
+ end
29
+
30
+ def list_tokens_request_transformed_params
31
+ {
32
+ merchantCode: fawry_merchant_code,
33
+ customerProfileId: request_params[:customer_profile_id],
34
+ signature: list_tokens_request_signature
35
+ }.compact
36
+ end
37
+
38
+ def fawry_merchant_code
39
+ Fawry.configuration.fawry_merchant_code || ENV.fetch('FAWRY_MERCHANT_CODE') { request_params[:merchant_code] }
40
+ end
41
+
42
+ def fawry_secure_key
43
+ Fawry.configuration.fawry_secure_key || ENV.fetch('FAWRY_SECURE_KEY') { request_params[:fawry_secure_key] }
44
+ end
45
+
46
+ def validate_list_tokens_params!
47
+ contract = Contracts::ListTokensRequestContract.new.call(request_params)
48
+ raise InvalidFawryRequestError, contract.errors.to_h if contract.failure?
49
+ end
50
+
51
+ def list_tokens_request_signature
52
+ Digest::SHA256.hexdigest("#{fawry_merchant_code}#{request_params[:customer_profile_id]}"\
53
+ "#{fawry_secure_key}")
54
+ end
55
+ end
56
+ end
57
+ end
@@ -16,7 +16,7 @@ module Fawry
16
16
 
17
17
  def build_payment_status_request
18
18
  {
19
- path: 'status',
19
+ path: 'payments/status',
20
20
  params: payment_status_request_transformed_params,
21
21
  body: {},
22
22
  options: options
@@ -36,11 +36,11 @@ module Fawry
36
36
  end
37
37
 
38
38
  def fawry_merchant_code
39
- ENV.fetch('FAWRY_MERCHANT_CODE') { request_params[:merchant_code] }
39
+ Fawry.configuration.fawry_merchant_code || ENV.fetch('FAWRY_MERCHANT_CODE') { request_params[:merchant_code] }
40
40
  end
41
41
 
42
42
  def fawry_secure_key
43
- ENV.fetch('FAWRY_SECURE_KEY') { request_params[:fawry_secure_key] }
43
+ Fawry.configuration.fawry_secure_key || ENV.fetch('FAWRY_SECURE_KEY') { request_params[:fawry_secure_key] }
44
44
  end
45
45
 
46
46
  def validate_payment_status_params!
@@ -16,7 +16,7 @@ module Fawry
16
16
 
17
17
  def build_refund_request
18
18
  {
19
- path: 'refund',
19
+ path: 'payments/refund',
20
20
  params: {},
21
21
  body: refund_request_transformed_params,
22
22
  options: options
@@ -38,11 +38,11 @@ module Fawry
38
38
  end
39
39
 
40
40
  def fawry_merchant_code
41
- ENV.fetch('FAWRY_MERCHANT_CODE') { request_params[:merchant_code] }
41
+ Fawry.configuration.fawry_merchant_code || ENV.fetch('FAWRY_MERCHANT_CODE') { request_params[:merchant_code] }
42
42
  end
43
43
 
44
44
  def fawry_secure_key
45
- ENV.fetch('FAWRY_SECURE_KEY') { request_params[:fawry_secure_key] }
45
+ Fawry.configuration.fawry_secure_key || ENV.fetch('FAWRY_SECURE_KEY') { request_params[:fawry_secure_key] }
46
46
  end
47
47
 
48
48
  def validate_refund_params!
data/lib/fawry/utils.rb CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
  module Fawry
4
4
  module Utils
5
+ TRUTH_VALUES = [true, 'true', '1', 't'].freeze
6
+
5
7
  # Adds keys from fawry API response as methods
6
8
  # on object instance that return the value
7
9
  # of each key
@@ -17,7 +19,7 @@ module Fawry
17
19
  # fawry_res.status_code => 200
18
20
  # fawry_res.reference_number => 1234567
19
21
  def enrich_object(fawry_params)
20
- fawry_params.keys.each do |key|
22
+ fawry_params.each_key do |key|
21
23
  method_name = key.to_s.split(/(?=[A-Z])/).map(&:downcase).join('_') # statusCode => status_code
22
24
  instance_variable_set("@#{method_name}", fawry_params[key])
23
25
  method_body = proc { instance_variable_get("@#{method_name}") }
data/lib/fawry/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Fawry
4
- VERSION = '1.0.0'
4
+ VERSION = '1.3.1'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fawry
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.3.1
5
5
  platform: ruby
6
6
  authors:
7
- - Amr Bakry
8
- autorequire:
7
+ - Amr El Bakry
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-12-26 00:00:00.000000000 Z
11
+ date: 2021-08-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: dry-validation
@@ -65,9 +65,6 @@ dependencies:
65
65
  - - "~>"
66
66
  - !ruby/object:Gem::Version
67
67
  version: '11.0'
68
- - - ">="
69
- - !ruby/object:Gem::Version
70
- version: 11.0.1
71
68
  type: :development
72
69
  prerelease: false
73
70
  version_requirements: !ruby/object:Gem::Requirement
@@ -75,23 +72,20 @@ dependencies:
75
72
  - - "~>"
76
73
  - !ruby/object:Gem::Version
77
74
  version: '11.0'
78
- - - ">="
79
- - !ruby/object:Gem::Version
80
- version: 11.0.1
81
75
  - !ruby/object:Gem::Dependency
82
76
  name: rake
83
77
  requirement: !ruby/object:Gem::Requirement
84
78
  requirements:
85
79
  - - "~>"
86
80
  - !ruby/object:Gem::Version
87
- version: '10.0'
81
+ version: '13.0'
88
82
  type: :development
89
83
  prerelease: false
90
84
  version_requirements: !ruby/object:Gem::Requirement
91
85
  requirements:
92
86
  - - "~>"
93
87
  - !ruby/object:Gem::Version
94
- version: '10.0'
88
+ version: '13.0'
95
89
  - !ruby/object:Gem::Dependency
96
90
  name: rspec
97
91
  requirement: !ruby/object:Gem::Requirement
@@ -126,58 +120,58 @@ dependencies:
126
120
  requirements:
127
121
  - - "~>"
128
122
  - !ruby/object:Gem::Version
129
- version: 0.76.0
123
+ version: '1.11'
130
124
  type: :development
131
125
  prerelease: false
132
126
  version_requirements: !ruby/object:Gem::Requirement
133
127
  requirements:
134
128
  - - "~>"
135
129
  - !ruby/object:Gem::Version
136
- version: 0.76.0
130
+ version: '1.11'
137
131
  - !ruby/object:Gem::Dependency
138
132
  name: webmock
139
133
  requirement: !ruby/object:Gem::Requirement
140
134
  requirements:
141
135
  - - "~>"
142
136
  - !ruby/object:Gem::Version
143
- version: '3.7'
144
- - - ">="
145
- - !ruby/object:Gem::Version
146
- version: 3.7.6
137
+ version: '3.12'
147
138
  type: :development
148
139
  prerelease: false
149
140
  version_requirements: !ruby/object:Gem::Requirement
150
141
  requirements:
151
142
  - - "~>"
152
143
  - !ruby/object:Gem::Version
153
- version: '3.7'
154
- - - ">="
155
- - !ruby/object:Gem::Version
156
- version: 3.7.6
157
- description:
144
+ version: '3.12'
145
+ description:
158
146
  email:
159
- - amrrbakry17@gmail.com
147
+ - amrr@hey.com
160
148
  executables: []
161
149
  extensions: []
162
150
  extra_rdoc_files: []
163
151
  files:
164
152
  - ".circleci/config.yml"
165
- - ".github/workflows/greetings.yml"
153
+ - ".github/workflows/ruby.yml"
166
154
  - ".gitignore"
167
155
  - ".rspec"
168
156
  - ".rubocop.yml"
157
+ - CHANGELOG.md
169
158
  - CODE_OF_CONDUCT.md
170
159
  - Gemfile
171
160
  - Gemfile.lock
172
161
  - LICENSE.txt
162
+ - README-ar.md
173
163
  - README.md
174
164
  - Rakefile
175
165
  - bin/console
176
166
  - bin/setup
177
167
  - fawry.gemspec
178
168
  - lib/fawry.rb
169
+ - lib/fawry/config.rb
179
170
  - lib/fawry/connection.rb
180
171
  - lib/fawry/contracts/charge_request_contract.rb
172
+ - lib/fawry/contracts/create_card_token_request_contract.rb
173
+ - lib/fawry/contracts/delete_token_request_contract.rb
174
+ - lib/fawry/contracts/list_tokens_request_contract.rb
181
175
  - lib/fawry/contracts/payment_status_request_contract.rb
182
176
  - lib/fawry/contracts/refund_request_contract.rb
183
177
  - lib/fawry/errors.rb
@@ -185,6 +179,9 @@ files:
185
179
  - lib/fawry/fawry_request.rb
186
180
  - lib/fawry/fawry_response.rb
187
181
  - lib/fawry/requests/charge_request.rb
182
+ - lib/fawry/requests/create_card_token_request.rb
183
+ - lib/fawry/requests/delete_token_request.rb
184
+ - lib/fawry/requests/list_tokens_request.rb
188
185
  - lib/fawry/requests/payment_status_request.rb
189
186
  - lib/fawry/requests/refund_request.rb
190
187
  - lib/fawry/utils.rb
@@ -195,13 +192,13 @@ licenses:
195
192
  metadata:
196
193
  homepage_uri: https://github.com/amrrbakry/fawry
197
194
  source_code_uri: https://github.com/amrrbakry/fawry
198
- post_install_message:
195
+ post_install_message:
199
196
  rdoc_options: []
200
197
  require_paths:
201
198
  - lib
202
199
  required_ruby_version: !ruby/object:Gem::Requirement
203
200
  requirements:
204
- - - "~>"
201
+ - - ">="
205
202
  - !ruby/object:Gem::Version
206
203
  version: '2.6'
207
204
  required_rubygems_version: !ruby/object:Gem::Requirement
@@ -210,8 +207,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
210
207
  - !ruby/object:Gem::Version
211
208
  version: '0'
212
209
  requirements: []
213
- rubygems_version: 3.0.1
214
- signing_key:
210
+ rubygems_version: 3.1.4
211
+ signing_key:
215
212
  specification_version: 4
216
213
  summary: A library to interface with Fawry's payment gateway API (charge, refund,
217
214
  payment status, service callback).