Paychangu 0.2.4

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: c4064f2c7ef54302ac752392c02d19418342df8daf1fa8297b1945d131dcd7d6
4
+ data.tar.gz: 65025fdbdd67a02ea5609f6b0a168549d97153ac8a20a2af387595a9cadaf577
5
+ SHA512:
6
+ metadata.gz: 239500d3f0413df60d0ad412bf0cf0f4db40f5a853ac3546e5823331431cd6322a7268c7386b82e65ba980002ec68dc1a78b1e472025032a74c0f01af02053d9
7
+ data.tar.gz: 8c5df35c4d2593384ee60b6e970284e4058a137448393a99225ec33228e66049d16fa432fa736a5478a98625feb225f0f71856309555fdc7fb289d85c7c94e47
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,32 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.6
3
+ NewCops: enable
4
+ SuggestExtensions: false
5
+
6
+ Style/StringLiterals:
7
+ Enabled: true
8
+ EnforcedStyle: double_quotes
9
+
10
+ Style/StringLiteralsInInterpolation:
11
+ Enabled: true
12
+ EnforcedStyle: double_quotes
13
+
14
+ Layout/LineLength:
15
+ Enabled: true
16
+ Max: 260
17
+
18
+ Metrics/MethodLength:
19
+ Enabled: true
20
+ Max: 30
21
+
22
+ Metrics/BlockLength:
23
+ Enabled: true
24
+ Max: 60
25
+
26
+ Metrics/ClassLength:
27
+ Enabled: true
28
+ Max: 150
29
+
30
+ Metrics/AbcSize:
31
+ Enabled: true
32
+ Max: 45
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2023-12-12
4
+
5
+ - Initial release
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Danny Simfukwe
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,108 @@
1
+ # Paychangu Rails Gem
2
+
3
+ ## Installation
4
+
5
+ Install the gem and add to the application's Gemfile by executing:
6
+
7
+ $ bundle add paychangu
8
+
9
+ If bundler is not being used to manage dependencies, install the gem by executing:
10
+
11
+ $ gem install paychangu
12
+
13
+ ## Usage
14
+
15
+ Create an initializer in your application's `config > initializers` folder, for example `paychangu.rb` and paste the following
16
+
17
+ ```
18
+ require "paychangu"
19
+ ```
20
+
21
+ Then use it in your application like below with your Paychangu secret key, you can get this key in your Paychangu account settings
22
+
23
+ `paychangu = Paychangu::Payment.new("sec-test-SDsYTCSh...")`
24
+
25
+ #### Creating a Payment Link
26
+
27
+ ```
28
+ payload = {
29
+ amount: "50000",
30
+ currency: "MWK",
31
+ email: "test@example.com",
32
+ first_name: "Danny",
33
+ last_name: "Simfukwe",
34
+ callback_url: "https://webhook.site/9d0b00ba-9a69-44fa-a43d-a82c33c36fdc",
35
+ return_url: "https://webhook.site",
36
+ tx_ref: SecureRandom.hex(10),
37
+ title: "Title of payment",
38
+ description: "Description of payment",
39
+ logo: "https://assets.piedpiper.com/logo.png"
40
+ }
41
+ ```
42
+
43
+ `link = paychangu.create_payment_link(payload)`
44
+
45
+ #### Creating a Virtual Card
46
+
47
+ ```
48
+ card_payload = {
49
+ amount: "500",
50
+ currency: "USD",
51
+ first_name: "Danny",
52
+ last_name: "Simfukwe",
53
+ callback_url: "https://webhook.site/9d0b00ba-9a69-44fa-a43d-a82c33c36fdc"
54
+ }
55
+ ```
56
+
57
+ `card = paychangu.create_virtual_card(card_payload)`
58
+
59
+ #### Funding a Virtual Card
60
+
61
+ ```
62
+ fund_card_payload = {
63
+ amount: "50000",
64
+ card_hash: card[:card_hash],
65
+ }
66
+ ```
67
+
68
+ `paychangu.fund_card(fund_card_payload)`
69
+
70
+ #### Withdrawing from a Virtual Card
71
+
72
+ `withdraw = paychangu.withdraw_card_funds(fund_card_payload)`
73
+
74
+ #### Buying Airtime
75
+
76
+ First get all operators
77
+
78
+ ```
79
+ operators = paychangu.airtime_operators
80
+
81
+ ```
82
+
83
+ Then use the operator's ID to buy Airtime
84
+
85
+ ```
86
+ airtime_payment_payload = {
87
+ operator: "123",
88
+ amount: "300",
89
+ phone: "0900000000",
90
+ callback_url: "https://webhook.site/9d0b00ba-9a69-44fa-a43d-a82c33c36fdc"
91
+ }
92
+
93
+ paychangu.airtime_payment(airtime_payment_payload)
94
+ ```
95
+
96
+ ## Development
97
+
98
+ 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.
99
+
100
+ 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).
101
+
102
+ ## Contributing
103
+
104
+ Bug reports and pull requests are welcome on GitHub at https://github.com/dannysimfukwe/paychangugem.
105
+
106
+ ## License
107
+
108
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
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,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Paychangu
4
+ VERSION = "0.2.4"
5
+ end
data/lib/paychangu.rb ADDED
@@ -0,0 +1,185 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "uri"
4
+ require "json"
5
+ require "net/http"
6
+ require "securerandom"
7
+
8
+ require_relative "paychangu/version"
9
+
10
+ module Paychangu
11
+ # Main Payemnt Class
12
+ class Payment
13
+ API_URL = URI("https://api.paychangu.com/").freeze
14
+ SUPPORTED_CURRENCIES = %w[MWK NGN ZAR GBP USD ZMW].freeze
15
+ SUPPORTED_CARD_CURRENCIES = %w[USD].freeze
16
+
17
+ API_ENDPOINTS = {
18
+ payment: "payment",
19
+ create_card: "virtual_card/create",
20
+ fund_card: "virtual_card/fund",
21
+ withdraw: "virtual_card/withdraw",
22
+ get_operators: "bill_payment/get-operators",
23
+ airtime_payment: "bill_payment/create",
24
+ verify_payment: "verify-payment"
25
+ }.freeze
26
+
27
+ REQUEST_METHODS = {
28
+ post: "POST",
29
+ get: "GET",
30
+ put: "PUT",
31
+ delete: "DELETE"
32
+ }.freeze
33
+
34
+ def initialize(secret_key)
35
+ @secret = paychangu_secret(secret_key)
36
+ end
37
+
38
+ def create_payment_link(data = {})
39
+ payload = create_link_payload(data)
40
+
41
+ process_request(payload, API_ENDPOINTS[:payment], REQUEST_METHODS[:post])
42
+ end
43
+
44
+ def create_virtual_card(data = {})
45
+ payload = create_card_payload(data)
46
+
47
+ process_request(payload, API_ENDPOINTS[:create_card], REQUEST_METHODS[:post])
48
+ end
49
+
50
+ def fund_card(data = {})
51
+ payload = fund_card_payload(data)
52
+
53
+ process_request(payload, API_ENDPOINTS[:fund_card], REQUEST_METHODS[:post])
54
+ end
55
+
56
+ def withdraw_card_funds(data = {})
57
+ payload = withdraw_card_funds_payload(data)
58
+
59
+ process_request(payload, API_ENDPOINTS[:withdraw], REQUEST_METHODS[:post])
60
+ end
61
+
62
+ def airtime_operators
63
+ process_request(nil, API_ENDPOINTS[:get_operators], REQUEST_METHODS[:get])
64
+ end
65
+
66
+ def airtime_payment(data = {})
67
+ payload = create_airtime_payment_payload(data)
68
+
69
+ process_request(payload, API_ENDPOINTS[:airtime_payment], REQUEST_METHODS[:post])
70
+ end
71
+
72
+ def verify_payment(data = {})
73
+ process_request(nil, "#{API_ENDPOINTS[:verify_payment]}/#{data[:tx_ref]}", REQUEST_METHODS[:get])
74
+ end
75
+
76
+ private
77
+
78
+ def paychangu_secret(secret_key)
79
+ raise "Secret key not provided!" unless secret_key
80
+
81
+ secret_key
82
+ end
83
+
84
+ def get_supported_currencies(currency)
85
+ raise "#{currency} currency not supported!" unless SUPPORTED_CURRENCIES.include?(currency)
86
+
87
+ currency
88
+ end
89
+
90
+ def get_card_supported_currencies(currency)
91
+ raise "#{currency} currency not supported" unless SUPPORTED_CARD_CURRENCIES.include?(currency)
92
+
93
+ currency
94
+ end
95
+
96
+ def create_link_payload(data)
97
+ {
98
+ amount: data[:amount],
99
+ currency: get_supported_currencies(data[:currency]),
100
+ email: data[:email],
101
+ first_name: data[:first_name],
102
+ last_name: data[:last_name],
103
+ callback_url: data[:callback_url],
104
+ return_url: data[:return_url],
105
+ tx_ref: data[:tx_ref] || SecureRandom.hex(10),
106
+ customization: customization(data),
107
+ logo: data[:logo]
108
+ }.to_json
109
+ end
110
+
111
+ def create_card_payload(data)
112
+ {
113
+ amount: data[:amount],
114
+ currency: get_card_supported_currencies(data[:currency]),
115
+ first_name: data[:first_name],
116
+ last_name: data[:last_name],
117
+ callback_url: data[:callback_url]
118
+ }.to_json
119
+ end
120
+
121
+ def fund_card_payload(data)
122
+ {
123
+ amount: data[:amount],
124
+ card_hash: data[:card_hash]
125
+ }.to_json
126
+ end
127
+
128
+ def withdraw_card_funds_payload(data)
129
+ {
130
+ amount: data[:amount],
131
+ card_hash: data[:card_hash]
132
+ }.to_json
133
+ end
134
+
135
+ def customization(data)
136
+ {
137
+ title: data[:title],
138
+ description: data[:description]
139
+ }
140
+ end
141
+
142
+ def create_airtime_payment_payload(data)
143
+ {
144
+ operator: data[:operator],
145
+ amount: data[:amount],
146
+ phone: data[:phone],
147
+ callback_url: data[:callback_url]
148
+ }.to_json
149
+ end
150
+
151
+ def process_request(payload, path, method)
152
+ http = Net::HTTP.new(API_URL.host, API_URL.port)
153
+ http.use_ssl = true
154
+
155
+ case method
156
+ when "POST"
157
+ request = Net::HTTP::Post.new("#{API_URL}#{path}")
158
+ request["accept"] = "application/json"
159
+ request["Authorization"] = "Bearer #{@secret}"
160
+ request["content-type"] = "application/json"
161
+ request.body = payload
162
+ when "GET"
163
+ request = Net::HTTP::Get.new("#{API_URL}#{path}")
164
+ request["accept"] = "application/json"
165
+ request["Authorization"] = "Bearer #{@secret}"
166
+ request["content-type"] = "application/json"
167
+ when "PUT"
168
+ request = Net::HTTP::Put.new("#{API_URL}#{path}")
169
+ request["accept"] = "application/json"
170
+ request["Authorization"] = "Bearer #{@secret}"
171
+ request["content-type"] = "application/json"
172
+ request.body = payload
173
+ when "DELETE"
174
+ request = Net::HTTP::Delete.new("#{API_URL}#{path}")
175
+ request["accept"] = "application/json"
176
+ request["Authorization"] = "Bearer #{@secret}"
177
+ request["content-type"] = "application/json"
178
+ request.body = payload
179
+ end
180
+
181
+ response = http.request(request)
182
+ JSON.parse(response.body)
183
+ end
184
+ end
185
+ end
data/paychangu.gemspec ADDED
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/paychangu/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "Paychangu"
7
+ spec.version = Paychangu::VERSION
8
+ spec.authors = ["Danny Simfukwe"]
9
+ spec.email = ["dannysimfukwe@gmail.com"]
10
+
11
+ spec.summary = "Paychangu is a Ruby gem that allows you to interact with Paychangu.com's API."
12
+ spec.description = "Paychangu is a Ruby gem that allows you to interact with Paychangu.com's API and perform several operations like: creating payment links, creating virtual cards etc"
13
+ spec.homepage = "https://github.com/dannysimfukwe/paychangugem"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = ">= 2.6.0"
16
+
17
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
18
+
19
+ spec.metadata["homepage_uri"] = spec.homepage
20
+ spec.metadata["source_code_uri"] = "https://github.com/dannysimfukwe/paychangugem"
21
+ spec.metadata["changelog_uri"] = "https://github.com/dannysimfukwe/paychangugem"
22
+
23
+ # Specify which files should be added to the gem when it is released.
24
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
25
+ spec.files = Dir.chdir(__dir__) do
26
+ `git ls-files -z`.split("\x0").reject do |f|
27
+ (File.expand_path(f) == __FILE__) ||
28
+ f.start_with?(*%w[bin/ test/ spec/ features/ .git .github appveyor Gemfile])
29
+ end
30
+ end
31
+ spec.bindir = "exe"
32
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
33
+ spec.require_paths = ["lib"]
34
+
35
+ # Uncomment to register a new dependency of your gem
36
+ # spec.add_dependency "example-gem", "~> 1.0"
37
+
38
+ # For more information and examples about making a new gem, check out our
39
+ # guide at: https://bundler.io/guides/creating_gem.html
40
+ spec.metadata["rubygems_mfa_required"] = "false"
41
+ end
data/sig/paychangu.rbs ADDED
@@ -0,0 +1,4 @@
1
+ module Paychangu
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,61 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: Paychangu
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.4
5
+ platform: ruby
6
+ authors:
7
+ - Danny Simfukwe
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-02-02 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: 'Paychangu is a Ruby gem that allows you to interact with Paychangu.com''s
14
+ API and perform several operations like: creating payment links, creating virtual
15
+ cards etc'
16
+ email:
17
+ - dannysimfukwe@gmail.com
18
+ executables: []
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - ".rspec"
23
+ - ".rubocop.yml"
24
+ - CHANGELOG.md
25
+ - LICENSE.txt
26
+ - README.md
27
+ - Rakefile
28
+ - lib/paychangu.rb
29
+ - lib/paychangu/version.rb
30
+ - paychangu.gemspec
31
+ - sig/paychangu.rbs
32
+ homepage: https://github.com/dannysimfukwe/paychangugem
33
+ licenses:
34
+ - MIT
35
+ metadata:
36
+ allowed_push_host: https://rubygems.org
37
+ homepage_uri: https://github.com/dannysimfukwe/paychangugem
38
+ source_code_uri: https://github.com/dannysimfukwe/paychangugem
39
+ changelog_uri: https://github.com/dannysimfukwe/paychangugem
40
+ rubygems_mfa_required: 'false'
41
+ post_install_message:
42
+ rdoc_options: []
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: 2.6.0
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ requirements: []
56
+ rubygems_version: 3.4.22
57
+ signing_key:
58
+ specification_version: 4
59
+ summary: Paychangu is a Ruby gem that allows you to interact with Paychangu.com's
60
+ API.
61
+ test_files: []