class XapoTools::MicroPayment

Xapo's payment buttons snippet builder.

This class allows the construction of 2 kind of widgets, div and iframe. The result is a HTML snippet that could be embedded in a web page for doing micro payments though a payment button.

Params:

+service_url+ (str): The endpoint URL that returns the payment widget.
+app_id+ (str, optional): The id of the TPA for which the widget will be created.
+app_secret+ (str, optional): The TPA secret used to encrypt widget configuration.

Public Class Methods

new(service_url, app_id=nil, app_secret=nil) click to toggle source
# File lib/xapo_tools.rb, line 89
def initialize(service_url, app_id=nil, app_secret=nil)
  @service_url = service_url
  @app_id = app_id
  @app_secret = app_secret
end

Public Instance Methods

build_div_widget(config, customization=XapoTools::micro_payment_customization) click to toggle source

Build div HTML snippet in order to be embedded in apps.

Params:

+config+ (+Hash+): The button configuration options.
See @micro_payment_config.

Returns:

string: the div HTML snippet ot be embedded in a page.
# File lib/xapo_tools.rb, line 148
    def build_div_widget(config, customization=XapoTools::micro_payment_customization)
      widget_url = build_url(config, customization)

      snippet = YAML::load(<<-END)
      |
          <div id="tipButtonDiv" class="tipButtonDiv"></div>
          <div id="tipButtonPopup" class="tipButtonPopup"></div>
          <script>
              $(document).ready(function() {{
                  $("#tipButtonDiv").load("#{widget_url}");
              }});
          </script>
      END

      return snippet
    end
build_iframe_widget(config, customization=XapoTools::micro_payment_customization) click to toggle source

Build an iframe HTML snippet in order to be embedded in apps.

Params:

+config+ (+Hash+): The button configuration options.
See @micro_payment_config.

Returns:

string: the iframe HTML snippet ot be embedded in a page.
# File lib/xapo_tools.rb, line 126
    def build_iframe_widget(config, customization=XapoTools::micro_payment_customization)
      widget_url = build_url(config, customization)

      snippet = YAML::load(<<-END)
      |
          <iframe id="tipButtonFrame" scrolling="no" frameborder="0"
              style="border:none; overflow:hidden; height:22px;"
              allowTransparency="true" src="#{widget_url}">
          </iframe>
      END

      return snippet
    end

Private Instance Methods

build_url(config, customization) click to toggle source
# File lib/xapo_tools.rb, line 95
def build_url(config, customization)
  json_config = JSON.generate(config, customization)
  
  if @app_secret == nil || @app_id == nil
    query_str = URI.encode_www_form(
      :payload => json_config,
      :customization => JSON.generate(customization)
    )
  else
    encrypted_config = XapoUtils.encrypt(json_config, @app_secret)

    query_str = URI.encode_www_form(
      :app_id => @app_id, 
      :button_request => encrypted_config,
      :customization => JSON.generate(customization)
    )
  end

  widget_url = @service_url + "?" + query_str

  return widget_url
end