require "xapo_sdk/version" require "xapo_utils" require "json" require "uri" require "yaml" module XapoTools module_function # Micro payment button configuration options. # # This function is intended to be a helper for creating empty micro # payments buttons configuration but also serves for documenting. A # hash with the intended fields would give the same results. # # Attributes: # sender_user_id (str): The id of the user sending the payment. # sender_user_email (str, optional): The email of the user sending # the payment. # sender_user_cellphone (str, optional): The celphone number of the user # sending the payment. # receiver_user_id (str): The id of the user receiving the payment. # receiver_user_email (str): The email of the user receiving the payment. # pay_object_id (str): A payment identifier in the TPA context. # amount_BIT (float, optional): The amount of bitcoins to be payed by the # widget. If not specified here, it must be entered on payment basis. # pay_type (str): The string representing the type of operation # ("Tip", "Pay", "Deposit" or "Donate"). def micro_payment_config return Hash[ :sender_user_id => "", :sender_user_email => "", :sender_user_cellphone => "", :receiver_user_id => "", :receiver_user_email => "", :pay_object_id => "", :amount_BIT => 0, :timestamp => XapoUtils.timestamp, :pay_type => "" ] end # 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. # # Attributes: # 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. class MicroPayment def initialize(service_url, app_id=nil, app_secret=nil) @service_url = service_url @app_id = app_id @app_secret = app_secret end def build_url(config) json_config = JSON.generate(config) if @app_secret == nil || @app_id == nil query_str = URI.encode_www_form( :payload => json_config, :customization => JSON.generate({:button_text => config[:pay_type]}) ) 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({:button_text => config[:pay_type]}) ) end widget_url = @service_url + "?" + query_str return widget_url end # Build an iframe HTML snippet in order to be embedded in apps. # # Args: # config (MicroPaymentConfig): The button configuration options. # See @MicroPaymentConfig. # # Returns: # string: the iframe HTML snippet ot be embedded in a page. def build_iframe_widget(config) widget_url = build_url(config) snippet = YAML::load(<<-END) | END return snippet end # Build div HTML snippet in order to be embedded in apps. # # Args: # config (MicroPaymentConfig): The button configuration options. # See @MicroPaymentConfig. # # Returns: # string: the div HTML snippet ot be embedded in a page. def build_div_widget(config) widget_url = build_url(config) snippet = YAML::load(<<-END) |
END return snippet end private :build_url end end module PayType TIP = "Tip" DONATE = "Donate" PAY = "Pay" DEPOSIT = "Deposit" end