nexio_activemerchant 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: dfb690b171aa84d521a855689a86cc301b580820564b6d510d800cda2bdd686f
4
+ data.tar.gz: e8328d6c3d67a4bae931833627ee611722b0dfd3c29f288c6e50bd34aeefba4f
5
+ SHA512:
6
+ metadata.gz: 9db4a030573f29a2e6e94a7f9a3d4c44022741a4d792bc2d04f55c1fe84ef72f5e54088670ac88502456ff21f04f599cd06a1d1981b0575f50cc5443e6f0c8e7
7
+ data.tar.gz: b865b5f47ab674b4dc1e823887bb1461f2fd8c3b90aa791776da518dec9feb729b13949aeb0767269fe6bf0b0b248272ba4eee33d85203bd22c1390abf0db431
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ /Gemfile.lock
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,21 @@
1
+ # The behavior of RuboCop can be controlled via the .rubocop.yml
2
+ # configuration file. It makes it possible to enable/disable
3
+ # certain cops (checks) and to alter their behavior if they accept
4
+ # any parameters. The file can be placed either in your home
5
+ # directory or in some project directory.
6
+ #
7
+ # RuboCop will start looking for the configuration file in the directory
8
+ # where the inspected file is and continue its way up to the root directory.
9
+ #
10
+ # See https://docs.rubocop.org/rubocop/configuration
11
+
12
+ Gemspec/RequiredRubyVersion:
13
+ Enabled: false
14
+ Metrics/ClassLength:
15
+ Enabled: false
16
+ Metrics/MethodLength:
17
+ Max: 30
18
+ Lint/AssignmentInCondition:
19
+ Enabled: false
20
+ Style/GuardClause:
21
+ Enabled: false
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.7.1
6
+ before_install: gem install bundler -v 2.1.4
data/CHANGELOG ADDED
File without changes
data/Gemfile ADDED
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in nexio_activemerchant.gemspec
6
+ gemspec
7
+
8
+ gem 'rake', '~> 12.0'
9
+
10
+ group :test do
11
+ gem 'rspec', '~> 3.0'
12
+ end
13
+
14
+ group :tools do
15
+ gem 'rubocop'
16
+ end
data/README.md ADDED
@@ -0,0 +1,36 @@
1
+ # NexioActivemerchant
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/nexio_activemerchant`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'nexio_activemerchant'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install nexio_activemerchant
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ 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.
30
+
31
+ 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).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/nexio_activemerchant.
36
+
data/Rakefile ADDED
@@ -0,0 +1,8 @@
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
+ task default: :spec
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 'nexio_activemerchant'
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,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveMerchant
4
+ module Billing
5
+ class EncryptedNexioCard < CreditCard
6
+ attr_accessor :encrypted_number, :own_form, :one_time_token
7
+
8
+ attr_reader :brand
9
+
10
+ def short_year
11
+ year % 100 if year
12
+ end
13
+
14
+ private
15
+
16
+ def validate_card_brand_and_number
17
+ errors = []
18
+
19
+ if empty?(brand)
20
+ errors << [:brand, 'is required'] if own_form
21
+ els
22
+ errors << [:brand, 'is invalid'] unless self.class.card_companies.include?(brand)
23
+ end
24
+
25
+ errors << [:encrypted_number, 'is required'] if empty?(encrypted_number)
26
+
27
+ errors
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,259 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'json'
4
+
5
+ module ActiveMerchant
6
+ module Billing
7
+ class NexioGateway < Gateway
8
+ self.test_url = 'https://api.nexiopaysandbox.com'
9
+ self.live_url = 'https://api.nexiopay.com'
10
+
11
+ self.supported_countries = %w[CA US]
12
+ self.default_currency = 'USD'
13
+ self.supported_cardtypes = %i[visa master american_express discover]
14
+
15
+ self.homepage_url = 'https://nex.io'
16
+ self.display_name = 'Nexio'
17
+
18
+ STANDARD_ERROR_CODE_MAPPING = {}.freeze
19
+
20
+ OneTimeToken = Struct.new(:token, :expiration, :fraud_url)
21
+
22
+ def initialize(options = {})
23
+ requires!(options, :merchant_id, :auth_token)
24
+ super
25
+ end
26
+
27
+ def generate_token(options = {})
28
+ post = build_payload(options)
29
+ add_currency(post, options)
30
+ add_order_data(post, options)
31
+ add_card_data(post, options)
32
+ resp = commit('token', post)
33
+ return unless resp.success?
34
+
35
+ token, expiration, fraud_url = resp.params.values_at('token', 'expiration', 'fraudUrl')
36
+ OneTimeToken.new(token, Time.parse(expiration), fraud_url)
37
+ end
38
+
39
+ def purchase(money, payment, options = {})
40
+ post = build_payload(options)
41
+ add_invoice(post, money, options)
42
+ add_payment(post, payment, options)
43
+ add_order_data(post, options)
44
+ commit('process', post)
45
+ end
46
+
47
+ def authorize(money, payment, options = {})
48
+ purchase(money, payment, options.merge(payload: options.fetch(:payload, {}).merge(isAuthOnly: true)))
49
+ end
50
+
51
+ def capture(_money, _authorization, _options = {})
52
+ commit('capture', post)
53
+ end
54
+
55
+ def refund(_money, _authorization, _options = {})
56
+ commit('refund', post)
57
+ end
58
+
59
+ def void(_authorization, _options = {})
60
+ commit('void', post)
61
+ end
62
+
63
+ def verify(credit_card, options = {})
64
+ MultiResponse.run(:use_first_response) do |r|
65
+ r.process { authorize(100, credit_card, options) }
66
+ r.process(:ignore_result) { void(r.authorization, options) }
67
+ end
68
+ end
69
+
70
+ def supports_scrubbing?
71
+ false
72
+ end
73
+
74
+ def scrub(transcript)
75
+ transcript
76
+ end
77
+
78
+ def store(payment, options = {})
79
+ post = build_payload(options)
80
+ add_card_details(post, payment, options)
81
+ add_currency(post, options)
82
+ add_order_data(post, options)
83
+ resp = commit('saveCard', post)
84
+ return unless resp.success?
85
+
86
+ resp.params.fetch('token', {}).fetch('token', nil)
87
+ end
88
+
89
+ private
90
+
91
+ def add_invoice(post, money, options)
92
+ post[:data][:amount] = amount(money).to_f
93
+ add_currency(post, options)
94
+ end
95
+
96
+ def add_currency(post, options)
97
+ post[:data][:currency] = options[:currency] || currency(money)
98
+ end
99
+
100
+ def add_order_data(post, options)
101
+ if customer = options[:customer]
102
+ case customer
103
+ when String
104
+ post[:data][:customer][:email] = customer
105
+ when Hash
106
+ post[:data][:customer].merge!({
107
+ firstName: customer[:first_name],
108
+ lastName: customer[:last_name],
109
+ email: customer[:email]
110
+ })
111
+ end
112
+ end
113
+
114
+ if order = options[:order]
115
+ add_cart(post, order[:line_items]) if order.key?(:line_items)
116
+ post[:data][:customer][:orderNumber] = order[:number] if order.key?(:number)
117
+ post[:data][:customer][:orderDate] = order[:date] if order.key?(:date)
118
+ end
119
+
120
+ add_address(post, options[:billing_address], :billTo)
121
+ add_address(post, options[:address], :shipTo)
122
+ if phone = options.fetch(:address, options.fetch(:billing_address, {}))[:phone]
123
+ post[:data][:customer][:phone] = phone
124
+ end
125
+ end
126
+
127
+ def add_cart(post, list)
128
+ items = list.map do |item|
129
+ {
130
+ item: item[:id],
131
+ description: item[:description],
132
+ quantity: item.fetch(:quantity, 1),
133
+ price: amount(item[:price]).to_f,
134
+ type: item.fetch(:type, :sale)
135
+ }
136
+ end
137
+ post[:data][:cart] = { items: items }
138
+ end
139
+
140
+ def add_address(post, data, prefix)
141
+ return post if data.blank?
142
+
143
+ post[:data][:customer].merge!({
144
+ "#{prefix}AddressOne": data[:address1],
145
+ "#{prefix}AddressTwo": data[:address2],
146
+ "#{prefix}City": data[:city],
147
+ "#{prefix}Country": data[:country],
148
+ "#{prefix}Phone": data[:phone],
149
+ "#{prefix}Postal": data[:zip],
150
+ "#{prefix}State": data[:state]
151
+ })
152
+ end
153
+
154
+ def add_payment(post, payment, options)
155
+ post[:tokenex] = token_from(payment)
156
+ post[:processingOptions] ||= {}
157
+ post[:processingOptions][:merchantId] = self.options[:merchant_id].to_s
158
+ post[:processingOptions][:saveCardToken] = options[:save_credit_card] if options.key?(:save_credit_card)
159
+ end
160
+
161
+ def token_from(payment)
162
+ return { token: payment } if payment.is_a?(String)
163
+
164
+ {
165
+ token: payment.gateway_payment_profile_id,
166
+ lastFour: payment.last_digits
167
+ }
168
+ end
169
+
170
+ def add_card_data(post, options)
171
+ if card = options[:card]
172
+ post[:card] = {
173
+ cardHolderName: card[:name],
174
+ expirationMonth: card[:month],
175
+ expirationYear: card[:year]
176
+ }
177
+ end
178
+ end
179
+
180
+ def add_card_details(post, payment, _options)
181
+ if payment.is_a?(EncryptedNexioCard)
182
+ raise ArgumentError, 'The provided card is invalid' unless payment.valid?
183
+
184
+ post[:card] = {
185
+ cardHolderName: payment.name,
186
+ encryptedNumber: payment.encrypted_number,
187
+ expirationMonth: payment.month,
188
+ expirationYear: payment.short_year,
189
+ cardType: payment.brand,
190
+ securityCode: payment.verification_value
191
+ }
192
+ post[:token] = payment.one_time_token
193
+ else
194
+ raise ArgumentError, "Only #{EncryptedNexioCard} payment method is supported to store cards"
195
+ end
196
+ end
197
+
198
+ def parse(body)
199
+ JSON.parse(body)
200
+ rescue StandardError
201
+ {}
202
+ end
203
+
204
+ def commit(action, parameters)
205
+ payload = parse(ssl_post(action_url(action, parameters), post_data(action, parameters),
206
+ Authorization: "Basic #{options[:auth_token]}"))
207
+
208
+ Response.new(
209
+ true,
210
+ nil,
211
+ payload,
212
+ authorization: authorization_from(payload),
213
+ avs_result: build_avs_result(payload['avsResults']),
214
+ cvv_result: build_cvv_result(payload['cvcResults']),
215
+ test: test?,
216
+ network_transaction_id: payload['id']
217
+ )
218
+ rescue ResponseError => e
219
+ error_payload = parse(e.response.body)
220
+ Response.new(
221
+ false,
222
+ error_payload['message'],
223
+ {},
224
+ test: test?,
225
+ error_code: error_payload['error'] || e.response.code.to_i
226
+ )
227
+ end
228
+
229
+ def authorization_from(payload)
230
+ payload.fetch('id', nil)
231
+ end
232
+
233
+ def post_data(_action, parameters = {})
234
+ parameters.to_json
235
+ end
236
+
237
+ def action_url(action, _parameters)
238
+ path = "/pay/v3/#{action}"
239
+ "#{test? ? test_url : live_url}#{path}"
240
+ end
241
+
242
+ def build_avs_result(data)
243
+ return if data.blank?
244
+
245
+ AVSResult.new(street_match: data['matchAddress'], postal_match: data['matchPostal'])
246
+ end
247
+
248
+ def build_cvv_result(data)
249
+ return if data.blank?
250
+
251
+ CVVResult.new(data.fetch('gatewayMessage', {}).fetch('cvvresponse', nil))
252
+ end
253
+
254
+ def build_payload(options)
255
+ { data: { customer: {} } }.merge!(options.fetch(:payload, {}))
256
+ end
257
+ end
258
+ end
259
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'activemerchant'
4
+ require 'active_merchant/billing/encrypted_nexio_card'
5
+ require 'active_merchant/billing/gateways/nexio'
6
+ require 'nexio_activemerchant/version'
7
+
8
+ module NexioActivemerchant
9
+ class Error < StandardError; end
10
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module NexioActivemerchant
4
+ VERSION = '0.1.0'
5
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/nexio_activemerchant/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'nexio_activemerchant'
7
+ spec.version = NexioActivemerchant::VERSION
8
+ spec.authors = %w[Whitespectre]
9
+ spec.email = %w[hello@whitespectre.com]
10
+
11
+ spec.summary = 'ActiveMechant gateway for Nexio'
12
+ spec.homepage = 'https://github.com/whitespectre/nexio_activemerchant'
13
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.3.0')
14
+
15
+ spec.metadata['homepage_uri'] = spec.homepage
16
+ spec.metadata['source_code_uri'] = 'https://github.com/whitespectre/nexio_activemerchant'
17
+ spec.metadata['changelog_uri'] = 'https://github.com/whitespectre/nexio_activemerchant/blob/master/CHANGELOG'
18
+
19
+ # Specify which files should be added to the gem when it is released.
20
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
21
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
22
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
23
+ end
24
+ spec.bindir = 'exe'
25
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
26
+ spec.require_paths = ['lib']
27
+
28
+ spec.add_dependency 'activemerchant'
29
+ spec.add_dependency 'json'
30
+ end
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nexio_activemerchant
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Whitespectre
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-07-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activemerchant
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
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:
42
+ email:
43
+ - hello@whitespectre.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - ".rspec"
50
+ - ".rubocop.yml"
51
+ - ".travis.yml"
52
+ - CHANGELOG
53
+ - Gemfile
54
+ - README.md
55
+ - Rakefile
56
+ - bin/console
57
+ - bin/setup
58
+ - lib/active_merchant/billing/encrypted_nexio_card.rb
59
+ - lib/active_merchant/billing/gateways/nexio.rb
60
+ - lib/nexio_activemerchant.rb
61
+ - lib/nexio_activemerchant/version.rb
62
+ - nexio_activemerchant.gemspec
63
+ homepage: https://github.com/whitespectre/nexio_activemerchant
64
+ licenses: []
65
+ metadata:
66
+ homepage_uri: https://github.com/whitespectre/nexio_activemerchant
67
+ source_code_uri: https://github.com/whitespectre/nexio_activemerchant
68
+ changelog_uri: https://github.com/whitespectre/nexio_activemerchant/blob/master/CHANGELOG
69
+ post_install_message:
70
+ rdoc_options: []
71
+ require_paths:
72
+ - lib
73
+ required_ruby_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: 2.3.0
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ requirements: []
84
+ rubygems_version: 3.1.2
85
+ signing_key:
86
+ specification_version: 4
87
+ summary: ActiveMechant gateway for Nexio
88
+ test_files: []