class Newebpay::MPG::Form

Constants

PAYMENT_METHODS
REQUIRED_ATTRS

Attributes

attrs[RW]
merchant_id[RW]

Public Class Methods

new(options) click to toggle source
# File lib/newebpay/mpg/form.rb, line 8
def initialize(options)
  check_valid(options)
  @attrs = {}
  @merchant_id = options[:merchant_id] || Newebpay.config.merchant_id
  parse_attr(options)

  @attrs['Version'] = version
  @attrs['TimeStamp'] = Time.now.to_i
  @attrs['RespondType'] = 'JSON'
end

Public Instance Methods

encode_url_params() click to toggle source
# File lib/newebpay/mpg/form.rb, line 36
def encode_url_params
  URI.encode_www_form(attrs)
end
form_attrs() click to toggle source
# File lib/newebpay/mpg/form.rb, line 19
def form_attrs
  @form_attrs ||= {
    MerchantID: merchant_id,
    TradeInfo: trade_info,
    TradeSha: trade_sha,
    Version: version
  }
end
trade_info() click to toggle source
# File lib/newebpay/mpg/form.rb, line 28
def trade_info
  @trade_info ||= Newebpay::NewebpayHelper.encrypt_data(encode_url_params)
end
trade_sha() click to toggle source
# File lib/newebpay/mpg/form.rb, line 32
def trade_sha
  @trade_sha ||= Newebpay::NewebpayHelper.sha256_encode_trade_info(trade_info)
end
version() click to toggle source
# File lib/newebpay/mpg/form.rb, line 40
def version
  '1.5'
end

Private Instance Methods

check_valid(options) click to toggle source
# File lib/newebpay/mpg/form.rb, line 74
def check_valid(options)
  unless options.is_a? Hash
    raise ArgumentError, "When initializing #{self.class.name}, you must pass a hash."
  end

  REQUIRED_ATTRS.each do |argument|
    raise ArgumentError, "Missing required argument: #{argument}." unless options[argument]
  end

  unless options[:payment_methods].is_a? Array
    raise ArgumentError, 'payment_methods must be an Array'
  end

  if (options[:payment_methods] - PAYMENT_METHODS).any?
    raise ArgumentError, 'Invalid payment method'
  end
end
parse_attr(options) click to toggle source
# File lib/newebpay/mpg/form.rb, line 46
def parse_attr(options)
  attrs[:MerchantID] = merchant_id
  attrs[:MerchantOrderNo] = options[:order_number]
  attrs[:ItemDesc] = options[:description]
  attrs[:Amt] = options[:price]
  attrs[:Email] = options[:email]
  attrs[:CVSCOM] = options[:cvscom]
  attrs[:LoginType] = options[:login_required] ? '1' : '0'
  attrs[:LangType] = options[:locale] || 'zh-tw'
  attrs[:TradeLimit] = options[:trade_limit]
  attrs[:ExpireDate] = options[:expire_date]
  attrs[:ClientBackURL] = options[:cancel_url]
  attrs[:OrderComment] = options[:comment]
  attrs[:EmailModify] = options[:email_editable] ? '1' : '0'
  attrs[:InstFlag] = options[:inst_flag]
  attrs[:ReturnURL] = Newebpay::Engine.routes.url_helpers.mpg_callbacks_url(host: Newebpay.host)
  attrs[:CustomerURL] = Newebpay::Engine.routes.url_helpers.payment_code_callbacks_url(host: Newebpay.host) if Newebpay.config.payment_code_callback
  attrs[:NotifyURL] = Newebpay::Engine.routes.url_helpers.notify_callbacks_url(host: Newebpay.host) if Newebpay.config.notify_callback

  options[:payment_methods].each do |payment_method|
    if payment_method == :credit_red
      attrs[:CreditRed] = '1'
    else
      attrs[payment_method.upcase] = '1'
    end
  end
end