class Puerta::Nl::Checkout

Constants

BANK_CODES
MODE
TYPES
VERSION

Attributes

result[RW]
type[RW]

Public Class Methods

new(type, options={}) click to toggle source

@type(string), @option(Hash{:merchant_id, :merchant_password, :receiver_email, :cur_code})

# File lib/puerta/nl/checkout.rb, line 54
def initialize(type, options={})
  raise "Invalid type: #{type} in #{Checkout::TYPES}" if !Checkout::TYPES.include?(type)

  @type    = type
  @options = options
end
payment_types() click to toggle source
# File lib/puerta/nl/checkout.rb, line 77
def self.payment_types
  result = {}
  TYPES.each do |payment_type|
    result[payment_type] = Checkout::BANK_CODES.reject {|k,v|  !v[:type].include?(payment_type)}
  end
  result
end

Public Instance Methods

call(card_options) click to toggle source

<< paymennt_options(card_options)

# File lib/puerta/nl/checkout.rb, line 164
def call(card_options)
  params = payment_options(card_options)

  connection = Faraday::Connection.new host, ssl: { verify: false }

  response = connection.post do |req|
    req.url endpoint
    req.body = params
  end

  docs= parse_xml(response.body)
  error_code = docs.at("//error_code").text

  @result = {
    token: docs.at('//token').text,
    error_code: error_code,
    error_message: error(error_code.to_s),
    time_limit: docs.at("//time_limit").text,
    description: docs.at("//description").text,
    checkout_url: docs.at("//checkout_url").text,
  }
  @result
end
endpoint() click to toggle source
# File lib/puerta/nl/checkout.rb, line 69
def endpoint
  if Puerta.config.sandbox?
    Checkout::MODE[:sandbox][:endpoint]
  elsif Puerta.config.production?
    Checkout::MODE[:production][:endpoint]
  end
end
error(error_code) click to toggle source

@error_code(string)

# File lib/puerta/nl/checkout.rb, line 194
def error(error_code)
  errors = {
                          '00' => 'Thành công',
                          '99' => 'Lỗi chưa xác minh',
                          '06' => 'Mã merchant không tồn tại hoặc bị khóa',
                          '02' => 'Địa chỉ IP truy cập bị từ chối',
                          '03' => 'Mã checksum không chính xác, truy cập bị từ chối',
                          '04' => 'Tên hàm API do merchant gọi tới không hợp lệ (không tồn tại)',
                          '05' => 'Sai version của API',
                          '07' => 'Sai mật khẩu của merchant',
                          '08' => 'Địa chỉ email tài khoản nhận tiền không tồn tại',
                          '09' => 'Tài khoản nhận tiền đang bị phong tỏa giao dịch',
                          '10' => 'Mã đơn hàng không hợp lệ',
                          '11' => 'Số tiền giao dịch lớn hơn hoặc nhỏ hơn quy định',
                          '12' => 'Loại tiền tệ không hợp lệ',
                          '29' => 'Token không tồn tại',
                          '80' => 'Không thêm được đơn hàng',
                          '81' => 'Đơn hàng chưa được thanh toán',
                          '110' => 'Địa chỉ email tài khoản nhận tiền không phải email chính',
                          '111' => 'Tài khoản nhận tiền đang bị khóa',
                          '113' => 'Tài khoản nhận tiền chưa cấu hình là người bán nội dung số',
                          '114' => 'Giao dịch đang thực hiện, chưa kết thúc',
                          '115' => 'Giao dịch bị hủy',
                          '118' => 'tax_amount không hợp lệ',
                          '119' => 'discount_amount không hợp lệ',
                          '120' => 'fee_shipping không hợp lệ',
                          '121' => 'return_url không hợp lệ',
                          '122' => 'cancel_url không hợp lệ',
                          '123' => 'items không hợp lệ',
                          '124' => 'transaction_info không hợp lệ',
                          '125' => 'quantity không hợp lệ',
                          '126' => 'order_description không hợp lệ',
                          '127' => 'affiliate_code không hợp lệ',
                          '128' => 'time_limit không hợp lệ',
                          '129' => 'buyer_fullname không hợp lệ',
                          '130' => 'buyer_email không hợp lệ',
                          '131' => 'buyer_mobile không hợp lệ',
                          '132' => 'buyer_address không hợp lệ',
                          '133' => 'total_item không hợp lệ',
                          '134' => 'payment_method, bank_code không hợp lệ',
                          '135' => 'Lỗi kết nối tới hệ thống ngân hàng',
                          '140' => 'Đơn hàng không hỗ trợ thanh toán trả góp'
  }
  errors[error_code]
