class Newebpay::QueryTrade::Search

Constants

REQUIRED_ATTRS

Attributes

attrs[RW]
merchant_id[RW]
response[RW]

Public Class Methods

new(options) click to toggle source
# File lib/newebpay/query_trade/search.rb, line 8
def initialize(options)
  check_valid(options)
  @attrs = {}
  @merchant_id = options[:merchant_id] || Newebpay.config.merchant_id
  parse_attr(options)

  @attrs['Version'] = version
  @attrs['TimeStamp'] = Time.now.to_i
  @attrs['RespondType'] = 'JSON'
  @attrs['CheckValue'] = check_value

  result = HTTP.post(Newebpay.config.query_trade_url, form: attrs).body.to_s
  @response = Response.new(result)
end

Public Instance Methods

check_value() click to toggle source
# File lib/newebpay/query_trade/search.rb, line 23
def check_value
  @check_value ||= Newebpay::NewebpayHelper.create_check_value(check_value_raw)
end
check_value_raw() click to toggle source
# File lib/newebpay/query_trade/search.rb, line 27
def check_value_raw
  URI.encode_www_form(attrs.slice('Amt', 'MerchantID', 'MerchantOrderNo').sort)
end
version() click to toggle source
# File lib/newebpay/query_trade/search.rb, line 31
def version
  '1.1'
end

Private Instance Methods

check_valid(options) click to toggle source
# File lib/newebpay/query_trade/search.rb, line 43
def check_valid(options)
  unless options.is_a? Hash
    raise ArgumentError, "When initializing #{self.class.name}, you must pass a hash as an argument."
  end

  REQUIRED_ATTRS.each do |argument|
    raise ArgumentError, "Missing required argument: #{argument}." unless options[argument]
  end
end
parse_attr(options) click to toggle source
# File lib/newebpay/query_trade/search.rb, line 37
def parse_attr(options)
  attrs['MerchantID'] = merchant_id
  attrs['MerchantOrderNo'] = options[:order_number]
  attrs['Amt'] = options[:price]
end