class Allpay::Client
Constants
- PRODUCTION_API_HOST
- TEST_API_HOST
- TEST_OPTIONS
Attributes
options[R]
Public Class Methods
new(options = {})
click to toggle source
# File lib/allpay/client.rb, line 20 def initialize options = {} @options = {mode: :production}.merge!(options) case @options[:mode] when :production option_required! :merchant_id, :hash_key, :hash_iv when :test @options = TEST_OPTIONS.merge(options) else raise InvalidMode, %Q{option :mode is either :test or :production} end @options.freeze end
Public Instance Methods
api_host()
click to toggle source
# File lib/allpay/client.rb, line 33 def api_host case @options[:mode] when :production then PRODUCTION_API_HOST when :test then TEST_API_HOST end end
generate_checkout_params(overwrite_params = {})
click to toggle source
# File lib/allpay/client.rb, line 60 def generate_checkout_params overwrite_params = {} generate_params({ MerchantTradeDate: Time.now.strftime('%Y/%m/%d %H:%M:%S'), MerchantTradeNo: SecureRandom.hex(4), PaymentType: 'aio' }.merge!(overwrite_params)) end
generate_params(overwrite_params = {})
click to toggle source
# File lib/allpay/client.rb, line 53 def generate_params overwrite_params = {} result = overwrite_params.clone result[:MerchantID] = @options[:merchant_id] result[:CheckMacValue] = make_mac(result) result end
make_mac(params = {})
click to toggle source
# File lib/allpay/client.rb, line 40 def make_mac params = {} raw = params.sort_by{|k,v|k.downcase}.map!{|k,v| "#{k}=#{v}"}.join('&') padded = "HashKey=#{@options[:hash_key]}&#{raw}&HashIV=#{@options[:hash_iv]}" url_encoded = CGI.escape(padded).downcase! Digest::MD5.hexdigest(url_encoded).upcase! end
query_period_credit_card_trade_info(merchant_trade_number)
click to toggle source
# File lib/allpay/client.rb, line 84 def query_period_credit_card_trade_info merchant_trade_number res = request '/Cashier/QueryPeriodCreditCardTradeInfo', MerchantTradeNo: merchant_trade_number, TimeStamp: Time.now.to_i JSON.parse(res.body) end
query_trade_info(merchant_trade_number, platform = nil)
click to toggle source
# File lib/allpay/client.rb, line 73 def query_trade_info merchant_trade_number, platform = nil params = { MerchantTradeNo: merchant_trade_number, TimeStamp: Time.now.to_i, PlatformID: platform } params.delete_if{ |k, v| v.nil? } res = request '/Cashier/QueryTradeInfo', params Hash[res.body.split('&').map!{|i| i.split('=')}] end
request(path, params = {})
click to toggle source
# File lib/allpay/client.rb, line 68 def request path, params = {} api_url = URI.join(api_host, path) Net::HTTP.post_form api_url, generate_params(params) end
verify_mac(params = {})
click to toggle source
# File lib/allpay/client.rb, line 47 def verify_mac params = {} stringified_keys = params.stringify_keys check_mac_value = stringified_keys.delete('CheckMacValue') make_mac(stringified_keys) == check_mac_value end
Private Instance Methods
option_required!(*option_names)
click to toggle source
# File lib/allpay/client.rb, line 93 def option_required! *option_names option_names.each do |option_name| raise MissingOption, %Q{option "#{option_name}" is required.} if @options[option_name].nil? end end