class Simplepay::Service
This is a base class from which to inherit functionality for all Amazon Simple Pay services (Subscriptions, Marketplace Buttons, etc.)
Required Fields¶ ↑
The following fields are required for all Simple Pay services:
- access_key
-
Your Amazon Web
Service
(AWS) access key (automatically filled from Simplepay.aws_access_key_id). - signature
-
The validation string, guaranteeing that you are the one generating the request and that the values were not tampered with enroute (automatically generated by the form generators)
Constants
- ENDPOINT_URL
Fully-qualified URL for the production endpoint for the service.
- SANDBOX_URL
Fully-qualified URL for the sandbox (test) endpoint for the service.
Attributes
Public Class Methods
Defines a field for the service.
Usage¶ ↑
class Foo < Service field :access_key field :amount, :class => Amount, :map_to => :value end
# File lib/simplepay/service.rb, line 39 def field(name, options = {}) field = Support::Field.new(self, name, options) define_method("#{name.to_s.underscore}=", Proc.new { |value| self.fields.detect { |f| f.name == name }.value = value }) self.fields << field field end
Returns the collection of fields defined by the service class.
# File lib/simplepay/service.rb, line 60 def fields @fields ||= [] end
Optional convenience method for:
field :field_name, :required => true
Any other options
given will be still be passed through.
# File lib/simplepay/service.rb, line 53 def required_field(name, options = {}) field(name, options.merge(:required => true)) end
# File lib/simplepay/service.rb, line 64 def set_submit_tag(value) @submit_tag = value end
# File lib/simplepay/service.rb, line 68 def submit_tag @submit_tag end
Public Instance Methods
Returns the fields for the service instance.
# File lib/simplepay/service.rb, line 79 def fields @fields ||= self.class.fields.collect { |f| f.clone_for(self) } end
# File lib/simplepay/service.rb, line 91 def form(attributes = {}, submit = nil) self.output_buffer = ActionView::OutputBuffer.new set_accessor_fields set_fields(attributes) set_signature content = generate_input_fields content += submit ? submit.to_s : generate_submit_field() content_tag(:form, content.html_safe, method: 'post', action: url) end
Returns the URL for the service endpoint to use. If sandbox
is true, the SANDBOX_URL
will be used. Otherwise, the ENDPOINT_URL
will be used.
# File lib/simplepay/service.rb, line 87 def url(sandbox = Simplepay.use_sandbox?) sandbox ? self.class.const_get(:SANDBOX_URL) : self.class.const_get(:ENDPOINT_URL) end
Private Instance Methods
# File lib/simplepay/service.rb, line 103 def generate_input_fields self.fields.collect { |f| f.to_input }.join end
# File lib/simplepay/service.rb, line 107 def generate_submit_field submit_tag(self.class.submit_tag) end
# File lib/simplepay/service.rb, line 111 def set_accessor_fields self.access_key = Simplepay.aws_access_key_id if self.respond_to?(:access_key=) end
# File lib/simplepay/service.rb, line 115 def set_fields(hash) hash.each_pair do |key, value| self.send("#{key}=", value) if self.respond_to?("#{key}=") end end
# File lib/simplepay/service.rb, line 121 def set_signature fields = {} self.fields.each { |f| fields[f.service_name] = f.value unless f.service_name == 'signature' } self.signature = Signature.new(URI.parse(url), fields).sign if self.respond_to?(:signature=) end