simplepay 0.1.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 (41) hide show
  1. data.tar.gz.sig +2 -0
  2. data/History.txt +5 -0
  3. data/Manifest.txt +38 -0
  4. data/README.rdoc +78 -0
  5. data/Rakefile +24 -0
  6. data/lib/simplepay.rb +26 -0
  7. data/lib/simplepay/authentication.rb +69 -0
  8. data/lib/simplepay/constants.rb +26 -0
  9. data/lib/simplepay/errors.rb +16 -0
  10. data/lib/simplepay/helpers/form_helper.rb +29 -0
  11. data/lib/simplepay/helpers/rails_helper.rb +28 -0
  12. data/lib/simplepay/service.rb +122 -0
  13. data/lib/simplepay/services/standard.rb +36 -0
  14. data/lib/simplepay/services/subscription.rb +53 -0
  15. data/lib/simplepay/support.rb +15 -0
  16. data/lib/simplepay/support/amount.rb +55 -0
  17. data/lib/simplepay/support/billing_frequency.rb +14 -0
  18. data/lib/simplepay/support/boolean.rb +28 -0
  19. data/lib/simplepay/support/currency.rb +21 -0
  20. data/lib/simplepay/support/epoch.rb +39 -0
  21. data/lib/simplepay/support/field.rb +147 -0
  22. data/lib/simplepay/support/interval.rb +143 -0
  23. data/lib/simplepay/support/subscription_period.rb +25 -0
  24. data/script/console +10 -0
  25. data/script/destroy +14 -0
  26. data/script/generate +14 -0
  27. data/simplepay.gemspec +43 -0
  28. data/test/simplepay/services/test_subscription.rb +109 -0
  29. data/test/simplepay/support/test_amount.rb +46 -0
  30. data/test/simplepay/support/test_billing_frequency.rb +43 -0
  31. data/test/simplepay/support/test_boolean.rb +17 -0
  32. data/test/simplepay/support/test_epoch.rb +34 -0
  33. data/test/simplepay/support/test_field.rb +99 -0
  34. data/test/simplepay/support/test_interval.rb +92 -0
  35. data/test/simplepay/support/test_subscription_period.rb +49 -0
  36. data/test/simplepay/test_authentication.rb +33 -0
  37. data/test/simplepay/test_service.rb +118 -0
  38. data/test/test_helper.rb +77 -0
  39. data/test/test_simplepay.rb +11 -0
  40. metadata +155 -0
  41. metadata.gz.sig +0 -0
