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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d6998f735f27720f3c35bc55f6723eb342664d86
4
- data.tar.gz: bd26cf6c73b72f96b1748664d2fa60e13aaa1157
3
+ metadata.gz: d901596ca4dfc8d6cc31a037c6226d752d5deade
4
+ data.tar.gz: bfa78c3eeceb6bbd5a002d12f0074057f50eb8dc
5
5
  SHA512:
6
- metadata.gz: 4e0ef292152cb66d1da4b74d6c4227dd13c8635a1e287e84ccf9c36ea06d3e4ab72c8943ef17109b4208b828283e8876e224ba9f916c8ce8a3931c75b73303cc
7
- data.tar.gz: c9ad2b64d51e8c5aeb08a8674fe97a6dbc1bd9a65369ec91525b95bb13eff29e30fa12d24d4f384ea2b4ef012f597cb7f7701320332688f7bedecded9e11f65e
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(opts)
69
- raise ArgumentError, "Expecting hash" unless opts.is_a? Hash
70
- raise ArgumentError, "Expecting `amount` option" unless opts.has_key? :amount
71
- raise ArgumentError, "Expecting `currency` option" unless opts.has_key? :currency
72
- raise ArgumentError, "Expecting `ref` option" unless opts.has_key? :ref
73
- raise ArgumentError, "Expecting `cc_expire` option" unless opts.has_key? :cc_expire
74
- raise ArgumentError, "Expecting `cc_cvv` option" unless opts.has_key? :cc_cvv
75
- raise ArgumentError, "amount: Expecting Numeric" unless opts[:amount].is_a? Numeric
76
- raise ArgumentError, "currency: Not supported" unless CURRENCIES.has_key? opts[:currency]
77
- raise ArgumentError, "cc_expire: Expecting Date" unless opts[:cc_expire].is_a? Date
78
- opts[:debit] = false if !opts.has_key? :debit
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 opts.has_key? :subscriber
81
- if opts.has_key? :wallet
82
- raise ArgumentError, "cc_number: Unexpected when `wallet` provided" if opts.has_key? :cc_number
83
- op_code = opts[:debit] ? 53 : 51
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" unless opts.has_key? :cc_number
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" unless opts.has_key? :cc_number
90
- raise ArgumentError, "Unexpected `wallet` option" if opts.has_key? :wallet
91
- op_code = opts[:debit] ? 3 : 1
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 + opts[:ref],
97
- "MONTANT" => (opts[:amount].round(2) * 100).round.to_s.rjust(10, "0"),
98
- "DEVISE" => CURRENCIES[opts[:currency]].to_s.rjust(3, "0"),
99
- "PORTEUR" => opts.has_key?(:wallet) ? opts[:wallet] : opts[:cc_number].gsub(/[ -.]/, ""),
100
- "DATEVAL" => opts[:cc_expire].strftime("%m%y"),
101
- "CVV" => opts[:cc_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 opts.has_key? :subscriber
104
- vars["REFABONNE"] = @@config.ref_prefix + opts[:subscriber]
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 opts[:debit]
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: opts[:amount],
124
- currency: opts[: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(opts)
158
- raise ArgumentError, "Expecting hash" unless opts.is_a? Hash
159
- raise ArgumentError, "Expecting `amount` option" unless opts.has_key? :amount
160
- raise ArgumentError, "Expecting `currency` option" unless opts.has_key? :currency
161
- raise ArgumentError, "Expecting `request_id` option" unless opts.has_key? :request_id
162
- raise ArgumentError, "Expecting `transaction_id` option" unless opts.has_key? :transaction_id
163
- raise ArgumentError, "amount: Expecting Numeric" unless opts[:amount].is_a? Numeric
164
- raise ArgumentError, "currency: Not supported" unless CURRENCIES.has_key? opts[:currency]
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" => (opts[:amount].round(2) * 100).round.to_s.rjust(10, "0"),
171
- "DEVISE" => CURRENCIES[opts[:currency]].to_s.rjust(3, "0"),
172
- "NUMAPPEL" => opts[:request_id].to_s.rjust(10, "0"),
173
- "NUMTRANS" => opts[:transaction_id].to_s.rjust(10, "0")
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(opts)
206
- raise ArgumentError, "Expecting hash" unless opts.is_a? Hash
207
- raise ArgumentError, "Expecting `amount` option" unless opts.has_key? :amount
208
- raise ArgumentError, "Expecting `currency` option" unless opts.has_key? :currency
209
- raise ArgumentError, "Expecting `ref` option" unless opts.has_key? :ref
210
- raise ArgumentError, "Expecting `cc_cvv` option" unless opts.has_key? :cc_cvv
211
- raise ArgumentError, "Expecting `request_id` option" unless opts.has_key? :request_id
212
- raise ArgumentError, "Expecting `transaction_id` option" unless opts.has_key? :transaction_id
213
- raise ArgumentError, "amount: Expecting Numeric" unless opts[:amount].is_a? Numeric
214
- raise ArgumentError, "currency: Not supported" unless CURRENCIES.has_key? opts[:currency]
215
- raise ArgumentError, "cc_expire: Expecting Date" unless opts[:cc_expire].is_a? Date
216
- raise ArgumentError, "request_id: Expecting Numeric" unless opts[:request_id].is_a? Numeric
217
- raise ArgumentError, "transaction_id: Expecting Numeric" unless opts[:transaction_id].is_a? Numeric
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 opts.has_key? :subscriber
220
- raise ArgumentError, "Expecting `wallet` option" unless opts.has_key? :wallet
221
- raise ArgumentError, "Expecting `cc_expire` option" unless opts.has_key? :cc_expire
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" if opts.has_key? :wallet
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 + opts[:ref],
231
- "MONTANT" => (opts[:amount].round(2) * 100).round.to_s.rjust(10, "0"),
232
- "DEVISE" => CURRENCIES[opts[:currency]].to_s.rjust(3, "0"),
233
- "DATEVAL" => opts[:cc_expire].strftime("%m%y"),
234
- "CVV" => opts[:cc_cvv],
235
- "NUMAPPEL" => opts[:request_id].to_s.rjust(10, "0"),
236
- "NUMTRANS" => opts[:transaction_id].to_s.rjust(10, "0")
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 opts.has_key? :subscriber
239
- vars["PORTEUR"] = opts[:wallet]
240
- vars["REFABONNE"] = @@config.ref_prefix + opts[:subscriber]
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(opts)
268
- raise ArgumentError, "Expecting hash" unless opts.is_a? Hash
269
- raise ArgumentError, "Expecting `amount` option" unless opts.has_key? :amount
270
- raise ArgumentError, "Expecting `currency` option" unless opts.has_key? :currency
271
- raise ArgumentError, "Expecting `request_id` option" unless opts.has_key? :request_id
272
- raise ArgumentError, "Expecting `transaction_id` option" unless opts.has_key? :transaction_id
273
- raise ArgumentError, "amount: Expecting Numeric" unless opts[:amount].is_a? Numeric
274
- raise ArgumentError, "currency: Not supported" unless CURRENCIES.has_key? opts[:currency]
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" => (opts[:amount].round(2) * 100).round.to_s.rjust(10, "0"),
281
- "DEVISE" => CURRENCIES[opts[:currency]].to_s.rjust(3, "0"),
282
- "NUMAPPEL" => opts[:request_id].to_s.rjust(10, "0"),
283
- "NUMTRANS" => opts[:transaction_id].to_s.rjust(10, "0")
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(opts)
315
- raise ArgumentError, "Expecting hash" unless opts.is_a? Hash
316
- raise ArgumentError, "Expecting `amount` option" unless opts.has_key? :amount
317
- raise ArgumentError, "Expecting `currency` option" unless opts.has_key? :currency
318
- raise ArgumentError, "Expecting `ref` option" unless opts.has_key? :ref
319
- raise ArgumentError, "Expecting `cc_expire` option" unless opts.has_key? :cc_expire
320
- raise ArgumentError, "Expecting `cc_cvv` option" unless opts.has_key? :cc_cvv
321
- raise ArgumentError, "amount: Expecting Numeric" unless opts[:amount].is_a? Numeric
322
- raise ArgumentError, "currency: Not supported" unless CURRENCIES.has_key? opts[:currency]
323
- raise ArgumentError, "cc_expire: Expecting Date" unless opts[:cc_expire].is_a? Date
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 opts.has_key? :subscriber
326
- raise ArgumentError, "Expecting `wallet` option" unless opts.has_key? :wallet
327
- raise ArgumentError, "cc_number: Unexpected when `wallet` provided" if opts.has_key? :cc_number
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" unless opts.has_key? :cc_number
331
- raise ArgumentError, "Unexpected `wallet` option" if opts.has_key? :wallet
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 + opts[:ref],
338
- "MONTANT" => (opts[:amount].round(2) * 100).round.to_s.rjust(10, "0"),
339
- "DEVISE" => CURRENCIES[opts[:currency]].to_s.rjust(3, "0"),
340
- "PORTEUR" => opts.has_key?(:wallet) ? opts[:wallet] : opts[:cc_number].gsub(/[ -.]/, ""),
341
- "DATEVAL" => opts[:cc_expire].strftime("%m%y"),
342
- "CVV" => opts[:cc_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 opts.has_key? :subscriber
345
- vars["REFABONNE"] = @@config.ref_prefix + opts[:subscriber]
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
- resp = http.request(@post_request)
95
- return resp
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.1.3
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-10-14 00:00:00.000000000 Z
11
+ date: 2015-11-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake