class AtPay::Button

Constants

OPTIONS

Public Class Methods

new(token, short_token, amount, merchant_name, options={}) click to toggle source
# File lib/atpay/button.rb, line 21
def initialize(token, short_token, amount, merchant_name, options={})
  @token            = token
  @short_token      = short_token
  @amount           = amount
  @merchant_name    = merchant_name
  @options          = OPTIONS.merge(options)
  @options[:image]  = nil if @options[:image] == ''
end

Public Instance Methods

default_mailto() click to toggle source
# File lib/atpay/button.rb, line 30
def default_mailto
  "mailto:#{mailto_processor}?subject=#{mailto_subject}&body=#{mailto_body}"
end
render(args={}) click to toggle source
# File lib/atpay/button.rb, line 34
def render(args={})
  @options.update args

  template.render({
    'url'          => default_mailto,
    'outlook_url'  => outlook_mailto,
    'yahoo_url'    => yahoo_mailto,
    'content'      => amount,
    'dollar'       => amount.match(/\$\d+(?=\.)/).to_s,
    'cents'        => ".#{amount.match(/(?<=\.)[^.]*/).to_s}",
  }.update(string_hash @options))
end

Private Instance Methods

amount() click to toggle source
# File lib/atpay/button.rb, line 48
def amount
  "$%.2f" % @amount.to_f
end
mailto_body() click to toggle source

Parse the mailto body, this is where we inject the token, merchant_name and amount values we received in the options.

@return [String]

# File lib/atpay/button.rb, line 98
def mailto_body
  URI.encode(mailto_body_template.render({
    'amount' => amount,
    'merchant_name' => @merchant_name}))
end
mailto_body_template() click to toggle source

Load the mailto body template from the specified location

# File lib/atpay/button.rb, line 86
def mailto_body_template
  Liquid::Template.parse(File.read(File.join(@options[:templates], "mailto_body.liquid")))
end
mailto_processor() click to toggle source
# File lib/atpay/button.rb, line 90
def mailto_processor
  "payment-id-#{@short_token}@#{@options[:processor]}"
end
mailto_subject() click to toggle source
# File lib/atpay/button.rb, line 73
def mailto_subject
  URI.encode("Press send to pay #{amount} to #{@merchant_name} ")
end
outlook_mailto() click to toggle source
# File lib/atpay/button.rb, line 81
def outlook_mailto
  "https://www.hotmail.com/secure/start?action=compose&to=#{mailto_processor}&subject=#{mailto_subject}&body=#{mailto_body}"
end
provider() click to toggle source
# File lib/atpay/button.rb, line 59
def provider
  return :default if @options[:email].nil?

  if ["yahoo.com", "ymail.com", "rocketmail.com"].any? { |c| @options[:email].include? c }
    :yahoo
  else
    :default
  end
end
string_hash(hsh) click to toggle source
# File lib/atpay/button.rb, line 52
def string_hash(hsh)
  hsh.inject({}) do |result, key|
    result[key[0].to_s] = key[1]
    result
  end
end
template() click to toggle source

This is processed as liquid - in the future we can allow overwriting the template here and creating custom buttons.

# File lib/atpay/button.rb, line 106
def template
  Liquid::Template.parse(template_content(template_name))
end
template_content(template_name) click to toggle source

Determine which template to load based on the domain of the email address. This preserves the mailto behavior across email environments.

# File lib/atpay/button.rb, line 123
def template_content(template_name)
  File.read(File.join(@options[:templates], template_name))
end
template_name() click to toggle source
# File lib/atpay/button.rb, line 110
def template_name
  wrap_prefix = @options[:wrap] ? "wrap_" : ""

  case provider
    when :yahoo
      "#{wrap_prefix}yahoo.liquid"
    when :default
      "#{wrap_prefix}default.liquid"
  end
end
token() click to toggle source
# File lib/atpay/button.rb, line 69
def token
  @token.chars.each_slice(50).map(&:join).join("\n")
end
yahoo_mailto() click to toggle source
# File lib/atpay/button.rb, line 77
def yahoo_mailto
  "http://compose.mail.yahoo.com/?to=#{mailto_processor}&subject=#{mailto_subject}&body=#{mailto_body}"
end