data.tar.gz.sig ADDED
@@ -0,0 +1,2 @@
1
+ C�9��l��"�E�>�(Ϳ��=+fp�}t?8�}� ��Q<��;���(-���tԮ�����S�FxED|��i��9ߓ_;*,�r���=/<��
2
+ x���8�"�.lS�׿����޽����fQ�:����.R-����U���.C�=��H��&�,��G�=���r��J��*�Bs�[���Ot���$�5mI���1�W �@����IL1~�ڗ���+w�~F��rpJ]CP�-T�~�(�?V�7�
data/History.txt ADDED
@@ -0,0 +1,5 @@
1
+ == 0.0.1 2008-12-05
2
+
3
+ * Initial release
4
+ * Supports Amazon Simple Pay Standard and the-soon-to-be-released Subscriptions
5
+ * Generates your dynamic service HTML forms automatically.
data/Manifest.txt ADDED
@@ -0,0 +1,38 @@
1
+ History.txt
2
+ Manifest.txt
3
+ README.rdoc
4
+ Rakefile
5
+ lib/simplepay.rb
6
+ lib/simplepay/authentication.rb
7
+ lib/simplepay/constants.rb
8
+ lib/simplepay/errors.rb
9
+ lib/simplepay/helpers/form_helper.rb
10
+ lib/simplepay/helpers/rails_helper.rb
11
+ lib/simplepay/service.rb
12
+ lib/simplepay/services/standard.rb
13
+ lib/simplepay/services/subscription.rb
14
+ lib/simplepay/support.rb
15
+ lib/simplepay/support/amount.rb
16
+ lib/simplepay/support/billing_frequency.rb
17
+ lib/simplepay/support/boolean.rb
18
+ lib/simplepay/support/currency.rb
19
+ lib/simplepay/support/epoch.rb
20
+ lib/simplepay/support/field.rb
21
+ lib/simplepay/support/interval.rb
22
+ lib/simplepay/support/subscription_period.rb
23
+ script/console
24
+ script/destroy
25
+ script/generate
26
+ simplepay.gemspec
27
+ test/simplepay/services/test_subscription.rb
28
+ test/simplepay/support/test_amount.rb
29
+ test/simplepay/support/test_billing_frequency.rb
30
+ test/simplepay/support/test_boolean.rb
31
+ test/simplepay/support/test_epoch.rb
32
+ test/simplepay/support/test_field.rb
33
+ test/simplepay/support/test_interval.rb
34
+ test/simplepay/support/test_subscription_period.rb
35
+ test/simplepay/test_authentication.rb
36
+ test/simplepay/test_service.rb
37
+ test/test_helper.rb
38
+ test/test_simplepay.rb
data/README.rdoc ADDED
@@ -0,0 +1,78 @@
1
+ = simplepay (Rails Amazon Simple Pay Interface)
2
+
3
+ http://simplepay.rubyforge.org
4
+
5
+ == DESCRIPTION:
6
+
7
+ This gem provides a Rails interface to the Amazon Simple Pay payment service.
8
+
9
+ == FEATURES/PROBLEMS:
10
+
11
+ Supports the following Amazon Simple Pay services:
12
+
13
+ * Standard
14
+ * Subscription
15
+
16
+ The following services are coming someday (hopefully soon):
17
+
18
+ * Donation
19
+ * Marketplace
20
+
21
+ == SYNOPSIS:
22
+
23
+ Configure the gem with your Amazon credentials:
24
+
25
+ (in config/initializers/simplepay.rb)
26
+
27
+ Simplepay.aws_access_key_id = 'MYAMAZONACCESSKEYID'
28
+ Simplepay.aws_secret_access_key = 'MYAMAZONSECRETACCESSKEY'
29
+ Simplepay.account_id = 'ACCOUNTIDFORAMAZONPAYMENTS'
30
+
31
+ Generally, this library will then be used directly from one (or more) of your
32
+ views, like so:
33
+
34
+ <%= simplepay_form_for(:standard, {
35
+ :amount => 10.95,
36
+ :description => "Profit!"
37
+ }) %>
38
+
39
+ <%= simplepay_form_for(:subscription, {
40
+ :amount => 10.95,
41
+ :description => "MORE Profit!",
42
+ :recurring_frequency => "1 month"
43
+ }) %>
44
+
45
+ == REQUIREMENTS:
46
+
47
+ 1. You must have an Amazon Payments <b>business account</b>.
48
+ 2. You must know your Amazon Payments <b>account identifier</b> [instructions to come]
49
+ 3. You must know your Amazon Web Services <b>access key</b> and <b>secret access key</b>.
50
+
51
+ == INSTALL:
52
+
53
+ sudo gem install simplepay
54
+
55
+ == LICENSE:
56
+
57
+ (The MIT License)
58
+
59
+ Copyright (c) 2008 Nathaniel E. Bibler
60
+
61
+ Permission is hereby granted, free of charge, to any person obtaining
62
+ a copy of this software and associated documentation files (the
63
+ 'Software'), to deal in the Software without restriction, including
64
+ without limitation the rights to use, copy, modify, merge, publish,
65
+ distribute, sublicense, and/or sell copies of the Software, and to
66
+ permit persons to whom the Software is furnished to do so, subject to
67
+ the following conditions:
68
+
69
+ The above copyright notice and this permission notice shall be
70
+ included in all copies or substantial portions of the Software.
71
+
72
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
73
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
74
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
75
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
76
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
77
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
78
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,24 @@
1
+ %w[rubygems rake rake/clean fileutils newgem rubigen].each { |f| require f }
2
+ require File.dirname(__FILE__) + '/lib/simplepay'
3
+
4
+ $hoe = Hoe.new('simplepay', Simplepay::VERSION) do |p|
5
+ p.developer('Nathaniel E. Bibler', 'gem@nathanielbibler.com')
6
+ p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n")
7
+ p.rubyforge_name = p.name
8
+ p.extra_deps = [
9
+ ['activesupport','>= 2.0.2']
10
+ ]
11
+ p.extra_dev_deps = [
12
+ ['newgem', ">= #{::Newgem::VERSION}"]
13
+ ]
14
+
15
+ p.clean_globs |= %w[**/.DS_Store tmp *.log]
16
+ path = (p.rubyforge_name == p.name) ? p.rubyforge_name : "\#{p.rubyforge_name}/\#{p.name}"
17
+ p.remote_rdoc_dir = ''
18
+ p.rsync_args = '-av --delete --ignore-errors'
19
+ end
20
+
21
+ require 'newgem/tasks' # load /tasks/*.rake
22
+ Dir['tasks/**/*.rake'].each { |t| load t }
23
+
24
+ task :default => [:test]
data/lib/simplepay.rb ADDED
@@ -0,0 +1,26 @@
1
+ $:.unshift(File.dirname(__FILE__)) unless
2
+ $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
3
+
4
+ require 'active_support'
5
+
6
+ module Simplepay
7
+
8
+ VERSION = '0.1.0' unless const_defined?(:VERSION)
9
+
10
+ mattr_accessor :aws_access_key_id
11
+ mattr_accessor :aws_secret_access_key
12
+ mattr_accessor :account_id
13
+
14
+ mattr_accessor :use_sandbox
15
+ @@use_sandbox = true
16
+
17
+ def self.use_sandbox?
18
+ @@use_sandbox
19
+ end
20
+
21
+ end
22
+
23
+ require 'simplepay/constants'
24
+ require 'simplepay/support'
25
+ require 'simplepay/service'
26
+ require 'simplepay/helpers/form_helper'
@@ -0,0 +1,69 @@
1
+ require 'openssl'
2
+ require 'base64'
3
+
4
+ module Simplepay
5
+
6
+ ##
7
+ # This module generates RFC-2104-compliant HMAC signatures. These
8
+ # signatures are used by both Amazon and you to determine whether or not
9
+ # data transmitted is authentic. The hash is based on the amazon secret
10
+ # access key, which is a trusted secret between both parties.
11
+ #
12
+ # === HMAC (RFC-2104 Specification)
13
+ #
14
+ # For more information about the RFC-2104 spec,
15
+ # see http://www.ietf.org/rfc/rfc2104.txt
16
+ #
17
+ module Authentication
18
+
19
+ class << self
20
+
21
+ def generate(hash_data, secret_access_key = Simplepay.aws_secret_access_key)
22
+ encode(digest(convert_to_string(hash_data), secret_access_key))
23
+ end
24
+
25
+ def authentic?(hash_data, signature, secret_access_key = Simplepay.aws_secret_access_key)
26
+ signature == generate(hash_data, secret_access_key)
27
+ end
28
+
29
+
30
+ private
31
+
32
+
33
+ ##
34
+ # Converts a Hash of key-value pairs into an Amazon compliant block of
35
+ # signature text.
36
+ #
37
+ #=== Example
38
+ #
39
+ # {:key1 => 'Value1', 'foo' => 3 }
40
+ #
41
+ # would become
42
+ #
43
+ # "foo3key1Value1"
44
+ #
45
+ def convert_to_string(hash)
46
+ raise(ArgumentError, "Expected a Hash of data") unless hash.kind_of?(Hash)
47
+ data = hash.stringify_keys
48
+ string = ''
49
+ data.keys.sort.each { |k| string += "#{k}#{data[k]}" unless data[k].blank? }
50
+ string
51
+ end
52
+
53
+ ##
54
+ # Generate an RFC-2104-compliant HMAC
55
+ #
56
+ def digest(value, secret_access_key)
57
+ digest = OpenSSL::Digest::Digest.new('sha1')
58
+ OpenSSL::HMAC.digest(digest, secret_access_key, value)
59
+ end
60
+
61
+ def encode(value)
62
+ Base64.encode64(value).chomp
63
+ end
64
+
65
+ end
66
+
67
+ end
68
+
69
+ end
@@ -0,0 +1,26 @@
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
+ module Currency
21
+ USD = Support::Currency.new('United States Dollar', 'USD', "%0.2f")
22
+ end
23
+
24
+ Currencies = [Currency::USD]
25
+
26
+ 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,29 @@
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
+ "<#{name}#{tag_options(options)}>#{content}</#{name}>"
12
+ end
13
+
14
+
15
+ private
16
+
17
+
18
+ def self.tag_options(options)
19
+ unless options.blank?
20
+ attrs = []
21
+ attrs = options.map { |key, value| %(#{key}="#{value}") }
22
+ " #{attrs.sort * ' '}" unless attrs.empty?
23
+ end
24
+ end
25
+
26
+ end
27
+
28
+ end
29
+ end
@@ -0,0 +1,28 @@
1
+ require 'action_view/base'
2
+
3
+ module Simplepay
4
+ module Helpers
5
+
6
+ module RailsHelper
7
+
8
+ def simplepay_form_for(service_name, attributes = {})
9
+ service = get_simplepay_service(service_name)
10
+ service.form(attributes)
11
+ end
12
+
13
+
14
+ private
15
+
16
+
17
+ def get_simplepay_service(name)
18
+ service = "Simplepay::Services::#{name.to_s.camelize}".constantize
19
+ service.new
20
+ end
21
+
22
+ end
23
+
24
+ end
25
+ end
26
+
27
+ # Inject helper into Rails ActionView.
28
+ ActionView::Base.__send__(:include, Simplepay::Helpers::RailsHelper)
@@ -0,0 +1,122 @@
1
+ module Simplepay
2
+
3
+ ##
4
+ # This is a base class from which to inherit functionality for all Amazon
5
+ # Simple Pay services (Subscriptions, Marketplace Buttons, etc.)
6
+ #
7
+ class Service
8
+
9
+ # Fully-qualified URL for the production endpoint for the service.
10
+ ENDPOINT_URL = 'https://authorize.payments.amazon.com/pba/paypipeline'
11
+
12
+ # Fully-qualified URL for the sandbox (test) endpoint for the service.
13
+ SANDBOX_URL = 'https://authorize.payments-sandbox.amazon.com/pba/paypipeline'
14
+
15
+ class << self
16
+
17
+ ##
18
+ # Defines a field for the service.
19
+ #
20
+ # === Usage
21
+ #
22
+ # class Foo < Service
23
+ # field :access_key
24
+ # field :amount, :class => Amount, :map_to => :value
25
+ # end
26
+ #
27
+ def field(name, options = {})
28
+ field = Support::Field.new(self, name, options)
29
+ define_method("#{name.to_s.underscore}=", Proc.new { |value| self.fields.detect { |f| f.name == name }.value = value })
30
+ self.fields << field
31
+ field
32
+ end
33
+
34
+ ##
35
+ # Optional convenience method for:
36
+ #
37
+ # field :field_name, :required => true
38
+ #
39
+ # Any other +options+ given will be still be passed through.
40
+ #
41
+ def required_field(name, options = {})
42
+ field(name, options.merge(:required => true))
43
+ end
44
+
45
+ ##
46
+ # Returns the collection of fields defined by the service class.
47
+ #
48
+ def fields
49
+ @fields ||= []
50
+ end
51
+
52
+ def set_submit_tag(value)
53
+ @submit_tag = value
54
+ end
55
+
56
+ def submit_tag
57
+ @submit_tag
58
+ end
59
+
60
+ end
61
+
62
+
63
+ ##
64
+ # Returns the fields for the service instance.
65
+ #
66
+ def fields
67
+ @fields ||= self.class.fields.collect { |f| f.clone_for(self) }
68
+ end
69
+
70
+ ##
71
+ # Returns the URL for the service endpoint to use. If +sandbox+ is true,
72
+ # the SANDBOX_URL will be used. Otherwise, the ENDPOINT_URL will be used.
73
+ #
74
+ def url(sandbox = Simplepay.use_sandbox?)
75
+ sandbox ? self.class.const_get(:SANDBOX_URL) : self.class.const_get(:ENDPOINT_URL)
76
+ end
77
+
78
+ def form(attributes = {}, submit = nil)
79
+ set_accessor_fields
80
+ set_fields(attributes)
81
+ set_signature
82
+ content = generate_input_fields + generate_submit_field(submit)
83
+ Simplepay::Helpers::FormHelper.content_tag(:form, content, {:method => 'post', :action => url})
84
+ end
85
+
86
+
87
+ private
88
+
89
+
90
+ def generate_input_fields
91
+ self.fields.collect { |f| f.to_input }.join
92
+ end
93
+
94
+ def generate_submit_field(submit)
95
+ options = {:type => 'submit'}
96
+ options.merge!(:value => self.class.submit_tag) if self.class.submit_tag
97
+ submit ? submit.to_s : Simplepay::Helpers::FormHelper.tag(:input, options)
98
+ end
99
+
100
+ def set_accessor_fields
101
+ self.access_key = Simplepay.aws_access_key_id if self.respond_to?(:access_key=)
102
+ self.account_id = Simplepay.account_id if self.respond_to?(:account_id=)
103
+ end
104
+
105
+ def set_fields(hash)
106
+ hash.each_pair do |key, value|
107
+ self.send("#{key}=", value) if self.respond_to?("#{key}=")
108
+ end
109
+ end
110
+
111
+ def set_signature
112
+ fields = {}
113
+ self.fields.each { |f| fields[f.service_name] = f.value unless f.service_name == 'signature' }
114
+ self.signature = Authentication.generate(fields) if self.respond_to?(:signature=)
115
+ end
116
+
117
+ end
118
+
119
+ end
120
+
121
+ require 'simplepay/services/subscription'
122
+ require 'simplepay/services/standard'