Class: Pay2go::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/pay2go/client.rb

Constant Summary collapse

TRANSACTION_API_ENDPOINTS =
{
  test: 'https://capi.pay2go.com/API/QueryTradeInfo',
  production: 'https://api.pay2go.com/API/QueryTradeInfo'
}.freeze
CREDITCARD_COLLECT_REFUND_API_ENDPOINTS =
{
  test: 'https://cweb.pay2go.com/API/CreditCard/Close',
  production: 'https://web.pay2go.com/API/CreditCard/Close'
}.freeze
CREDITCARD_DEAUTHORIZE_API_ENDPOINTS =
{
  test: 'https://cweb.pay2go.com/API/CreditCard/Cancel',
  production: 'https://web.pay2go.com/API/CreditCard/Cancel'
}.freeze
NEED_CHECK_VALUE_APIS =
[
  :query_trade_info # Transaction API
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/pay2go/client.rb', line 27

def initialize options = {}
  @options = { mode: :production }.merge!(options)

  case @options[:mode]
  when :test, :production
    option_required! :merchant_id, :hash_key, :hash_iv
  else
    raise InvalidMode, %Q{option :mode is either :test or :production}
  end

  @options.freeze
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



25
26
27
# File 'lib/pay2go/client.rb', line 25

def options
  @options
end

Instance Method Details

#credit_card_collect_refund(params = {}) ⇒ Object

Raises:



108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/pay2go/client.rb', line 108

def credit_card_collect_refund params = {}
  param_required! params, [:Amt, :IndexType, :CloseType]

  raise MissingOption, %Q{One of the following param is required: MerchantOrderNo, TradeNo} if params[:MerchantOrderNo].nil? and params[:TradeNo].nil?

  post_params = {
    RespondType: 'String',
    Version: '1.0',
    TimeStamp: Time.now.to_i
  }.merge!(params)

  res = request :credit_card_collect_refund, post_params
  Hash[res.body.split('&').map!{|i| URI::decode(i.force_encoding('ASCII-8BIT').force_encoding('UTF-8')).split('=')}]
end

#credit_card_collect_refund_by_merchant_order_no(params = {}) ⇒ Object



123
124
125
126
127
128
129
130
131
# File 'lib/pay2go/client.rb', line 123

def credit_card_collect_refund_by_merchant_order_no params = {}
  param_required! params, [:Amt, :MerchantOrderNo, :CloseType]

  post_params = {
    IndexType: 1
  }.merge!(params)

  credit_card_collect_refund post_params
end

#credit_card_collect_refund_by_trade_no(params = {}) ⇒ Object



133
134
135
136
137
138
139
140
141
# File 'lib/pay2go/client.rb', line 133

def credit_card_collect_refund_by_trade_no params = {}
  param_required! params, [:Amt, :TradeNo, :CloseType]

  post_params = {
    IndexType: 1
  }.merge!(params)

  credit_card_collect_refund post_params
end

#credit_card_deauthorize(params = {}) ⇒ Object

Raises:



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/pay2go/client.rb', line 71

def credit_card_deauthorize params = {}
  param_required! params, [:Amt, :IndexType]

  raise MissingOption, %Q{One of the following param is required: MerchantOrderNo, TradeNo} if params[:MerchantOrderNo].nil? and params[:TradeNo].nil?

  post_params = {
    RespondType: 'String',
    Version: '1.0',
    TimeStamp: Time.now.to_i
  }.merge!(params)

  post_params.delete_if { |key, value| value.nil? }

  res = request :credit_card_deauthorize, post_params
  Hash[res.body.split('&').map!{|i| URI::decode(i.force_encoding('ASCII-8BIT').force_encoding('UTF-8')).split('=')}]
end

#credit_card_deauthorize_by_merchant_order_no(params = {}) ⇒ Object



88
89
90
91
92
93
94
95
96
# File 'lib/pay2go/client.rb', line 88

def credit_card_deauthorize_by_merchant_order_no params = {}
  param_required! params, [:Amt, :MerchantOrderNo]

  post_params = {
    IndexType: 1
  }.merge!(params)

  credit_card_deauthorize post_params
end

#credit_card_deauthorize_by_trade_no(params = {}) ⇒ Object



98
99
100
101
102
103
104
105
106
# File 'lib/pay2go/client.rb', line 98

def credit_card_deauthorize_by_trade_no params = {}
  param_required! params, [:Amt, :TradeNo]

  post_params = {
    IndexType: 2
  }.merge!(params)

  credit_card_deauthorize post_params
end

#encode_post_data(data) ⇒ Object



178
179
180
181
182
183
184
185
186
187
# File 'lib/pay2go/client.rb', line 178

def encode_post_data data
  cipher = OpenSSL::Cipher::AES256.new(:CBC)
  cipher.encrypt
  cipher.padding = 0
  cipher.key = @options[:hash_key]
  cipher.iv = @options[:hash_iv]
  data = add_padding(data)
  encrypted = cipher.update(data) + cipher.final
  encrypted.unpack('H*').first
end

#generate_credit_card_period_params(params = {}) ⇒ Object



143
144
145
146
147
148
149
150
151
# File 'lib/pay2go/client.rb', line 143

def generate_credit_card_period_params params = {}
  param_required! params, [:MerchantOrderNo, :ProdDesc, :PeriodAmt, :PeriodAmtMode, :PeriodType, :PeriodPoint, :PeriodStartType, :PeriodTimes]

  generate_params(:credit_card_period, {
    RespondType: 'String',
    TimeStamp: Time.now.to_i,
    Version: '1.0'
  }.merge!(params))
end

#generate_mpg_params(params = {}) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/pay2go/client.rb', line 46

def generate_mpg_params params = {}
  param_required! params, [:MerchantOrderNo, :Amt, :ItemDesc, :Email, :LoginType]

  post_params = {
    RespondType: 'String',
    TimeStamp: Time.now.to_i,
    Version: '1.2'
  }.merge!(params)

  generate_params(:mpg, post_params)
end

#make_check_value(type, params = {}) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/pay2go/client.rb', line 153

def make_check_value type, params = {}
  case type
  when :mpg
    check_value_fields = [:Amt, :MerchantID, :MerchantOrderNo, :TimeStamp, :Version]
    padded = "HashKey=#{@options[:hash_key]}&%s&HashIV=#{@options[:hash_iv]}"
  when :query_trade_info
    check_value_fields = [:Amt, :MerchantID, :MerchantOrderNo]
    padded = "IV=#{@options[:hash_iv]}&%s&Key=#{@options[:hash_key]}"
  when :credit_card_period
    check_value_fields = [:MerchantID, :MerchantOrderNo, :PeriodAmt, :PeriodType, :TimeStamp]
    padded = "HashKey=#{@options[:hash_key]}&%s&HashIV=#{@options[:hash_iv]}"
  else
    raise UnsupportedType, "Unsupported API type."
  end

  param_required! params, check_value_fields

  raw = params.select { |key, value| key.to_s.match(/^(#{check_value_fields.join('|')})$/) }
    .sort_by{ |k, v| k.downcase }.map!{ |k, v| "#{k}=#{v}" }.join('&')

  padded = padded % raw

  Digest::SHA256.hexdigest(padded).upcase!
end

#query_trade_info(params = {}) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/pay2go/client.rb', line 58

def query_trade_info params = {}
  param_required! params, [:MerchantOrderNo, :Amt]

  post_params = {
    Version: '1.1',
    RespondType: 'String',
    TimeStamp: Time.now.to_i
  }.merge!(params)

  res = request :query_trade_info, post_params
  Hash[res.body.split('&').map!{|i| URI::decode(i).split('=')}]
end

#verify_check_code(params = {}) ⇒ Object



40
41
42
43
44
# File 'lib/pay2go/client.rb', line 40

def verify_check_code params = {}
  stringified_keys = params.stringify_keys
  check_code = stringified_keys.delete('CheckCode')
  make_check_code(stringified_keys) == check_code
end