module Cardinity
Constants
- API_PAYMENTS
- CODES_WITH_RESPONSE
- DEFAULT_API_BASE
- STATUS_APPROVED
- STATUS_DECLINED
- STATUS_PENDING
- TYPE_AUTHORIZATION
- TYPE_ERROR
- TYPE_PURCHASE
- VERSION
Public Class Methods
# File lib/cardinity/utils.rb, line 82 def self.api_base @config[:api_base] end
# File lib/cardinity/utils.rb, line 9 def self.check_payment_data(payment) payment = payment.dup payment_method = payment['payment_method'] || payment[:payment_method] if payment_method.nil? payment['payment_method'] = 'card' end amount = payment['amount'] || payment[:amount] if amount.is_a?(Numeric) payment.delete(:amount) payment['amount'] = format('%.2f', amount) end payment end
Configure Cardinity
. Must be called before other methods.
@param [Hash] options @option options [String] :key API key. @option options [String] :secret API secret. @option options [String] :api_base (DEFAULT_API_BASE
) API base URL.
# File lib/cardinity.rb, line 29 def self.configure!(options) @config = { # key and secret to be supplied from outside api_base: DEFAULT_API_BASE } @config.merge! options @auth = Cardinity::Auth.new(@config) end
Creates a Payment object
@param [Hash] payment_hash Payment data. Keys can be symbols or strings. @option payment_hash [Numeric, String] :amount #0.00.
If a string, two decimals are required.
@option payment_hash [String] :currency Three-letter ISO currency code,
e.g. "EUR".
@option payment_hash [Boolean] :settle (true) If false, creates a
pre-authorization instead of settling immediately.
@option payment_hash [String] :country Customer's billing country as
ISO 3166-1 alpha-2 country code, required.
@option payment_hash [String] :payment_method ('card') @option payment_hash [Hash] :payment_instrument Card details:
* :pan [String] Card number. * :exp_year [Integer] 4-digit year. * :exp_month [Integer] 2-digit month * :cvc [Integer] Card security code. * :holder [String] Cardholder's name, max length 32 characters.
@option payment_hash [String, nil] :order_id (nil) @option payment_hash [String, nil] :description (nil) @return [Hash] Updated payment object or an error object.
# File lib/cardinity.rb, line 59 def self.create_payment(payment_hash) checked_payment_data = check_payment_data(payment_hash) parse post(payments_uri, serialize(checked_payment_data)) end
Fully or partially refund a payment @param [String] payment_id @param [Numeric, String] amount If a string, two decimals are required. @param [String] description @return [Hash] Refund or error object.
# File lib/cardinity.rb, line 98 def self.create_refund(payment_id, amount:, description: '') amount = format('%.2f', amount) if amount.is_a?(Numeric) parse post(refunds_uri(payment_id), serialize(amount: amount, description: description)) end
Finalizes a Payment
This is necessary for 3D secure payments, when the customer has completed the 3D secure redirects and authorization.
@param [String] payment_id @param [String] authorize_data PaRes string received from 3D secure. @return [Hash] Payment or error object.
# File lib/cardinity.rb, line 72 def self.finalize_payment(payment_id, authorize_hash) parse patch(payment_uri(payment_id), serialize(authorize_hash)) end
# File lib/cardinity/utils.rb, line 55 def self.get(base_url, params = {}) uri = URI.parse(base_url) uri.query = URI.encode_www_form(params) RestClient.get uri.to_s, headers(:get, base_url, params) rescue RestClient::ExceptionWithResponse => e handle_error_response e end
# File lib/cardinity/utils.rb, line 47 def self.handle_error_response(e) if CODES_WITH_RESPONSE.index e.response.code e.response else raise e end end
# File lib/cardinity/utils.rb, line 75 def self.headers(method, uri, params = {}) { content_type: 'application/json', authorization: @auth.sign_request(method, uri, params) } end
# File lib/cardinity/utils.rb, line 39 def self.parse(response) JSON.parse response.body end
# File lib/cardinity/utils.rb, line 69 def self.patch(uri, body) RestClient.patch uri, body, headers(:patch, uri) rescue RestClient::ExceptionWithResponse => e handle_error_response e end
Get the payment information for the given payment ID. @param [String] payment_id @return [Hash] Payment or error object.
# File lib/cardinity.rb, line 89 def self.payment(payment_id) parse get(payment_uri(payment_id)) end
# File lib/cardinity/utils.rb, line 27 def self.payment_uri(payment_id) "#{api_base}#{API_PAYMENTS}/#{payment_id}" end
Get list of the last payments. By default, cardinity returns 10 payments. Pass `limit` to override. @param [Integer, nil] limit @return [Array<Hash>, Hash] Payment objects or an error object.
# File lib/cardinity.rb, line 80 def self.payments(limit: nil) query = {} query[:limit] = limit if limit parse get(payments_uri, query) end
# File lib/cardinity/utils.rb, line 23 def self.payments_uri "#{api_base}#{API_PAYMENTS}" end
# File lib/cardinity/utils.rb, line 63 def self.post(uri, body) RestClient.post uri, body, headers(:post, uri) rescue RestClient::ExceptionWithResponse => e handle_error_response e end
Get the refund for the given payment ID and refund ID. @param [String] payment_id @param [String] refund_id @return [Hash] Refund or error object.
# File lib/cardinity.rb, line 115 def self.refund(payment_id, refund_id) parse get(refund_uri(payment_id, refund_id)) end
# File lib/cardinity/utils.rb, line 35 def self.refund_uri(payment_id, refund_id) "#{api_base}#{API_PAYMENTS}/#{payment_id}/refunds/#{refund_id}" end
Get all the refunds for the given payment ID. @param [String] payment_id @return [Array<Hash>, Hash] Refund objects or an error object.
# File lib/cardinity.rb, line 107 def self.refunds(payment_id) parse get(refunds_uri(payment_id)) end
# File lib/cardinity/utils.rb, line 31 def self.refunds_uri(payment_id) "#{api_base}#{API_PAYMENTS}/#{payment_id}/refunds" end
# File lib/cardinity/utils.rb, line 43 def self.serialize(data) JSON.generate(data) end