paybox_direct 0.1.1 → 0.1.2
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/exceptions.rb +24 -0
- data/lib/paybox_direct/request.rb +92 -0
- data/lib/paybox_direct.rb +2 -2
- metadata +3 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 96b8102e2e2bbdeaa9e7b0f135dd830d72731f2e
|
4
|
+
data.tar.gz: c4af4de994a7a25cb624f6ef00f7b006121f9f2e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2689575f1327dad65fb2e34cda320545c41ed23d1d3cc92bc36d4518191189dddc333038ec7f49ab4ef3b45964cba066478d8c154454b30a9536365040f58778
|
7
|
+
data.tar.gz: 28fe97e750b3a71d2c43c5ee854d66f69d289bcf39ab6e8c1010bcd7f48401f2f8331e1d973953f6b8614f1fa1b32a604d3eaf73c0dd4b166d82077498217060
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# Copyright © 2015, Kévin Lesénéchal <kevin.lesenechal@gmail.com>.
|
2
|
+
#
|
3
|
+
# This library is licensed under the new BSD license. Checkout the license text
|
4
|
+
# in the LICENSE file or online at <http://opensource.org/licenses/BSD-3-Clause>.
|
5
|
+
|
6
|
+
class PayboxDirect
|
7
|
+
class PayboxRequestError < StandardError
|
8
|
+
attr_reader :code, :comment
|
9
|
+
|
10
|
+
def initialize(code, comment)
|
11
|
+
super("#{code.to_s.rjust(5, "0")}: #{comment}")
|
12
|
+
@code = code
|
13
|
+
@comment = comment
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
class AuthorizationError < PayboxRequestError; end
|
18
|
+
class DebitError < AuthorizationError; end
|
19
|
+
class CancelError < PayboxRequestError; end
|
20
|
+
class RefundError < PayboxRequestError; end
|
21
|
+
class CreditError < PayboxRequestError; end
|
22
|
+
class DeleteSubscriberError < PayboxRequestError; end
|
23
|
+
class ServerUnavailableError < StandardError; end
|
24
|
+
end
|
@@ -0,0 +1,92 @@
|
|
1
|
+
# Copyright © 2015, Kévin Lesénéchal <kevin.lesenechal@gmail.com>.
|
2
|
+
#
|
3
|
+
# This library is licensed under the new BSD license. Checkout the license text
|
4
|
+
# in the LICENSE file or online at <http://opensource.org/licenses/BSD-3-Clause>.
|
5
|
+
|
6
|
+
require 'rack'
|
7
|
+
|
8
|
+
class PayboxDirect::Request
|
9
|
+
attr_reader :vars, :post_request, :fields
|
10
|
+
attr_accessor :response
|
11
|
+
|
12
|
+
def initialize(vars)
|
13
|
+
defaults = {
|
14
|
+
"VERSION" => PayboxDirect.config.version.to_s.rjust(5, "0"),
|
15
|
+
"SITE" => PayboxDirect.config.site.to_s.rjust(7, "0"),
|
16
|
+
"RANG" => PayboxDirect.config.rank.to_s.rjust(2, "0"),
|
17
|
+
"CLE" => PayboxDirect.config.password,
|
18
|
+
"DATEQ" => Time.new.utc.strftime("%d%m%Y%H%M%S"),
|
19
|
+
"NUMQUESTION" => Time.new.utc.strftime("%H%M%S%L").rjust(10, "0")
|
20
|
+
}
|
21
|
+
if !PayboxDirect.config.activity.nil?
|
22
|
+
defaults["ACTIVITE"] = PayboxDirect.config.activity.to_s.rjust(3, "0")
|
23
|
+
end
|
24
|
+
if !PayboxDirect.config.bank.nil?
|
25
|
+
defaults["ACQUEREUR"] = PayboxDirect.config.bank
|
26
|
+
end
|
27
|
+
@vars = defaults.merge(vars)
|
28
|
+
|
29
|
+
@post_request = nil
|
30
|
+
@fields = nil
|
31
|
+
@response = {}
|
32
|
+
end
|
33
|
+
|
34
|
+
# Executes the POST request on the Paybox server.
|
35
|
+
#
|
36
|
+
# == Raises:
|
37
|
+
# * PayboxDirect::ServerUnavailableError, if Paybox server is unavailable
|
38
|
+
def execute!
|
39
|
+
use_alt = false
|
40
|
+
begin
|
41
|
+
begin
|
42
|
+
prod_url = use_alt ? PayboxDirect::PROD_FALLBACK_URL : PayboxDirect::PROD_URL
|
43
|
+
uri = URI(PayboxDirect.config.is_prod ? prod_url : PayboxDirect::DEV_URL)
|
44
|
+
resp = run_http_post!(uri)
|
45
|
+
rescue
|
46
|
+
raise PayboxDirect::ServerUnavailableError
|
47
|
+
end
|
48
|
+
raise PayboxDirect::ServerUnavailableError if resp.code != "200"
|
49
|
+
@fields = Rack::Utils.parse_query(resp.body)
|
50
|
+
if !@fields.has_key?("CODEREPONSE") or @fields["CODEREPONSE"] == "00001"
|
51
|
+
raise PayboxDirect::ServerUnavailableError
|
52
|
+
end
|
53
|
+
rescue PayboxDirect::ServerUnavailableError => e
|
54
|
+
raise e if use_alt or !PayboxDirect.config.is_prod
|
55
|
+
use_alt = true
|
56
|
+
sleep 1
|
57
|
+
retry
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def failed?
|
62
|
+
raise "Not executed yet" if @fields.nil?
|
63
|
+
return @fields["CODEREPONSE"] != "00000"
|
64
|
+
end
|
65
|
+
|
66
|
+
def error_code
|
67
|
+
raise "Not executed yet" if @fields.nil?
|
68
|
+
return @fields["CODEREPONSE"].to_i
|
69
|
+
end
|
70
|
+
|
71
|
+
def error_comment
|
72
|
+
raise "Not executed yet" if @fields.nil?
|
73
|
+
return @fields["COMMENTAIRE"]
|
74
|
+
end
|
75
|
+
|
76
|
+
def self.http_connection(uri)
|
77
|
+
# We may want to execute multiple requests on a single HTTP connection in the future.
|
78
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
79
|
+
http.use_ssl = true
|
80
|
+
return http
|
81
|
+
end
|
82
|
+
|
83
|
+
private
|
84
|
+
|
85
|
+
def run_http_post!(uri)
|
86
|
+
http = self.class.http_connection(uri)
|
87
|
+
@post_request = Net::HTTP::Post.new(uri.request_uri)
|
88
|
+
@post_request.set_form_data(@vars)
|
89
|
+
resp = http.request(@post_request)
|
90
|
+
return resp
|
91
|
+
end
|
92
|
+
end
|
data/lib/paybox_direct.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: paybox_direct
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kévin Lesénéchal
|
@@ -47,6 +47,8 @@ files:
|
|
47
47
|
- LICENSE
|
48
48
|
- README.md
|
49
49
|
- lib/paybox_direct.rb
|
50
|
+
- lib/paybox_direct/exceptions.rb
|
51
|
+
- lib/paybox_direct/request.rb
|
50
52
|
- spec/paybox_direct_spec.rb
|
51
53
|
- spec/request_spec.rb
|
52
54
|
- spec/spec_helper.rb
|