Class: AtPay::Button::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/atpay/button/generator.rb

Constant Summary collapse

TOKEN_TYPES =

TODO: This should really move to the underlying token library.

{
  payment: nil,
  validation: 1
}

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Generator

Returns a new instance of Generator.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/atpay/button/generator.rb', line 13

def initialize(options)
  @options = { 
    :title => "Pay",
    :type => :payment,
    :group => nil,
    :user_data => nil,
    :template => {}
  }.update options

  @options[:version] = TOKEN_TYPES[@options[:type]] if TOKEN_TYPES[@options[:type]]

  validate_user_data
  @options[:amount] = amount
end

Instance Method Details

#amountObject



28
29
30
# File 'lib/atpay/button/generator.rb', line 28

def amount
  @amount ||= @options[:amount].respond_to?(:gsub) ? @options[:amount].gsub(/[^0-9\.]/, "").to_f : @options[:amount]
end

#build(token_map) ⇒ Array

Build buttons for the provided collection. build will accept an array of email addresses [‘bob@bob’, ‘sue@sue’], or any “paired” collection: For example [[‘bob@bob’, ‘card_token’], [‘sue@sue’, ‘card_token’]] will work, as will ‘bob@bob’ => ‘card_token’, ‘sue@sue’ => ‘card_token’. If providing only a list of email addresses build will create email tokens. Only use this behavior if you are sure it is what both you and your customer want. An email token isn’t ultra-specific about the card it charges. It will charge the first card for that individual it finds that your @Pay account has permission to use.

Parameters:

  • token_map (Enumerable)

Returns:

  • (Array)

    a list of tuples containing the email address and button code as [String]s



90
91
92
93
94
# File 'lib/atpay/button/generator.rb', line 90

def build(token_map)
  token_map.collect do |email, card|
    card ? [email, generate(email: email, source: card)] : [email, generate(email: email, type: :email)]
  end
end

#generate(options) ⇒ String

Generate the actual button HTML. There are three options available :source (the card token), :email (the email address the button is intended for), :type (the type of the source identifier) :type defaults to card.

Parameters:

  • options (Hash)

    there are three options that matter: :source, :email and :type

Returns:

  • (String)


72
73
74
75
76
77
78
# File 'lib/atpay/button/generator.rb', line 72

def generate(options)
  options[:source] ? source = options[:source] : source = options[:email]
  options[:type] ? type = options[:type] : type = :card
  email = options[:email]

  to_html token(type, source), email
end

#template(email = nil) ⇒ AtPay::Button::Template

Instanciate a button template instance for a specific email address. The email is necessary for selecting the correct template files. There are some special cases depending on the environment that the button is being viewed in.

Parameters:

  • email (String) (defaults to: nil)

    the email address the button is intended for.

Returns:



56
57
58
# File 'lib/atpay/button/generator.rb', line 56

def template(email = nil)
  Template.new(@options.update(:email => email, :amount => amount))
end

#to_html(token, email = nil) ⇒ Object



60
61
62
63
# File 'lib/atpay/button/generator.rb', line 60

def to_html(token, email = nil)
  @options.update({:token => token})
  template(email).render(token: token, :email => email)
end

#token(type, source) ⇒ String

Builds an @Pay Payment token for injection into a mailto button.

Parameters:

  • type (Symbol)

    The button type: :email, :card, :member

  • source (String)

    The coresponding type value

Returns:

  • (String)


45
46
47
# File 'lib/atpay/button/generator.rb', line 45

def token(type, source)
  session.security_key(@options.merge({type => source})).email_token
end

#validate_user_dataObject

Custom data that is to be added to the token can’t exceed 2,500 characters.



33
34
35
36
37
# File 'lib/atpay/button/generator.rb', line 33

def validate_user_data
  return unless @options[:user_data]

  raise LengthError.new "user_data can't be longer than 2,500 characters, you provided #{@options[:user_data].length} characters" if @options[:user_data].length > 2500
end