cardinity 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/cardinity.rb +71 -28
- data/lib/cardinity/auth.rb +5 -3
- data/lib/cardinity/utils.rb +27 -10
- data/lib/cardinity/version.rb +3 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 869663da5da3a101dacfec24b52bcfe60ded6df1beece8c0475969fb0db2e597
|
4
|
+
data.tar.gz: 543f216baba16da8ac063faec41a6c9f398827be50eb5a244bf0688fad4d9112
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 748e5e48e97038dd4570afdacd4e659af54169ce3775b5e93a64d52fd4a2981b7900ddd54919ee8735aedfac5a11ddb517cffdc9d779dfec323edba1ab074fec
|
7
|
+
data.tar.gz: b395bf1643b76c7817bc74e18c317747ff09a39dd0af2158d09043236ee194fdfeccc8e82eaf334df33ae3707a66f0053936d213791d4e75f344913e148a13a0
|
data/lib/cardinity.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'cardinity/version'
|
2
4
|
require 'cardinity/utils'
|
3
5
|
require 'cardinity/auth'
|
@@ -18,9 +20,15 @@ module Cardinity
|
|
18
20
|
TYPE_PURCHASE = 'purchase'
|
19
21
|
TYPE_ERROR = 'https://developers.cardinity.com/api/v1/'
|
20
22
|
|
23
|
+
# Configure Cardinity. Must be called before other methods.
|
24
|
+
#
|
25
|
+
# @param [Hash] options
|
26
|
+
# @option options [String] :key API key.
|
27
|
+
# @option options [String] :secret API secret.
|
28
|
+
# @option options [String] :api_base (DEFAULT_API_BASE) API base URL.
|
21
29
|
def self.configure!(options)
|
22
30
|
@config = {
|
23
|
-
# key and secret to be supplied from
|
31
|
+
# key and secret to be supplied from outside
|
24
32
|
api_base: DEFAULT_API_BASE
|
25
33
|
}
|
26
34
|
@config.merge! options
|
@@ -29,24 +37,25 @@ module Cardinity
|
|
29
37
|
|
30
38
|
# Creates a Payment object
|
31
39
|
#
|
32
|
-
#
|
33
|
-
#
|
34
|
-
#
|
35
|
-
#
|
36
|
-
#
|
37
|
-
#
|
38
|
-
#
|
39
|
-
#
|
40
|
-
# -
|
41
|
-
#
|
42
|
-
#
|
43
|
-
#
|
44
|
-
# -
|
45
|
-
# -
|
46
|
-
#
|
47
|
-
#
|
48
|
-
#
|
49
|
-
#
|
40
|
+
# @param [Hash] payment_hash Payment data. Keys can be symbols or strings.
|
41
|
+
# @option payment_hash [Numeric, String] :amount #0.00.
|
42
|
+
# If a string, two decimals are required.
|
43
|
+
# @option payment_hash [String] :currency Three-letter ISO currency code,
|
44
|
+
# e.g. "EUR".
|
45
|
+
# @option payment_hash [Boolean] :settle (true) If false, creates a
|
46
|
+
# pre-authorization instead of settling immediately.
|
47
|
+
# @option payment_hash [String] :country Customer's billing country as
|
48
|
+
# ISO 3166-1 alpha-2 country code, required.
|
49
|
+
# @option payment_hash [String] :payment_method ('card')
|
50
|
+
# @option payment_hash [Hash] :payment_instrument Card details:
|
51
|
+
# * :pan [String] Card number.
|
52
|
+
# * :exp_year [Integer] 4-digit year.
|
53
|
+
# * :exp_month [Integer] 2-digit month
|
54
|
+
# * :cvc [Integer] Card security code.
|
55
|
+
# * :holder [String] Cardholder's name, max length 32 characters.
|
56
|
+
# @option payment_hash [String, nil] :order_id (nil)
|
57
|
+
# @option payment_hash [String, nil] :description (nil)
|
58
|
+
# @return [Hash] Updated payment object or an error object.
|
50
59
|
def self.create_payment(payment_hash)
|
51
60
|
checked_payment_data = check_payment_data(payment_hash)
|
52
61
|
parse post(payments_uri, serialize(checked_payment_data))
|
@@ -57,19 +66,53 @@ module Cardinity
|
|
57
66
|
# This is necessary for 3D secure payments, when the customer has completed
|
58
67
|
# the 3D secure redirects and authorization.
|
59
68
|
#
|
60
|
-
#
|
61
|
-
#
|
62
|
-
#
|
63
|
-
# Returns:
|
64
|
-
# - updated payment object on success
|
65
|
-
# - error object on error
|
69
|
+
# @param [String] payment_id
|
70
|
+
# @param [String] authorize_data PaRes string received from 3D secure.
|
71
|
+
# @return [Hash] Payment or error object.
|
66
72
|
def self.finalize_payment(payment_id, authorize_hash)
|
67
73
|
parse patch(payment_uri(payment_id), serialize(authorize_hash))
|
68
74
|
end
|
69
75
|
|
70
|
-
# Get list of
|
71
|
-
|
72
|
-
|
76
|
+
# Get list of the last payments.
|
77
|
+
# By default, cardinity returns 10 payments. Pass `limit` to override.
|
78
|
+
# @param [Integer, nil] limit
|
79
|
+
# @return [Array<Hash>, Hash] Payment objects or an error object.
|
80
|
+
def self.payments(limit: nil)
|
81
|
+
query = {}
|
82
|
+
query[:limit] = limit if limit
|
83
|
+
parse get(payments_uri, query)
|
84
|
+
end
|
85
|
+
|
86
|
+
# Get the payment information for the given payment ID.
|
87
|
+
# @param [String] payment_id
|
88
|
+
# @return [Hash] Payment or error object.
|
89
|
+
def self.payment(payment_id)
|
90
|
+
parse get(payment_uri(payment_id))
|
73
91
|
end
|
74
92
|
|
93
|
+
# Fully or partially refund a payment
|
94
|
+
# @param [String] payment_id
|
95
|
+
# @param [Numeric, String] amount If a string, two decimals are required.
|
96
|
+
# @param [String] description
|
97
|
+
# @return [Hash] Refund or error object.
|
98
|
+
def self.create_refund(payment_id, amount:, description: '')
|
99
|
+
amount = format('%.2f', amount) if amount.is_a?(Numeric)
|
100
|
+
parse post(refunds_uri(payment_id),
|
101
|
+
serialize(amount: amount, description: description))
|
102
|
+
end
|
103
|
+
|
104
|
+
# Get all the refunds for the given payment ID.
|
105
|
+
# @param [String] payment_id
|
106
|
+
# @return [Array<Hash>, Hash] Refund objects or an error object.
|
107
|
+
def self.refunds(payment_id)
|
108
|
+
parse get(refunds_uri(payment_id))
|
109
|
+
end
|
110
|
+
|
111
|
+
# Get the refund for the given payment ID and refund ID.
|
112
|
+
# @param [String] payment_id
|
113
|
+
# @param [String] refund_id
|
114
|
+
# @return [Hash] Refund or error object.
|
115
|
+
def self.refund(payment_id, refund_id)
|
116
|
+
parse get(refund_uri(payment_id, refund_id))
|
117
|
+
end
|
75
118
|
end
|
data/lib/cardinity/auth.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'base64'
|
2
4
|
require 'openssl'
|
3
5
|
require 'uri'
|
@@ -12,16 +14,16 @@ class Cardinity::Auth
|
|
12
14
|
@config = config
|
13
15
|
end
|
14
16
|
|
15
|
-
def sign_request(method, uri)
|
17
|
+
def sign_request(method, uri, params = {})
|
16
18
|
params = {
|
17
19
|
oauth_consumer_key: @config[:key],
|
18
20
|
oauth_nonce: generate_key,
|
19
21
|
oauth_signature_method: SIGNATURE_METHOD,
|
20
22
|
oauth_timestamp: generate_timestamp,
|
21
23
|
oauth_version: OAUTH_VERSION
|
22
|
-
}
|
24
|
+
}.update(params)
|
23
25
|
params[:oauth_signature] = request_signature(method, uri, params)
|
24
|
-
params_str = params.
|
26
|
+
params_str = params.map { |k, v| "#{k}=\"#{escape(v.to_s)}\"" }.join(', ')
|
25
27
|
"OAuth #{params_str}"
|
26
28
|
end
|
27
29
|
|
data/lib/cardinity/utils.rb
CHANGED
@@ -1,14 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'uri'
|
4
|
+
|
1
5
|
module Cardinity
|
2
6
|
|
3
|
-
CODES_WITH_RESPONSE = [200, 201, 202, 400, 402]
|
7
|
+
CODES_WITH_RESPONSE = [200, 201, 202, 400, 401, 402, 405, 500, 503]
|
4
8
|
|
5
9
|
def self.check_payment_data(payment)
|
6
10
|
payment = payment.dup
|
7
|
-
|
8
|
-
|
11
|
+
payment_method = payment['payment_method'] || payment[:payment_method]
|
12
|
+
if payment_method.nil?
|
13
|
+
payment['payment_method'] = 'card'
|
9
14
|
end
|
10
|
-
|
11
|
-
|
15
|
+
amount = payment['amount'] || payment[:amount]
|
16
|
+
if amount.is_a?(Numeric)
|
17
|
+
payment.delete(:amount)
|
18
|
+
payment['amount'] = format('%.2f', amount)
|
12
19
|
end
|
13
20
|
payment
|
14
21
|
end
|
@@ -21,6 +28,14 @@ module Cardinity
|
|
21
28
|
"#{api_base}#{API_PAYMENTS}/#{payment_id}"
|
22
29
|
end
|
23
30
|
|
31
|
+
def self.refunds_uri(payment_id)
|
32
|
+
"#{api_base}#{API_PAYMENTS}/#{payment_id}/refunds"
|
33
|
+
end
|
34
|
+
|
35
|
+
def self.refund_uri(payment_id, refund_id)
|
36
|
+
"#{api_base}#{API_PAYMENTS}/#{payment_id}/refunds/#{refund_id}"
|
37
|
+
end
|
38
|
+
|
24
39
|
def self.parse(response)
|
25
40
|
JSON.parse response.body
|
26
41
|
end
|
@@ -37,8 +52,10 @@ module Cardinity
|
|
37
52
|
end
|
38
53
|
end
|
39
54
|
|
40
|
-
def self.get(
|
41
|
-
|
55
|
+
def self.get(base_url, params = {})
|
56
|
+
uri = URI.parse(base_url)
|
57
|
+
uri.query = URI.encode_www_form(params)
|
58
|
+
RestClient.get uri.to_s, headers(:get, base_url, params)
|
42
59
|
rescue RestClient::ExceptionWithResponse => e
|
43
60
|
handle_error_response e
|
44
61
|
end
|
@@ -55,10 +72,10 @@ module Cardinity
|
|
55
72
|
handle_error_response e
|
56
73
|
end
|
57
74
|
|
58
|
-
def self.headers(method, uri)
|
75
|
+
def self.headers(method, uri, params = {})
|
59
76
|
{
|
60
77
|
content_type: 'application/json',
|
61
|
-
authorization: @auth.sign_request(method, uri)
|
78
|
+
authorization: @auth.sign_request(method, uri, params)
|
62
79
|
}
|
63
80
|
end
|
64
81
|
|
@@ -66,4 +83,4 @@ module Cardinity
|
|
66
83
|
@config[:api_base]
|
67
84
|
end
|
68
85
|
|
69
|
-
end
|
86
|
+
end
|
data/lib/cardinity/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cardinity
|
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
|
- Jan Sterba
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-10-
|
11
|
+
date: 2018-10-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|