end
error_code() click to toggle source
# File lib/puerta/nl/checkout.rb, line 125
def error_code
  @result[:error_code]
end
error_message() click to toggle source
# File lib/puerta/nl/checkout.rb, line 129
def error_message
  error(@result[:error_code] )
end
get_transaction_detail(token) click to toggle source

@token(string)

# File lib/puerta/nl/checkout.rb, line 90
def get_transaction_detail(token)
  params = {
    merchant_id: @options[:merchant_id],
    merchant_password: Digest::MD5.hexdigest(@options[:merchant_password]),
    version: VERSION,
    function: 'GetTransactionDetail',
    token:token
  }

  connection = Faraday::Connection.new host, ssl: { verify: false }

  response = connection.post do |req|
    req.url endpoint
    req.body = params
  end

  docs = parse_xml(response.body)

  error_code = docs.at('//error_code').text

  @result = {
    error_code: error_code,
    error_message: error(error_code.to_s),
    token: docs.at('//token').text,
    transaction_id: docs.at('//token').text,
    transaction_status: docs.at('//transaction_status').text,
    bank_code: docs.at('//bank_code').text,
  }
  @result
end
host() click to toggle source
# File lib/puerta/nl/checkout.rb, line 61
def host
  if Puerta.config.sandbox?
    Checkout::MODE[:sandbox][:host]
  elsif Puerta.config.production?
    Checkout::MODE[:production][:host]
  end
end
ok?() click to toggle source
# File lib/puerta/nl/checkout.rb, line 121
def ok?
  @result && @result[:error_code] == '00'
end
parse_xml(xml_string) click to toggle source

@xml_string(string)

# File lib/puerta/nl/checkout.rb, line 189
def parse_xml(xml_string)
   Nokogiri::XML(xml_string)
end
payment_methods() click to toggle source
# File lib/puerta/nl/checkout.rb, line 85
def payment_methods
  Checkout::BANK_CODES.reject {|k,v|  !v[:type].include?(@type)}
end
payment_options(card_options) click to toggle source

@card_options(Hash({:order_code, :total_amount, :payment_type, :order_description, :tax_amount, :fee_shipping, :discount_amount, :return_url, :cancel_url, :buyer_fullname, :buyer_email, :buyer_mobile, :buyer_address, :array_items, :bank_code OjO: bank_code: ( VISA, CREDIT_CARD_PREPAID, ATM_ONLINE, ATM_OFFLINE), no bank code: ( NH_OFFLINE, NL, IB_ONLINE )

# File lib/puerta/nl/checkout.rb, line 136
def payment_options(card_options)
  card_options[:array_items] ||= []

  params = {
    function: 'SetExpressCheckout',
    version: Checkout::VERSION,
    payment_method: @type,
    merchant_password: Digest::MD5.hexdigest(@options[:merchant_password]),
    total_item: card_options[:array_items].count
  }


  params = params.merge(@options.select{|k,v| [:cur_code, :receiver_email, :merchant_id].include?(k)})
  params = params.merge(card_options.select{|k,v| k != :array_items } )

  sending_params = params.reject{|k,v| v == nil || v == ''}

  if(card_options[:array_items] && card_options[:array_items].count > 0)
    card_options[:array_items].each do |item|
      item.each do |key, value|
        sending_params[key] = value
      end
    end
  end
  sending_params
end