simplepay-rails4 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (53) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +8 -0
  3. data/CHANGELOG.md +34 -0
  4. data/Gemfile +8 -0
  5. data/Gemfile.lock +104 -0
  6. data/MIT-LICENSE +22 -0
  7. data/README.md +106 -0
  8. data/Rakefile +13 -0
  9. data/app/helpers/rails_helper.rb +38 -0
  10. data/lib/simplepay/authentication.rb +41 -0
  11. data/lib/simplepay/constants.rb +64 -0
  12. data/lib/simplepay/errors.rb +16 -0
  13. data/lib/simplepay/helpers/form_helper.rb +31 -0
  14. data/lib/simplepay/rails.rb +2 -0
  15. data/lib/simplepay/service.rb +133 -0
  16. data/lib/simplepay/services/donation.rb +90 -0
  17. data/lib/simplepay/services/marketplace.rb +88 -0
  18. data/lib/simplepay/services/marketplace_policy.rb +53 -0
  19. data/lib/simplepay/services/standard.rb +58 -0
  20. data/lib/simplepay/services/subscription.rb +95 -0
  21. data/lib/simplepay/support/amount.rb +55 -0
  22. data/lib/simplepay/support/billing_frequency.rb +14 -0
  23. data/lib/simplepay/support/boolean.rb +28 -0
  24. data/lib/simplepay/support/currency.rb +21 -0
  25. data/lib/simplepay/support/epoch.rb +39 -0
  26. data/lib/simplepay/support/field.rb +147 -0
  27. data/lib/simplepay/support/interval.rb +145 -0
  28. data/lib/simplepay/support/simple_amount.rb +37 -0
  29. data/lib/simplepay/support/subscription_period.rb +25 -0
  30. data/lib/simplepay/support.rb +16 -0
  31. data/lib/simplepay/validator.rb +59 -0
  32. data/lib/simplepay/version.rb +3 -0
  33. data/lib/simplepay.rb +29 -0
  34. data/simplepay.gemspec +27 -0
  35. data/test/simplepay/services/test_donation.rb +81 -0
  36. data/test/simplepay/services/test_marketplace.rb +81 -0
  37. data/test/simplepay/services/test_marketplace_policy.rb +48 -0
  38. data/test/simplepay/services/test_standard.rb +71 -0
  39. data/test/simplepay/services/test_subscription.rb +105 -0
  40. data/test/simplepay/support/test_amount.rb +46 -0
  41. data/test/simplepay/support/test_billing_frequency.rb +43 -0
  42. data/test/simplepay/support/test_boolean.rb +17 -0
  43. data/test/simplepay/support/test_epoch.rb +34 -0
  44. data/test/simplepay/support/test_field.rb +99 -0
  45. data/test/simplepay/support/test_interval.rb +92 -0
  46. data/test/simplepay/support/test_simple_amount.rb +28 -0
  47. data/test/simplepay/support/test_subscription_period.rb +49 -0
  48. data/test/simplepay/test_authentication.rb +25 -0
  49. data/test/simplepay/test_service.rb +118 -0
  50. data/test/simplepay/test_validator.rb +32 -0
  51. data/test/test_helper.rb +75 -0
  52. data/test/test_simplepay.rb +11 -0
  53. metadata +145 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e42f97931af0e65beccc52d2d544f2cbf8c2815d
