class Newebpay::Periodical::Form

Constants

REQUIRED_ATTRS

Attributes

attrs[RW]
merchant_id[RW]

Public Class Methods

new(options) click to toggle source
# File lib/newebpay/periodical/form.rb, line 9
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/periodical/form.rb, line 31
def encode_url_params
  URI.encode_www_form(attrs)
end
form_attrs() click to toggle source
# File lib/newebpay/periodical/form.rb, line 20
def form_attrs
  @form_attrs ||= {
    MerchantID_: merchant_id,
    PostData_: trade_info
  }
end
trade_info() click to toggle source
# File lib/newebpay/periodical/form.rb, line 27
def trade_info
  @trade_info ||= Newebpay::NewebpayHelper.encrypt_data(encode_url_params)
end
version() click to toggle source
# File lib/newebpay/periodical/form.rb, line 35
def version
  '1.0'
end

Private Instance Methods

check_valid(options) click to toggle source
# File lib/newebpay/periodical/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
end
parse_attr(options) click to toggle source
# File lib/newebpay/periodical/form.rb, line 41
def parse_attr(options)
  attrs[:MerOrderNo] = options[:order_number]
  attrs[:ProdDesc] = options[:description]
  attrs[:PeriodAmt] = options[:price]
  attrs[:PayerEmail] = options[:email]
  attrs[:PeriodPoint] = options[:period_point] || '01'
  attrs[:PeriodTimes] = options[:period_times] || '99'
  attrs[:PeriodStartType] = options[:check_type] || '1'
  attrs[:MerchantID] = merchant_id
  attrs[:PeriodMemo] = options[:comment]
  attrs[:PaymentInfo] = options[:payment_info] || 'N'
  attrs[:OrderInfo] = options[:order_info] || 'N'
  attrs[:EmailModify] = options[:email_editable] || '0'
  attrs[:LangType] = options[:locale] || 'zh-tw'
  attrs[:BackURL] = options[:cancel_url]
  attrs[:ReturnURL] = Newebpay::Engine.routes.url_helpers.periodical_callbacks_url(host: Newebpay.host) if  Newebpay.config.periodical_callback
  attrs[:NotifyURL] = Newebpay::Engine.routes.url_helpers.periodical_notify_callbacks_url(host: Newebpay.host) if  Newebpay.config.periodical_notify_callback

  options[:period_type] ||= :monthly
  case options[:period_type]
  when :daily
    attrs[:PeriodType] = 'D'
  when :weekly
    attrs[:PeriodType] = 'W'
  when :monthly
    attrs[:PeriodType] = 'M'
  when :yearly
    attrs[:PeriodType] = 'Y'
  else
    raise ArgumentError, "Invalid period_type: #{options[:period_type]}"
  end
end