Class: Ecpay::Client
- Inherits:
-
Object
- Object
- Ecpay::Client
- Defined in:
- lib/ecpay/client.rb
Overview
:nodoc:
Constant Summary collapse
- PRODUCTION_API_HOST =
'https://payment.ecpay.com.tw'
- TEST_API_HOST =
'https://payment-stage.ecpay.com.tw'
- TEST_OPTIONS =
{ merchant_id: '2000132', hash_key: '5294y06JbISpM5x9', hash_iv: 'v77hoKGq4kWxNNIS' }.freeze
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
- #api_host ⇒ Object
- #generate_checkout_params(overwrite_params = {}) ⇒ Object
- #generate_params(overwrite_params = {}) ⇒ Object
-
#initialize(options = {}) ⇒ Client
constructor
A new instance of Client.
- #make_mac(params = {}) ⇒ Object
- #query_credit_card_period_info(merchant_trade_number) ⇒ Object
- #query_trade_info(merchant_trade_number, platform = nil) ⇒ Object
- #request(path, params = {}) ⇒ Object
- #verify_mac(params = {}) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Client
Returns a new instance of Client.
21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/ecpay/client.rb', line 21 def initialize( = {}) @options = { mode: :production }.merge!() case @options[:mode] when :production option_required! :merchant_id, :hash_key, :hash_iv when :test @options = TEST_OPTIONS.merge() else raise InvalidMode, %(option :mode is either :test or :production) end @options.freeze end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
19 20 21 |
# File 'lib/ecpay/client.rb', line 19 def @options end |
Instance Method Details
#api_host ⇒ Object
34 35 36 37 38 39 |
# File 'lib/ecpay/client.rb', line 34 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 = {}) ⇒ Object
61 62 63 64 65 66 67 68 |
# File 'lib/ecpay/client.rb', line 61 def generate_checkout_params(overwrite_params = {}) generate_params({ MerchantTradeNo: SecureRandom.hex(4), MerchantTradeDate: Time.now.strftime('%Y/%m/%d %H:%M:%S'), PaymentType: 'aio', EncryptType: 1 }.merge!(overwrite_params)) end |
#generate_params(overwrite_params = {}) ⇒ Object
54 55 56 57 58 59 |
# File 'lib/ecpay/client.rb', line 54 def generate_params(overwrite_params = {}) result = overwrite_params.clone result[:MerchantID] = @options[:merchant_id] result[:CheckMacValue] = make_mac(result) result end |
#make_mac(params = {}) ⇒ Object
41 42 43 44 45 46 |
# File 'lib/ecpay/client.rb', line 41 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::SHA256.hexdigest(url_encoded).upcase! end |
#query_credit_card_period_info(merchant_trade_number) ⇒ Object
87 88 89 90 91 92 93 94 95 |
# File 'lib/ecpay/client.rb', line 87 def query_credit_card_period_info(merchant_trade_number) res = request( '/Cashier/QueryCreditCardPeriodInfo', MerchantTradeNo: merchant_trade_number, TimeStamp: Time.now.to_i ) JSON.parse(res.body) end |
#query_trade_info(merchant_trade_number, platform = nil) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/ecpay/client.rb', line 75 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/V2', params) Hash[res.body.split('&').map! { |i| i.split('=') }] end |
#request(path, params = {}) ⇒ Object
70 71 72 73 |
# File 'lib/ecpay/client.rb', line 70 def request(path, params = {}) api_url = URI.join(api_host, path) Net::HTTP.post_form api_url, generate_params(params) end |
#verify_mac(params = {}) ⇒ Object
48 49 50 51 52 |
# File 'lib/ecpay/client.rb', line 48 def verify_mac(params = {}) stringified_keys = params.stringify_keys check_mac_value = stringified_keys.delete('CheckMacValue') make_mac(stringified_keys) == check_mac_value end |