4
+ data.tar.gz: bdea7625587ce52b6f579e1b65fe82d888352d17
5
+ SHA512:
6
+ metadata.gz: 391e3464befaf62006b0adcbc4f81e31738cc45b1db0ad4737654d09cdc10ee0242b78c6426ec8c3f5886533acc2801cdeb8c112c18e885ebf72c5998b3d235f
7
+ data.tar.gz: f8d320f3c25251a44a33fca8b5dfe6eb36d58154a7d623a55a434be7cf16347528392edce8b89b1b98f2466033c260fe5dd4a7825fe7f08de07d125ddc4816e1
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ .rvmrc
2
+ .DS_Store
3
+ *~
4
+ coverage
5
+ doc
6
+ pkg
7
+ .ruby-version
8
+ *.gem
data/CHANGELOG.md ADDED
@@ -0,0 +1,34 @@
1
+ == 0.4.0 2014-07-05
2
+
3
+ * Stripping gem to bare essentials
4
+
5
+ == 0.2.3 2011-2-10
6
+
7
+ * Supports Amazon Signature Version 2
8
+ * Updated for Rails 3
9
+
10
+ == 0.2.2 2009-06-07
11
+
12
+ * Added Donation service support.
13
+
14
+ == 0.2.1 2009-01-06
15
+
16
+ * Maintenance release for missing files.
17
+
18
+ == 0.2.0 2009-01-03
19
+
20
+ * Added Marketplace Service support [Nick Plante (zapnap)]
21
+ * Added Standard and Subscription documentation.
22
+ * Added a submit tag override to simplepay_form_for helper.
23
+ * Added a Simplepay::ResponseStatusCode::All shortcut.
24
+
25
+ == 0.1.1 2008-12-11
26
+
27
+ * Added a NotificationHelper to aid with Amazon IPN validation.
28
+ * Updated the Rails helper and added more detailed documentation.
29
+
30
+ == 0.1.0 2008-12-05
31
+
32
+ * Initial release
33
+ * Supports Amazon Simple Pay Standard and the-soon-to-be-released Subscriptions
34
+ * Generates your dynamic service HTML forms automatically.
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
4
+
5
+ gem "rails", "~> 4.1.0"
6
+ gem 'rake'
7
+ gem 'minitest-spec-rails'
8
+ gem "mocha"
data/Gemfile.lock ADDED
@@ -0,0 +1,104 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ simplepay-rails4 (0.4.0)
5
+ nokogiri (~> 1.6)
6
+ rails (~> 4)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ actionmailer (4.1.4)
12
+ actionpack (= 4.1.4)
13
+ actionview (= 4.1.4)
14
+ mail (~> 2.5.4)
15
+ actionpack (4.1.4)
16
+ actionview (= 4.1.4)
17
+ activesupport (= 4.1.4)
18
+ rack (~> 1.5.2)
19
+ rack-test (~> 0.6.2)
20
+ actionview (4.1.4)
21
+ activesupport (= 4.1.4)
22
+ builder (~> 3.1)
23
+ erubis (~> 2.7.0)
24
+ activemodel (4.1.4)
25
+ activesupport (= 4.1.4)
26
+ builder (~> 3.1)
27
+ activerecord (4.1.4)
28
+ activemodel (= 4.1.4)
29
+ activesupport (= 4.1.4)
30
+ arel (~> 5.0.0)
31
+ activesupport (4.1.4)
32
+ i18n (~> 0.6, >= 0.6.9)
33
+ json (~> 1.7, >= 1.7.7)
34
+ minitest (~> 5.1)
35
+ thread_safe (~> 0.1)
36
+ tzinfo (~> 1.1)
37
+ arel (5.0.1.20140414130214)
38
+ builder (3.2.2)
39
+ erubis (2.7.0)
40
+ hike (1.2.3)
41
+ i18n (0.6.9)
42
+ json (1.8.1)
43
+ mail (2.5.4)
44
+ mime-types (~> 1.16)
45
+ treetop (~> 1.4.8)
46
+ metaclass (0.0.4)
47
+ mime-types (1.25.1)
48
+ mini_portile (0.6.0)
49
+ minitest (5.3.5)
50
+ minitest-spec-rails (5.0.4)
51
+ minitest (~> 5.0)
52
+ rails (~> 4.1.0.rc2)
53
+ mocha (1.1.0)
54
+ metaclass (~> 0.0.1)
55
+ multi_json (1.10.1)
56
+ nokogiri (1.6.2.1)
57
+ mini_portile (= 0.6.0)
58
+ polyglot (0.3.5)
59
+ rack (1.5.2)
60
+ rack-test (0.6.2)
61
+ rack (>= 1.0)
62
+ rails (4.1.4)
63
+ actionmailer (= 4.1.4)
64
+ actionpack (= 4.1.4)
65
+ actionview (= 4.1.4)
66
+ activemodel (= 4.1.4)
67
+ activerecord (= 4.1.4)
68
+ activesupport (= 4.1.4)
69
+ bundler (>= 1.3.0, < 2.0)
70
+ railties (= 4.1.4)
71
+ sprockets-rails (~> 2.0)
72
+ railties (4.1.4)
73
+ actionpack (= 4.1.4)
74
+ activesupport (= 4.1.4)
75
+ rake (>= 0.8.7)
76
+ thor (>= 0.18.1, < 2.0)
77
+ rake (10.1.0)
78
+ sprockets (2.12.1)
79
+ hike (~> 1.2)
80
+ multi_json (~> 1.0)
81
+ rack (~> 1.0)
82
+ tilt (~> 1.1, != 1.3.0)
83
+ sprockets-rails (2.1.3)
84
+ actionpack (>= 3.0)
85
+ activesupport (>= 3.0)
86
+ sprockets (~> 2.8)
87
+ thor (0.19.1)
88
+ thread_safe (0.3.4)
89
+ tilt (1.4.1)
90
+ treetop (1.4.15)
91
+ polyglot
92
+ polyglot (>= 0.3.1)
93
+ tzinfo (1.2.1)
94
+ thread_safe (~> 0.1)
95
+
96
+ PLATFORMS
97
+ ruby
98
+
99
+ DEPENDENCIES
100
+ minitest-spec-rails
101
+ mocha
102
+ rails (~> 4.1.0)
103
+ rake
104
+ simplepay-rails4!
data/MIT-LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2008 Nathaniel E. Bibler
2
+ Copyright (c) 2011 Derrick Parkhurst
3
+ Copyright (c) 2014 Charles DuBose
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,106 @@
1
+ = simplepay (Rails Amazon Simple Pay Interface)
2
+
3
+ == DESCRIPTION:
4
+
5
+ This gem provides a Rails 4 interface to the Amazon Simple Pay payment service.
6
+
7
+ == FEATURES/PROBLEMS:
8
+
9
+ Supports the following Amazon Simple Pay services:
10
+
11
+ * Standard
12
+ * Subscription
13
+ * Marketplace
14
+ * Donation
15
+
16
+ == SYNOPSIS:
17
+
18
+ Add simplepay to your Bundler Gemfile
19
+
20
+ (in Gemfile)
21
+
22
+ gem "simplepay-rails4", :require => 'simplepay'
23
+
24
+ Configure the gem with your Amazon Web Services credentials:
25
+
26
+ (in config/initializers/simplepay.rb)
27
+
28
+ Simplepay.aws_access_key_id = 'MYAMAZONACCESSKEYID'
29
+ Simplepay.aws_secret_access_key = 'MYAMAZONSECRETACCESSKEY'
30
+
31
+ === Obtaining Amazon Web Services Credentials
32
+
33
+ 1) Create an Amazon Payments Personal Account
34
+ https://payments.amazon.com/sdui/sdui/basicaccount
35
+ or on Sandbox:
36
+ https://payments-sandbox.amazon.com/sdui/sdui/basicaccount
37
+
38
+ 2) Create an Amazon Payments Business Account
39
+ https://payments.amazon.com/sdui/sdui/premiumaccount
40
+ or in the sandbox for testing
41
+ https://payments-sandbox.amazon.com/sdui/sdui/premiumaccount
42
+
43
+ 3) Create an Amazon Web Services Account
44
+ https://aws-portal.amazon.com/gp/aws/developer/registration/index.html
45
+
46
+ 4) Retrieve the Access Key ID and Secret Access Key
47
+ https://aws-portal.amazon.com/gp/aws/developer/account/index.html?ie=UTF8&action=access-key
48
+
49
+ Note that the AWS Access Key ID and Secret Access Key is good for both testing in the sandbox and for production use.
50
+
51
+ === Generating your Simple Pay forms
52
+
53
+ Generally, this library will then be used directly from one (or more) of your
54
+ views. Depending on the type of Simple Pay service you're using (see
55
+ Simplepay::Services), some form values will be required, while others may be
56
+ optional. This is done like so:
57
+
58
+ <%= simplepay_form_for(:standard, {
59
+ :amount => 10.95,
60
+ :description => "Profit!"
61
+ }) %>
62
+
63
+ <%= simplepay_form_for(:subscription, {
64
+ :amount => 10.95,
65
+ :description => "MORE Profit!",
66
+ :recurring_frequency => "1 month"
67
+ }) %>
68
+
69
+
70
+ === Marketplace forms
71
+
72
+ Amazon Simple Pay Marketplace makes it easy to facilitate payments between
73
+ buyers and sellers, and to charge a fee for the transaction. If you're building
74
+ a marketplace application, your sellers must first agree to the policy that you
75
+ set. You can do this by specifying the Simplepay::Services::MarketplacePolicy
76
+ service:
77
+
78
+ <%= simplepay_form_for(:marketplace_policy, {
79
+ :max_fixed_fee => 5.00,
80
+ :max_variable_fee => 5,
81
+ :return_url => 'http://yourservice.com',
82
+ :reference_id => '1234567890'
83
+ }) %>
84
+
85
+ The user will be prompted to login to their Amazon account and accept the policy
86
+ you propose. After they complete the process, they'll be returned to the return_url
87
+ specified. Amazon will also provide a recipientEmail parameter that will contain
88
+ the seller email here (you may want to craft your return url so that it contains the
89
+ reference_id value and use this to 'acknowledge' a sellers consent and record their
90
+ Amazon recipient email address).
91
+
92
+ Only once this is done will you be able to offer marketplace checkout options for
93
+ that sellers items:
94
+
95
+ <%= simplepay_form_for(:marketplace, {
96
+ :amount => 34.95,
97
+ :description => "Mutual profit!",
98
+ :recipient_email => 'seller@gmail.com',
99
+ :fixed_marketplace_fee => 10.00,
100
+ :variable_marketplace_fee => 5
101
+ }) %>
102
+
103
+ == INSTALL:
104
+
105
+ sudo gem install simplepay-rails4
106
+
data/Rakefile ADDED
@@ -0,0 +1,13 @@
1
+ %w[rubygems rake rake/clean rake/testtask fileutils].each { |f| require f }
2
+ #require File.dirname(__FILE__) + '/lib/simplepay'
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.pattern = 'test/**/test_*.rb'
6
+ t.libs << 'app'
7
+ t.libs << 'lib'
8
+ t.libs << 'test'
9
+ t.verbose = true
10
+ end
11
+
12
+ task :default => [:test]
13
+ #task :release_and_update => [:gemspec, :release, :website, :post_news]
@@ -0,0 +1,38 @@
1
+ module Simplepay
2
+ module Rails
3
+
4
+ ##
5
+ # Adds helpers to your views for generating the correct HTML forms and
6
+ # valid signatures.
7
+ #
8
+ module SimplepayHelper
9
+
10
+ ##
11
+ # This is the general interface for generating your Simple Pay service
12
+ # forms. See Simplepay::Services for available services and information
13
+ # specific to each.
14
+ #
15
+ # === Example
16
+ #
17
+ # (in your view)
18
+ #
19
+ # <%= simplepay_form_for(:service_name, {:attr => 'foo'}) %>
20
+ #
21
+ def simplepay_form_for(service_name, attributes = {}, submit_tag = nil)
22
+ service = get_simplepay_service(service_name)
23
+ service.form(attributes, submit_tag).html_safe
24
+ end
25
+
26
+
27
+ private
28
+
29
+
30
+ def get_simplepay_service(name) #:nodoc:
31
+ service = "Simplepay::Services::#{name.to_s.camelize}".constantize
32
+ service.new
33
+ end
34
+
35
+ end
36
+
37
+ end
38
+ end
@@ -0,0 +1,41 @@
1
+ require 'openssl'
2
+ require 'base64'
3
+ require 'uri'
4
+ require 'cgi'
5
+
6
+ module Simplepay
7
+
8
+ class Signature
9
+
10
+ def initialize(uri, params, secret_key = Simplepay.aws_secret_access_key)
11
+ @uri = uri
12
+ @params = params
13
+ @secret_key = secret_key
14
+ end
15
+
16
+ def sign
17
+ make_canonical_string
18
+ compute_signature
19
+ end
20
+
21
+ private
22
+
23
+ def compute_signature
24
+ digest = OpenSSL::Digest.new('sha256')
25
+ Base64.encode64(OpenSSL::HMAC.digest(digest, @secret_key, @canonical)).chomp
26
+ end
27
+
28
+ def make_canonical_string
29
+ @canonical = "POST\n#{@uri.host}\n#{@uri.path}\n"
30
+ params = @params.reject{|k,v| v.blank? }.inject({}) { |a, (k,v)| a[k.to_s] = v; a }
31
+ params = params.sort.inject([]) { |a, v| a << urlencode(v[0]) + '=' + urlencode(v[1]) }.join('&')
32
+ @canonical += params
33
+ end
34
+
35
+ def urlencode(plaintext)
36
+ CGI.escape(plaintext.to_s).gsub('+', '%20').gsub('%7E', '~')
37
+ end
38
+
39
+ end
40
+
41
+ end
@@ -0,0 +1,64 @@
1
+ require 'simplepay/errors'
2
+ require 'simplepay/support/currency'
3
+
4
+ module Simplepay
5
+
6
+ module Boolean
7
+ True = '1'
8
+ False = '0'
9
+ end
10
+
11
+ module Interval
12
+ Day = 'day'
13
+ Week = 'week'
14
+ Month = 'month'
15
+ Year = 'year'
16
+ end
17
+
18
+ Intervals = [Interval::Day, Interval::Week, Interval::Month, Interval::Year]
19
+
20
+ ##
21
+ # Amazon Status Codes (FPS Basic)
22
+ # SA Success status for the ABT payment method.
23
+ # SB Success status for the ACH (bank account) payment method.
24
+ # SC Success status for the credit card payment method.
25
+ # SE System error.
26
+ # A Buyer abandoned the pipeline.
27
+ # CE Specifies a caller exception.
28
+ # PE Payment Method Mismatch Error: Specifies that the buyer does not have the payment method you requested.
29
+ # NP This account type does not support the specified payment method.
30
+ # NM You are not registered as a third-party caller to make this transaction. Contact Amazon Payments for more information.
31
+ #
32
+ module ResponseStatusCode
33
+ SuccessfulABT = 'SA'
34
+ SuccessfulACH = 'SB'
35
+ SuccessfulCC = 'SC'
36
+ Abandoned = 'A'
37
+ CallerException = 'CE'
38
+ PaymentMismatch = 'PE'
39
+ NotSupported = 'NP'
40
+ NotRegistered = 'NM'
41
+
42
+ # These haven't been verified so I'll leave them in for the time being
43
+ HtmlError = 'ME'
44
+ PaymentFailed = 'PF'
45
+ PaymentInitiated = 'PI'
46
+ PaymentReserved = 'PR'
47
+ PaymentSuccessful = 'PS'
48
+ SystemError = 'SE'
49
+ SubscriptionFailed = 'SF'
50
+ SubscriptionSuccessful = 'SS'
51
+
52
+ Successful = [SuccessfulABT, SuccessfulACH, SuccessfulCC, PaymentSuccessful, SubscriptionSuccessful]
53
+ Failure = [Abandoned, CallerException, PaymentMismatch, NotSupported, NotRegistered, HtmlError, PaymentFailed, SystemError, SubscriptionFailed]
54
+ Pending = [PaymentInitiated, PaymentReserved]
55
+ All = [Successful, Failure, Pending].flatten
56
+ end
57
+
58
+ module Currency
59
+ USD = Support::Currency.new('United States Dollar', 'USD', "%0.2f")
60
+ end
61
+
62
+ Currencies = [Currency::USD]
63
+
64
+ end
@@ -0,0 +1,16 @@
1
+ module Simplepay
2
+
3
+ ##
4
+ # All SimplePay errors will inherit from the base Simplepay::Error.
5
+ #
6
+ class Error < RuntimeError; end
7
+
8
+ #:stopdoc:
9
+
10
+ class RequiredFieldMissing < Error; end
11
+ class InvalidOptions < Error; end
12
+ class ParseError < Error; end
13
+
14
+ #:startdoc:
15
+
16
+ end
@@ -0,0 +1,31 @@
1
+ module Simplepay
2
+ module Helpers
3
+
4
+ module FormHelper
5
+
6
+ def self.tag(name, options = nil, open = false)
7
+ "<#{name}#{tag_options(options) if options}" + (open ? ">" : " />")
8
+ end
9
+
10
+ def self.content_tag(name, content, options = nil)
11
+ tag = "<#{name}#{tag_options(options)}>#{content}</#{name}>"
12
+ tag = tag.html_safe if tag.respond_to?(:html_safe)
13
+ tag
14
+ end
15
+
16
+
17
+ private
18
+
19
+
20
+ def self.tag_options(options)
21
+ unless options.blank?
22
+ attrs = []
23
+ attrs = options.map { |key, value| %(#{key}="#{value}") }
24
+ " #{attrs.sort * ' '}" unless attrs.empty?
25
+ end
26
+ end
27
+
28
+ end
29
+
30
+ end
31
+ end
@@ -0,0 +1,2 @@
1
+
2
+
@@ -0,0 +1,133 @@
1
+ require 'simplepay/helpers/form_helper'
2
+
3
+ module Simplepay
4
+
5
+ ##
6
+ # This is a base class from which to inherit functionality for all Amazon
7
+ # Simple Pay services (Subscriptions, Marketplace Buttons, etc.)
8
+ #
9
+ # === Required Fields
10
+ #
11
+ # The following fields are required for all Simple Pay services:
12
+ #
13
+ # access_key:: Your Amazon Web Service (AWS) access key (automatically filled from Simplepay.aws_access_key_id).
14
+ # 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)
15
+ #
16
+ class Service
17
+
18
+ # Fully-qualified URL for the production endpoint for the service.
19
+ ENDPOINT_URL = 'https://authorize.payments.amazon.com/pba/paypipeline'
20
+
21
+ # Fully-qualified URL for the sandbox (test) endpoint for the service.
22
+ SANDBOX_URL = 'https://authorize.payments-sandbox.amazon.com/pba/paypipeline'
23
+
24
+ class << self
25
+
26
+ ##
27
+ # Defines a field for the service.
28
+ #
29
+ # === Usage
30
+ #
31
+ # class Foo < Service
32
+ # field :access_key
33
+ # field :amount, :class => Amount, :map_to => :value
34
+ # end
35
+ #
36
+ def field(name, options = {})
37
+ field = Support::Field.new(self, name, options)
38
+ define_method("#{name.to_s.underscore}=", Proc.new { |value| self.fields.detect { |f| f.name == name }.value = value })
39
+ self.fields << field
40
+ field
41
+ end
42
+
43
+ ##
44
+ # Optional convenience method for:
45
+ #
46
+ # field :field_name, :required => true
47
+ #
48
+ # Any other +options+ given will be still be passed through.
49
+ #
50
+ def required_field(name, options = {})
51
+ field(name, options.merge(:required => true))
52
+ end
53
+
54
+ ##
55
+ # Returns the collection of fields defined by the service class.
56
+ #
57
+ def fields
58
+ @fields ||= []
59
+ end
60
+
61
+ def set_submit_tag(value)
62
+ @submit_tag = value
63
+ end
64
+
65
+ def submit_tag
66
+ @submit_tag
67
+ end
68
+
69
+ end
70
+
71
+
72
+ ##
73
+ # Returns the fields for the service instance.
74
+ #
75
+ def fields
76
+ @fields ||= self.class.fields.collect { |f| f.clone_for(self) }
77
+ end
78
+
79
+ ##
80
+ # Returns the URL for the service endpoint to use. If +sandbox+ is true,
81
+ # the SANDBOX_URL will be used. Otherwise, the ENDPOINT_URL will be used.
82
+ #
83
+ def url(sandbox = Simplepay.use_sandbox?)
84
+ sandbox ? self.class.const_get(:SANDBOX_URL) : self.class.const_get(:ENDPOINT_URL)
85
+ end
86
+
87
+ def form(attributes = {}, submit = nil)
88
+ set_accessor_fields
89
+ set_fields(attributes)
90
+ set_signature
91
+ content = generate_input_fields + generate_submit_field(submit)
92
+ Simplepay::Helpers::FormHelper.content_tag(:form, content, {:method => 'post', :action => url})
93
+ end
94
+
95
+
96
+ private
97
+
98
+
99
+ def generate_input_fields
100
+ self.fields.collect { |f| f.to_input }.join
101
+ end
102
+
103
+ def generate_submit_field(submit)
104
+ options = {:type => 'submit'}
105
+ options.merge!(:value => self.class.submit_tag) if self.class.submit_tag
106
+ submit ? submit.to_s : Simplepay::Helpers::FormHelper.tag(:input, options)
107
+ end
108
+
109
+ def set_accessor_fields
110
+ self.access_key = Simplepay.aws_access_key_id if self.respond_to?(:access_key=)
111
+ end
112
+
113
+ def set_fields(hash)
114
+ hash.each_pair do |key, value|
115
+ self.send("#{key}=", value) if self.respond_to?("#{key}=")
116
+ end
117
+ end
118
+
119
+ def set_signature
120
+ fields = {}
121
+ self.fields.each { |f| fields[f.service_name] = f.value unless f.service_name == 'signature' }
122
+ self.signature = Signature.new(URI.parse(url), fields).sign if self.respond_to?(:signature=)
123
+ end
124
+
125
+ end
126
+
127
+ end
128
+
129
+ require 'simplepay/services/subscription'
130
+ require 'simplepay/services/standard'
131
+ require 'simplepay/services/donation'
132
+ require 'simplepay/services/marketplace'
133
+ require 'simplepay/services/marketplace_policy'