nitro_pay 0.1.0 → 1.0.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4b8e8f5759fc7498db9fe8b9c92c2be99a5ebad1
4
- data.tar.gz: 28b5935dd66a713cf61559d9dfd81c225745ad35
3
+ metadata.gz: 7599ad547e37aee9ee05248a4c2b0d0a3f4a2fcc
4
+ data.tar.gz: c37529e921e9491a13276368200b0e5edf39bfd0
5
5
  SHA512:
6
- metadata.gz: 6d15f991d87677c9dd06f43f2d9b7d8d1ca05a55e35a35ff10143ff2902cbf886a8ae4bea10d0a51ae2e329812c528e28d28c0fa686fa107684a62baba0745ab
7
- data.tar.gz: cd5b0db7f31aeaf15ef0ebdbf4b8913b76f81d34293937c0ff8ea52ceb9b8c0c4804a978924841a71f604ef0deda2fd27f9922bc1a4b03e1517c0c427e69b5ef
6
+ metadata.gz: c0bde7dfa82a7fe059d23418390af5dc50eb16ad8c559910a8fcbea1038cd90b445d6bb19ab8b710f477eb5678df415fb493d8e8beeb5986402e7de8822efa58
7
+ data.tar.gz: eb1e0b6cbe9bc95d25f5cc1afeeb5fcfa6db939ade3e29de7f4c88393fc8ff6c272efb182a9783b887051786d2b787886e348c1ee8b7e8cfd975286a0d5f6ffe
data/.gitignore CHANGED
@@ -9,7 +9,7 @@ _yardoc
9
9
  .idea/
