payOS 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: 6bb1ea8be43cc547a8501508fa27eb6b0ff6362b4435057c6c4f2a68dc0f523f
4
+ data.tar.gz: 30e5e2a4c6b1677025061307e362dfd4556effc1a501ae1294511ac93f8f65f5
5
+ SHA512:
6
+ metadata.gz: 65b2c71bdf2feb5353a24ad9ff53f3d3b47464b7eade0fc8a3501189922ccb4b11df342ed085aef2de081c5950e0470760a52008471d09e64cb1757205d90c12
7
+ data.tar.gz: 7a718232722397c9573563096ba404046c5f7af01fba605000bb194ef178a0369c8de9f1b5a9611053f8c433ac1a2090f6ef1f959954a4684c2d9b61ad296a18
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,28 @@
1
+ AllCops:
2
+ TargetRubyVersion: 3.0
3
+
4
+ Style/StringLiterals:
5
+ EnforcedStyle: double_quotes
6
+
7
+ Style/StringLiteralsInInterpolation:
8
+ EnforcedStyle: double_quotes
9
+
10
+ Naming/FileName:
11
+ Exclude:
12
+ - 'lib/payOS.rb'
13
+ - 'spec/payOS_spec.rb'
14
+
15
+ Style/Documentation:
16
+ Enabled: false
17
+
18
+ Metrics/AbcSize:
19
+ Enabled: false
20
+
21
+ Metrics/MethodLength:
22
+ Enabled: false
23
+
24
+ Metrics/BlockLength:
25
+ Enabled: false
26
+
27
+ Gemspec/RequiredRubyVersion:
28
+ Enabled: false
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 3.3.6
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2024-12-25
4
+
5
+ - Initial release
@@ -0,0 +1,132 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our
6
+ community a harassment-free experience for everyone, regardless of age, body
7
+ size, visible or invisible disability, ethnicity, sex characteristics, gender
8
+ identity and expression, level of experience, education, socio-economic status,
9
+ nationality, personal appearance, race, caste, color, religion, or sexual
10
+ identity and orientation.
11
+
12
+ We pledge to act and interact in ways that contribute to an open, welcoming,
13
+ diverse, inclusive, and healthy community.
14
+
15
+ ## Our Standards
16
+
17
+ Examples of behavior that contributes to a positive environment for our
18
+ community include:
19
+
20
+ * Demonstrating empathy and kindness toward other people
21
+ * Being respectful of differing opinions, viewpoints, and experiences
22
+ * Giving and gracefully accepting constructive feedback
23
+ * Accepting responsibility and apologizing to those affected by our mistakes,
24
+ and learning from the experience
25
+ * Focusing on what is best not just for us as individuals, but for the overall
26
+ community
27
+
28
+ Examples of unacceptable behavior include:
29
+
30
+ * The use of sexualized language or imagery, and sexual attention or advances of
31
+ any kind
32
+ * Trolling, insulting or derogatory comments, and personal or political attacks
33
+ * Public or private harassment
34
+ * Publishing others' private information, such as a physical or email address,
35
+ without their explicit permission
36
+ * Other conduct which could reasonably be considered inappropriate in a
37
+ professional setting
38
+
39
+ ## Enforcement Responsibilities
40
+
41
+ Community leaders are responsible for clarifying and enforcing our standards of
42
+ acceptable behavior and will take appropriate and fair corrective action in
43
+ response to any behavior that they deem inappropriate, threatening, offensive,
44
+ or harmful.
45
+
46
+ Community leaders have the right and responsibility to remove, edit, or reject
47
+ comments, commits, code, wiki edits, issues, and other contributions that are
48
+ not aligned to this Code of Conduct, and will communicate reasons for moderation
49
+ decisions when appropriate.
50
+
51
+ ## Scope
52
+
53
+ This Code of Conduct applies within all community spaces, and also applies when
54
+ an individual is officially representing the community in public spaces.
55
+ Examples of representing our community include using an official email address,
56
+ posting via an official social media account, or acting as an appointed
57
+ representative at an online or offline event.
58
+
59
+ ## Enforcement
60
+
61
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
62
+ reported to the community leaders responsible for enforcement at
63
+ [INSERT CONTACT METHOD].
64
+ All complaints will be reviewed and investigated promptly and fairly.
65
+
66
+ All community leaders are obligated to respect the privacy and security of the
67
+ reporter of any incident.
68
+
69
+ ## Enforcement Guidelines
70
+
71
+ Community leaders will follow these Community Impact Guidelines in determining
72
+ the consequences for any action they deem in violation of this Code of Conduct:
73
+
74
+ ### 1. Correction
75
+
76
+ **Community Impact**: Use of inappropriate language or other behavior deemed
77
+ unprofessional or unwelcome in the community.
78
+
79
+ **Consequence**: A private, written warning from community leaders, providing
80
+ clarity around the nature of the violation and an explanation of why the
81
+ behavior was inappropriate. A public apology may be requested.
82
+
83
+ ### 2. Warning
84
+
85
+ **Community Impact**: A violation through a single incident or series of
86
+ actions.
87
+
88
+ **Consequence**: A warning with consequences for continued behavior. No
89
+ interaction with the people involved, including unsolicited interaction with
90
+ those enforcing the Code of Conduct, for a specified period of time. This
91
+ includes avoiding interactions in community spaces as well as external channels
92
+ like social media. Violating these terms may lead to a temporary or permanent
93
+ ban.
94
+
95
+ ### 3. Temporary Ban
96
+
97
+ **Community Impact**: A serious violation of community standards, including
98
+ sustained inappropriate behavior.
99
+
100
+ **Consequence**: A temporary ban from any sort of interaction or public
101
+ communication with the community for a specified period of time. No public or
102
+ private interaction with the people involved, including unsolicited interaction
103
+ with those enforcing the Code of Conduct, is allowed during this period.
104
+ Violating these terms may lead to a permanent ban.
105
+
106
+ ### 4. Permanent Ban
107
+
108
+ **Community Impact**: Demonstrating a pattern of violation of community
109
+ standards, including sustained inappropriate behavior, harassment of an
110
+ individual, or aggression toward or disparagement of classes of individuals.
111
+
112
+ **Consequence**: A permanent ban from any sort of public interaction within the
113
+ community.
114
+
115
+ ## Attribution
116
+
117
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
118
+ version 2.1, available at
119
+ [https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
120
+
121
+ Community Impact Guidelines were inspired by
122
+ [Mozilla's code of conduct enforcement ladder][Mozilla CoC].
123
+
124
+ For answers to common questions about this code of conduct, see the FAQ at
125
+ [https://www.contributor-covenant.org/faq][FAQ]. Translations are available at
126
+ [https://www.contributor-covenant.org/translations][translations].
127
+
128
+ [homepage]: https://www.contributor-covenant.org
129
+ [v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
130
+ [Mozilla CoC]: https://github.com/mozilla/diversity
131
+ [FAQ]: https://www.contributor-covenant.org/faq
132
+ [translations]: https://www.contributor-covenant.org/translations
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 ldhoax
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,77 @@
1
+ # PayOS
2
+
3
+ PayOS is a Ruby library that provides a simple and elegant way to integrate with the PayOS payment gateway services.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'payOS'
11
+ ```
12
+
13
+ Then run `bundle install` to install the gem.
14
+
15
+ ## Usage
16
+
17
+ Before using PayOS, you need to configure it with your credentials. You can do this in an initializer file (e.g., `config/initializers/payOS.rb` in Rails):
18
+
19
+ ```ruby
20
+ PayOS.configure do |config|
21
+ config.client_id = ENV['CLIENT_ID']
22
+ config.api_key = ENV['API_KEY']
23
+ config.checksum_secret = ENV['CHECKSUM_SECRET']
24
+ # Optional: config.partner_code = ENV['PARTNER_CODE']
25
+ end
26
+ ```
27
+
28
+ ### Creating a Payment URL
29
+
30
+ To create a payment URL, use the `create_payment_url` method:
31
+
32
+ ```ruby
33
+ payment_params = {
34
+ amount: 123,
35
+ cancelUrl: "https://your-domain.com/cancel-webhook-path",
36
+ description: "description",
37
+ orderCode: 4573928465,
38
+ returnUrl: "https://your-domain.com/success-webhook-path",
39
+ ... other params
40
+ }
41
+
42
+ payment_url = PayOS.create_payment_url(payment_params)
43
+ ```
44
+
45
+ ### Getting Payment Information
46
+
47
+ To retrieve information about a payment:
48
+
49
+ ```ruby
50
+ payment_info = PayOS.get_payment_info('4573928465')
51
+ ```
52
+
53
+ ### Canceling a Payment
54
+
55
+ To cancel a payment:
56
+
57
+ ```ruby
58
+ PayOS.cancel_payment('4573928465')
59
+ ```
60
+
61
+ ## Development
62
+
63
+ 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.
64
+
65
+ 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 the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
66
+
67
+ ## Contributing
68
+
69
+ Bug reports and pull requests are welcome on GitHub at https://github.com/ldhoax/payOS. 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/ldhoax/payOS/blob/master/CODE_OF_CONDUCT.md).
70
+
71
+ ## License
72
+
73
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
74
+
75
+ ## Code of Conduct
76
+
77
+ Everyone interacting in the PayOS project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/payOS/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
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
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PayOS
4
+ class Client
5
+ def initialize(config)
6
+ @config = config
7
+ @http_client = Faraday.new(url: BASE_URL) do |faraday|
8
+ # Set default headers
9
+ faraday.headers["Content-Type"] = "application/json"
10
+ faraday.headers["x-client-id"] = config.client_id
11
+ faraday.headers["x-api-key"] = config.api_key
12
+ faraday.headers["x-partner-code"] = config.partner_code
13
+
14
+ # Add middleware for JSON parsing
15
+ faraday.request :json
16
+ faraday.response :json
17
+
18
+ # Add middleware for logging (optional)
19
+ # faraday.response :logger if config.debug_mode
20
+
21
+ # Use default adapter
22
+ faraday.adapter Faraday.default_adapter
23
+ end
24
+ end
25
+
26
+ def post(path, payload)
27
+ response = @http_client.post(path) do |req|
28
+ req.body = payload.to_json
29
+ end
30
+
31
+ handle_response(response)
32
+ end
33
+
34
+ def get(path)
35
+ response = @http_client.get(path)
36
+ handle_response(response)
37
+ end
38
+
39
+ private
40
+
41
+ def handle_response(response)
42
+ case response.status
43
+ when 200
44
+ response_obj = Models::Response.new(response.body, @config.checksum_secret)
45
+ response_obj.verify_signature!
46
+ response_obj
47
+ when 429
48
+ raise RateLimitError, "Rate limit exceeded. Please try again later."
49
+ else
50
+ raise APIError, "API request failed: #{response.body}"
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PayOS
4
+ class Configuration
5
+ attr_accessor :client_id, :api_key, :checksum_secret, :partner_code
6
+
7
+ def initialize
8
+ @client_id = nil
9
+ @api_key = nil
10
+ @checksum_secret = nil
11
+ @partner_code = nil
12
+ end
13
+
14
+ def validate!
15
+ raise Error, "client_id is required" if client_id.nil?
16
+ raise Error, "api_key is required" if api_key.nil?
17
+ raise Error, "checksum_secret is required" if checksum_secret.nil?
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PayOS
4
+ BASE_URL = "https://api-merchant.payos.vn"
5
+ API_VERSION = "v2"
6
+ PAYMENT_URL_PATH = "payment-requests"
7
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PayOS
4
+ class Error < StandardError; end
5
+ class SignatureVerificationError < Error; end
6
+ class RateLimitError < Error; end
7
+ class APIError < Error; end
8
+ end
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "openssl"
4
+
5
+ module PayOS
6
+ module Models
7
+ class Response
8
+ attr_reader :code, :desc, :data, :signature, :raw_response
9
+
10
+ def initialize(response_body, checksum_secret)
11
+ @raw_response = response_body
12
+ @code = response_body["code"]
13
+ @desc = response_body["desc"]
14
+ @data = response_body["data"]
15
+ @signature = response_body.dig("data", "signature")
16
+ @checksum_secret = checksum_secret
17
+ end
18
+
19
+ def success?
20
+ code == "00"
21
+ end
22
+
23
+ def verify_signature!
24
+ return true if @data.nil? || @data["signature"].nil?
25
+
26
+ # Remove signature from data before verification
27
+ data_without_signature = @data.reject { |k, _| k == "signature" }
28
+
29
+ # Sort parameters alphabetically and create string to verify
30
+ string_to_verify = data_without_signature.sort.to_h
31
+ .map { |k, v| "#{k}=#{v}" }
32
+ .join("&")
33
+
34
+ # Generate signature
35
+ expected_signature = OpenSSL::HMAC.hexdigest(
36
+ "SHA256",
37
+ @checksum_secret,
38
+ string_to_verify
39
+ )
40
+
41
+ return true if expected_signature == @signature
42
+
43
+ raise SignatureVerificationError,
44
+ "Invalid signature!"
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../constants"
4
+ require_relative "../utils/signature"
5
+
6
+ module PayOS
7
+ module Services
8
+ class PaymentUrl
9
+ def initialize(client)
10
+ @client = client
11
+ end
12
+
13
+ def create(params)
14
+ @client.post("#{PayOS::API_VERSION}/#{PayOS::PAYMENT_URL_PATH}", params_with_signature(params))
15
+ end
16
+
17
+ def get_info(payment_url_id)
18
+ @client.get("#{PayOS::API_VERSION}/#{PayOS::PAYMENT_URL_PATH}/#{payment_url_id}")
19
+ end
20
+
21
+ def cancel(payment_url_id)
22
+ @client.post("#{PayOS::API_VERSION}/#{PayOS::PAYMENT_URL_PATH}/#{payment_url_id}/cancel", {})
23
+ end
24
+
25
+ private
26
+
27
+ def params_with_signature(params)
28
+ # Generate signature from the formatted params
29
+ formatted_params = params.sort_by { |key, _| key.to_s }.map do |key, value|
30
+ formatted_key = key.to_s.split("_").map.with_index do |word, i|
31
+ i.zero? ? word : word.capitalize
32
+ end.join
33
+ [formatted_key, value]
34
+ end.to_h
35
+
36
+ formatted_params["signature"] = Utils::Signature.generate(formatted_params.map do |k, v|
37
+ "#{k}=#{v}"
38
+ end.join("&"), PayOS.configuration.checksum_secret)
39
+
40
+ formatted_params
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PayOS
4
+ module Utils
5
+ class Signature
6
+ def self.generate(data, secret_key)
7
+ OpenSSL::HMAC.hexdigest("SHA256", secret_key, data)
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PayOS
4
+ VERSION = "0.1.0"
5
+ end
data/lib/payOS.rb ADDED
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "faraday"
4
+ require "json"
5
+
6
+ require_relative "payOS/version"
7
+ require_relative "payOS/configuration"
8
+ require_relative "payOS/errors"
9
+ require_relative "payOS/models/response"
10
+ require_relative "payOS/client"
11
+ require_relative "payOS/services/payment_url"
12
+
13
+ module PayOS
14
+ class Error < StandardError; end
15
+
16
+ class << self
17
+ attr_accessor :configuration
18
+
19
+ def client
20
+ @client ||= Client.new(configuration)
21
+ end
22
+
23
+ def payment_service
24
+ @payment_service ||= Services::PaymentUrl.new(client)
25
+ end
26
+ end
27
+
28
+ def self.configure
29
+ self.configuration ||= Configuration.new
30
+ yield(configuration) if block_given?
31
+ configuration.validate!
32
+ end
33
+
34
+ def self.create_payment_url(params)
35
+ payment_service.create(params)
36
+ end
37
+
38
+ def self.get_payment_info(payment_url_id)
39
+ payment_service.get_info(payment_url_id)
40
+ end
41
+
42
+ def self.cancel_payment(payment_url_id)
43
+ payment_service.cancel(payment_url_id)
44
+ end
45
+ end
data/sig/payOS.rbs ADDED
@@ -0,0 +1,4 @@
1
+ module PayOS
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: payOS
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - ldhoax
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-12-31 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: '2.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: json
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.0'
41
+ description: A Ruby library for integrating with PayOS payment services
42
+ email:
43
+ - leduchoabatr12@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".rspec"
49
+ - ".rubocop.yml"
50
+ - ".ruby-version"
51
+ - CHANGELOG.md
52
+ - CODE_OF_CONDUCT.md
53
+ - LICENSE.txt
54
+ - README.md
55
+ - Rakefile
56
+ - lib/payOS.rb
57
+ - lib/payOS/client.rb
58
+ - lib/payOS/configuration.rb
59
+ - lib/payOS/constants.rb
60
+ - lib/payOS/errors.rb
61
+ - lib/payOS/models/response.rb
62
+ - lib/payOS/services/payment_url.rb
63
+ - lib/payOS/utils/signature.rb
64
+ - lib/payOS/version.rb
65
+ - sig/payOS.rbs
66
+ homepage: https://github.com/ldhoax/payOS
67
+ licenses:
68
+ - MIT
69
+ metadata:
70
+ homepage_uri: https://github.com/ldhoax/payOS
71
+ source_code_uri: https://github.com/ldhoax/payOS
72
+ changelog_uri: https://github.com/ldhoax/payOS/blob/master/CHANGELOG.md
73
+ post_install_message:
74
+ rdoc_options: []
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: 2.6.0
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ requirements: []
88
+ rubygems_version: 3.5.22
89
+ signing_key:
90
+ specification_version: 4
91
+ summary: Ruby SDK for PayOS payment gateway
92
+ test_files: []