lib/alipay/service.rb in alipay-0.3.1 vs lib/alipay/service.rb in alipay-0.4.0

- old
+ new

@@ -112,23 +112,36 @@ "#{GATEWAY_URL}?#{query_string(options)}" end CLOSE_TRADE_REQUIRED_OPTIONS = %w( service partner _input_charset) + CLOSE_TRADE_REQUIRED_OPTIONAL_OPTIONS = %w( trade_no out_order_no ) def self.close_trade(options) options = { 'service' => 'close_trade', '_input_charset' => 'utf-8', 'partner' => Alipay.pid }.merge(Utils.stringify_keys(options)) check_required_options(options, CLOSE_TRADE_REQUIRED_OPTIONS) + check_optional_options(options, CLOSE_TRADE_REQUIRED_OPTIONAL_OPTIONS) - if options['trade_no'].nil? and options['out_order_no'].nil? - warn("Ailpay Warn: must specify either trade_no or out_order_no") - end + open("#{GATEWAY_URL}?#{query_string(options)}").read + end + SINGLE_TRADE_QUERY_OPTIONS = %w( service partner _input_charset) + SINGLE_TRADE_QUERY_OPTIONAL_OPTIONS = %w( trade_no out_order_no ) + def self.single_trade_query(options) + options = { + "service" => 'single_trade_query', + "_input_charset" => "utf-8", + "partner" => Alipay.pid, + }.merge(Utils.stringify_keys(options)) + + check_required_options(options, SINGLE_TRADE_QUERY_OPTIONS) + check_optional_options(options, SINGLE_TRADE_QUERY_OPTIONAL_OPTIONS) + open("#{GATEWAY_URL}?#{query_string(options)}").read end def self.query_string(options) options.merge('sign_type' => 'MD5', 'sign' => Alipay::Sign.generate(options)).map do |key, value| @@ -138,8 +151,12 @@ def self.check_required_options(options, names) names.each do |name| warn("Ailpay Warn: missing required option: #{name}") unless options.has_key?(name) end + end + + def self.check_optional_options(options, names) + warn("Ailpay Warn: must specify either #{names.join(' or ')}") if names.all? {|name| options[name].nil? } end end end