10
10
  .idea/*
11
11
  /pkg/
12
- lib/rents/config/proxy.yml
12
+ lib/nitro_pay/config/proxy.yml
13
13
  coverage
14
14
  coverage/
15
15
  coverage/*
@@ -0,0 +1,16 @@
1
+ require 'colorize'
2
+ module NitroPay
3
+ module Generators
4
+ class InstallGenerator < Rails::Generators::Base
5
+ source_root File.expand_path('../../templates', __FILE__)
6
+
7
+ desc 'It automatically create it initializer NitroPay rails app config'
8
+
9
+ def copy_initializer
10
+ template 'nitro_pay.rb', 'config/initializers/nitro_pay.rb'
11
+ puts 'Check your config/initializers/nitro_pay.rb & read it comments'.colorize(:light_yellow)
12
+ end
13
+
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,39 @@
1
+ # encoding: utf-8
2
+
3
+ # TODO if using Rails 4 or greater use copy and paste on your secret.yml:
4
+ # nitro_pay:
5
+ # app_id: # TODO your app_id
6
+ # app_secret_key: # TODO your app_secret_key
7
+ # IMPORTANT: remember that test & development is not necessary if using test_env, but if you want your test app remember to use you test_app id & secret
8
+
9
+ # It automatic the NitroPay to it TestEnv & ProductionEnv
10
+ if Rails.env.development? || Rails.env.test?
11
+ # TODO if using Rails 3 or older & not using the TEST_ENV, put here your TEST app_id & your secret_key
12
+ NitroPay.app_id = ''
13
+ NitroPay.secret_key = ''
14
+
15
+ # TODO: Uncomment test_env if you want to test using NitroPay default & global app
16
+ # NitroPay.test_env = true
17
+ # TODO: Uncomment debugger if you have an NitroPay instance on your machine
18
+ # NitroPay.debug = true
19
+ elsif Rails.env.production?
20
+ # TODO if using Rails 3 or older, put here your PRODUCTION app_id & your secret_key
21
+ NitroPay.app_id = ''
22
+ NitroPay.secret_key = ''
23
+
24
+ # For production remember to keep it false or just remove it
25
+ NitroPay.debug = false
26
+ NitroPay.test_env = false
27
+ end
28
+
29
+ # Get your App config if your not using TEST_ENV nor DEBUGGER
30
+ if (NitroPay.test_env.nil? && NitroPay.debug.nil?) || (NitroPay.test_env == false && NitroPay.debug == false)
31
+ if Rails.version[0].to_i >= 4
32
+ begin
33
+ NitroPay.app_id = Rails.application.secrets.NitroPay['app_id']
34
+ NitroPay.secret_key = Rails.application.secrets.NitroPay['app_secret_key']
35
+ rescue
36
+ p 'Check your Secret.yml... Please add on it your Rent$ app_id & app_secret_key'
37
+ end
38
+ end
39
+ end
data/lib/nitro_pay.rb CHANGED
@@ -1,5 +1,93 @@
1
- require "nitro_pay/version"
1
+ # Libs/Gems or Ruby classes
2
+ require 'json'
3
+ require 'curl'
4
+ require 'yaml'
5
+ require 'rest_client'
6
+ require 'bigdecimal'
7
+ require 'bigdecimal/util'
8
+
9
+ # Overrides
10
+ require 'nitro_pay/hash'
11
+ require 'nitro_pay/array'
12
+
13
+ # Gem files
14
+ [:version, :connection, :status, :transaction, :currency, :string].each { |lib| require "nitro_pay/#{lib}" }
2
15
 
3
16
  module NitroPay
4
- # Your code goes here...
5
- end
17
+ # Module attr
18
+ @@enum = nil
19
+
20
+ # Production settings
21
+ @@app_id = nil
22
+ @@secret_key = nil
23
+
24
+ # Tests settings
25
+ @@proxy_yml = nil
26
+ @@test_env = nil
27
+ @@debug = nil
28
+
29
+ def self.proxy_yml
30
+ @@proxy_yml = NitroPay.get_proxy_from_yml if @@proxy_yml.nil?
31
+ @@proxy_yml
32
+ end
33
+
34
+ def self.proxy
35
+ return nil if NitroPay.proxy_yml.nil? || NitroPay.proxy_yml.empty?
36
+ "http://#{NitroPay.proxy_yml[:login]}:#{NitroPay.proxy_yml[:password]}@#{NitroPay.proxy_yml[:host]}:#{NitroPay.proxy_yml[:port]}/"
37
+ end
38
+
39
+ def self.app_id=(app_id)
40
+ @@app_id = app_id
41
+ end
42
+
43
+ def self.app_id
44
+ @@app_id
45
+ end
46
+
47
+ def self.secret_key=(secret_key)
48
+ @@secret_key = secret_key
49
+ end
50
+
51
+ def self.secret_key
52
+ @@secret_key
53
+ end
54
+
55
+ def self.test_env
56
+ @@test_env
57
+ end
58
+
59
+ def self.test_env=(test_env)
60
+ @@test_env = test_env
61
+ end
62
+
63
+ def self.debug
64
+ @@debug
65
+ end
66
+
67
+ def self.debug=(debug)
68
+ @@debug = debug
69
+ end
70
+
71
+ def self.get_proxy_from_yml
72
+ yml = YAML::load_file(File.join(File.dirname(File.expand_path(__FILE__)), 'nitro_pay/config/proxy.yml'))
73
+ !yml.nil? || yml.is_a?(Hash) ? yml.it_keys_to_sym : {} if yml
74
+ end
75
+
76
+ def self.enum
77
+ enum = {}
78
+ return @@enum unless @@enum.nil?
79
+
80
+ enum = enum.merge load_yml('brands.yml')
81
+ enum = enum.merge load_yml('currencies.yml')
82
+ enum = enum.merge load_yml('payment_methods.yml')
83
+ enum = enum.merge load_yml('recurrence_periods.yml')
84
+ enum = enum.merge load_yml('transaction_codes.yml')
85
+
86
+ enum = enum.it_keys_to_sym
87
+ @@enum = enum
88
+ end
89
+
90
+ def self.load_yml(file_name)
91
+ YAML::load_file(File.join(File.dirname(File.expand_path(__FILE__)), 'nitro_pay/config/enums/' + file_name))
92
+ end
93
+ end
@@ -0,0 +1,6 @@
1
+ class Array
2
+ # Convert string keys to symbol keys
3
+ def it_keys_to_sym
4
+ self.each_with_index {|element, i| element.is_a?(Hash) ? self[i] = element.it_keys_to_sym : next }
5
+ end
6
+ end
@@ -0,0 +1,45 @@
1
+ brands:
2
+ # SampleObj
3
+ - id: 0
4
+ name: Nil
5
+ username: nil
6
+
7
+ # VISA
8
+ - id: 1
9
+ name: VISA
10
+ username: visa
11
+
12
+ # MasterCard
13
+ - id: 2
14
+ name: MasterCard
15
+ username: mastercard
16
+
17
+ # Diners Club
18
+ - id: 3
19
+ name: Diners Club
20
+ username: diners
21
+
22
+ # Discover
23
+ - id: 4
24
+ name: Discover
25
+ username: discover
26
+
27
+ # Elo
28
+ - id: 5
29
+ name: Elo
30
+ username: elo
31
+
32
+ # American Express
33
+ - id: 6
34
+ name: American Express
35
+ username: amex
36
+
37
+ # JCB
38
+ - id: 7
39
+ name: JCB
40
+ username: jcb
41
+
42
+ # Aura
43
+ - id: 8
44
+ name: Aura
45
+ username: aura
@@ -0,0 +1,35 @@
1
+ currencies:
2
+ # SampleObj
3
+ - id: 0
4
+ name: Nil
5
+ acronym: nil$
6
+ iso_code: NIL
7
+ iso_number: 000
8
+
9
+ # Real
10
+ - id: 1
11
+ name: Real
12
+ acronym: R$
13
+ iso_code: BRL
14
+ iso_number: 986
15
+
16
+ # Euro
17
+ - id: 2
18
+ name: Euro
19
+ acronym: €
20
+ iso_code: EUR
21
+ iso_number: 978
22
+
23
+ # US Dollar
24
+ - id: 3
25
+ name: US Dollar
26
+ acronym: US$
27
+ iso_code: USD
28
+ iso_number: 840
29
+
30
+ # CAD Dollar
31
+ - id: 4
32
+ name: CAD Dollar
33
+ acronym: CAD$
34
+ iso_code: CAD
35
+ iso_number: 124
@@ -0,0 +1,20 @@
1
+ payment_methods:
2
+ # SampleObj
3
+ - id: 0
4
+ name: Nil
5
+
6
+ # Credit
7
+ - id: 1
8
+ name: Credit
9
+
10
+ # Debit
11
+ - id: 2
12
+ name: Debit
13
+
14
+ # Installments shop
15
+ - id: 3
16
+ name: Installments shop
17
+
18
+ # Installments administrator
19
+ - id: 4
20
+ name: Installments administrator
@@ -0,0 +1,18 @@
1
+ recurrence_periods:
2
+ # Daily
3
+ daily: 1
4
+
5
+ # Weekly
6
+ weekly: 2
7
+
8
+ # Monthly
9
+ monthly: 3
10
+
11
+ # Bimonthly
12
+ bimonthly: 4
13
+
14
+ # Semiannual
15
+ semiannual: 5
16
+
17
+ # Annual
18
+ annual: 6
@@ -0,0 +1,31 @@
1
+ transaction_code:
2
+ 0:
3
+ status: pending
4
+ msg: Transação Criada
5
+ 1:
6
+ status: pending
7
+ msg: Transação em Andamento
8
+ 2:
9
+ status: pending
10
+ msg: Transação Autenticada
11
+ 3:
12
+ status: error
13
+ msg: Transação não Autenticada
14
+ 4:
15
+ status: pending
16
+ msg: Transação Autorizada
17
+ 5:
18
+ status: error
19
+ msg: Transação não Autorizada
20
+ 6:
21
+ status: charged
22
+ msg: Transação Capturada
23
+ 9:
24
+ status: error
25
+ msg: Transação Cancelada
26
+ 10:
27
+ status: pending
28
+ msg: Transação em Autenticação
29
+ 12:
30
+ status: pending
31
+ msg: Transação em Cancelamento
@@ -0,0 +1,4 @@
1
+ login: your_login
2
+ password: your_password
3
+ host: proxy_host
4
+ port: proxy_port
@@ -0,0 +1,173 @@
1
+ module NitroPay
2
+ class Connection
3
+ # Attrs
4
+ attr_accessor :auth
5
+ attr_accessor :path
6
+ attr_accessor :domain
7
+ attr_accessor :protocol
8
+ attr_accessor :end_point
9
+ attr_accessor :api_version
10
+ attr_accessor :recurrent_rid
11
+ attr_accessor :request_params
12
+ attr_accessor :end_point_versioned
13
+
14
+ # Constructor
15
+ def initialize(params = {})
16
+ # An work around added to prevent a lot of changes
17
+ params = params.merge({test_env:true}) if NitroPay.test_env
18
+ params = params.merge({debug:true}) if NitroPay.debug
19
+
20
+ # Static part
21
+ self.request_params = {transaction:params}
22
+ setup_config
23
+ self.domain = 'pay.nitrostart.me'
24
+
25
+ # If using test or Debug it is not production
26
+ if params[:debug] || params[:test]
27
+ self.protocol = 'http'
28
+ self.domain = 'pay.dev:4000'
29
+ else
30
+ # TODO when add SSL to Production replace http to https
31
+ self.protocol = 'http'
32
+ self.domain = 'pay.nitrostart.me'
33
+ end
34
+
35
+ self.api_version = 'v1'
36
+ self.end_point = "#{self.protocol}://#{self.domain}/api"
37
+ self.end_point_versioned = "#{self.protocol}://#{self.domain}/api/#{self.api_version}"
38
+
39
+ # Dynamic env
40
+ setup_default_app if params[:test_env]
41
+ setup_attrs(params)
42
+ self.recurrent_rid = params[:rid] unless params[:rid].nil?
43
+ end
44
+
45
+ # Full URL for the last request
46
+ def url_requested
47
+ "#{self.end_point}/#{self.api_version}/#{self.path}"
48
+ end
49
+
50
+ # GET http
51
+ def get_request
52
+ RestClient.get self.url_requested
53
+ end
54
+
55
+ # GET json
56
+ def get_json_request
57
+ resp = RestClient.get(self.url_requested)
58
+ to_hash_with_symbols(resp).it_keys_to_sym
59
+ end
60
+
61
+ # POST http
62
+ def post_request
63
+ RestClient.post self.url_requested, self.request_params
64
+ end
65
+
66
+ # POST json
67
+ def post_json_request
68
+ resp = RestClient.post(self.url_requested, self.request_params)
69
+ to_hash_with_symbols(resp)
70
+ end
71
+
72
+ # PUT http
73
+ def put_request
74
+ RestClient.put self.url_requested, self.request_params
75
+ end
76
+
77
+ # PUT json
78
+ def put_json_request
79
+ resp = RestClient.put(self.url_requested, self.request_params)
80
+ to_hash_with_symbols(resp)
81
+ end
82
+
83
+ # DELETE http
84
+ def delete_request
85
+ auth = self.request_params[:auth]
86
+ RestClient.delete self.url_requested, app_id:auth[:app_id], secret_key:auth[:secret_key]
87
+ end
88
+
89
+ # DELETE json
90
+ def delete_json_request
91
+ auth = self.request_params[:auth]
92
+ resp = RestClient.delete self.url_requested, auth_app_id:auth[:app_id], auth_secret_key:auth[:secret_key]
93
+ to_hash_with_symbols(resp)
94
+ end
95
+
96
+ # Callbacks
97
+ protected
98
+ # Config Attrs
99
+ def setup_config
100
+ self.auth = {app_id:NitroPay.app_id, secret_key:NitroPay.secret_key}
101
+ self.request_params.merge!(auth:self.auth)
102
+ end
103
+
104
+ # SetUp a default app
105
+ def setup_default_app
106
+ # setup test_app path
107
+ self.path = 'global_app'
108
+
109
+ # Get the App & setup config
110
+ app = get_json_request[:app]
111
+ NitroPay.app_id = app[:id]
112
+ NitroPay.secret_key = app[:secret]
113
+
114
+ # Get the GlobalRecurrent & setup/config
115
+ self.path = 'global_subscription'
116
+ recurrence = get_json_request
117
+ self.recurrent_rid = recurrence[:rid]
118
+
119
+ return puts 'Please run: rails g nitro_pay:install' if NitroPay.app_id.nil? || NitroPay.secret_key.nil?
120
+ self.auth = {app_id:NitroPay.app_id, secret_key:NitroPay.secret_key}
121
+ self.request_params.merge!(auth:self.auth)
122
+ end
123
+
124
+ # SetUp all attrs
125
+ def setup_attrs(params)
126
+ # Dynamic part
127
+ params.each do |key, value|
128
+ next unless key.to_s.index('[]').nil?
129
+ self.class.__send__(:attr_accessor, :"#{key}")
130
+ self.__send__("#{key}=", value)
131
+ end
132
+ end
133
+
134
+ # HTTP requests must have '[]' on it key name to send Array
135
+ def custom_http_params
136
+ setup_format_and_validators
137
+
138
+ return if self.sold_items.nil?
139
+ self.sold_items.each_with_index do |sold_item, i|
140
+ self.request_params[:transaction]["sold_items[#{i}]"] = sold_item
141
+ end
142
+ end
143
+
144
+ # Validate params to prevent errors like BAD Request & format values like value to Operator format
145
+ def setup_format_and_validators
146
+ validate_operator_format
147
+ end
148
+
149
+ # if necessary convert amount to operator value
150
+ def validate_operator_format
151
+ # prevent fatal error
152
+ return if self.amount.nil?
153
+
154
+ # aux vars
155
+ amount_str = self.amount.to_s
156
+ format_regex = /[.,]/
157
+
158
+ # if nil (it is not formatted, so it is not necessary to convert it format)
159
+ unless amount_str.match(format_regex).nil?
160
+ return if self.request_params.nil? || self.request_params[:transaction].nil?
161
+ self.amount = NitroPay::Currency.to_operator_str(self.amount)
162
+ self.request_params[:transaction][:amount] = self.amount
163
+ end
164
+ end
165
+
166
+ # Return the JSON in a Hash with it keys in symbols
167
+ def to_hash_with_symbols(json)
168
+ hashed = JSON.parse(json)
169
+ hashed.is_a?(Array) ? hashed.each_with_index { |hash, i| hashed[i] = hash.it_keys_to_sym } : hashed.it_keys_to_sym
170
+ hashed
171
+ end
172
+ end
173
+ end