payment_gateway 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/payment_gateway.rb +224 -0
  3. metadata +124 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f35e5e66c22b9eaa3c3b9d89a866b3340122b740
4
+ data.tar.gz: d3604afb3a2ec015459d964a83dec3ab0ad91923
5
+ SHA512:
6
+ metadata.gz: b363f72db5815432da279c3fd6f19568531e437531302f66ec710b8a2abfb97a572ba41f34db2257f93078366d2e911f3de47c98f4152127e9d57479d54b814c
7
+ data.tar.gz: 17e00270b9e77bc725368d0d072b3bec48f45a4d96bfca9756a31c1316ab39246831b2f3fcd22918171e5f3dc5ce0225386fd8a5bc78fb5fa72ddb58d51670bc
@@ -0,0 +1,224 @@
1
+ require 'cgi'
2
+ require 'logger'
3
+ require 'yaml'
4
+ require 'rest-client'
5
+ require 'base64'
6
+
7
+ #module Payment
8
+
9
+ class DefaultLogger
10
+
11
+ attr_accessor :request, :response
12
+
13
+ def log(request, response)
14
+ self.request ||= request
15
+ self.response ||= response
16
+
17
+ puts self.inspect
18
+ end
19
+
20
+ def inspect
21
+ res = ''
22
+ res += ' Request: ' + (self.request ? self.request.to_s : '')
23
+ res += ' Response: ' + (self.response ? self.response.to_s : '')
24
+
25
+ return res
26
+ rescue => e
27
+ # don't raise an exception, default logger fails silently
28
+ return e.message
29
+ end
30
+
31
+ end # Default logger
32
+
33
+ class PaymentGateway
34
+
35
+ # constants forbasic URLS and method names
36
+ REST_API = '/api/rest'
37
+ TEST_HOST = 'test.paymentgateway.hu'
38
+ PROD_HOST = 'paymentgateway.hu'
39
+ INIT = 'Init'
40
+ RESULT = 'Result'
41
+ CLOSE = 'Close'
42
+
43
+ @@config = {
44
+ :provider => 'OTP',
45
+ :store => 'sdk_test',
46
+ :currency => 'HUF',
47
+ :language => 'HU',
48
+ :host => '',
49
+ :header_host => PROD_HOST,
50
+ :port => '',
51
+ :use_ssl => 'true',
52
+ :auto_commit_providers => ['MPP2'],
53
+ :auto_commit_not_implemented => ['OTPayMP'],
54
+ :app_host => '',
55
+ :api_key => '86af3-80e4f-f8228-9498f-910ad'
56
+ }
57
+
58
+ @@valid_config_keys = @@config.keys
59
+
60
+ attr_accessor :provider, :response_url, :amount, :currency, :order_id, :user_id, :language, :approved, :transaction_id
61
+
62
+ # config through hash
63
+ def self.configure(opts = {})
64
+ opts.each { |k,v| @@config[k.to_sym] = v if @@valid_config_keys.include?(k.to_sym) }
65
+ end
66
+
67
+ # config through yaml file
68
+ def self.configure_with(path_to_yaml_file)
69
+ begin
70
+ config = YAML::load(IO.read(path_to_yaml_file))
71
+ rescue Errno::ENOENT
72
+ log(:warning, "YAML configuration file couldn't be found. Using defaults."); return
73
+ rescue Psych::SyntaxError
74
+ log(:warning, "YAML configuration file contains invalid syntax. Using defaults."); return
75
+ end
76
+
77
+ configure(config)
78
+ end
79
+
80
+ def self.config
81
+ @@config
82
+ end
83
+
84
+ def initialize(provider = @@config[:provider])
85
+ self.provider = provider
86
+ end
87
+
88
+ # The +init+ instance method initializes the payment transaction at
89
+ # PaymentGateway. Returns response as a hash.
90
+ # === Attributes
91
+ # * _provider_ optional, initialize sets it to defaults
92
+ # * _response_url_ mandatory, relative to the app host
93
+ # * _amount_ amount to charge the customer
94
+ # * _order_id_ optional, vendor-specific id of the order
95
+ # * _user_id_ optional, vendor-specific id of the user
96
+ # === Parameters
97
+ # * _logger_ optional logger to use (must implement <tt>log(request_param_string, response_param_string)</tt>)
98
+ # === Example
99
+ # pg = PaymentGateway.new
100
+ # pg.provider = provider
101
+ # pg.response_url = some_url
102
+ # pg.amount = 3000
103
+ # pg.currency = "HUF"
104
+ # pg.order_id = "order123"
105
+ # pg.user_id = "user123"
106
+ # pg.language = "HU"
107
+ # response = pg.init(logger)
108
+ def init(logger = DefaultLogger.new)
109
+ request_hash = {
110
+ 'ProviderName' => provider,
111
+ 'StoreName' => @@config[:store],
112
+ 'ResponseUrl' => CGI::escape(@@config[:app_host] + response_url),
113
+ 'Amount' => amount.to_s,
114
+ 'OrderId' => order_id.to_s,
115
+ 'UserId' => user_id.to_s,
116
+ 'Currency' => @@config[:currency],
117
+ 'Language' => @@config[:language],
118
+ 'AutoCommit' => (@@config[:auto_commit_providers].include?(provider).to_s if !@@config[:auto_commit_not_implemented].include?(provider))
119
+ }
120
+
121
+ result = submit_request(INIT, request_hash)
122
+
123
+ logger.log(request_hash.to_s, result.to_s)
124
+
125
+ return result
126
+
127
+ rescue => e
128
+ logger.log(request_hash.to_s, result.to_s)
129
+ raise e
130
+ end
131
+
132
+ # The +start+ instance method composes the url the user has to be redirected to.
133
+ # === Attributes
134
+ # * _transaction_id_ mandatory
135
+ # === Example
136
+ # pg = PaymentGateway.new
137
+ # pg.transaction_id = '123456789abc'
138
+ # redirect_to pg.start
139
+ def start(logger = DefaultLogger.new)
140
+ url = 'http://' + @@config[:header_host] + '/Start?TransactionId=' + transaction_id.to_s
141
+ logger.log(url, nil)
142
+ return url
143
+ end
144
+
145
+ # The +result+ instance method queries the status of the payment from PG, returns
146
+ # result in a hash.
147
+ # === Attributes
148
+ # * _transaction_id_ mandatory, PG transaction id, returned by init
149
+ # === Parameters
150
+ # * _logger_ (optional) = logger to use (must implement <tt>log(request_param_string, response_param_string)</tt>)
151
+ # === Example
152
+ # pg = PaymentGateway.new
153
+ # pg.transaction_id = '123456789abc'
154
+ # result = pg.result
155
+ def result(logger = DefaultLogger.new)
156
+ request_hash = {
157
+ 'TransactionId' => transaction_id.to_s
158
+ }
159
+
160
+ result = submit_request(RESULT, request_hash)
161
+
162
+ logger.log(request_hash.to_s, result.to_s)
163
+
164
+ return result
165
+
166
+ rescue => e
167
+ logger.log(request_hash.to_s, result.to_s)
168
+ raise e
169
+ end
170
+
171
+ # The +close+ instance method finalizes the payment in one of the following
172
+ # ways: 1) cancel the authorization hold on the customer's credit or debit card,
173
+ # or 2) submit the transaction and thus effectively charging the customer's
174
+ # card.
175
+ # === Attributes
176
+ # * _transaction_id_ mandatory PG transaction id, returned by init
177
+ # * _approve_ (optional, boolean) cancel, or submit, defaults to true
178
+ # === Parameters
179
+ # * _logger_ (optional) = logger to use (must implement <tt>log(request_param_string, response_param_string)</tt>
180
+ # === Example
181
+ # pg = PaymentGateway.new
182
+ # pg.transaction_id = '1234hfajl'
183
+ # pg.approve = false
184
+ # pg.close
185
+ def close(logger = DefaultLogger.new)
186
+ request_hash = {
187
+ 'TransactionId' => transaction_id.to_s,
188
+ 'Approved' => approved.to_s || true
189
+ }
190
+
191
+ result = submit_request(CLOSE, request_hash)
192
+
193
+ logger.log(request_hash.to_s, result.to_s)
194
+
195
+ return result
196
+
197
+ rescue => e
198
+ logger.log(request_hash.to_s, result.to_s)
199
+ raise e
200
+ end
201
+
202
+ private
203
+
204
+ # +submit_request+ takes the method name and the request parameters as a hash,
205
+ # uses RestClient to submit the request to the PaymentGateway REST API,
206
+ # and returs the parsed response as hash.
207
+ # === Parameters
208
+ # * _method_ = method name, eg.: 'Init'
209
+ # * _request_hash_ = request parameters as hash
210
+ # === Example
211
+ # submit_request('Result', {'TransactionId' => '123456789abc'})
212
+ def submit_request(method, request_hash)
213
+ header_key = Base64.encode64(@@config[:store] + ':' + @@config[:api_key])
214
+ response = RestClient.post(
215
+ @@config[:header_host] + REST_API,
216
+ {:method => method, :json => request_hash.to_json},
217
+ :accept => :json,
218
+ :Authorization => "Basic #{header_key}")
219
+ JSON.load(response).to_hash
220
+ end
221
+
222
+ end # class PaymentGateway
223
+
224
+ #end
metadata ADDED
@@ -0,0 +1,124 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: payment_gateway
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Gábor BÁNHEGYESI
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-12-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '3.4'
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
22
+ version: 3.4.0
23
+ type: :development
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '3.4'
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: 3.4.0
33
+ - !ruby/object:Gem::Dependency
34
+ name: webmock
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ~>
38
+ - !ruby/object:Gem::Version
39
+ version: '1.22'
40
+ - - ! '>='
41
+ - !ruby/object:Gem::Version
42
+ version: 1.22.3
43
+ type: :development
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ~>
48
+ - !ruby/object:Gem::Version
49
+ version: '1.22'
50
+ - - ! '>='
51
+ - !ruby/object:Gem::Version
52
+ version: 1.22.3
53
+ - !ruby/object:Gem::Dependency
54
+ name: sinatra
55
+ requirement: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ~>
58
+ - !ruby/object:Gem::Version
59
+ version: 1.4.0
60
+ - - ! '>='
61
+ - !ruby/object:Gem::Version
62
+ version: 1.4.6
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: 1.4.0
70
+ - - ! '>='
71
+ - !ruby/object:Gem::Version
72
+ version: 1.4.6
73
+ - !ruby/object:Gem::Dependency
74
+ name: rest-client
75
+ requirement: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ~>
78
+ - !ruby/object:Gem::Version
79
+ version: '1.8'
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: 1.8.0
83
+ type: :runtime
84
+ prerelease: false
85
+ version_requirements: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ~>
88
+ - !ruby/object:Gem::Version
89
+ version: '1.8'
90
+ - - ! '>='
91
+ - !ruby/object:Gem::Version
92
+ version: 1.8.0
93
+ description: Use BigFish's payment methods in your ruby project.
94
+ email: banhill@gmail.com
95
+ executables: []
96
+ extensions: []
97
+ extra_rdoc_files: []
98
+ files:
99
+ - lib/payment_gateway.rb
100
+ homepage:
101
+ licenses:
102
+ - MIT
103
+ metadata: {}
104
+ post_install_message:
105
+ rdoc_options: []
106
+ require_paths:
107
+ - lib
108
+ required_ruby_version: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - ! '>='
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ required_rubygems_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ requirements: []
119
+ rubyforge_project:
120
+ rubygems_version: 2.2.2
121
+ signing_key:
122
+ specification_version: 4
123
+ summary: Gem for Bigfish's PaymentGateway service
124
+ test_files: []