class Clt::CreditCard

Public Class Methods

new() click to toggle source
# File lib/clt/credit_card.rb, line 5
def initialize
  raise ArgumentError, 'Please setup cocs_link_id first' if Clt.cocs_link_id.nil?
  ErrorMessage.raise_error(msg: :parameter_should_be, field: :cocs_link_id, data: :String) unless Clt.cocs_link_id.is_a? String
  ErrorMessage.raise_error(msg: :cannot_be_empty, field: :cocs_link_id) if Clt.cocs_link_id.empty?

  raise ArgumentError, 'Please setup cocs_hash_base first' if Clt.cocs_hash_base.nil?
  ErrorMessage.raise_error(msg: :parameter_should_be, field: :cocs_hash_base, data: :String) unless Clt.cocs_hash_base.is_a? String
  ErrorMessage.raise_error(msg: :cannot_be_empty, field: :cocs_hash_base) if Clt.cocs_hash_base.empty?
end

Public Instance Methods

auth_fail_callback_valid?(params) click to toggle source

Authorize fail callback validation

# File lib/clt/credit_card.rb, line 155
def auth_fail_callback_valid?(params)
  params = params.stringify_keys
  checksum = params['chk']
  checksum_generated = generate_checksum "#{Clt.cocs_hash_base}$#{params['order_amount']}$#{params['send_time']}$#{params['ret']}$#{params['notify_time']}$#{params['cust_order_no']}"

  checksum == checksum_generated
end
auth_success_callback_valid?(params) click to toggle source

Authorize success callback validation

# File lib/clt/credit_card.rb, line 146
def auth_success_callback_valid?(params)
  params = params.stringify_keys
  checksum = params['chk']
  checksum_generated = generate_checksum "#{Clt.cocs_hash_base}$#{params['order_amount']}$#{params['send_time']}$#{params['ret']}$#{params['acquire_time']}$#{params['auth_code']}$#{params['card_no']}$#{params['notify_time']}$#{params['cust_order_no']}"

  checksum == checksum_generated
end
order_cancel(params = {}) click to toggle source

Order Cancel

# File lib/clt/credit_card.rb, line 68
def order_cancel(params = {})
  ErrorMessage.raise_error(msg: :parameter_should_be, field: :Parameter, data: :Hash) unless params.is_a? Hash

  ErrorMessage.raise_error(msg: :missing_parameter, field: :service_url) if params[:service_url].nil?
  ErrorMessage.raise_error(msg: :parameter_should_be, field: :service_url, data: :String) unless params[:service_url].is_a? String
  ErrorMessage.raise_error(msg: :cannot_be_empty, field: :service_url) if params[:service_url].empty?

  ErrorMessage.raise_error(msg: :parameter_should_be, field: :cust_order_no, data: :String) unless params[:cust_order_no].is_a? String
  ErrorMessage.raise_error(msg: :cannot_be_empty, field: :cust_order_no) if params[:cust_order_no].empty?

  ErrorMessage.raise_error(msg: :missing_parameter, field: :order_amount) if params[:order_amount].nil?
  ErrorMessage.raise_error(msg: :parameter_should_be, field: :order_amount, data: :Integer) unless params[:order_amount].is_a? Integer
  ErrorMessage.raise_error(msg: :parameter_should_be, field: :order_amount, data: 'greater than 0') if params[:order_amount] <= 0

  if params.has_key? :chk
    raise_error(msg: :parameter_should_be, field: :chk, data: :String) unless params[:chk].is_a? String
    raise_error(msg: :cannot_be_empty, field: :chk) if params[:chk].empty?
  end

  # get and converts time to local time
  current_time = Time.now.localtime('+08:00')

  data = {
    link_id: Clt.cocs_link_id,
    send_time: current_time.strftime('%F %T')
  }.merge(params).merge(return_type: 'json')

  unless params.has_key? :chk
    data[:chk] = generate_checksum "#{Clt.cocs_hash_base}$#{params[:cust_order_no]}$#{params[:order_amount]}$#{data[:send_time]}"
  end

  post_request(service_url: params[:service_url], data: data)
end
order_create(params = {}) click to toggle source

Order Create

