request.rb 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. 'QUERYCARD', # Cards
  15. 'EWALLETDELETECARD', # Delete Card
  16. 'CURRENCYEXCHANGE', # Currency Exchange
  17. ]
  18. PROTOCOL_VERSIONS = [
  19. '2.0'
  20. ]
  21. CURRENCIES = [
  22. 'USD', 'GBP', 'EUR', 'TRY',
  23. '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'
  24. ]
  25. def initialize(language: 'EN',
  26. protocol_version: '2.0',
  27. time: DateTime.now)
  28. @language = language.to_s.upcase
  29. @protocol_version = validate(protocol_version.to_s, of: PROTOCOL_VERSIONS)
  30. @time = time
  31. @merchant = Paratika.config.merchant_id
  32. @merchant_user = Paratika.config.merchant_user
  33. @merchant_password = Paratika.config.merchant_password
  34. end
  35. def decorate
  36. self.as_json.except!('language', 'protocol_version', 'time')
  37. .merge!({ "action" => self.class.name.demodulize.upcase })
  38. .transform_keys!{ |k| k.gsub('_', '').upcase }
  39. end
  40. def send
  41. Paratika::Response.new Net::HTTP.post_form(self.url, self.decorate)
  42. end
  43. private
  44. def url
  45. URI("#{Paratika.config.url}/paratika/api/v2")
  46. end
  47. def validate(value, of:)
  48. unless of.include?(value)
  49. raise ArgumentError, "Expected one of #{of.inspect}, got: #{value.inspect}"
  50. end
  51. value
  52. end
  53. def validate_money(value)
  54. unless value.is_a?(BigDecimal) && value >= 0.01 && value.scale <= 2
  55. raise ArgumentError, "Expected BigDecimal value with max scale 2, got: #{value.inspect}"
  56. end
  57. "%.2f" % value
  58. end
  59. def validate_integer(value)
  60. if !value.is_a? Integer
  61. raise ArgumentError, "Value must be an Integer, got: #{value.inspect}"
  62. end
  63. value
  64. end
  65. def validate_presence(value)
  66. if !value.is_a?(Numeric) && value.blank?
  67. raise ArgumentError, "Expected not blank or nil value, got: #{value.inspect}"
  68. end
  69. value
  70. end
  71. def validate_hash(value)
  72. if !value.is_a? Hash
  73. raise ArgumentError, "Expected Hash, got: #{value.inspect}"
  74. end
  75. value
  76. end
  77. end
  78. end