liberalize_backend_ruby_sdk 0.0.1

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: e20b13b0363910a97e9cc09cbdf347889a4a7f5cfd3c08e8abcf1b71b64f86ab
4
+ data.tar.gz: 67626404df69d91b778b5425c2f1867f06f08ba3fa6d627161cfe568fa8b3187
5
+ SHA512:
6
+ metadata.gz: 7ca50d5e6b0c4edd8c4875a404f415e739000a772dbf3068868698537d0bb0967c91bc2049fb9ecae0f9c18e5228a0a5776266d52e6675f9027259a8e324c064
7
+ data.tar.gz: 71a6ec8fdae48d290a306fd06775dd37e2b6bd288b85f7d07f3170e34d41c56bd6e090891d64d87243709a08633bc18b81acc9790eb26d39aa5a5471fb4a3d96
@@ -0,0 +1,274 @@
1
+ require "base64"
2
+ require 'uri'
3
+ require 'net/http'
4
+ require 'json'
5
+
6
+ class LiberalizeBackendRubySdk
7
+
8
+ def initialize(privateKey, environment="production")
9
+
10
+ case environment
11
+ when "production"
12
+ @paymentApi = "https://payment.api.liberalize.io/payments"
13
+ @customerApi = "https://customer.api.liberalize.io"
14
+ when "staging"
15
+ @paymentApi = "https://payment.api.staging.liberalize.io/payments"
16
+ @customerApi = "https://customer.api.staging.liberalize.io"
17
+ when "dev"
18
+ @paymentApi = "https://payment.api.dev.liberalize.io/payments"
19
+ @customerApi = "https://customer.api.dev.liberalize.io"
20
+ when "local"
21
+ @paymentApi = "https://payment.api.dev.liberalize.io/payments"
22
+ @customerApi = "https://customer.api.dev.liberalize.io"
23
+ end
24
+
25
+ @privateKey = Base64.strict_encode64("#{privateKey}:")
26
+
27
+ end
28
+
29
+ def createPayment(requestBody, libService="elements")
30
+ begin
31
+ validatedRequest = {}
32
+ requestBody.each do |key, value|
33
+ case "#{key}"
34
+ when "amount"
35
+ target_amount = value.to_i
36
+ validatedRequest[:amount] = target_amount
37
+ when "source"
38
+ if value.is_a?(String)
39
+ validatedRequest[:source] = value
40
+ else
41
+ validatedRequest[:source] = "lib:customer:paymentMethods/#{value[:paymentMethodId]}"
42
+ end
43
+ else
44
+ validatedRequest[key] = value
45
+ end
46
+ end
47
+
48
+ uri = URI.parse(@paymentApi)
49
+ body = validatedRequest.to_json
50
+ header = {
51
+ "Content-Type": "application/json",
52
+ "x-lib-pos-type": "#{libService}",
53
+ "Authorization": "Basic #{@privateKey}",
54
+ }
55
+
56
+ http = Net::HTTP.new(uri.host, uri.port)
57
+ # http.set_debug_output($stdout)
58
+ http.use_ssl = true
59
+ request = Net::HTTP::Post.new(uri.request_uri, header)
60
+ request.body = body
61
+
62
+ response = http.request(request)
63
+
64
+ if response.code.to_i >= 200 && response.code.to_i < 300
65
+ return response.body
66
+ else
67
+ return response
68
+ end
69
+ rescue Exception => exception
70
+
71
+ return exception
72
+ end
73
+ end
74
+
75
+ def authorizePayment(requestBody, libService="elements")
76
+ begin
77
+ validatedRequest = {}
78
+ paymentId = ""
79
+ requestBody.each do |key, value|
80
+ case "#{key}"
81
+ when "source"
82
+ if value.is_a?(String)
83
+ validatedRequest[:source] = value
84
+ else
85
+ validatedRequest[:source] = "lib:customer:paymentMethods/#{value[:paymentMethodId]}"
86
+ end
87
+ when "paymentId"
88
+ paymentId = value
89
+ else
90
+ validatedRequest[key] = value
91
+ end
92
+ end
93
+
94
+ uri = URI.parse(@paymentApi + "/#{paymentId}/authorizations")
95
+ body = validatedRequest.to_json
96
+ header = {
97
+ "Content-Type": "application/json",
98
+ "x-lib-pos-type": "#{libService}",
99
+ "Authorization": "Basic #{@privateKey}",
100
+ }
101
+
102
+ http = Net::HTTP.new(uri.host, uri.port)
103
+ # http.set_debug_output($stdout)
104
+ http.use_ssl = true
105
+ request = Net::HTTP::Post.new(uri.request_uri, header)
106
+ request.body = body
107
+
108
+ response = http.request(request)
109
+
110
+ if response.code.to_i >= 200 && response.code.to_i < 300
111
+ return response.body
112
+ else
113
+ return response
114
+ end
115
+ rescue Exception => exception
116
+
117
+ return exception
118
+ end
119
+ end
120
+
121
+ def capturePayment(requestBody, libService="elements")
122
+ begin
123
+ validatedRequest = {}
124
+ paymentId = ""
125
+ requestBody.each do |key, value|
126
+ case "#{key}"
127
+ when "amount"
128
+ target_amount = value.to_i
129
+ validatedRequest[:amount] = target_amount
130
+ when "paymentId"
131
+ paymentId = value
132
+ else
133
+ validatedRequest[key] = value
134
+ end
135
+ end
136
+
137
+ uri = URI.parse(@paymentApi + "/#{paymentId}/captures")
138
+ body = validatedRequest.to_json
139
+ header = {
140
+ "Content-Type": "application/json",
141
+ "x-lib-pos-type": "#{libService}",
142
+ "Authorization": "Basic #{@privateKey}",
143
+ }
144
+
145
+ http = Net::HTTP.new(uri.host, uri.port)
146
+ # http.set_debug_output($stdout)
147
+ http.use_ssl = true
148
+ request = Net::HTTP::Post.new(uri.request_uri, header)
149
+ request.body = body
150
+
151
+ response = http.request(request)
152
+
153
+ if response.code.to_i >= 200 && response.code.to_i < 300
154
+ return response.body
155
+ else
156
+ return response
157
+ end
158
+ rescue Exception => exception
159
+
160
+ return exception
161
+ end
162
+ end
163
+
164
+ def refundPayment(requestBody, libService="elements")
165
+ begin
166
+ validatedRequest = {}
167
+ paymentId = ""
168
+ requestBody.each do |key, value|
169
+ case "#{key}"
170
+ when "amount"
171
+ target_amount = value.to_i
172
+ validatedRequest[:amount] = target_amount
173
+ when "paymentId"
174
+ paymentId = value
175
+ else
176
+ validatedRequest[key] = value
177
+ end
178
+ end
179
+
180
+ uri = URI.parse(@paymentApi + "/#{paymentId}/refunds")
181
+ body = validatedRequest.to_json
182
+ header = {
183
+ "Content-Type": "application/json",
184
+ "x-lib-pos-type": "#{libService}",
185
+ "Authorization": "Basic #{@privateKey}",
186
+ }
187
+
188
+ http = Net::HTTP.new(uri.host, uri.port)
189
+ http.set_debug_output($stdout)
190
+ http.use_ssl = true
191
+ request = Net::HTTP::Post.new(uri.request_uri, header)
192
+ request.body = body
193
+
194
+ response = http.request(request)
195
+
196
+ if response.code.to_i >= 200 && response.code.to_i < 300
197
+ return response.body
198
+ else
199
+ return response
200
+ end
201
+ rescue Exception => exception
202
+
203
+ return exception
204
+ end
205
+ end
206
+
207
+ def voidPayment(requestBody, libService="elements")
208
+ begin
209
+ validatedRequest = {}
210
+ paymentId = ""
211
+ requestBody.each do |key, value|
212
+ case "#{key}"
213
+ when "paymentId"
214
+ paymentId = value
215
+ else
216
+ validatedRequest[key] = value
217
+ end
218
+ end
219
+
220
+ uri = URI.parse(@paymentApi + "/#{paymentId}/voids")
221
+ body = validatedRequest.to_json
222
+ header = {
223
+ "Content-Type": "application/json",
224
+ "x-lib-pos-type": "#{libService}",
225
+ "Authorization": "Basic #{@privateKey}",
226
+ }
227
+
228
+ http = Net::HTTP.new(uri.host, uri.port)
229
+ # http.set_debug_output($stdout)
230
+ http.use_ssl = true
231
+ request = Net::HTTP::Post.new(uri.request_uri, header)
232
+ request.body = body
233
+
234
+ response = http.request(request)
235
+
236
+ if response.code.to_i >= 200 && response.code.to_i < 300
237
+ return response.body
238
+ else
239
+ return response
240
+ end
241
+ rescue Exception => exception
242
+
243
+ return exception
244
+ end
245
+ end
246
+
247
+ def getPayment(paymentId, libService="elements")
248
+ begin
249
+ uri = URI.parse(@paymentApi + "/#{paymentId}")
250
+ header = {
251
+ "Content-Type": "application/json",
252
+ "x-lib-pos-type": "#{libService}",
253
+ "Authorization": "Basic #{@privateKey}",
254
+ }
255
+
256
+ http = Net::HTTP.new(uri.host, uri.port)
257
+ # http.set_debug_output($stdout)
258
+ http.use_ssl = true
259
+ request = Net::HTTP::Get.new(uri.request_uri, header)
260
+
261
+ response = http.request(request)
262
+
263
+ if response.code.to_i >= 200 && response.code.to_i < 300
264
+ return response.body
265
+ else
266
+ return response
267
+ end
268
+ rescue Exception => exception
269
+
270
+ return exception
271
+ end
272
+ end
273
+
274
+ end
metadata ADDED
@@ -0,0 +1,44 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: liberalize_backend_ruby_sdk
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Kenneth Wu
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-10-21 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Liberalize Gem for Ruby Backend Framework
14
+ email: support@liberalize.io
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/liberalize_backend_ruby_sdk.rb
20
+ homepage: https://rubygems.org/gems/liberalize_backend_ruby_sdk
21
+ licenses:
22
+ - MIT
23
+ metadata:
24
+ source_code_uri: https://github.com/LiberationNetwork/liberalize-backend-ruby-sdk
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubygems_version: 3.0.3
41
+ signing_key:
42
+ specification_version: 4
43
+ summary: Start receiving payments with Liberalize
44
+ test_files: []