# File lib/clt/credit_card.rb, line 16
def order_create(params = {})
  ErrorMessage.raise_error(msg: :parameter_should_be, field: :Parameter, data: :Hash) unless params.is_a? Hash

  ErrorMessage.raise_error(msg: :missing_parameter, field: :service_url) if params[:service_url].nil?
  ErrorMessage.raise_error(msg: :parameter_should_be, field: :service_url, data: :String) unless params[:service_url].is_a? String
  ErrorMessage.raise_error(msg: :cannot_be_empty, field: :service_url) if params[:service_url].empty?

  if params.has_key? :cust_order_no
    ErrorMessage.raise_error(msg: :parameter_should_be, field: :cust_order_no, data: :String) unless params[:cust_order_no].is_a? String
    ErrorMessage.raise_error(msg: :cannot_be_empty, field: :cust_order_no) if params[:cust_order_no].empty?
    ErrorMessage.raise_error(msg: :data_length_too_short, field: :cust_order_no) if params[:cust_order_no].size < 3
  end

  ErrorMessage.raise_error(msg: :missing_parameter, field: :order_amount) if params[:order_amount].nil?
  ErrorMessage.raise_error(msg: :parameter_should_be, field: :order_amount, data: :Integer) unless params[:order_amount].is_a? Integer
  ErrorMessage.raise_error(msg: :parameter_should_be, field: :order_amount, data: 'greater than 0') if params[:order_amount] <= 0

  ErrorMessage.raise_error(msg: :missing_parameter, field: :order_detail) if params[:order_detail].nil?
  ErrorMessage.raise_error(msg: :parameter_should_be, field: :order_detail, data: :String) unless params[:order_detail].is_a? String
  ErrorMessage.raise_error(msg: :cannot_be_empty, field: :order_detail) if params[:order_detail].empty?

  if params.has_key? :limit_product_id
    ErrorMessage.raise_error(msg: :parameter_should_be, field: :limit_product_id, data: :String) unless params[:limit_product_id].is_a? String
    ErrorMessage.raise_error(msg: :cannot_be_empty, field: :limit_product_id) if params[:limit_product_id].empty?
  end

  if params.has_key? :chk
    raise_error(msg: :parameter_should_be, field: :chk, data: :String) unless params[:chk].is_a? String
    raise_error(msg: :cannot_be_empty, field: :chk) if params[:chk].empty?
  end

  service_url = params[:service_url]
  params.delete :service_url

  # get and converts time to local time
  current_time = Time.now.localtime('+08:00')

  data = {
    link_id: Clt.cocs_link_id,
    cust_order_no: '',
    limit_product_id: 'esun.normal',
    send_time: current_time.strftime('%F %T')
  }.merge(params).merge(return_type: 'json')

  unless params.has_key? :chk
    data[:chk] = generate_checksum "#{Clt.cocs_hash_base}$#{params[:order_amount]}$#{data[:send_time]}"
  end

  post_request(service_url: service_url, data: data)
end
order_refund(params = {}) click to toggle source

Order Refund

# File lib/clt/credit_card.rb, line 103
def order_refund(params = {})
  ErrorMessage.raise_error(msg: :parameter_should_be, field: :Parameter, data: :Hash) unless params.is_a? Hash

  ErrorMessage.raise_error(msg: :missing_parameter, field: :service_url) if params[:service_url].nil?
  ErrorMessage.raise_error(msg: :parameter_should_be, field: :service_url, data: :String) unless params[:service_url].is_a? String
  ErrorMessage.raise_error(msg: :cannot_be_empty, field: :service_url) if params[:service_url].empty?

  ErrorMessage.raise_error(msg: :parameter_should_be, field: :cust_order_no, data: :String) unless params[:cust_order_no].is_a? String
  ErrorMessage.raise_error(msg: :cannot_be_empty, field: :cust_order_no) if params[:cust_order_no].empty?

  ErrorMessage.raise_error(msg: :missing_parameter, field: :order_amount) if params[:order_amount].nil?
  ErrorMessage.raise_error(msg: :parameter_should_be, field: :order_amount, data: :Integer) unless params[:order_amount].is_a? Integer
  ErrorMessage.raise_error(msg: :parameter_should_be, field: :order_amount, data: 'greater than 0') if params[:order_amount] <= 0

  ErrorMessage.raise_error(msg: :missing_parameter, field: :refund_amount) if params[:refund_amount].nil?
  ErrorMessage.raise_error(msg: :parameter_should_be, field: :refund_amount, data: :Integer) unless params[:refund_amount].is_a? Integer
  ErrorMessage.raise_error(msg: :parameter_should_be, field: :refund_amount, data: 'greater than 0') if params[:refund_amount] <= 0

  if params[:refund_amount] > params[:order_amount]
    ErrorMessage.raise_error(msg: :parameter_should_be, field: :refund_amount, data: 'less than or equal to order_amount')
  end

  if params.has_key? :chk
    raise_error(msg: :parameter_should_be, field: :chk, data: :String) unless params[:chk].is_a? String
    raise_error(msg: :cannot_be_empty, field: :chk) if params[:chk].empty?
  end

  # get and converts time to local time
  current_time = Time.now.localtime('+08:00')

  data = {
    link_id: Clt.cocs_link_id,
    send_time: current_time.strftime('%F %T')
  }.merge(params).merge(return_type: 'json')

  unless params.has_key? :chk
    data[:chk] = generate_checksum "#{Clt.cocs_hash_base}$#{params[:cust_order_no]}$#{params[:order_amount]}$#{params[:refund_amount]}$#{data[:send_time]}"
  end

  post_request(service_url: params[:service_url], data: data)
end

Private Instance Methods

post_request(service_url:, data:) click to toggle source
# File lib/clt/credit_card.rb, line 165
def post_request(service_url:, data:)
  api_url = URI.parse(service_url)
  response = Net::HTTP.post_form api_url, data

  case response
  when Net::HTTPOK
    begin
      result = JSON.parse(response.body)
    rescue
      result = { 'status' => 'ERROR', 'msg' => 'Response parsing failed', 'response' => response.body }
    end

    result
  when Net::HTTPClientError, Net::HTTPInternalServerError
    raise Net::HTTPError, response.message
  else
    raise Net::HTTPError, 'Unexpected HTTP response'
  end
end