pesepay 0.1.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: e0310be3c2bfdbae8d5cdc50c3e92a88ef21ee5df8c077fa3fc3fcc5d426bc01
4
+ data.tar.gz: b77593488d232fe8ff3237a55c30235689fee336838e0f95b1630d182ca27049
5
+ SHA512:
6
+ metadata.gz: ba8cd9f90119178b55621257be2f22f4debd8ccbdb23ad8043f667e4c81d866748b08dc327657cf24b92aecca644099e7f6a9b3eb5c185f18eaf3510c1fe3dce
7
+ data.tar.gz: ca8601cd93e053d0e0cb48da00028a487d9ba98b60ee68cc33fbc5c2fc0c12e528698cf54b3981dcab2d863537ce433f7e73012bb1ae1700afec8209093c830f
@@ -0,0 +1,16 @@
1
+ name: Ruby
2
+
3
+ on: [push,pull_request]
4
+
5
+ jobs:
6
+ build:
7
+ runs-on: ubuntu-latest
8
+ steps:
9
+ - uses: actions/checkout@v2
10
+ - name: Set up Ruby
11
+ uses: ruby/setup-ruby@v1
12
+ with:
13
+ ruby-version: 3.0.2
14
+ bundler-cache: true
15
+ - name: Run the default task
16
+ run: bundle exec rake
data/.gitignore ADDED
@@ -0,0 +1,13 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+ .env
13
+ .DS_Store
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,13 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.4
3
+
4
+ Style/StringLiterals:
5
+ Enabled: true
6
+ EnforcedStyle: double_quotes
7
+
8
+ Style/StringLiteralsInInterpolation:
9
+ Enabled: true
10
+ EnforcedStyle: double_quotes
11
+
12
+ Layout/LineLength:
13
+ Max: 120
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2022-11-03
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 marshall.chikari@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/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in pesepay.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rspec", "~> 3.0"
11
+
12
+ gem "rubocop", "~> 1.7"
13
+
14
+
data/Gemfile.lock ADDED
@@ -0,0 +1,57 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ pesepay (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ ast (2.4.2)
10
+ diff-lcs (1.5.0)
11
+ json (2.6.2)
12
+ parallel (1.22.1)
13
+ parser (3.1.2.1)
14
+ ast (~> 2.4.1)
15
+ rainbow (3.1.1)
16
+ rake (13.0.6)
17
+ regexp_parser (2.6.0)
18
+ rexml (3.2.5)
19
+ rspec (3.12.0)
20
+ rspec-core (~> 3.12.0)
21
+ rspec-expectations (~> 3.12.0)
22
+ rspec-mocks (~> 3.12.0)
23
+ rspec-core (3.12.0)
24
+ rspec-support (~> 3.12.0)
25
+ rspec-expectations (3.12.0)
26
+ diff-lcs (>= 1.2.0, < 2.0)
27
+ rspec-support (~> 3.12.0)
28
+ rspec-mocks (3.12.0)
29
+ diff-lcs (>= 1.2.0, < 2.0)
30
+ rspec-support (~> 3.12.0)
31
+ rspec-support (3.12.0)
32
+ rubocop (1.38.0)
33
+ json (~> 2.3)
34
+ parallel (~> 1.10)
35
+ parser (>= 3.1.2.1)
36
+ rainbow (>= 2.2.2, < 4.0)
37
+ regexp_parser (>= 1.8, < 3.0)
38
+ rexml (>= 3.2.5, < 4.0)
39
+ rubocop-ast (>= 1.23.0, < 2.0)
40
+ ruby-progressbar (~> 1.7)
41
+ unicode-display_width (>= 1.4.0, < 3.0)
42
+ rubocop-ast (1.23.0)
43
+ parser (>= 3.1.1.0)
44
+ ruby-progressbar (1.11.0)
45
+ unicode-display_width (2.3.0)
46
+
47
+ PLATFORMS
48
+ x86_64-darwin-20
49
+
50
+ DEPENDENCIES
51
+ pesepay!
52
+ rake (~> 13.0)
53
+ rspec (~> 3.0)
54
+ rubocop (~> 1.7)
55
+
56
+ BUNDLED WITH
57
+ 2.2.22
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 gitnyasha
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,144 @@
1
+ # Pesepay
2
+
3
+ Pesepay is a payment gateway ruby gem that allows you to make and manage payments online.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'pesepay'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle install
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install pesepay
20
+
21
+ ## Usage
22
+
23
+ - initiate_transaction is a method in the Pesepay class that allows merchants to initiate a payment transaction. When called, it sends a request to the Pesepay API with the transaction details, such as the amount, currency, description, and reference number. If the request is successful, the method returns a Response object containing a reference number, poll URL, and redirect URL. The reference number can be used for tracking purposes, while the poll URL and redirect URL are used for further processing and redirecting the customer to complete the payment.
24
+
25
+ - make_seamless_payment is another method in the Pesepay class, designed for seamless payment processing. It enables merchants to process credit card payments without redirecting customers to the Pesepay platform. This method also sends a request to the Pesepay API with the necessary payment details, and if the request is successful, it returns a Response object with a reference number, poll URL, and redirect URL. These URLs are crucial for handling the payment process and communicating the status back to the merchant's platform. Additionally, this method requires specific payment method details, like credit card information, to perform the seamless payment.
26
+
27
+ - get_payment_method_code(currency_code): This method returns the payment method code for the specified currency code.
28
+
29
+
30
+ Here is an example of how you might use the Pesepay gem in your code:
31
+
32
+ ```ruby
33
+ require 'pesepay'
34
+
35
+ # Create a new Pesepay object with your integration key and encryption key
36
+
37
+ pesepay = Pesepay::Pesepay.new('INTEGRATION_KEY', 'ENCRYPTION_KEY')
38
+
39
+ ```
40
+ ### To redirect to pesepay page and make paymnets
41
+
42
+ ```ruby
43
+
44
+ # Set the return URL and result URL for handling the transaction status
45
+
46
+ pesepay.result_url = "http://example.com/gateway/return"
47
+ pesepay.return_url = "http://example.com/gateway/return"
48
+
49
+ # Create a transaction for $100 with the currency code "USD" and the reason "Pizza"
50
+
51
+ transaction = pesepay.create_transaction(100, "USD", "Pizza", 11.99)
52
+
53
+ # Initiate the transaction and get the response from the API
54
+
55
+ response = pesepay.initiate_transaction(transaction)
56
+
57
+ if response.success
58
+
59
+ # Transaction initiation was successful, so redirect the user to the provided redirect URL
60
+
61
+ poll_url = response.pollUrl
62
+ redirect_to response.redirectUrl
63
+ else
64
+
65
+ # There was an error, so display the error message to the user
66
+
67
+ puts response.message
68
+ end
69
+ ```
70
+ ### To Make a seamless payment using credit card for $50 with the currency code "USD"
71
+
72
+ ```ruby
73
+ payment = pesepay.create_seamless_transaction("USD", "PZW204", "customer@example.com", "555-555-1212", "John Smith")
74
+ payment_method_required_fields = {
75
+ "creditCardExpiryDate": "09/23",
76
+ "creditCardNumber": "4867960000005461",
77
+ "creditCardSecurityNumber": "608"
78
+ }
79
+
80
+ response = pesepay.make_seamless_payment(payment, "Test payment", 100, payment_method_required_fields, "123453")
81
+
82
+ if response.success
83
+
84
+ # Payment was successful, so save the reference number and poll URL for checking the transaction status
85
+
86
+ reference_number = response.referenceNumber
87
+ poll_url = response.pollUrl
88
+ else
89
+
90
+ # There was an error, so display the error message to the user
91
+
92
+ puts response.message
93
+ end
94
+
95
+ # If paying using ecocash, provide the required payment details for ecocash method
96
+
97
+ payment = pesepay.create_seamless_transaction("USD", "PZW204", "customer@example.com", "555-555-1212", "John Smith")
98
+ payment_method_required_fields = {
99
+ 'customerPhoneNumber': '0777777777'
100
+ }
101
+
102
+ response = pesepay.make_seamless_payment(payment, "Test payment", 100, payment_method_required_fields, "123453")
103
+
104
+ if response.success
105
+
106
+ # Payment was successful, so save the reference number and poll URL for checking the transaction status
107
+
108
+ reference_number = response.referenceNumber
109
+ poll_url = response.pollUrl
110
+ else
111
+
112
+ # There was an error, so display the error message to the user
113
+
114
+ puts response.message
115
+ end
116
+
117
+ # Get the payment method code for the currency code "USD"
118
+
119
+ payment_method_code = payment.get_payment_method_code('USD')
120
+
121
+ puts payment_method_code
122
+
123
+
124
+ ```
125
+
126
+ Make sure to replace 'INTEGRATION_KEY' and 'ENCRYPTION_KEY' with your actual Pesepay integration and encryption keys. Additionally, adjust the URLs and payment details accordingly to match your application's requirements.
127
+
128
+ ## Development
129
+
130
+ 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.
131
+
132
+ 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).
133
+
134
+ ## Contributing
135
+
136
+ Bug reports and pull requests are welcome on GitHub at https://github.com/gitnyasha/pesepay. 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/gitnyasha/pesepay/blob/master/CODE_OF_CONDUCT.md).
137
+
138
+ ## License
139
+
140
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
141
+
142
+ ## Code of Conduct
143
+
144
+ Everyone interacting in the Pesepay project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/gitnyasha/pesepay/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]
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "pesepay"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Pesepay
4
+ VERSION = "0.1.0"
5
+ end
data/lib/pesepay.rb ADDED
@@ -0,0 +1,262 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "pesepay/version"
4
+ require "cgi"
5
+ require "digest"
6
+ require "uri"
7
+ require "net/http"
8
+ require "openssl"
9
+ require "base64"
10
+ require "json"
11
+
12
+ module Pesepay
13
+ class Pesepay
14
+ attr_accessor :result_url, :return_url
15
+
16
+ def initialize(integration_key, encryption_key)
17
+ @integration_key = integration_key
18
+ @encryption_key = encryption_key
19
+ end
20
+
21
+ def create_transaction(amount, currency_code, reason_for_payment, reference)
22
+ Transaction.new(amount, currency_code, reason_for_payment, reference)
23
+ end
24
+
25
+ def create_seamless_transaction(currency_code, payment_method_code, customer_email = nil, customer_phone = nil, customer_name = nil)
26
+ Payment.new(currency_code, payment_method_code, customer_email, customer_phone, customer_name)
27
+ end
28
+
29
+ def initiate_transaction(transaction)
30
+ response = create_transaction_request(transaction)
31
+ process_response(response)
32
+ end
33
+
34
+ def make_seamless_payment(payment, reason_for_payment, amount, payment_method_required_fields, reference = nil)
35
+ response = create_seamless_payment_request(payment, reason_for_payment, amount, payment_method_required_fields, reference)
36
+ process_response(response)
37
+ end
38
+
39
+ def get_payment_method_code(currency_code)
40
+ response = get_payment_method_code_request(currency_code)
41
+ process_get_code_method(response)
42
+ end
43
+
44
+ def poll_transaction(poll_url)
45
+ url = URI(poll_url)
46
+
47
+ http = Net::HTTP.new(url.host, url.port)
48
+ http.use_ssl = true
49
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
50
+
51
+ request = Net::HTTP::Get.new(url)
52
+ request["Authorization"] = @integration_key
53
+ request["Content-Type"] = "application/json"
54
+ response = http.request(request)
55
+
56
+ process_poll_response(response)
57
+ end
58
+
59
+ def check_payment(reference_number)
60
+ url = "https://api.pesepay.com/api/payments-engine/v1/payments/check-payment?referenceNumber=#{reference_number}"
61
+ poll_transaction(url)
62
+ end
63
+
64
+ private
65
+
66
+ def create_transaction_request(transaction)
67
+ data = build_transaction_data(transaction)
68
+
69
+ url = URI("https://api.pesepay.com/api/payments-engine/v1/payments/initiate")
70
+
71
+ http = Net::HTTP.new(url.host, url.port)
72
+ http.use_ssl = true
73
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
74
+
75
+ request = Net::HTTP::Post.new(url)
76
+ request["Authorization"] = @integration_key
77
+ request["Content-Type"] = "application/json"
78
+ request.body = { payload: data }.to_json
79
+ http.request(request)
80
+ end
81
+
82
+ def create_seamless_payment_request(payment, reason_for_payment, amount, payment_method_required_fields, reference)
83
+ data = build_seamless_payment_data(payment, reason_for_payment, amount, payment_method_required_fields, reference)
84
+
85
+ url = URI("https://api.pesepay.com/api/payments-engine/v2/payments/make-payment")
86
+
87
+ http = Net::HTTP.new(url.host, url.port)
88
+ http.use_ssl = true
89
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
90
+
91
+ request = Net::HTTP::Post.new(url)
92
+ request["Authorization"] = @integration_key
93
+ request["Content-Type"] = "application/json"
94
+ request.body = { payload: data }.to_json
95
+ http.request(request)
96
+ end
97
+
98
+ def get_payment_method_code_request(currency_code)
99
+ url = URI("https://api.pesepay.com/api/payments-engine/v1/payment-methods/for-currency?currencyCode=#{currency_code}")
100
+
101
+ http = Net::HTTP.new(url.host, url.port)
102
+ http.use_ssl = true
103
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
104
+
105
+ request = Net::HTTP::Get.new(url)
106
+ http.request(request)
107
+ end
108
+
109
+ def process_get_code_method(response)
110
+ if response.code == "200"
111
+ response_body = JSON.parse(response.body)
112
+ payment_method_code = response_body[0]["code"]
113
+ return payment_method_code
114
+ else
115
+ Response.new(false, message: "Unable to get payment method code")
116
+ end
117
+ end
118
+
119
+ def process_response(response)
120
+ if response.code == "200"
121
+ response_body = JSON.parse(response.body.gsub("'", '"'))
122
+ inner_payload = response_body["payload"]
123
+ raw_response = decrypt(inner_payload, @encryption_key)
124
+ json_string = JSON.parse(raw_response)
125
+ ref_no = json_string["referenceNumber"]
126
+ poll_url = json_string["pollUrl"]
127
+ redirect_url = json_string["redirectUrl"]
128
+ Response.new(true, referenceNumber: ref_no, pollUrl: poll_url, redirectUrl: redirect_url)
129
+ else
130
+ message = JSON.parse(response.body)["message"]
131
+ Response.new(false, message: message)
132
+ end
133
+ end
134
+
135
+ def process_poll_response(response)
136
+ if response.code == "200"
137
+ response_body = JSON.parse(response.body)
138
+ inner_payload = response_body["payload"]
139
+ raw_response = decrypt(inner_payload)
140
+ json_string = JSON.parse(raw_response)
141
+ reference_number = json_string["referenceNumber"]
142
+ poll_url = json_string["pollUrl"]
143
+ paid = json_string["transactionStatus"] == "SUCCESS"
144
+
145
+ StatusResponse.new(reference_number, poll_url, paid)
146
+ else
147
+ message = JSON.parse(response.body)["message"]
148
+ StatusResponse.new(nil, nil, false, message)
149
+ end
150
+ end
151
+
152
+ def build_transaction_data(transaction)
153
+ payload = {
154
+ "amountDetails": {
155
+ "amount": transaction.amount,
156
+ "currencyCode": transaction.currency_code,
157
+ },
158
+ "reasonForPayment": transaction.reason_for_payment,
159
+ "resultUrl": @result_url,
160
+ "returnUrl": @return_url,
161
+ "merchantReference": transaction.reference,
162
+ }
163
+
164
+ encrypted = encrypt(payload.to_json, @encryption_key)
165
+ encrypted
166
+ end
167
+
168
+ def build_seamless_payment_data(payment, reason_for_payment, amount, payment_method_required_fields, reference)
169
+ payload = {
170
+ "amountDetails": {
171
+ "amount": amount,
172
+ "currencyCode": payment.currency_code,
173
+ },
174
+ "merchantReference": reference,
175
+ "reasonForPayment": reason_for_payment,
176
+ "resultUrl": @result_url,
177
+ "returnUrl": @return_url,
178
+ "paymentMethodCode": payment.payment_method_code,
179
+ "customer": {
180
+ "email": payment.customer_email,
181
+ "phoneNumber": payment.customer_phone,
182
+ "name": payment.customer_name,
183
+ },
184
+ "paymentMethodRequiredFields": payment_method_required_fields,
185
+ }
186
+
187
+ encrypted = encrypt(payload.to_json, @encryption_key)
188
+ encrypted
189
+ end
190
+
191
+ def encrypt(payload, key)
192
+ init_vector = key[0, 16]
193
+ cryptor = OpenSSL::Cipher::AES.new(256, :CBC)
194
+ cryptor.encrypt
195
+ cryptor.key = key
196
+ cryptor.iv = init_vector
197
+ ciphertext = cryptor.update(pad(payload)) + cryptor.final
198
+ Base64.strict_encode64(ciphertext)
199
+ end
200
+
201
+ def decrypt(payload, key)
202
+ ciphertext = Base64.strict_decode64(payload)
203
+ init_vector = key[0, 16]
204
+ cryptor = OpenSSL::Cipher::AES.new(256, :CBC)
205
+ cryptor.decrypt
206
+ cryptor.key = key
207
+ cryptor.iv = init_vector
208
+ plaintext = cryptor.update(ciphertext) + cryptor.final
209
+ plaintext
210
+ end
211
+
212
+ def pad(input)
213
+ padding_length = 16 - (input.length % 16)
214
+ input + (padding_length.chr * padding_length)
215
+ end
216
+ end
217
+
218
+ class Response
219
+ attr_reader :success, :referenceNumber, :pollUrl, :redirectUrl, :message
220
+
221
+ def initialize(success, referenceNumber: nil, pollUrl: nil, redirectUrl: nil, message: nil)
222
+ @success = success
223
+ @referenceNumber = referenceNumber
224
+ @pollUrl = pollUrl
225
+ @redirectUrl = redirectUrl
226
+ @message = message
227
+ end
228
+ end
229
+
230
+ class StatusResponse
231
+ attr_reader :referenceNumber, :pollUrl, :paid
232
+
233
+ def initialize(referenceNumber: nil, pollUrl: nil, paid: false)
234
+ @paid = paid
235
+ @referenceNumber = referenceNumber
236
+ @pollUrl = pollUrl
237
+ end
238
+ end
239
+
240
+ class Transaction
241
+ attr_accessor :amount, :currency_code, :reason_for_payment, :reference
242
+
243
+ def initialize(amount, currency_code, reason_for_payment, reference)
244
+ @amount = amount
245
+ @currency_code = currency_code
246
+ @reason_for_payment = reason_for_payment
247
+ @reference = reference
248
+ end
249
+ end
250
+
251
+ class Payment
252
+ attr_accessor :currency_code, :payment_method_code, :customer_email, :customer_phone, :customer_name
253
+
254
+ def initialize(currency_code, payment_method_code, customer_email = nil, customer_phone = nil, customer_name = nil)
255
+ @currency_code = currency_code
256
+ @payment_method_code = payment_method_code
257
+ @customer_email = customer_email
258
+ @customer_phone = customer_phone
259
+ @customer_name = customer_name
260
+ end
261
+ end
262
+ end
data/pesepay.gemspec ADDED
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/pesepay/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "pesepay"
7
+ spec.version = Pesepay::VERSION
8
+ spec.authors = ["gitnyasha"]
9
+ spec.email = ["marshall.chikari@gmail.com"]
10
+
11
+ spec.summary = "Pesepay ruby library."
12
+ spec.description = "A ruby library for Pesepay Payment Gateway API."
13
+ spec.homepage = "https://github.com/gitnyasha/pesepay-ruby"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = ">= 2.4.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/gitnyasha/pesepay-ruby"
21
+ spec.metadata["changelog_uri"] = "https://github.com/gitnyasha/pesepay-ruby"
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(File.expand_path(__dir__)) do
26
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
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
+ # Uncomment to register a new dependency of your gem
33
+ # spec.add_dependency "example-gem", "~> 1.0"
34
+
35
+ # For more information and examples about making a new gem, checkout our
36
+ # guide at: https://bundler.io/guides/creating_gem.html
37
+ end
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pesepay
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - gitnyasha
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-07-05 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A ruby library for Pesepay Payment Gateway API.
14
+ email:
15
+ - marshall.chikari@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".github/workflows/main.yml"
21
+ - ".gitignore"
22
+ - ".rspec"
23
+ - ".rubocop.yml"
24
+ - CHANGELOG.md
25
+ - CODE_OF_CONDUCT.md
26
+ - Gemfile
27
+ - Gemfile.lock
28
+ - LICENSE.txt
29
+ - README.md
30
+ - Rakefile
31
+ - bin/console
32
+ - bin/setup
33
+ - lib/pesepay.rb
34
+ - lib/pesepay/version.rb
35
+ - pesepay.gemspec
36
+ homepage: https://github.com/gitnyasha/pesepay-ruby
37
+ licenses:
38
+ - MIT
39
+ metadata:
40
+ allowed_push_host: https://rubygems.org/
41
+ homepage_uri: https://github.com/gitnyasha/pesepay-ruby
42
+ source_code_uri: https://github.com/gitnyasha/pesepay-ruby
43
+ changelog_uri: https://github.com/gitnyasha/pesepay-ruby
44
+ post_install_message:
45
+ rdoc_options: []
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: 2.4.0
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ requirements: []
59
+ rubygems_version: 3.4.15
60
+ signing_key:
61
+ specification_version: 4
62
+ summary: Pesepay ruby library.
63
+ test_files: []