harleytt-simplepay 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (51) hide show
  1. data/History.txt +25 -0
  2. data/Manifest.txt +50 -0
  3. data/README.rdoc +127 -0
  4. data/Rakefile +25 -0
  5. data/lib/simplepay.rb +27 -0
  6. data/lib/simplepay/authentication.rb +41 -0
  7. data/lib/simplepay/constants.rb +64 -0
  8. data/lib/simplepay/errors.rb +16 -0
  9. data/lib/simplepay/helpers/form_helper.rb +29 -0
  10. data/lib/simplepay/helpers/notification_helper.rb +54 -0
  11. data/lib/simplepay/helpers/rails_helper.rb +40 -0
  12. data/lib/simplepay/rails.rb +9 -0
  13. data/lib/simplepay/service.rb +133 -0
  14. data/lib/simplepay/services/donation.rb +91 -0
  15. data/lib/simplepay/services/marketplace.rb +89 -0
  16. data/lib/simplepay/services/marketplace_policy.rb +54 -0
  17. data/lib/simplepay/services/standard.rb +58 -0
  18. data/lib/simplepay/services/subscription.rb +96 -0
  19. data/lib/simplepay/support.rb +16 -0
  20. data/lib/simplepay/support/amount.rb +55 -0
  21. data/lib/simplepay/support/billing_frequency.rb +14 -0
  22. data/lib/simplepay/support/boolean.rb +28 -0
  23. data/lib/simplepay/support/currency.rb +21 -0
  24. data/lib/simplepay/support/epoch.rb +39 -0
  25. data/lib/simplepay/support/field.rb +147 -0
  26. data/lib/simplepay/support/interval.rb +143 -0
  27. data/lib/simplepay/support/simple_amount.rb +37 -0
  28. data/lib/simplepay/support/subscription_period.rb +25 -0
  29. data/script/console +10 -0
  30. data/script/destroy +14 -0
  31. data/script/generate +14 -0
  32. data/simplepay.gemspec +41 -0
  33. data/test/simplepay/helpers/test_notifier.rb +32 -0
  34. data/test/simplepay/services/test_donation.rb +85 -0
  35. data/test/simplepay/services/test_marketplace.rb +85 -0
  36. data/test/simplepay/services/test_marketplace_policy.rb +52 -0
  37. data/test/simplepay/services/test_standard.rb +71 -0
  38. data/test/simplepay/services/test_subscription.rb +109 -0
  39. data/test/simplepay/support/test_amount.rb +46 -0
  40. data/test/simplepay/support/test_billing_frequency.rb +43 -0
  41. data/test/simplepay/support/test_boolean.rb +17 -0
  42. data/test/simplepay/support/test_epoch.rb +34 -0
  43. data/test/simplepay/support/test_field.rb +99 -0
  44. data/test/simplepay/support/test_interval.rb +92 -0
  45. data/test/simplepay/support/test_simple_amount.rb +28 -0
  46. data/test/simplepay/support/test_subscription_period.rb +49 -0
  47. data/test/simplepay/test_authentication.rb +25 -0
  48. data/test/simplepay/test_service.rb +118 -0
  49. data/test/test_helper.rb +87 -0
  50. data/test/test_simplepay.rb +11 -0
  51. metadata +184 -0
