class Newebpay::Donation::Form

Constants

PAYMENT_METHODS
REQUIRED_ATTRS

Attributes

attrs[RW]
donation_url[RW]
merchant_id[RW]

Public Class Methods

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

  check_valid(options)
  parse_attr(options)

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

Public Instance Methods

check_value() click to toggle source
# File lib/newebpay/donation/form.rb, line 27
def check_value
  @check_value ||= Newebpay::NewebpayHelper.sha256_encode(Newebpay.config.hash_key, Newebpay.config.hash_iv, check_value_raw)
end
check_value_raw() click to toggle source
# File lib/newebpay/donation/form.rb, line 31
def check_value_raw
  URI.encode_www_form(attrs.slice('Amt', 'MerchantID', 'MerchantOrderNo', 'TimeStamp', 'Version').sort)
end
form_attrs() click to toggle source
# File lib/newebpay/donation/form.rb, line 23
def form_attrs
  @form_attrs ||= @attrs
end
version() click to toggle source
# File lib/newebpay/donation/form.rb, line 35
def version
  '1.0'
end

Private Instance Methods

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

  raise ArgumentError, 'Missing required argument: donation_url.' unless donation_url

  unless Newebpay.config.donation_notify_callback
    raise ArgumentError, 'Missing donation_notify_callback block in initializer'
  end

  %i[order_number description price].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/donation/form.rb, line 41
def parse_attr(options)
  attrs['MerchantID'] = merchant_id
  attrs['MerchantOrderNo'] = options[:order_number]
  attrs['ItemDesc'] = options[:description]
  attrs['Amt'] = options[:price]
  attrs['Templates'] = options[:template_type] || 'donate'
  attrs['ExpireDate'] = options[:expire_date]
  attrs['Nickname'] = options[:anonymous] ? 'on' : 'off'
  attrs['PaymentMAIL'] = options[:email]
  attrs['PaymentName'] = options[:name]
  attrs['PaymentID'] = options[:uni_no]
  attrs['PaymentTEL'] = options[:phone]
  attrs['PaymentRegisterAddress'] = options[:id_address]
  attrs['PaymentMailAddress'] = options[:address]
  attrs['Receipt'] = 'on'
  attrs['ReceiptTitle'] = options[:receipt_name]
  attrs['PaymentReceiptAddress'] = options[:receipt_address]
  attrs['ReturnURL'] = options[:return_url]
  attrs['NotifyURL'] = Newebpay::Engine.routes.url_helpers.donation_notify_callbacks_url(host: Newebpay.host) if Newebpay.config.donation_notify_callback

  options[:payment_methods].each do |payment_method|
    attrs[payment_method.to_s.upcase] = 'on'
  end
end