paybox_direct 0.1.3 → 0.2.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 +4 -4
- data/lib/paybox_direct.rb +107 -108
- data/lib/paybox_direct/request.rb +27 -3
- data/spec/request_spec.rb +14 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d901596ca4dfc8d6cc31a037c6226d752d5deade
|
4
|
+
data.tar.gz: bfa78c3eeceb6bbd5a002d12f0074057f50eb8dc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ba3dd9e300e848cf51767c906f5b2b0be01a6497f361155320c286cebab22154d9e5bd4f0743987c6927c6d3989b0a4df30574c781a2a86d403056c49c6dc904
|
7
|
+
data.tar.gz: a476ab097a23f45cd154f513b7bb3846c85674e53601b9e257fed5b86cbdde74ff9bb373c1381b3e18bf5742cd8a4550379cc4cf0b48d70f294fa18a6cd2e510
|
data/lib/paybox_direct.rb
CHANGED
@@ -65,43 +65,44 @@ module PayboxDirect
|
|
65
65
|
# == Raises:
|
66
66
|
# * PayboxDirect::AuthorizationError, if authorization fails
|
67
67
|
# * PayboxDirect::ServerUnavailableError, if Paybox server is unavailable
|
68
|
-
def self.authorize(
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
raise ArgumentError, "
|
78
|
-
|
68
|
+
def self.authorize(amount:,
|
69
|
+
currency:,
|
70
|
+
ref:,
|
71
|
+
cc_number: nil,
|
72
|
+
wallet: nil,
|
73
|
+
cc_expire:,
|
74
|
+
cc_cvv:,
|
75
|
+
subscriber: nil,
|
76
|
+
debit: false)
|
77
|
+
raise ArgumentError, "amount: Expecting Numeric" unless amount.is_a? Numeric
|
78
|
+
raise ArgumentError, "currency: Not supported" unless CURRENCIES.has_key? currency
|
79
|
+
raise ArgumentError, "cc_expire: Expecting Date" unless cc_expire.is_a? Date
|
79
80
|
|
80
|
-
if
|
81
|
-
if
|
82
|
-
raise ArgumentError, "cc_number: Unexpected when `wallet` provided"
|
83
|
-
op_code =
|
81
|
+
if !subscriber.nil?
|
82
|
+
if !wallet.nil?
|
83
|
+
raise ArgumentError, "cc_number: Unexpected when `wallet` provided" unless cc_number.nil?
|
84
|
+
op_code = debit ? 53 : 51
|
84
85
|
else
|
85
|
-
raise ArgumentError, "Expecting `cc_number` option"
|
86
|
+
raise ArgumentError, "Expecting `cc_number` option" if cc_number.nil?
|
86
87
|
op_code = 56 # Paybox can't create a new subscriber with immediate debit
|
87
88
|
end
|
88
89
|
else
|
89
|
-
raise ArgumentError, "Expecting `cc_number` option"
|
90
|
-
raise ArgumentError, "Unexpected `wallet` option"
|
91
|
-
op_code =
|
90
|
+
raise ArgumentError, "Expecting `cc_number` option" if cc_number.nil?
|
91
|
+
raise ArgumentError, "Unexpected `wallet` option" unless wallet.nil?
|
92
|
+
op_code = debit ? 3 : 1
|
92
93
|
end
|
93
94
|
|
94
95
|
vars = {
|
95
96
|
"TYPE" => op_code.to_s.rjust(5, "0"),
|
96
|
-
"REFERENCE" => @@config.ref_prefix +
|
97
|
-
"MONTANT" => (
|
98
|
-
"DEVISE" => CURRENCIES[
|
99
|
-
"PORTEUR" =>
|
100
|
-
"DATEVAL" =>
|
101
|
-
"CVV" =>
|
97
|
+
"REFERENCE" => @@config.ref_prefix + ref,
|
98
|
+
"MONTANT" => (amount.round(2) * 100).round.to_s.rjust(10, "0"),
|
99
|
+
"DEVISE" => CURRENCIES[currency].to_s.rjust(3, "0"),
|
100
|
+
"PORTEUR" => !wallet.nil? ? wallet : cc_number.gsub(/[ -.]/, ""),
|
101
|
+
"DATEVAL" => cc_expire.strftime("%m%y"),
|
102
|
+
"CVV" => cc_cvv
|
102
103
|
}
|
103
|
-
if
|
104
|
-
vars["REFABONNE"] = @@config.ref_prefix +
|
104
|
+
if !subscriber.nil?
|
105
|
+
vars["REFABONNE"] = @@config.ref_prefix + subscriber
|
105
106
|
end
|
106
107
|
req = Request.new(vars)
|
107
108
|
req.execute!
|
@@ -117,11 +118,11 @@ module PayboxDirect
|
|
117
118
|
req.response[:wallet] = req.fields["PORTEUR"]
|
118
119
|
|
119
120
|
# We now execute debit after authorization-only operation #56
|
120
|
-
if
|
121
|
+
if debit
|
121
122
|
sleep 1 # Paybox recommends to wait a few seconds between the authorization and the debit
|
122
123
|
debit_authorization(
|
123
|
-
amount:
|
124
|
-
currency:
|
124
|
+
amount: amount,
|
125
|
+
currency: currency,
|
125
126
|
request_id: req.request_id,
|
126
127
|
transaction_id: req.response[:transaction_id]
|
127
128
|
)
|
@@ -154,23 +155,21 @@ module PayboxDirect
|
|
154
155
|
# == Raises:
|
155
156
|
# * PayboxDirect::DebitError, if debit fails
|
156
157
|
# * PayboxDirect::ServerUnavailableError, if Paybox server is unavailable
|
157
|
-
def self.debit_authorization(
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
raise ArgumentError, "Expecting
|
162
|
-
raise ArgumentError, "
|
163
|
-
raise ArgumentError, "
|
164
|
-
raise ArgumentError, "
|
165
|
-
raise ArgumentError, "request_id: Expecting Fixnum" unless opts[:request_id].is_a? Fixnum
|
166
|
-
raise ArgumentError, "transaction_id: Expecting Fixnum" unless opts[:transaction_id].is_a? Fixnum
|
158
|
+
def self.debit_authorization(amount:,
|
159
|
+
currency:,
|
160
|
+
request_id:,
|
161
|
+
transaction_id:)
|
162
|
+
raise ArgumentError, "amount: Expecting Numeric" unless amount.is_a? Numeric
|
163
|
+
raise ArgumentError, "currency: Not supported" unless CURRENCIES.has_key? currency
|
164
|
+
raise ArgumentError, "request_id: Expecting Fixnum" unless request_id.is_a? Fixnum
|
165
|
+
raise ArgumentError, "transaction_id: Expecting Fixnum" unless transaction_id.is_a? Fixnum
|
167
166
|
|
168
167
|
req = Request.new({
|
169
168
|
"TYPE" => "00002",
|
170
|
-
"MONTANT" => (
|
171
|
-
"DEVISE" => CURRENCIES[
|
172
|
-
"NUMAPPEL" =>
|
173
|
-
"NUMTRANS" =>
|
169
|
+
"MONTANT" => (amount.round(2) * 100).round.to_s.rjust(10, "0"),
|
170
|
+
"DEVISE" => CURRENCIES[currency].to_s.rjust(3, "0"),
|
171
|
+
"NUMAPPEL" => request_id.to_s.rjust(10, "0"),
|
172
|
+
"NUMTRANS" => transaction_id.to_s.rjust(10, "0")
|
174
173
|
})
|
175
174
|
req.execute!
|
176
175
|
|
@@ -202,42 +201,43 @@ module PayboxDirect
|
|
202
201
|
# == Raises:
|
203
202
|
# * PayboxDirect::CancelError, if cancellation fails
|
204
203
|
# * PayboxDirect::ServerUnavailableError, if Paybox server is unavailable
|
205
|
-
def self.cancel(
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
raise ArgumentError, "
|
215
|
-
raise ArgumentError, "
|
216
|
-
raise ArgumentError, "
|
217
|
-
raise ArgumentError, "
|
204
|
+
def self.cancel(amount:,
|
205
|
+
currency:,
|
206
|
+
ref:,
|
207
|
+
wallet: nil,
|
208
|
+
cc_expire: nil,
|
209
|
+
cc_cvv:,
|
210
|
+
subscriber: nil,
|
211
|
+
request_id:,
|
212
|
+
transaction_id:)
|
213
|
+
raise ArgumentError, "amount: Expecting Numeric" unless amount.is_a? Numeric
|
214
|
+
raise ArgumentError, "currency: Not supported" unless CURRENCIES.has_key? currency
|
215
|
+
raise ArgumentError, "cc_expire: Expecting Date" unless cc_expire.is_a? Date
|
216
|
+
raise ArgumentError, "request_id: Expecting Numeric" unless request_id.is_a? Numeric
|
217
|
+
raise ArgumentError, "transaction_id: Expecting Numeric" unless transaction_id.is_a? Numeric
|
218
218
|
|
219
|
-
if
|
220
|
-
raise ArgumentError, "Expecting `wallet` option"
|
221
|
-
raise ArgumentError, "Expecting `cc_expire` option"
|
219
|
+
if !subscriber.nil?
|
220
|
+
raise ArgumentError, "Expecting `wallet` option" if wallet.nil?
|
221
|
+
raise ArgumentError, "Expecting `cc_expire` option" if cc_expire.nil?
|
222
222
|
op_code = 55
|
223
223
|
else
|
224
|
-
raise ArgumentError, "Unexpected `wallet` option"
|
224
|
+
raise ArgumentError, "Unexpected `wallet` option" unless wallet.nil?
|
225
225
|
op_code = 5
|
226
226
|
end
|
227
227
|
|
228
228
|
vars = {
|
229
229
|
"TYPE" => op_code.to_s.rjust(5, "0"),
|
230
|
-
"REFERENCE" => @@config.ref_prefix +
|
231
|
-
"MONTANT" => (
|
232
|
-
"DEVISE" => CURRENCIES[
|
233
|
-
"DATEVAL" =>
|
234
|
-
"CVV" =>
|
235
|
-
"NUMAPPEL" =>
|
236
|
-
"NUMTRANS" =>
|
230
|
+
"REFERENCE" => @@config.ref_prefix + ref,
|
231
|
+
"MONTANT" => (amount.round(2) * 100).round.to_s.rjust(10, "0"),
|
232
|
+
"DEVISE" => CURRENCIES[currency].to_s.rjust(3, "0"),
|
233
|
+
"DATEVAL" => cc_expire.strftime("%m%y"),
|
234
|
+
"CVV" => cc_cvv,
|
235
|
+
"NUMAPPEL" => request_id.to_s.rjust(10, "0"),
|
236
|
+
"NUMTRANS" => transaction_id.to_s.rjust(10, "0")
|
237
237
|
}
|
238
|
-
if
|
239
|
-
vars["PORTEUR"] =
|
240
|
-
vars["REFABONNE"] = @@config.ref_prefix +
|
238
|
+
if !subscriber.nil?
|
239
|
+
vars["PORTEUR"] = wallet
|
240
|
+
vars["REFABONNE"] = @@config.ref_prefix + subscriber
|
241
241
|
end
|
242
242
|
req = Request.new(vars)
|
243
243
|
req.execute!
|
@@ -264,23 +264,21 @@ module PayboxDirect
|
|
264
264
|
# == Raises:
|
265
265
|
# * PayboxDirect::RefundError, if cancellation fails
|
266
266
|
# * PayboxDirect::ServerUnavailableError, if Paybox server is unavailable
|
267
|
-
def self.refund(
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
raise ArgumentError, "Expecting
|
272
|
-
raise ArgumentError, "
|
273
|
-
raise ArgumentError, "
|
274
|
-
raise ArgumentError, "
|
275
|
-
raise ArgumentError, "request_id: Expecting Numeric" unless opts[:request_id].is_a? Numeric
|
276
|
-
raise ArgumentError, "transaction_id: Expecting Numeric" unless opts[:transaction_id].is_a? Numeric
|
267
|
+
def self.refund(amount:,
|
268
|
+
currency:,
|
269
|
+
request_id:,
|
270
|
+
transaction_id:)
|
271
|
+
raise ArgumentError, "amount: Expecting Numeric" unless amount.is_a? Numeric
|
272
|
+
raise ArgumentError, "currency: Not supported" unless CURRENCIES.has_key? currency
|
273
|
+
raise ArgumentError, "request_id: Expecting Numeric" unless request_id.is_a? Numeric
|
274
|
+
raise ArgumentError, "transaction_id: Expecting Numeric" unless transaction_id.is_a? Numeric
|
277
275
|
|
278
276
|
req = Request.new({
|
279
277
|
"TYPE" => "00014",
|
280
|
-
"MONTANT" => (
|
281
|
-
"DEVISE" => CURRENCIES[
|
282
|
-
"NUMAPPEL" =>
|
283
|
-
"NUMTRANS" =>
|
278
|
+
"MONTANT" => (amount.round(2) * 100).round.to_s.rjust(10, "0"),
|
279
|
+
"DEVISE" => CURRENCIES[currency].to_s.rjust(3, "0"),
|
280
|
+
"NUMAPPEL" => request_id.to_s.rjust(10, "0"),
|
281
|
+
"NUMTRANS" => transaction_id.to_s.rjust(10, "0")
|
284
282
|
})
|
285
283
|
req.execute!
|
286
284
|
|
@@ -311,38 +309,39 @@ module PayboxDirect
|
|
311
309
|
# == Raises:
|
312
310
|
# * PayboxDirect::CreditError, if credit fails
|
313
311
|
# * PayboxDirect::ServerUnavailableError, if Paybox server is unavailable
|
314
|
-
def self.credit(
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
raise ArgumentError, "
|
323
|
-
raise ArgumentError, "
|
312
|
+
def self.credit(amount:,
|
313
|
+
currency:,
|
314
|
+
ref:,
|
315
|
+
cc_number: nil,
|
316
|
+
wallet: nil,
|
317
|
+
cc_expire:,
|
318
|
+
cc_cvv:,
|
319
|
+
subscriber: nil)
|
320
|
+
raise ArgumentError, "amount: Expecting Numeric" unless amount.is_a? Numeric
|
321
|
+
raise ArgumentError, "currency: Not supported" unless CURRENCIES.has_key? currency
|
322
|
+
raise ArgumentError, "cc_expire: Expecting Date" unless cc_expire.is_a? Date
|
324
323
|
|
325
|
-
if
|
326
|
-
raise ArgumentError, "Expecting `wallet` option"
|
327
|
-
raise ArgumentError, "cc_number: Unexpected when `wallet` provided"
|
324
|
+
if !subscriber.nil?
|
325
|
+
raise ArgumentError, "Expecting `wallet` option" if wallet.nil?
|
326
|
+
raise ArgumentError, "cc_number: Unexpected when `wallet` provided" unless cc_number.nil?
|
328
327
|
op_code = 54
|
329
328
|
else
|
330
|
-
raise ArgumentError, "Expecting `cc_number` option"
|
331
|
-
raise ArgumentError, "Unexpected `wallet` option"
|
329
|
+
raise ArgumentError, "Expecting `cc_number` option" if cc_number.nil?
|
330
|
+
raise ArgumentError, "Unexpected `wallet` option" unless wallet.nil?
|
332
331
|
op_code = 4
|
333
332
|
end
|
334
333
|
|
335
334
|
vars = {
|
336
335
|
"TYPE" => op_code.to_s.rjust(5, "0"),
|
337
|
-
"REFERENCE" => @@config.ref_prefix +
|
338
|
-
"MONTANT" => (
|
339
|
-
"DEVISE" => CURRENCIES[
|
340
|
-
"PORTEUR" =>
|
341
|
-
"DATEVAL" =>
|
342
|
-
"CVV" =>
|
336
|
+
"REFERENCE" => @@config.ref_prefix + ref,
|
337
|
+
"MONTANT" => (amount.round(2) * 100).round.to_s.rjust(10, "0"),
|
338
|
+
"DEVISE" => CURRENCIES[currency].to_s.rjust(3, "0"),
|
339
|
+
"PORTEUR" => !wallet.nil? ? wallet : cc_number.gsub(/[ -.]/, ""),
|
340
|
+
"DATEVAL" => cc_expire.strftime("%m%y"),
|
341
|
+
"CVV" => cc_cvv
|
343
342
|
}
|
344
|
-
if
|
345
|
-
vars["REFABONNE"] = @@config.ref_prefix +
|
343
|
+
if !subscriber.nil?
|
344
|
+
vars["REFABONNE"] = @@config.ref_prefix + subscriber
|
346
345
|
end
|
347
346
|
req = Request.new(vars)
|
348
347
|
req.execute!
|
@@ -6,9 +6,20 @@
|
|
6
6
|
require 'rack'
|
7
7
|
|
8
8
|
class PayboxDirect::Request
|
9
|
-
attr_reader :vars, :post_request, :fields
|
9
|
+
attr_reader :vars, :post_request, :fields, :http_resp
|
10
10
|
attr_accessor :response
|
11
11
|
|
12
|
+
@@request_callbacks = []
|
13
|
+
@@undo_callbacks = []
|
14
|
+
|
15
|
+
def self.on_request(&block)
|
16
|
+
@@request_callbacks << block
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.on_undo(&block)
|
20
|
+
@@undo_callbacks << block
|
21
|
+
end
|
22
|
+
|
12
23
|
def initialize(vars)
|
13
24
|
defaults = {
|
14
25
|
"VERSION" => PayboxDirect.config.version.to_s.rjust(5, "0"),
|
@@ -29,6 +40,7 @@ class PayboxDirect::Request
|
|
29
40
|
@post_request = nil
|
30
41
|
@fields = nil
|
31
42
|
@response = {}
|
43
|
+
@is_executed = false
|
32
44
|
end
|
33
45
|
|
34
46
|
# Executes the POST request on the Paybox server.
|
@@ -58,6 +70,16 @@ class PayboxDirect::Request
|
|
58
70
|
end
|
59
71
|
end
|
60
72
|
|
73
|
+
def undo!
|
74
|
+
return unless executed? and !failed?
|
75
|
+
|
76
|
+
@@undo_callbacks.each{ |proc| proc.call(self) }
|
77
|
+
end
|
78
|
+
|
79
|
+
def executed?
|
80
|
+
@is_executed
|
81
|
+
end
|
82
|
+
|
61
83
|
def failed?
|
62
84
|
raise "Not executed yet" if @fields.nil?
|
63
85
|
return @fields["CODEREPONSE"] != "00000"
|
@@ -91,7 +113,9 @@ private
|
|
91
113
|
http = self.class.http_connection(uri)
|
92
114
|
@post_request = Net::HTTP::Post.new(uri.request_uri)
|
93
115
|
@post_request.set_form_data(@vars)
|
94
|
-
|
95
|
-
|
116
|
+
@http_resp = http.request(@post_request)
|
117
|
+
@is_executed = true
|
118
|
+
@@request_callbacks.each{ |proc| proc.call(self) }
|
119
|
+
return @http_resp
|
96
120
|
end
|
97
121
|
end
|
data/spec/request_spec.rb
CHANGED
@@ -47,6 +47,20 @@ RSpec.describe PayboxDirect::Request do
|
|
47
47
|
expect(req.error_comment).to eq "My error description"
|
48
48
|
end
|
49
49
|
|
50
|
+
it "calls request callbacks" do
|
51
|
+
expect_any_instance_of(Net::HTTP).to receive(:request){
|
52
|
+
OpenStruct.new({
|
53
|
+
code: "200",
|
54
|
+
body: "CODEREPONSE=00000&COMMENTAIRE=OK"
|
55
|
+
})
|
56
|
+
}
|
57
|
+
cb = proc {}
|
58
|
+
req = PayboxDirect::Request.new("VAR1" => "VAL1", "VAR2" => "VAL2")
|
59
|
+
PayboxDirect::Request.on_request &cb
|
60
|
+
expect(cb).to receive(:call).with(req)
|
61
|
+
req.execute!
|
62
|
+
end
|
63
|
+
|
50
64
|
it "should raise ServerUnavailable in dev" do
|
51
65
|
was_prod = PayboxDirect.config.is_prod
|
52
66
|
PayboxDirect.config.is_prod = false
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: paybox_direct
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kévin Lesénéchal
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-11-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|