Module: Cardinity

Defined in:
lib/cardinity.rb,
lib/cardinity/utils.rb,
lib/cardinity/version.rb

Defined Under Namespace

Classes: Auth

Constant Summary collapse

DEFAULT_API_BASE =
'https://api.cardinity.com/v1/'
API_PAYMENTS =
'payments'
STATUS_PENDING =
'pending'
STATUS_APPROVED =
'approved'
STATUS_DECLINED =
'declined'
TYPE_AUTHORIZATION =
'authorization'
TYPE_PURCHASE =
'purchase'
TYPE_ERROR =
'https://developers.cardinity.com/api/v1/'
CODES_WITH_RESPONSE =
[200, 201, 202, 400, 402]
VERSION =
"0.1.0"

Class Method Summary collapse

Class Method Details

.api_baseObject



65
66
67
# File 'lib/cardinity/utils.rb', line 65

def self.api_base
  @config[:api_base]
end

.check_payment_data(payment) ⇒ Object



5
6
7
8
9
10
11
12
13
14
# File 'lib/cardinity/utils.rb', line 5

def self.check_payment_data(payment)
  payment = payment.dup
  if payment[:payment_method].nil?
    payment[:payment_method] = 'card'
  end
  if payment[:amount].is_a?(Numeric)
    payment[:amount] = '%.2f' % payment[:amount]
  end
  payment
end

.configure!(options) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/cardinity.rb', line 21

def self.configure!(options)
  @config = {
      # key and secret to be supplied from outsite
      api_base: DEFAULT_API_BASE
  }
  @config.merge! options
  @auth = Cardinity::Auth.new(@config)
end

.create_payment(payment_hash) ⇒ Object

Creates a Payment object

Accepted attributes:

  • amount (#0.00, two decimals required)

  • currency (EUR,USD)

  • settle (boolean, default true, false means just pre-authorize and finish later)

  • order_id (not required)

  • description (not required)

  • country (country of customer, required)

  • payment_method (required, only supported value is card)

  • payment_instrument (card details)

    • pan (card number)

    • exp_year

    • exp_month

    • cvc

    • holder

Returns:

  • updated payment object on success

  • error object on error



50
51
52
53
# File 'lib/cardinity.rb', line 50

def self.create_payment(payment_hash)
  checked_payment_data = check_payment_data(payment_hash)
  parse post(payments_uri, serialize(checked_payment_data))
end

.finalize_payment(payment_id, authorize_hash) ⇒ Object

Finalizes a Payment

This is necessary for 3D secure payments, when the customer has completed the 3D secure redirects and authorization.

Accepted attributes:

- authorize_data (PaRes string received from 3D secure)

Returns:

  • updated payment object on success

  • error object on error



66
67
68
# File 'lib/cardinity.rb', line 66

def self.finalize_payment(payment_id, authorize_hash)
  parse patch(payment_uri(payment_id), serialize(authorize_hash))
end

.get(uri) ⇒ Object



40
41
42
43
44
# File 'lib/cardinity/utils.rb', line 40

def self.get(uri)
  RestClient.get uri, headers(:get, uri)
rescue RestClient::ExceptionWithResponse => e
  handle_error_response e
end

.handle_error_response(e) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/cardinity/utils.rb', line 32

def self.handle_error_response(e)
  if CODES_WITH_RESPONSE.index e.response.code
    e.response
  else
    raise e
  end
end

.headers(method, uri) ⇒ Object



58
59
60
61
62
63
# File 'lib/cardinity/utils.rb', line 58

def self.headers(method, uri)
  {
      content_type: 'application/json',
      authorization: @auth.sign_request(method, uri)
  }
end

.parse(response) ⇒ Object



24
25
26
# File 'lib/cardinity/utils.rb', line 24

def self.parse(response)
  JSON.parse response.body
end

.patch(uri, body) ⇒ Object



52
53
54
55
56
# File 'lib/cardinity/utils.rb', line 52

def self.patch(uri, body)
  RestClient.patch uri, body, headers(:patch, uri)
rescue RestClient::ExceptionWithResponse => e
  handle_error_response e
end

.payment_uri(payment_id) ⇒ Object



20
21
22
# File 'lib/cardinity/utils.rb', line 20

def self.payment_uri(payment_id)
  "#{api_base}#{API_PAYMENTS}/#{payment_id}"
end

.paymentsObject

Get list of all payments



71
72
73
# File 'lib/cardinity.rb', line 71

def self.payments
  parse get(payments_uri)
end

.payments_uriObject



16
17
18
# File 'lib/cardinity/utils.rb', line 16

def self.payments_uri
  "#{api_base}#{API_PAYMENTS}"
end

.post(uri, body) ⇒ Object



46
47
48
49
50
# File 'lib/cardinity/utils.rb', line 46

def self.post(uri, body)
  RestClient.post uri, body, headers(:post, uri)
rescue RestClient::ExceptionWithResponse => e
  handle_error_response e
end

.serialize(data) ⇒ Object



28
29
30
# File 'lib/cardinity/utils.rb', line 28

def self.serialize(data)
  JSON.generate(data)
end