| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- # frozen_string_literal: true
- require 'net/http'
- module Paratika
- class Request
- # attr_reader :order_id, :currency, :amount, :action, :customer, :order_summary
- attr_reader :time, :language, :protocol_version
- attr_reader :merchant, :merchant_user, :merchant_password
- # attr_reader :action
- TRANSACTION_TYPES = [
- 'PREAUTH', # Pre Authorization
- 'SALE', # Charge
- 'VOID', # Cancel
- 'REFUND', # Refund
- 'QUERYCARD', # Cards
- 'EWALLETDELETECARD', # Delete Card
- 'CURRENCYEXCHANGE', # Currency Exchange
- ]
- PROTOCOL_VERSIONS = [
- '2.0'
- ]
-
- CURRENCIES = [
- 'USD', 'GBP', 'EUR', 'TRY',
- 'CHF', 'MXN', 'ARS', 'SAR', 'ZAR', 'INR', 'CNY', 'AUD', 'ILS', 'JPY', 'PLN', 'BOB', 'IDR', 'HUF', 'KWD', 'RUB', 'AED', 'RSD', 'DKK', 'COP', 'CAD', 'BGN', 'NOK', 'RON', 'CZK', 'SEK', 'NZD', 'BRL', 'BHD'
- ]
- def initialize(language: 'EN',
- protocol_version: '2.0',
- time: DateTime.now)
- @language = language.to_s.upcase
- @protocol_version = validate(protocol_version.to_s, of: PROTOCOL_VERSIONS)
- @time = time
- @merchant = Paratika.config.merchant_id
- @merchant_user = Paratika.config.merchant_user
- @merchant_password = Paratika.config.merchant_password
- end
- def decorate
- self.as_json.except!('language', 'protocol_version', 'time')
- .merge!({ "action" => self.class.name.demodulize.upcase })
- .transform_keys!{ |k| k.gsub('_', '').upcase }
- end
- def send
- Paratika::Response.new Net::HTTP.post_form(self.url, self.decorate)
- end
- private
- def url
- URI("#{Paratika.config.url}/paratika/api/v2")
- end
- def validate(value, of:)
- unless of.include?(value)
- raise ArgumentError, "Expected one of #{of.inspect}, got: #{value.inspect}"
- end
- value
- end
- def validate_money(value)
- unless value.is_a?(BigDecimal) && value >= 0.01 && value.scale <= 2
- raise ArgumentError, "Expected BigDecimal value with max scale 2, got: #{value.inspect}"
- end
- "%.2f" % value
- end
- def validate_integer(value)
- if !value.is_a? Integer
- raise ArgumentError, "Value must be an Integer, got: #{value.inspect}"
- end
- value
- end
- def validate_presence(value)
- if !value.is_a?(Numeric) && value.blank?
- raise ArgumentError, "Expected not blank or nil value, got: #{value.inspect}"
- end
- value
- end
- def validate_hash(value)
- if !value.is_a? Hash
- raise ArgumentError, "Expected Hash, got: #{value.inspect}"
- end
- value
- end
- end
- end
|