Class: PciProxy::Check

Inherits:
Base
  • Object
show all
Defined in:
lib/pci_proxy/check.rb

Constant Summary collapse

SANDBOX_ENDPOINT =
'https://api.sandbox.datatrans.com/v1/transactions/validate'.freeze
LIVE_ENDPOINT =
'https://api.datatrans.com/v1/transactions/validate'.freeze
CHF =
'CHF'.freeze
EUR =
'EUR'.freeze

Constants inherited from Base

Base::JSON_UTF8_CONTENT_TYPE

Instance Attribute Summary

Attributes inherited from Base

#api_endpoint, #api_password, #api_username

Instance Method Summary collapse

Methods inherited from Base

#api_get, #api_post, #client, #error_class

Constructor Details

#initialize(api_username:, api_password:, endpoint: SANDBOX_ENDPOINT) ⇒ Check

Initialise with the specified api_username and api_password from PCI Proxy.

Defaults to the sandbox API endpoint - to use the live environment, supply the LIVE_ENDPOINT constant as the value of the endpoint kwarg



18
19
20
21
22
# File 'lib/pci_proxy/check.rb', line 18

def initialize(api_username:, api_password:, endpoint: SANDBOX_ENDPOINT)
  @api_endpoint = endpoint
  @api_username = api_username
  @api_password = api_password
end

Instance Method Details

#execute(reference:, card_token:, card_type:, expiry_month:, expiry_year:, currency: nil) ⇒ Hash

Perform a check API request to verify the card token

Parameters:

  • +reference+ (String)
    • caller’s unique reference (any non-empty string value)

  • +card_token+ (String)
    • card token obtained from the Token API - see PciProxy::Token

  • +card_type+ (Symbol / String)
    • one of ‘visa’, ‘mastercard’ or ‘amex’

  • +expiry_month+ (Integer / String)
    • integer 1-12, or string ‘01’ - ‘12’

  • +expiry_year+ (Integer / String)
    • two or four digit year as a string or integer

  • +currency+ (Integer / String)

    (Optional) - one of ‘CHF’ or ‘EUR’ - will default by card type where not specified

Returns:

  • (Hash)

    result from PCI Proxy, decoded from JSON

Raises:

  • (PciProxyAPIError)

    in cases where the API responds with a non-200 response code



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/pci_proxy/check.rb', line 36

def execute(reference:, card_token:, card_type:, expiry_month:, expiry_year:, currency: nil)
  raise "reference is required" if reference.empty?
  raise "card_token is required" unless card_token && !card_token.empty?
  raise "card_type must be one of 'visa', 'mastercard' or 'amex'" unless [:visa, :mastercard, :amex].include?(card_type.to_sym)
  raise "invalid expiry_month" unless (1..12).include?(expiry_month.to_i)
  raise "invalid expiry_year" unless expiry_year.to_i > 0

  # default the currency where not specified - according to the documentation Amex should use EUR, Visa and MC should use CHF
  currency ||= :amex == card_type.to_sym ? EUR : CHF

  card = {
      alias: card_token,
      expiryMonth: expiry_month.to_s,
      expiryYear: expiry_year.to_s.chars.last(2).join
  }

  body = {
      refno: reference,
      currency: currency,
      card: card
  }

  PciProxy::Model::CheckResult.new(api_post(endpoint: @api_endpoint, body: body, raise_on_error: false))
end