Module: AlphaCard

Defined in:
lib/alpha_card.rb,
lib/alpha_card/account.rb,
lib/alpha_card/version.rb,
lib/alpha_card/resource.rb,
lib/alpha_card/response.rb,
lib/alpha_card/attribute.rb,
lib/alpha_card/transaction.rb,
lib/alpha_card/resources/order.rb,
lib/alpha_card/resources/billing.rb,
lib/alpha_card/transactions/auth.rb,
lib/alpha_card/transactions/sale.rb,
lib/alpha_card/transactions/void.rb,
lib/alpha_card/resources/shipping.rb,
lib/alpha_card/transactions/credit.rb,
lib/alpha_card/transactions/refund.rb,
lib/alpha_card/transactions/update.rb,
lib/alpha_card/transactions/capture.rb,
lib/alpha_card/transactions/validate.rb,
lib/alpha_card/errors/validation_error.rb,
lib/alpha_card/errors/api_connection_error.rb,
lib/alpha_card/errors/invalid_attribute_type.rb,
lib/alpha_card/errors/invalid_attribute_value.rb,
lib/alpha_card/errors/invalid_attribute_format.rb

Overview

AlphaCard is a library for processing payments with Alpha Card Services, Inc.

Defined Under Namespace

Modules: Attribute, VERSION Classes: APIConnectionError, Account, Auth, Billing, Capture, Credit, InvalidAttributeFormat, InvalidAttributeType, InvalidAttributeValue, Order, Refund, Resource, Response, Sale, Shipping, Transaction, Update, Validate, ValidationError, Void

Constant Summary collapse

CREDIT_CARD_CODES =

Global Payment Systems (NDC) Credit Card Authorization Codes

YAML.load_file(File.expand_path('../alpha_card/data/credit_card_codes.yml', __FILE__))

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.api_baseString

Returns Alpha Card Gateway DirectPost API URL.

Returns:

  • (String)

    Alpha Card Gateway DirectPost API URL.



54
55
56
# File 'lib/alpha_card.rb', line 54

def api_base
  @api_base
end

Class Method Details

.gem_versionObject

AlphaCard gem version.



4
5
6
# File 'lib/alpha_card/version.rb', line 4

def self.gem_version
  Gem::Version.new VERSION::STRING
end

.handle_connection_errors(error) ⇒ Object

Raises an exception if a network error occurs. It could be request timeout, socket error or anything else.

Parameters:

  • error (Exception)

    exception object

Raises:



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/alpha_card.rb', line 110

def handle_connection_errors(error)
  case error
  when Timeout::Error, Errno::EINVAL, Errno::ECONNRESET
    message = "Could not connect to Alpha Card Gateway (#{@api_base}). " \
        'Please check your internet connection and try again. ' \
        'If this problem persists, you should check Alpha Card services status.'

  when SocketError
    message = 'Unexpected error communicating when trying to connect to Alpha Card Gateway. ' \
        'You may be seeing this message because your DNS is not working.'

  else
    message = 'Unexpected error communicating with Alpha Card Gateway.'
  end

  raise APIConnectionError, "#{message}\n\n(Network error: #{error.message})"
end

.http_post_request(url, params) ⇒ HTTPResponse

Send secure HTTP(S) request with params to requested URL.

Parameters:

  • url (String)

    URL

  • params (Hash)

    hash of params for the request

Returns:

  • (HTTPResponse)

    Response of the request as HTTPResponse object



136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/alpha_card.rb', line 136

def http_post_request(url, params)
  uri = URI.parse(url)

  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE

  request = Net::HTTP::Post.new(uri.request_uri)
  request.set_form_data(params)

  http.request(request)
end

.request(params = {}, credentials = Account.credentials) ⇒ AlphaCard::Response

Send the POST request to the AlphaCard Gateway from the specified account. Request must contains params - Alpha Card transaction variables.

Examples:

response = AlphaCard.request(
  {
    cexp: '0720',
    ccnumber: '4111111111111111',
    amount: '10.00'
  },
  {
    username: 'demo',
    password: 'password'
  }
)

#=> #<AlphaCard::Response:0x1a0fda8 @data={"response"=>"1",
    "responsetext"=>"SUCCESS", "authcode"=>"123", "transactionid"=>"123",
    "avsresponse"=>"", "cvvresponse"=>"N", "orderid"=>"", "type"=>"",
    "response_code"=>"100"}>

Parameters:

  • params (Hash) (defaults to: {})

    hash with Alpha Card transaction variables and it’s values

  • credentials (Hash) (defaults to: Account.credentials)

    Alpha Card merchant account credentials

Returns:

Raises:



90
91
92
93
94
95
96
97
98
99
100
# File 'lib/alpha_card.rb', line 90

def request(params = {}, credentials = Account.credentials)
  raise ArgumentError, 'You must pass a Hash with Account credentials!' unless Account.valid_credentials?(credentials)

  begin
    response = http_post_request(@api_base, params.merge(credentials))
  rescue => e
    handle_connection_errors(e)
  end

  Response.new(response.body)
end