chip_rails 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 0430e685730cb4ebb30a639f2c2bcd0d0b18472204c8229157e166aaef9458e7
4
+ data.tar.gz: ff6e71d75f7ad92e341cb186bd07ce6802013da4504017d45ed2b9d2c853b8db
5
+ SHA512:
6
+ metadata.gz: 4fb830ef5fb17d6b3c2ead1296fc2dc507c98a05e591507433ba41d61a5bb969114fb2e0ae8f8f932c1953d1c45eef3c7ad926dd7d24fe18344c8839869f408d
7
+ data.tar.gz: 27702590f85753773ddc39198f14b17c7325096f45486bf6148a149ac3095be8af3b562557a8df76a32e2ea08cbf3ec8ccf5cac59993ffd956822bf08451f46f
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ ruby-3.2.2
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2024-05-13
4
+
5
+ - Initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at kennedyweesupitang@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 Kennedy Wee
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,42 @@
1
+ # ChipRails
2
+
3
+ This is the unofficial Ruby gem for Chip In Asia payment gateway. Specifically designed to work with Ruby on Rails
4
+
5
+ ## Installation
6
+
7
+ Install the gem and add to the application's Gemfile by executing:
8
+
9
+ bundle add chip_rails
10
+
11
+ If bundler is not being used to manage dependencies, install the gem by executing:
12
+
13
+ gem install chip_rails
14
+
15
+ ## Usage
16
+
17
+ Add this file to your rails project at config/initializer/chip_rails.rb:
18
+
19
+ ChipRails.configure do |config|
20
+ config.api_key = ENV['CHIP_API_KEY']
21
+ config.brand_id = ENV['CHIP_BRAND_ID']
22
+ config.webhook_key = ENV['CHIP_WEBHOOK_KEY']
23
+ end
24
+
25
+
26
+ ## Development
27
+
28
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
29
+
30
+ 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).
31
+
32
+ ## Contributing
33
+
34
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/chip_rails. 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/[USERNAME]/chip_rails/blob/main/CODE_OF_CONDUCT.md).
35
+
36
+ ## License
37
+
38
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
39
+
40
+ ## Code of Conduct
41
+
42
+ Everyone interacting in the ChipRails project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/chip_rails/blob/main/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ task default: %i[]
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/chip_rails/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "chip_rails"
7
+ spec.version = ChipRails::VERSION
8
+ spec.authors = ["Kennedy Wee"]
9
+ spec.email = ["kennedyweesupitang@gmail.com"]
10
+
11
+ spec.summary = "Integrate Chip-in-Asia payment gateway with Ruby on Rails applications."
12
+ spec.homepage = "https://github.com/kennedywee/chip_rails"
13
+ spec.license = "MIT"
14
+ spec.required_ruby_version = ">= 2.6.0"
15
+
16
+ spec.metadata["homepage_uri"] = spec.homepage
17
+ spec.metadata["source_code_uri"] = "https://github.com/kennedywee/chip_rails"
18
+ spec.metadata["changelog_uri"] = "https://github.com/kennedywee/chip_rails/blob/main/CHANGELOG.md"
19
+
20
+ # Specify which files should be added to the gem when it is released.
21
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
22
+ spec.files = Dir.chdir(__dir__) do
23
+ `git ls-files -z`.split("\x0").reject do |f|
24
+ (File.expand_path(f) == __FILE__) ||
25
+ f.start_with?(*%w[bin/ test/ spec/ features/ .git appveyor Gemfile])
26
+ end
27
+ end
28
+ spec.bindir = "exe"
29
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
30
+ spec.require_paths = ["lib"]
31
+
32
+ spec.add_dependency 'net-http', '~> 0.4'
33
+ spec.add_dependency 'uri', '~> 0.13'
34
+ spec.add_dependency 'json', '~> 2.7'
35
+ spec.add_dependency 'ostruct', '~> 0.6'
36
+ spec.add_dependency 'openssl', '~> 3.2'
37
+ spec.add_dependency 'dotenv', '~> 2.8'
38
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ChipRails
4
+ VERSION = "0.2.0"
5
+ end
data/lib/chip_rails.rb ADDED
@@ -0,0 +1,178 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'net/http'
4
+ require 'uri'
5
+ require 'json'
6
+ require 'ostruct'
7
+ require 'openssl'
8
+
9
+ require 'dotenv'
10
+ Dotenv.load
11
+
12
+ module ChipRails
13
+ class Configuration
14
+ attr_accessor :webhook_key, :api_key, :brand_id, :base_url
15
+
16
+ def initialize
17
+ @webhook_key = ENV['CHIP_WEBHOOOK_KEY']
18
+ @api_key = ENV['CHIP_API_KEY']
19
+ @brand_id = ENV['CHIP_BRAND_ID']
20
+ @base_url = 'https://gate.chip-in.asia/api/v1/'
21
+ end
22
+
23
+ def validate!
24
+ raise 'ChipRails configuration: webhook_key is missing' unless @webhook_key
25
+ raise 'ChipRails configuration: api_key is missing' unless @api_key
26
+ raise 'ChipRails configuration: brand_id is missing' unless @brand_id
27
+ end
28
+ end
29
+ end
30
+
31
+ module ChipRails
32
+ class << self
33
+ attr_accessor :configuration
34
+
35
+ def configure
36
+ self.configuration ||= Configuration.new
37
+ yield(configuration)
38
+ configuration.validate!
39
+ config_methods
40
+ end
41
+
42
+ def config_methods
43
+ Configuration.instance_methods(false).each do |attribute|
44
+ define_singleton_method(attribute) do
45
+ configuration.send(attribute)
46
+ end
47
+ end
48
+ end
49
+
50
+ def make_request(method, path, body: nil)
51
+ uri = URI(configuration.base_url + path)
52
+
53
+ case method
54
+ when :get
55
+ request = Net::HTTP::Get.new(uri, default_headers)
56
+ when :post
57
+ request = Net::HTTP::Post.new(uri, default_headers)
58
+ request.body = body.to_json
59
+ else
60
+ raise ArgumentError, "Unsupported HTTP method: #{method}"
61
+ end
62
+
63
+ response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == 'https') do |http|
64
+ http.request(request)
65
+ end
66
+
67
+ handle_response(response)
68
+ end
69
+
70
+ def verify_signature(data, signature, public_key)
71
+ decoded_signature = Base64.decode64(signature)
72
+ rsa_public = OpenSSL::PKey::RSA.new(public_key)
73
+ rsa_public.verify(OpenSSL::Digest.new('SHA256'), decoded_signature, data)
74
+ end
75
+
76
+ private
77
+ def handle_response(response)
78
+ case response
79
+ when Net::HTTPSuccess
80
+ begin
81
+ JSON.parse(response.body, object_class: OpenStruct)
82
+ rescue JSON::ParserError
83
+ OpenStruct.new(error: 'Invalid JSON response')
84
+ end
85
+ else
86
+ begin
87
+ error_details = JSON.parse(response.body)
88
+ OpenStruct.new(error: error_details, code: response.code)
89
+ rescue JSON::ParserError
90
+ OpenStruct.new(error: response.message, code: response.code)
91
+ end
92
+ end
93
+ end
94
+
95
+ def default_headers
96
+ {
97
+ 'Content-Type' => 'application/json',
98
+ 'Authorization' => "Bearer #{configuration.api_key}"
99
+ }
100
+ end
101
+ end
102
+
103
+ class Billing
104
+ def self.create(billing_details)
105
+ ChipRails.make_request(:post, 'billing_templates/', body: billing_details)
106
+ end
107
+
108
+ def self.retrieve(billing_id)
109
+ ChipRails.make_request(:get, "billing_templates/#{billing_id}/")
110
+ end
111
+
112
+ def self.list
113
+ ChipRails.make_request(:get, 'billing_templates/')
114
+ end
115
+
116
+ def self.add_subscriber(billing_id, subscriber_details)
117
+ ChipRails.make_request(:post, "billing_templates/#{billing_id}/add_subscriber/", body: subscriber_details)
118
+ end
119
+
120
+ def self.list_client(billing_id)
121
+ ChipRails.make_request(:get, "billing_templates/#{billing_id}/clients/")
122
+ end
123
+
124
+ def self.retrieve_billing_template_client(billing_id, billing_template_client_id)
125
+ ChipRails.make_request(:get, "billing_templates/#{billing_id}/clients/#{billing_template_client_id}/")
126
+ end
127
+ end
128
+
129
+ class Client
130
+ def self.list
131
+ ChipRails.make_request(:get, 'clients/')
132
+ end
133
+
134
+ def self.list_find(params)
135
+ ChipRails.make_request(:get, "clients/?q=#{params}")
136
+ end
137
+
138
+ def self.find_by_email(params)
139
+ ChipRails.make_request(:get, "clients/?q=#{params}")
140
+ end
141
+
142
+ def self.create(client_details)
143
+ ChipRails.make_request(:post, 'clients/', body: client_details)
144
+ end
145
+
146
+ def self.retrieve(client_id)
147
+ ChipRails.make_request(:get, "clients/#{client_id}/")
148
+ end
149
+ end
150
+
151
+ class Purchase
152
+ def self.create(purchase_details)
153
+ ChipRails.make_request(:post, 'purchases/', body: purchase_details)
154
+ end
155
+
156
+ def self.retrieve(purchase_id)
157
+ ChipRails.make_request(:get, "purchases/#{purchase_id}/")
158
+ end
159
+ end
160
+
161
+ class Webhook
162
+ def self.create
163
+ ChipRails.make_request(:post, 'webhooks/', body: webhook_details)
164
+ end
165
+
166
+ def self.retrieve(webhook_id)
167
+ ChipRails.make_request(:get, "webhooks/#{webhook_id}")
168
+ end
169
+ end
170
+
171
+ class PublicKey
172
+ def self.retrieve
173
+ ChipRails.make_request(:get, 'public_key/')
174
+ end
175
+ end
176
+
177
+ class Error < StandardError; end
178
+ end
@@ -0,0 +1,4 @@
1
+ module ChipRails
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,140 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: chip_rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Kennedy Wee
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-06-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: net-http
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.4'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.4'
27
+ - !ruby/object:Gem::Dependency
28
+ name: uri
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.13'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.13'
41
+ - !ruby/object:Gem::Dependency
42
+ name: json
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2.7'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2.7'
55
+ - !ruby/object:Gem::Dependency
56
+ name: ostruct
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.6'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.6'
69
+ - !ruby/object:Gem::Dependency
70
+ name: openssl
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.2'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.2'
83
+ - !ruby/object:Gem::Dependency
84
+ name: dotenv
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '2.8'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '2.8'
97
+ description:
98
+ email:
99
+ - kennedyweesupitang@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".ruby-version"
105
+ - CHANGELOG.md
106
+ - CODE_OF_CONDUCT.md
107
+ - LICENSE.txt
108
+ - README.md
109
+ - Rakefile
110
+ - chip_rails.gemspec
111
+ - lib/chip_rails.rb
112
+ - lib/chip_rails/version.rb
113
+ - sig/chip_rails.rbs
114
+ homepage: https://github.com/kennedywee/chip_rails
115
+ licenses:
116
+ - MIT
117
+ metadata:
118
+ homepage_uri: https://github.com/kennedywee/chip_rails
119
+ source_code_uri: https://github.com/kennedywee/chip_rails
120
+ changelog_uri: https://github.com/kennedywee/chip_rails/blob/main/CHANGELOG.md
121
+ post_install_message:
122
+ rdoc_options: []
123
+ require_paths:
124
+ - lib
125
+ required_ruby_version: !ruby/object:Gem::Requirement
126
+ requirements:
127
+ - - ">="
128
+ - !ruby/object:Gem::Version
129
+ version: 2.6.0
130
+ required_rubygems_version: !ruby/object:Gem::Requirement
131
+ requirements:
132
+ - - ">="
133
+ - !ruby/object:Gem::Version
134
+ version: '0'
135
+ requirements: []
136
+ rubygems_version: 3.5.10
137
+ signing_key:
138
+ specification_version: 4
139
+ summary: Integrate Chip-in-Asia payment gateway with Ruby on Rails applications.
140
+ test_files: []