paybox_direct 0.1.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 +7 -0
- data/LICENSE +27 -0
- data/README.md +151 -0
- data/lib/paybox_direct.rb +387 -0
- data/spec/paybox_direct_spec.rb +378 -0
- data/spec/request_spec.rb +99 -0
- data/spec/spec_helper.rb +117 -0
- metadata +77 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 8f3f9d965c2389c181f66f32085909188b16c13f
|
4
|
+
data.tar.gz: ba08b3fed53319a6765594f57d021d8035d5ef64
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 1636b4051d042b6ff98ed410c837bcbac6c3f6c0d9e143ad27ff49519bb28818dd358c75cf3acc4e05ed8977bb3849478ca8224016a015da9f7ab402d20e4f5a
|
7
|
+
data.tar.gz: 830bea4f1d63f0cd24fc27ae0cf96274efa3c941d4dee446bca484535497cc6fec2ac1489874ded4101625215e1dee4169b00c764e88a125993b9bf28612b5b6
|
data/LICENSE
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
Copyright © 2015, Kévin Lesénéchal <kevin.lesenechal@gmail.com>
|
2
|
+
All rights reserved.
|
3
|
+
|
4
|
+
Redistribution and use in source and binary forms, with or without modification,
|
5
|
+
are permitted provided that the following conditions are met:
|
6
|
+
|
7
|
+
1. Redistributions of source code must retain the above copyright notice, this
|
8
|
+
list of conditions and the following disclaimer.
|
9
|
+
|
10
|
+
2. Redistributions in binary form must reproduce the above copyright notice,
|
11
|
+
this list of conditions and the following disclaimer in the documentation
|
12
|
+
and/or other materials provided with the distribution.
|
13
|
+
|
14
|
+
3. Neither the name of the copyright holder nor the names of its contributors
|
15
|
+
may be used to endorse or promote products derived from this software
|
16
|
+
without specific prior written permission.
|
17
|
+
|
18
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
19
|
+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
20
|
+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
21
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
|
22
|
+
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
23
|
+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
24
|
+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
25
|
+
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
26
|
+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
27
|
+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
data/README.md
ADDED
@@ -0,0 +1,151 @@
|
|
1
|
+
# Paybox Direct Ruby API #
|
2
|
+
|
3
|
+
The `paybox_direct` gem provides a Ruby interface to the French payment platform
|
4
|
+
**Paybox** and more specifically their **Paybox Direct** and **Paybox Direct
|
5
|
+
Plus** products. If you plan to use **Paybox Systems**, this gem won't be of any
|
6
|
+
help.
|
7
|
+
|
8
|
+
## Installation ##
|
9
|
+
|
10
|
+
Put this in your `Gemfile`:
|
11
|
+
|
12
|
+
gem 'paybox_direct'
|
13
|
+
|
14
|
+
To include all the necessary classes, do:
|
15
|
+
|
16
|
+
require 'paybox_direct'
|
17
|
+
|
18
|
+
## Configuration ##
|
19
|
+
|
20
|
+
Use `PayboxDirect.config` to configure the default settings of the API like your
|
21
|
+
Paybox credentials. Example:
|
22
|
+
|
23
|
+
PayboxDirect.config.site = 410555 # Your site number
|
24
|
+
|
25
|
+
The available configuration variables are:
|
26
|
+
|
27
|
+
* `site`: (Integer) Your site number, see your Paybox contract;
|
28
|
+
* `rank`: (Integer) Your rank number, see your Paybox contract;
|
29
|
+
* `login`: (String) Your login, see your Paybox contract;
|
30
|
+
* `password`: (String) Your password, see your Paybox contract;
|
31
|
+
* `is_prod`: (Boolean) Set to false to use the Paybox test server;
|
32
|
+
* `ref_prefix`: (String) A prefix to all references including subscriber IDs;
|
33
|
+
* `version`: (Integer) The protocol version, `103` for *Paybox Direct*, `104` for *Paybox Direct Plus*;
|
34
|
+
* `activity`: (Integer/nil) The activity code (`ACTIVITE`), see Paybox documentation;
|
35
|
+
* `bank`: (Integer/nil) The `ACQUEREUR` variable, let `nil` if you don't have special requirements.
|
36
|
+
|
37
|
+
## Usage ##
|
38
|
+
|
39
|
+
### Authorization only ###
|
40
|
+
|
41
|
+
This call will make an authorization without debit:
|
42
|
+
|
43
|
+
req = PayboxDirect.authorize(
|
44
|
+
ref: "my_app_reference",
|
45
|
+
amount: 38.29,
|
46
|
+
currency: :EUR,
|
47
|
+
cc_number: "4012-0010-3844-3335", # you may omit the dashes
|
48
|
+
cc_expire: Date.new(2016, 10, 1), # the day of month doesn't matter
|
49
|
+
cc_cvv: "123"
|
50
|
+
)
|
51
|
+
req.response[:request_id] # => The Paybox request ID (NUMAPPEL)
|
52
|
+
req.response[:transaction_id] # => The Paybox transaction ID (NUMTRANS)
|
53
|
+
req.response[:authorization] # => The authorization number, `nil` in dev
|
54
|
+
|
55
|
+
In case of failure, this will raise a `PayboxDirect::AuthorizationError`
|
56
|
+
exception. This exception contains two methods: `code` for the numeric error
|
57
|
+
code (see documentation) and `comment` for a brief error comment (in French)
|
58
|
+
returned by Paybox.
|
59
|
+
|
60
|
+
### Immediate debit ###
|
61
|
+
|
62
|
+
To immediately debit a credit card, use the exact same call than before, just
|
63
|
+
call the `debit` method instead. This may raise a `DebitError` which inherits
|
64
|
+
`AuthorizationError`.
|
65
|
+
|
66
|
+
### Debit on a prior authorization only ###
|
67
|
+
|
68
|
+
After an authorization only, you can make this call to proceed to debit:
|
69
|
+
|
70
|
+
PayboxDirect.debit_authorization(
|
71
|
+
amount: 38.29, # This may be lower than the authorization
|
72
|
+
currency: :EUR,
|
73
|
+
request_id: my_req_id, # The request ID returned from authorization
|
74
|
+
transaction_id: my_trans_id # The transaction ID returned from authorization
|
75
|
+
)
|
76
|
+
|
77
|
+
Will raise `DebitError` in case of failure.
|
78
|
+
|
79
|
+
### Subscribers and wallet codes ###
|
80
|
+
|
81
|
+
With *Paybox Direct Plus* you can register subscribers for future operations and
|
82
|
+
not having to store their credit card credentials. To create a subscriber, you
|
83
|
+
must make an authorization and/or debit and provide the `subscriber` parameter.
|
84
|
+
This parameter will contain a unique subscriber ID.
|
85
|
+
|
86
|
+
When creating a subscriber, the request will return a **wallet code** which is a
|
87
|
+
string representing the card number will you will pass to future calls on this
|
88
|
+
subscriber.
|
89
|
+
|
90
|
+
Creation:
|
91
|
+
|
92
|
+
req = PayboxDirect.authorize(
|
93
|
+
ref: "my_app_reference",
|
94
|
+
amount: 38.29,
|
95
|
+
currency: :EUR,
|
96
|
+
cc_number: "4012-0010-3844-3335",
|
97
|
+
cc_expire: Date.new(2016, 10, 1),
|
98
|
+
cc_cvv: "123",
|
99
|
+
subscriber: "my_sub_id" # This will create this subscriber
|
100
|
+
)
|
101
|
+
req.response[:wallet] # => Contains the wallet code to store in DB
|
102
|
+
|
103
|
+
To proceed to an authorization or debit on a previously created subscriber,
|
104
|
+
simply replace the `cc_number` argument with `wallet` and provide the subscriber
|
105
|
+
ID. Example:
|
106
|
+
|
107
|
+
PayboxDirect.debit(
|
108
|
+
ref: "my_app_reference",
|
109
|
+
amount: 38.29,
|
110
|
+
currency: :EUR,
|
111
|
+
wallet: my_wallet_code, # The wallet we got on the subscriber's creation
|
112
|
+
cc_expire: Date.new(2016, 10, 1),
|
113
|
+
cc_cvv: "123",
|
114
|
+
subscriber: "my_sub_id" # The subscriber ID previously created
|
115
|
+
)
|
116
|
+
|
117
|
+
### Refund a payment ###
|
118
|
+
|
119
|
+
If you want to refund a user, use the `refund` method with:
|
120
|
+
|
121
|
+
* `amount`;
|
122
|
+
* `currency`;
|
123
|
+
* `request_id`;
|
124
|
+
* `transaction_id`.
|
125
|
+
|
126
|
+
It may raise `RefundError`, which is clearly possible if the debit you want to
|
127
|
+
refund wasn't sent to bank yet. The `refund` operation can only apply to
|
128
|
+
definitive payments. Therefor, you should try to **cancel** the operation (see
|
129
|
+
below) before refunding.
|
130
|
+
|
131
|
+
### Cancel an operation ###
|
132
|
+
|
133
|
+
Almost all payment operations can be cancelled before they are transmitted to
|
134
|
+
the bank and become definitive. Use the `cancel` with these options:
|
135
|
+
|
136
|
+
* `amount`;
|
137
|
+
* `currency`;
|
138
|
+
* `ref`;
|
139
|
+
* `cc_number` or `wallet`;
|
140
|
+
* `cc_expire`;
|
141
|
+
* `cc_cvv`;
|
142
|
+
* `subscriber` (optional, only if on a subscriber);
|
143
|
+
* `request_id`;
|
144
|
+
* `transaction_id`.
|
145
|
+
|
146
|
+
In case of failures, it raises `CancelError`.
|
147
|
+
|
148
|
+
### Credits ###
|
149
|
+
|
150
|
+
You can credit a user using the `credit` method, this works the same than debits
|
151
|
+
including with subscribers. It may raise `CreditError`.
|
@@ -0,0 +1,387 @@
|
|
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 'ostruct'
|
7
|
+
require 'net/http'
|
8
|
+
|
9
|
+
class PayboxDirect
|
10
|
+
DEV_URL = 'https://preprod-ppps.paybox.com/PPPS.php'
|
11
|
+
PROD_URL = 'https://ppps.paybox.com/PPPS.php'
|
12
|
+
PROD_FALLBACK_URL = 'https://ppps1.paybox.com/PPPS.php'
|
13
|
+
|
14
|
+
CURRENCIES = {
|
15
|
+
:AUD => 36,
|
16
|
+
:CAD => 124,
|
17
|
+
:CHF => 756,
|
18
|
+
:DKK => 208,
|
19
|
+
:EUR => 978,
|
20
|
+
:GBP => 826,
|
21
|
+
:HKD => 344,
|
22
|
+
:JPY => 392,
|
23
|
+
:USD => 840
|
24
|
+
}
|
25
|
+
|
26
|
+
@@config = OpenStruct.new({
|
27
|
+
site: nil, # Your site number, see your Paybox contract
|
28
|
+
rank: nil, # Your rank number, see your Paybox contract
|
29
|
+
login: nil, # Your login, see your Paybox contract
|
30
|
+
password: nil, # Your password, see your Paybox contract
|
31
|
+
is_prod: true, # Set to false to use the Paybox test server
|
32
|
+
ref_prefix: '', # A prefix to all references including subscriber IDs
|
33
|
+
version: 104, # The protocol version, 103 for Paybox Direct, 104 for Paybox Direct Plus
|
34
|
+
activity: nil, # The activity code (ACTIVITE), see Paybox documentation
|
35
|
+
bank: nil # The ACQUEREUR variable, let nil if you don't have special requirements
|
36
|
+
})
|
37
|
+
|
38
|
+
def self.config
|
39
|
+
@@config
|
40
|
+
end
|
41
|
+
|
42
|
+
# Executes an authorization with or without debit.
|
43
|
+
#
|
44
|
+
# == Options:
|
45
|
+
# * amount: The decimal amount, e.g. 49.9 for €49.90 (or other currency)
|
46
|
+
# * currency: The currency code, e.g. :EUR
|
47
|
+
# * ref: The application reference for this authorization
|
48
|
+
# * cc_number: The credit card number, e.g. "1234123412341234" (if not subscribed)
|
49
|
+
# * wallet: The wallet number (if subscribed)
|
50
|
+
# * cc_expire: The credit card expiration date, e.g. Date.new(2015, 10, 1)
|
51
|
+
# * cc_cvv: The credit card CVV, e.g. "123"
|
52
|
+
# * subscriber: (optional) A subscriber ID
|
53
|
+
# * debit: (Bool) if true, will debit (default false)
|
54
|
+
#
|
55
|
+
# If a subscriber ID is provided:
|
56
|
+
# * and `wallet` is provided, will execute a #51 operation (#53 if debit);
|
57
|
+
# * and `wallet` is NOT provided, will execute a #56 operation (then #2 if debit);
|
58
|
+
# otherwise, will execute a #1 operation (#3 if debit).
|
59
|
+
#
|
60
|
+
# == Returns: (PayboxDirect::Request)
|
61
|
+
# The Paybox request with these response variables:
|
62
|
+
# * request_id: The Paybox request ID (NUMAPPEL)
|
63
|
+
# * transaction_id: The Paybox transaction ID (NUMTRANS)
|
64
|
+
# * wallet: (if new subscription) The created wallet code (PORTEUR)
|
65
|
+
#
|
66
|
+
# == Raises:
|
67
|
+
# * PayboxDirect::AuthorizationError, if authorization fails
|
68
|
+
# * PayboxDirect::ServerUnavailableError, if Paybox server is unavailable
|
69
|
+
def self.authorize(opts)
|
70
|
+
raise ArgumentError, "Expecting hash" unless opts.is_a? Hash
|
71
|
+
raise ArgumentError, "Expecting `amount` option" unless opts.has_key? :amount
|
72
|
+
raise ArgumentError, "Expecting `currency` option" unless opts.has_key? :currency
|
73
|
+
raise ArgumentError, "Expecting `ref` option" unless opts.has_key? :ref
|
74
|
+
raise ArgumentError, "Expecting `cc_expire` option" unless opts.has_key? :cc_expire
|
75
|
+
raise ArgumentError, "Expecting `cc_cvv` option" unless opts.has_key? :cc_cvv
|
76
|
+
raise ArgumentError, "amount: Expecting Numeric" unless opts[:amount].is_a? Numeric
|
77
|
+
raise ArgumentError, "currency: Not supported" unless CURRENCIES.has_key? opts[:currency]
|
78
|
+
raise ArgumentError, "cc_expire: Expecting Date" unless opts[:cc_expire].is_a? Date
|
79
|
+
opts[:debit] = false if !opts.has_key? :debit
|
80
|
+
|
81
|
+
if opts.has_key? :subscriber
|
82
|
+
if opts.has_key? :wallet
|
83
|
+
raise ArgumentError, "cc_number: Unexpected when `wallet` provided" if opts.has_key? :cc_number
|
84
|
+
op_code = opts[:debit] ? 53 : 51
|
85
|
+
else
|
86
|
+
raise ArgumentError, "Expecting `cc_number` option" unless opts.has_key? :cc_number
|
87
|
+
op_code = 56 # Paybox can't create a new subscriber with immediate debit
|
88
|
+
end
|
89
|
+
else
|
90
|
+
raise ArgumentError, "Expecting `cc_number` option" unless opts.has_key? :cc_number
|
91
|
+
raise ArgumentError, "Unexpected `wallet` option" if opts.has_key? :wallet
|
92
|
+
op_code = opts[:debit] ? 3 : 1
|
93
|
+
end
|
94
|
+
|
95
|
+
vars = {
|
96
|
+
"TYPE" => op_code.to_s.rjust(5, "0"),
|
97
|
+
"REFERENCE" => @@config.ref_prefix + opts[:ref],
|
98
|
+
"MONTANT" => (opts[:amount].round(2) * 100).round.to_s.rjust(10, "0"),
|
99
|
+
"DEVISE" => CURRENCIES[opts[:currency]].to_s.rjust(3, "0"),
|
100
|
+
"PORTEUR" => opts.has_key?(:wallet) ? opts[:wallet] : opts[:cc_number].gsub(/[ -.]/, ""),
|
101
|
+
"DATEVAL" => opts[:cc_expire].strftime("%m%y"),
|
102
|
+
"CVV" => opts[:cc_cvv]
|
103
|
+
}
|
104
|
+
if opts.has_key? :subscriber
|
105
|
+
vars["REFABONNE"] = @@config.ref_prefix + opts[:subscriber]
|
106
|
+
end
|
107
|
+
req = Request.new(vars)
|
108
|
+
req.execute!
|
109
|
+
|
110
|
+
if req.failed?
|
111
|
+
raise AuthorizationError.new(req.error_code, req.error_comment)
|
112
|
+
end
|
113
|
+
req.response = {
|
114
|
+
request_id: req.fields["NUMAPPEL"].to_i,
|
115
|
+
transaction_id: req.fields["NUMTRANS"].to_i,
|
116
|
+
authorization: req.fields["AUTORISATION"] == "XXXXXX" ? nil : req.fields["AUTORISATION"].to_i
|
117
|
+
}
|
118
|
+
if op_code == 56
|
119
|
+
req.response[:wallet] = req.fields["PORTEUR"]
|
120
|
+
|
121
|
+
# We now execute debit after authorization-only operation #56
|
122
|
+
if opts[:debit]
|
123
|
+
sleep 1 # Paybox recommends to wait a few seconds between the authorization and the debit
|
124
|
+
debit_authorization(
|
125
|
+
amount: opts[:amount],
|
126
|
+
currency: opts[:currency],
|
127
|
+
request_id: req.response[:request_id],
|
128
|
+
transaction_id: req.response[:transaction_id]
|
129
|
+
)
|
130
|
+
end
|
131
|
+
end
|
132
|
+
return req
|
133
|
+
end
|
134
|
+
|
135
|
+
# Executes a direct debit, without prior authorization.
|
136
|
+
#
|
137
|
+
# Calls #authorize with { debit: true }.
|
138
|
+
def self.debit(opts)
|
139
|
+
opts[:debit] = true
|
140
|
+
return authorize(opts)
|
141
|
+
end
|
142
|
+
|
143
|
+
# Executes a debit on a prior authorization.
|
144
|
+
#
|
145
|
+
# == Options:
|
146
|
+
# * amount: The decimal amount, e.g. 49.9 for €49.90 (or other currency)
|
147
|
+
# * currency: The currency code, e.g. :EUR
|
148
|
+
# * request_id: The Paybox request ID (NUMAPPEL)
|
149
|
+
# * transaction_id: The Paybox transaction ID (NUMTRANS)
|
150
|
+
#
|
151
|
+
# This will execute a #2 operation.
|
152
|
+
#
|
153
|
+
# == Returns: (PayboxDirect::Request)
|
154
|
+
# The Paybox request.
|
155
|
+
#
|
156
|
+
# == Raises:
|
157
|
+
# * PayboxDirect::DebitError, if debit fails
|
158
|
+
# * PayboxDirect::ServerUnavailableError, if Paybox server is unavailable
|
159
|
+
def self.debit_authorization(opts)
|
160
|
+
raise ArgumentError, "Expecting hash" unless opts.is_a? Hash
|
161
|
+
raise ArgumentError, "Expecting `amount` option" unless opts.has_key? :amount
|
162
|
+
raise ArgumentError, "Expecting `currency` option" unless opts.has_key? :currency
|
163
|
+
raise ArgumentError, "Expecting `request_id` option" unless opts.has_key? :request_id
|
164
|
+
raise ArgumentError, "Expecting `transaction_id` option" unless opts.has_key? :transaction_id
|
165
|
+
raise ArgumentError, "amount: Expecting Numeric" unless opts[:amount].is_a? Numeric
|
166
|
+
raise ArgumentError, "currency: Not supported" unless CURRENCIES.has_key? opts[:currency]
|
167
|
+
raise ArgumentError, "request_id: Expecting Fixnum" unless opts[:request_id].is_a? Fixnum
|
168
|
+
raise ArgumentError, "transaction_id: Expecting Fixnum" unless opts[:transaction_id].is_a? Fixnum
|
169
|
+
|
170
|
+
req = Request.new({
|
171
|
+
"TYPE" => "00002",
|
172
|
+
"MONTANT" => (opts[:amount].round(2) * 100).round.to_s.rjust(10, "0"),
|
173
|
+
"DEVISE" => CURRENCIES[opts[:currency]].to_s.rjust(3, "0"),
|
174
|
+
"NUMAPPEL" => opts[:request_id].to_s.rjust(10, "0"),
|
175
|
+
"NUMTRANS" => opts[:transaction_id].to_s.rjust(10, "0")
|
176
|
+
})
|
177
|
+
req.execute!
|
178
|
+
|
179
|
+
if req.failed?
|
180
|
+
raise DebitError.new(req.error_code, req.error_comment)
|
181
|
+
end
|
182
|
+
return req
|
183
|
+
end
|
184
|
+
|
185
|
+
# Cancels an operation if possible.
|
186
|
+
#
|
187
|
+
# == Options:
|
188
|
+
# * amount: The decimal amount, e.g. 49.9 for €49.90 (or other currency)
|
189
|
+
# * currency: The currency code, e.g. :EUR
|
190
|
+
# * ref: The credit application reference
|
191
|
+
# * wallet: The wallet number (if subscribed)
|
192
|
+
# * cc_expire: The credit card expiration date, e.g. Date.new(2015, 10, 1) (if subscribed)
|
193
|
+
# * cc_cvv: The credit card CVV, e.g. "123"
|
194
|
+
# * subscriber: (optional) A subscriber ID
|
195
|
+
# * request_id: The request ID (NUMAPPEL)
|
196
|
+
# * transaction_id: The transaction ID (NUMTRANS)
|
197
|
+
#
|
198
|
+
# This will execute a #55 operation if a subscriber is specified, otherwise
|
199
|
+
# a #5 operation.
|
200
|
+
#
|
201
|
+
# == Returns: (PayboxDirect::Request)
|
202
|
+
# The Paybox request.
|
203
|
+
#
|
204
|
+
# == Raises:
|
205
|
+
# * PayboxDirect::CancelError, if cancellation fails
|
206
|
+
# * PayboxDirect::ServerUnavailableError, if Paybox server is unavailable
|
207
|
+
def self.cancel(opts)
|
208
|
+
raise ArgumentError, "Expecting hash" unless opts.is_a? Hash
|
209
|
+
raise ArgumentError, "Expecting `amount` option" unless opts.has_key? :amount
|
210
|
+
raise ArgumentError, "Expecting `currency` option" unless opts.has_key? :currency
|
211
|
+
raise ArgumentError, "Expecting `ref` option" unless opts.has_key? :ref
|
212
|
+
raise ArgumentError, "Expecting `cc_cvv` option" unless opts.has_key? :cc_cvv
|
213
|
+
raise ArgumentError, "Expecting `request_id` option" unless opts.has_key? :request_id
|
214
|
+
raise ArgumentError, "Expecting `transaction_id` option" unless opts.has_key? :transaction_id
|
215
|
+
raise ArgumentError, "amount: Expecting Numeric" unless opts[:amount].is_a? Numeric
|
216
|
+
raise ArgumentError, "currency: Not supported" unless CURRENCIES.has_key? opts[:currency]
|
217
|
+
raise ArgumentError, "cc_expire: Expecting Date" unless opts[:cc_expire].is_a? Date
|
218
|
+
raise ArgumentError, "request_id: Expecting Numeric" unless opts[:request_id].is_a? Numeric
|
219
|
+
raise ArgumentError, "transaction_id: Expecting Numeric" unless opts[:transaction_id].is_a? Numeric
|
220
|
+
|
221
|
+
if opts.has_key? :subscriber
|
222
|
+
raise ArgumentError, "Expecting `wallet` option" unless opts.has_key? :wallet
|
223
|
+
raise ArgumentError, "Expecting `cc_expire` option" unless opts.has_key? :cc_expire
|
224
|
+
op_code = 55
|
225
|
+
else
|
226
|
+
raise ArgumentError, "Unexpected `wallet` option" if opts.has_key? :wallet
|
227
|
+
op_code = 5
|
228
|
+
end
|
229
|
+
|
230
|
+
vars = {
|
231
|
+
"TYPE" => op_code.to_s.rjust(5, "0"),
|
232
|
+
"REFERENCE" => @@config.ref_prefix + opts[:ref],
|
233
|
+
"MONTANT" => (opts[:amount].round(2) * 100).round.to_s.rjust(10, "0"),
|
234
|
+
"DEVISE" => CURRENCIES[opts[:currency]].to_s.rjust(3, "0"),
|
235
|
+
"DATEVAL" => opts[:cc_expire].strftime("%m%y"),
|
236
|
+
"CVV" => opts[:cc_cvv],
|
237
|
+
"NUMAPPEL" => opts[:request_id].to_s.rjust(10, "0"),
|
238
|
+
"NUMTRANS" => opts[:transaction_id].to_s.rjust(10, "0")
|
239
|
+
}
|
240
|
+
if opts.has_key? :subscriber
|
241
|
+
vars["PORTEUR"] = opts[:wallet]
|
242
|
+
vars["REFABONNE"] = @@config.ref_prefix + opts[:subscriber]
|
243
|
+
end
|
244
|
+
req = Request.new(vars)
|
245
|
+
req.execute!
|
246
|
+
|
247
|
+
if req.failed?
|
248
|
+
raise CancelError.new(req.error_code, req.error_comment)
|
249
|
+
end
|
250
|
+
return req
|
251
|
+
end
|
252
|
+
|
253
|
+
# Executes a refund operation.
|
254
|
+
#
|
255
|
+
# == Options:
|
256
|
+
# * amount: The decimal amount, e.g. 49.9 for €49.90 (or other currency)
|
257
|
+
# * currency: The currency code, e.g. :EUR
|
258
|
+
# * request_id: The request ID (NUMAPPEL)
|
259
|
+
# * transaction_id: The transaction ID (NUMTRANS)
|
260
|
+
#
|
261
|
+
# This will execute a #14 operation.
|
262
|
+
#
|
263
|
+
# == Returns: (PayboxDirect::Request)
|
264
|
+
# The Paybox request.
|
265
|
+
#
|
266
|
+
# == Raises:
|
267
|
+
# * PayboxDirect::RefundError, if cancellation fails
|
268
|
+
# * PayboxDirect::ServerUnavailableError, if Paybox server is unavailable
|
269
|
+
def self.refund(opts)
|
270
|
+
raise ArgumentError, "Expecting hash" unless opts.is_a? Hash
|
271
|
+
raise ArgumentError, "Expecting `amount` option" unless opts.has_key? :amount
|
272
|
+
raise ArgumentError, "Expecting `currency` option" unless opts.has_key? :currency
|
273
|
+
raise ArgumentError, "Expecting `request_id` option" unless opts.has_key? :request_id
|
274
|
+
raise ArgumentError, "Expecting `transaction_id` option" unless opts.has_key? :transaction_id
|
275
|
+
raise ArgumentError, "amount: Expecting Numeric" unless opts[:amount].is_a? Numeric
|
276
|
+
raise ArgumentError, "currency: Not supported" unless CURRENCIES.has_key? opts[:currency]
|
277
|
+
raise ArgumentError, "request_id: Expecting Numeric" unless opts[:request_id].is_a? Numeric
|
278
|
+
raise ArgumentError, "transaction_id: Expecting Numeric" unless opts[:transaction_id].is_a? Numeric
|
279
|
+
|
280
|
+
req = Request.new({
|
281
|
+
"TYPE" => "00014",
|
282
|
+
"MONTANT" => (opts[:amount].round(2) * 100).round.to_s.rjust(10, "0"),
|
283
|
+
"DEVISE" => CURRENCIES[opts[:currency]].to_s.rjust(3, "0"),
|
284
|
+
"NUMAPPEL" => opts[:request_id].to_s.rjust(10, "0"),
|
285
|
+
"NUMTRANS" => opts[:transaction_id].to_s.rjust(10, "0")
|
286
|
+
})
|
287
|
+
req.execute!
|
288
|
+
|
289
|
+
if req.failed?
|
290
|
+
raise RefundError.new(req.error_code, req.error_comment)
|
291
|
+
end
|
292
|
+
return req
|
293
|
+
end
|
294
|
+
|
295
|
+
# Executes a credit operation.
|
296
|
+
#
|
297
|
+
# == Options:
|
298
|
+
# * amount: The decimal amount, e.g. 49.9 for €49.90 (or other currency)
|
299
|
+
# * currency: The currency code, e.g. :EUR
|
300
|
+
# * ref: The credit application reference
|
301
|
+
# * cc_number: The credit card number, e.g. "1234123412341234" (if not subscribed)
|
302
|
+
# * wallet: The wallet number (if subscribed)
|
303
|
+
# * cc_expire: The credit card expiration date, e.g. Date.new(2015, 10, 1)
|
304
|
+
# * cc_cvv: The credit card CVV, e.g. "123"
|
305
|
+
# * subscriber: (optional) A subscriber ID
|
306
|
+
#
|
307
|
+
# This will execute a #54 operation if a subscriber is specified, otherwise
|
308
|
+
# a #4 operation.
|
309
|
+
#
|
310
|
+
# == Returns: (PayboxDirect::Request)
|
311
|
+
# The Paybox request.
|
312
|
+
#
|
313
|
+
# == Raises:
|
314
|
+
# * PayboxDirect::CreditError, if credit fails
|
315
|
+
# * PayboxDirect::ServerUnavailableError, if Paybox server is unavailable
|
316
|
+
def self.credit(opts)
|
317
|
+
raise ArgumentError, "Expecting hash" unless opts.is_a? Hash
|
318
|
+
raise ArgumentError, "Expecting `amount` option" unless opts.has_key? :amount
|
319
|
+
raise ArgumentError, "Expecting `currency` option" unless opts.has_key? :currency
|
320
|
+
raise ArgumentError, "Expecting `ref` option" unless opts.has_key? :ref
|
321
|
+
raise ArgumentError, "Expecting `cc_expire` option" unless opts.has_key? :cc_expire
|
322
|
+
raise ArgumentError, "Expecting `cc_cvv` option" unless opts.has_key? :cc_cvv
|
323
|
+
raise ArgumentError, "amount: Expecting Numeric" unless opts[:amount].is_a? Numeric
|
324
|
+
raise ArgumentError, "currency: Not supported" unless CURRENCIES.has_key? opts[:currency]
|
325
|
+
raise ArgumentError, "cc_expire: Expecting Date" unless opts[:cc_expire].is_a? Date
|
326
|
+
|
327
|
+
if opts.has_key? :subscriber
|
328
|
+
raise ArgumentError, "Expecting `wallet` option" unless opts.has_key? :wallet
|
329
|
+
raise ArgumentError, "cc_number: Unexpected when `wallet` provided" if opts.has_key? :cc_number
|
330
|
+
op_code = 54
|
331
|
+
else
|
332
|
+
raise ArgumentError, "Expecting `cc_number` option" unless opts.has_key? :cc_number
|
333
|
+
raise ArgumentError, "Unexpected `wallet` option" if opts.has_key? :wallet
|
334
|
+
op_code = 4
|
335
|
+
end
|
336
|
+
|
337
|
+
vars = {
|
338
|
+
"TYPE" => op_code.to_s.rjust(5, "0"),
|
339
|
+
"REFERENCE" => @@config.ref_prefix + opts[:ref],
|
340
|
+
"MONTANT" => (opts[:amount].round(2) * 100).round.to_s.rjust(10, "0"),
|
341
|
+
"DEVISE" => CURRENCIES[opts[:currency]].to_s.rjust(3, "0"),
|
342
|
+
"PORTEUR" => opts.has_key?(:wallet) ? opts[:wallet] : opts[:cc_number].gsub(/[ -.]/, ""),
|
343
|
+
"DATEVAL" => opts[:cc_expire].strftime("%m%y"),
|
344
|
+
"CVV" => opts[:cc_cvv]
|
345
|
+
}
|
346
|
+
if opts.has_key? :subscriber
|
347
|
+
vars["REFABONNE"] = @@config.ref_prefix + opts[:subscriber]
|
348
|
+
end
|
349
|
+
req = Request.new(vars)
|
350
|
+
req.execute!
|
351
|
+
|
352
|
+
if req.failed?
|
353
|
+
raise CreditError.new(req.error_code, req.error_comment)
|
354
|
+
end
|
355
|
+
return req
|
356
|
+
end
|
357
|
+
|
358
|
+
# Deletes a subscriber per ID.
|
359
|
+
#
|
360
|
+
# == Arguments:
|
361
|
+
# * id: The subscriber ID (REFABONNE).
|
362
|
+
#
|
363
|
+
# This will execute a #58 operation.
|
364
|
+
#
|
365
|
+
# == Returns: (PayboxDirect::Request)
|
366
|
+
# The Paybox request.
|
367
|
+
#
|
368
|
+
# == Raises:
|
369
|
+
# * PayboxDirect::DeleteSubscriberError, if deletion fails
|
370
|
+
# * PayboxDirect::ServerUnavailableError, if Paybox server is unavailable
|
371
|
+
def self.delete_subscriber(id)
|
372
|
+
raise ArgumentError, "id: Expecting String" unless id.is_a? String
|
373
|
+
|
374
|
+
req = Request.new({
|
375
|
+
"TYPE" => "00058",
|
376
|
+
"REFABONNE" => @@config.ref_prefix + id
|
377
|
+
})
|
378
|
+
req.execute!
|
379
|
+
if req.failed?
|
380
|
+
raise DeleteSubscriberError.new(req.error_code, req.error_comment)
|
381
|
+
end
|
382
|
+
return req
|
383
|
+
end
|
384
|
+
end
|
385
|
+
|
386
|
+
require 'paybox_direct/request'
|
387
|
+
require 'paybox_direct/exceptions'
|
@@ -0,0 +1,378 @@
|
|
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 'paybox_direct'
|
7
|
+
require 'ostruct'
|
8
|
+
|
9
|
+
RSpec.describe PayboxDirect do
|
10
|
+
before do
|
11
|
+
PayboxDirect.config.site = SITE
|
12
|
+
PayboxDirect.config.rank = RANK
|
13
|
+
PayboxDirect.config.login = LOGIN
|
14
|
+
PayboxDirect.config.password = PASSWORD
|
15
|
+
PayboxDirect.config.ref_prefix = REF_PREFIX
|
16
|
+
PayboxDirect.config.activity = ENV.key?("PB_ACTIVITY") ? ENV["PB_ACTIVITY"].to_i : nil
|
17
|
+
PayboxDirect.config.bank = ENV.key?("PB_BANK") ? ENV["PB_BANK"] : nil
|
18
|
+
PayboxDirect.config.is_prod = false
|
19
|
+
end
|
20
|
+
|
21
|
+
context "authorization and debit" do
|
22
|
+
it "should make a auth-only without subscription" do
|
23
|
+
stub_response "CODEREPONSE=00000&COMMENTAIRE=&NUMAPPEL=0000111111&NUMTRANS=0002222222&AUTORISATION=444444" if !DO_CALLS
|
24
|
+
|
25
|
+
req = PayboxDirect.authorize(
|
26
|
+
ref: "auth_only",
|
27
|
+
amount: 14.29,
|
28
|
+
currency: :EUR,
|
29
|
+
cc_number: CC_NUMBER,
|
30
|
+
cc_expire: CC_EXPIRE,
|
31
|
+
cc_cvv: CC_CVV
|
32
|
+
)
|
33
|
+
|
34
|
+
expect(req.vars).to include({
|
35
|
+
"TYPE" => "00001",
|
36
|
+
"REFERENCE" => REF_PREFIX + "auth_only",
|
37
|
+
"MONTANT" => "0000001429",
|
38
|
+
"DEVISE" => "978",
|
39
|
+
"PORTEUR" => CC_NUMBER.gsub("-", ""),
|
40
|
+
"DATEVAL" => CC_EXPIRE.strftime("%m%y"),
|
41
|
+
"CVV" => CC_CVV
|
42
|
+
})
|
43
|
+
|
44
|
+
if DO_CALLS
|
45
|
+
expect(req.response[:request_id]).to be_a Fixnum
|
46
|
+
expect(req.response[:transaction_id]).to be_a Fixnum
|
47
|
+
expect(req.response[:authorization]).to be_nil
|
48
|
+
else
|
49
|
+
expect(req.response[:request_id]).to eq 111111
|
50
|
+
expect(req.response[:transaction_id]).to eq 2222222
|
51
|
+
expect(req.response[:authorization]).to eq 444444
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
it "should make a auth-only and create a subscriber" do
|
56
|
+
stub_response "CODEREPONSE=00000&COMMENTAIRE=&NUMAPPEL=0000111111&NUMTRANS=0002222222&AUTORISATION=444444&PORTEUR=my_wallet_code" if !DO_CALLS
|
57
|
+
|
58
|
+
req = PayboxDirect.authorize(
|
59
|
+
ref: "auth_only_new_subscriber",
|
60
|
+
amount: 25,
|
61
|
+
currency: :EUR,
|
62
|
+
cc_number: CC_NUMBER,
|
63
|
+
cc_expire: CC_EXPIRE,
|
64
|
+
cc_cvv: CC_CVV,
|
65
|
+
subscriber: "my_sub_id"
|
66
|
+
)
|
67
|
+
|
68
|
+
expect(req.vars).to include({
|
69
|
+
"TYPE" => "00056",
|
70
|
+
"REFERENCE" => REF_PREFIX + "auth_only_new_subscriber",
|
71
|
+
"MONTANT" => "0000002500",
|
72
|
+
"DEVISE" => "978",
|
73
|
+
"PORTEUR" => CC_NUMBER.gsub("-", ""),
|
74
|
+
"DATEVAL" => CC_EXPIRE.strftime("%m%y"),
|
75
|
+
"CVV" => CC_CVV,
|
76
|
+
"REFABONNE" => REF_PREFIX + "my_sub_id"
|
77
|
+
})
|
78
|
+
|
79
|
+
if DO_CALLS
|
80
|
+
expect(req.response[:request_id]).to be_a Fixnum
|
81
|
+
expect(req.response[:transaction_id]).to be_a Fixnum
|
82
|
+
expect(req.response[:authorization]).to be_nil
|
83
|
+
expect(req.response[:wallet]).to eq "CMDLpStLLLs"
|
84
|
+
else
|
85
|
+
expect(req.response[:request_id]).to eq 111111
|
86
|
+
expect(req.response[:transaction_id]).to eq 2222222
|
87
|
+
expect(req.response[:authorization]).to eq 444444
|
88
|
+
expect(req.response[:wallet]).to eq "my_wallet_code"
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
it "should make a auth-only on a subscriber" do
|
93
|
+
stub_response "CODEREPONSE=00000&COMMENTAIRE=&NUMAPPEL=0000111111&NUMTRANS=0002222222&AUTORISATION=444444" if !DO_CALLS
|
94
|
+
|
95
|
+
if DO_CALLS
|
96
|
+
req = PayboxDirect.authorize(
|
97
|
+
ref: "auth_only_subscriber_create",
|
98
|
+
amount: 1,
|
99
|
+
currency: :EUR,
|
100
|
+
cc_number: CC_NUMBER,
|
101
|
+
cc_expire: CC_EXPIRE,
|
102
|
+
cc_cvv: CC_CVV,
|
103
|
+
subscriber: "my_sub_id2"
|
104
|
+
)
|
105
|
+
wallet = req.response[:wallet]
|
106
|
+
else
|
107
|
+
wallet = "my_wallet_code"
|
108
|
+
end
|
109
|
+
|
110
|
+
req = PayboxDirect.authorize(
|
111
|
+
ref: "auth_only_subscriber",
|
112
|
+
amount: 25,
|
113
|
+
currency: :EUR,
|
114
|
+
wallet: wallet,
|
115
|
+
cc_expire: CC_EXPIRE,
|
116
|
+
cc_cvv: CC_CVV,
|
117
|
+
subscriber: "my_sub_id2"
|
118
|
+
)
|
119
|
+
|
120
|
+
expect(req.vars).to include({
|
121
|
+
"TYPE" => "00051",
|
122
|
+
"REFERENCE" => REF_PREFIX + "auth_only_subscriber",
|
123
|
+
"MONTANT" => "0000002500",
|
124
|
+
"DEVISE" => "978",
|
125
|
+
"PORTEUR" => wallet,
|
126
|
+
"DATEVAL" => CC_EXPIRE.strftime("%m%y"),
|
127
|
+
"CVV" => CC_CVV,
|
128
|
+
"REFABONNE" => REF_PREFIX + "my_sub_id2"
|
129
|
+
})
|
130
|
+
|
131
|
+
if DO_CALLS
|
132
|
+
expect(req.response[:request_id]).to be_a Fixnum
|
133
|
+
expect(req.response[:transaction_id]).to be_a Fixnum
|
134
|
+
expect(req.response[:authorization]).to be_nil
|
135
|
+
else
|
136
|
+
expect(req.response[:request_id]).to eq 111111
|
137
|
+
expect(req.response[:transaction_id]).to eq 2222222
|
138
|
+
expect(req.response[:authorization]).to eq 444444
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
it "should make a debit without subscription" do
|
143
|
+
stub_response "CODEREPONSE=00000&COMMENTAIRE=&NUMAPPEL=0000111111&NUMTRANS=0002222222&AUTORISATION=444444" if !DO_CALLS
|
144
|
+
|
145
|
+
req = PayboxDirect.debit(
|
146
|
+
ref: "debit",
|
147
|
+
amount: 50.3,
|
148
|
+
currency: :EUR,
|
149
|
+
cc_number: CC_NUMBER,
|
150
|
+
cc_expire: CC_EXPIRE,
|
151
|
+
cc_cvv: CC_CVV
|
152
|
+
)
|
153
|
+
|
154
|
+
expect(req.vars).to include({
|
155
|
+
"TYPE" => "00003",
|
156
|
+
"REFERENCE" => REF_PREFIX + "debit",
|
157
|
+
"MONTANT" => "0000005030",
|
158
|
+
"DEVISE" => "978",
|
159
|
+
"PORTEUR" => CC_NUMBER.gsub("-", ""),
|
160
|
+
"DATEVAL" => CC_EXPIRE.strftime("%m%y"),
|
161
|
+
"CVV" => CC_CVV
|
162
|
+
})
|
163
|
+
|
164
|
+
if DO_CALLS
|
165
|
+
expect(req.response[:request_id]).to be_a Fixnum
|
166
|
+
expect(req.response[:transaction_id]).to be_a Fixnum
|
167
|
+
expect(req.response[:authorization]).to be_nil
|
168
|
+
else
|
169
|
+
expect(req.response[:request_id]).to eq 111111
|
170
|
+
expect(req.response[:transaction_id]).to eq 2222222
|
171
|
+
expect(req.response[:authorization]).to eq 444444
|
172
|
+
end
|
173
|
+
end
|
174
|
+
|
175
|
+
it "should make a debit and create a subscriber" do
|
176
|
+
if !DO_CALLS
|
177
|
+
allow_any_instance_of(PayboxDirect::Request).to receive(:run_http_post!) { |req|
|
178
|
+
if req.vars["TYPE"] == "00056"
|
179
|
+
OpenStruct.new({
|
180
|
+
code: "200",
|
181
|
+
body: "CODEREPONSE=00000&COMMENTAIRE=&NUMAPPEL=0000111111&NUMTRANS=0002222222&AUTORISATION=444444&PORTEUR=my_wallet_code"
|
182
|
+
})
|
183
|
+
else
|
184
|
+
OpenStruct.new({
|
185
|
+
code: "200",
|
186
|
+
body: "CODEREPONSE=00000&COMMENTAIRE="
|
187
|
+
})
|
188
|
+
end
|
189
|
+
}
|
190
|
+
end
|
191
|
+
|
192
|
+
expect(PayboxDirect).to receive(:debit_authorization).with(
|
193
|
+
amount: 50.3,
|
194
|
+
currency: :EUR,
|
195
|
+
request_id: DO_CALLS ? be_a(Fixnum) : 111111,
|
196
|
+
transaction_id: DO_CALLS ? be_a(Fixnum) : 2222222
|
197
|
+
).and_call_original
|
198
|
+
|
199
|
+
req = PayboxDirect.debit(
|
200
|
+
ref: "debit_new_subscriber",
|
201
|
+
amount: 50.3,
|
202
|
+
currency: :EUR,
|
203
|
+
cc_number: CC_NUMBER,
|
204
|
+
cc_expire: CC_EXPIRE,
|
205
|
+
cc_cvv: CC_CVV,
|
206
|
+
subscriber: "my_sub_id3"
|
207
|
+
)
|
208
|
+
|
209
|
+
expect(req.vars).to include({
|
210
|
+
"TYPE" => "00056",
|
211
|
+
"REFERENCE" => REF_PREFIX + "debit_new_subscriber",
|
212
|
+
"MONTANT" => "0000005030",
|
213
|
+
"DEVISE" => "978",
|
214
|
+
"PORTEUR" => CC_NUMBER.gsub("-", ""),
|
215
|
+
"DATEVAL" => CC_EXPIRE.strftime("%m%y"),
|
216
|
+
"CVV" => CC_CVV,
|
217
|
+
"REFABONNE" => REF_PREFIX + "my_sub_id3"
|
218
|
+
})
|
219
|
+
|
220
|
+
if DO_CALLS
|
221
|
+
expect(req.response[:request_id]).to be_a Fixnum
|
222
|
+
expect(req.response[:transaction_id]).to be_a Fixnum
|
223
|
+
expect(req.response[:authorization]).to be_nil
|
224
|
+
expect(req.response[:wallet]).to eq "CMDLpStLLLs"
|
225
|
+
else
|
226
|
+
expect(req.response[:request_id]).to eq 111111
|
227
|
+
expect(req.response[:transaction_id]).to eq 2222222
|
228
|
+
expect(req.response[:authorization]).to eq 444444
|
229
|
+
expect(req.response[:wallet]).to eq "my_wallet_code"
|
230
|
+
end
|
231
|
+
end
|
232
|
+
|
233
|
+
it "should make a debit on a subscriber" do
|
234
|
+
stub_response "CODEREPONSE=00000&COMMENTAIRE=&NUMAPPEL=0000111111&NUMTRANS=0002222222&AUTORISATION=444444" if !DO_CALLS
|
235
|
+
|
236
|
+
if DO_CALLS
|
237
|
+
req = PayboxDirect.authorize(
|
238
|
+
ref: "debit_subscriber_create",
|
239
|
+
amount: 1,
|
240
|
+
currency: :EUR,
|
241
|
+
cc_number: CC_NUMBER,
|
242
|
+
cc_expire: CC_EXPIRE,
|
243
|
+
cc_cvv: CC_CVV,
|
244
|
+
subscriber: "my_sub_id4"
|
245
|
+
)
|
246
|
+
wallet = req.response[:wallet]
|
247
|
+
else
|
248
|
+
wallet = "my_wallet_code"
|
249
|
+
end
|
250
|
+
|
251
|
+
req = PayboxDirect.debit(
|
252
|
+
ref: "debit_subscriber",
|
253
|
+
amount: 25,
|
254
|
+
currency: :EUR,
|
255
|
+
wallet: wallet,
|
256
|
+
cc_expire: CC_EXPIRE,
|
257
|
+
cc_cvv: CC_CVV,
|
258
|
+
subscriber: "my_sub_id4"
|
259
|
+
)
|
260
|
+
|
261
|
+
expect(req.vars).to include({
|
262
|
+
"TYPE" => "00053",
|
263
|
+
"REFERENCE" => REF_PREFIX + "debit_subscriber",
|
264
|
+
"MONTANT" => "0000002500",
|
265
|
+
"DEVISE" => "978",
|
266
|
+
"PORTEUR" => wallet,
|
267
|
+
"DATEVAL" => CC_EXPIRE.strftime("%m%y"),
|
268
|
+
"CVV" => CC_CVV,
|
269
|
+
"REFABONNE" => REF_PREFIX + "my_sub_id4"
|
270
|
+
})
|
271
|
+
|
272
|
+
if DO_CALLS
|
273
|
+
expect(req.response[:request_id]).to be_a Fixnum
|
274
|
+
expect(req.response[:transaction_id]).to be_a Fixnum
|
275
|
+
expect(req.response[:authorization]).to be_nil
|
276
|
+
else
|
277
|
+
expect(req.response[:request_id]).to eq 111111
|
278
|
+
expect(req.response[:transaction_id]).to eq 2222222
|
279
|
+
expect(req.response[:authorization]).to eq 444444
|
280
|
+
end
|
281
|
+
end
|
282
|
+
|
283
|
+
it "should make debit on a prior auth-only" do
|
284
|
+
stub_response "CODEREPONSE=00000&COMMENTAIRE=" if !DO_CALLS
|
285
|
+
|
286
|
+
if DO_CALLS
|
287
|
+
req = PayboxDirect.authorize(
|
288
|
+
ref: "auth_to_debit",
|
289
|
+
amount: 18.20,
|
290
|
+
currency: :EUR,
|
291
|
+
cc_number: CC_NUMBER,
|
292
|
+
cc_expire: CC_EXPIRE,
|
293
|
+
cc_cvv: CC_CVV
|
294
|
+
)
|
295
|
+
req_id = req.response[:request_id]
|
296
|
+
trans_id = req.response[:transaction_id]
|
297
|
+
else
|
298
|
+
req_id = 111111
|
299
|
+
trans_id = 2222222
|
300
|
+
end
|
301
|
+
|
302
|
+
req = PayboxDirect.debit_authorization(
|
303
|
+
amount: 18.20,
|
304
|
+
currency: :EUR,
|
305
|
+
request_id: req_id,
|
306
|
+
transaction_id: trans_id
|
307
|
+
)
|
308
|
+
|
309
|
+
expect(req.vars).to include({
|
310
|
+
"TYPE" => "00002",
|
311
|
+
"MONTANT" => "0000001820",
|
312
|
+
"DEVISE" => "978",
|
313
|
+
"NUMAPPEL" => req_id.to_s.rjust(10, "0"),
|
314
|
+
"NUMTRANS" => trans_id.to_s.rjust(10, "0")
|
315
|
+
})
|
316
|
+
end
|
317
|
+
end
|
318
|
+
|
319
|
+
it "should cancel an operation" do
|
320
|
+
stub_response "CODEREPONSE=00000&COMMENTAIRE=" if !DO_CALLS
|
321
|
+
|
322
|
+
if DO_CALLS
|
323
|
+
req = PayboxDirect.debit(
|
324
|
+
ref: "debit_to_cancel",
|
325
|
+
amount: 38.14,
|
326
|
+
currency: :EUR,
|
327
|
+
cc_number: CC_NUMBER,
|
328
|
+
cc_expire: CC_EXPIRE,
|
329
|
+
cc_cvv: CC_CVV
|
330
|
+
)
|
331
|
+
req_id = req.response[:request_id]
|
332
|
+
trans_id = req.response[:transaction_id]
|
333
|
+
else
|
334
|
+
req_id = 111111
|
335
|
+
trans_id = 2222222
|
336
|
+
end
|
337
|
+
|
338
|
+
req = PayboxDirect.cancel(
|
339
|
+
amount: 38.14,
|
340
|
+
currency: :EUR,
|
341
|
+
ref: "debit_to_cancel",
|
342
|
+
cc_expire: CC_EXPIRE,
|
343
|
+
cc_cvv: CC_CVV,
|
344
|
+
request_id: req_id,
|
345
|
+
transaction_id: trans_id
|
346
|
+
)
|
347
|
+
|
348
|
+
expect(req.vars).to include({
|
349
|
+
"TYPE" => "00005",
|
350
|
+
"REFERENCE" => REF_PREFIX + "debit_to_cancel",
|
351
|
+
"MONTANT" => "0000003814",
|
352
|
+
"DEVISE" => "978",
|
353
|
+
"DATEVAL" => CC_EXPIRE.strftime("%m%y"),
|
354
|
+
"CVV" => CC_CVV,
|
355
|
+
"NUMAPPEL" => req_id.to_s.rjust(10, "0"),
|
356
|
+
"NUMTRANS" => trans_id.to_s.rjust(10, "0")
|
357
|
+
})
|
358
|
+
end
|
359
|
+
|
360
|
+
it "should refund a debited payment" do
|
361
|
+
stub_response "CODEREPONSE=00000&COMMENTAIRE="
|
362
|
+
|
363
|
+
req = PayboxDirect.refund(
|
364
|
+
amount: 10.20,
|
365
|
+
currency: :EUR,
|
366
|
+
request_id: 111111,
|
367
|
+
transaction_id: 2222222
|
368
|
+
)
|
369
|
+
|
370
|
+
expect(req.vars).to include({
|
371
|
+
"TYPE" => "00014",
|
372
|
+
"MONTANT" => "0000001020",
|
373
|
+
"DEVISE" => "978",
|
374
|
+
"NUMAPPEL" => "0000111111",
|
375
|
+
"NUMTRANS" => "0002222222"
|
376
|
+
})
|
377
|
+
end
|
378
|
+
end
|
@@ -0,0 +1,99 @@
|
|
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 'paybox_direct'
|
7
|
+
|
8
|
+
RSpec.describe PayboxDirect::Request do
|
9
|
+
before do
|
10
|
+
PayboxDirect.config.site = "444444"
|
11
|
+
PayboxDirect.config.rank = "88"
|
12
|
+
PayboxDirect.config.login = "my_login"
|
13
|
+
PayboxDirect.config.password = "my_password"
|
14
|
+
PayboxDirect.config.version = 104
|
15
|
+
PayboxDirect.config.activity = 24
|
16
|
+
PayboxDirect.config.bank = "ems"
|
17
|
+
PayboxDirect.config.is_prod = false
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should assign variables" do
|
21
|
+
fake_time = DateTime.parse("2015-09-12 13:32:51").to_time.utc
|
22
|
+
expect(Time).to receive(:new).with(no_args).at_least(:once).and_return(fake_time)
|
23
|
+
|
24
|
+
req = PayboxDirect::Request.new("VAR1" => "VAL1", "VAR2" => "VAL2")
|
25
|
+
expect(req.vars).to eq({
|
26
|
+
"VERSION" => "00104",
|
27
|
+
"SITE" => "0444444",
|
28
|
+
"RANG" => "88",
|
29
|
+
"CLE" => "my_password",
|
30
|
+
"DATEQ" => "12092015133251",
|
31
|
+
"NUMQUESTION" => "0133251000",
|
32
|
+
"ACTIVITE" => "024",
|
33
|
+
"ACQUEREUR" => "ems",
|
34
|
+
"VAR1" => "VAL1",
|
35
|
+
"VAR2" => "VAL2"
|
36
|
+
})
|
37
|
+
end
|
38
|
+
|
39
|
+
it "should parse result error codes" do
|
40
|
+
stub_response "CODEREPONSE=00008&COMMENTAIRE=My+error+description"
|
41
|
+
|
42
|
+
req = PayboxDirect::Request.new("VAR1" => "VAL1", "VAR2" => "VAL2")
|
43
|
+
req.execute!
|
44
|
+
|
45
|
+
expect(req).to be_failed
|
46
|
+
expect(req.error_code).to eq 8
|
47
|
+
expect(req.error_comment).to eq "My error description"
|
48
|
+
end
|
49
|
+
|
50
|
+
it "should raise ServerUnavailable in dev" do
|
51
|
+
was_prod = PayboxDirect.config.is_prod
|
52
|
+
PayboxDirect.config.is_prod = false
|
53
|
+
dev_uri = URI(PayboxDirect::DEV_URL)
|
54
|
+
|
55
|
+
req = PayboxDirect::Request.new("VAR1" => "VAL1", "VAR2" => "VAL2")
|
56
|
+
expect(req).to receive(:run_http_post!).with(dev_uri).and_raise(SocketError)
|
57
|
+
expect {
|
58
|
+
req.execute!
|
59
|
+
}.to raise_error(PayboxDirect::ServerUnavailableError)
|
60
|
+
|
61
|
+
PayboxDirect.config.is_prod = was_prod
|
62
|
+
end
|
63
|
+
|
64
|
+
it "should fallback on alt URL in prod" do
|
65
|
+
was_prod = PayboxDirect.config.is_prod
|
66
|
+
PayboxDirect.config.is_prod = true
|
67
|
+
prod_uri = URI(PayboxDirect::PROD_URL)
|
68
|
+
prod_fallback_uri = URI(PayboxDirect::PROD_FALLBACK_URL)
|
69
|
+
|
70
|
+
req = PayboxDirect::Request.new("VAR1" => "VAL1", "VAR2" => "VAL2")
|
71
|
+
expect(req).to receive(:run_http_post!).with(prod_uri).and_raise(SocketError)
|
72
|
+
expect(req).to receive(:run_http_post!).with(prod_fallback_uri) {
|
73
|
+
OpenStruct.new({
|
74
|
+
code: "200",
|
75
|
+
body: "CODEREPONSE=00000&COMMENTAIRE=OK"
|
76
|
+
})
|
77
|
+
}
|
78
|
+
req.execute!
|
79
|
+
expect(req).not_to be_failed
|
80
|
+
|
81
|
+
PayboxDirect.config.is_prod = was_prod
|
82
|
+
end
|
83
|
+
|
84
|
+
it "should raise ServerUnavailable in prod" do
|
85
|
+
was_prod = PayboxDirect.config.is_prod
|
86
|
+
PayboxDirect.config.is_prod = true
|
87
|
+
prod_uri = URI(PayboxDirect::PROD_URL)
|
88
|
+
prod_fallback_uri = URI(PayboxDirect::PROD_FALLBACK_URL)
|
89
|
+
|
90
|
+
req = PayboxDirect::Request.new("VAR1" => "VAL1", "VAR2" => "VAL2")
|
91
|
+
expect(req).to receive(:run_http_post!).with(prod_uri).and_raise(SocketError)
|
92
|
+
expect(req).to receive(:run_http_post!).with(prod_fallback_uri).and_raise(SocketError)
|
93
|
+
expect {
|
94
|
+
req.execute!
|
95
|
+
}.to raise_error(PayboxDirect::ServerUnavailableError)
|
96
|
+
|
97
|
+
PayboxDirect.config.is_prod = was_prod
|
98
|
+
end
|
99
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,117 @@
|
|
1
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
2
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
3
|
+
# The generated `.rspec` file contains `--require spec_helper` which will cause
|
4
|
+
# this file to always be loaded, without a need to explicitly require it in any
|
5
|
+
# files.
|
6
|
+
#
|
7
|
+
# Given that it is always loaded, you are encouraged to keep this file as
|
8
|
+
# light-weight as possible. Requiring heavyweight dependencies from this file
|
9
|
+
# will add to the boot time of your test suite on EVERY test run, even for an
|
10
|
+
# individual file that may not need all of that loaded. Instead, consider making
|
11
|
+
# a separate helper file that requires the additional dependencies and performs
|
12
|
+
# the additional setup, and require it from the spec files that actually need
|
13
|
+
# it.
|
14
|
+
#
|
15
|
+
# The `.rspec` file also contains a few flags that are not defaults but that
|
16
|
+
# users commonly want.
|
17
|
+
#
|
18
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
19
|
+
RSpec.configure do |config|
|
20
|
+
# rspec-expectations config goes here. You can use an alternate
|
21
|
+
# assertion/expectation library such as wrong or the stdlib/minitest
|
22
|
+
# assertions if you prefer.
|
23
|
+
config.expect_with :rspec do |expectations|
|
24
|
+
# This option will default to `true` in RSpec 4. It makes the `description`
|
25
|
+
# and `failure_message` of custom matchers include text for helper methods
|
26
|
+
# defined using `chain`, e.g.:
|
27
|
+
# be_bigger_than(2).and_smaller_than(4).description
|
28
|
+
# # => "be bigger than 2 and smaller than 4"
|
29
|
+
# ...rather than:
|
30
|
+
# # => "be bigger than 2"
|
31
|
+
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
|
32
|
+
end
|
33
|
+
|
34
|
+
# rspec-mocks config goes here. You can use an alternate test double
|
35
|
+
# library (such as bogus or mocha) by changing the `mock_with` option here.
|
36
|
+
config.mock_with :rspec do |mocks|
|
37
|
+
# Prevents you from mocking or stubbing a method that does not exist on
|
38
|
+
# a real object. This is generally recommended, and will default to
|
39
|
+
# `true` in RSpec 4.
|
40
|
+
mocks.verify_partial_doubles = true
|
41
|
+
end
|
42
|
+
|
43
|
+
# The settings below are suggested to provide a good initial experience
|
44
|
+
# with RSpec, but feel free to customize to your heart's content.
|
45
|
+
=begin
|
46
|
+
# These two settings work together to allow you to limit a spec run
|
47
|
+
# to individual examples or groups you care about by tagging them with
|
48
|
+
# `:focus` metadata. When nothing is tagged with `:focus`, all examples
|
49
|
+
# get run.
|
50
|
+
config.filter_run :focus
|
51
|
+
config.run_all_when_everything_filtered = true
|
52
|
+
|
53
|
+
# Allows RSpec to persist some state between runs in order to support
|
54
|
+
# the `--only-failures` and `--next-failure` CLI options. We recommend
|
55
|
+
# you configure your source control system to ignore this file.
|
56
|
+
config.example_status_persistence_file_path = "spec/examples.txt"
|
57
|
+
|
58
|
+
# Limits the available syntax to the non-monkey patched syntax that is
|
59
|
+
# recommended. For more details, see:
|
60
|
+
# - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
|
61
|
+
# - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
|
62
|
+
# - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
|
63
|
+
config.disable_monkey_patching!
|
64
|
+
|
65
|
+
# This setting enables warnings. It's recommended, but in some cases may
|
66
|
+
# be too noisy due to issues in dependencies.
|
67
|
+
config.warnings = true
|
68
|
+
|
69
|
+
# Many RSpec users commonly either run the entire suite or an individual
|
70
|
+
# file, and it's useful to allow more verbose output when running an
|
71
|
+
# individual spec file.
|
72
|
+
if config.files_to_run.one?
|
73
|
+
# Use the documentation formatter for detailed output,
|
74
|
+
# unless a formatter has already been configured
|
75
|
+
# (e.g. via a command-line flag).
|
76
|
+
config.default_formatter = 'doc'
|
77
|
+
end
|
78
|
+
|
79
|
+
# Print the 10 slowest examples and example groups at the
|
80
|
+
# end of the spec run, to help surface which specs are running
|
81
|
+
# particularly slow.
|
82
|
+
config.profile_examples = 10
|
83
|
+
|
84
|
+
# Run specs in random order to surface order dependencies. If you find an
|
85
|
+
# order dependency and want to debug it, you can fix the order by providing
|
86
|
+
# the seed, which is printed after each run.
|
87
|
+
# --seed 1234
|
88
|
+
config.order = :random
|
89
|
+
|
90
|
+
# Seed global randomization in this process using the `--seed` CLI option.
|
91
|
+
# Setting this allows you to use `--seed` to deterministically reproduce
|
92
|
+
# test failures related to randomization by passing the same `--seed` value
|
93
|
+
# as the one that triggered the failure.
|
94
|
+
Kernel.srand config.seed
|
95
|
+
=end
|
96
|
+
end
|
97
|
+
|
98
|
+
require 'date'
|
99
|
+
|
100
|
+
DO_CALLS = ENV.key?("PB_SITE") and ENV.key?("PB_RANK") and ENV.key?("PB_LOGIN") and ENV.key?("PB_PASSWORD")
|
101
|
+
SITE = ENV["PB_SITE"] || "5555555"
|
102
|
+
RANK = ENV["PB_RANK"] || "88"
|
103
|
+
LOGIN = ENV["PB_LOGIN"] || "my_test_login"
|
104
|
+
PASSWORD = ENV["PB_PASSWORD"] || "my_test_password"
|
105
|
+
REF_PREFIX = "test_#{Time.new.strftime("%Y%m%d%H%I%S%L")}_"
|
106
|
+
CC_NUMBER = "4012-0010-3844-3335"
|
107
|
+
CC_EXPIRE = Date.new(2016, 10, 1)
|
108
|
+
CC_CVV = "123"
|
109
|
+
|
110
|
+
def stub_response(query, status = "200")
|
111
|
+
allow_any_instance_of(PayboxDirect::Request).to receive(:run_http_post!) {
|
112
|
+
OpenStruct.new({
|
113
|
+
code: status,
|
114
|
+
body: query
|
115
|
+
})
|
116
|
+
}
|
117
|
+
end
|
metadata
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: paybox_direct
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Kévin Lesénéchal
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-09-12 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rake
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '10.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '10.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rspec
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '3.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '3.0'
|
41
|
+
description:
|
42
|
+
email: kevin.lesenechal@gmail.com
|
43
|
+
executables: []
|
44
|
+
extensions: []
|
45
|
+
extra_rdoc_files: []
|
46
|
+
files:
|
47
|
+
- LICENSE
|
48
|
+
- README.md
|
49
|
+
- lib/paybox_direct.rb
|
50
|
+
- spec/paybox_direct_spec.rb
|
51
|
+
- spec/request_spec.rb
|
52
|
+
- spec/spec_helper.rb
|
53
|
+
homepage: https://github.com/kevin-lesenechal/paybox_direct
|
54
|
+
licenses:
|
55
|
+
- BSD-3-Clause
|
56
|
+
metadata: {}
|
57
|
+
post_install_message:
|
58
|
+
rdoc_options: []
|
59
|
+
require_paths:
|
60
|
+
- lib
|
61
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
62
|
+
requirements:
|
63
|
+
- - ">="
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '0'
|
66
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
67
|
+
requirements:
|
68
|
+
- - ">="
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: '0'
|
71
|
+
requirements: []
|
72
|
+
rubyforge_project:
|
73
|
+
rubygems_version: 2.4.5.1
|
74
|
+
signing_key:
|
75
|
+
specification_version: 4
|
76
|
+
summary: An API to Paybox Direct and Paybox Direct Plus
|
77
|
+
test_files: []
|