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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 89277a2f03c1fd84b02c4ccf81686059c0566d734e9f8b82ad0695a33c096f91
4
- data.tar.gz: 2602f3ad85e8937a94b125e704bf10f9586c8dee9c6b2db0ecdbe8beb1617af8
3
+ metadata.gz: 869663da5da3a101dacfec24b52bcfe60ded6df1beece8c0475969fb0db2e597
4
+ data.tar.gz: 543f216baba16da8ac063faec41a6c9f398827be50eb5a244bf0688fad4d9112
5
5
  SHA512:
6
- metadata.gz: 60e1b3633a5df1ea4e226544e659cd87e45aac2f41a0275d066a36b8ccf48a9a9713166273307cdb731689adb4780a2626d32b7160bf87822d9e5c5d7f0af581
7
- data.tar.gz: 93e4477d0dbd84e596b71921ebe6ad0ba3ed412d2af14d53cbef93de447c6982f2dacfc84bfbf94f15bf2890ae191d0fa014c17390eb07b86a208d1baaa4297d
6
+ metadata.gz: 748e5e48e97038dd4570afdacd4e659af54169ce3775b5e93a64d52fd4a2981b7900ddd54919ee8735aedfac5a11ddb517cffdc9d779dfec323edba1ab074fec
7
+ data.tar.gz: b395bf1643b76c7817bc74e18c317747ff09a39dd0af2158d09043236ee194fdfeccc8e82eaf334df33ae3707a66f0053936d213791d4e75f344913e148a13a0
@@ -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 outsite
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
- # Accepted attributes:
33
- # - amount (#0.00, two decimals required)
34
- # - currency (EUR,USD)
35
- # - settle (boolean, default true, false means just pre-authorize and finish later)
36
- # - order_id (not required)
37
- # - description (not required)
38
- # - country (country of customer, required)
39
- # - payment_method (required, only supported value is card)
40
- # - payment_instrument (card details)
41
- # - pan (card number)
42
- # - exp_year
43
- # - exp_month
44
- # - cvc
45
- # - holder
46
- #
47
- # Returns:
48
- # - updated payment object on success
49
- # - error object on error
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
- # Accepted attributes:
61
- # - authorize_data (PaRes string received from 3D secure)
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 all payments
71
- def self.payments
72
- parse get(payments_uri)
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
@@ -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.collect { |k, v| "#{k}=\"#{escape(v)}\"" }.join(', ')
26
+ params_str = params.map { |k, v| "#{k}=\"#{escape(v.to_s)}\"" }.join(', ')
25
27
  "OAuth #{params_str}"
26
28
  end
27
29
 
@@ -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
- if payment[:payment_method].nil?
8
- payment[:payment_method] = 'card'
11
+ payment_method = payment['payment_method'] || payment[:payment_method]
12
+ if payment_method.nil?
13
+ payment['payment_method'] = 'card'
9
14
  end
10
- if payment[:amount].is_a?(Numeric)
11
- payment[:amount] = '%.2f' % payment[:amount]
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(uri)
41
- RestClient.get uri, headers(:get, uri)
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
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Cardinity
2
- VERSION = "0.1.1"
4
+ VERSION = "0.1.2"
3
5
  end
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.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-19 00:00:00.000000000 Z
11
+ date: 2018-10-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler