request.rb 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. # frozen_string_literal: true
  2. require 'net/http'
  3. module Paratika
  4. class Request
  5. # attr_reader :order_id, :currency, :amount, :action, :customer, :order_summary
  6. attr_reader :time, :language, :protocol_version
  7. attr_reader :merchant, :merchant_user, :merchant_password
  8. # attr_reader :action
  9. TRANSACTION_TYPES = [
  10. 'PREAUTH', # Pre Authorization
  11. 'SALE', # Charge
  12. 'VOID', # Cancel
  13. 'REFUND', # Refund
  14. 'QUERYPAYMENTSYSTEMS', # Cards
  15. 'QUERYBIN', # Bin
  16. 'EWALLETDELETECARD', # Delete Card
  17. 'CURRENCYEXCHANGE', # Currency Exchange
  18. ]
  19. PROTOCOL_VERSIONS = [
  20. '2.0'
  21. ]
  22. CURRENCIES = [
  23. 'USD', 'GBP', 'EUR', 'TRY',
  24. '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'
  25. ]
  26. def initialize(language: 'EN',
  27. protocol_version: '2.0',
  28. time: DateTime.now)
  29. @language = language.to_s.upcase
  30. @protocol_version = validate(protocol_version.to_s, of: PROTOCOL_VERSIONS)
  31. @time = time
  32. @merchant = Paratika.config.merchant_id
  33. @merchant_user = Paratika.config.merchant_user
  34. @merchant_password = Paratika.config.merchant_password
  35. end
  36. def decorate
  37. self.as_json.except!('language', 'protocol_version', 'time')
  38. .merge!({ "action" => self.class.name.demodulize.upcase })
  39. .transform_keys!{ |k| k.gsub('_', '').upcase }
  40. end
  41. def data
  42. self.as_json.merge!({"action" => self.class.name.demodulize.upcase})
  43. end
  44. def send
  45. Paratika::Response.new Net::HTTP.post_form(self.url, self.decorate)
  46. end
  47. private
  48. def url
  49. URI("#{Paratika.config.url}/paratika/api/v2")
  50. end
  51. def validate(value, of:)
  52. unless of.include?(value)
  53. raise ArgumentError, "Expected one of #{of.inspect}, got: #{value.inspect}"
  54. end
  55. value
  56. end
  57. def validate_money(value)
  58. unless value.is_a?(BigDecimal) && value >= 0.01 && value.scale <= 2
  59. raise ArgumentError, "Expected BigDecimal value with max scale 2, got: #{value.inspect}"
  60. end
  61. "%.2f" % value
  62. end
  63. def validate_integer(value)
  64. if !value.is_a? Integer
  65. raise ArgumentError, "Value must be an Integer, got: #{value.inspect}"
  66. end
  67. value
  68. end
  69. def validate_presence(value)
  70. if !value.is_a?(Numeric) && value.blank?
  71. raise ArgumentError, "Expected not blank or nil value, got: #{value.inspect}"
  72. end
  73. value
  74. end
  75. def validate_hash(value)
  76. if !value.is_a? Hash
  77. raise ArgumentError, "Expected Hash, got: #{value.inspect}"
  78. end
  79. value
  80. end
  81. end
  82. end