@@ -0,0 +1,40 @@
1
+ require 'action_view/base'
2
+
3
+ module Simplepay
4
+ module Helpers
5
+
6
+ ##
7
+ # Adds helpers to your views for generating the correct HTML forms and
8
+ # valid signatures.
9
+ #
10
+ module RailsHelper
11
+
12
+ ##
13
+ # This is the general interface for generating your Simple Pay service
14
+ # forms. See Simplepay::Services for available services and information
15
+ # specific to each.
16
+ #
17
+ # === Example
18
+ #
19
+ # (in your view)
20
+ #
21
+ # <%= simplepay_form_for(:service_name, {:attr => 'foo'}) %>
22
+ #
23
+ def simplepay_form_for(service_name, attributes = {}, submit_tag = nil)
24
+ service = get_simplepay_service(service_name)
25
+ service.form(attributes, submit_tag)
26
+ end
27
+
28
+
29
+ private
30
+
31
+
32
+ def get_simplepay_service(name) #:nodoc:
33
+ service = "Simplepay::Services::#{name.to_s.camelize}".constantize
34
+ service.new
35
+ end
36
+
37
+ end
38
+
39
+ end
40
+ end
@@ -0,0 +1,9 @@
1
+ require 'simplepay'
2
+ require 'simplepay/helpers/rails_helper'
3
+ require 'simplepay/helpers/notification_helper'
4
+
5
+ # Inject helper into Rails ActionView.
6
+ ActionView::Base.__send__(:include, Simplepay::Helpers::RailsHelper)
7
+
8
+ # Inject notification helper into ActionController
9
+ ActionController::Base.__send__(:include, Simplepay::Helpers::NotificationHelper)
@@ -0,0 +1,133 @@
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
+ # === Required Fields
8
+ #
9
+ # The following fields are required for all Simple Pay services:
10
+ #
11
+ # access_key:: Your Amazon Web Service (AWS) access key (automatically filled from Simplepay.aws_access_key_id).
12
+ # account_id:: Your Amazon Payments account identifier (automatically filled from Simplepay.account_id)
13
+ # 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)
14
+ #
15
+ class Service
16
+
17
+ # Fully-qualified URL for the production endpoint for the service.
18
+ ENDPOINT_URL = 'https://authorize.payments.amazon.com/pba/paypipeline'
19
+
20
+ # Fully-qualified URL for the sandbox (test) endpoint for the service.
21
+ SANDBOX_URL = 'https://authorize.payments-sandbox.amazon.com/pba/paypipeline'
22
+
23
+ class << self
24
+
25
+ ##
26
+ # Defines a field for the service.
27
+ #
28
+ # === Usage
29
+ #
30
+ # class Foo < Service
31
+ # field :access_key
32
+ # field :amount, :class => Amount, :map_to => :value
33
+ # end
34
+ #
35
+ def field(name, options = {})
36
+ field = Support::Field.new(self, name, options)
37
+ define_method("#{name.to_s.underscore}=", Proc.new { |value| self.fields.detect { |f| f.name == name }.value = value })
38
+ self.fields << field
39
+ field
40
+ end
41
+
42
+ ##
43
+ # Optional convenience method for:
44
+ #
45
+ # field :field_name, :required => true
46
+ #
47
+ # Any other +options+ given will be still be passed through.
48
+ #
49
+ def required_field(name, options = {})
50
+ field(name, options.merge(:required => true))
51
+ end
52
+
53
+ ##
54
+ # Returns the collection of fields defined by the service class.
55
+ #
56
+ def fields
57
+ @fields ||= []
58
+ end
59
+
60
+ def set_submit_tag(value)
61
+ @submit_tag = value
62
+ end
63
+
64
+ def submit_tag
65
+ @submit_tag
66
+ end
67
+
68
+ end
69
+
70
+
71
+ ##
72
+ # Returns the fields for the service instance.
73
+ #
74
+ def fields
75
+ @fields ||= self.class.fields.collect { |f| f.clone_for(self) }
76
+ end
77
+
78
+ ##
79
+ # Returns the URL for the service endpoint to use. If +sandbox+ is true,
80
+ # the SANDBOX_URL will be used. Otherwise, the ENDPOINT_URL will be used.
81
+ #
82
+ def url(sandbox = Simplepay.use_sandbox?)
83
+ sandbox ? self.class.const_get(:SANDBOX_URL) : self.class.const_get(:ENDPOINT_URL)
84
+ end
85
+
86
+ def form(attributes = {}, submit = nil)
87
+ set_accessor_fields
88
+ set_fields(attributes)
89
+ set_signature
90
+ content = generate_input_fields + generate_submit_field(submit)
91
+ Simplepay::Helpers::FormHelper.content_tag(:form, content, {:method => 'post', :action => url})
92
+ end
93
+
94
+
95
+ private
96
+
97
+
98
+ def generate_input_fields
99
+ self.fields.collect { |f| f.to_input }.join
100
+ end
101
+
102
+ def generate_submit_field(submit)
103
+ options = {:type => 'submit'}
104
+ options.merge!(:value => self.class.submit_tag) if self.class.submit_tag
105
+ submit ? submit.to_s : Simplepay::Helpers::FormHelper.tag(:input, options)
106
+ end
107
+
108
+ def set_accessor_fields
109
+ self.access_key = Simplepay.aws_access_key_id if self.respond_to?(:access_key=)
110
+ self.account_id = Simplepay.account_id if self.respond_to?(:account_id=)
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'
@@ -0,0 +1,91 @@
1
+ module Simplepay
2
+ module Services
3
+
4
+ ##
5
+ # The Amazon Simple Pay Donation service is used to facilitate the collection
6
+ # of donations to your organization, or another's organization via the
7
+ # marketplace functionality. When used through the marketplace functionality,
8
+ # you may charge a commission fee for brokering the exchange.
9
+ #
10
+ # Note that donation recipients must accept your marketplace fee policy before
11
+ # the payment buttons for their donations may be generated. This can be
12
+ # accomplished using the +MarketplacePolicy+ service with the form helper
13
+ # (see examples).
14
+ #
15
+ # === Simple Pay Donation Fields
16
+ #
17
+ # ==== Required Fields
18
+ #
19
+ # The following attributes are required when generating a Simple Pay Donation
20
+ # form (in addition to those listed in +SimplePay::Service+):
21
+ #
22
+ # amount:: The dollar value you'd like to collect
23
+ #
24
+ # ==== Optional Fields
25
+ #
26
+ # immediate_return:: Immediately returns the customer to your +return_url+ directly after payment.
27
+ # ipn_url:: Fully-qualified URL to which Amazon will POST instant payment notifications.
28
+ # process_immediately:: Instructs Amazon to immediately process the payment.
29
+ # reference_id:: A custom string your can set to identify this transaction, it will be returned with the IPNs and other returned data.
30
+ # return_url:: Fully-qualified URL for where to send your customer following payment.
31
+ #
32
+ # ===== Marketplace Fields
33
+ #
34
+ # recipient_email:: The email address of the donation recipient (important and must be correct)
35
+ # fixed_marketplace_fee:: The fixed marketplace fee to add to each transaction
36
+ # variable_marketplace_fee:: The variable marketplace fee to add to each transaction
37
+ #
38
+ # === Example
39
+ #
40
+ # (in your view, sellers need to accept the marketplace fee policy using the form helper)
41
+ #
42
+ # <%= simplepay_form_for(:marketplace_policy, {
43
+ # :max_fixed_fee => 10.00,
44
+ # :max_variable_fee => 5,
45
+ # :return_url => 'http://yourservice.com'
46
+ # }) %>
47
+ #
48
+ # (in your view, payment form generated for end users using the form helper)
49
+ #
50
+ # <%= simplepay_form_for(:donation, {
51
+ # :amount => 34.95,
52
+ # :description => "Mutual profit!",
53
+ # :recipient_email => 'seller@gmail.com',
54
+ # :fixed_marketplace_fee => 10.00,
55
+ # :variable_marketplace_fee => 5
56
+ # }) %>
57
+ #
58
+ class Donation < Service
59
+
60
+ required_field :access_key
61
+ required_field :signature
62
+ required_field :account_id, :as => :amazon_payments_account_id
63
+ required_field :signature_method, :value => 'HmacSHA256'
64
+ required_field :signature_version, :value => '2'
65
+
66
+
67
+ required_field :description
68
+ required_field :amount, :class => Support::Amount
69
+ required_field :cobranding_style, :value => 'logo'
70
+ required_field :donation_widget, :as => :is_donation_widget,
71
+ :value => '1'
72
+
73
+ field :reference_id
74
+ field :immediate_return, :class => Support::Boolean
75
+ field :collect_shipping_address, :class => Support::Boolean
76
+ field :process_immediately, :class => Support::Boolean,
77
+ :as => :process_immediate
78
+
79
+ field :return_url
80
+ field :ipn_url
81
+ field :abandon_url
82
+
83
+ # Marketplace inputs (recipient email required when using)
84
+ field :fixed_marketplace_fee, :class => Support::Amount
85
+ field :variable_marketplace_fee
86
+ field :recipient_email
87
+
88
+ end
89
+
90
+ end
91
+ end
@@ -0,0 +1,89 @@
1
+ module Simplepay
2
+ module Services
3
+
4
+ ##
5
+ # The Amazon Simple Pay Marketplace service is used to facilitate payments for others.
6
+ # Use it to charge a commission fee for brokering the exchange between buyers and sellers.
7
+ #
8
+ # Note that sellers must accept your marketplace fee policy before the payment buttons
9
+ # for their products can be generated. This can be accomplished using the +MarketplacePolicy+
10
+ # service with the form helper (see examples).
11
+ #
12
+ # === Simple Pay Marketplace Fields
13
+ #
14
+ # ==== Required Fields
15
+ #
16
+ # The following attributes are required when creating a Simple Pay
17
+ # Marketplace form (in addition to those listed in +Simplepay::Service+):
18
+ #
19
+ # amount:: The dollar value you'd like to collect.
20
+ # description:: A summary of the reason for the payment, this is displayed to your customer during checkout.
21
+ # recipient_email:: The e-mail address of the seller (important and must be correct).
22
+ # fixed_marketplace_fee:: The fixed marketplace fee to add to each transaction.
23
+ # variable_marketplace_fee:: The variable percentage fee to add to each transaction.
24
+ #
25
+ # ==== Optional Fields
26
+ #
27
+ # abandon_url:: The fully-qualified URL to send your custom if they cancel during payment.
28
+ # cobranding_style:: Defines the type of cobranding to use during the checkout process.
29
+ # collect_shipping_address:: Tells Amazon whether or not to ask for shipping address and contact information.
30
+ # immediate_return:: Immediately returns the customer to your +return_url+ directly after payment.
31
+ # ipn_url:: Fully-qualified URL to which Amazon will POST instant payment notifications.
32
+ # process_immediately:: Instructs Amazon to immediately process the payment.
33
+ # reference_id:: A custom string your can set to identify this transaction, it will be returned with the IPNs and other returned data.
34
+ # return_url:: Fully-qualified URL for where to send your customer following payment.
35
+ #
36
+ # === Example
37
+ #
38
+ # (in your view, sellers need to accept the marketplace fee policy using the form helper)
39
+ #
40
+ # <%= simplepay_form_for(:marketplace_policy, {
41
+ # :max_fixed_fee => 10.00,
42
+ # :max_variable_fee => 5,
43
+ # :return_url => 'http://yourservice.com'
44
+ # }) %>
45
+ #
46
+ # (in your view, payment form generated for end users using the form helper)
47
+ #
48
+ # <%= simplepay_form_for(:standard, {
49
+ # :amount => 34.95,
50
+ # :description => "Mutual profit!",
51
+ # :recipient_email => 'seller@gmail.com',
52
+ # :fixed_marketplace_fee => 10.00,
53
+ # :variable_marketplace_fee => 5
54
+ # }) %>
55
+ #
56
+ class Marketplace < Service
57
+
58
+ required_field :access_key
59
+ required_field :signature
60
+ required_field :account_id, :as => :amazon_payments_account_id
61
+ required_field :signature_method, :value => 'HmacSHA256'
62
+ required_field :signature_version, :value => '2'
63
+
64
+
65
+ required_field :description
66
+ required_field :amount, :class => Support::Amount
67
+ required_field :fixed_marketplace_fee, :class => Support::Amount
68
+ required_field :variable_marketplace_fee
69
+ required_field :cobranding_style, :value => 'logo'
70
+ required_field :recipient_email
71
+
72
+ field :reference_id
73
+ field :immediate_return, :class => Support::Boolean
74
+ field :collect_shipping_address, :class => Support::Boolean
75
+ field :process_immediately, :class => Support::Boolean,
76
+ :as => :process_immediate
77
+
78
+ field :return_url
79
+ field :ipn_url
80
+ field :abandon_url
81
+
82
+ # These fields are not currently utilized by the service
83
+ field :donation_widget, :as => :is_donation_widget,
84
+ :value => '0'
85
+
86
+ end
87
+
88
+ end
89
+ end
@@ -0,0 +1,54 @@
1
+ module Simplepay
2
+ module Services
3
+
4
+ ##
5
+ # The Amazon Simple Pay Marketplace Policy service is used to allow sellers to acknowledge marketplace policy fees.
6
+ # Only once a set policy has been agreed to will marketplace transactions be able to proceed.
7
+ #
8
+ # === Simple Pay Marketplace Policy Fields
9
+ #
10
+ # ==== Required Fields
11
+ #
12
+ # The following attributes are required when creating a Simple Pay Marketplace policy fee acceptance form
13
+ # (in addition to those listed in +Simplepay::Service+):
14
+ #
15
+ # max_fixed_fee:: The maximum fixed fee that will be appended to transactions.
16
+ # max_variable_fee:: The maximum variable fee (%) that will be calculated and added to transactions.
17
+ # return_url:: Fully-qualified URL for where to send they buyer following payment.
18
+ # reference_id:: A custom string used to identify this transaction, it will be returned with return data.
19
+ #
20
+ # === Example
21
+ #
22
+ # (in your view, using the form helper)
23
+ #
24
+ # <%= simplepay_form_for(:marketplace_policy, {
25
+ # :max_fixed_fee => 10.00,
26
+ # :max_variable_fee => 5,
27
+ # :return_url => 'http://yourservice.com',
28
+ # :reference_id => '123456789'
29
+ # }) %>
30
+ #
31
+ class MarketplacePolicy < Service
32
+
33
+ required_field :access_key, :as => :caller_key
34
+ required_field :signature, :as => :aws_signature
35
+ required_field :account_id, :as => :caller_account_id
36
+ required_field :signature_method, :value => 'HmacSHA256'
37
+ required_field :signature_version, :value => '2'
38
+
39
+
40
+ required_field :max_fixed_fee
41
+ required_field :max_variable_fee
42
+ required_field :return_url
43
+
44
+ required_field :reference_id, :as => :caller_reference
45
+ required_field :collect_email_address, :value => 'True'
46
+ required_field :pipeline_name, :value => 'Recipient'
47
+
48
+ # These fields are not currently utilized by the service
49
+ required_field :recipient_pays_fee, :value => 'True'
50
+
51
+ end
52
+
53
+ end
54
+ end
@@ -0,0 +1,58 @@
1
+ module Simplepay
2
+ module Services
3
+
4
+ ##
5
+ # The Amazon Simple Pay Standard service is used for one-time payments.
6
+ #
7
+ # === Simple Pay Standard Fields
8
+ #
9
+ # ==== Required Fields
10
+ #
11
+ # The following attributes are required when creating a Simple Pay
12
+ # Standard form (in addition to those listed in +Simplepay::Service+):
13
+ #
14
+ # amount:: The dollar value you'd like to collect.
15
+ # description:: A summary of the reason for the payment, this is displayed to your customer during checkout.
16
+ #
17
+ # ==== Optional Fields
18
+ #
19
+ # abandon_url:: The fully-qualified URL to send your custom if they cancel during payment.
20
+ # cobranding_style:: Defines the type of cobranding to use during the checkout process.
21
+ # collect_shipping_address:: Tells Amazon whether or not to ask for shipping address and contact information.
22
+ # immediate_return:: Immediately returns the customer to your +return_url+ directly after payment.
23
+ # ipn_url:: Fully-qualified URL to which Amazon will POST instant payment notifications.
24
+ # process_immediately:: Instructs Amazon to immediately process the payment.
25
+ # reference_id:: A custom string your can set to identify this transaction, it will be returned with the IPNs and other returned data.
26
+ # return_url:: Fully-qualified URL for where to send your customer following payment.
27
+ #
28
+ # === Example
29
+ #
30
+ # (in your view, using the form helper)
31
+ #
32
+ # <%= simplepay_form_for(:standard, {
33
+ # :amount => 34.95,
34
+ # :description => "I want my money NOW!"
35
+ # }) %>
36
+ #
37
+ class Standard < Service
38
+
39
+ required_field :access_key
40
+ required_field :amount
41
+ required_field :description
42
+ required_field :signature
43
+ required_field :signature_method, :value => 'HmacSHA256'
44
+ required_field :signature_version, :value => '2'
45
+
46
+ field :abandon_url
47
+ field :cobranding_style, :value => 'logo'
48
+ field :collect_shipping_address, :class => Support::Boolean
49
+ field :immediate_return, :class => Support::Boolean
50
+ field :ipn_url
51
+ field :process_immediate, :class => Support::Boolean
52
+ field :reference_id
53
+ field :return_url
54
+
55
+ end
56
+
57
+ end
58
+ end