paymob_accept 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 +7 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/.travis.yml +6 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +7 -0
- data/Gemfile.lock +46 -0
- data/LICENSE.txt +21 -0
- data/README.md +116 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/paymob_accept/api/base.rb +129 -0
- data/lib/paymob_accept/api/charge.rb +43 -0
- data/lib/paymob_accept/api/client.rb +60 -0
- data/lib/paymob_accept/api/pay.rb +91 -0
- data/lib/paymob_accept/configuration.rb +14 -0
- data/lib/paymob_accept/errors/bad_gateway.rb +13 -0
- data/lib/paymob_accept/version.rb +3 -0
- data/lib/paymob_accept.rb +25 -0
- data/paymob.gemspec +31 -0
- metadata +95 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 519375883a2c35dd6406dc2aee7e471f3525905ded1faee4c286d9d4518014aa
|
4
|
+
data.tar.gz: dbb89c1765419b3114e0366302494097dae654d13fc9d0bdf20794420230b07e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0e9bb8812b46f509a9ccbb96bfc97a140726fb360e044980eec42bad30828575e457361ba5a61df608324021bdf58424815c7ef828dad7909322b20ffdaae3a4
|
7
|
+
data.tar.gz: 7d2811bd93ee7c9a43492c984fa8a5c237929b64ad1f3393bb528f24f489d666427fccff34d15d914f55002e8e52cbce7501b7efa1aad9006b03467fafde577d
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -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 hesham_magdy97@hotmail.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 [https://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: https://contributor-covenant.org
|
74
|
+
[version]: https://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
paymob_accept (0.1.0)
|
5
|
+
faraday
|
6
|
+
json-schema
|
7
|
+
|
8
|
+
GEM
|
9
|
+
remote: https://rubygems.org/
|
10
|
+
specs:
|
11
|
+
addressable (2.8.0)
|
12
|
+
public_suffix (>= 2.0.2, < 5.0)
|
13
|
+
diff-lcs (1.5.0)
|
14
|
+
faraday (2.5.2)
|
15
|
+
faraday-net_http (>= 2.0, < 3.1)
|
16
|
+
ruby2_keywords (>= 0.0.4)
|
17
|
+
faraday-net_http (3.0.0)
|
18
|
+
json-schema (3.0.0)
|
19
|
+
addressable (>= 2.8)
|
20
|
+
public_suffix (4.0.7)
|
21
|
+
rake (12.3.3)
|
22
|
+
rspec (3.11.0)
|
23
|
+
rspec-core (~> 3.11.0)
|
24
|
+
rspec-expectations (~> 3.11.0)
|
25
|
+
rspec-mocks (~> 3.11.0)
|
26
|
+
rspec-core (3.11.0)
|
27
|
+
rspec-support (~> 3.11.0)
|
28
|
+
rspec-expectations (3.11.0)
|
29
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
30
|
+
rspec-support (~> 3.11.0)
|
31
|
+
rspec-mocks (3.11.1)
|
32
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
33
|
+
rspec-support (~> 3.11.0)
|
34
|
+
rspec-support (3.11.0)
|
35
|
+
ruby2_keywords (0.0.5)
|
36
|
+
|
37
|
+
PLATFORMS
|
38
|
+
ruby
|
39
|
+
|
40
|
+
DEPENDENCIES
|
41
|
+
paymob_accept!
|
42
|
+
rake (~> 12.0)
|
43
|
+
rspec (~> 3.0)
|
44
|
+
|
45
|
+
BUNDLED WITH
|
46
|
+
2.1.4
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2022 Hesham Ghaith
|
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,116 @@
|
|
1
|
+
|
2
|
+
# PaymobAccept
|
3
|
+
|
4
|
+
`paymob_accept` is a Ruby gem created by [OneOrder](https://www.oneorder.net/) for integrating [Paymob](https://paymob.com/en) payment solutions with your Ruby application.
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
gem 'paymob_accept'
|
12
|
+
```
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
|
16
|
+
`$ bundle install`
|
17
|
+
|
18
|
+
Or install it yourself as:
|
19
|
+
|
20
|
+
`$ gem install paymob_accept`
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
### Configuration
|
25
|
+
|
26
|
+
Configure the gem with your `api_key`
|
27
|
+
|
28
|
+
```ruby
|
29
|
+
PaymobAccept.configure do |config|
|
30
|
+
config.api_key = YOUR_API_KEY
|
31
|
+
end
|
32
|
+
```
|
33
|
+
|
34
|
+
Also you can pass the api_key to the constructor when initializing your payment service
|
35
|
+
|
36
|
+
:raised_back_of_hand: You get your API_KEY from Settings -> Account info -> API Key in your Paymob portal
|
37
|
+
|
38
|
+
### Creating the payment service
|
39
|
+
|
40
|
+
When performing a payment we expect to have two hashes (customer, address)
|
41
|
+
|
42
|
+
- Customer schema
|
43
|
+
|
44
|
+
|
45
|
+
## Payment service
|
46
|
+
|
47
|
+
- Initialize your payment service
|
48
|
+
```ruby
|
49
|
+
service = PaymobAccept::Api::Pay.new(api_key: api_key)
|
50
|
+
```
|
51
|
+
|
52
|
+
- To charge:
|
53
|
+
```ruby
|
54
|
+
customer_data = {name: "test", email: "test@test.com", phone_number: "01000000000"}
|
55
|
+
address_data = {address_line1: "10 street name", address_line2: "apt x. floor x", region: "region", city: "Cairo", country: "EG"}
|
56
|
+
service.charge(customer: customer, address: address, integration_id: 'xxxxx', method: :online, iframe_id: 'xxxxx', amount_cents: 1000, amount_currency: 'EGP', order_id: order_id)
|
57
|
+
```
|
58
|
+
If order_id is not provided, an order is created then linked to this payment_intent
|
59
|
+
|
60
|
+
---
|
61
|
+
### Also, You can do it step by step
|
62
|
+
|
63
|
+
- Step #1 Get auth_token
|
64
|
+
```ruby
|
65
|
+
token = service.get_auth_token
|
66
|
+
```
|
67
|
+
|
68
|
+
- Step #2 Create_order
|
69
|
+
```ruby
|
70
|
+
service.create_order(auth_token: auth_token, amount_cents: 1000, amount_currency: 'EGP', items: [])
|
71
|
+
```
|
72
|
+
|
73
|
+
auth_token is optional if not passed, it will be automatically generated.
|
74
|
+
</br>
|
75
|
+
- Items are optional
|
76
|
+
|
77
|
+
```ruby
|
78
|
+
items = [{
|
79
|
+
"name": "xxxxxxx-1",
|
80
|
+
"amount_cents": "5000",
|
81
|
+
"description": "Smart Watch",
|
82
|
+
"quantity": "10"
|
83
|
+
}]
|
84
|
+
```
|
85
|
+
|
86
|
+
</br>
|
87
|
+
|
88
|
+
- Step #3 Create payment key
|
89
|
+
|
90
|
+
```ruby
|
91
|
+
generate_payment_intent(customer: customer, address: address, integration_id: "xxxxx", amount_cents: amount_cents, amount_currency: "EGP", iframe_id: "xxxxxx", order_id: "xxxxxx")
|
92
|
+
```
|
93
|
+
---
|
94
|
+
## Dealing with charges
|
95
|
+
- retrieve transaction `PaymobAccept::Api::Charge.new.charge(transaction_id: transaction_id)`
|
96
|
+
- refund transaction `PaymobAccept::Api::Charge.new.refund!(transaction_id: transaction_id, amount_cents: amount_cents)`
|
97
|
+
- void a transaction using `PaymobAccept::Api::Charge.new.void!(transaction_id: transaction_id)`
|
98
|
+
|
99
|
+
## Development
|
100
|
+
|
101
|
+
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.
|
102
|
+
|
103
|
+
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).
|
104
|
+
|
105
|
+
## Contributing
|
106
|
+
|
107
|
+
Bug reports and pull requests are welcome on [GitHub](https://github.com/HeshamMagdy97/paymob).
|
108
|
+
This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/HeshamMagdy97/paymob/blob/master/CODE_OF_CONDUCT.md).
|
109
|
+
|
110
|
+
## License
|
111
|
+
|
112
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
113
|
+
|
114
|
+
## Code of Conduct
|
115
|
+
|
116
|
+
Everyone interacting in the PaymobAccept project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/HeshamMagdy97/paymob/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "paymob"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,129 @@
|
|
1
|
+
module PaymobAccept
|
2
|
+
module Api
|
3
|
+
class Base
|
4
|
+
attr_reader :integration_id, :method, :customer, :address
|
5
|
+
attr_accessor :api_key, :iframe_id
|
6
|
+
|
7
|
+
def initialize(api_key: PaymobAccept.configuration.api_key)
|
8
|
+
@client = PaymobAccept::Api::Client.new
|
9
|
+
@api_key = api_key
|
10
|
+
@address = address
|
11
|
+
end
|
12
|
+
|
13
|
+
# STEP #!
|
14
|
+
def get_auth_token
|
15
|
+
response = @client.request('/auth/tokens', { api_key: api_key })
|
16
|
+
response['token']
|
17
|
+
end
|
18
|
+
|
19
|
+
# 2. Order Registration API
|
20
|
+
def create_order(amount_cents:, amount_currency: 'EGP', auth_token: get_auth_token,
|
21
|
+
delivery_needed: false, items: [])
|
22
|
+
body = {
|
23
|
+
"auth_token": auth_token,
|
24
|
+
"delivery_needed": delivery_needed,
|
25
|
+
"amount_cents": amount_cents.to_i,
|
26
|
+
"currency": amount_currency,
|
27
|
+
"items": items
|
28
|
+
}
|
29
|
+
@client.request('/ecommerce/orders', body)
|
30
|
+
end
|
31
|
+
|
32
|
+
def generate_payment_intent(customer:, address:, integration_id:, amount_cents:, amount_currency:, iframe_id: nil, order_id: nil, auth_token: get_auth_token)
|
33
|
+
if order_id.nil?
|
34
|
+
order = create_order(amount_cents: amount_cents, amount_currency: amount_currency)
|
35
|
+
order_id = order['id']
|
36
|
+
end
|
37
|
+
payment_token = generate_payment_key(auth_token: auth_token, customer: customer, address: address, order_id: order_id, amount_cents: amount_cents, amount_currency: amount_currency,
|
38
|
+
integration_id: integration_id)
|
39
|
+
|
40
|
+
format_bill_reference(payment_token, iframe_id)
|
41
|
+
end
|
42
|
+
|
43
|
+
# 3. Payment Key Request
|
44
|
+
|
45
|
+
private
|
46
|
+
|
47
|
+
def generate_payment_key(customer:, address:, amount_cents:, amount_currency:, integration_id:, order_id: nil, auth_token: get_auth_token)
|
48
|
+
body = {
|
49
|
+
"auth_token": auth_token,
|
50
|
+
"amount_cents": amount_cents.to_i,
|
51
|
+
"currency": amount_currency,
|
52
|
+
"expiration": 36_000,
|
53
|
+
"order_id": order_id, # Remote
|
54
|
+
"billing_data": {
|
55
|
+
"first_name": customer&.dig(:first_name) || customer&.dig(:name)&.split(/\s/, 2)&.first,
|
56
|
+
"last_name": customer&.dig(:last_name) || customer&.dig(:name)&.split(/\s/, 2)&.last,
|
57
|
+
"email": customer[:email],
|
58
|
+
"phone_number": customer&.dig(:phone_number),
|
59
|
+
"street": address&.dig(:address_line1) || 'NA',
|
60
|
+
"building": address&.dig(:address_line2) || 'NA',
|
61
|
+
"floor": address&.dig(:address_clarification) || 'NA',
|
62
|
+
"apartment": 'NA',
|
63
|
+
"postal_code": address&.dig(:postal_code) || 'NA',
|
64
|
+
"city": address&.dig(:region) || 'NA',
|
65
|
+
"state": address&.dig(:city) || 'NA',
|
66
|
+
"country": address&.dig(:country) || 'NA',
|
67
|
+
"shipping_method": 'PKG'
|
68
|
+
},
|
69
|
+
"integration_id": integration_id
|
70
|
+
}
|
71
|
+
body['token'] = customer[:cc_token] unless customer[:cc_token].nil?
|
72
|
+
|
73
|
+
response = @client.request('/acceptance/payment_keys', body)
|
74
|
+
|
75
|
+
response['token']
|
76
|
+
end
|
77
|
+
|
78
|
+
def format_bill_reference(payment_token, iframe_id)
|
79
|
+
iframe_id.nil? ? payment_token : "#{Api::Client::API_ENDPOINT}/acceptance/iframes/#{iframe_id}?payment_token=#{payment_token}"
|
80
|
+
end
|
81
|
+
|
82
|
+
def customer_validator(customer)
|
83
|
+
JSON::Validator.validate!(customer_schema, customer)
|
84
|
+
rescue JSON::Schema::ValidationError => e
|
85
|
+
raise ArgumentError, "Customer field has the following error: #{e.message}"
|
86
|
+
end
|
87
|
+
|
88
|
+
def address_validator(address)
|
89
|
+
JSON::Validator.validate!(address_schema, address)
|
90
|
+
rescue JSON::Schema::ValidationError => e
|
91
|
+
raise ArgumentError, "Address field has the following error: #{e.message}"
|
92
|
+
end
|
93
|
+
|
94
|
+
def customer_schema
|
95
|
+
{
|
96
|
+
"type": 'object',
|
97
|
+
"$schema": 'http://json-schema.org/draft-04/schema',
|
98
|
+
"properties": {
|
99
|
+
"name": { "type": 'string' },
|
100
|
+
"first_name": { "type": 'string' },
|
101
|
+
"last_name": { "type": 'string' },
|
102
|
+
"email": { "type": 'string' },
|
103
|
+
"phone_number": { "type": 'string' },
|
104
|
+
"cc_token": { "type": 'string' },
|
105
|
+
"wallet_mobile_number": { "type": 'string' }
|
106
|
+
},
|
107
|
+
"required": %w[email phone_number]
|
108
|
+
}
|
109
|
+
end
|
110
|
+
|
111
|
+
def address_schema
|
112
|
+
{
|
113
|
+
"type": 'object',
|
114
|
+
"$schema": 'http://json-schema.org/draft-04/schema',
|
115
|
+
"properties": {
|
116
|
+
"address_line1": { "type": 'string' },
|
117
|
+
"address_line2": { "type": 'string' },
|
118
|
+
"postal_code": { "type": 'string' },
|
119
|
+
"region": { "type": 'string' },
|
120
|
+
"city": { "type": 'string' },
|
121
|
+
"country": { "type": 'string' },
|
122
|
+
"address_clarification": { "type": 'string' }
|
123
|
+
},
|
124
|
+
"required": %w[address_line1 address_line2 region city country]
|
125
|
+
}
|
126
|
+
end
|
127
|
+
end
|
128
|
+
end
|
129
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
module PaymobAccept
|
2
|
+
module Api
|
3
|
+
class Charge < Base
|
4
|
+
attr_reader :transaction_id
|
5
|
+
|
6
|
+
def initialize(api_key: PaymobAccept.configuration.api_key)
|
7
|
+
super(api_key: api_key)
|
8
|
+
end
|
9
|
+
|
10
|
+
def charge(transaction_id:)
|
11
|
+
response = @client.get(endpoint: "/acceptance/transactions/#{transaction_id}", headers: auth_headers)
|
12
|
+
JSON.parse(response.body).to_h
|
13
|
+
end
|
14
|
+
|
15
|
+
def capture!(transaction_id:, amount_cents:)
|
16
|
+
body = {
|
17
|
+
auth_token: get_auth_token,
|
18
|
+
transaction_id: transaction_id,
|
19
|
+
amount_cents: amount_cents
|
20
|
+
}
|
21
|
+
@client.request('/acceptance/capture', body)
|
22
|
+
end
|
23
|
+
|
24
|
+
def void!(transaction_id:)
|
25
|
+
body = { auth_token: get_auth_token, transaction_id: transaction_id }
|
26
|
+
response = @client.request('/acceptance/void_refund/void', body)
|
27
|
+
['true', true].include? response['success']
|
28
|
+
end
|
29
|
+
|
30
|
+
def refund!(transaction_id:, amount_cents:)
|
31
|
+
body = { auth_token: get_auth_token, transaction_id: transaction_id, amount_cents: amount_cents }
|
32
|
+
response = @client.request('/acceptance/void_refund/refund', body)
|
33
|
+
['true', true].include? response['success']
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
def auth_headers
|
39
|
+
{ 'Authorization' => "Bearer #{get_auth_token}" }
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
module PaymobAccept
|
2
|
+
module Api
|
3
|
+
class Client
|
4
|
+
API_ENDPOINT = 'https://accept.paymobsolutions.com/api'.freeze
|
5
|
+
def initialize; end
|
6
|
+
|
7
|
+
def get(endpoint:, params: {}, headers: {})
|
8
|
+
response = Faraday.get(
|
9
|
+
"#{API_ENDPOINT}/#{endpoint}",
|
10
|
+
params,
|
11
|
+
headers
|
12
|
+
)
|
13
|
+
raise StandardError, "code: #{response.status}, response: #{response.body}" unless response.success?
|
14
|
+
|
15
|
+
response
|
16
|
+
end
|
17
|
+
|
18
|
+
def request(endpoint, body = {})
|
19
|
+
response = Faraday.post(
|
20
|
+
"#{API_ENDPOINT}/#{endpoint.gsub(%r{^/+}, '')}",
|
21
|
+
body.to_json,
|
22
|
+
'Content-Type' => 'application/json'
|
23
|
+
)
|
24
|
+
|
25
|
+
begin
|
26
|
+
parsed_body = JSON.parse(response.body).to_h
|
27
|
+
rescue StandardError => e
|
28
|
+
# Manually send the error to Sentry
|
29
|
+
end
|
30
|
+
|
31
|
+
unless response.success?
|
32
|
+
# TODO: Instead of setting the status to failed, this payment intent could be destroyed
|
33
|
+
|
34
|
+
message = parsed_body&.dig('message') || response.body || default_error_message
|
35
|
+
raise PaymobAccept::Errors::BadGateway.new(message: "code: #{response.status}, gateway response: #{message}")
|
36
|
+
end
|
37
|
+
|
38
|
+
handle_paymob_request_errors unless paymob_request_successful?(parsed_body)
|
39
|
+
|
40
|
+
parsed_body
|
41
|
+
end
|
42
|
+
|
43
|
+
def handle_paymob_request_errors
|
44
|
+
raise PaymobAccept::Errors::BadGateway.new(message: default_error_message)
|
45
|
+
end
|
46
|
+
|
47
|
+
def default_error_message
|
48
|
+
'Gateway could not handle your request properly. Please try again later.'
|
49
|
+
end
|
50
|
+
|
51
|
+
def paymob_request_successful?(response)
|
52
|
+
!response.key?('success') || (response.key?('success') && ([true,
|
53
|
+
'true'].include?(response['success']) || ([false,
|
54
|
+
'false'].include?(response['success']) && [
|
55
|
+
true, 'true'
|
56
|
+
].include?(response['pending']))))
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
@@ -0,0 +1,91 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module PaymobAccept
|
4
|
+
module Api
|
5
|
+
class Pay < Base
|
6
|
+
def initialize(api_key: PaymobAccept.configuration.api_key)
|
7
|
+
super(api_key: api_key)
|
8
|
+
end
|
9
|
+
|
10
|
+
def charge(customer:, address:, amount_cents:, integration_id:, method:, amount_currency: 'EGP', order_id: nil, iframe_id: nil, auth_token: get_auth_token)
|
11
|
+
raise ArgumentError, "unsupported payment method #{method}" unless %i[online moto kiosk cash auth
|
12
|
+
wallet].include? method
|
13
|
+
|
14
|
+
send("pay_#{method}".to_sym,
|
15
|
+
{ auth_token: auth_token, customer: customer, address: address, amount_cents: amount_cents, amount_currency: amount_currency,
|
16
|
+
integration_id: integration_id, iframe_id: iframe_id, order_id: order_id })
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def request_auth(customer:, address:, amount_cents:, amount_currency:, integration_id:, iframe_id:, order_id:, auth_token:)
|
22
|
+
generate_payment_intent(customer: customer, address: address, amount_cents: amount_cents, amount_currency: amount_currency,
|
23
|
+
integration_id: integration_id, iframe_id: iframe_id, order_id: order_id, auth_token: auth_token)
|
24
|
+
end
|
25
|
+
|
26
|
+
# Returns iFrame URL. The iframe will be prepoulated if the credit card token is provided
|
27
|
+
def pay_online(customer:, address:, amount_cents:, amount_currency:, integration_id:, iframe_id:, order_id:, auth_token:)
|
28
|
+
generate_payment_intent(customer: customer, address: address, amount_cents: amount_cents, amount_currency: amount_currency,
|
29
|
+
integration_id: integration_id, iframe_id: iframe_id, order_id: order_id, auth_token: auth_token)
|
30
|
+
end
|
31
|
+
|
32
|
+
def pay_moto(customer:, address:, amount_cents:, amount_currency:, integration_id:, iframe_id:, order_id:, auth_token:)
|
33
|
+
if customer[:cc_token].nil?
|
34
|
+
raise ArgumentError,
|
35
|
+
'You need to provide a credit card token for moto payments'
|
36
|
+
end
|
37
|
+
|
38
|
+
bill_reference = generate_payment_intent(customer: customer, address: address, amount_cents: amount_cents, amount_currency: amount_currency,
|
39
|
+
integration_id: integration_id, iframe_id: iframe_id, order_id: order_id, auth_token: auth_token)
|
40
|
+
body = {
|
41
|
+
"source": { "subtype": 'TOKEN', "identifier": cc_token },
|
42
|
+
"payment_token": bill_reference
|
43
|
+
}
|
44
|
+
@client.request('/acceptance/payments/pay', body)
|
45
|
+
end
|
46
|
+
|
47
|
+
def pay_wallet(customer:, address:, amount_cents:, amount_currency:, integration_id:, iframe_id:, order_id:, auth_token:)
|
48
|
+
if customer[:wallet_mobile_number].nil?
|
49
|
+
raise ArgumentError,
|
50
|
+
'You need to provide a mobile number for wallet payments'
|
51
|
+
end
|
52
|
+
|
53
|
+
bill_reference = generate_payment_intent(customer: customer, address: address, amount_cents: amount_cents, amount_currency: amount_currency,
|
54
|
+
integration_id: integration_id, iframe_id: iframe_id, order_id: order_id, auth_token: auth_token)
|
55
|
+
body = {
|
56
|
+
"source": { "subtype": 'WALLET', "identifier": wallet_mobile_number },
|
57
|
+
"payment_token": bill_reference
|
58
|
+
}
|
59
|
+
@client.request('/acceptance/payments/pay', body)
|
60
|
+
end
|
61
|
+
|
62
|
+
def pay_cash(customer:, address:, amount_cents:, amount_currency:, integration_id:, iframe_id:, order_id:, auth_token:)
|
63
|
+
if address.nil?
|
64
|
+
raise ArgumentError,
|
65
|
+
"Please provide a valid address in options. You must provide those keys: #{address_schema[:required]}"
|
66
|
+
end
|
67
|
+
|
68
|
+
address_validator(address)
|
69
|
+
|
70
|
+
bill_reference = generate_payment_intent(customer: customer, address: address, amount_cents: amount_cents, amount_currency: amount_currency,
|
71
|
+
integration_id: integration_id, iframe_id: iframe_id, order_id: order_id, auth_token: auth_token)
|
72
|
+
body = {
|
73
|
+
"source": { "subtype": 'CASH', "identifier": 'cash' },
|
74
|
+
"payment_token": bill_reference
|
75
|
+
}
|
76
|
+
@client.request('/acceptance/payments/pay', body)
|
77
|
+
end
|
78
|
+
|
79
|
+
def pay_kiosk(customer:, address:, amount_cents:, amount_currency:, integration_id:, iframe_id:, order_id:, auth_token:)
|
80
|
+
bill_reference = generate_payment_intent(customer: customer, address: address, amount_cents: amount_cents, amount_currency: amount_currency,
|
81
|
+
integration_id: integration_id, iframe_id: iframe_id, order_id: order_id, auth_token: auth_token)
|
82
|
+
body = {
|
83
|
+
"source": { "subtype": 'AGGREGATOR', "identifier": 'aggregator' },
|
84
|
+
"payment_token": bill_reference
|
85
|
+
}
|
86
|
+
|
87
|
+
@client.request('/acceptance/payments/pay', body)
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module PaymobAccept
|
2
|
+
module Errors
|
3
|
+
class BadGateway < StandardError
|
4
|
+
def initialize(message:)
|
5
|
+
super(
|
6
|
+
title: 'Bad Gateway',
|
7
|
+
status: 502,
|
8
|
+
detail: message || 'An error has occured communicating with the external gateway',
|
9
|
+
)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'paymob_accept'
|
2
|
+
require 'paymob_accept/api/base'
|
3
|
+
require 'paymob_accept/api/pay'
|
4
|
+
require 'paymob_accept/api/client'
|
5
|
+
require 'paymob_accept/api/charge'
|
6
|
+
require 'paymob_accept/configuration'
|
7
|
+
|
8
|
+
require 'json'
|
9
|
+
require 'faraday'
|
10
|
+
require 'faraday/net_http'
|
11
|
+
require 'paymob_accept/errors/bad_gateway'
|
12
|
+
require 'json-schema'
|
13
|
+
|
14
|
+
module PaymobAccept
|
15
|
+
class Error < StandardError; end
|
16
|
+
class << self
|
17
|
+
def configure
|
18
|
+
yield configuration
|
19
|
+
end
|
20
|
+
|
21
|
+
def configuration
|
22
|
+
@configuration ||= Configuration.new
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
data/paymob.gemspec
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
lib = File.expand_path('lib', __dir__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require_relative 'lib/paymob_accept/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = 'paymob_accept'
|
7
|
+
spec.version = PaymobAccept::VERSION
|
8
|
+
spec.authors = ['Hesham Magdy']
|
9
|
+
spec.email = ['hesham_magdy97@hotmail.com']
|
10
|
+
|
11
|
+
spec.summary = 'Easy integration of Paymob payment gateway'
|
12
|
+
spec.description = 'Paymob is a Ruby gem created by OneOrder tech team for integrating Paymob payment solutions with your Ruby application.'
|
13
|
+
spec.homepage = "https://github.com/oneorder-tech/paymob"
|
14
|
+
spec.license = 'MIT'
|
15
|
+
spec.homepage = 'https://github.com/oneorder-tech/paymob'
|
16
|
+
spec.required_ruby_version = Gem::Requirement.new('>= 2.6.0')
|
17
|
+
|
18
|
+
|
19
|
+
spec.metadata['homepage_uri'] = spec.homepage
|
20
|
+
spec.metadata['source_code_uri'] = 'https://github.com/oneorder-tech/paymob'
|
21
|
+
spec.metadata['changelog_uri'] = 'https://github.com/oneorder-tech/paymob'
|
22
|
+
|
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
|
+
spec.add_dependency 'faraday'
|
30
|
+
spec.add_dependency 'json-schema'
|
31
|
+
end
|
metadata
ADDED
@@ -0,0 +1,95 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: paymob_accept
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Hesham Magdy
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2022-08-18 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: faraday
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
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: json-schema
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description: Paymob is a Ruby gem created by OneOrder tech team for integrating Paymob
|
42
|
+
payment solutions with your Ruby application.
|
43
|
+
email:
|
44
|
+
- hesham_magdy97@hotmail.com
|
45
|
+
executables: []
|
46
|
+
extensions: []
|
47
|
+
extra_rdoc_files: []
|
48
|
+
files:
|
49
|
+
- ".gitignore"
|
50
|
+
- ".rspec"
|
51
|
+
- ".travis.yml"
|
52
|
+
- CODE_OF_CONDUCT.md
|
53
|
+
- Gemfile
|
54
|
+
- Gemfile.lock
|
55
|
+
- LICENSE.txt
|
56
|
+
- README.md
|
57
|
+
- Rakefile
|
58
|
+
- bin/console
|
59
|
+
- bin/setup
|
60
|
+
- lib/paymob_accept.rb
|
61
|
+
- lib/paymob_accept/api/base.rb
|
62
|
+
- lib/paymob_accept/api/charge.rb
|
63
|
+
- lib/paymob_accept/api/client.rb
|
64
|
+
- lib/paymob_accept/api/pay.rb
|
65
|
+
- lib/paymob_accept/configuration.rb
|
66
|
+
- lib/paymob_accept/errors/bad_gateway.rb
|
67
|
+
- lib/paymob_accept/version.rb
|
68
|
+
- paymob.gemspec
|
69
|
+
homepage: https://github.com/oneorder-tech/paymob
|
70
|
+
licenses:
|
71
|
+
- MIT
|
72
|
+
metadata:
|
73
|
+
homepage_uri: https://github.com/oneorder-tech/paymob
|
74
|
+
source_code_uri: https://github.com/oneorder-tech/paymob
|
75
|
+
changelog_uri: https://github.com/oneorder-tech/paymob
|
76
|
+
post_install_message:
|
77
|
+
rdoc_options: []
|
78
|
+
require_paths:
|
79
|
+
- lib
|
80
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
81
|
+
requirements:
|
82
|
+
- - ">="
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: 2.6.0
|
85
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
requirements: []
|
91
|
+
rubygems_version: 3.1.4
|
92
|
+
signing_key:
|
93
|
+
specification_version: 4
|
94
|
+
summary: Easy integration of Paymob payment gateway
|
95
|
+
test_files: []
|